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