| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "sandbox/mac/seatbelt_exec.h" |
| 6 |
| 7 #include "base/process/kill.h" |
| 8 #include "base/test/multiprocess_test.h" |
| 9 #include "base/test/test_timeouts.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/multiprocess_func_list.h" |
| 12 |
| 13 namespace sandbox { |
| 14 |
| 15 class SeatbeltExecTest : public base::MultiProcessTest {}; |
| 16 |
| 17 MULTIPROCESS_TEST_MAIN(ServerTest) { |
| 18 std::string profile = |
| 19 "(version 1)\n" |
| 20 "(deny default (with no-log))\n" |
| 21 "(define allowed-dir \"ALLOWED_READ_DIR\")\n" |
| 22 "(define executable-path \"EXECUTABLE_PATH\")\n" |
| 23 "(allow process-exec (literal (param executable-path)))\n" |
| 24 "(allow file-read* (literal (param executable-path)))\n" |
| 25 "(allow file-read* (subpath (param allowed-dir)))\n"; |
| 26 |
| 27 SeatbeltExecServer exec_server(-1); |
| 28 std::string exec_path = "/bin/ls"; |
| 29 std::string allowed_path = "/Applications"; |
| 30 |
| 31 mac::SandboxPolicy policy; |
| 32 google::protobuf::MapPair<std::string, std::string> allowed_pair( |
| 33 "ALLOWED_READ_DIR", allowed_path); |
| 34 google::protobuf::MapPair<std::string, std::string> exec_pair( |
| 35 "EXECUTABLE_PATH", exec_path); |
| 36 CHECK(policy.mutable_params()->insert(allowed_pair).second); |
| 37 CHECK(policy.mutable_params()->insert(exec_pair).second); |
| 38 policy.set_profile(profile); |
| 39 |
| 40 CHECK(exec_server.ApplySandboxProfile(policy)); |
| 41 |
| 42 // Test that the sandbox profile is actually applied. |
| 43 struct stat sb; |
| 44 CHECK_EQ(0, stat(allowed_path.c_str(), &sb)); |
| 45 CHECK_EQ(-1, stat("/", &sb)); |
| 46 CHECK_EQ(0, stat(exec_path.c_str(), &sb)); |
| 47 |
| 48 return 0; |
| 49 } |
| 50 |
| 51 TEST_F(SeatbeltExecTest, ServerTest) { |
| 52 base::SpawnChildResult spawn_child = SpawnChild("ServerTest"); |
| 53 ASSERT_TRUE(spawn_child.process.IsValid()); |
| 54 int exit_code = 42; |
| 55 EXPECT_TRUE(spawn_child.process.WaitForExitWithTimeout( |
| 56 TestTimeouts::action_max_timeout(), &exit_code)); |
| 57 EXPECT_EQ(exit_code, 0); |
| 58 } |
| 59 |
| 60 MULTIPROCESS_TEST_MAIN(ClientTest) { |
| 61 SeatbeltExecClient exec_client; |
| 62 |
| 63 CHECK(exec_client.SetBooleanParameter("key1", true)); |
| 64 CHECK(!exec_client.SetBooleanParameter("key1", false)); |
| 65 CHECK(exec_client.SetBooleanParameter("key2", false)); |
| 66 CHECK(exec_client.SetParameter("key3", "value")); |
| 67 CHECK(!exec_client.SetParameter("key3", "value")); |
| 68 exec_client.SetProfile("(version 1)(deny default)"); |
| 69 |
| 70 mac::SandboxPolicy policy = exec_client.GetPolicyForTesting(); |
| 71 CHECK(policy.params_size() == 3); |
| 72 CHECK(!policy.profile().empty()); |
| 73 |
| 74 return 0; |
| 75 } |
| 76 |
| 77 TEST_F(SeatbeltExecTest, ClientTest) { |
| 78 base::SpawnChildResult spawn_child = SpawnChild("ClientTest"); |
| 79 ASSERT_TRUE(spawn_child.process.IsValid()); |
| 80 int exit_code = 42; |
| 81 EXPECT_TRUE(spawn_child.process.WaitForExitWithTimeout( |
| 82 TestTimeouts::action_max_timeout(), &exit_code)); |
| 83 EXPECT_EQ(exit_code, 0); |
| 84 } |
| 85 |
| 86 } // namespace sandbox |
| OLD | NEW |