| 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" | 17 #include "content/test/test_content_client.h" |
| 18 #include "testing/multiprocess_func_list.h" | 18 #include "testing/multiprocess_func_list.h" |
| 19 | 19 |
| 20 using sandbox::Sandbox; | 20 namespace content { |
| 21 | |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 const char* kSandboxTypeKey = "CHROMIUM_SANDBOX_SANDBOX_TYPE"; | 23 const char* kSandboxTypeKey = "CHROMIUM_SANDBOX_SANDBOX_TYPE"; |
| 25 const char* kSandboxTestNameKey = "CHROMIUM_SANDBOX_TEST_NAME"; | 24 const char* kSandboxTestNameKey = "CHROMIUM_SANDBOX_TEST_NAME"; |
| 26 const char* kTestDataKey = "CHROMIUM_SANDBOX_USER_DATA"; | 25 const char* kTestDataKey = "CHROMIUM_SANDBOX_USER_DATA"; |
| 27 | 26 |
| 28 } // namespace | 27 } // namespace |
| 29 | 28 |
| 30 namespace sandboxtest { | |
| 31 | |
| 32 // Support infrastructure for REGISTER_SANDBOX_TEST_CASE macro. | 29 // Support infrastructure for REGISTER_SANDBOX_TEST_CASE macro. |
| 33 namespace internal { | 30 namespace internal { |
| 34 | 31 |
| 35 typedef std::map<std::string,MacSandboxTestCase*> SandboxTestMap; | 32 typedef std::map<std::string,MacSandboxTestCase*> SandboxTestMap; |
| 36 | 33 |
| 37 // A function that returns a common map from string -> test case class. | 34 // A function that returns a common map from string -> test case class. |
| 38 SandboxTestMap& GetSandboxTestMap() { | 35 SandboxTestMap& GetSandboxTestMap() { |
| 39 static SandboxTestMap test_map; | 36 static SandboxTestMap test_map; |
| 40 return test_map; | 37 return test_map; |
| 41 } | 38 } |
| 42 | 39 |
| 43 void AddSandboxTestCase(const char* test_name, MacSandboxTestCase* test_class) { | 40 void AddSandboxTestCase(const char* test_name, MacSandboxTestCase* test_class) { |
| 44 SandboxTestMap& test_map = GetSandboxTestMap(); | 41 SandboxTestMap& test_map = GetSandboxTestMap(); |
| 45 if (test_map.find(test_name) != test_map.end()) { | 42 if (test_map.find(test_name) != test_map.end()) { |
| 46 LOG(ERROR) << "Trying to register duplicate test" << test_name; | 43 LOG(ERROR) << "Trying to register duplicate test" << test_name; |
| 47 NOTREACHED(); | 44 NOTREACHED(); |
| 48 } | 45 } |
| 49 test_map[test_name] = test_class; | 46 test_map[test_name] = test_class; |
| 50 } | 47 } |
| 51 | 48 |
| 52 } // namespace internal | 49 } // namespace internal |
| 53 | 50 |
| 54 bool MacSandboxTest::RunTestInAllSandboxTypes(const char* test_name, | 51 bool MacSandboxTest::RunTestInAllSandboxTypes(const char* test_name, |
| 55 const char* test_data) { | 52 const char* test_data) { |
| 56 // Go through all the sandbox types, and run the test case in each of them | 53 // Go through all the sandbox types, and run the test case in each of them |
| 57 // if one fails, abort. | 54 // if one fails, abort. |
| 58 for(int i = static_cast<int>(content::SANDBOX_TYPE_FIRST_TYPE); | 55 for(int i = static_cast<int>(SANDBOX_TYPE_FIRST_TYPE); |
| 59 i < content::SANDBOX_TYPE_AFTER_LAST_TYPE; | 56 i < SANDBOX_TYPE_AFTER_LAST_TYPE; |
| 60 ++i) { | 57 ++i) { |
| 61 | 58 |
| 62 if (!RunTestInSandbox(static_cast<content::SandboxType>(i), | 59 if (!RunTestInSandbox(static_cast<SandboxType>(i), |
| 63 test_name, test_data)) { | 60 test_name, test_data)) { |
| 64 LOG(ERROR) << "Sandboxed test (" << test_name << ")" << | 61 LOG(ERROR) << "Sandboxed test (" << test_name << ")" << |
| 65 "Failed in sandbox type " << i << | 62 "Failed in sandbox type " << i << |
| 66 "user data: (" << test_data << ")"; | 63 "user data: (" << test_data << ")"; |
| 67 return false; | 64 return false; |
| 68 } | 65 } |
| 69 } | 66 } |
| 70 return true; | 67 return true; |
| 71 } | 68 } |
| 72 | 69 |
| 73 bool MacSandboxTest::RunTestInSandbox(content::SandboxType sandbox_type, | 70 bool MacSandboxTest::RunTestInSandbox(SandboxType sandbox_type, |
| 74 const char* test_name, | 71 const char* test_name, |
| 75 const char* test_data) { | 72 const char* test_data) { |
| 76 std::stringstream s; | 73 std::stringstream s; |
| 77 s << static_cast<int>(static_cast<int>(sandbox_type)); | 74 s << static_cast<int>(static_cast<int>(sandbox_type)); |
| 78 setenv(kSandboxTypeKey, s.str().c_str(), 1); | 75 setenv(kSandboxTypeKey, s.str().c_str(), 1); |
| 79 setenv(kSandboxTestNameKey, test_name, 1); | 76 setenv(kSandboxTestNameKey, test_name, 1); |
| 80 if (test_data) | 77 if (test_data) |
| 81 setenv(kTestDataKey, test_data, 1); | 78 setenv(kTestDataKey, test_data, 1); |
| 82 | 79 |
| 83 base::ProcessHandle child_process = SpawnChild("mac_sandbox_test_runner", | 80 base::ProcessHandle child_process = SpawnChild("mac_sandbox_test_runner", |
| (...skipping 28 matching lines...) Expand all Loading... |
| 112 | 109 |
| 113 SandboxTestMap::iterator it = all_tests.find(name); | 110 SandboxTestMap::iterator it = all_tests.find(name); |
| 114 if (it == all_tests.end()) { | 111 if (it == all_tests.end()) { |
| 115 LOG(ERROR) << "Couldn't find sandbox test case(" << name << ")"; | 112 LOG(ERROR) << "Couldn't find sandbox test case(" << name << ")"; |
| 116 return NULL; | 113 return NULL; |
| 117 } | 114 } |
| 118 | 115 |
| 119 return it->second; | 116 return it->second; |
| 120 } | 117 } |
| 121 | 118 |
| 122 } // namespace sandboxtest | |
| 123 | |
| 124 namespace { | |
| 125 | |
| 126 // Main function for driver process that enables the sandbox and runs test | 119 // Main function for driver process that enables the sandbox and runs test |
| 127 // code. | 120 // code. |
| 128 MULTIPROCESS_TEST_MAIN(mac_sandbox_test_runner) { | 121 MULTIPROCESS_TEST_MAIN(mac_sandbox_test_runner) { |
| 129 TestContentClient content_client; | 122 TestContentClient content_client; |
| 130 content::SetContentClient(&content_client); | 123 SetContentClient(&content_client); |
| 131 // Extract parameters. | 124 // Extract parameters. |
| 132 char* sandbox_type_str = getenv(kSandboxTypeKey); | 125 char* sandbox_type_str = getenv(kSandboxTypeKey); |
| 133 if (!sandbox_type_str) { | 126 if (!sandbox_type_str) { |
| 134 LOG(ERROR) << "Sandbox type not specified"; | 127 LOG(ERROR) << "Sandbox type not specified"; |
| 135 return -1; | 128 return -1; |
| 136 } | 129 } |
| 137 content::SandboxType sandbox_type = | 130 SandboxType sandbox_type = static_cast<SandboxType>(atoi(sandbox_type_str)); |
| 138 static_cast<content::SandboxType>(atoi(sandbox_type_str)); | |
| 139 char* sandbox_test_name = getenv(kSandboxTestNameKey); | 131 char* sandbox_test_name = getenv(kSandboxTestNameKey); |
| 140 if (!sandbox_test_name) { | 132 if (!sandbox_test_name) { |
| 141 LOG(ERROR) << "Sandbox test name not specified"; | 133 LOG(ERROR) << "Sandbox test name not specified"; |
| 142 return -1; | 134 return -1; |
| 143 } | 135 } |
| 144 | 136 |
| 145 const char* test_data = getenv(kTestDataKey); | 137 const char* test_data = getenv(kTestDataKey); |
| 146 | 138 |
| 147 // Find Test Function to run; | 139 // Find Test Function to run; |
| 148 scoped_ptr<sandboxtest::MacSandboxTestCase> | 140 scoped_ptr<MacSandboxTestCase> |
| 149 test_case(sandboxtest::SandboxTestForName(sandbox_test_name)); | 141 test_case(SandboxTestForName(sandbox_test_name)); |
| 150 if (!test_case.get()) { | 142 if (!test_case.get()) { |
| 151 LOG(ERROR) << "Invalid sandbox test name (" << sandbox_test_name << ")"; | 143 LOG(ERROR) << "Invalid sandbox test name (" << sandbox_test_name << ")"; |
| 152 return -1; | 144 return -1; |
| 153 } | 145 } |
| 154 if (test_data) | 146 if (test_data) |
| 155 test_case->SetTestData(test_data); | 147 test_case->SetTestData(test_data); |
| 156 | 148 |
| 157 // Run Test. | 149 // Run Test. |
| 158 if (!test_case->BeforeSandboxInit()) { | 150 if (!test_case->BeforeSandboxInit()) { |
| 159 LOG(ERROR) << sandbox_test_name << "Failed test before sandbox init"; | 151 LOG(ERROR) << sandbox_test_name << "Failed test before sandbox init"; |
| 160 return -1; | 152 return -1; |
| 161 } | 153 } |
| 162 | 154 |
| 163 Sandbox::SandboxWarmup(sandbox_type); | 155 Sandbox::SandboxWarmup(sandbox_type); |
| 164 | 156 |
| 165 if (!Sandbox::EnableSandbox(sandbox_type, FilePath())) { | 157 if (!Sandbox::EnableSandbox(sandbox_type, FilePath())) { |
| 166 LOG(ERROR) << "Failed to initialize sandbox " << sandbox_type; | 158 LOG(ERROR) << "Failed to initialize sandbox " << sandbox_type; |
| 167 return -1; | 159 return -1; |
| 168 } | 160 } |
| 169 | 161 |
| 170 if (!test_case->SandboxedTest()) { | 162 if (!test_case->SandboxedTest()) { |
| 171 LOG(ERROR) << sandbox_test_name << "Failed sandboxed test"; | 163 LOG(ERROR) << sandbox_test_name << "Failed sandboxed test"; |
| 172 return -1; | 164 return -1; |
| 173 } | 165 } |
| 174 | 166 |
| 175 return 0; | 167 return 0; |
| 176 } | 168 } |
| 177 | 169 |
| 178 } // namespace | 170 } // namespace content |
| OLD | NEW |