OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 #ifndef SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
| 6 #define SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace sandbox { |
| 12 |
| 13 // Define a new test case that runs inside of a death test. This is necessary, |
| 14 // as most of our tests by definition make global and irreversible changes to |
| 15 // the system (i.e. they install a sandbox). GTest provides death tests as a |
| 16 // tool to isolate global changes from the rest of the tests. |
| 17 #define SANDBOX_TEST(test_case_name, test_name) \ |
| 18 void TEST_##test_name(void *); \ |
| 19 TEST(test_case_name, test_name) { \ |
| 20 sandbox::UnitTests::RunTestInProcess(TEST_##test_name, NULL); \ |
| 21 } \ |
| 22 void TEST_##test_name(void *) |
| 23 |
| 24 // Simple assertion macro that is compatible with running inside of a death |
| 25 // test. We unfortunately cannot use any of the GTest macros. |
| 26 #define SANDBOX_STR(x) #x |
| 27 #define SANDBOX_ASSERT(expr) \ |
| 28 ((expr) \ |
| 29 ? static_cast<void>(0) \ |
| 30 : sandbox::UnitTests::AssertionFailure(SANDBOX_STR(expr), \ |
| 31 __FILE__, __LINE__)) |
| 32 |
| 33 class UnitTests { |
| 34 public: |
| 35 typedef void (*Test)(void *); |
| 36 |
| 37 // Runs a test inside a short-lived process. Do not call this function |
| 38 // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing |
| 39 // functions make global irreversible changes to the execution environment |
| 40 // and must therefore execute in their own isolated process. |
| 41 static void RunTestInProcess(Test test, void *arg); |
| 42 |
| 43 // Report a useful error message and terminate the current SANDBOX_TEST(). |
| 44 // Calling this function from outside a SANDBOX_TEST() is unlikely to do |
| 45 // anything useful. |
| 46 static void AssertionFailure(const char *expr, const char *file, int line); |
| 47 |
| 48 private: |
| 49 DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests); |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
OLD | NEW |