Skip to content

Commit d22b369

Browse files
cblichmanncopybara-github
authored andcommitted
Add a MakeSandbox factory function
The new `sapi::MakeSandbox` function simplifies the creation and initialization of Sandbox instances by returning a `absl::StatusOr<std::unique_ptr<SandboxT>>`. The stringop example is updated to demonstrate the use of this new factory function. PiperOrigin-RevId: 909993578 Change-Id: I900ce6e708ae474bf4cc4e26c89c0060dd03a739
1 parent a30d18d commit d22b369

2 files changed

Lines changed: 36 additions & 19 deletions

File tree

sandboxed_api/examples/stringop/main_stringop.cc

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <cstring>
16+
#include <memory>
1617
#include <string>
1718

1819
#include "gmock/gmock.h"
@@ -67,9 +68,9 @@ TEST(StringopTest, ProtobufStringDuplication) {
6768
}
6869

6970
TEST(StringopTest, ProtobufStringReversal) {
70-
StringopSandbox sandbox;
71-
ASSERT_THAT(sandbox.Init(), IsOk());
72-
StringopApi api(&sandbox);
71+
SAPI_ASSERT_OK_AND_ASSIGN(std::unique_ptr<StringopSandbox> sandbox,
72+
sapi::MakeSandbox<StringopSandbox>());
73+
StringopApi api(sandbox.get());
7374

7475
stringop::StringReverse proto;
7576
proto.set_input("Hello");
@@ -84,9 +85,9 @@ TEST(StringopTest, ProtobufStringReversal) {
8485
}
8586

8687
TEST(StringopTest, RawStringDuplication) {
87-
StringopSandbox sandbox;
88-
ASSERT_THAT(sandbox.Init(), IsOk());
89-
StringopApi api(&sandbox);
88+
SAPI_ASSERT_OK_AND_ASSIGN(std::unique_ptr<StringopSandbox> sandbox,
89+
sapi::MakeSandbox<StringopSandbox>());
90+
StringopApi api(sandbox.get());
9091

9192
sapi::v::LenVal param("0123456789", 10);
9293
SAPI_ASSERT_OK_AND_ASSIGN(int return_value,
@@ -101,9 +102,9 @@ TEST(StringopTest, RawStringDuplication) {
101102
}
102103

103104
TEST(StringopTest, RawStringReversal) {
104-
StringopSandbox sandbox;
105-
ASSERT_THAT(sandbox.Init(), IsOk());
106-
StringopApi api(&sandbox);
105+
SAPI_ASSERT_OK_AND_ASSIGN(std::unique_ptr<StringopSandbox> sandbox,
106+
sapi::MakeSandbox<StringopSandbox>());
107+
StringopApi api(sandbox.get());
107108

108109
sapi::v::LenVal param("0123456789", 10);
109110
{
@@ -121,7 +122,7 @@ TEST(StringopTest, RawStringReversal) {
121122
{
122123
// Let's call it again with different data as argument, reusing the
123124
// existing LenVal object.
124-
EXPECT_THAT(param.ResizeData(sandbox.rpc_channel(), 16), IsOk());
125+
EXPECT_THAT(param.ResizeData(sandbox->rpc_channel(), 16), IsOk());
125126
memcpy(param.GetData() + 10, "ABCDEF", 6);
126127
absl::string_view data(reinterpret_cast<const char*>(param.GetData()),
127128
param.GetDataSize());
@@ -139,26 +140,27 @@ TEST(StringopTest, RawStringReversal) {
139140
}
140141

141142
TEST(StringopTest, RawStringLength) {
142-
StringopSandbox sandbox;
143-
ASSERT_THAT(sandbox.Init(), IsOk());
144-
StringopApi api(&sandbox);
143+
SAPI_ASSERT_OK_AND_ASSIGN(std::unique_ptr<StringopSandbox> sandbox,
144+
sapi::MakeSandbox<StringopSandbox>());
145+
StringopApi api(sandbox.get());
145146
SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string());
146147
SAPI_ASSERT_OK_AND_ASSIGN(size_t len,
147-
sandbox.rpc_channel()->Strlen(target_mem_ptr));
148+
sandbox->rpc_channel()->Strlen(target_mem_ptr));
148149
EXPECT_THAT(len, Eq(10));
149150
}
150151

151152
TEST(StringopTest, RawStringReading) {
152-
StringopSandbox sandbox;
153-
ASSERT_THAT(sandbox.Init(), IsOk());
154-
StringopApi api(&sandbox);
153+
SAPI_ASSERT_OK_AND_ASSIGN(std::unique_ptr<StringopSandbox> sandbox,
154+
sapi::MakeSandbox<StringopSandbox>());
155+
StringopApi api(sandbox.get());
155156
SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string());
156157
SAPI_ASSERT_OK_AND_ASSIGN(size_t len,
157-
sandbox.rpc_channel()->Strlen(target_mem_ptr));
158+
sandbox->rpc_channel()->Strlen(target_mem_ptr));
158159
EXPECT_THAT(len, Eq(10));
159160

160161
SAPI_ASSERT_OK_AND_ASSIGN(
161-
std::string data, sandbox.GetCString(sapi::v::RemotePtr(target_mem_ptr)));
162+
std::string data,
163+
sandbox->GetCString(sapi::v::RemotePtr(target_mem_ptr)));
162164
EXPECT_THAT(data, StrEq("Ten chars."));
163165
}
164166

sandboxed_api/sandbox.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <initializer_list>
2121
#include <memory>
2222
#include <string>
23+
#include <type_traits>
2324
#include <utility>
2425

2526
#include "absl/base/attributes.h"
@@ -33,12 +34,14 @@
3334
#include "sandboxed_api/rpcchannel.h"
3435
#include "sandboxed_api/sandbox2/notify.h"
3536
#include "sandboxed_api/sandbox_config.h"
37+
#include "sandboxed_api/util/status_macros.h"
3638
#include "sandboxed_api/var_abstract.h"
3739
#include "sandboxed_api/var_reg.h"
3840
#include "sandboxed_api/vars.h"
3941

4042
namespace sapi {
4143
namespace internal {
44+
4245
class PtrOrCallable {
4346
public:
4447
explicit PtrOrCallable(v::Callable* callable) : callable_(callable) {}
@@ -55,6 +58,7 @@ class PtrOrCallable {
5558
v::Callable* callable_ = nullptr;
5659
v::Ptr* ptr_ = nullptr;
5760
};
61+
5862
} // namespace internal
5963

6064
// The Sandbox class represents the sandboxed library. It provides users with
@@ -227,6 +231,17 @@ template <typename Backend>
227231
using SandboxImpl [[deprecated("Use Sandbox instead")]] ABSL_REFACTOR_INLINE =
228232
Sandbox<Backend>;
229233

234+
// Factory function that creates and initializes a new sandbox instance.
235+
// SandboxT must be a subclass of SandboxBase.
236+
template <typename SandboxT, class... Args>
237+
absl::StatusOr<std::unique_ptr<SandboxT>> MakeSandbox(Args&&... args) {
238+
static_assert(std::is_base_of_v<SandboxBase, SandboxT>,
239+
"SandboxT must be a subclass of SandboxBase");
240+
auto sandbox = std::make_unique<SandboxT>(std::forward<Args>(args)...);
241+
SAPI_RETURN_IF_ERROR(sandbox->Init());
242+
return sandbox;
243+
}
244+
230245
} // namespace sapi
231246

232247
#endif // SANDBOXED_API_SANDBOX_H_

0 commit comments

Comments
 (0)