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 22 matching lines...) Expand all Loading... |
33 | 33 |
34 // While it is perfectly OK for a complex test to provide its own DeathCheck | 34 // While it is perfectly OK for a complex test to provide its own DeathCheck |
35 // function. Most death tests have very simple requirements. These tests should | 35 // function. Most death tests have very simple requirements. These tests should |
36 // use one of the predefined DEATH_XXX macros as an argument to | 36 // use one of the predefined DEATH_XXX macros as an argument to |
37 // SANDBOX_DEATH_TEST(). You can check for a (sub-)string in the output of the | 37 // SANDBOX_DEATH_TEST(). You can check for a (sub-)string in the output of the |
38 // test, for a particular exit code, or for a particular death signal. | 38 // test, for a particular exit code, or for a particular death signal. |
39 // NOTE: If you do decide to write your own DeathCheck, make sure to use | 39 // NOTE: If you do decide to write your own DeathCheck, make sure to use |
40 // gtests's ASSERT_XXX() macros instead of SANDBOX_ASSERT(). See | 40 // gtests's ASSERT_XXX() macros instead of SANDBOX_ASSERT(). See |
41 // unit_tests.cc for examples. | 41 // unit_tests.cc for examples. |
42 #define DEATH_SUCCESS() sandbox::UnitTests::DeathSuccess, NULL | 42 #define DEATH_SUCCESS() sandbox::UnitTests::DeathSuccess, NULL |
| 43 #define DEATH_SUCCESS_ALLOW_NOISE() \ |
| 44 sandbox::UnitTests::DeathSuccessAllowNoise, NULL |
43 #define DEATH_MESSAGE(msg) \ | 45 #define DEATH_MESSAGE(msg) \ |
44 sandbox::UnitTests::DeathMessage, \ | 46 sandbox::UnitTests::DeathMessage, \ |
45 static_cast<const void*>(static_cast<const char*>(msg)) | 47 static_cast<const void*>(static_cast<const char*>(msg)) |
46 #define DEATH_EXIT_CODE(rc) \ | 48 #define DEATH_EXIT_CODE(rc) \ |
47 sandbox::UnitTests::DeathExitCode, \ | 49 sandbox::UnitTests::DeathExitCode, \ |
48 reinterpret_cast<void*>(static_cast<intptr_t>(rc)) | 50 reinterpret_cast<void*>(static_cast<intptr_t>(rc)) |
49 #define DEATH_BY_SIGNAL(s) \ | 51 #define DEATH_BY_SIGNAL(s) \ |
50 sandbox::UnitTests::DeathBySignal, \ | 52 sandbox::UnitTests::DeathBySignal, \ |
51 reinterpret_cast<void*>(static_cast<intptr_t>(s)) | 53 reinterpret_cast<void*>(static_cast<intptr_t>(s)) |
52 | 54 |
53 // A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes | 55 // A SANDBOX_DEATH_TEST is just like a SANDBOX_TEST (see below), but it assumes |
54 // that the test actually dies. The death test only passes if the death occurs | 56 // that the test actually dies. The death test only passes if the death occurs |
55 // in the expected fashion, as specified by "death" and "death_aux". These two | 57 // in the expected fashion, as specified by "death" and "death_aux". These two |
56 // parameters are typically set to one of the DEATH_XXX() macros. | 58 // parameters are typically set to one of the DEATH_XXX() macros. |
57 #define SANDBOX_DEATH_TEST(test_case_name, test_name, death) \ | 59 #define SANDBOX_DEATH_TEST(test_case_name, test_name, death) \ |
58 void TEST_##test_name(void*); \ | 60 void TEST_##test_name(void*); \ |
59 TEST(test_case_name, test_name) { \ | 61 TEST(test_case_name, test_name) { \ |
60 sandbox::UnitTests::RunTestInProcess(TEST_##test_name, NULL, death); \ | 62 sandbox::UnitTests::RunTestInProcess(TEST_##test_name, NULL, death); \ |
61 } \ | 63 } \ |
62 void TEST_##test_name(void*) | 64 void TEST_##test_name(void*) |
63 | 65 |
64 // Define a new test case that runs inside of a GTest death test. This is | 66 // Define a new test case that runs inside of a GTest death test. This is |
65 // necessary, as most of our tests by definition make global and irreversible | 67 // necessary, as most of our tests by definition make global and irreversible |
66 // changes to the system (i.e. they install a sandbox). GTest provides death | 68 // changes to the system (i.e. they install a sandbox). GTest provides death |
67 // tests as a tool to isolate global changes from the rest of the tests. | 69 // tests as a tool to isolate global changes from the rest of the tests. |
68 #define SANDBOX_TEST(test_case_name, test_name) \ | 70 #define SANDBOX_TEST(test_case_name, test_name) \ |
69 SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS()) | 71 SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS()) |
70 | 72 |
| 73 // SANDBOX_TEST_ALLOW_NOISE is just like SANDBOX_TEST, except it does not |
| 74 // consider log error messages printed by the test to be test failures. |
| 75 #define SANDBOX_TEST_ALLOW_NOISE(test_case_name, test_name) \ |
| 76 SANDBOX_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS_ALLOW_NOISE()) |
| 77 |
71 // Simple assertion macro that is compatible with running inside of a death | 78 // Simple assertion macro that is compatible with running inside of a death |
72 // test. We unfortunately cannot use any of the GTest macros. | 79 // test. We unfortunately cannot use any of the GTest macros. |
73 #define SANDBOX_STR(x) #x | 80 #define SANDBOX_STR(x) #x |
74 #define SANDBOX_ASSERT(expr) \ | 81 #define SANDBOX_ASSERT(expr) \ |
75 ((expr) ? static_cast<void>(0) : sandbox::UnitTests::AssertionFailure( \ | 82 ((expr) ? static_cast<void>(0) : sandbox::UnitTests::AssertionFailure( \ |
76 SANDBOX_STR(expr), __FILE__, __LINE__)) | 83 SANDBOX_STR(expr), __FILE__, __LINE__)) |
77 | 84 |
78 class UnitTests { | 85 class UnitTests { |
79 public: | 86 public: |
80 typedef void (*Test)(void*); | 87 typedef void (*Test)(void*); |
(...skipping 21 matching lines...) Expand all Loading... |
102 // You should not call this method, if the test already ran any test-relevant | 109 // You should not call this method, if the test already ran any test-relevant |
103 // code. Most notably, you should not call it, you already wrote any messages | 110 // code. Most notably, you should not call it, you already wrote any messages |
104 // to stderr. | 111 // to stderr. |
105 static void IgnoreThisTest(); | 112 static void IgnoreThisTest(); |
106 | 113 |
107 // A DeathCheck method that verifies that the test completed succcessfully. | 114 // A DeathCheck method that verifies that the test completed succcessfully. |
108 // This is the default test mode for SANDBOX_TEST(). The "aux" parameter | 115 // This is the default test mode for SANDBOX_TEST(). The "aux" parameter |
109 // of this DeathCheck is unused (and thus unnamed) | 116 // of this DeathCheck is unused (and thus unnamed) |
110 static void DeathSuccess(int status, const std::string& msg, const void*); | 117 static void DeathSuccess(int status, const std::string& msg, const void*); |
111 | 118 |
| 119 // A DeathCheck method that verifies that the test completed succcessfully |
| 120 // allowing for log error messages. |
| 121 static void DeathSuccessAllowNoise(int status, |
| 122 const std::string& msg, |
| 123 const void*); |
| 124 |
112 // A DeathCheck method that verifies that the test completed with error | 125 // A DeathCheck method that verifies that the test completed with error |
113 // code "1" and printed a message containing a particular substring. The | 126 // code "1" and printed a message containing a particular substring. The |
114 // "aux" pointer should point to a C-string containing the expected error | 127 // "aux" pointer should point to a C-string containing the expected error |
115 // message. This method is useful for checking assertion failures such as | 128 // message. This method is useful for checking assertion failures such as |
116 // in SANDBOX_ASSERT() and/or SANDBOX_DIE(). | 129 // in SANDBOX_ASSERT() and/or SANDBOX_DIE(). |
117 static void DeathMessage(int status, const std::string& msg, const void* aux); | 130 static void DeathMessage(int status, const std::string& msg, const void* aux); |
118 | 131 |
119 // A DeathCheck method that verifies that the test completed with a | 132 // A DeathCheck method that verifies that the test completed with a |
120 // particular exit code. If the test output any messages to stderr, they are | 133 // particular exit code. If the test output any messages to stderr, they are |
121 // silently ignored. The expected exit code should be passed in by | 134 // silently ignored. The expected exit code should be passed in by |
(...skipping 10 matching lines...) Expand all Loading... |
132 const std::string& msg, | 145 const std::string& msg, |
133 const void* aux); | 146 const void* aux); |
134 | 147 |
135 private: | 148 private: |
136 DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests); | 149 DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests); |
137 }; | 150 }; |
138 | 151 |
139 } // namespace | 152 } // namespace |
140 | 153 |
141 #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ | 154 #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
OLD | NEW |