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