xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/buffer_test.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 "sandboxed_api/sandbox2/buffer.h"
16 
17 #include <sys/stat.h>
18 #include <unistd.h>
19 
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "gmock/gmock.h"
27 #include "gtest/gtest.h"
28 #include "sandboxed_api/sandbox2/executor.h"
29 #include "sandboxed_api/sandbox2/ipc.h"
30 #include "sandboxed_api/sandbox2/policy.h"
31 #include "sandboxed_api/sandbox2/result.h"
32 #include "sandboxed_api/sandbox2/sandbox2.h"
33 #include "sandboxed_api/testing.h"
34 #include "sandboxed_api/util/status_matchers.h"
35 
36 namespace sandbox2 {
37 namespace {
38 
39 using ::sapi::CreateDefaultPermissiveTestPolicy;
40 using ::sapi::GetTestSourcePath;
41 using ::testing::Eq;
42 using ::testing::Ne;
43 
44 // Test all public methods of sandbox2::Buffer.
TEST(BufferTest,TestImplementation)45 TEST(BufferTest, TestImplementation) {
46   constexpr int kSize = 1024;
47   SAPI_ASSERT_OK_AND_ASSIGN(auto buffer, Buffer::CreateWithSize(kSize));
48   EXPECT_THAT(buffer->size(), Eq(kSize));
49   uint8_t* raw_buf = buffer->data();
50   for (int i = 0; i < kSize; i++) {
51     raw_buf[i] = 'X';
52   }
53   SAPI_ASSERT_OK_AND_ASSIGN(auto buffer2, Buffer::CreateFromFd(buffer->fd()));
54   uint8_t* raw_buf2 = buffer2->data();
55   for (int i = 0; i < kSize; i++) {
56     EXPECT_THAT(raw_buf2[i], Eq('X'));
57   }
58 }
59 
60 // Test sharing of buffer between executor/sandboxee using dup/MapFd.
TEST(BufferTest,TestWithSandboxeeMapFd)61 TEST(BufferTest, TestWithSandboxeeMapFd) {
62   const std::string path = GetTestSourcePath("sandbox2/testcases/buffer");
63   std::vector<std::string> args = {path};
64   auto executor = std::make_unique<Executor>(path, args);
65   SAPI_ASSERT_OK_AND_ASSIGN(auto policy,
66                             CreateDefaultPermissiveTestPolicy(path).TryBuild());
67 
68   SAPI_ASSERT_OK_AND_ASSIGN(auto buffer,
69                             Buffer::CreateWithSize(1ULL << 20 /* 1MiB */));
70   // buffer() uses the internal fd to mmap the buffer.
71   uint8_t* buf = buffer->data();
72   // Test that we can write data to the sandboxee.
73   buf[0] = 'A';
74 
75   // Map buffer as fd 3, but careful because MapFd closes the buffer fd and
76   // we need to keep it since buffer uses it for mmap, so we must dup.
77   executor->ipc()->MapFd(dup(buffer->fd()), 3);
78 
79   Sandbox2 s2(std::move(executor), std::move(policy));
80   auto result = s2.Run();
81 
82   EXPECT_THAT(result.final_status(), Eq(Result::OK));
83   EXPECT_THAT(result.reason_code(), Eq(0));
84 
85   // Test that we can read data from the sandboxee.
86   EXPECT_THAT(buf[buffer->size() - 1], Eq('B'));
87 
88   // Test that internal buffer fd remains valid.
89   struct stat stat_buf;
90   EXPECT_THAT(fstat(buffer->fd(), &stat_buf), Ne(-1));
91 }
92 }  // namespace
93 }  // namespace sandbox2
94