• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "build/build_config.h"
10 #include "sandbox/linux/tests/sandbox_test_runner_function_pointer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace sandbox {
14 
15 // Has this been compiled to run on Android?
16 bool IsAndroid();
17 
18 bool IsArchitectureArm();
19 
20 // Is Valgrind currently being used?
21 bool IsRunningOnValgrind();
22 
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)
28 
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
34 
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)
40 
41 #if defined(OS_ANDROID)
42 #define DISABLE_ON_ANDROID(test_name) DISABLED_##test_name
43 #else
44 #define DISABLE_ON_ANDROID(test_name) test_name
45 #endif
46 
47 // While it is perfectly OK for a complex test to provide its own DeathCheck
48 // function. Most death tests have very simple requirements. These tests should
49 // use one of the predefined DEATH_XXX macros as an argument to
50 // SANDBOX_DEATH_TEST(). You can check for a (sub-)string in the output of the
51 // test, for a particular exit code, or for a particular death signal.
52 // NOTE: If you do decide to write your own DeathCheck, make sure to use
53 //       gtests's ASSERT_XXX() macros instead of SANDBOX_ASSERT(). See
54 //       unit_tests.cc for examples.
55 #define DEATH_SUCCESS() sandbox::UnitTests::DeathSuccess, NULL
56 #define DEATH_SUCCESS_ALLOW_NOISE() \
57   sandbox::UnitTests::DeathSuccessAllowNoise, NULL
58 #define DEATH_MESSAGE(msg)          \
59   sandbox::UnitTests::DeathMessage, \
60       static_cast<const void*>(static_cast<const char*>(msg))
61 #define DEATH_EXIT_CODE(rc)          \
62   sandbox::UnitTests::DeathExitCode, \
63       reinterpret_cast<void*>(static_cast<intptr_t>(rc))
64 #define DEATH_BY_SIGNAL(s)           \
65   sandbox::UnitTests::DeathBySignal, \
66       reinterpret_cast<void*>(static_cast<intptr_t>(s))
67 
68 // A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes
69 // that the test actually dies. The death test only passes if the death occurs
70 // in the expected fashion, as specified by "death" and "death_aux". These two
71 // parameters are typically set to one of the DEATH_XXX() macros.
72 #define SANDBOX_DEATH_TEST(test_case_name, test_name, death)                \
73   void TEST_##test_name(void);                                              \
74   TEST(test_case_name, test_name) {                                         \
75     SandboxTestRunnerFunctionPointer sandbox_test_runner(TEST_##test_name); \
76     sandbox::UnitTests::RunTestInProcess(&sandbox_test_runner, death);      \
77   }                                                                         \
78   void TEST_##test_name(void)
79 
80 // Define a new test case that runs inside of a GTest death test. This is
81 // necessary, as most of our tests by definition make global and irreversible
82 // changes to the system (i.e. they install a sandbox). GTest provides death
83 // tests as a tool to isolate global changes from the rest of the tests.
84 #define SANDBOX_TEST(test_case_name, test_name) \
85   SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS())
86 
87 // SANDBOX_TEST_ALLOW_NOISE is just like SANDBOX_TEST, except it does not
88 // consider log error messages printed by the test to be test failures.
89 #define SANDBOX_TEST_ALLOW_NOISE(test_case_name, test_name) \
90   SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS_ALLOW_NOISE())
91 
92 // Simple assertion macro that is compatible with running inside of a death
93 // test. We unfortunately cannot use any of the GTest macros.
94 #define SANDBOX_STR(x) #x
95 #define SANDBOX_ASSERT(expr)                                             \
96   ((expr) ? static_cast<void>(0) : sandbox::UnitTests::AssertionFailure( \
97                                        SANDBOX_STR(expr), __FILE__, __LINE__))
98 
99 // This class allows to run unittests in their own process. The main method is
100 // RunTestInProcess().
101 class UnitTests {
102  public:
103   typedef void (*DeathCheck)(int status,
104                              const std::string& msg,
105                              const void* aux);
106 
107   // Runs a test inside a short-lived process. Do not call this function
108   // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing
109   // functions make global irreversible changes to the execution environment
110   // and must therefore execute in their own isolated process.
111   // |test_runner| must implement the SandboxTestRunner interface and will run
112   // in a subprocess.
113   // Note: since the child process (created with fork()) will never return from
114   // RunTestInProcess(), |test_runner| is guaranteed to exist for the lifetime
115   // of the child process.
116   static void RunTestInProcess(SandboxTestRunner* test_runner,
117                                DeathCheck death,
118                                const void* death_aux);
119 
120   // Report a useful error message and terminate the current SANDBOX_TEST().
121   // Calling this function from outside a SANDBOX_TEST() is unlikely to do
122   // anything useful.
123   static void AssertionFailure(const char* expr, const char* file, int line);
124 
125   // Sometimes we determine at run-time that a test should be disabled.
126   // Call this method if we want to return from a test and completely
127   // ignore its results.
128   // You should not call this method, if the test already ran any test-relevant
129   // code. Most notably, you should not call it, you already wrote any messages
130   // to stderr.
131   static void IgnoreThisTest();
132 
133   // A DeathCheck method that verifies that the test completed succcessfully.
134   // This is the default test mode for SANDBOX_TEST(). The "aux" parameter
135   // of this DeathCheck is unused (and thus unnamed)
136   static void DeathSuccess(int status, const std::string& msg, const void*);
137 
138   // A DeathCheck method that verifies that the test completed succcessfully
139   // allowing for log error messages.
140   static void DeathSuccessAllowNoise(int status,
141                                      const std::string& msg,
142                                      const void*);
143 
144   // A DeathCheck method that verifies that the test completed with error
145   // code "1" and printed a message containing a particular substring. The
146   // "aux" pointer should point to a C-string containing the expected error
147   // message. This method is useful for checking assertion failures such as
148   // in SANDBOX_ASSERT() and/or SANDBOX_DIE().
149   static void DeathMessage(int status, const std::string& msg, const void* aux);
150 
151   // A DeathCheck method that verifies that the test completed with a
152   // particular exit code. If the test output any messages to stderr, they are
153   // silently ignored. The expected exit code should be passed in by
154   // casting the its "int" value to a "void *", which is then used for "aux".
155   static void DeathExitCode(int status,
156                             const std::string& msg,
157                             const void* aux);
158 
159   // A DeathCheck method that verifies that the test was terminated by a
160   // particular signal. If the test output any messages to stderr, they are
161   // silently ignore. The expected signal number should be passed in by
162   // casting the its "int" value to a "void *", which is then used for "aux".
163   static void DeathBySignal(int status,
164                             const std::string& msg,
165                             const void* aux);
166 
167  private:
168   DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests);
169 };
170 
171 }  // namespace
172 
173 #endif  // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__
174