Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
Yabin Cui | ead0814 | 2015-02-04 20:53:56 -0800 | [diff] [blame] | 19 | #include <ctype.h> |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 20 | #include <errno.h> |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <inttypes.h> |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 23 | #include <libgen.h> |
Yabin Cui | ead0814 | 2015-02-04 20:53:56 -0800 | [diff] [blame] | 24 | #include <limits.h> |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 25 | #include <signal.h> |
Elliott Hughes | 10ba4bd | 2017-11-14 13:11:41 -0800 | [diff] [blame] | 26 | #include <spawn.h> |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 27 | #include <stdarg.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/wait.h> |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 33 | #include <chrono> |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 34 | #include <string> |
| 35 | #include <tuple> |
| 36 | #include <utility> |
| 37 | #include <vector> |
| 38 | |
Elliott Hughes | 10ba4bd | 2017-11-14 13:11:41 -0800 | [diff] [blame] | 39 | #include <android-base/file.h> |
| 40 | #include <android-base/strings.h> |
| 41 | #include <android-base/unique_fd.h> |
| 42 | |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 43 | #ifndef TEMP_FAILURE_RETRY |
| 44 | |
| 45 | /* Used to retry syscalls that can return EINTR. */ |
| 46 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 47 | __typeof__(exp) _rc; \ |
| 48 | do { \ |
| 49 | _rc = (exp); \ |
| 50 | } while (_rc == -1 && errno == EINTR); \ |
| 51 | _rc; }) |
| 52 | |
| 53 | #endif |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 54 | |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 55 | static std::string g_executable_path; |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 56 | static int g_argc; |
| 57 | static char** g_argv; |
| 58 | static char** g_envp; |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 59 | |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 60 | const std::string& get_executable_path() { |
| 61 | return g_executable_path; |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 64 | int get_argc() { |
| 65 | return g_argc; |
| 66 | } |
| 67 | |
| 68 | char** get_argv() { |
| 69 | return g_argv; |
| 70 | } |
| 71 | |
| 72 | char** get_envp() { |
| 73 | return g_envp; |
| 74 | } |
| 75 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 76 | namespace testing { |
| 77 | namespace internal { |
| 78 | |
| 79 | // Reuse of testing::internal::ColoredPrintf in gtest. |
| 80 | enum GTestColor { |
| 81 | COLOR_DEFAULT, |
| 82 | COLOR_RED, |
| 83 | COLOR_GREEN, |
| 84 | COLOR_YELLOW |
| 85 | }; |
| 86 | |
| 87 | void ColoredPrintf(GTestColor color, const char* fmt, ...); |
| 88 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 89 | } // namespace internal |
| 90 | } // namespace testing |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 91 | |
| 92 | using testing::internal::GTestColor; |
| 93 | using testing::internal::COLOR_DEFAULT; |
| 94 | using testing::internal::COLOR_RED; |
| 95 | using testing::internal::COLOR_GREEN; |
| 96 | using testing::internal::COLOR_YELLOW; |
| 97 | using testing::internal::ColoredPrintf; |
| 98 | |
Christopher Ferris | daaaed1 | 2015-09-24 18:45:53 -0700 | [diff] [blame] | 99 | constexpr int DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS = 90000; |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 100 | constexpr int DEFAULT_GLOBAL_TEST_RUN_SLOW_THRESHOLD_MS = 2000; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 101 | |
| 102 | // The time each test can run before killed for the reason of timeout. |
| 103 | // It takes effect only with --isolate option. |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 104 | static int global_test_run_deadline_ms = DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 105 | |
| 106 | // The time each test can run before be warned for too much running time. |
| 107 | // It takes effect only with --isolate option. |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 108 | static int global_test_run_slow_threshold_ms = DEFAULT_GLOBAL_TEST_RUN_SLOW_THRESHOLD_MS; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 109 | |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 110 | // Return timeout duration for a test, in ms. |
| 111 | static int GetTimeoutMs(const std::string& /*test_name*/) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 112 | return global_test_run_deadline_ms; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 115 | // Return threshold for calling a test slow, in ms. |
| 116 | static int GetSlowThresholdMs(const std::string& /*test_name*/) { |
| 117 | return global_test_run_slow_threshold_ms; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 120 | static void PrintHelpInfo() { |
| 121 | printf("Bionic Unit Test Options:\n" |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 122 | " -j [JOB_COUNT] or -j[JOB_COUNT]\n" |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 123 | " Run up to JOB_COUNT tests in parallel.\n" |
| 124 | " Use isolation mode, Run each test in a separate process.\n" |
| 125 | " If JOB_COUNT is not given, it is set to the count of available processors.\n" |
| 126 | " --no-isolate\n" |
| 127 | " Don't use isolation mode, run all tests in a single process.\n" |
| 128 | " --deadline=[TIME_IN_MS]\n" |
| 129 | " Run each test in no longer than [TIME_IN_MS] time.\n" |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 130 | " Only valid in isolation mode. Default deadline is 90000 ms.\n" |
| 131 | " --slow-threshold=[TIME_IN_MS]\n" |
| 132 | " Test running longer than [TIME_IN_MS] will be called slow.\n" |
| 133 | " Only valid in isolation mode. Default slow threshold is 2000 ms.\n" |
Yabin Cui | 11c4353 | 2015-01-28 14:28:14 -0800 | [diff] [blame] | 134 | " --gtest-filter=POSITIVE_PATTERNS[-NEGATIVE_PATTERNS]\n" |
| 135 | " Used as a synonym for --gtest_filter option in gtest.\n" |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 136 | "Default bionic unit test option is -j.\n" |
| 137 | "In isolation mode, you can send SIGQUIT to the parent process to show current\n" |
| 138 | "running tests, or send SIGINT to the parent process to stop testing and\n" |
| 139 | "clean up current running tests.\n" |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 140 | "\n"); |
| 141 | } |
| 142 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 143 | enum TestResult { |
| 144 | TEST_SUCCESS = 0, |
| 145 | TEST_FAILED, |
| 146 | TEST_TIMEOUT |
| 147 | }; |
| 148 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 149 | class Test { |
| 150 | public: |
| 151 | Test() {} // For std::vector<Test>. |
| 152 | explicit Test(const char* name) : name_(name) {} |
| 153 | |
| 154 | const std::string& GetName() const { return name_; } |
| 155 | |
Elliott Hughes | 93a89f8 | 2017-07-21 18:51:06 -0700 | [diff] [blame] | 156 | void SetResult(TestResult result) { |
| 157 | // Native xfails are inherently likely to actually be relying on undefined |
| 158 | // behavior/uninitialized memory, and thus likely to pass from time to time |
| 159 | // on CTS. Avoid that unpleasantness by just rewriting all xfail failures |
| 160 | // as successes. You'll still see the actual failure details. |
| 161 | if (GetName().find("xfail") == 0) result = TEST_SUCCESS; |
| 162 | result_ = result; |
| 163 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 164 | |
| 165 | TestResult GetResult() const { return result_; } |
| 166 | |
| 167 | void SetTestTime(int64_t elapsed_time_ns) { elapsed_time_ns_ = elapsed_time_ns; } |
| 168 | |
| 169 | int64_t GetTestTime() const { return elapsed_time_ns_; } |
| 170 | |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 171 | void AppendTestOutput(const std::string& s) { output_ += s; } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 172 | |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 173 | const std::string& GetTestOutput() const { return output_; } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 174 | |
| 175 | private: |
| 176 | const std::string name_; |
| 177 | TestResult result_; |
| 178 | int64_t elapsed_time_ns_; |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 179 | std::string output_; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 182 | class TestCase { |
| 183 | public: |
| 184 | TestCase() {} // For std::vector<TestCase>. |
| 185 | explicit TestCase(const char* name) : name_(name) {} |
| 186 | |
| 187 | const std::string& GetName() const { return name_; } |
| 188 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 189 | void AppendTest(const char* test_name) { |
| 190 | test_list_.push_back(Test(test_name)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 193 | size_t TestCount() const { return test_list_.size(); } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 194 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 195 | std::string GetTestName(size_t test_id) const { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 196 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 197 | return name_ + "." + test_list_[test_id].GetName(); |
| 198 | } |
| 199 | |
| 200 | Test& GetTest(size_t test_id) { |
| 201 | VerifyTestId(test_id); |
| 202 | return test_list_[test_id]; |
| 203 | } |
| 204 | |
| 205 | const Test& GetTest(size_t test_id) const { |
| 206 | VerifyTestId(test_id); |
| 207 | return test_list_[test_id]; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 210 | void SetTestResult(size_t test_id, TestResult result) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 211 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 212 | test_list_[test_id].SetResult(result); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 215 | TestResult GetTestResult(size_t test_id) const { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 216 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 217 | return test_list_[test_id].GetResult(); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 220 | bool GetTestSuccess(size_t test_id) const { |
Elliott Hughes | 93a89f8 | 2017-07-21 18:51:06 -0700 | [diff] [blame] | 221 | return GetTestResult(test_id) == TEST_SUCCESS; |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 224 | void SetTestTime(size_t test_id, int64_t elapsed_time_ns) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 225 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 226 | test_list_[test_id].SetTestTime(elapsed_time_ns); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 229 | int64_t GetTestTime(size_t test_id) const { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 230 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 231 | return test_list_[test_id].GetTestTime(); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | private: |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 235 | void VerifyTestId(size_t test_id) const { |
| 236 | if(test_id >= test_list_.size()) { |
| 237 | fprintf(stderr, "test_id %zu out of range [0, %zu)\n", test_id, test_list_.size()); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 238 | exit(1); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | private: |
| 243 | const std::string name_; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 244 | std::vector<Test> test_list_; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 245 | }; |
| 246 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 247 | class TestResultPrinter : public testing::EmptyTestEventListener { |
| 248 | public: |
| 249 | TestResultPrinter() : pinfo_(NULL) {} |
| 250 | virtual void OnTestStart(const testing::TestInfo& test_info) { |
| 251 | pinfo_ = &test_info; // Record test_info for use in OnTestPartResult. |
| 252 | } |
| 253 | virtual void OnTestPartResult(const testing::TestPartResult& result); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 254 | |
| 255 | private: |
| 256 | const testing::TestInfo* pinfo_; |
| 257 | }; |
| 258 | |
| 259 | // Called after an assertion failure. |
| 260 | void TestResultPrinter::OnTestPartResult(const testing::TestPartResult& result) { |
| 261 | // If the test part succeeded, we don't need to do anything. |
| 262 | if (result.type() == testing::TestPartResult::kSuccess) |
| 263 | return; |
| 264 | |
| 265 | // Print failure message from the assertion (e.g. expected this and got that). |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 266 | printf("%s:(%d) Failure in test %s.%s\n%s\n", result.file_name(), result.line_number(), |
| 267 | pinfo_->test_case_name(), pinfo_->name(), result.message()); |
| 268 | fflush(stdout); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 271 | static int64_t NanoTime() { |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 272 | std::chrono::nanoseconds duration(std::chrono::steady_clock::now().time_since_epoch()); |
| 273 | return static_cast<int64_t>(duration.count()); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static bool EnumerateTests(int argc, char** argv, std::vector<TestCase>& testcase_list) { |
Elliott Hughes | 10ba4bd | 2017-11-14 13:11:41 -0800 | [diff] [blame] | 277 | std::vector<const char*> args; |
| 278 | for (int i = 0; i < argc; ++i) args.push_back(argv[i]); |
| 279 | args.push_back("--gtest_list_tests"); |
| 280 | args.push_back(nullptr); |
| 281 | |
| 282 | // We use posix_spawn(3) rather than the simpler popen(3) because we don't want an intervening |
| 283 | // surprise shell invocation making quoting interesting for --gtest_filter (http://b/68949647). |
| 284 | |
| 285 | android::base::unique_fd read_fd; |
| 286 | android::base::unique_fd write_fd; |
| 287 | if (!android::base::Pipe(&read_fd, &write_fd)) { |
| 288 | perror("pipe"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | |
Elliott Hughes | 10ba4bd | 2017-11-14 13:11:41 -0800 | [diff] [blame] | 292 | posix_spawn_file_actions_t fa; |
| 293 | posix_spawn_file_actions_init(&fa); |
| 294 | posix_spawn_file_actions_addclose(&fa, read_fd); |
| 295 | posix_spawn_file_actions_adddup2(&fa, write_fd, 1); |
| 296 | posix_spawn_file_actions_adddup2(&fa, write_fd, 2); |
| 297 | posix_spawn_file_actions_addclose(&fa, write_fd); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 298 | |
Elliott Hughes | 10ba4bd | 2017-11-14 13:11:41 -0800 | [diff] [blame] | 299 | pid_t pid; |
| 300 | int result = posix_spawnp(&pid, argv[0], &fa, nullptr, const_cast<char**>(args.data()), nullptr); |
| 301 | posix_spawn_file_actions_destroy(&fa); |
| 302 | if (result == -1) { |
| 303 | perror("posix_spawn"); |
| 304 | return false; |
| 305 | } |
| 306 | write_fd.reset(); |
| 307 | |
| 308 | std::string content; |
| 309 | if (!android::base::ReadFdToString(read_fd, &content)) { |
| 310 | perror("ReadFdToString"); |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | for (auto& line : android::base::Split(content, "\n")) { |
Yabin Cui | 5e235c8 | 2017-11-16 16:20:28 -0800 | [diff] [blame^] | 315 | line = android::base::Split(line, "#")[0]; |
Elliott Hughes | 10ba4bd | 2017-11-14 13:11:41 -0800 | [diff] [blame] | 316 | line = android::base::Trim(line); |
| 317 | if (line.empty()) continue; |
| 318 | if (android::base::EndsWith(line, ".")) { |
| 319 | line.pop_back(); |
| 320 | testcase_list.push_back(TestCase(line.c_str())); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 321 | } else { |
Elliott Hughes | 10ba4bd | 2017-11-14 13:11:41 -0800 | [diff] [blame] | 322 | testcase_list.back().AppendTest(line.c_str()); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
Elliott Hughes | 10ba4bd | 2017-11-14 13:11:41 -0800 | [diff] [blame] | 325 | |
| 326 | int status; |
| 327 | if (waitpid(pid, &status, 0) != pid) { |
| 328 | perror("waitpid"); |
| 329 | return false; |
| 330 | } |
| 331 | return (WIFEXITED(status) && WEXITSTATUS(status) == 0); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 334 | // Part of the following *Print functions are copied from external/gtest/src/gtest.cc: |
| 335 | // PrettyUnitTestResultPrinter. The reason for copy is that PrettyUnitTestResultPrinter |
| 336 | // is defined and used in gtest.cc, which is hard to reuse. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 337 | static void OnTestIterationStartPrint(const std::vector<TestCase>& testcase_list, size_t iteration, |
Elliott Hughes | 48de71e | 2016-10-28 10:04:44 -0700 | [diff] [blame] | 338 | int iteration_count, size_t job_count) { |
Christopher Ferris | 119cb55 | 2015-04-02 12:02:55 -0700 | [diff] [blame] | 339 | if (iteration_count != 1) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 340 | printf("\nRepeating all tests (iteration %zu) . . .\n\n", iteration); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 341 | } |
| 342 | ColoredPrintf(COLOR_GREEN, "[==========] "); |
| 343 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 344 | size_t testcase_count = testcase_list.size(); |
| 345 | size_t test_count = 0; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 346 | for (const auto& testcase : testcase_list) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 347 | test_count += testcase.TestCount(); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Elliott Hughes | 48de71e | 2016-10-28 10:04:44 -0700 | [diff] [blame] | 350 | printf("Running %zu %s from %zu %s (%zu %s).\n", |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 351 | test_count, (test_count == 1) ? "test" : "tests", |
Elliott Hughes | 48de71e | 2016-10-28 10:04:44 -0700 | [diff] [blame] | 352 | testcase_count, (testcase_count == 1) ? "test case" : "test cases", |
| 353 | job_count, (job_count == 1) ? "job" : "jobs"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 354 | fflush(stdout); |
| 355 | } |
| 356 | |
Yabin Cui | f623747 | 2015-02-26 19:03:54 -0800 | [diff] [blame] | 357 | // bionic cts test needs gtest output format. |
| 358 | #if defined(USING_GTEST_OUTPUT_FORMAT) |
| 359 | |
| 360 | static void OnTestEndPrint(const TestCase& testcase, size_t test_id) { |
| 361 | ColoredPrintf(COLOR_GREEN, "[ RUN ] "); |
| 362 | printf("%s\n", testcase.GetTestName(test_id).c_str()); |
| 363 | |
| 364 | const std::string& test_output = testcase.GetTest(test_id).GetTestOutput(); |
| 365 | printf("%s", test_output.c_str()); |
| 366 | |
| 367 | TestResult result = testcase.GetTestResult(test_id); |
Elliott Hughes | 93a89f8 | 2017-07-21 18:51:06 -0700 | [diff] [blame] | 368 | if (result == TEST_SUCCESS) { |
Yabin Cui | f623747 | 2015-02-26 19:03:54 -0800 | [diff] [blame] | 369 | ColoredPrintf(COLOR_GREEN, "[ OK ] "); |
| 370 | } else { |
| 371 | ColoredPrintf(COLOR_RED, "[ FAILED ] "); |
| 372 | } |
| 373 | printf("%s", testcase.GetTestName(test_id).c_str()); |
| 374 | if (testing::GTEST_FLAG(print_time)) { |
| 375 | printf(" (%" PRId64 " ms)", testcase.GetTestTime(test_id) / 1000000); |
| 376 | } |
| 377 | printf("\n"); |
| 378 | fflush(stdout); |
| 379 | } |
| 380 | |
| 381 | #else // !defined(USING_GTEST_OUTPUT_FORMAT) |
| 382 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 383 | static void OnTestEndPrint(const TestCase& testcase, size_t test_id) { |
| 384 | TestResult result = testcase.GetTestResult(test_id); |
| 385 | if (result == TEST_SUCCESS) { |
Elliott Hughes | 93a89f8 | 2017-07-21 18:51:06 -0700 | [diff] [blame] | 386 | ColoredPrintf(COLOR_GREEN, "[ OK ] "); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 387 | } else if (result == TEST_FAILED) { |
Elliott Hughes | 93a89f8 | 2017-07-21 18:51:06 -0700 | [diff] [blame] | 388 | ColoredPrintf(COLOR_RED, "[ FAILED ] "); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 389 | } else if (result == TEST_TIMEOUT) { |
| 390 | ColoredPrintf(COLOR_RED, "[ TIMEOUT ] "); |
| 391 | } |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 392 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 393 | printf("%s", testcase.GetTestName(test_id).c_str()); |
| 394 | if (testing::GTEST_FLAG(print_time)) { |
Yabin Cui | f623747 | 2015-02-26 19:03:54 -0800 | [diff] [blame] | 395 | printf(" (%" PRId64 " ms)", testcase.GetTestTime(test_id) / 1000000); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 396 | } |
Yabin Cui | f623747 | 2015-02-26 19:03:54 -0800 | [diff] [blame] | 397 | printf("\n"); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 398 | |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 399 | const std::string& test_output = testcase.GetTest(test_id).GetTestOutput(); |
| 400 | printf("%s", test_output.c_str()); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 401 | fflush(stdout); |
| 402 | } |
| 403 | |
Yabin Cui | f623747 | 2015-02-26 19:03:54 -0800 | [diff] [blame] | 404 | #endif // !defined(USING_GTEST_OUTPUT_FORMAT) |
| 405 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 406 | static void OnTestIterationEndPrint(const std::vector<TestCase>& testcase_list, size_t /*iteration*/, |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 407 | int64_t elapsed_time_ns) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 408 | |
| 409 | std::vector<std::string> fail_test_name_list; |
| 410 | std::vector<std::pair<std::string, int64_t>> timeout_test_list; |
| 411 | |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 412 | // For tests that were slow but didn't time out. |
Yabin Cui | 4a82ede | 2015-01-26 17:19:37 -0800 | [diff] [blame] | 413 | std::vector<std::tuple<std::string, int64_t, int>> slow_test_list; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 414 | size_t testcase_count = testcase_list.size(); |
| 415 | size_t test_count = 0; |
| 416 | size_t success_test_count = 0; |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 417 | size_t expected_failure_count = 0; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 418 | |
| 419 | for (const auto& testcase : testcase_list) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 420 | test_count += testcase.TestCount(); |
| 421 | for (size_t i = 0; i < testcase.TestCount(); ++i) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 422 | TestResult result = testcase.GetTestResult(i); |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 423 | if (result == TEST_TIMEOUT) { |
| 424 | timeout_test_list.push_back( |
| 425 | std::make_pair(testcase.GetTestName(i), testcase.GetTestTime(i))); |
Elliott Hughes | 93a89f8 | 2017-07-21 18:51:06 -0700 | [diff] [blame] | 426 | } else if (result == TEST_SUCCESS) { |
| 427 | ++success_test_count; |
| 428 | if (testcase.GetTestName(i).find(".xfail_") != std::string::npos) ++expected_failure_count; |
| 429 | } else if (result == TEST_FAILED) { |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 430 | fail_test_name_list.push_back(testcase.GetTestName(i)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 431 | } |
| 432 | if (result != TEST_TIMEOUT && |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 433 | testcase.GetTestTime(i) / 1000000 >= GetSlowThresholdMs(testcase.GetTestName(i))) { |
Yabin Cui | 4a82ede | 2015-01-26 17:19:37 -0800 | [diff] [blame] | 434 | slow_test_list.push_back(std::make_tuple(testcase.GetTestName(i), |
| 435 | testcase.GetTestTime(i), |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 436 | GetSlowThresholdMs(testcase.GetTestName(i)))); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 441 | ColoredPrintf(COLOR_GREEN, "[==========] "); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 442 | printf("%zu %s from %zu %s ran.", test_count, (test_count == 1) ? "test" : "tests", |
| 443 | testcase_count, (testcase_count == 1) ? "test case" : "test cases"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 444 | if (testing::GTEST_FLAG(print_time)) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 445 | printf(" (%" PRId64 " ms total)", elapsed_time_ns / 1000000); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 446 | } |
| 447 | printf("\n"); |
Yabin Cui | 4a82ede | 2015-01-26 17:19:37 -0800 | [diff] [blame] | 448 | ColoredPrintf(COLOR_GREEN, "[ PASS ] "); |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 449 | printf("%zu %s.", success_test_count, (success_test_count == 1) ? "test" : "tests"); |
| 450 | if (expected_failure_count > 0) { |
Elliott Hughes | 93a89f8 | 2017-07-21 18:51:06 -0700 | [diff] [blame] | 451 | printf(" (%zu expected failure%s.)", expected_failure_count, |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 452 | (expected_failure_count == 1) ? "" : "s"); |
| 453 | } |
| 454 | printf("\n"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 455 | |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 456 | // Print tests that timed out. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 457 | size_t timeout_test_count = timeout_test_list.size(); |
| 458 | if (timeout_test_count > 0) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 459 | ColoredPrintf(COLOR_RED, "[ TIMEOUT ] "); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 460 | printf("%zu %s, listed below:\n", timeout_test_count, (timeout_test_count == 1) ? "test" : "tests"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 461 | for (const auto& timeout_pair : timeout_test_list) { |
| 462 | ColoredPrintf(COLOR_RED, "[ TIMEOUT ] "); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 463 | printf("%s (stopped at %" PRId64 " ms)\n", timeout_pair.first.c_str(), |
| 464 | timeout_pair.second / 1000000); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 468 | // Print tests that were slow. |
Yabin Cui | 4a82ede | 2015-01-26 17:19:37 -0800 | [diff] [blame] | 469 | size_t slow_test_count = slow_test_list.size(); |
| 470 | if (slow_test_count > 0) { |
| 471 | ColoredPrintf(COLOR_YELLOW, "[ SLOW ] "); |
| 472 | printf("%zu %s, listed below:\n", slow_test_count, (slow_test_count == 1) ? "test" : "tests"); |
| 473 | for (const auto& slow_tuple : slow_test_list) { |
| 474 | ColoredPrintf(COLOR_YELLOW, "[ SLOW ] "); |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 475 | printf("%s (%" PRId64 " ms, exceeded %d ms)\n", std::get<0>(slow_tuple).c_str(), |
Yabin Cui | 4a82ede | 2015-01-26 17:19:37 -0800 | [diff] [blame] | 476 | std::get<1>(slow_tuple) / 1000000, std::get<2>(slow_tuple)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 480 | // Print tests that failed. |
| 481 | size_t fail_test_count = fail_test_name_list.size(); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 482 | if (fail_test_count > 0) { |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 483 | ColoredPrintf(COLOR_RED, "[ FAIL ] "); |
| 484 | printf("%zu %s, listed below:\n", fail_test_count, (fail_test_count == 1) ? "test" : "tests"); |
| 485 | for (const auto& name : fail_test_name_list) { |
| 486 | ColoredPrintf(COLOR_RED, "[ FAIL ] "); |
| 487 | printf("%s\n", name.c_str()); |
| 488 | } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 489 | } |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 490 | |
Elliott Hughes | 93a89f8 | 2017-07-21 18:51:06 -0700 | [diff] [blame] | 491 | if (timeout_test_count > 0 || slow_test_count > 0 || fail_test_count > 0) { |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 492 | printf("\n"); |
| 493 | } |
| 494 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 495 | if (timeout_test_count > 0) { |
| 496 | printf("%2zu TIMEOUT %s\n", timeout_test_count, (timeout_test_count == 1) ? "TEST" : "TESTS"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 497 | } |
Yabin Cui | 4a82ede | 2015-01-26 17:19:37 -0800 | [diff] [blame] | 498 | if (slow_test_count > 0) { |
| 499 | printf("%2zu SLOW %s\n", slow_test_count, (slow_test_count == 1) ? "TEST" : "TESTS"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 500 | } |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 501 | if (fail_test_count > 0) { |
| 502 | printf("%2zu FAILED %s\n", fail_test_count, (fail_test_count == 1) ? "TEST" : "TESTS"); |
| 503 | } |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 504 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 505 | fflush(stdout); |
| 506 | } |
| 507 | |
Dan Albert | 09a9964 | 2016-01-13 21:48:56 -0800 | [diff] [blame] | 508 | std::string XmlEscape(const std::string& xml) { |
| 509 | std::string escaped; |
| 510 | escaped.reserve(xml.size()); |
| 511 | |
| 512 | for (auto c : xml) { |
| 513 | switch (c) { |
| 514 | case '<': |
| 515 | escaped.append("<"); |
| 516 | break; |
| 517 | case '>': |
| 518 | escaped.append(">"); |
| 519 | break; |
| 520 | case '&': |
| 521 | escaped.append("&"); |
| 522 | break; |
| 523 | case '\'': |
| 524 | escaped.append("'"); |
| 525 | break; |
| 526 | case '"': |
| 527 | escaped.append("""); |
| 528 | break; |
| 529 | default: |
| 530 | escaped.append(1, c); |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return escaped; |
| 536 | } |
| 537 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 538 | // Output xml file when --gtest_output is used, write this function as we can't reuse |
| 539 | // gtest.cc:XmlUnitTestResultPrinter. The reason is XmlUnitTestResultPrinter is totally |
| 540 | // defined in gtest.cc and not expose to outside. What's more, as we don't run gtest in |
| 541 | // the parent process, we don't have gtest classes which are needed by XmlUnitTestResultPrinter. |
| 542 | void OnTestIterationEndXmlPrint(const std::string& xml_output_filename, |
| 543 | const std::vector<TestCase>& testcase_list, |
| 544 | time_t epoch_iteration_start_time, |
| 545 | int64_t elapsed_time_ns) { |
| 546 | FILE* fp = fopen(xml_output_filename.c_str(), "w"); |
| 547 | if (fp == NULL) { |
| 548 | fprintf(stderr, "failed to open '%s': %s\n", xml_output_filename.c_str(), strerror(errno)); |
| 549 | exit(1); |
| 550 | } |
| 551 | |
| 552 | size_t total_test_count = 0; |
| 553 | size_t total_failed_count = 0; |
| 554 | std::vector<size_t> failed_count_list(testcase_list.size(), 0); |
| 555 | std::vector<int64_t> elapsed_time_list(testcase_list.size(), 0); |
| 556 | for (size_t i = 0; i < testcase_list.size(); ++i) { |
| 557 | auto& testcase = testcase_list[i]; |
| 558 | total_test_count += testcase.TestCount(); |
| 559 | for (size_t j = 0; j < testcase.TestCount(); ++j) { |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 560 | if (!testcase.GetTestSuccess(j)) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 561 | ++failed_count_list[i]; |
| 562 | } |
| 563 | elapsed_time_list[i] += testcase.GetTestTime(j); |
| 564 | } |
| 565 | total_failed_count += failed_count_list[i]; |
| 566 | } |
| 567 | |
| 568 | const tm* time_struct = localtime(&epoch_iteration_start_time); |
| 569 | char timestamp[40]; |
| 570 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", |
| 571 | time_struct->tm_year + 1900, time_struct->tm_mon + 1, time_struct->tm_mday, |
| 572 | time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec); |
| 573 | |
| 574 | fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp); |
| 575 | fprintf(fp, "<testsuites tests=\"%zu\" failures=\"%zu\" disabled=\"0\" errors=\"0\"", |
| 576 | total_test_count, total_failed_count); |
| 577 | fprintf(fp, " timestamp=\"%s\" time=\"%.3lf\" name=\"AllTests\">\n", timestamp, elapsed_time_ns / 1e9); |
| 578 | for (size_t i = 0; i < testcase_list.size(); ++i) { |
| 579 | auto& testcase = testcase_list[i]; |
| 580 | fprintf(fp, " <testsuite name=\"%s\" tests=\"%zu\" failures=\"%zu\" disabled=\"0\" errors=\"0\"", |
| 581 | testcase.GetName().c_str(), testcase.TestCount(), failed_count_list[i]); |
| 582 | fprintf(fp, " time=\"%.3lf\">\n", elapsed_time_list[i] / 1e9); |
| 583 | |
| 584 | for (size_t j = 0; j < testcase.TestCount(); ++j) { |
| 585 | fprintf(fp, " <testcase name=\"%s\" status=\"run\" time=\"%.3lf\" classname=\"%s\"", |
| 586 | testcase.GetTest(j).GetName().c_str(), testcase.GetTestTime(j) / 1e9, |
| 587 | testcase.GetName().c_str()); |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 588 | if (!testcase.GetTestSuccess(j)) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 589 | fputs(" />\n", fp); |
| 590 | } else { |
| 591 | fputs(">\n", fp); |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 592 | const std::string& test_output = testcase.GetTest(j).GetTestOutput(); |
Dan Albert | 09a9964 | 2016-01-13 21:48:56 -0800 | [diff] [blame] | 593 | const std::string escaped_test_output = XmlEscape(test_output); |
| 594 | fprintf(fp, " <failure message=\"%s\" type=\"\">\n", escaped_test_output.c_str()); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 595 | fputs(" </failure>\n", fp); |
| 596 | fputs(" </testcase>\n", fp); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | fputs(" </testsuite>\n", fp); |
| 601 | } |
| 602 | fputs("</testsuites>\n", fp); |
| 603 | fclose(fp); |
| 604 | } |
| 605 | |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 606 | static bool sigint_flag; |
| 607 | static bool sigquit_flag; |
| 608 | |
| 609 | static void signal_handler(int sig) { |
| 610 | if (sig == SIGINT) { |
| 611 | sigint_flag = true; |
| 612 | } else if (sig == SIGQUIT) { |
| 613 | sigquit_flag = true; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | static bool RegisterSignalHandler() { |
| 618 | sigint_flag = false; |
| 619 | sigquit_flag = false; |
| 620 | sig_t ret = signal(SIGINT, signal_handler); |
| 621 | if (ret != SIG_ERR) { |
| 622 | ret = signal(SIGQUIT, signal_handler); |
| 623 | } |
| 624 | if (ret == SIG_ERR) { |
| 625 | perror("RegisterSignalHandler"); |
| 626 | return false; |
| 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | static bool UnregisterSignalHandler() { |
| 632 | sig_t ret = signal(SIGINT, SIG_DFL); |
| 633 | if (ret != SIG_ERR) { |
| 634 | ret = signal(SIGQUIT, SIG_DFL); |
| 635 | } |
| 636 | if (ret == SIG_ERR) { |
| 637 | perror("UnregisterSignalHandler"); |
| 638 | return false; |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 643 | struct ChildProcInfo { |
| 644 | pid_t pid; |
| 645 | int64_t start_time_ns; |
| 646 | int64_t end_time_ns; |
| 647 | int64_t deadline_end_time_ns; // The time when the test is thought of as timeout. |
| 648 | size_t testcase_id, test_id; |
| 649 | bool finished; |
| 650 | bool timed_out; |
| 651 | int exit_status; |
| 652 | int child_read_fd; // File descriptor to read child test failure info. |
| 653 | }; |
| 654 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 655 | // Forked Child process, run the single test. |
| 656 | static void ChildProcessFn(int argc, char** argv, const std::string& test_name) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 657 | char** new_argv = new char*[argc + 2]; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 658 | memcpy(new_argv, argv, sizeof(char*) * argc); |
| 659 | |
| 660 | char* filter_arg = new char [test_name.size() + 20]; |
| 661 | strcpy(filter_arg, "--gtest_filter="); |
| 662 | strcat(filter_arg, test_name.c_str()); |
| 663 | new_argv[argc] = filter_arg; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 664 | new_argv[argc + 1] = NULL; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 665 | |
| 666 | int new_argc = argc + 1; |
| 667 | testing::InitGoogleTest(&new_argc, new_argv); |
| 668 | int result = RUN_ALL_TESTS(); |
| 669 | exit(result); |
| 670 | } |
| 671 | |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 672 | static ChildProcInfo RunChildProcess(const std::string& test_name, int testcase_id, int test_id, |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 673 | int argc, char** argv) { |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 674 | int pipefd[2]; |
Yabin Cui | d4c9b9d | 2015-11-16 20:39:58 -0800 | [diff] [blame] | 675 | if (pipe(pipefd) == -1) { |
| 676 | perror("pipe in RunTestInSeparateProc"); |
| 677 | exit(1); |
| 678 | } |
| 679 | if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1) { |
| 680 | perror("fcntl in RunTestInSeparateProc"); |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 681 | exit(1); |
| 682 | } |
| 683 | pid_t pid = fork(); |
| 684 | if (pid == -1) { |
| 685 | perror("fork in RunTestInSeparateProc"); |
| 686 | exit(1); |
| 687 | } else if (pid == 0) { |
| 688 | // In child process, run a single test. |
| 689 | close(pipefd[0]); |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 690 | close(STDOUT_FILENO); |
| 691 | close(STDERR_FILENO); |
| 692 | dup2(pipefd[1], STDOUT_FILENO); |
| 693 | dup2(pipefd[1], STDERR_FILENO); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 694 | |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 695 | if (!UnregisterSignalHandler()) { |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 696 | exit(1); |
| 697 | } |
| 698 | ChildProcessFn(argc, argv, test_name); |
| 699 | // Unreachable. |
| 700 | } |
| 701 | // In parent process, initialize child process info. |
| 702 | close(pipefd[1]); |
| 703 | ChildProcInfo child_proc; |
| 704 | child_proc.child_read_fd = pipefd[0]; |
| 705 | child_proc.pid = pid; |
| 706 | child_proc.start_time_ns = NanoTime(); |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 707 | child_proc.deadline_end_time_ns = child_proc.start_time_ns + GetTimeoutMs(test_name) * 1000000LL; |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 708 | child_proc.testcase_id = testcase_id; |
| 709 | child_proc.test_id = test_id; |
| 710 | child_proc.finished = false; |
| 711 | return child_proc; |
| 712 | } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 713 | |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 714 | static void HandleSignals(std::vector<TestCase>& testcase_list, |
| 715 | std::vector<ChildProcInfo>& child_proc_list) { |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 716 | if (sigquit_flag) { |
| 717 | sigquit_flag = false; |
| 718 | // Print current running tests. |
| 719 | printf("List of current running tests:\n"); |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 720 | for (const auto& child_proc : child_proc_list) { |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 721 | if (child_proc.pid != 0) { |
| 722 | std::string test_name = testcase_list[child_proc.testcase_id].GetTestName(child_proc.test_id); |
| 723 | int64_t current_time_ns = NanoTime(); |
| 724 | int64_t run_time_ms = (current_time_ns - child_proc.start_time_ns) / 1000000; |
| 725 | printf(" %s (%" PRId64 " ms)\n", test_name.c_str(), run_time_ms); |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 726 | } |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 727 | } |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 728 | } else if (sigint_flag) { |
| 729 | sigint_flag = false; |
| 730 | // Kill current running tests. |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 731 | for (const auto& child_proc : child_proc_list) { |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 732 | if (child_proc.pid != 0) { |
| 733 | // Send SIGKILL to ensure the child process can be killed unconditionally. |
| 734 | kill(child_proc.pid, SIGKILL); |
| 735 | } |
| 736 | } |
| 737 | // SIGINT kills the parent process as well. |
| 738 | exit(1); |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | static bool CheckChildProcExit(pid_t exit_pid, int exit_status, |
| 743 | std::vector<ChildProcInfo>& child_proc_list) { |
| 744 | for (size_t i = 0; i < child_proc_list.size(); ++i) { |
| 745 | if (child_proc_list[i].pid == exit_pid) { |
| 746 | child_proc_list[i].finished = true; |
| 747 | child_proc_list[i].timed_out = false; |
| 748 | child_proc_list[i].exit_status = exit_status; |
| 749 | child_proc_list[i].end_time_ns = NanoTime(); |
| 750 | return true; |
| 751 | } |
| 752 | } |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | static size_t CheckChildProcTimeout(std::vector<ChildProcInfo>& child_proc_list) { |
| 757 | int64_t current_time_ns = NanoTime(); |
| 758 | size_t timeout_child_count = 0; |
| 759 | for (size_t i = 0; i < child_proc_list.size(); ++i) { |
| 760 | if (child_proc_list[i].deadline_end_time_ns <= current_time_ns) { |
| 761 | child_proc_list[i].finished = true; |
| 762 | child_proc_list[i].timed_out = true; |
| 763 | child_proc_list[i].end_time_ns = current_time_ns; |
| 764 | ++timeout_child_count; |
| 765 | } |
| 766 | } |
| 767 | return timeout_child_count; |
| 768 | } |
| 769 | |
Yabin Cui | d4c9b9d | 2015-11-16 20:39:58 -0800 | [diff] [blame] | 770 | static void ReadChildProcOutput(std::vector<TestCase>& testcase_list, |
| 771 | std::vector<ChildProcInfo>& child_proc_list) { |
| 772 | for (const auto& child_proc : child_proc_list) { |
| 773 | TestCase& testcase = testcase_list[child_proc.testcase_id]; |
| 774 | int test_id = child_proc.test_id; |
| 775 | while (true) { |
| 776 | char buf[1024]; |
| 777 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(child_proc.child_read_fd, buf, sizeof(buf) - 1)); |
| 778 | if (bytes_read > 0) { |
| 779 | buf[bytes_read] = '\0'; |
| 780 | testcase.GetTest(test_id).AppendTestOutput(buf); |
| 781 | } else if (bytes_read == 0) { |
| 782 | break; // Read end. |
| 783 | } else { |
| 784 | if (errno == EAGAIN) { |
| 785 | break; |
| 786 | } |
| 787 | perror("failed to read child_read_fd"); |
| 788 | exit(1); |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 794 | static void WaitChildProcs(std::vector<TestCase>& testcase_list, |
| 795 | std::vector<ChildProcInfo>& child_proc_list) { |
| 796 | size_t finished_child_count = 0; |
| 797 | while (true) { |
| 798 | int status; |
| 799 | pid_t result; |
| 800 | while ((result = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG))) > 0) { |
| 801 | if (CheckChildProcExit(result, status, child_proc_list)) { |
| 802 | ++finished_child_count; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
| 806 | if (result == -1) { |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 807 | if (errno == ECHILD) { |
| 808 | // This happens when we have no running child processes. |
| 809 | return; |
| 810 | } else { |
| 811 | perror("waitpid"); |
| 812 | exit(1); |
| 813 | } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 814 | } else if (result == 0) { |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 815 | finished_child_count += CheckChildProcTimeout(child_proc_list); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Yabin Cui | d4c9b9d | 2015-11-16 20:39:58 -0800 | [diff] [blame] | 818 | ReadChildProcOutput(testcase_list, child_proc_list); |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 819 | if (finished_child_count > 0) { |
| 820 | return; |
| 821 | } |
| 822 | |
| 823 | HandleSignals(testcase_list, child_proc_list); |
| 824 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 825 | // sleep 1 ms to avoid busy looping. |
| 826 | timespec sleep_time; |
| 827 | sleep_time.tv_sec = 0; |
| 828 | sleep_time.tv_nsec = 1000000; |
| 829 | nanosleep(&sleep_time, NULL); |
| 830 | } |
| 831 | } |
| 832 | |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 833 | static TestResult WaitForOneChild(pid_t pid) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 834 | int exit_status; |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 835 | pid_t result = TEMP_FAILURE_RETRY(waitpid(pid, &exit_status, 0)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 836 | |
| 837 | TestResult test_result = TEST_SUCCESS; |
| 838 | if (result != pid || WEXITSTATUS(exit_status) != 0) { |
| 839 | test_result = TEST_FAILED; |
| 840 | } |
| 841 | return test_result; |
| 842 | } |
| 843 | |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 844 | static void CollectChildTestResult(const ChildProcInfo& child_proc, TestCase& testcase) { |
| 845 | int test_id = child_proc.test_id; |
| 846 | testcase.SetTestTime(test_id, child_proc.end_time_ns - child_proc.start_time_ns); |
| 847 | if (child_proc.timed_out) { |
| 848 | // The child process marked as timed_out has not exited, and we should kill it manually. |
| 849 | kill(child_proc.pid, SIGKILL); |
| 850 | WaitForOneChild(child_proc.pid); |
| 851 | } |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 852 | close(child_proc.child_read_fd); |
| 853 | |
| 854 | if (child_proc.timed_out) { |
| 855 | testcase.SetTestResult(test_id, TEST_TIMEOUT); |
| 856 | char buf[1024]; |
| 857 | snprintf(buf, sizeof(buf), "%s killed because of timeout at %" PRId64 " ms.\n", |
| 858 | testcase.GetTestName(test_id).c_str(), testcase.GetTestTime(test_id) / 1000000); |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 859 | testcase.GetTest(test_id).AppendTestOutput(buf); |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 860 | |
| 861 | } else if (WIFSIGNALED(child_proc.exit_status)) { |
| 862 | // Record signal terminated test as failed. |
| 863 | testcase.SetTestResult(test_id, TEST_FAILED); |
| 864 | char buf[1024]; |
| 865 | snprintf(buf, sizeof(buf), "%s terminated by signal: %s.\n", |
| 866 | testcase.GetTestName(test_id).c_str(), strsignal(WTERMSIG(child_proc.exit_status))); |
Yabin Cui | ea9c933 | 2015-02-24 14:39:19 -0800 | [diff] [blame] | 867 | testcase.GetTest(test_id).AppendTestOutput(buf); |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 868 | |
| 869 | } else { |
Yabin Cui | d4c9b9d | 2015-11-16 20:39:58 -0800 | [diff] [blame] | 870 | int exitcode = WEXITSTATUS(child_proc.exit_status); |
| 871 | testcase.SetTestResult(test_id, exitcode == 0 ? TEST_SUCCESS : TEST_FAILED); |
| 872 | if (exitcode != 0) { |
Yabin Cui | a32fc86 | 2015-12-03 16:28:03 -0800 | [diff] [blame] | 873 | char buf[1024]; |
| 874 | snprintf(buf, sizeof(buf), "%s exited with exitcode %d.\n", |
| 875 | testcase.GetTestName(test_id).c_str(), exitcode); |
| 876 | testcase.GetTest(test_id).AppendTestOutput(buf); |
Yabin Cui | d4c9b9d | 2015-11-16 20:39:58 -0800 | [diff] [blame] | 877 | } |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 881 | // We choose to use multi-fork and multi-wait here instead of multi-thread, because it always |
| 882 | // makes deadlock to use fork in multi-thread. |
Yabin Cui | 64a9c4f | 2015-03-12 22:16:03 -0700 | [diff] [blame] | 883 | // Returns true if all tests run successfully, otherwise return false. |
| 884 | static bool RunTestInSeparateProc(int argc, char** argv, std::vector<TestCase>& testcase_list, |
Christopher Ferris | 119cb55 | 2015-04-02 12:02:55 -0700 | [diff] [blame] | 885 | int iteration_count, size_t job_count, |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 886 | const std::string& xml_output_filename) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 887 | // Stop default result printer to avoid environment setup/teardown information for each test. |
| 888 | testing::UnitTest::GetInstance()->listeners().Release( |
| 889 | testing::UnitTest::GetInstance()->listeners().default_result_printer()); |
| 890 | testing::UnitTest::GetInstance()->listeners().Append(new TestResultPrinter); |
| 891 | |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 892 | if (!RegisterSignalHandler()) { |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 893 | exit(1); |
| 894 | } |
| 895 | |
Yabin Cui | 64a9c4f | 2015-03-12 22:16:03 -0700 | [diff] [blame] | 896 | bool all_tests_passed = true; |
| 897 | |
Christopher Ferris | 119cb55 | 2015-04-02 12:02:55 -0700 | [diff] [blame] | 898 | for (size_t iteration = 1; |
| 899 | iteration_count < 0 || iteration <= static_cast<size_t>(iteration_count); |
| 900 | ++iteration) { |
Elliott Hughes | 48de71e | 2016-10-28 10:04:44 -0700 | [diff] [blame] | 901 | OnTestIterationStartPrint(testcase_list, iteration, iteration_count, job_count); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 902 | int64_t iteration_start_time_ns = NanoTime(); |
| 903 | time_t epoch_iteration_start_time = time(NULL); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 904 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 905 | // Run up to job_count tests in parallel, each test in a child process. |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 906 | std::vector<ChildProcInfo> child_proc_list; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 907 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 908 | // Next test to run is [next_testcase_id:next_test_id]. |
| 909 | size_t next_testcase_id = 0; |
| 910 | size_t next_test_id = 0; |
| 911 | |
| 912 | // Record how many tests are finished. |
| 913 | std::vector<size_t> finished_test_count_list(testcase_list.size(), 0); |
| 914 | size_t finished_testcase_count = 0; |
| 915 | |
| 916 | while (finished_testcase_count < testcase_list.size()) { |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 917 | // run up to job_count child processes. |
| 918 | while (child_proc_list.size() < job_count && next_testcase_id < testcase_list.size()) { |
| 919 | std::string test_name = testcase_list[next_testcase_id].GetTestName(next_test_id); |
| 920 | ChildProcInfo child_proc = RunChildProcess(test_name, next_testcase_id, next_test_id, |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 921 | argc, argv); |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 922 | child_proc_list.push_back(child_proc); |
| 923 | if (++next_test_id == testcase_list[next_testcase_id].TestCount()) { |
| 924 | next_test_id = 0; |
| 925 | ++next_testcase_id; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
| 929 | // Wait for any child proc finish or timeout. |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 930 | WaitChildProcs(testcase_list, child_proc_list); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 931 | |
| 932 | // Collect result. |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 933 | auto it = child_proc_list.begin(); |
| 934 | while (it != child_proc_list.end()) { |
| 935 | auto& child_proc = *it; |
| 936 | if (child_proc.finished == true) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 937 | size_t testcase_id = child_proc.testcase_id; |
| 938 | size_t test_id = child_proc.test_id; |
| 939 | TestCase& testcase = testcase_list[testcase_id]; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 940 | |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 941 | CollectChildTestResult(child_proc, testcase); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 942 | OnTestEndPrint(testcase, test_id); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 943 | |
| 944 | if (++finished_test_count_list[testcase_id] == testcase.TestCount()) { |
| 945 | ++finished_testcase_count; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 946 | } |
Josh Gao | 0105222 | 2017-01-09 16:43:33 -0800 | [diff] [blame] | 947 | if (!testcase.GetTestSuccess(test_id)) { |
Yabin Cui | 64a9c4f | 2015-03-12 22:16:03 -0700 | [diff] [blame] | 948 | all_tests_passed = false; |
| 949 | } |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 950 | |
| 951 | it = child_proc_list.erase(it); |
| 952 | } else { |
| 953 | ++it; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 958 | int64_t elapsed_time_ns = NanoTime() - iteration_start_time_ns; |
| 959 | OnTestIterationEndPrint(testcase_list, iteration, elapsed_time_ns); |
| 960 | if (!xml_output_filename.empty()) { |
| 961 | OnTestIterationEndXmlPrint(xml_output_filename, testcase_list, epoch_iteration_start_time, |
| 962 | elapsed_time_ns); |
| 963 | } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 964 | } |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 965 | |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 966 | if (!UnregisterSignalHandler()) { |
Yabin Cui | 1d4c780 | 2015-02-02 19:14:05 -0800 | [diff] [blame] | 967 | exit(1); |
| 968 | } |
Yabin Cui | 64a9c4f | 2015-03-12 22:16:03 -0700 | [diff] [blame] | 969 | |
| 970 | return all_tests_passed; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Christopher Ferris | daaaed1 | 2015-09-24 18:45:53 -0700 | [diff] [blame] | 973 | static size_t GetDefaultJobCount() { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 974 | return static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 975 | } |
| 976 | |
Yabin Cui | ead0814 | 2015-02-04 20:53:56 -0800 | [diff] [blame] | 977 | static void AddPathSeparatorInTestProgramPath(std::vector<char*>& args) { |
| 978 | // To run DeathTest in threadsafe mode, gtest requires that the user must invoke the |
| 979 | // test program via a valid path that contains at least one path separator. |
| 980 | // The reason is that gtest uses clone() + execve() to run DeathTest in threadsafe mode, |
| 981 | // and execve() doesn't read environment variable PATH, so execve() will not success |
| 982 | // until we specify the absolute path or relative path of the test program directly. |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 983 | if (strchr(args[0], '/') == nullptr) { |
| 984 | args[0] = strdup(g_executable_path.c_str()); |
Yabin Cui | ead0814 | 2015-02-04 20:53:56 -0800 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
Yabin Cui | 11c4353 | 2015-01-28 14:28:14 -0800 | [diff] [blame] | 988 | static void AddGtestFilterSynonym(std::vector<char*>& args) { |
| 989 | // Support --gtest-filter as a synonym for --gtest_filter. |
| 990 | for (size_t i = 1; i < args.size(); ++i) { |
| 991 | if (strncmp(args[i], "--gtest-filter", strlen("--gtest-filter")) == 0) { |
| 992 | args[i][7] = '_'; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 997 | struct IsolationTestOptions { |
| 998 | bool isolate; |
| 999 | size_t job_count; |
| 1000 | int test_deadline_ms; |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 1001 | int test_slow_threshold_ms; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1002 | std::string gtest_color; |
| 1003 | bool gtest_print_time; |
Christopher Ferris | 119cb55 | 2015-04-02 12:02:55 -0700 | [diff] [blame] | 1004 | int gtest_repeat; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1005 | std::string gtest_output; |
| 1006 | }; |
| 1007 | |
| 1008 | // Pick options not for gtest: There are two parts in args, one part is used in isolation test mode |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1009 | // as described in PrintHelpInfo(), the other part is handled by testing::InitGoogleTest() in |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1010 | // gtest. PickOptions() picks the first part into IsolationTestOptions structure, leaving the second |
| 1011 | // part in args. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1012 | // Arguments: |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1013 | // args is used to pass in all command arguments, and pass out only the part of options for gtest. |
| 1014 | // options is used to pass out test options in isolation mode. |
| 1015 | // Return false if there is error in arguments. |
| 1016 | static bool PickOptions(std::vector<char*>& args, IsolationTestOptions& options) { |
| 1017 | for (size_t i = 1; i < args.size(); ++i) { |
| 1018 | if (strcmp(args[i], "--help") == 0 || strcmp(args[i], "-h") == 0) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1019 | PrintHelpInfo(); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1020 | options.isolate = false; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1021 | return true; |
| 1022 | } |
| 1023 | } |
| 1024 | |
Yabin Cui | ead0814 | 2015-02-04 20:53:56 -0800 | [diff] [blame] | 1025 | AddPathSeparatorInTestProgramPath(args); |
Yabin Cui | 11c4353 | 2015-01-28 14:28:14 -0800 | [diff] [blame] | 1026 | AddGtestFilterSynonym(args); |
| 1027 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1028 | // if --bionic-selftest argument is used, only enable self tests, otherwise remove self tests. |
| 1029 | bool enable_selftest = false; |
| 1030 | for (size_t i = 1; i < args.size(); ++i) { |
| 1031 | if (strcmp(args[i], "--bionic-selftest") == 0) { |
| 1032 | // This argument is to enable "bionic_selftest*" for self test, and is not shown in help info. |
| 1033 | // Don't remove this option from arguments. |
| 1034 | enable_selftest = true; |
| 1035 | } |
| 1036 | } |
| 1037 | std::string gtest_filter_str; |
| 1038 | for (size_t i = args.size() - 1; i >= 1; --i) { |
| 1039 | if (strncmp(args[i], "--gtest_filter=", strlen("--gtest_filter=")) == 0) { |
Yabin Cui | c641a95 | 2016-12-12 13:32:15 -0800 | [diff] [blame] | 1040 | gtest_filter_str = args[i] + strlen("--gtest_filter="); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1041 | args.erase(args.begin() + i); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1042 | break; |
| 1043 | } |
| 1044 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1045 | if (enable_selftest == true) { |
Yabin Cui | c641a95 | 2016-12-12 13:32:15 -0800 | [diff] [blame] | 1046 | gtest_filter_str = "bionic_selftest*"; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1047 | } else { |
Yabin Cui | c641a95 | 2016-12-12 13:32:15 -0800 | [diff] [blame] | 1048 | if (gtest_filter_str.empty()) { |
| 1049 | gtest_filter_str = "-bionic_selftest*"; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1050 | } else { |
Yabin Cui | 0bc4e96 | 2015-01-27 11:22:46 -0800 | [diff] [blame] | 1051 | // Find if '-' for NEGATIVE_PATTERNS exists. |
wy | 828b9e1 | 2017-05-10 15:21:13 -0700 | [diff] [blame] | 1052 | if (gtest_filter_str.find('-') != std::string::npos) { |
Yabin Cui | 0bc4e96 | 2015-01-27 11:22:46 -0800 | [diff] [blame] | 1053 | gtest_filter_str += ":bionic_selftest*"; |
| 1054 | } else { |
| 1055 | gtest_filter_str += ":-bionic_selftest*"; |
| 1056 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1057 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1058 | } |
Yabin Cui | c641a95 | 2016-12-12 13:32:15 -0800 | [diff] [blame] | 1059 | gtest_filter_str = "--gtest_filter=" + gtest_filter_str; |
| 1060 | args.push_back(strdup(gtest_filter_str.c_str())); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1061 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1062 | options.isolate = true; |
| 1063 | // Parse arguments that make us can't run in isolation mode. |
| 1064 | for (size_t i = 1; i < args.size(); ++i) { |
| 1065 | if (strcmp(args[i], "--no-isolate") == 0) { |
| 1066 | options.isolate = false; |
| 1067 | } else if (strcmp(args[i], "--gtest_list_tests") == 0) { |
| 1068 | options.isolate = false; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1072 | // Stop parsing if we will not run in isolation mode. |
| 1073 | if (options.isolate == false) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1074 | return true; |
| 1075 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1076 | |
| 1077 | // Init default isolation test options. |
Christopher Ferris | daaaed1 | 2015-09-24 18:45:53 -0700 | [diff] [blame] | 1078 | options.job_count = GetDefaultJobCount(); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1079 | options.test_deadline_ms = DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS; |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 1080 | options.test_slow_threshold_ms = DEFAULT_GLOBAL_TEST_RUN_SLOW_THRESHOLD_MS; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1081 | options.gtest_color = testing::GTEST_FLAG(color); |
| 1082 | options.gtest_print_time = testing::GTEST_FLAG(print_time); |
| 1083 | options.gtest_repeat = testing::GTEST_FLAG(repeat); |
| 1084 | options.gtest_output = testing::GTEST_FLAG(output); |
| 1085 | |
| 1086 | // Parse arguments speficied for isolation mode. |
| 1087 | for (size_t i = 1; i < args.size(); ++i) { |
| 1088 | if (strncmp(args[i], "-j", strlen("-j")) == 0) { |
| 1089 | char* p = args[i] + strlen("-j"); |
| 1090 | int count = 0; |
| 1091 | if (*p != '\0') { |
| 1092 | // Argument like -j5. |
| 1093 | count = atoi(p); |
| 1094 | } else if (args.size() > i + 1) { |
| 1095 | // Arguments like -j 5. |
| 1096 | count = atoi(args[i + 1]); |
| 1097 | ++i; |
| 1098 | } |
| 1099 | if (count <= 0) { |
| 1100 | fprintf(stderr, "invalid job count: %d\n", count); |
| 1101 | return false; |
| 1102 | } |
| 1103 | options.job_count = static_cast<size_t>(count); |
| 1104 | } else if (strncmp(args[i], "--deadline=", strlen("--deadline=")) == 0) { |
| 1105 | int time_ms = atoi(args[i] + strlen("--deadline=")); |
| 1106 | if (time_ms <= 0) { |
| 1107 | fprintf(stderr, "invalid deadline: %d\n", time_ms); |
| 1108 | return false; |
| 1109 | } |
| 1110 | options.test_deadline_ms = time_ms; |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 1111 | } else if (strncmp(args[i], "--slow-threshold=", strlen("--slow-threshold=")) == 0) { |
| 1112 | int time_ms = atoi(args[i] + strlen("--slow-threshold=")); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1113 | if (time_ms <= 0) { |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 1114 | fprintf(stderr, "invalid slow test threshold: %d\n", time_ms); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1115 | return false; |
| 1116 | } |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 1117 | options.test_slow_threshold_ms = time_ms; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1118 | } else if (strncmp(args[i], "--gtest_color=", strlen("--gtest_color=")) == 0) { |
| 1119 | options.gtest_color = args[i] + strlen("--gtest_color="); |
| 1120 | } else if (strcmp(args[i], "--gtest_print_time=0") == 0) { |
| 1121 | options.gtest_print_time = false; |
| 1122 | } else if (strncmp(args[i], "--gtest_repeat=", strlen("--gtest_repeat=")) == 0) { |
Christopher Ferris | 119cb55 | 2015-04-02 12:02:55 -0700 | [diff] [blame] | 1123 | // If the value of gtest_repeat is < 0, then it indicates the tests |
| 1124 | // should be repeated forever. |
| 1125 | options.gtest_repeat = atoi(args[i] + strlen("--gtest_repeat=")); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1126 | // Remove --gtest_repeat=xx from arguments, so child process only run one iteration for a single test. |
| 1127 | args.erase(args.begin() + i); |
| 1128 | --i; |
| 1129 | } else if (strncmp(args[i], "--gtest_output=", strlen("--gtest_output=")) == 0) { |
| 1130 | std::string output = args[i] + strlen("--gtest_output="); |
| 1131 | // generate output xml file path according to the strategy in gtest. |
| 1132 | bool success = true; |
| 1133 | if (strncmp(output.c_str(), "xml:", strlen("xml:")) == 0) { |
| 1134 | output = output.substr(strlen("xml:")); |
| 1135 | if (output.size() == 0) { |
| 1136 | success = false; |
| 1137 | } |
| 1138 | // Make absolute path. |
| 1139 | if (success && output[0] != '/') { |
| 1140 | char* cwd = getcwd(NULL, 0); |
| 1141 | if (cwd != NULL) { |
| 1142 | output = std::string(cwd) + "/" + output; |
| 1143 | free(cwd); |
| 1144 | } else { |
| 1145 | success = false; |
| 1146 | } |
| 1147 | } |
| 1148 | // Add file name if output is a directory. |
| 1149 | if (success && output.back() == '/') { |
| 1150 | output += "test_details.xml"; |
| 1151 | } |
| 1152 | } |
| 1153 | if (success) { |
| 1154 | options.gtest_output = output; |
| 1155 | } else { |
| 1156 | fprintf(stderr, "invalid gtest_output file: %s\n", args[i]); |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
| 1160 | // Remove --gtest_output=xxx from arguments, so child process will not write xml file. |
| 1161 | args.erase(args.begin() + i); |
| 1162 | --i; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | // Add --no-isolate in args to prevent child process from running in isolation mode again. |
| 1167 | // As DeathTest will try to call execve(), this argument should always be added. |
| 1168 | args.insert(args.begin() + 1, strdup("--no-isolate")); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1169 | return true; |
| 1170 | } |
| 1171 | |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 1172 | static std::string get_proc_self_exe() { |
| 1173 | char path[PATH_MAX]; |
| 1174 | ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path)); |
| 1175 | if (path_len <= 0 || path_len >= static_cast<ssize_t>(sizeof(path))) { |
| 1176 | perror("readlink"); |
| 1177 | exit(1); |
| 1178 | } |
| 1179 | |
| 1180 | return std::string(path, path_len); |
| 1181 | } |
| 1182 | |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 1183 | int main(int argc, char** argv, char** envp) { |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 1184 | g_executable_path = get_proc_self_exe(); |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 1185 | g_argc = argc; |
| 1186 | g_argv = argv; |
| 1187 | g_envp = envp; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1188 | std::vector<char*> arg_list; |
| 1189 | for (int i = 0; i < argc; ++i) { |
| 1190 | arg_list.push_back(argv[i]); |
| 1191 | } |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1192 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1193 | IsolationTestOptions options; |
| 1194 | if (PickOptions(arg_list, options) == false) { |
| 1195 | return 1; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1196 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1197 | |
| 1198 | if (options.isolate == true) { |
| 1199 | // Set global variables. |
| 1200 | global_test_run_deadline_ms = options.test_deadline_ms; |
Elliott Hughes | a456fae | 2016-08-31 13:30:14 -0700 | [diff] [blame] | 1201 | global_test_run_slow_threshold_ms = options.test_slow_threshold_ms; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1202 | testing::GTEST_FLAG(color) = options.gtest_color.c_str(); |
| 1203 | testing::GTEST_FLAG(print_time) = options.gtest_print_time; |
| 1204 | std::vector<TestCase> testcase_list; |
| 1205 | |
| 1206 | argc = static_cast<int>(arg_list.size()); |
| 1207 | arg_list.push_back(NULL); |
| 1208 | if (EnumerateTests(argc, arg_list.data(), testcase_list) == false) { |
| 1209 | return 1; |
| 1210 | } |
Yabin Cui | 64a9c4f | 2015-03-12 22:16:03 -0700 | [diff] [blame] | 1211 | bool all_test_passed = RunTestInSeparateProc(argc, arg_list.data(), testcase_list, |
| 1212 | options.gtest_repeat, options.job_count, options.gtest_output); |
| 1213 | return all_test_passed ? 0 : 1; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1214 | } else { |
| 1215 | argc = static_cast<int>(arg_list.size()); |
| 1216 | arg_list.push_back(NULL); |
| 1217 | testing::InitGoogleTest(&argc, arg_list.data()); |
| 1218 | return RUN_ALL_TESTS(); |
| 1219 | } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | //################################################################################ |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1223 | // Bionic Gtest self test, run this by --bionic-selftest option. |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1224 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1225 | TEST(bionic_selftest, test_success) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1226 | ASSERT_EQ(1, 1); |
| 1227 | } |
| 1228 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1229 | TEST(bionic_selftest, test_fail) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1230 | ASSERT_EQ(0, 1); |
| 1231 | } |
| 1232 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1233 | TEST(bionic_selftest, test_time_warn) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1234 | sleep(4); |
| 1235 | } |
| 1236 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1237 | TEST(bionic_selftest, test_timeout) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1238 | while (1) {} |
| 1239 | } |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 1240 | |
| 1241 | TEST(bionic_selftest, test_signal_SEGV_terminated) { |
| 1242 | char* p = reinterpret_cast<char*>(static_cast<intptr_t>(atoi("0"))); |
| 1243 | *p = 3; |
| 1244 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1245 | |
Yabin Cui | 767fb1c | 2015-09-01 15:06:39 -0700 | [diff] [blame] | 1246 | class bionic_selftest_DeathTest : public ::testing::Test { |
| 1247 | protected: |
| 1248 | virtual void SetUp() { |
| 1249 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 1250 | } |
| 1251 | }; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame] | 1252 | |
| 1253 | static void deathtest_helper_success() { |
| 1254 | ASSERT_EQ(1, 1); |
| 1255 | exit(0); |
| 1256 | } |
| 1257 | |
| 1258 | TEST_F(bionic_selftest_DeathTest, success) { |
| 1259 | ASSERT_EXIT(deathtest_helper_success(), ::testing::ExitedWithCode(0), ""); |
| 1260 | } |
| 1261 | |
| 1262 | static void deathtest_helper_fail() { |
| 1263 | ASSERT_EQ(1, 0); |
| 1264 | } |
| 1265 | |
| 1266 | TEST_F(bionic_selftest_DeathTest, fail) { |
| 1267 | ASSERT_EXIT(deathtest_helper_fail(), ::testing::ExitedWithCode(0), ""); |
| 1268 | } |
Yabin Cui | 5e235c8 | 2017-11-16 16:20:28 -0800 | [diff] [blame^] | 1269 | |
| 1270 | class BionicSelfTest : public ::testing::TestWithParam<bool> { |
| 1271 | }; |
| 1272 | |
| 1273 | TEST_P(BionicSelfTest, test_success) { |
| 1274 | ASSERT_EQ(GetParam(), GetParam()); |
| 1275 | } |
| 1276 | |
| 1277 | INSTANTIATE_TEST_CASE_P(bionic_selftest, BionicSelfTest, ::testing::Values(true, false)); |
| 1278 | |
| 1279 | template <typename T> |
| 1280 | class bionic_selftest_TestT : public ::testing::Test { |
| 1281 | }; |
| 1282 | |
| 1283 | typedef ::testing::Types<char, int> MyTypes; |
| 1284 | |
| 1285 | TYPED_TEST_CASE(bionic_selftest_TestT, MyTypes); |
| 1286 | |
| 1287 | TYPED_TEST(bionic_selftest_TestT, test_success) { |
| 1288 | ASSERT_EQ(true, true); |
| 1289 | } |