| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ | 5 #ifndef SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
| 6 #define SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ | 6 #define SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #endif // defined(THREAD_SANITIZER) | 26 #endif // defined(THREAD_SANITIZER) |
| 27 | 27 |
| 28 // While it is perfectly OK for a complex test to provide its own DeathCheck | 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 | 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 | 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 | 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. | 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 | 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 | 34 // gtests's ASSERT_XXX() macros instead of SANDBOX_ASSERT(). See |
| 35 // unit_tests.cc for examples. | 35 // unit_tests.cc for examples. |
| 36 #define DEATH_SUCCESS() sandbox::UnitTests::DeathSuccess, NULL | 36 #define DEATH_SUCCESS() sandbox::UnitTests::DeathSuccess, NULL |
| 37 #define DEATH_MESSAGE(msg) sandbox::UnitTests::DeathMessage, \ | 37 #define DEATH_MESSAGE(msg) \ |
| 38 static_cast<const void *>( \ | 38 sandbox::UnitTests::DeathMessage, \ |
| 39 static_cast<const char *>(msg)) | 39 static_cast<const void*>(static_cast<const char*>(msg)) |
| 40 #define DEATH_EXIT_CODE(rc) sandbox::UnitTests::DeathExitCode, \ | 40 #define DEATH_EXIT_CODE(rc) \ |
| 41 reinterpret_cast<void *>(static_cast<intptr_t>(rc)) | 41 sandbox::UnitTests::DeathExitCode, \ |
| 42 #define DEATH_BY_SIGNAL(s) sandbox::UnitTests::DeathExitCode, \ | 42 reinterpret_cast<void*>(static_cast<intptr_t>(rc)) |
| 43 reinterpret_cast<void *>(static_cast<intptr_t>(s)) | 43 #define DEATH_BY_SIGNAL(s) \ |
| 44 sandbox::UnitTests::DeathExitCode, \ |
| 45 reinterpret_cast<void*>(static_cast<intptr_t>(s)) |
| 44 | 46 |
| 45 // A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes | 47 // 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 | 48 // 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 | 49 // 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. | 50 // parameters are typically set to one of the DEATH_XXX() macros. |
| 49 #define SANDBOX_DEATH_TEST(test_case_name, test_name, death) \ | 51 #define SANDBOX_DEATH_TEST(test_case_name, test_name, death) \ |
| 50 void TEST_##test_name(void *); \ | 52 void TEST_##test_name(void*); \ |
| 51 TEST(test_case_name, test_name) { \ | 53 TEST(test_case_name, test_name) { \ |
| 52 sandbox::UnitTests::RunTestInProcess(TEST_##test_name, NULL, death); \ | 54 sandbox::UnitTests::RunTestInProcess(TEST_##test_name, NULL, death); \ |
| 53 } \ | 55 } \ |
| 54 void TEST_##test_name(void *) | 56 void TEST_##test_name(void*) |
| 55 | 57 |
| 56 // Define a new test case that runs inside of a GTest death test. This is | 58 // 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 | 59 // 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 | 60 // 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. | 61 // tests as a tool to isolate global changes from the rest of the tests. |
| 60 #define SANDBOX_TEST(test_case_name, test_name) \ | 62 #define SANDBOX_TEST(test_case_name, test_name) \ |
| 61 SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS()) | 63 SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS()) |
| 62 | 64 |
| 63 // Simple assertion macro that is compatible with running inside of a death | 65 // Simple assertion macro that is compatible with running inside of a death |
| 64 // test. We unfortunately cannot use any of the GTest macros. | 66 // test. We unfortunately cannot use any of the GTest macros. |
| 65 #define SANDBOX_STR(x) #x | 67 #define SANDBOX_STR(x) #x |
| 66 #define SANDBOX_ASSERT(expr) \ | 68 #define SANDBOX_ASSERT(expr) \ |
| 67 ((expr) \ | 69 ((expr) ? static_cast<void>(0) : sandbox::UnitTests::AssertionFailure( \ |
| 68 ? static_cast<void>(0) \ | 70 SANDBOX_STR(expr), __FILE__, __LINE__)) |
| 69 : sandbox::UnitTests::AssertionFailure(SANDBOX_STR(expr), \ | |
| 70 __FILE__, __LINE__)) | |
| 71 | 71 |
| 72 class UnitTests { | 72 class UnitTests { |
| 73 public: | 73 public: |
| 74 typedef void (*Test)(void *); | 74 typedef void (*Test)(void*); |
| 75 typedef void (*DeathCheck)(int status, const std::string& msg, | 75 typedef void (*DeathCheck)(int status, |
| 76 const void *aux); | 76 const std::string& msg, |
| 77 const void* aux); |
| 77 | 78 |
| 78 // Runs a test inside a short-lived process. Do not call this function | 79 // 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 // directly. It is automatically invoked by SANDBOX_TEST(). Most sandboxing |
| 80 // functions make global irreversible changes to the execution environment | 81 // functions make global irreversible changes to the execution environment |
| 81 // and must therefore execute in their own isolated process. | 82 // and must therefore execute in their own isolated process. |
| 82 static void RunTestInProcess(Test test, void *arg, DeathCheck death, | 83 static void RunTestInProcess(Test test, |
| 83 const void *death_aux); | 84 void* arg, |
| 85 DeathCheck death, |
| 86 const void* death_aux); |
| 84 | 87 |
| 85 // Report a useful error message and terminate the current SANDBOX_TEST(). | 88 // Report a useful error message and terminate the current SANDBOX_TEST(). |
| 86 // Calling this function from outside a SANDBOX_TEST() is unlikely to do | 89 // Calling this function from outside a SANDBOX_TEST() is unlikely to do |
| 87 // anything useful. | 90 // anything useful. |
| 88 static void AssertionFailure(const char *expr, const char *file, int line); | 91 static void AssertionFailure(const char* expr, const char* file, int line); |
| 89 | 92 |
| 90 // Sometimes we determine at run-time that a test should be disabled. | 93 // 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 | 94 // Call this method if we want to return from a test and completely |
| 92 // ignore its results. | 95 // ignore its results. |
| 93 // You should not call this method, if the test already ran any test-relevant | 96 // 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 | 97 // code. Most notably, you should not call it, you already wrote any messages |
| 95 // to stderr. | 98 // to stderr. |
| 96 static void IgnoreThisTest(); | 99 static void IgnoreThisTest(); |
| 97 | 100 |
| 98 // A DeathCheck method that verifies that the test completed succcessfully. | 101 // A DeathCheck method that verifies that the test completed succcessfully. |
| 99 // This is the default test mode for SANDBOX_TEST(). The "aux" parameter | 102 // This is the default test mode for SANDBOX_TEST(). The "aux" parameter |
| 100 // of this DeathCheck is unused (and thus unnamed) | 103 // of this DeathCheck is unused (and thus unnamed) |
| 101 static void DeathSuccess(int status, const std::string& msg, const void *); | 104 static void DeathSuccess(int status, const std::string& msg, const void*); |
| 102 | 105 |
| 103 // A DeathCheck method that verifies that the test completed with error | 106 // A DeathCheck method that verifies that the test completed with error |
| 104 // code "1" and printed a message containing a particular substring. The | 107 // code "1" and printed a message containing a particular substring. The |
| 105 // "aux" pointer should point to a C-string containing the expected error | 108 // "aux" pointer should point to a C-string containing the expected error |
| 106 // message. This method is useful for checking assertion failures such as | 109 // message. This method is useful for checking assertion failures such as |
| 107 // in SANDBOX_ASSERT() and/or SANDBOX_DIE(). | 110 // in SANDBOX_ASSERT() and/or SANDBOX_DIE(). |
| 108 static void DeathMessage(int status, const std::string& msg, | 111 static void DeathMessage(int status, const std::string& msg, const void* aux); |
| 109 const void *aux); | |
| 110 | 112 |
| 111 // A DeathCheck method that verifies that the test completed with a | 113 // 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 | 114 // 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 | 115 // 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". | 116 // casting the its "int" value to a "void *", which is then used for "aux". |
| 115 static void DeathExitCode(int status, const std::string& msg, | 117 static void DeathExitCode(int status, |
| 116 const void *aux); | 118 const std::string& msg, |
| 119 const void* aux); |
| 117 | 120 |
| 118 // A DeathCheck method that verifies that the test was terminated by a | 121 // 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 | 122 // particular signal. If the test output any messages to stderr, they are |
| 120 // silently ignore. The expected signal number should be passed in by | 123 // 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". | 124 // casting the its "int" value to a "void *", which is then used for "aux". |
| 122 static void DeathBySignal(int status, const std::string& msg, | 125 static void DeathBySignal(int status, |
| 123 const void *aux); | 126 const std::string& msg, |
| 127 const void* aux); |
| 124 | 128 |
| 125 private: | 129 private: |
| 126 DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests); | 130 DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests); |
| 127 }; | 131 }; |
| 128 | 132 |
| 129 } // namespace | 133 } // namespace |
| 130 | 134 |
| 131 #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ | 135 #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
| OLD | NEW |