| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/common/sandbox_mac.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <stdint.h> | |
| 9 #include <sys/stat.h> | |
| 10 #include <unistd.h> | |
| 11 | |
| 12 #include "base/process/kill.h" | |
| 13 #include "base/test/multiprocess_test.h" | |
| 14 #include "base/test/test_timeouts.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "testing/multiprocess_func_list.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 class SandboxMacCompilerTest : public base::MultiProcessTest {}; | |
| 21 | |
| 22 MULTIPROCESS_TEST_MAIN(BasicProfileProcess) { | |
| 23 std::string profile = | |
| 24 "(version 1)" | |
| 25 "(allow file-read* file-write* (literal \"/\"))"; | |
| 26 | |
| 27 SandboxCompiler compiler(profile); | |
| 28 | |
| 29 std::string error; | |
| 30 CHECK(compiler.CompileAndApplyProfile(&error)); | |
| 31 | |
| 32 return 0; | |
| 33 } | |
| 34 | |
| 35 TEST_F(SandboxMacCompilerTest, BasicProfileTest) { | |
| 36 base::Process process = SpawnChild("BasicProfileProcess"); | |
| 37 ASSERT_TRUE(process.IsValid()); | |
| 38 int exit_code = 42; | |
| 39 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 40 &exit_code)); | |
| 41 EXPECT_EQ(exit_code, 0); | |
| 42 } | |
| 43 | |
| 44 MULTIPROCESS_TEST_MAIN(BasicProfileWithParamProcess) { | |
| 45 std::string profile = | |
| 46 "(version 1)" | |
| 47 "(allow file-read* file-write* (literal (param \"DIR\")))"; | |
| 48 | |
| 49 SandboxCompiler compiler(profile); | |
| 50 CHECK(compiler.InsertStringParam("DIR", "/")); | |
| 51 | |
| 52 std::string error; | |
| 53 CHECK(compiler.CompileAndApplyProfile(&error)); | |
| 54 | |
| 55 return 0; | |
| 56 } | |
| 57 | |
| 58 TEST_F(SandboxMacCompilerTest, BasicProfileTestWithParam) { | |
| 59 base::Process process = SpawnChild("BasicProfileWithParamProcess"); | |
| 60 ASSERT_TRUE(process.IsValid()); | |
| 61 int exit_code = 42; | |
| 62 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 63 &exit_code)); | |
| 64 EXPECT_EQ(exit_code, 0); | |
| 65 } | |
| 66 | |
| 67 MULTIPROCESS_TEST_MAIN(ProfileFunctionalProcess) { | |
| 68 std::string profile = | |
| 69 "(version 1)" | |
| 70 "(debug deny)" | |
| 71 "(allow file-read-data file-read-metadata (literal \"/dev/urandom\"))"; | |
| 72 | |
| 73 SandboxCompiler compiler(profile); | |
| 74 | |
| 75 std::string error; | |
| 76 CHECK(compiler.CompileAndApplyProfile(&error)); | |
| 77 | |
| 78 // The profile compiled and applied successfully, now try and read 1 byte from | |
| 79 // /dev/urandom. | |
| 80 uint8_t byte; | |
| 81 int fd = open("/dev/urandom", O_RDONLY); | |
| 82 CHECK_NE(fd, -1); | |
| 83 | |
| 84 EXPECT_TRUE(read(fd, &byte, sizeof(byte)) == sizeof(byte)); | |
| 85 | |
| 86 return 0; | |
| 87 } | |
| 88 | |
| 89 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTest) { | |
| 90 base::Process process = SpawnChild("ProfileFunctionalProcess"); | |
| 91 ASSERT_TRUE(process.IsValid()); | |
| 92 int exit_code = 42; | |
| 93 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 94 &exit_code)); | |
| 95 EXPECT_EQ(exit_code, 0); | |
| 96 } | |
| 97 | |
| 98 MULTIPROCESS_TEST_MAIN(ProfileFunctionalTestWithParamsProcess) { | |
| 99 std::string profile = | |
| 100 "(version 1)" | |
| 101 "(debug deny)" | |
| 102 "(if (string=? (param \"ALLOW_FILE\") \"TRUE\")" | |
| 103 " (allow file-read-data file-read-metadata (literal (param " | |
| 104 "\"URANDOM\"))))"; | |
| 105 | |
| 106 SandboxCompiler compiler(profile); | |
| 107 | |
| 108 CHECK(compiler.InsertBooleanParam("ALLOW_FILE", true)); | |
| 109 CHECK(compiler.InsertStringParam("URANDOM", "/dev/urandom")); | |
| 110 | |
| 111 std::string error; | |
| 112 CHECK(compiler.CompileAndApplyProfile(&error)); | |
| 113 | |
| 114 // The profile compiled and applied successfully, now try and read 1 byte from | |
| 115 // /dev/urandom. | |
| 116 uint8_t byte; | |
| 117 int fd = open("/dev/urandom", O_RDONLY); | |
| 118 CHECK_NE(fd, -1); | |
| 119 | |
| 120 EXPECT_TRUE(read(fd, &byte, sizeof(byte)) == sizeof(byte)); | |
| 121 | |
| 122 // Make sure the sandbox isn't overly permissive. | |
| 123 struct stat st; | |
| 124 EXPECT_EQ(stat("/", &st), -1); | |
| 125 | |
| 126 return 0; | |
| 127 } | |
| 128 | |
| 129 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTestWithParams) { | |
| 130 base::Process process = SpawnChild("ProfileFunctionalTestWithParamsProcess"); | |
| 131 ASSERT_TRUE(process.IsValid()); | |
| 132 int exit_code = 42; | |
| 133 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 134 &exit_code)); | |
| 135 EXPECT_EQ(exit_code, 0); | |
| 136 } | |
| 137 | |
| 138 MULTIPROCESS_TEST_MAIN(ProfileFunctionalityTestErrorProcess) { | |
| 139 std::string profile = "(+ 5 a)"; | |
| 140 | |
| 141 SandboxCompiler compiler(profile); | |
| 142 | |
| 143 // Make sure that this invalid profile results in an error returned. | |
| 144 std::string error; | |
| 145 CHECK_EQ(error, ""); | |
| 146 CHECK(!compiler.CompileAndApplyProfile(&error)); | |
| 147 CHECK_NE(error, ""); | |
| 148 | |
| 149 return 0; | |
| 150 } | |
| 151 | |
| 152 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTestError) { | |
| 153 base::Process process = SpawnChild("ProfileFunctionalityTestErrorProcess"); | |
| 154 ASSERT_TRUE(process.IsValid()); | |
| 155 int exit_code = 42; | |
| 156 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 157 &exit_code)); | |
| 158 EXPECT_EQ(exit_code, 0); | |
| 159 } | |
| 160 | |
| 161 } // namespace content | |
| OLD | NEW |