// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef SANDBOX_LINUX_TESTS_UNIT_TESTS_H_ #define SANDBOX_LINUX_TESTS_UNIT_TESTS_H_ #include <sys/syscall.h> #include "build/build_config.h" #include "sandbox/linux/tests/sandbox_test_runner_function_pointer.h" #include "testing/gtest/include/gtest/gtest.h" namespace sandbox { // Different platforms use different symbols for the six-argument version // of the mmap() system call. Test for the correct symbol at compile time. #ifdef __NR_mmap2 const int kMMapNr = __NR_mmap2; #else const int kMMapNr = …; #endif // Has this been compiled to run on Android? bool IsAndroid(); bool IsArchitectureArm(); #if defined(ADDRESS_SANITIZER) #define DISABLE_ON_ASAN … #else #define DISABLE_ON_ASAN(test_name) … #endif // defined(ADDRESS_SANITIZER) #if defined(LEAK_SANITIZER) #define DISABLE_ON_LSAN … #else #define DISABLE_ON_LSAN(test_name) … #endif #if defined(THREAD_SANITIZER) #define DISABLE_ON_TSAN … #else #define DISABLE_ON_TSAN(test_name) … #endif // defined(THREAD_SANITIZER) #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \ defined(UNDEFINED_SANITIZER) || defined(SANITIZER_COVERAGE) #define DISABLE_ON_SANITIZERS … #else #define DISABLE_ON_SANITIZERS(test_name) … #endif #if BUILDFLAG(IS_ANDROID) #define DISABLE_ON_ANDROID … #else #define DISABLE_ON_ANDROID(test_name) … #endif // While it is perfectly OK for a complex test to provide its own DeathCheck // function. Most death tests have very simple requirements. These tests should // use one of the predefined DEATH_XXX macros as an argument to // SANDBOX_DEATH_TEST(). You can check for a (sub-)string in the output of the // test, for a particular exit code, or for a particular death signal. // NOTE: If you do decide to write your own DeathCheck, make sure to use // gtests's ASSERT_XXX() macros instead of SANDBOX_ASSERT(). See // unit_tests.cc for examples. #define DEATH_SUCCESS() … #define DEATH_SUCCESS_ALLOW_NOISE() … #define DEATH_MESSAGE(msg) … #define DEATH_SEGV_MESSAGE(msg) … #define DEATH_EXIT_CODE(rc) … #define DEATH_BY_SIGNAL(s) … // A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes // that the test actually dies. The death test only passes if the death occurs // in the expected fashion, as specified by "death" and "death_aux". These two // parameters are typically set to one of the DEATH_XXX() macros. #define SANDBOX_DEATH_TEST(test_case_name, test_name, death) … // Define a new test case that runs inside of a GTest death test. This is // necessary, as most of our tests by definition make global and irreversible // changes to the system (i.e. they install a sandbox). GTest provides death // tests as a tool to isolate global changes from the rest of the tests. #define SANDBOX_TEST(test_case_name, test_name) … // SANDBOX_TEST_ALLOW_NOISE is just like SANDBOX_TEST, except it does not // consider log error messages printed by the test to be test failures. #define SANDBOX_TEST_ALLOW_NOISE(test_case_name, test_name) … // Simple assertion macro that is compatible with running inside of a death // test. We unfortunately cannot use any of the GTest macros. #define SANDBOX_STR(x) … #define SANDBOX_ASSERT(expr) … #define SANDBOX_ASSERT_EQ(x, y) … #define SANDBOX_ASSERT_NE(x, y) … #define SANDBOX_ASSERT_LT(x, y) … #define SANDBOX_ASSERT_GT(x, y) … #define SANDBOX_ASSERT_LE(x, y) … #define SANDBOX_ASSERT_GE(x, y) … // This class allows to run unittests in their own process. The main method is // RunTestInProcess(). class UnitTests { … }; } // namespace #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H_