tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MozHelpers.h (4293B)


      1 #ifndef TESTING_GTEST_MOZILLA_HELPERS_H_
      2 #define TESTING_GTEST_MOZILLA_HELPERS_H_
      3 
      4 #include "gtest/gtest.h"
      5 
      6 #include "mozilla/UniquePtr.h"
      7 #include "nsCOMPtr.h"
      8 #include "nsServiceManagerUtils.h"
      9 #include "nsICrashReporter.h"
     10 
     11 #if defined(DEBUG) && !defined(XP_WIN) && !defined(ANDROID)
     12 #  define HAS_GDB_SLEEP_DURATION 1
     13 extern unsigned int _gdb_sleep_duration;
     14 #endif
     15 
     16 namespace mozilla::gtest {
     17 
     18 #if defined(HAS_GDB_SLEEP_DURATION)
     19 #  define ZERO_GDB_SLEEP() _gdb_sleep_duration = 0;
     20 
     21 #  define SAVE_GDB_SLEEP(v)  \
     22    v = _gdb_sleep_duration; \
     23    ZERO_GDB_SLEEP();
     24 #  define RESTORE_GDB_SLEEP(v) _gdb_sleep_duration = v;
     25 
     26 // Some use needs to be in the global namespace
     27 #  define SAVE_GDB_SLEEP_GLOBAL(v) \
     28    v = ::_gdb_sleep_duration;     \
     29    ZERO_GDB_SLEEP();
     30 #  define RESTORE_GDB_SLEEP_GLOBAL(v) ::_gdb_sleep_duration = v;
     31 
     32 #  define SAVE_GDB_SLEEP_LOCAL()          \
     33    unsigned int _old_gdb_sleep_duration; \
     34    SAVE_GDB_SLEEP(_old_gdb_sleep_duration);
     35 #  define RESTORE_GDB_SLEEP_LOCAL() RESTORE_GDB_SLEEP(_old_gdb_sleep_duration);
     36 
     37 #else  // defined(HAS_GDB_SLEEP_DURATION)
     38 
     39 #  define ZERO_GDB_SLEEP() ;
     40 
     41 #  define SAVE_GDB_SLEEP(v)
     42 #  define SAVE_GDB_SLEEP_GLOBAL(v)
     43 #  define SAVE_GDB_SLEEP_LOCAL()
     44 #  define RESTORE_GDB_SLEEP(v)
     45 #  define RESTORE_GDB_SLEEP_GLOBAL(v)
     46 #  define RESTORE_GDB_SLEEP_LOCAL()
     47 #endif  // defined(HAS_GDB_SLEEP_DURATION)
     48 
     49 // Death tests are too slow on OSX because of the system crash reporter.
     50 #if !defined(XP_DARWIN)
     51 // Wrap ASSERT_DEATH_IF_SUPPORTED to disable the crash reporter
     52 // when entering the subprocess, so that the expected crashes don't
     53 // create a minidump that the gtest harness will interpret as an error.
     54 #  define ASSERT_DEATH_WRAP(a, b)                 \
     55    ASSERT_DEATH_IF_SUPPORTED(                    \
     56        {                                         \
     57          mozilla::gtest::DisableCrashReporter(); \
     58          a;                                      \
     59        },                                        \
     60        b)
     61 
     62 // Wrap EXPECT_DEATH_* macros to also disable the crash reporter.
     63 #  define EXPECT_DEATH_WRAP(a, b)                 \
     64    EXPECT_DEATH_IF_SUPPORTED(                    \
     65        {                                         \
     66          mozilla::gtest::DisableCrashReporter(); \
     67          a;                                      \
     68        },                                        \
     69        b)
     70 #  define EXPECT_DEBUG_DEATH_WRAP(a, b)           \
     71    EXPECT_DEBUG_DEATH(                           \
     72        {                                         \
     73          mozilla::gtest::DisableCrashReporter(); \
     74          a;                                      \
     75        },                                        \
     76        b)
     77 #else
     78 #  define ASSERT_DEATH_WRAP(a, b)
     79 #  define EXPECT_DEATH_WRAP(a, b)
     80 #  define EXPECT_DEBUG_DEATH_WRAP(a, b)
     81 #endif
     82 
     83 void DisableCrashReporter();
     84 
     85 /**
     86 * Exit mode used for ScopedTestResultReporter.
     87 */
     88 enum class ExitMode {
     89  // The user of the reporter handles exit.
     90  NoExit,
     91  // As the reporter goes out of scope, exit with ExitCode().
     92  ExitOnDtor,
     93 };
     94 
     95 /**
     96 * Status used by ScopedTestResultReporter.
     97 */
     98 enum class TestResultStatus : int {
     99  Pass = 0,
    100  NonFatalFailure = 1,
    101  FatalFailure = 2,
    102 };
    103 
    104 inline int ExitCode(TestResultStatus aStatus) {
    105  return static_cast<int>(aStatus);
    106 }
    107 
    108 /**
    109 * This is a helper that reports test results to stderr in death test child
    110 * processes, since that is normally disabled by default (with no way of
    111 * enabling).
    112 *
    113 * Note that for this to work as intended the death test child has to, on
    114 * failure, exit with an exit code that is unexpected to the death test parent,
    115 * so the parent can mark the test case as failed.
    116 *
    117 * If the parent expects a graceful exit (code 0), ExitCode() can be used with
    118 * Status() to exit the child process.
    119 *
    120 * For simplicity the reporter can exit automatically as it goes out of scope,
    121 * when created with ExitMode::ExitOnDtor.
    122 */
    123 class ScopedTestResultReporter {
    124 public:
    125  virtual ~ScopedTestResultReporter() = default;
    126 
    127  /**
    128   * The aggregate status of all observed test results.
    129   */
    130  virtual TestResultStatus Status() const = 0;
    131 
    132  static UniquePtr<ScopedTestResultReporter> Create(ExitMode aExitMode);
    133 };
    134 
    135 }  // namespace mozilla::gtest
    136 
    137 #endif  // TESTING_GTEST_MOZILLA_HELPERS_H_