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 #ifndef CONTENT_COMMON_SANDBOX_MAC_UNITTEST_HELPER_H_
6 #define CONTENT_COMMON_SANDBOX_MAC_UNITTEST_HELPER_H_
9 #include "base/test/multiprocess_test.h"
10 #include "content/common/sandbox_mac.h"
12 namespace sandboxtest
{
14 // Helpers for writing unit tests that runs in the context of the Mac sandbox.
16 // How to write a sandboxed test:
17 // 1. Create a class that inherits from MacSandboxTestCase and overrides
18 // its functions to run code before or after the sandbox is initialised in a
20 // 2. Register the class you just created with the REGISTER_SANDBOX_TEST_CASE()
22 // 3. Write a test [using TEST_F()] that inherits from MacSandboxTest and call
23 // one of its helper functions to launch the test.
26 // class TestCaseThatRunsInSandboxedSubprocess :
27 // public sandboxtest::MacSandboxTestCase {
29 // virtual bool SandboxedTest() {
30 // .. test code that runs in sandbox goes here ..
31 // return true; // always succeed.
35 // // Register the test case you just created.
36 // REGISTER_SANDBOX_TEST_CASE(TestCaseThatRunsInSandboxedSubprocess);
38 // TEST_F(MacSandboxTest, ATest) {
39 // EXPECT_TRUE(RunTestInAllSandboxTypes(
40 // "TestCaseThatRunsInSandboxedSubprocess",
44 // Base test type with helper functions to spawn a subprocess that exercises
45 // a given test in the sandbox.
46 class MacSandboxTest
: public base::MultiProcessTest
{
48 // Runs a test specified by |test_name| in a sandbox of the type specified
49 // by |sandbox_type|. |test_data| is a custom string that a test can pass
50 // to the child process runing in the sandbox, or NULL if additional data is
52 // Returns true if the test passes, false if either of the functions in
53 // the corresponding MacSandboxTestCase return false.
54 bool RunTestInSandbox(content::SandboxType sandbox_type
,
55 const char* test_name
,
56 const char* test_data
);
58 // Runs the test specified by |test_name| in all the different sandbox types
59 // known to content, one by one.
60 // Returns true if the test passes, false if either of the functions in
61 // the corresponding MacSandboxTestCase return false in any of the spawned
64 // DANGER DANGER DANGER:
65 // Additional sandbox types defined by the embedder (e.g. the NaCL sandbox)
66 // won't be covered by these tests.
67 bool RunTestInAllSandboxTypes(const char* test_name
,
68 const char* test_data
);
71 // Class to ease writing test cases that run inside the OS X sandbox.
72 // This class is instantiated in a subprocess, and allows you to run test code
73 // at various stages of execution.
74 // Note that you must register the subclass you create with the
75 // REGISTER_SANDBOX_TEST_CASE so it's visible to the test driver.
76 class MacSandboxTestCase
{
78 virtual ~MacSandboxTestCase() {}
80 // Code that runs in the sandboxed subprocess before the sandbox is
82 // Returning false from this function will cause the entire test case to fail.
83 virtual bool BeforeSandboxInit();
85 // Code that runs in the sandboxed subprocess when the sandbox has been
87 // Returning false from this function will cause the entire test case to fail.
88 virtual bool SandboxedTest() = 0;
90 // The data that's passed in the |user_data| parameter of
91 // RunTest[s]InSandbox() is passed to this function.
92 virtual void SetTestData(const char* test_data
);
95 std::string test_data_
;
98 // Plumbing to support the REGISTER_SANDBOX_TEST_CASE macro.
101 // Register a test case with a given name.
102 void AddSandboxTestCase(const char* test_name
, MacSandboxTestCase
* test_class
);
104 // Construction of this class causes a new entry to be placed in a global
106 template <class T
> struct RegisterSandboxTest
{
107 RegisterSandboxTest(const char* test_name
) {
108 AddSandboxTestCase(test_name
, new T
);
112 #define REGISTER_SANDBOX_TEST_CASE(class_name) \
114 sandboxtest::internal::RegisterSandboxTest<class_name> \
115 register_test##class_name(#class_name); \
118 } // namespace internal
120 } // namespace sandboxtest
122 #endif // CONTENT_COMMON_SANDBOX_MAC_UNITTEST_HELPER_H_