diff options
Diffstat (limited to 'sandbox/linux/tests')
-rw-r--r-- | sandbox/linux/tests/sandbox_test_runner.h | 25 | ||||
-rw-r--r-- | sandbox/linux/tests/sandbox_test_runner_function_pointer.cc | 25 | ||||
-rw-r--r-- | sandbox/linux/tests/sandbox_test_runner_function_pointer.h | 26 | ||||
-rw-r--r-- | sandbox/linux/tests/unit_tests.cc | 6 | ||||
-rw-r--r-- | sandbox/linux/tests/unit_tests.h | 25 |
5 files changed, 95 insertions, 12 deletions
diff --git a/sandbox/linux/tests/sandbox_test_runner.h b/sandbox/linux/tests/sandbox_test_runner.h new file mode 100644 index 0000000..4cb7102 --- /dev/null +++ b/sandbox/linux/tests/sandbox_test_runner.h @@ -0,0 +1,25 @@ +// Copyright 2014 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 SANDBOX_LINUX_TESTS_SANDBOX_TEST_RUNNER_H_ +#define SANDBOX_LINUX_TESTS_SANDBOX_TEST_RUNNER_H_ + +#include "base/basictypes.h" + +namespace sandbox { + +// A simple "runner" class to implement tests. +class SandboxTestRunner { + public: + SandboxTestRunner() {} + virtual ~SandboxTestRunner() {} + virtual void Run() = 0; + + private: + DISALLOW_COPY_AND_ASSIGN(SandboxTestRunner); +}; + +} // namespace sandbox + +#endif // SANDBOX_LINUX_TESTS_SANDBOX_TEST_RUNNER_H_ diff --git a/sandbox/linux/tests/sandbox_test_runner_function_pointer.cc b/sandbox/linux/tests/sandbox_test_runner_function_pointer.cc new file mode 100644 index 0000000..69e05ac --- /dev/null +++ b/sandbox/linux/tests/sandbox_test_runner_function_pointer.cc @@ -0,0 +1,25 @@ +// Copyright 2014 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. + +#include "sandbox/linux/tests/sandbox_test_runner_function_pointer.h" + +#include "base/logging.h" +#include "build/build_config.h" + +namespace sandbox { + +SandboxTestRunnerFunctionPointer::SandboxTestRunnerFunctionPointer( + void (*function_to_run)(void)) + : function_to_run_(function_to_run) { +} + +SandboxTestRunnerFunctionPointer::~SandboxTestRunnerFunctionPointer() { +} + +void SandboxTestRunnerFunctionPointer::Run() { + DCHECK(function_to_run_); + function_to_run_(); +} + +} // namespace sandbox diff --git a/sandbox/linux/tests/sandbox_test_runner_function_pointer.h b/sandbox/linux/tests/sandbox_test_runner_function_pointer.h new file mode 100644 index 0000000..1cb709f --- /dev/null +++ b/sandbox/linux/tests/sandbox_test_runner_function_pointer.h @@ -0,0 +1,26 @@ +// Copyright 2014 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 SANDBOX_LINUX_TESTS_SANDBOX_TEST_RUNNER_FUNCTION_POINTER_H_ +#define SANDBOX_LINUX_TESTS_SANDBOX_TEST_RUNNER_FUNCTION_POINTER_H_ + +#include "base/basictypes.h" +#include "sandbox/linux/tests/sandbox_test_runner.h" + +namespace sandbox { + +class SandboxTestRunnerFunctionPointer : public SandboxTestRunner { + public: + SandboxTestRunnerFunctionPointer(void (*function_to_run)(void)); + virtual ~SandboxTestRunnerFunctionPointer() OVERRIDE; + virtual void Run() OVERRIDE; + + private: + void (*function_to_run_)(void); + DISALLOW_COPY_AND_ASSIGN(SandboxTestRunnerFunctionPointer); +}; + +} // namespace sandbox + +#endif // SANDBOX_LINUX_TESTS_SANDBOX_TEST_RUNNER__FUNCTION_POINTER_H_ diff --git a/sandbox/linux/tests/unit_tests.cc b/sandbox/linux/tests/unit_tests.cc index 42b85a8..282c718 100644 --- a/sandbox/linux/tests/unit_tests.cc +++ b/sandbox/linux/tests/unit_tests.cc @@ -105,10 +105,10 @@ static void SetProcessTimeout(int time_in_seconds) { // in the BPF sandbox, as it potentially makes global state changes and as // it also tends to raise fatal errors, if the code has been used in an // insecure manner. -void UnitTests::RunTestInProcess(UnitTests::Test test, - void* arg, +void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner, DeathCheck death, const void* death_aux) { + CHECK(test_runner); // We need to fork(), so we can't be multi-threaded, as threads could hold // locks. int num_threads = CountThreads(); @@ -174,7 +174,7 @@ void UnitTests::RunTestInProcess(UnitTests::Test test, struct rlimit no_core = {0}; setrlimit(RLIMIT_CORE, &no_core); - test(arg); + test_runner->Run(); _exit(kExpectedValue); } diff --git a/sandbox/linux/tests/unit_tests.h b/sandbox/linux/tests/unit_tests.h index bc1939c..9531595 100644 --- a/sandbox/linux/tests/unit_tests.h +++ b/sandbox/linux/tests/unit_tests.h @@ -7,6 +7,7 @@ #include "base/basictypes.h" #include "build/build_config.h" +#include "sandbox/linux/tests/sandbox_test_runner_function_pointer.h" #include "testing/gtest/include/gtest/gtest.h" namespace sandbox { @@ -62,12 +63,13 @@ bool IsRunningOnValgrind(); // that the test actually dies. The death test only passes if the death occurs // in the expected fashion, as specified by "death" and "death_aux". These two // parameters are typically set to one of the DEATH_XXX() macros. -#define SANDBOX_DEATH_TEST(test_case_name, test_name, death) \ - void TEST_##test_name(void*); \ - TEST(test_case_name, test_name) { \ - sandbox::UnitTests::RunTestInProcess(TEST_##test_name, NULL, death); \ - } \ - void TEST_##test_name(void*) +#define SANDBOX_DEATH_TEST(test_case_name, test_name, death) \ + void TEST_##test_name(void); \ + TEST(test_case_name, test_name) { \ + SandboxTestRunnerFunctionPointer sandbox_test_runner(TEST_##test_name); \ + sandbox::UnitTests::RunTestInProcess(&sandbox_test_runner, death); \ + } \ + void TEST_##test_name(void) // Define a new test case that runs inside of a GTest death test. This is // necessary, as most of our tests by definition make global and irreversible @@ -88,9 +90,10 @@ bool IsRunningOnValgrind(); ((expr) ? static_cast<void>(0) : sandbox::UnitTests::AssertionFailure( \ SANDBOX_STR(expr), __FILE__, __LINE__)) +// This class allows to run unittests in their own process. The main method is +// RunTestInProcess(). class UnitTests { public: - typedef void (*Test)(void*); typedef void (*DeathCheck)(int status, const std::string& msg, const void* aux); @@ -99,8 +102,12 @@ class UnitTests { // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing // functions make global irreversible changes to the execution environment // and must therefore execute in their own isolated process. - static void RunTestInProcess(Test test, - void* arg, + // |test_runner| must implement the SandboxTestRunner interface and will run + // in a subprocess. + // Note: since the child process (created with fork()) will never return from + // RunTestInProcess(), |test_runner| is guaranteed to exist for the lifetime + // of the child process. + static void RunTestInProcess(SandboxTestRunner* test_runner, DeathCheck death, const void* death_aux); |