1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <cstring>
16 #include <string>
17
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 #include "absl/log/log.h"
21 #include "absl/memory/memory.h"
22 #include "absl/status/status.h"
23 #include "absl/status/statusor.h"
24 #include "absl/strings/string_view.h"
25 #include "sandboxed_api/examples/stringop/stringop_params.pb.h"
26 #include "sandboxed_api/transaction.h"
27 #include "sandboxed_api/util/status_macros.h"
28 #include "sandboxed_api/util/status_matchers.h"
29 #include "sandboxed_api/vars.h"
30
31 #include "sandboxed_api/examples/stringop/stringop-sapi.sapi.h"
32
33 namespace {
34
35 using ::sapi::IsOk;
36 using ::testing::Eq;
37 using ::testing::Ne;
38 using ::testing::SizeIs;
39 using ::testing::StrEq;
40
41 // Tests using a simple transaction (and function pointers):
TEST(StringopTest,ProtobufStringDuplication)42 TEST(StringopTest, ProtobufStringDuplication) {
43 sapi::BasicTransaction st(absl::make_unique<StringopSandbox>());
44 EXPECT_THAT(st.Run([](sapi::Sandbox* sandbox) -> absl::Status {
45 StringopApi api(sandbox);
46 stringop::StringDuplication proto;
47 proto.set_input("Hello");
48 auto pp = sapi::v::Proto<stringop::StringDuplication>::FromMessage(proto);
49 if (!pp.ok()) {
50 return pp.status();
51 }
52 {
53 SAPI_ASSIGN_OR_RETURN(int return_value,
54 api.pb_duplicate_string(pp->PtrBoth()));
55 TRANSACTION_FAIL_IF_NOT(return_value, "pb_duplicate_string() failed");
56 }
57
58 SAPI_ASSIGN_OR_RETURN(auto pb_result, pp->GetMessage());
59 LOG(INFO) << "Result PB: " << pb_result;
60 TRANSACTION_FAIL_IF_NOT(pb_result.output() == "HelloHello",
61 "Incorrect output");
62 return absl::OkStatus();
63 }),
64 IsOk());
65 }
66
TEST(StringopTest,ProtobufStringReversal)67 TEST(StringopTest, ProtobufStringReversal) {
68 StringopSandbox sandbox;
69 ASSERT_THAT(sandbox.Init(), IsOk());
70 StringopApi api(&sandbox);
71
72 stringop::StringReverse proto;
73 proto.set_input("Hello");
74 auto pp = sapi::v::Proto<stringop::StringReverse>::FromMessage(proto);
75 SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
76 api.pb_reverse_string(pp->PtrBoth()));
77 EXPECT_THAT(return_value, Ne(0)) << "pb_reverse_string() failed";
78
79 SAPI_ASSERT_OK_AND_ASSIGN(auto pb_result, pp->GetMessage());
80 LOG(INFO) << "Result PB: " << pb_result;
81 EXPECT_THAT(pb_result.output(), StrEq("olleH"));
82 }
83
TEST(StringopTest,RawStringDuplication)84 TEST(StringopTest, RawStringDuplication) {
85 StringopSandbox sandbox;
86 ASSERT_THAT(sandbox.Init(), IsOk());
87 StringopApi api(&sandbox);
88
89 sapi::v::LenVal param("0123456789", 10);
90 SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
91 api.duplicate_string(param.PtrBoth()));
92 EXPECT_THAT(return_value, Eq(1)) << "duplicate_string() failed";
93
94 absl::string_view data(reinterpret_cast<const char*>(param.GetData()),
95 param.GetDataSize());
96 EXPECT_THAT(data, SizeIs(20))
97 << "duplicate_string() did not return enough data";
98 EXPECT_THAT(std::string(data), StrEq("01234567890123456789"));
99 }
100
TEST(StringopTest,RawStringReversal)101 TEST(StringopTest, RawStringReversal) {
102 StringopSandbox sandbox;
103 ASSERT_THAT(sandbox.Init(), IsOk());
104 StringopApi api(&sandbox);
105
106 sapi::v::LenVal param("0123456789", 10);
107 {
108 SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
109 api.reverse_string(param.PtrBoth()));
110 EXPECT_THAT(return_value, Eq(1))
111 << "reverse_string() returned incorrect value";
112 absl::string_view data(reinterpret_cast<const char*>(param.GetData()),
113 param.GetDataSize());
114 EXPECT_THAT(param.GetDataSize(), Eq(10))
115 << "reverse_string() did not return enough data";
116 EXPECT_THAT(std::string(data), StrEq("9876543210"))
117 << "reverse_string() did not return the expected data";
118 }
119 {
120 // Let's call it again with different data as argument, reusing the
121 // existing LenVal object.
122 EXPECT_THAT(param.ResizeData(sandbox.rpc_channel(), 16), IsOk());
123 memcpy(param.GetData() + 10, "ABCDEF", 6);
124 absl::string_view data(reinterpret_cast<const char*>(param.GetData()),
125 param.GetDataSize());
126 EXPECT_THAT(data, SizeIs(16)) << "Resize did not behave correctly";
127 EXPECT_THAT(std::string(data), StrEq("9876543210ABCDEF"));
128
129 SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
130 api.reverse_string(param.PtrBoth()));
131 EXPECT_THAT(return_value, Eq(1))
132 << "reverse_string() returned incorrect value";
133 data = absl::string_view(reinterpret_cast<const char*>(param.GetData()),
134 param.GetDataSize());
135 EXPECT_THAT(std::string(data), StrEq("FEDCBA0123456789"));
136 }
137 }
138
TEST(StringopTest,RawStringLength)139 TEST(StringopTest, RawStringLength) {
140 StringopSandbox sandbox;
141 ASSERT_THAT(sandbox.Init(), IsOk());
142 StringopApi api(&sandbox);
143 SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string());
144 SAPI_ASSERT_OK_AND_ASSIGN(size_t len,
145 sandbox.rpc_channel()->Strlen(target_mem_ptr));
146 EXPECT_THAT(len, Eq(10));
147 }
148
TEST(StringopTest,RawStringReading)149 TEST(StringopTest, RawStringReading) {
150 StringopSandbox sandbox;
151 ASSERT_THAT(sandbox.Init(), IsOk());
152 StringopApi api(&sandbox);
153 SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string());
154 SAPI_ASSERT_OK_AND_ASSIGN(size_t len,
155 sandbox.rpc_channel()->Strlen(target_mem_ptr));
156 EXPECT_THAT(len, Eq(10));
157
158 SAPI_ASSERT_OK_AND_ASSIGN(
159 std::string data, sandbox.GetCString(sapi::v::RemotePtr(target_mem_ptr)));
160 EXPECT_THAT(data, StrEq("Ten chars."));
161 }
162
163 } // namespace
164