2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2019-06-05 15:25:50 +08:00
|
|
|
#include "sandboxed_api/util/flag.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/memory/memory.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/time/time.h"
|
|
|
|
#include "sandboxed_api/examples/stringop/lib/sandbox.h"
|
|
|
|
#include "sandboxed_api/examples/stringop/lib/stringop-sapi.sapi.h"
|
|
|
|
#include "sandboxed_api/examples/stringop/lib/stringop_params.pb.h"
|
|
|
|
#include "sandboxed_api/transaction.h"
|
2021-01-22 22:01:05 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
#include "sandboxed_api/vars.h"
|
|
|
|
|
|
|
|
using ::sapi::IsOk;
|
2019-09-13 17:28:09 +08:00
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::Ne;
|
|
|
|
using ::testing::SizeIs;
|
|
|
|
using ::testing::StrEq;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-09-13 17:28:09 +08:00
|
|
|
// Tests using a simple transaction (and function pointers):
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(StringopTest, ProtobufStringDuplication) {
|
|
|
|
sapi::BasicTransaction st(absl::make_unique<StringopSapiSandbox>());
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(st.Run([](sapi::Sandbox* sandbox) -> absl::Status {
|
2019-09-13 17:28:09 +08:00
|
|
|
StringopApi api(sandbox);
|
2019-03-19 00:21:48 +08:00
|
|
|
stringop::StringDuplication proto;
|
|
|
|
proto.set_input("Hello");
|
|
|
|
sapi::v::Proto<stringop::StringDuplication> pp(proto);
|
2019-09-13 17:28:09 +08:00
|
|
|
{
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(int return_value,
|
|
|
|
api.pb_duplicate_string(pp.PtrBoth()));
|
2019-09-13 17:28:09 +08:00
|
|
|
TRANSACTION_FAIL_IF_NOT(return_value, "pb_duplicate_string() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto pb_result, pp.GetMessage());
|
|
|
|
LOG(INFO) << "Result PB: " << pb_result.DebugString();
|
|
|
|
TRANSACTION_FAIL_IF_NOT(pb_result.output() == "HelloHello",
|
2019-03-19 00:21:48 +08:00
|
|
|
"Incorrect output");
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}),
|
|
|
|
IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringopTest, ProtobufStringReversal) {
|
2019-09-13 17:28:09 +08:00
|
|
|
StringopSapiSandbox sandbox;
|
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk());
|
|
|
|
StringopApi api(&sandbox);
|
|
|
|
|
|
|
|
stringop::StringReverse proto;
|
|
|
|
proto.set_input("Hello");
|
|
|
|
sapi::v::Proto<stringop::StringReverse> pp(proto);
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
|
|
|
|
api.pb_reverse_string(pp.PtrBoth()));
|
2019-09-13 17:28:09 +08:00
|
|
|
EXPECT_THAT(return_value, Ne(0)) << "pb_reverse_string() failed";
|
|
|
|
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(auto pb_result, pp.GetMessage());
|
|
|
|
LOG(INFO) << "Result PB: " << pb_result.DebugString();
|
|
|
|
EXPECT_THAT(pb_result.output(), StrEq("olleH"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringopTest, RawStringDuplication) {
|
2019-09-13 17:28:09 +08:00
|
|
|
StringopSapiSandbox sandbox;
|
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk());
|
|
|
|
StringopApi api(&sandbox);
|
|
|
|
|
|
|
|
sapi::v::LenVal param("0123456789", 10);
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
|
|
|
|
api.duplicate_string(param.PtrBoth()));
|
2019-09-13 17:28:09 +08:00
|
|
|
EXPECT_THAT(return_value, Eq(1)) << "duplicate_string() failed";
|
|
|
|
|
|
|
|
absl::string_view data(reinterpret_cast<const char*>(param.GetData()),
|
|
|
|
param.GetDataSize());
|
|
|
|
EXPECT_THAT(data, SizeIs(20))
|
|
|
|
<< "duplicate_string() did not return enough data";
|
|
|
|
EXPECT_THAT(std::string(data), StrEq("01234567890123456789"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringopTest, RawStringReversal) {
|
2019-09-13 17:28:09 +08:00
|
|
|
StringopSapiSandbox sandbox;
|
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk());
|
|
|
|
StringopApi api(&sandbox);
|
|
|
|
|
|
|
|
sapi::v::LenVal param("0123456789", 10);
|
|
|
|
{
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
|
|
|
|
api.reverse_string(param.PtrBoth()));
|
2019-09-13 17:28:09 +08:00
|
|
|
EXPECT_THAT(return_value, Eq(1))
|
|
|
|
<< "reverse_string() returned incorrect value";
|
|
|
|
absl::string_view data(reinterpret_cast<const char*>(param.GetData()),
|
|
|
|
param.GetDataSize());
|
|
|
|
EXPECT_THAT(param.GetDataSize(), Eq(10))
|
|
|
|
<< "reverse_string() did not return enough data";
|
|
|
|
EXPECT_THAT(std::string(data), StrEq("9876543210"))
|
|
|
|
<< "reverse_string() did not return the expected data";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Let's call it again with different data as argument, reusing the
|
|
|
|
// existing LenVal object.
|
2020-08-07 15:30:04 +08:00
|
|
|
EXPECT_THAT(param.ResizeData(sandbox.rpc_channel(), 16), IsOk());
|
2019-09-13 17:28:09 +08:00
|
|
|
memcpy(param.GetData() + 10, "ABCDEF", 6);
|
|
|
|
absl::string_view data(reinterpret_cast<const char*>(param.GetData()),
|
|
|
|
param.GetDataSize());
|
|
|
|
EXPECT_THAT(data, SizeIs(16)) << "Resize did not behave correctly";
|
|
|
|
EXPECT_THAT(std::string(data), StrEq("9876543210ABCDEF"));
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
|
|
|
|
api.reverse_string(param.PtrBoth()));
|
2019-09-13 17:28:09 +08:00
|
|
|
EXPECT_THAT(return_value, Eq(1))
|
|
|
|
<< "reverse_string() returned incorrect value";
|
|
|
|
data = absl::string_view(reinterpret_cast<const char*>(param.GetData()),
|
2019-03-19 00:21:48 +08:00
|
|
|
param.GetDataSize());
|
2019-09-13 17:28:09 +08:00
|
|
|
EXPECT_THAT(std::string(data), StrEq("FEDCBA0123456789"));
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 22:44:03 +08:00
|
|
|
TEST(StringopTest, RawStringLength) {
|
|
|
|
StringopSapiSandbox sandbox;
|
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk());
|
|
|
|
StringopApi api(&sandbox);
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string());
|
2020-09-18 22:44:34 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(size_t len,
|
2021-01-22 22:01:05 +08:00
|
|
|
sandbox.rpc_channel()->Strlen(target_mem_ptr));
|
2019-11-22 22:44:03 +08:00
|
|
|
EXPECT_THAT(len, Eq(10));
|
|
|
|
}
|
|
|
|
|
2019-11-22 22:59:37 +08:00
|
|
|
TEST(StringopTest, RawStringReading) {
|
|
|
|
StringopSapiSandbox sandbox;
|
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk());
|
|
|
|
StringopApi api(&sandbox);
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string());
|
2020-09-18 22:44:34 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(size_t len,
|
2021-01-22 22:01:05 +08:00
|
|
|
sandbox.rpc_channel()->Strlen(target_mem_ptr));
|
2019-11-22 22:59:37 +08:00
|
|
|
EXPECT_THAT(len, Eq(10));
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
std::string data, sandbox.GetCString(sapi::v::RemotePtr(target_mem_ptr)));
|
2019-11-22 22:59:37 +08:00
|
|
|
EXPECT_THAT(data, StrEq("Ten chars."));
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|