xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/syscall_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/syscall.h"
16 
17 #include <syscall.h>
18 
19 #include <array>
20 #include <vector>
21 
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "absl/strings/str_cat.h"
25 #include "sandboxed_api/config.h"
26 
27 using ::testing::Eq;
28 using ::testing::StartsWith;
29 
30 namespace sandbox2 {
31 namespace {
32 
TEST(SyscallTest,Basic)33 TEST(SyscallTest, Basic) {
34   Syscall::Args args{1, 0xbadbeef, 5};
35   Syscall syscall(Syscall::GetHostArch(), __NR_read, args);
36 
37   EXPECT_THAT(syscall.pid(), Eq(-1));
38   EXPECT_THAT(syscall.arch(), Eq(Syscall::GetHostArch()));
39   EXPECT_THAT(syscall.nr(), Eq(__NR_read));
40   EXPECT_THAT(syscall.args(), Eq(args));
41   EXPECT_THAT(syscall.stack_pointer(), Eq(0));
42   EXPECT_THAT(syscall.instruction_pointer(), Eq(0));
43 
44   EXPECT_THAT(syscall.GetName(), Eq("read"));
45   auto arg_desc = syscall.GetArgumentsDescription();
46   EXPECT_THAT(arg_desc.size(), Eq(3));
47   EXPECT_THAT(arg_desc[0], Eq("0x1 [1]"));
48   EXPECT_THAT(arg_desc[1], Eq("0xbadbeef"));
49   EXPECT_THAT(arg_desc[2], Eq("0x5 [5]"));
50   EXPECT_THAT(syscall.GetDescription(),
51               Eq(absl::StrCat(
52                   Syscall::GetArchDescription(sapi::host_cpu::Architecture()),
53                   " read [", __NR_read,
54                   "](0x1 [1], 0xbadbeef, 0x5 [5]) IP: 0, STACK: 0")));
55 }
56 
TEST(SyscallTest,Empty)57 TEST(SyscallTest, Empty) {
58   Syscall syscall;
59 
60   EXPECT_THAT(syscall.arch(), Eq(sapi::cpu::kUnknown));
61   EXPECT_THAT(syscall.GetName(), StartsWith("UNKNOWN"));
62   EXPECT_THAT(syscall.GetArgumentsDescription().size(), Eq(Syscall::kMaxArgs));
63 }
64 
65 }  // namespace
66 }  // namespace sandbox2
67