Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / sandbox / linux / tests / unit_tests.h
blob5a7116e932e69425d6c5d6a545d214449fe1a8b8
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/macros.h"
9 #include "build/build_config.h"
10 #include "sandbox/linux/tests/sandbox_test_runner_function_pointer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace sandbox {
15 // Has this been compiled to run on Android?
16 bool IsAndroid();
18 bool IsArchitectureArm();
20 // Is Valgrind currently being used?
21 bool IsRunningOnValgrind();
23 #if defined(ADDRESS_SANITIZER)
24 #define DISABLE_ON_ASAN(test_name) DISABLED_##test_name
25 #else
26 #define DISABLE_ON_ASAN(test_name) test_name
27 #endif // defined(ADDRESS_SANITIZER)
29 #if defined(LEAK_SANITIZER)
30 #define DISABLE_ON_LSAN(test_name) DISABLED_##test_name
31 #else
32 #define DISABLE_ON_LSAN(test_name) test_name
33 #endif
35 #if defined(THREAD_SANITIZER)
36 #define DISABLE_ON_TSAN(test_name) DISABLED_##test_name
37 #else
38 #define DISABLE_ON_TSAN(test_name) test_name
39 #endif // defined(THREAD_SANITIZER)
41 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
42 defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
43 defined(UNDEFINED_SANITIZER) || defined(SANITIZER_COVERAGE)
44 #define DISABLE_ON_SANITIZERS(test_name) DISABLED_##test_name
45 #else
46 #define DISABLE_ON_SANITIZERS(test_name) test_name
47 #endif
49 #if defined(OS_ANDROID)
50 #define DISABLE_ON_ANDROID(test_name) DISABLED_##test_name
51 #else
52 #define DISABLE_ON_ANDROID(test_name) test_name
53 #endif
55 // While it is perfectly OK for a complex test to provide its own DeathCheck
56 // function. Most death tests have very simple requirements. These tests should
57 // use one of the predefined DEATH_XXX macros as an argument to
58 // SANDBOX_DEATH_TEST(). You can check for a (sub-)string in the output of the
59 // test, for a particular exit code, or for a particular death signal.
60 // NOTE: If you do decide to write your own DeathCheck, make sure to use
61 // gtests's ASSERT_XXX() macros instead of SANDBOX_ASSERT(). See
62 // unit_tests.cc for examples.
63 #define DEATH_SUCCESS() sandbox::UnitTests::DeathSuccess, NULL
64 #define DEATH_SUCCESS_ALLOW_NOISE() \
65 sandbox::UnitTests::DeathSuccessAllowNoise, NULL
66 #define DEATH_MESSAGE(msg) \
67 sandbox::UnitTests::DeathMessage, \
68 static_cast<const void*>(static_cast<const char*>(msg))
69 #define DEATH_SEGV_MESSAGE(msg) \
70 sandbox::UnitTests::DeathSEGVMessage, \
71 static_cast<const void*>(static_cast<const char*>(msg))
72 #define DEATH_EXIT_CODE(rc) \
73 sandbox::UnitTests::DeathExitCode, \
74 reinterpret_cast<void*>(static_cast<intptr_t>(rc))
75 #define DEATH_BY_SIGNAL(s) \
76 sandbox::UnitTests::DeathBySignal, \
77 reinterpret_cast<void*>(static_cast<intptr_t>(s))
79 // A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes
80 // that the test actually dies. The death test only passes if the death occurs
81 // in the expected fashion, as specified by "death" and "death_aux". These two
82 // parameters are typically set to one of the DEATH_XXX() macros.
83 #define SANDBOX_DEATH_TEST(test_case_name, test_name, death) \
84 void TEST_##test_name(void); \
85 TEST(test_case_name, test_name) { \
86 SandboxTestRunnerFunctionPointer sandbox_test_runner(TEST_##test_name); \
87 sandbox::UnitTests::RunTestInProcess(&sandbox_test_runner, death); \
88 } \
89 void TEST_##test_name(void)
91 // Define a new test case that runs inside of a GTest death test. This is
92 // necessary, as most of our tests by definition make global and irreversible
93 // changes to the system (i.e. they install a sandbox). GTest provides death
94 // tests as a tool to isolate global changes from the rest of the tests.
95 #define SANDBOX_TEST(test_case_name, test_name) \
96 SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS())
98 // SANDBOX_TEST_ALLOW_NOISE is just like SANDBOX_TEST, except it does not
99 // consider log error messages printed by the test to be test failures.
100 #define SANDBOX_TEST_ALLOW_NOISE(test_case_name, test_name) \
101 SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS_ALLOW_NOISE())
103 // Simple assertion macro that is compatible with running inside of a death
104 // test. We unfortunately cannot use any of the GTest macros.
105 #define SANDBOX_STR(x) #x
106 #define SANDBOX_ASSERT(expr) \
107 ((expr) ? static_cast<void>(0) : sandbox::UnitTests::AssertionFailure( \
108 SANDBOX_STR(expr), __FILE__, __LINE__))
110 #define SANDBOX_ASSERT_EQ(x, y) SANDBOX_ASSERT((x) == (y))
111 #define SANDBOX_ASSERT_NE(x, y) SANDBOX_ASSERT((x) != (y))
112 #define SANDBOX_ASSERT_LT(x, y) SANDBOX_ASSERT((x) < (y))
113 #define SANDBOX_ASSERT_GT(x, y) SANDBOX_ASSERT((x) > (y))
114 #define SANDBOX_ASSERT_LE(x, y) SANDBOX_ASSERT((x) <= (y))
115 #define SANDBOX_ASSERT_GE(x, y) SANDBOX_ASSERT((x) >= (y))
117 // This class allows to run unittests in their own process. The main method is
118 // RunTestInProcess().
119 class UnitTests {
120 public:
121 typedef void (*DeathCheck)(int status,
122 const std::string& msg,
123 const void* aux);
125 // Runs a test inside a short-lived process. Do not call this function
126 // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing
127 // functions make global irreversible changes to the execution environment
128 // and must therefore execute in their own isolated process.
129 // |test_runner| must implement the SandboxTestRunner interface and will run
130 // in a subprocess.
131 // Note: since the child process (created with fork()) will never return from
132 // RunTestInProcess(), |test_runner| is guaranteed to exist for the lifetime
133 // of the child process.
134 static void RunTestInProcess(SandboxTestRunner* test_runner,
135 DeathCheck death,
136 const void* death_aux);
138 // Report a useful error message and terminate the current SANDBOX_TEST().
139 // Calling this function from outside a SANDBOX_TEST() is unlikely to do
140 // anything useful.
141 static void AssertionFailure(const char* expr, const char* file, int line);
143 // Sometimes we determine at run-time that a test should be disabled.
144 // Call this method if we want to return from a test and completely
145 // ignore its results.
146 // You should not call this method, if the test already ran any test-relevant
147 // code. Most notably, you should not call it, you already wrote any messages
148 // to stderr.
149 static void IgnoreThisTest();
151 // A DeathCheck method that verifies that the test completed succcessfully.
152 // This is the default test mode for SANDBOX_TEST(). The "aux" parameter
153 // of this DeathCheck is unused (and thus unnamed)
154 static void DeathSuccess(int status, const std::string& msg, const void*);
156 // A DeathCheck method that verifies that the test completed succcessfully
157 // allowing for log error messages.
158 static void DeathSuccessAllowNoise(int status,
159 const std::string& msg,
160 const void*);
162 // A DeathCheck method that verifies that the test completed with error
163 // code "1" and printed a message containing a particular substring. The
164 // "aux" pointer should point to a C-string containing the expected error
165 // message. This method is useful for checking assertion failures such as
166 // in SANDBOX_ASSERT() and/or SANDBOX_DIE().
167 static void DeathMessage(int status, const std::string& msg, const void* aux);
169 // Like DeathMessage() but the process must be terminated with a segmentation
170 // fault.
171 // Implementation detail: On Linux (but not on Android), this does check for
172 // the return value of our default signal handler rather than for the actual
173 // reception of a SIGSEGV.
174 // TODO(jln): make this more robust.
175 static void DeathSEGVMessage(int status,
176 const std::string& msg,
177 const void* aux);
179 // A DeathCheck method that verifies that the test completed with a
180 // particular exit code. If the test output any messages to stderr, they are
181 // silently ignored. The expected exit code should be passed in by
182 // casting the its "int" value to a "void *", which is then used for "aux".
183 static void DeathExitCode(int status,
184 const std::string& msg,
185 const void* aux);
187 // A DeathCheck method that verifies that the test was terminated by a
188 // particular signal. If the test output any messages to stderr, they are
189 // silently ignore. The expected signal number should be passed in by
190 // casting the its "int" value to a "void *", which is then used for "aux".
191 static void DeathBySignal(int status,
192 const std::string& msg,
193 const void* aux);
195 private:
196 DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests);
199 } // namespace
201 #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H_