1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/common/sandbox_mac.h"
11 #include "base/process/kill.h"
12 #include "base/test/multiprocess_test.h"
13 #include "base/test/test_timeouts.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/multiprocess_func_list.h"
19 class SandboxMacCompilerTest : public base::MultiProcessTest {};
21 MULTIPROCESS_TEST_MAIN(BasicProfileProcess) {
24 "(allow file-read* file-write* (literal \"/\"))";
26 SandboxCompiler compiler(profile);
29 CHECK(compiler.CompileAndApplyProfile(&error));
34 TEST_F(SandboxMacCompilerTest, BasicProfileTest) {
35 base::Process process = SpawnChild("BasicProfileProcess");
36 ASSERT_TRUE(process.IsValid());
38 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
40 EXPECT_EQ(exit_code, 0);
43 MULTIPROCESS_TEST_MAIN(BasicProfileWithParamProcess) {
46 "(allow file-read* file-write* (literal (param \"DIR\")))";
48 SandboxCompiler compiler(profile);
49 CHECK(compiler.InsertStringParam("DIR", "/"));
52 CHECK(compiler.CompileAndApplyProfile(&error));
57 TEST_F(SandboxMacCompilerTest, BasicProfileTestWithParam) {
58 base::Process process = SpawnChild("BasicProfileWithParamProcess");
59 ASSERT_TRUE(process.IsValid());
61 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
63 EXPECT_EQ(exit_code, 0);
66 MULTIPROCESS_TEST_MAIN(ProfileFunctionalProcess) {
70 "(allow file-read-data file-read-metadata (literal \"/dev/urandom\"))";
72 SandboxCompiler compiler(profile);
75 CHECK(compiler.CompileAndApplyProfile(&error));
77 // The profile compiled and applied successfully, now try and read 1 byte from
80 int fd = open("/dev/urandom", O_RDONLY);
83 EXPECT_TRUE(read(fd, &byte, sizeof(byte)) == sizeof(byte));
88 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTest) {
89 base::Process process = SpawnChild("ProfileFunctionalProcess");
90 ASSERT_TRUE(process.IsValid());
92 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
94 EXPECT_EQ(exit_code, 0);
97 MULTIPROCESS_TEST_MAIN(ProfileFunctionalTestWithParamsProcess) {
101 "(if (string=? (param \"ALLOW_FILE\") \"TRUE\")"
102 " (allow file-read-data file-read-metadata (literal (param "
105 SandboxCompiler compiler(profile);
107 CHECK(compiler.InsertBooleanParam("ALLOW_FILE", true));
108 CHECK(compiler.InsertStringParam("URANDOM", "/dev/urandom"));
111 CHECK(compiler.CompileAndApplyProfile(&error));
113 // The profile compiled and applied successfully, now try and read 1 byte from
116 int fd = open("/dev/urandom", O_RDONLY);
119 EXPECT_TRUE(read(fd, &byte, sizeof(byte)) == sizeof(byte));
121 // Make sure the sandbox isn't overly permissive.
123 EXPECT_EQ(stat("/", &st), -1);
128 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTestWithParams) {
129 base::Process process = SpawnChild("ProfileFunctionalTestWithParamsProcess");
130 ASSERT_TRUE(process.IsValid());
132 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
134 EXPECT_EQ(exit_code, 0);
137 MULTIPROCESS_TEST_MAIN(ProfileFunctionalityTestErrorProcess) {
138 std::string profile = "(+ 5 a)";
140 SandboxCompiler compiler(profile);
142 // Make sure that this invalid profile results in an error returned.
145 CHECK(!compiler.CompileAndApplyProfile(&error));
151 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTestError) {
152 base::Process process = SpawnChild("ProfileFunctionalityTestErrorProcess");
153 ASSERT_TRUE(process.IsValid());
155 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
157 EXPECT_EQ(exit_code, 0);
160 } // namespace content