Chromium Code Reviews| 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 <unistd.h> | |
| 9 #include <sys/stat.h> | |
| 10 | |
| 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" | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 class SandboxMacCompilerTest : public base::MultiProcessTest {}; | |
| 20 | |
| 21 MULTIPROCESS_TEST_MAIN(BasicProfileProcess) { | |
| 22 std::string profile = | |
| 23 "(version 1)" | |
| 24 "(allow file-read* file-write* (literal \"/\"))"; | |
| 25 | |
| 26 SandboxCompiler compiler(profile); | |
| 27 CHECK_EQ(compiler.Init(), true); | |
|
Robert Sesek
2015/06/16 23:52:41
CHECK_EQ(…, bool) can be jut CHECK(…)
Greg K
2015/06/18 20:45:18
Done: since Init() went away this did too.
| |
| 28 | |
| 29 std::string error; | |
| 30 CHECK_EQ(compiler.CompileAndApplyProfile(&error), 0); | |
| 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_EQ(compiler.Init(), true); | |
| 51 compiler.InsertStringParam("DIR", "/"); | |
| 52 | |
| 53 std::string error; | |
| 54 EXPECT_EQ(compiler.CompileAndApplyProfile(&error), 0); | |
| 55 | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 TEST_F(SandboxMacCompilerTest, BasicProfileTestWithParam) { | |
| 60 base::Process process = SpawnChild("BasicProfileWithParamProcess"); | |
| 61 ASSERT_TRUE(process.IsValid()); | |
| 62 int exit_code = 42; | |
| 63 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 64 &exit_code)); | |
| 65 EXPECT_EQ(exit_code, 0); | |
| 66 } | |
| 67 | |
| 68 MULTIPROCESS_TEST_MAIN(ProfileFunctionalProcess) { | |
| 69 std::string profile = | |
| 70 "(version 1)" | |
| 71 "(debug deny)" | |
| 72 "(allow file-read-data file-read-metadata (literal \"/dev/urandom\"))"; | |
| 73 | |
| 74 SandboxCompiler compiler(profile); | |
| 75 CHECK_EQ(compiler.Init(), true); | |
| 76 | |
| 77 std::string error; | |
| 78 EXPECT_EQ(compiler.CompileAndApplyProfile(&error), 0); | |
| 79 | |
| 80 // The profile compiled and applied successfully, now try and read 1 byte from | |
| 81 // /dev/urandom | |
| 82 uint8_t byte; | |
| 83 int fd = open("/dev/urandom", O_RDONLY); | |
| 84 CHECK_NE(fd, -1); | |
| 85 | |
| 86 EXPECT_TRUE(read(fd, &byte, sizeof(byte)) == sizeof(byte)); | |
| 87 | |
| 88 return 0; | |
| 89 } | |
| 90 | |
| 91 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTest) { | |
| 92 base::Process process = SpawnChild("ProfileFunctionalProcess"); | |
| 93 ASSERT_TRUE(process.IsValid()); | |
| 94 int exit_code = 42; | |
| 95 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 96 &exit_code)); | |
| 97 EXPECT_EQ(exit_code, 0); | |
| 98 } | |
| 99 | |
| 100 MULTIPROCESS_TEST_MAIN(ProfileFunctionalTestWithParamsProcess) { | |
| 101 std::string profile = | |
| 102 "(version 1)" | |
| 103 "(debug deny)" | |
| 104 "(if (string=? (param \"ALLOW_FILE\") \"TRUE\")" | |
| 105 " (allow file-read-data file-read-metadata (literal (param " | |
| 106 "\"URANDOM\"))))"; | |
| 107 | |
| 108 SandboxCompiler compiler(profile); | |
| 109 CHECK_EQ(compiler.Init(), true); | |
| 110 | |
| 111 compiler.InsertBooleanParam("ALLOW_FILE", true); | |
| 112 compiler.InsertStringParam("URANDOM", "/dev/urandom"); | |
| 113 | |
| 114 std::string error; | |
| 115 EXPECT_EQ(compiler.CompileAndApplyProfile(&error), 0); | |
| 116 | |
| 117 // The profile compiled and applied successfully, now try and read 1 byte from | |
| 118 // /dev/urandom | |
| 119 uint8_t byte; | |
| 120 int fd = open("/dev/urandom", O_RDONLY); | |
| 121 CHECK_NE(fd, -1); | |
| 122 | |
| 123 EXPECT_TRUE(read(fd, &byte, sizeof(byte)) == sizeof(byte)); | |
| 124 | |
| 125 // Make sure the sandbox isn't overly permissive | |
| 126 struct stat st; | |
| 127 EXPECT_EQ(stat("/", &st), -1); | |
| 128 | |
| 129 return 0; | |
| 130 } | |
| 131 | |
| 132 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTestWithParams) { | |
| 133 base::Process process = SpawnChild("ProfileFunctionalTestWithParamsProcess"); | |
| 134 ASSERT_TRUE(process.IsValid()); | |
| 135 int exit_code = 42; | |
| 136 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 137 &exit_code)); | |
| 138 EXPECT_EQ(exit_code, 0); | |
| 139 } | |
| 140 | |
| 141 MULTIPROCESS_TEST_MAIN(ProfileFunctionalityTestErrorProcess) { | |
| 142 std::string profile = "(+ 5 a)"; | |
| 143 | |
| 144 SandboxCompiler compiler(profile); | |
| 145 CHECK_EQ(compiler.Init(), true); | |
| 146 | |
| 147 // Make sure that this invalid profile results in an error returned | |
| 148 std::string error; | |
| 149 CHECK_EQ(error, ""); | |
| 150 CHECK_NE(compiler.CompileAndApplyProfile(&error), 0); | |
| 151 CHECK_NE(error, ""); | |
| 152 | |
| 153 return 0; | |
| 154 } | |
| 155 | |
| 156 TEST_F(SandboxMacCompilerTest, ProfileFunctionalityTestError) { | |
| 157 base::Process process = SpawnChild("ProfileFunctionalityTestErrorProcess"); | |
| 158 ASSERT_TRUE(process.IsValid()); | |
| 159 int exit_code = 42; | |
| 160 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 161 &exit_code)); | |
| 162 EXPECT_EQ(exit_code, 0); | |
| 163 } | |
| 164 } | |
|
Robert Sesek
2015/06/16 23:52:41
nit: Blank line before, and closing namespaces (ex
Greg K
2015/06/18 20:45:18
Done.
| |
| OLD | NEW |