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
3 // found in the LICENSE file.
5 #include "content/common/sandbox_mac_unittest_helper.h"
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "content/common/sandbox_mac.h"
17 #include "content/test/test_content_client.h"
18 #include "testing/multiprocess_func_list.h"
23 const char* kSandboxTypeKey = "CHROMIUM_SANDBOX_SANDBOX_TYPE";
24 const char* kSandboxTestNameKey = "CHROMIUM_SANDBOX_TEST_NAME";
25 const char* kTestDataKey = "CHROMIUM_SANDBOX_USER_DATA";
29 // Support infrastructure for REGISTER_SANDBOX_TEST_CASE macro.
32 typedef std::map<std::string,MacSandboxTestCase*> SandboxTestMap;
34 // A function that returns a common map from string -> test case class.
35 SandboxTestMap& GetSandboxTestMap() {
36 static SandboxTestMap test_map;
40 void AddSandboxTestCase(const char* test_name, MacSandboxTestCase* test_class) {
41 SandboxTestMap& test_map = GetSandboxTestMap();
42 if (test_map.find(test_name) != test_map.end()) {
43 LOG(ERROR) << "Trying to register duplicate test" << test_name;
46 test_map[test_name] = test_class;
49 } // namespace internal
51 bool MacSandboxTest::RunTestInAllSandboxTypes(const char* test_name,
52 const char* test_data) {
53 // Go through all the sandbox types, and run the test case in each of them
54 // if one fails, abort.
55 for(int i = static_cast<int>(SANDBOX_TYPE_FIRST_TYPE);
56 i < SANDBOX_TYPE_AFTER_LAST_TYPE;
59 if (!RunTestInSandbox(static_cast<SandboxType>(i),
60 test_name, test_data)) {
61 LOG(ERROR) << "Sandboxed test (" << test_name << ")" <<
62 "Failed in sandbox type " << i <<
63 "user data: (" << test_data << ")";
70 bool MacSandboxTest::RunTestInSandbox(SandboxType sandbox_type,
71 const char* test_name,
72 const char* test_data) {
74 s << static_cast<int>(static_cast<int>(sandbox_type));
75 setenv(kSandboxTypeKey, s.str().c_str(), 1);
76 setenv(kSandboxTestNameKey, test_name, 1);
78 setenv(kTestDataKey, test_data, 1);
80 base::ProcessHandle child_process = SpawnChild("mac_sandbox_test_runner",
82 if (child_process == base::kNullProcessHandle) {
83 LOG(WARNING) << "SpawnChild failed";
87 if (!base::WaitForExitCode(child_process, &code)) {
88 LOG(WARNING) << "base::WaitForExitCode failed";
94 bool MacSandboxTestCase::BeforeSandboxInit() {
98 void MacSandboxTestCase::SetTestData(const char* test_data) {
99 test_data_ = test_data;
102 // Given a test name specified by |name| return that test case.
103 // If no test case is found for the given name, return NULL.
104 MacSandboxTestCase *SandboxTestForName(const char* name) {
105 using internal::SandboxTestMap;
106 using internal::GetSandboxTestMap;
108 SandboxTestMap all_tests = GetSandboxTestMap();
110 SandboxTestMap::iterator it = all_tests.find(name);
111 if (it == all_tests.end()) {
112 LOG(ERROR) << "Couldn't find sandbox test case(" << name << ")";
119 // Main function for driver process that enables the sandbox and runs test
121 MULTIPROCESS_TEST_MAIN(mac_sandbox_test_runner) {
122 TestContentClient content_client;
123 SetContentClient(&content_client);
124 // Extract parameters.
125 char* sandbox_type_str = getenv(kSandboxTypeKey);
126 if (!sandbox_type_str) {
127 LOG(ERROR) << "Sandbox type not specified";
130 SandboxType sandbox_type = static_cast<SandboxType>(atoi(sandbox_type_str));
131 char* sandbox_test_name = getenv(kSandboxTestNameKey);
132 if (!sandbox_test_name) {
133 LOG(ERROR) << "Sandbox test name not specified";
137 const char* test_data = getenv(kTestDataKey);
139 // Find Test Function to run;
140 scoped_ptr<MacSandboxTestCase>
141 test_case(SandboxTestForName(sandbox_test_name));
143 LOG(ERROR) << "Invalid sandbox test name (" << sandbox_test_name << ")";
147 test_case->SetTestData(test_data);
150 if (!test_case->BeforeSandboxInit()) {
151 LOG(ERROR) << sandbox_test_name << "Failed test before sandbox init";
155 Sandbox::SandboxWarmup(sandbox_type);
157 if (!Sandbox::EnableSandbox(sandbox_type, base::FilePath())) {
158 LOG(ERROR) << "Failed to initialize sandbox " << sandbox_type;
162 if (!test_case->SandboxedTest()) {
163 LOG(ERROR) << sandbox_test_name << "Failed sandboxed test";
170 } // namespace content