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(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_SEGV_MESSAGE(msg)         \
62  sandbox::UnitTests::DeathSEGVMessage, \
63      static_cast<const void*>(static_cast<const char*>(msg))
64#define DEATH_EXIT_CODE(rc)          \
65  sandbox::UnitTests::DeathExitCode, \
66      reinterpret_cast<void*>(static_cast<intptr_t>(rc))
67#define DEATH_BY_SIGNAL(s)           \
68  sandbox::UnitTests::DeathBySignal, \
69      reinterpret_cast<void*>(static_cast<intptr_t>(s))
70
71// A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes
72// that the test actually dies. The death test only passes if the death occurs
73// in the expected fashion, as specified by "death" and "death_aux". These two
74// parameters are typically set to one of the DEATH_XXX() macros.
75#define SANDBOX_DEATH_TEST(test_case_name, test_name, death)                \
76  void TEST_##test_name(void);                                              \
77  TEST(test_case_name, test_name) {                                         \
78    SandboxTestRunnerFunctionPointer sandbox_test_runner(TEST_##test_name); \
79    sandbox::UnitTests::RunTestInProcess(&sandbox_test_runner, death);      \
80  }                                                                         \
81  void TEST_##test_name(void)
82
83// Define a new test case that runs inside of a GTest death test. This is
84// necessary, as most of our tests by definition make global and irreversible
85// changes to the system (i.e. they install a sandbox). GTest provides death
86// tests as a tool to isolate global changes from the rest of the tests.
87#define SANDBOX_TEST(test_case_name, test_name) \
88  SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS())
89
90// SANDBOX_TEST_ALLOW_NOISE is just like SANDBOX_TEST, except it does not
91// consider log error messages printed by the test to be test failures.
92#define SANDBOX_TEST_ALLOW_NOISE(test_case_name, test_name) \
93  SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS_ALLOW_NOISE())
94
95// Simple assertion macro that is compatible with running inside of a death
96// test. We unfortunately cannot use any of the GTest macros.
97#define SANDBOX_STR(x) #x
98#define SANDBOX_ASSERT(expr)                                             \
99  ((expr) ? static_cast<void>(0) : sandbox::UnitTests::AssertionFailure( \
100                                       SANDBOX_STR(expr), __FILE__, __LINE__))
101
102// This class allows to run unittests in their own process. The main method is
103// RunTestInProcess().
104class UnitTests {
105 public:
106  typedef void (*DeathCheck)(int status,
107                             const std::string& msg,
108                             const void* aux);
109
110  // Runs a test inside a short-lived process. Do not call this function
111  // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing
112  // functions make global irreversible changes to the execution environment
113  // and must therefore execute in their own isolated process.
114  // |test_runner| must implement the SandboxTestRunner interface and will run
115  // in a subprocess.
116  // Note: since the child process (created with fork()) will never return from
117  // RunTestInProcess(), |test_runner| is guaranteed to exist for the lifetime
118  // of the child process.
119  static void RunTestInProcess(SandboxTestRunner* test_runner,
120                               DeathCheck death,
121                               const void* death_aux);
122
123  // Report a useful error message and terminate the current SANDBOX_TEST().
124  // Calling this function from outside a SANDBOX_TEST() is unlikely to do
125  // anything useful.
126  static void AssertionFailure(const char* expr, const char* file, int line);
127
128  // Sometimes we determine at run-time that a test should be disabled.
129  // Call this method if we want to return from a test and completely
130  // ignore its results.
131  // You should not call this method, if the test already ran any test-relevant
132  // code. Most notably, you should not call it, you already wrote any messages
133  // to stderr.
134  static void IgnoreThisTest();
135
136  // A DeathCheck method that verifies that the test completed succcessfully.
137  // This is the default test mode for SANDBOX_TEST(). The "aux" parameter
138  // of this DeathCheck is unused (and thus unnamed)
139  static void DeathSuccess(int status, const std::string& msg, const void*);
140
141  // A DeathCheck method that verifies that the test completed succcessfully
142  // allowing for log error messages.
143  static void DeathSuccessAllowNoise(int status,
144                                     const std::string& msg,
145                                     const void*);
146
147  // A DeathCheck method that verifies that the test completed with error
148  // code "1" and printed a message containing a particular substring. The
149  // "aux" pointer should point to a C-string containing the expected error
150  // message. This method is useful for checking assertion failures such as
151  // in SANDBOX_ASSERT() and/or SANDBOX_DIE().
152  static void DeathMessage(int status, const std::string& msg, const void* aux);
153
154  // Like DeathMessage() but the process must be terminated with a segmentation
155  // fault.
156  // Implementation detail: On Linux (but not on Android), this does check for
157  // the return value of our default signal handler rather than for the actual
158  // reception of a SIGSEGV.
159  // TODO(jln): make this more robust.
160  static void DeathSEGVMessage(int status,
161                               const std::string& msg,
162                               const void* aux);
163
164  // A DeathCheck method that verifies that the test completed with a
165  // particular exit code. If the test output any messages to stderr, they are
166  // silently ignored. The expected exit code should be passed in by
167  // casting the its "int" value to a "void *", which is then used for "aux".
168  static void DeathExitCode(int status,
169                            const std::string& msg,
170                            const void* aux);
171
172  // A DeathCheck method that verifies that the test was terminated by a
173  // particular signal. If the test output any messages to stderr, they are
174  // silently ignore. The expected signal number should be passed in by
175  // casting the its "int" value to a "void *", which is then used for "aux".
176  static void DeathBySignal(int status,
177                            const std::string& msg,
178                            const void* aux);
179
180 private:
181  DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests);
182};
183
184}  // namespace
185
186#endif  // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__
187