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.
5 #ifndef SANDBOX_LINUX_TESTS_UNIT_TESTS_H__
6 #define SANDBOX_LINUX_TESTS_UNIT_TESTS_H__
8 #include "base/basictypes.h"
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 // Has this been compiled to run on Android?
17 bool IsArchitectureArm();
19 // Is Valgrind currently being used?
20 bool IsRunningOnValgrind();
22 #if defined(THREAD_SANITIZER)
23 #define DISABLE_ON_TSAN(test_name) DISABLED_##test_name
25 #define DISABLE_ON_TSAN(test_name) test_name
26 #endif // defined(THREAD_SANITIZER)
28 // While it is perfectly OK for a complex test to provide its own DeathCheck
29 // function. Most death tests have very simple requirements. These tests should
30 // use one of the predefined DEATH_XXX macros as an argument to
31 // SANDBOX_DEATH_TEST(). You can check for a (sub-)string in the output of the
32 // test, for a particular exit code, or for a particular death signal.
33 // NOTE: If you do decide to write your own DeathCheck, make sure to use
34 // gtests's ASSERT_XXX() macros instead of SANDBOX_ASSERT(). See
35 // unit_tests.cc for examples.
36 #define DEATH_SUCCESS() sandbox::UnitTests::DeathSuccess, NULL
37 #define DEATH_MESSAGE(msg) \
38 sandbox::UnitTests::DeathMessage, \
39 static_cast<const void*>(static_cast<const char*>(msg))
40 #define DEATH_EXIT_CODE(rc) \
41 sandbox::UnitTests::DeathExitCode, \
42 reinterpret_cast<void*>(static_cast<intptr_t>(rc))
43 #define DEATH_BY_SIGNAL(s) \
44 sandbox::UnitTests::DeathExitCode, \
45 reinterpret_cast<void*>(static_cast<intptr_t>(s))
47 // A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes
48 // that the test actually dies. The death test only passes if the death occurs
49 // in the expected fashion, as specified by "death" and "death_aux". These two
50 // parameters are typically set to one of the DEATH_XXX() macros.
51 #define SANDBOX_DEATH_TEST(test_case_name, test_name, death) \
52 void TEST_##test_name(void*); \
53 TEST(test_case_name, test_name) { \
54 sandbox::UnitTests::RunTestInProcess(TEST_##test_name, NULL, death); \
56 void TEST_##test_name(void*)
58 // Define a new test case that runs inside of a GTest death test. This is
59 // necessary, as most of our tests by definition make global and irreversible
60 // changes to the system (i.e. they install a sandbox). GTest provides death
61 // tests as a tool to isolate global changes from the rest of the tests.
62 #define SANDBOX_TEST(test_case_name, test_name) \
63 SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS())
65 // Simple assertion macro that is compatible with running inside of a death
66 // test. We unfortunately cannot use any of the GTest macros.
67 #define SANDBOX_STR(x) #x
68 #define SANDBOX_ASSERT(expr) \
69 ((expr) ? static_cast<void>(0) : sandbox::UnitTests::AssertionFailure( \
70 SANDBOX_STR(expr), __FILE__, __LINE__))
74 typedef void (*Test
)(void*);
75 typedef void (*DeathCheck
)(int status
,
76 const std::string
& msg
,
79 // Runs a test inside a short-lived process. Do not call this function
80 // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing
81 // functions make global irreversible changes to the execution environment
82 // and must therefore execute in their own isolated process.
83 static void RunTestInProcess(Test test
,
86 const void* death_aux
);
88 // Report a useful error message and terminate the current SANDBOX_TEST().
89 // Calling this function from outside a SANDBOX_TEST() is unlikely to do
91 static void AssertionFailure(const char* expr
, const char* file
, int line
);
93 // Sometimes we determine at run-time that a test should be disabled.
94 // Call this method if we want to return from a test and completely
95 // ignore its results.
96 // You should not call this method, if the test already ran any test-relevant
97 // code. Most notably, you should not call it, you already wrote any messages
99 static void IgnoreThisTest();
101 // A DeathCheck method that verifies that the test completed succcessfully.
102 // This is the default test mode for SANDBOX_TEST(). The "aux" parameter
103 // of this DeathCheck is unused (and thus unnamed)
104 static void DeathSuccess(int status
, const std::string
& msg
, const void*);
106 // A DeathCheck method that verifies that the test completed with error
107 // code "1" and printed a message containing a particular substring. The
108 // "aux" pointer should point to a C-string containing the expected error
109 // message. This method is useful for checking assertion failures such as
110 // in SANDBOX_ASSERT() and/or SANDBOX_DIE().
111 static void DeathMessage(int status
, const std::string
& msg
, const void* aux
);
113 // A DeathCheck method that verifies that the test completed with a
114 // particular exit code. If the test output any messages to stderr, they are
115 // silently ignored. The expected exit code should be passed in by
116 // casting the its "int" value to a "void *", which is then used for "aux".
117 static void DeathExitCode(int status
,
118 const std::string
& msg
,
121 // A DeathCheck method that verifies that the test was terminated by a
122 // particular signal. If the test output any messages to stderr, they are
123 // silently ignore. The expected signal number should be passed in by
124 // casting the its "int" value to a "void *", which is then used for "aux".
125 static void DeathBySignal(int status
,
126 const std::string
& msg
,
130 DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests
);
135 #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__