| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/sandbox_mac_unittest_helper.h" | 5 #include "chrome/common/sandbox_mac_unittest_helper.h" |
| 6 | 6 |
| 7 extern "C" { | 7 extern "C" { |
| 8 #include <sandbox.h> | 8 #include <sandbox.h> |
| 9 } | 9 } |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 | 12 |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/scoped_ptr.h" | 15 #include "base/scoped_ptr.h" |
| 16 #include "chrome/common/sandbox_mac.h" | 16 #include "chrome/common/sandbox_mac.h" |
| 17 #include "testing/multiprocess_func_list.h" | 17 #include "testing/multiprocess_func_list.h" |
| 18 | 18 |
| 19 using sandbox::Sandbox; |
| 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 const char* kSandboxTypeKey = "CHROMIUM_SANDBOX_SANDBOX_TYPE"; | 23 const char* kSandboxTypeKey = "CHROMIUM_SANDBOX_SANDBOX_TYPE"; |
| 22 const char* kSandboxTestNameKey = "CHROMIUM_SANDBOX_TEST_NAME"; | 24 const char* kSandboxTestNameKey = "CHROMIUM_SANDBOX_TEST_NAME"; |
| 23 const char* kTestDataKey = "CHROMIUM_SANDBOX_USER_DATA"; | 25 const char* kTestDataKey = "CHROMIUM_SANDBOX_USER_DATA"; |
| 24 | 26 |
| 25 } // namespace | 27 } // namespace |
| 26 | 28 |
| 27 namespace sandboxtest { | 29 namespace sandboxtest { |
| 28 | 30 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 45 } | 47 } |
| 46 test_map[test_name] = test_class; | 48 test_map[test_name] = test_class; |
| 47 } | 49 } |
| 48 | 50 |
| 49 } // namespace internal | 51 } // namespace internal |
| 50 | 52 |
| 51 bool MacSandboxTest:: RunTestInAllSandboxTypes(const char* test_name, | 53 bool MacSandboxTest:: RunTestInAllSandboxTypes(const char* test_name, |
| 52 const char* test_data) { | 54 const char* test_data) { |
| 53 // Go through all the sandbox types, and run the test case in each of them | 55 // Go through all the sandbox types, and run the test case in each of them |
| 54 // if one fails, abort. | 56 // if one fails, abort. |
| 55 for(int i = static_cast<int>(sandbox::SANDBOX_TYPE_FIRST_TYPE); | 57 for(int i = static_cast<int>(Sandbox::SANDBOX_TYPE_FIRST_TYPE); |
| 56 i < sandbox::SANDBOX_AFTER_TYPE_LAST_TYPE; | 58 i < Sandbox::SANDBOX_AFTER_TYPE_LAST_TYPE; |
| 57 ++i) { | 59 ++i) { |
| 58 if (!RunTestInSandbox(static_cast<sandbox::SandboxProcessType>(i), | 60 if (!RunTestInSandbox(static_cast<Sandbox::SandboxProcessType>(i), |
| 59 test_name, test_data)) { | 61 test_name, test_data)) { |
| 60 LOG(ERROR) << "Sandboxed test (" << test_name << ")" << | 62 LOG(ERROR) << "Sandboxed test (" << test_name << ")" << |
| 61 "Failed in sandbox type " << i << | 63 "Failed in sandbox type " << i << |
| 62 "user data: (" << test_data << ")"; | 64 "user data: (" << test_data << ")"; |
| 63 return false; | 65 return false; |
| 64 } | 66 } |
| 65 } | 67 } |
| 66 return true; | 68 return true; |
| 67 } | 69 } |
| 68 | 70 |
| 69 bool MacSandboxTest::RunTestInSandbox(sandbox::SandboxProcessType sandbox_type, | 71 bool MacSandboxTest::RunTestInSandbox(Sandbox::SandboxProcessType sandbox_type, |
| 70 const char* test_name, | 72 const char* test_name, |
| 71 const char* test_data) { | 73 const char* test_data) { |
| 72 std::stringstream s; | 74 std::stringstream s; |
| 73 s << static_cast<int>(static_cast<int>(sandbox_type)); | 75 s << static_cast<int>(static_cast<int>(sandbox_type)); |
| 74 setenv(kSandboxTypeKey, s.str().c_str(), 1); | 76 setenv(kSandboxTypeKey, s.str().c_str(), 1); |
| 75 setenv(kSandboxTestNameKey, test_name, 1); | 77 setenv(kSandboxTestNameKey, test_name, 1); |
| 76 if (test_data) | 78 if (test_data) |
| 77 setenv(kTestDataKey, test_data, 1); | 79 setenv(kTestDataKey, test_data, 1); |
| 78 | 80 |
| 79 base::ProcessHandle child_process = SpawnChild("mac_sandbox_test_runner", | 81 base::ProcessHandle child_process = SpawnChild("mac_sandbox_test_runner", |
| (...skipping 29 matching lines...) Expand all Loading... |
| 109 | 111 |
| 110 // Main function for driver process that enables the sandbox and runs test | 112 // Main function for driver process that enables the sandbox and runs test |
| 111 // code. | 113 // code. |
| 112 MULTIPROCESS_TEST_MAIN(mac_sandbox_test_runner) { | 114 MULTIPROCESS_TEST_MAIN(mac_sandbox_test_runner) { |
| 113 // Extract parameters. | 115 // Extract parameters. |
| 114 char* sandbox_type_str = getenv(kSandboxTypeKey); | 116 char* sandbox_type_str = getenv(kSandboxTypeKey); |
| 115 if (!sandbox_type_str) { | 117 if (!sandbox_type_str) { |
| 116 LOG(ERROR) << "Sandbox type not specified"; | 118 LOG(ERROR) << "Sandbox type not specified"; |
| 117 return -1; | 119 return -1; |
| 118 } | 120 } |
| 119 sandbox::SandboxProcessType sandbox_type = | 121 Sandbox::SandboxProcessType sandbox_type = |
| 120 static_cast<sandbox::SandboxProcessType>(atoi(sandbox_type_str)); | 122 static_cast<Sandbox::SandboxProcessType>(atoi(sandbox_type_str)); |
| 121 char* sandbox_test_name = getenv(kSandboxTestNameKey); | 123 char* sandbox_test_name = getenv(kSandboxTestNameKey); |
| 122 if (!sandbox_test_name) { | 124 if (!sandbox_test_name) { |
| 123 LOG(ERROR) << "Sandbox test name not specified"; | 125 LOG(ERROR) << "Sandbox test name not specified"; |
| 124 return -1; | 126 return -1; |
| 125 } | 127 } |
| 126 | 128 |
| 127 const char* test_data = getenv(kTestDataKey); | 129 const char* test_data = getenv(kTestDataKey); |
| 128 | 130 |
| 129 // Find Test Function to run; | 131 // Find Test Function to run; |
| 130 scoped_ptr<sandboxtest::MacSandboxTestCase> | 132 scoped_ptr<sandboxtest::MacSandboxTestCase> |
| 131 test_case(sandboxtest::SandboxTestForName(sandbox_test_name)); | 133 test_case(sandboxtest::SandboxTestForName(sandbox_test_name)); |
| 132 if (!test_case.get()) { | 134 if (!test_case.get()) { |
| 133 LOG(ERROR) << "Invalid sandbox test name (" << sandbox_test_name << ")"; | 135 LOG(ERROR) << "Invalid sandbox test name (" << sandbox_test_name << ")"; |
| 134 return -1; | 136 return -1; |
| 135 } | 137 } |
| 136 test_case->SetTestData(test_data); | 138 test_case->SetTestData(test_data); |
| 137 | 139 |
| 138 // Run Test. | 140 // Run Test. |
| 139 if (!test_case->BeforeSandboxInit()) { | 141 if (!test_case->BeforeSandboxInit()) { |
| 140 LOG(ERROR) << sandbox_test_name << "Failed test before sandbox init"; | 142 LOG(ERROR) << sandbox_test_name << "Failed test before sandbox init"; |
| 141 return -1; | 143 return -1; |
| 142 } | 144 } |
| 143 | 145 |
| 144 sandbox::SandboxWarmup(); | 146 Sandbox::SandboxWarmup(); |
| 145 | 147 |
| 146 if (!sandbox::EnableSandbox(sandbox_type, FilePath())) { | 148 if (!Sandbox::EnableSandbox(sandbox_type, FilePath())) { |
| 147 LOG(ERROR) << "Failed to initialize sandbox " << sandbox_type; | 149 LOG(ERROR) << "Failed to initialize sandbox " << sandbox_type; |
| 148 return -1; | 150 return -1; |
| 149 } | 151 } |
| 150 | 152 |
| 151 if (!test_case->SandboxedTest()) { | 153 if (!test_case->SandboxedTest()) { |
| 152 LOG(ERROR) << sandbox_test_name << "Failed sandboxed test"; | 154 LOG(ERROR) << sandbox_test_name << "Failed sandboxed test"; |
| 153 return -1; | 155 return -1; |
| 154 } | 156 } |
| 155 | 157 |
| 156 return 0; | 158 return 0; |
| 157 } | 159 } |
| 158 | 160 |
| 159 } // namespace | 161 } // namespace |
| OLD | NEW |