- add sources.
[platform/framework/web/crosswalk.git] / src / sandbox / linux / tests / unit_tests.h
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 "testing/gtest/include/gtest/gtest.h"
11
12 namespace sandbox {
13
14 // Has this been compiled to run on Android?
15 bool IsAndroid();
16
17 bool IsArchitectureArm();
18
19 // Is Valgrind currently being used?
20 bool IsRunningOnValgrind();
21
22 #if defined(THREAD_SANITIZER)
23 #define DISABLE_ON_TSAN(test_name) DISABLED_##test_name
24 #else
25 #define DISABLE_ON_TSAN(test_name) test_name
26 #endif  // defined(THREAD_SANITIZER)
27
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)  sandbox::UnitTests::DeathMessage,                 \
38                             static_cast<const void *>(                        \
39                                 static_cast<const char *>(msg))
40 #define DEATH_EXIT_CODE(rc) sandbox::UnitTests::DeathExitCode,                \
41                             reinterpret_cast<void *>(static_cast<intptr_t>(rc))
42 #define DEATH_BY_SIGNAL(s)  sandbox::UnitTests::DeathExitCode,                \
43                             reinterpret_cast<void *>(static_cast<intptr_t>(s))
44
45 // A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes
46 // that the test actually dies. The death test only passes if the death occurs
47 // in the expected fashion, as specified by "death" and "death_aux". These two
48 // parameters are typically set to one of the DEATH_XXX() macros.
49 #define SANDBOX_DEATH_TEST(test_case_name, test_name, death)                  \
50   void TEST_##test_name(void *);                                              \
51   TEST(test_case_name, test_name) {                                           \
52     sandbox::UnitTests::RunTestInProcess(TEST_##test_name, NULL, death);      \
53   }                                                                           \
54   void TEST_##test_name(void *)
55
56 // Define a new test case that runs inside of a GTest death test. This is
57 // necessary, as most of our tests by definition make global and irreversible
58 // changes to the system (i.e. they install a sandbox). GTest provides death
59 // tests as a tool to isolate global changes from the rest of the tests.
60 #define SANDBOX_TEST(test_case_name, test_name)                               \
61   SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS())
62
63 // Simple assertion macro that is compatible with running inside of a death
64 // test. We unfortunately cannot use any of the GTest macros.
65 #define SANDBOX_STR(x) #x
66 #define SANDBOX_ASSERT(expr)                                                  \
67   ((expr)                                                                     \
68    ? static_cast<void>(0)                                                     \
69    : sandbox::UnitTests::AssertionFailure(SANDBOX_STR(expr),                  \
70                                           __FILE__, __LINE__))
71
72 class UnitTests {
73  public:
74   typedef void (*Test)(void *);
75   typedef void (*DeathCheck)(int status, const std::string& msg,
76                              const void *aux);
77
78   // Runs a test inside a short-lived process. Do not call this function
79   // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing
80   // functions make global irreversible changes to the execution environment
81   // and must therefore execute in their own isolated process.
82   static void RunTestInProcess(Test test, void *arg, DeathCheck death,
83                                const void *death_aux);
84
85   // Report a useful error message and terminate the current SANDBOX_TEST().
86   // Calling this function from outside a SANDBOX_TEST() is unlikely to do
87   // anything useful.
88   static void AssertionFailure(const char *expr, const char *file, int line);
89
90   // Sometimes we determine at run-time that a test should be disabled.
91   // Call this method if we want to return from a test and completely
92   // ignore its results.
93   // You should not call this method, if the test already ran any test-relevant
94   // code. Most notably, you should not call it, you already wrote any messages
95   // to stderr.
96   static void IgnoreThisTest();
97
98   // A DeathCheck method that verifies that the test completed succcessfully.
99   // This is the default test mode for SANDBOX_TEST(). The "aux" parameter
100   // of this DeathCheck is unused (and thus unnamed)
101   static void DeathSuccess(int status, const std::string& msg, const void *);
102
103   // A DeathCheck method that verifies that the test completed with error
104   // code "1" and printed a message containing a particular substring. The
105   // "aux" pointer should point to a C-string containing the expected error
106   // message. This method is useful for checking assertion failures such as
107   // in SANDBOX_ASSERT() and/or SANDBOX_DIE().
108   static void DeathMessage(int status, const std::string& msg,
109                            const void *aux);
110
111   // A DeathCheck method that verifies that the test completed with a
112   // particular exit code. If the test output any messages to stderr, they are
113   // silently ignored. The expected exit code should be passed in by
114   // casting the its "int" value to a "void *", which is then used for "aux".
115   static void DeathExitCode(int status, const std::string& msg,
116                             const void *aux);
117
118   // A DeathCheck method that verifies that the test was terminated by a
119   // particular signal. If the test output any messages to stderr, they are
120   // silently ignore. The expected signal number should be passed in by
121   // casting the its "int" value to a "void *", which is then used for "aux".
122   static void DeathBySignal(int status, const std::string& msg,
123                             const void *aux);
124
125  private:
126   DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests);
127 };
128
129 }  // namespace
130
131 #endif  // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__