unit_tests.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifndef SANDBOX_LINUX_TESTS_UNIT_TESTS_H_
  5. #define SANDBOX_LINUX_TESTS_UNIT_TESTS_H_
  6. #include <sys/syscall.h>
  7. #include "build/build_config.h"
  8. #include "sandbox/linux/tests/sandbox_test_runner_function_pointer.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace sandbox {
  11. // Different platforms use different symbols for the six-argument version
  12. // of the mmap() system call. Test for the correct symbol at compile time.
  13. #ifdef __NR_mmap2
  14. const int kMMapNr = __NR_mmap2;
  15. #else
  16. const int kMMapNr = __NR_mmap;
  17. #endif
  18. // Has this been compiled to run on Android?
  19. bool IsAndroid();
  20. bool IsArchitectureArm();
  21. #if defined(ADDRESS_SANITIZER)
  22. #define DISABLE_ON_ASAN(test_name) DISABLED_##test_name
  23. #else
  24. #define DISABLE_ON_ASAN(test_name) test_name
  25. #endif // defined(ADDRESS_SANITIZER)
  26. #if defined(LEAK_SANITIZER)
  27. #define DISABLE_ON_LSAN(test_name) DISABLED_##test_name
  28. #else
  29. #define DISABLE_ON_LSAN(test_name) test_name
  30. #endif
  31. #if defined(THREAD_SANITIZER)
  32. #define DISABLE_ON_TSAN(test_name) DISABLED_##test_name
  33. #else
  34. #define DISABLE_ON_TSAN(test_name) test_name
  35. #endif // defined(THREAD_SANITIZER)
  36. #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
  37. defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
  38. defined(UNDEFINED_SANITIZER) || defined(SANITIZER_COVERAGE)
  39. #define DISABLE_ON_SANITIZERS(test_name) DISABLED_##test_name
  40. #else
  41. #define DISABLE_ON_SANITIZERS(test_name) test_name
  42. #endif
  43. #if BUILDFLAG(IS_ANDROID)
  44. #define DISABLE_ON_ANDROID(test_name) DISABLED_##test_name
  45. #else
  46. #define DISABLE_ON_ANDROID(test_name) test_name
  47. #endif
  48. // While it is perfectly OK for a complex test to provide its own DeathCheck
  49. // function. Most death tests have very simple requirements. These tests should
  50. // use one of the predefined DEATH_XXX macros as an argument to
  51. // SANDBOX_DEATH_TEST(). You can check for a (sub-)string in the output of the
  52. // test, for a particular exit code, or for a particular death signal.
  53. // NOTE: If you do decide to write your own DeathCheck, make sure to use
  54. // gtests's ASSERT_XXX() macros instead of SANDBOX_ASSERT(). See
  55. // unit_tests.cc for examples.
  56. #define DEATH_SUCCESS() sandbox::UnitTests::DeathSuccess, NULL
  57. #define DEATH_SUCCESS_ALLOW_NOISE() \
  58. sandbox::UnitTests::DeathSuccessAllowNoise, NULL
  59. #define DEATH_MESSAGE(msg) \
  60. sandbox::UnitTests::DeathMessage, \
  61. static_cast<const void*>(static_cast<const char*>(msg))
  62. #define DEATH_SEGV_MESSAGE(msg) \
  63. sandbox::UnitTests::DeathSEGVMessage, \
  64. static_cast<const void*>(static_cast<const char*>(msg))
  65. #define DEATH_EXIT_CODE(rc) \
  66. sandbox::UnitTests::DeathExitCode, \
  67. reinterpret_cast<void*>(static_cast<intptr_t>(rc))
  68. #define DEATH_BY_SIGNAL(s) \
  69. sandbox::UnitTests::DeathBySignal, \
  70. reinterpret_cast<void*>(static_cast<intptr_t>(s))
  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. // Define a new test case that runs inside of a GTest death test. This is
  83. // necessary, as most of our tests by definition make global and irreversible
  84. // changes to the system (i.e. they install a sandbox). GTest provides death
  85. // tests as a tool to isolate global changes from the rest of the tests.
  86. #define SANDBOX_TEST(test_case_name, test_name) \
  87. SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS())
  88. // SANDBOX_TEST_ALLOW_NOISE is just like SANDBOX_TEST, except it does not
  89. // consider log error messages printed by the test to be test failures.
  90. #define SANDBOX_TEST_ALLOW_NOISE(test_case_name, test_name) \
  91. SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS_ALLOW_NOISE())
  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. #define SANDBOX_ASSERT_EQ(x, y) SANDBOX_ASSERT((x) == (y))
  99. #define SANDBOX_ASSERT_NE(x, y) SANDBOX_ASSERT((x) != (y))
  100. #define SANDBOX_ASSERT_LT(x, y) SANDBOX_ASSERT((x) < (y))
  101. #define SANDBOX_ASSERT_GT(x, y) SANDBOX_ASSERT((x) > (y))
  102. #define SANDBOX_ASSERT_LE(x, y) SANDBOX_ASSERT((x) <= (y))
  103. #define SANDBOX_ASSERT_GE(x, y) SANDBOX_ASSERT((x) >= (y))
  104. // This class allows to run unittests in their own process. The main method is
  105. // RunTestInProcess().
  106. class UnitTests {
  107. public:
  108. typedef void (*DeathCheck)(int status,
  109. const std::string& msg,
  110. const void* aux);
  111. UnitTests() = delete;
  112. UnitTests(const UnitTests&) = delete;
  113. UnitTests& operator=(const UnitTests&) = delete;
  114. // Runs a test inside a short-lived process. Do not call this function
  115. // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing
  116. // functions make global irreversible changes to the execution environment
  117. // and must therefore execute in their own isolated process.
  118. // |test_runner| must implement the SandboxTestRunner interface and will run
  119. // in a subprocess.
  120. // Note: since the child process (created with fork()) will never return from
  121. // RunTestInProcess(), |test_runner| is guaranteed to exist for the lifetime
  122. // of the child process.
  123. static void RunTestInProcess(SandboxTestRunner* test_runner,
  124. DeathCheck death,
  125. const void* death_aux);
  126. // Report a useful error message and terminate the current SANDBOX_TEST().
  127. // Calling this function from outside a SANDBOX_TEST() is unlikely to do
  128. // anything useful.
  129. static void AssertionFailure(const char* expr, const char* file, int line);
  130. // Sometimes we determine at run-time that a test should be disabled.
  131. // Call this method if we want to return from a test and completely
  132. // ignore its results.
  133. // You should not call this method, if the test already ran any test-relevant
  134. // code. Most notably, you should not call it, you already wrote any messages
  135. // to stderr.
  136. static void IgnoreThisTest();
  137. // A DeathCheck method that verifies that the test completed successfully.
  138. // This is the default test mode for SANDBOX_TEST(). The "aux" parameter
  139. // of this DeathCheck is unused (and thus unnamed)
  140. static void DeathSuccess(int status, const std::string& msg, const void*);
  141. // A DeathCheck method that verifies that the test completed successfully
  142. // allowing for log error messages.
  143. static void DeathSuccessAllowNoise(int status,
  144. const std::string& msg,
  145. const void*);
  146. // A DeathCheck method that verifies that the test completed with error
  147. // code "1" and printed a message containing a particular substring. The
  148. // "aux" pointer should point to a C-string containing the expected error
  149. // message. This method is useful for checking assertion failures such as
  150. // in SANDBOX_ASSERT() and/or SANDBOX_DIE().
  151. static void DeathMessage(int status, const std::string& msg, const void* aux);
  152. // Like DeathMessage() but the process must be terminated with a segmentation
  153. // fault.
  154. // Implementation detail: On Linux (but not on Android), this does check for
  155. // the return value of our default signal handler rather than for the actual
  156. // reception of a SIGSEGV.
  157. // TODO(jln): make this more robust.
  158. static void DeathSEGVMessage(int status,
  159. const std::string& msg,
  160. const void* aux);
  161. // A DeathCheck method that verifies that the test completed with a
  162. // particular exit code. If the test output any messages to stderr, they are
  163. // silently ignored. The expected exit code should be passed in by
  164. // casting the its "int" value to a "void *", which is then used for "aux".
  165. static void DeathExitCode(int status,
  166. const std::string& msg,
  167. const void* aux);
  168. // A DeathCheck method that verifies that the test was terminated by a
  169. // particular signal. If the test output any messages to stderr, they are
  170. // silently ignore. The expected signal number should be passed in by
  171. // casting the its "int" value to a "void *", which is then used for "aux".
  172. static void DeathBySignal(int status,
  173. const std::string& msg,
  174. const void* aux);
  175. };
  176. } // namespace
  177. #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H_