| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/common/sandbox_mac_unittest_helper.h" | 5 #include "content/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/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "content/common/sandbox_mac.h" | 16 #include "content/common/sandbox_mac.h" |
| 17 #include "content/test/test_content_client.h" |
| 18 #include "grit/content_resources.h" |
| 17 #include "testing/multiprocess_func_list.h" | 19 #include "testing/multiprocess_func_list.h" |
| 18 | 20 |
| 19 using sandbox::Sandbox; | 21 using sandbox::Sandbox; |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 const char* kSandboxTypeKey = "CHROMIUM_SANDBOX_SANDBOX_TYPE"; | 25 const char* kSandboxTypeKey = "CHROMIUM_SANDBOX_SANDBOX_TYPE"; |
| 24 const char* kSandboxTestNameKey = "CHROMIUM_SANDBOX_TEST_NAME"; | 26 const char* kSandboxTestNameKey = "CHROMIUM_SANDBOX_TEST_NAME"; |
| 25 const char* kTestDataKey = "CHROMIUM_SANDBOX_USER_DATA"; | 27 const char* kTestDataKey = "CHROMIUM_SANDBOX_USER_DATA"; |
| 26 | 28 |
| 29 const int kSandboxDefinitionResourceIds[] = { |
| 30 IDR_GPU_SANDBOX_DEFINITION, |
| 31 IDR_WORKER_SANDBOX_DEFINITION, |
| 32 IDR_COMMON_SANDBOX_DEFINITION, |
| 33 IDR_PPAPI_SANDBOX_DEFINITION, |
| 34 IDR_RENDERER_SANDBOX_DEFINITION, |
| 35 IDR_UTILITY_SANDBOX_DEFINITION, |
| 36 }; |
| 37 |
| 27 } // namespace | 38 } // namespace |
| 28 | 39 |
| 29 namespace sandboxtest { | 40 namespace sandboxtest { |
| 30 | 41 |
| 31 // Support infrastructure for REGISTER_SANDBOX_TEST_CASE macro. | 42 // Support infrastructure for REGISTER_SANDBOX_TEST_CASE macro. |
| 32 namespace internal { | 43 namespace internal { |
| 33 | 44 |
| 34 typedef std::map<std::string,MacSandboxTestCase*> SandboxTestMap; | 45 typedef std::map<std::string,MacSandboxTestCase*> SandboxTestMap; |
| 35 | 46 |
| 36 // A function that returns a common map from string -> test case class. | 47 // A function that returns a common map from string -> test case class. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 47 } | 58 } |
| 48 test_map[test_name] = test_class; | 59 test_map[test_name] = test_class; |
| 49 } | 60 } |
| 50 | 61 |
| 51 } // namespace internal | 62 } // namespace internal |
| 52 | 63 |
| 53 bool MacSandboxTest::RunTestInAllSandboxTypes(const char* test_name, | 64 bool MacSandboxTest::RunTestInAllSandboxTypes(const char* test_name, |
| 54 const char* test_data) { | 65 const char* test_data) { |
| 55 // Go through all the sandbox types, and run the test case in each of them | 66 // Go through all the sandbox types, and run the test case in each of them |
| 56 // if one fails, abort. | 67 // if one fails, abort. |
| 57 for(int i = static_cast<int>(Sandbox::SANDBOX_TYPE_FIRST_TYPE); | 68 for(unsigned i = 0; i < arraysize(kSandboxDefinitionResourceIds); ++i) { |
| 58 i < Sandbox::SANDBOX_AFTER_TYPE_LAST_TYPE; | |
| 59 ++i) { | |
| 60 | 69 |
| 61 if (!RunTestInSandbox(static_cast<Sandbox::SandboxProcessType>(i), | 70 if (!RunTestInSandbox(kSandboxDefinitionResourceIds[i], |
| 62 test_name, test_data)) { | 71 test_name, test_data)) { |
| 63 LOG(ERROR) << "Sandboxed test (" << test_name << ")" << | 72 LOG(ERROR) << "Sandboxed test (" << test_name << ")" << |
| 64 "Failed in sandbox type " << i << | 73 "Failed in sandbox type " << i << |
| 65 "user data: (" << test_data << ")"; | 74 " user data: (" << test_data << ")"; |
| 66 return false; | 75 return false; |
| 67 } | 76 } |
| 68 } | 77 } |
| 69 return true; | 78 return true; |
| 70 } | 79 } |
| 71 | 80 |
| 72 bool MacSandboxTest::RunTestInSandbox(Sandbox::SandboxProcessType sandbox_type, | 81 bool MacSandboxTest::RunTestInSandbox(int sandbox_definition_resource_id, |
| 73 const char* test_name, | 82 const char* test_name, |
| 74 const char* test_data) { | 83 const char* test_data) { |
| 75 std::stringstream s; | 84 std::stringstream s; |
| 76 s << static_cast<int>(static_cast<int>(sandbox_type)); | 85 s << sandbox_definition_resource_id; |
| 77 setenv(kSandboxTypeKey, s.str().c_str(), 1); | 86 setenv(kSandboxTypeKey, s.str().c_str(), 1); |
| 78 setenv(kSandboxTestNameKey, test_name, 1); | 87 setenv(kSandboxTestNameKey, test_name, 1); |
| 79 if (test_data) | 88 if (test_data) |
| 80 setenv(kTestDataKey, test_data, 1); | 89 setenv(kTestDataKey, test_data, 1); |
| 81 | 90 |
| 82 base::ProcessHandle child_process = SpawnChild("mac_sandbox_test_runner", | 91 base::ProcessHandle child_process = SpawnChild("mac_sandbox_test_runner", |
| 83 false); | 92 false); |
| 84 if (child_process == base::kNullProcessHandle) { | 93 if (child_process == base::kNullProcessHandle) { |
| 85 LOG(WARNING) << "SpawnChild failed"; | 94 LOG(WARNING) << "SpawnChild failed"; |
| 86 return false; | 95 return false; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 return it->second; | 127 return it->second; |
| 119 } | 128 } |
| 120 | 129 |
| 121 } // namespace sandboxtest | 130 } // namespace sandboxtest |
| 122 | 131 |
| 123 namespace { | 132 namespace { |
| 124 | 133 |
| 125 // Main function for driver process that enables the sandbox and runs test | 134 // Main function for driver process that enables the sandbox and runs test |
| 126 // code. | 135 // code. |
| 127 MULTIPROCESS_TEST_MAIN(mac_sandbox_test_runner) { | 136 MULTIPROCESS_TEST_MAIN(mac_sandbox_test_runner) { |
| 137 TestContentClient content_client; |
| 138 content::SetContentClient(&content_client); |
| 128 // Extract parameters. | 139 // Extract parameters. |
| 129 char* sandbox_type_str = getenv(kSandboxTypeKey); | 140 char* sandbox_type_str = getenv(kSandboxTypeKey); |
| 130 if (!sandbox_type_str) { | 141 if (!sandbox_type_str) { |
| 131 LOG(ERROR) << "Sandbox type not specified"; | 142 LOG(ERROR) << "Sandbox type not specified"; |
| 132 return -1; | 143 return -1; |
| 133 } | 144 } |
| 134 Sandbox::SandboxProcessType sandbox_type = | 145 int sandbox_definition_resource_id = atoi(sandbox_type_str); |
| 135 static_cast<Sandbox::SandboxProcessType>(atoi(sandbox_type_str)); | |
| 136 char* sandbox_test_name = getenv(kSandboxTestNameKey); | 146 char* sandbox_test_name = getenv(kSandboxTestNameKey); |
| 137 if (!sandbox_test_name) { | 147 if (!sandbox_test_name) { |
| 138 LOG(ERROR) << "Sandbox test name not specified"; | 148 LOG(ERROR) << "Sandbox test name not specified"; |
| 139 return -1; | 149 return -1; |
| 140 } | 150 } |
| 141 | 151 |
| 142 const char* test_data = getenv(kTestDataKey); | 152 const char* test_data = getenv(kTestDataKey); |
| 143 | 153 |
| 144 // Find Test Function to run; | 154 // Find Test Function to run; |
| 145 scoped_ptr<sandboxtest::MacSandboxTestCase> | 155 scoped_ptr<sandboxtest::MacSandboxTestCase> |
| 146 test_case(sandboxtest::SandboxTestForName(sandbox_test_name)); | 156 test_case(sandboxtest::SandboxTestForName(sandbox_test_name)); |
| 147 if (!test_case.get()) { | 157 if (!test_case.get()) { |
| 148 LOG(ERROR) << "Invalid sandbox test name (" << sandbox_test_name << ")"; | 158 LOG(ERROR) << "Invalid sandbox test name (" << sandbox_test_name << ")"; |
| 149 return -1; | 159 return -1; |
| 150 } | 160 } |
| 151 test_case->SetTestData(test_data); | 161 test_case->SetTestData(test_data); |
| 152 | 162 |
| 153 // Run Test. | 163 // Run Test. |
| 154 if (!test_case->BeforeSandboxInit()) { | 164 if (!test_case->BeforeSandboxInit()) { |
| 155 LOG(ERROR) << sandbox_test_name << "Failed test before sandbox init"; | 165 LOG(ERROR) << sandbox_test_name << "Failed test before sandbox init"; |
| 156 return -1; | 166 return -1; |
| 157 } | 167 } |
| 158 | 168 |
| 159 Sandbox::SandboxWarmup(sandbox_type); | 169 Sandbox::SandboxWarmup(sandbox_definition_resource_id); |
| 160 | 170 |
| 161 if (!Sandbox::EnableSandbox(sandbox_type, FilePath())) { | 171 if (!Sandbox::EnableSandbox(sandbox_definition_resource_id, FilePath())) { |
| 162 LOG(ERROR) << "Failed to initialize sandbox " << sandbox_type; | 172 LOG(ERROR) << "Failed to initialize sandbox (definition resource id " |
| 173 << sandbox_definition_resource_id << ")"; |
| 163 return -1; | 174 return -1; |
| 164 } | 175 } |
| 165 | 176 |
| 166 if (!test_case->SandboxedTest()) { | 177 if (!test_case->SandboxedTest()) { |
| 167 LOG(ERROR) << sandbox_test_name << "Failed sandboxed test"; | 178 LOG(ERROR) << sandbox_test_name << "Failed sandboxed test"; |
| 168 return -1; | 179 return -1; |
| 169 } | 180 } |
| 170 | 181 |
| 171 return 0; | 182 return 0; |
| 172 } | 183 } |
| 173 | 184 |
| 174 } // namespace | 185 } // namespace |
| OLD | NEW |