summaryrefslogtreecommitdiffstats
path: root/chrome/common/sandbox_mac_unittest_helper.h
diff options
context:
space:
mode:
authorjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-13 08:02:06 +0000
committerjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-13 08:02:06 +0000
commitb0c7f613f8b7fd87106e730d6418c76e48764ba8 (patch)
treefd9fe89ee72120cdc7af117659f9e21c7d457474 /chrome/common/sandbox_mac_unittest_helper.h
parent38a74e7cecca512ae344b206d0729764512b2401 (diff)
downloadchromium_src-b0c7f613f8b7fd87106e730d6418c76e48764ba8.zip
chromium_src-b0c7f613f8b7fd87106e730d6418c76e48764ba8.tar.gz
chromium_src-b0c7f613f8b7fd87106e730d6418c76e48764ba8.tar.bz2
Mac: Testing infrastructure for sandboxed tests.
Create an initial framework for running tests across all Chrome sandbox types. Also added 2 simple initial tests to illustrate usage. The main motivation for adding this code is for an upcoming x-process font loading cl which needs this infrastructure. Test case registration is currently done manually. It would be better to have a macro that does this at compile time but given the small number of current tests this seems like overkill at this point. BUG=37285 TEST=Unit tests should pass Review URL: http://codereview.chromium.org/1993005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47127 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/sandbox_mac_unittest_helper.h')
-rw-r--r--chrome/common/sandbox_mac_unittest_helper.h113
1 files changed, 113 insertions, 0 deletions
diff --git a/chrome/common/sandbox_mac_unittest_helper.h b/chrome/common/sandbox_mac_unittest_helper.h
new file mode 100644
index 0000000..533b132
--- /dev/null
+++ b/chrome/common/sandbox_mac_unittest_helper.h
@@ -0,0 +1,113 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_COMMON_SANDBOX_MAC_UNITTEST_RUNNER_H_
+#define CHROME_COMMON_SANDBOX_MAC_UNITTEST_RUNNER_H_
+
+#include "base/multiprocess_test.h"
+#include "chrome/common/sandbox_mac.h"
+
+namespace sandboxtest {
+
+// Helpers for writing unit tests that runs in the context of the Mac sandbox.
+//
+// How to write a sandboxed test:
+// 1. Create a class that inherits from MacSandboxTestCase and overrides
+// it's functions to run code before or after the sandbox is initialised in a
+// subprocess.
+// 2. Register the class you just created with the REGISTER_SANDBOX_TEST_CASE()
+// macro.
+// 3. Write a test [using TEST_F()] that inherits from MacSandboxTest and call
+// one of it's helper functions to launch the test.
+//
+// Example:
+// class TestCaseThatRunsInSandboxedSubprocess :
+// public sandboxtest::MacSandboxTestCase {
+// public:
+// virtual bool SandboxedTest() {
+// .. test code that runs in sandbox goes here ..
+// return true; // always succeed.
+// }
+// };
+//
+// // Register the test case you just created.
+// REGISTER_SANDBOX_TEST_CASE(TestCaseThatRunsInSandboxedSubprocess);
+//
+// TEST_F(MacSandboxTest, ATest) {
+// EXPECT_TRUE(RunTestInAllSandboxTypes(
+// "TestCaseThatRunsInSandboxedSubprocess",
+// NULL));
+// }
+
+// Base test type with helper functions to spawn a subprocess that exercises
+// a given test in the sandbox.
+class MacSandboxTest : public MultiProcessTest {
+ public:
+ // Runs a test specified by |test_name| in a sandbox of the type specified
+ // by |sandbox_type|. |test_data| is a custom string that a test can pass
+ // to the child process runing in the sandbox.
+ // Returns true if the test passes, false if either of the functions in
+ // the corresponding MacSandboxTestCase return false.
+ bool RunTestInSandbox(sandbox::SandboxProcessType sandbox_type,
+ const char* test_name,
+ const char* test_data);
+
+ // Runs the test specified by |test_name| in all the different sandbox types,
+ // one by one.
+ // Returns true if the test passes, false if either of the functions in
+ // the corresponding MacSandboxTestCase return false in any of the spawned
+ // processes.
+ bool RunTestInAllSandboxTypes(const char* test_name,
+ const char* test_data);
+};
+
+// Class to ease writing test cases that run inside the OS X sandbox.
+// This class is instantiated in a subprocess, and allows you to run test code
+// at various stages of execution.
+// Note that you must register the subclass you create with the
+// REGISTER_SANDBOX_TEST_CASE so it's visible to the test driver.
+class MacSandboxTestCase {
+ public:
+ // Code that runs in the sandboxed subprocess before the sandbox is
+ // initialized.
+ // Returning false from this function will cause the entire test case to fail.
+ virtual bool BeforeSandboxInit() { return true; };
+
+ // Code that runs in the sandboxed subprocess when the sandbox has been
+ // enabled.
+ // Returning false from this function will cause the entire test case to fail.
+ virtual bool SandboxedTest() = 0;
+
+ // The data that's passed in the |user_data| parameter of
+ // RunTest[s]InSandbox() is passed to this function.
+ virtual void SetTestData(const char* test_data) {}
+};
+
+// Plumbing to support the REGISTER_SANDBOX_TEST_CASE macro.
+namespace internal {
+
+typedef std::map<std::string,MacSandboxTestCase*> SandboxTestMap;
+
+// A function that returns a common map from string -> test case class.
+SandboxTestMap& GetSandboxTestMap();
+
+// Construction of this class causes a new entry to be placed in a global
+// map.
+template <class T> struct RegisterSandboxTest {
+ RegisterSandboxTest(const char* test_name) {
+ GetSandboxTestMap()[test_name] = new T;
+ }
+};
+
+#define REGISTER_SANDBOX_TEST_CASE(class_name) \
+ namespace { \
+ sandboxtest::internal::RegisterSandboxTest<class_name> \
+ register_test##class_name(#class_name); \
+ } // namespace
+
+} // namespace internal
+
+} // namespace sandboxtest
+
+#endif // CHROME_COMMON_SANDBOX_MAC_UNITTEST_RUNNER_H_