blob: 11828eef24dc07edb370a4e84a8f5d5bffef9a3f [file] [log] [blame]
Yabin Cui294d1e22014-12-07 20:43:37 -08001/*
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
19#include <errno.h>
Yabin Cui657b1f92015-01-22 19:26:12 -080020#include <fcntl.h>
21#include <inttypes.h>
Yabin Cui294d1e22014-12-07 20:43:37 -080022#include <stdarg.h>
23#include <stdio.h>
24#include <string.h>
25#include <sys/wait.h>
26#include <time.h>
27#include <unistd.h>
28
29#include <string>
30#include <tuple>
31#include <utility>
32#include <vector>
33
Yabin Cui657b1f92015-01-22 19:26:12 -080034#include "BionicDeathTest.h" // For selftest.
35
Yabin Cui294d1e22014-12-07 20:43:37 -080036namespace testing {
37namespace internal {
38
39// Reuse of testing::internal::ColoredPrintf in gtest.
40enum GTestColor {
41 COLOR_DEFAULT,
42 COLOR_RED,
43 COLOR_GREEN,
44 COLOR_YELLOW
45};
46
47void ColoredPrintf(GTestColor color, const char* fmt, ...);
48
Yabin Cuibe837362015-01-02 18:45:37 -080049} // namespace internal
50} // namespace testing
Yabin Cui294d1e22014-12-07 20:43:37 -080051
52using testing::internal::GTestColor;
53using testing::internal::COLOR_DEFAULT;
54using testing::internal::COLOR_RED;
55using testing::internal::COLOR_GREEN;
56using testing::internal::COLOR_YELLOW;
57using testing::internal::ColoredPrintf;
58
Yabin Cui657b1f92015-01-22 19:26:12 -080059constexpr int DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS = 60000;
60constexpr int DEFAULT_GLOBAL_TEST_RUN_WARNLINE_MS = 2000;
Yabin Cui294d1e22014-12-07 20:43:37 -080061
62// The time each test can run before killed for the reason of timeout.
63// It takes effect only with --isolate option.
Yabin Cui657b1f92015-01-22 19:26:12 -080064static int global_test_run_deadline_ms = DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS;
Yabin Cui294d1e22014-12-07 20:43:37 -080065
66// The time each test can run before be warned for too much running time.
67// It takes effect only with --isolate option.
Yabin Cui657b1f92015-01-22 19:26:12 -080068static int global_test_run_warnline_ms = DEFAULT_GLOBAL_TEST_RUN_WARNLINE_MS;
Yabin Cui294d1e22014-12-07 20:43:37 -080069
70// Return deadline duration for a test, in ms.
71static int GetDeadlineInfo(const std::string& /*test_name*/) {
Yabin Cui657b1f92015-01-22 19:26:12 -080072 return global_test_run_deadline_ms;
Yabin Cui294d1e22014-12-07 20:43:37 -080073}
74
75// Return warnline duration for a test, in ms.
76static int GetWarnlineInfo(const std::string& /*test_name*/) {
Yabin Cui657b1f92015-01-22 19:26:12 -080077 return global_test_run_warnline_ms;
Yabin Cui294d1e22014-12-07 20:43:37 -080078}
79
Yabin Cuibe837362015-01-02 18:45:37 -080080static void PrintHelpInfo() {
81 printf("Bionic Unit Test Options:\n"
Yabin Cui657b1f92015-01-22 19:26:12 -080082 " -j [JOB_COUNT] or -j[JOB_COUNT]\n"
Yabin Cuibe837362015-01-02 18:45:37 -080083 " Run up to JOB_COUNT tests in parallel.\n"
84 " Use isolation mode, Run each test in a separate process.\n"
85 " If JOB_COUNT is not given, it is set to the count of available processors.\n"
86 " --no-isolate\n"
87 " Don't use isolation mode, run all tests in a single process.\n"
88 " --deadline=[TIME_IN_MS]\n"
89 " Run each test in no longer than [TIME_IN_MS] time.\n"
90 " It takes effect only in isolation mode. Deafult deadline is 60000 ms.\n"
91 " --warnline=[TIME_IN_MS]\n"
92 " Test running longer than [TIME_IN_MS] will be warned.\n"
93 " It takes effect only in isolation mode. Default warnline is 2000 ms.\n"
94 "\nDefault bionic unit test option is -j.\n"
95 "\n");
96}
97
Yabin Cui294d1e22014-12-07 20:43:37 -080098enum TestResult {
99 TEST_SUCCESS = 0,
100 TEST_FAILED,
101 TEST_TIMEOUT
102};
103
Yabin Cui657b1f92015-01-22 19:26:12 -0800104class Test {
105 public:
106 Test() {} // For std::vector<Test>.
107 explicit Test(const char* name) : name_(name) {}
108
109 const std::string& GetName() const { return name_; }
110
111 void SetResult(TestResult result) { result_ = result; }
112
113 TestResult GetResult() const { return result_; }
114
115 void SetTestTime(int64_t elapsed_time_ns) { elapsed_time_ns_ = elapsed_time_ns; }
116
117 int64_t GetTestTime() const { return elapsed_time_ns_; }
118
119 void AppendFailureMessage(const std::string& s) { failure_message_ += s; }
120
121 const std::string& GetFailureMessage() const { return failure_message_; }
122
123 private:
124 const std::string name_;
125 TestResult result_;
126 int64_t elapsed_time_ns_;
127 std::string failure_message_;
128};
129
Yabin Cui294d1e22014-12-07 20:43:37 -0800130class TestCase {
131 public:
132 TestCase() {} // For std::vector<TestCase>.
133 explicit TestCase(const char* name) : name_(name) {}
134
135 const std::string& GetName() const { return name_; }
136
Yabin Cui657b1f92015-01-22 19:26:12 -0800137 void AppendTest(const char* test_name) {
138 test_list_.push_back(Test(test_name));
Yabin Cui294d1e22014-12-07 20:43:37 -0800139 }
140
Yabin Cuibe837362015-01-02 18:45:37 -0800141 size_t TestCount() const { return test_list_.size(); }
Yabin Cui294d1e22014-12-07 20:43:37 -0800142
Yabin Cuibe837362015-01-02 18:45:37 -0800143 std::string GetTestName(size_t test_id) const {
Yabin Cui294d1e22014-12-07 20:43:37 -0800144 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800145 return name_ + "." + test_list_[test_id].GetName();
146 }
147
148 Test& GetTest(size_t test_id) {
149 VerifyTestId(test_id);
150 return test_list_[test_id];
151 }
152
153 const Test& GetTest(size_t test_id) const {
154 VerifyTestId(test_id);
155 return test_list_[test_id];
Yabin Cui294d1e22014-12-07 20:43:37 -0800156 }
157
Yabin Cuibe837362015-01-02 18:45:37 -0800158 void SetTestResult(size_t test_id, TestResult result) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800159 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800160 test_list_[test_id].SetResult(result);
Yabin Cui294d1e22014-12-07 20:43:37 -0800161 }
162
Yabin Cuibe837362015-01-02 18:45:37 -0800163 TestResult GetTestResult(size_t test_id) const {
Yabin Cui294d1e22014-12-07 20:43:37 -0800164 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800165 return test_list_[test_id].GetResult();
Yabin Cui294d1e22014-12-07 20:43:37 -0800166 }
167
Yabin Cui657b1f92015-01-22 19:26:12 -0800168 void SetTestTime(size_t test_id, int64_t elapsed_time_ns) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800169 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800170 test_list_[test_id].SetTestTime(elapsed_time_ns);
Yabin Cui294d1e22014-12-07 20:43:37 -0800171 }
172
Yabin Cuibe837362015-01-02 18:45:37 -0800173 int64_t GetTestTime(size_t test_id) const {
Yabin Cui294d1e22014-12-07 20:43:37 -0800174 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800175 return test_list_[test_id].GetTestTime();
Yabin Cui294d1e22014-12-07 20:43:37 -0800176 }
177
178 private:
Yabin Cuibe837362015-01-02 18:45:37 -0800179 void VerifyTestId(size_t test_id) const {
180 if(test_id >= test_list_.size()) {
181 fprintf(stderr, "test_id %zu out of range [0, %zu)\n", test_id, test_list_.size());
Yabin Cui294d1e22014-12-07 20:43:37 -0800182 exit(1);
183 }
184 }
185
186 private:
187 const std::string name_;
Yabin Cui657b1f92015-01-22 19:26:12 -0800188 std::vector<Test> test_list_;
Yabin Cui294d1e22014-12-07 20:43:37 -0800189};
190
Yabin Cui657b1f92015-01-22 19:26:12 -0800191// This is the file descriptor used by the child process to write failure message.
192// The parent process will collect the information and dump to stdout / xml file.
193static int child_output_fd;
194
Yabin Cui294d1e22014-12-07 20:43:37 -0800195class TestResultPrinter : public testing::EmptyTestEventListener {
196 public:
197 TestResultPrinter() : pinfo_(NULL) {}
198 virtual void OnTestStart(const testing::TestInfo& test_info) {
199 pinfo_ = &test_info; // Record test_info for use in OnTestPartResult.
200 }
201 virtual void OnTestPartResult(const testing::TestPartResult& result);
Yabin Cui294d1e22014-12-07 20:43:37 -0800202
203 private:
204 const testing::TestInfo* pinfo_;
205};
206
207// Called after an assertion failure.
208void TestResultPrinter::OnTestPartResult(const testing::TestPartResult& result) {
209 // If the test part succeeded, we don't need to do anything.
210 if (result.type() == testing::TestPartResult::kSuccess)
211 return;
212
213 // Print failure message from the assertion (e.g. expected this and got that).
214 char buf[1024];
215 snprintf(buf, sizeof(buf), "%s:(%d) Failure in test %s.%s\n%s\n", result.file_name(),
216 result.line_number(),
217 pinfo_->test_case_name(),
218 pinfo_->name(),
219 result.message());
220
Yabin Cui294d1e22014-12-07 20:43:37 -0800221 int towrite = strlen(buf);
222 char* p = buf;
223 while (towrite > 0) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800224 ssize_t write_count = TEMP_FAILURE_RETRY(write(child_output_fd, p, towrite));
Yabin Cuibe837362015-01-02 18:45:37 -0800225 if (write_count == -1) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800226 fprintf(stderr, "failed to write child_output_fd: %s\n", strerror(errno));
227 exit(1);
Yabin Cui294d1e22014-12-07 20:43:37 -0800228 } else {
Yabin Cuibe837362015-01-02 18:45:37 -0800229 towrite -= write_count;
230 p += write_count;
Yabin Cui294d1e22014-12-07 20:43:37 -0800231 }
232 }
233}
234
Yabin Cui294d1e22014-12-07 20:43:37 -0800235static int64_t NanoTime() {
236 struct timespec t;
237 t.tv_sec = t.tv_nsec = 0;
238 clock_gettime(CLOCK_MONOTONIC, &t);
239 return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
240}
241
242static bool EnumerateTests(int argc, char** argv, std::vector<TestCase>& testcase_list) {
243 std::string command;
244 for (int i = 0; i < argc; ++i) {
245 command += argv[i];
246 command += " ";
247 }
248 command += "--gtest_list_tests";
249 FILE* fp = popen(command.c_str(), "r");
250 if (fp == NULL) {
251 perror("popen");
252 return false;
253 }
254
255 char buf[200];
256 while (fgets(buf, sizeof(buf), fp) != NULL) {
257 char* p = buf;
258
259 while (*p != '\0' && isspace(*p)) {
260 ++p;
261 }
262 if (*p == '\0') continue;
263 char* start = p;
264 while (*p != '\0' && !isspace(*p)) {
265 ++p;
266 }
267 char* end = p;
268 while (*p != '\0' && isspace(*p)) {
269 ++p;
270 }
271 if (*p != '\0') {
272 // This is not we want, gtest must meet with some error when parsing the arguments.
273 fprintf(stderr, "argument error, check with --help\n");
274 return false;
275 }
276 *end = '\0';
277 if (*(end - 1) == '.') {
278 *(end - 1) = '\0';
279 testcase_list.push_back(TestCase(start));
280 } else {
281 testcase_list.back().AppendTest(start);
282 }
283 }
284 int result = pclose(fp);
285 return (result != -1 && WEXITSTATUS(result) == 0);
286}
287
Yabin Cui294d1e22014-12-07 20:43:37 -0800288// Part of the following *Print functions are copied from external/gtest/src/gtest.cc:
289// PrettyUnitTestResultPrinter. The reason for copy is that PrettyUnitTestResultPrinter
290// is defined and used in gtest.cc, which is hard to reuse.
Yabin Cuibe837362015-01-02 18:45:37 -0800291static void OnTestIterationStartPrint(const std::vector<TestCase>& testcase_list, size_t iteration,
292 size_t iteration_count) {
293 if (iteration_count > 1) {
294 printf("\nRepeating all tests (iteration %zu) . . .\n\n", iteration);
Yabin Cui294d1e22014-12-07 20:43:37 -0800295 }
296 ColoredPrintf(COLOR_GREEN, "[==========] ");
297
Yabin Cuibe837362015-01-02 18:45:37 -0800298 size_t testcase_count = testcase_list.size();
299 size_t test_count = 0;
Yabin Cui294d1e22014-12-07 20:43:37 -0800300 for (const auto& testcase : testcase_list) {
Yabin Cuibe837362015-01-02 18:45:37 -0800301 test_count += testcase.TestCount();
Yabin Cui294d1e22014-12-07 20:43:37 -0800302 }
303
Yabin Cuibe837362015-01-02 18:45:37 -0800304 printf("Running %zu %s from %zu %s.\n",
305 test_count, (test_count == 1) ? "test" : "tests",
306 testcase_count, (testcase_count == 1) ? "test case" : "test cases");
Yabin Cui294d1e22014-12-07 20:43:37 -0800307 fflush(stdout);
308}
309
Yabin Cui657b1f92015-01-22 19:26:12 -0800310static void OnTestEndPrint(const TestCase& testcase, size_t test_id) {
311 TestResult result = testcase.GetTestResult(test_id);
312 if (result == TEST_SUCCESS) {
313 ColoredPrintf(COLOR_GREEN, "[ OK ] ");
314 } else if (result == TEST_FAILED) {
315 ColoredPrintf(COLOR_RED, "[ FAILED ] ");
316 } else if (result == TEST_TIMEOUT) {
317 ColoredPrintf(COLOR_RED, "[ TIMEOUT ] ");
318 }
Yabin Cuibe837362015-01-02 18:45:37 -0800319
Yabin Cui657b1f92015-01-22 19:26:12 -0800320 printf("%s", testcase.GetTestName(test_id).c_str());
321 if (testing::GTEST_FLAG(print_time)) {
322 printf(" (%" PRId64 " ms)\n", testcase.GetTestTime(test_id) / 1000000);
323 } else {
324 printf("\n");
325 }
326
327 const std::string& failure_message = testcase.GetTest(test_id).GetFailureMessage();
328 printf("%s", failure_message.c_str());
Yabin Cui294d1e22014-12-07 20:43:37 -0800329 fflush(stdout);
330}
331
Yabin Cuibe837362015-01-02 18:45:37 -0800332static void OnTestIterationEndPrint(const std::vector<TestCase>& testcase_list, size_t /*iteration*/,
Yabin Cui657b1f92015-01-22 19:26:12 -0800333 int64_t elapsed_time_ns) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800334
335 std::vector<std::string> fail_test_name_list;
336 std::vector<std::pair<std::string, int64_t>> timeout_test_list;
337
338 // For tests run exceed warnline but not timeout.
Yabin Cui4a82ede2015-01-26 17:19:37 -0800339 std::vector<std::tuple<std::string, int64_t, int>> slow_test_list;
Yabin Cuibe837362015-01-02 18:45:37 -0800340 size_t testcase_count = testcase_list.size();
341 size_t test_count = 0;
342 size_t success_test_count = 0;
Yabin Cui294d1e22014-12-07 20:43:37 -0800343
344 for (const auto& testcase : testcase_list) {
Yabin Cuibe837362015-01-02 18:45:37 -0800345 test_count += testcase.TestCount();
346 for (size_t i = 0; i < testcase.TestCount(); ++i) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800347 TestResult result = testcase.GetTestResult(i);
348 if (result == TEST_SUCCESS) {
Yabin Cuibe837362015-01-02 18:45:37 -0800349 ++success_test_count;
Yabin Cui294d1e22014-12-07 20:43:37 -0800350 } else if (result == TEST_FAILED) {
351 fail_test_name_list.push_back(testcase.GetTestName(i));
352 } else if (result == TEST_TIMEOUT) {
353 timeout_test_list.push_back(std::make_pair(testcase.GetTestName(i),
354 testcase.GetTestTime(i)));
355 }
356 if (result != TEST_TIMEOUT &&
357 testcase.GetTestTime(i) / 1000000 >= GetWarnlineInfo(testcase.GetTestName(i))) {
Yabin Cui4a82ede2015-01-26 17:19:37 -0800358 slow_test_list.push_back(std::make_tuple(testcase.GetTestName(i),
359 testcase.GetTestTime(i),
360 GetWarnlineInfo(testcase.GetTestName(i))));
Yabin Cui294d1e22014-12-07 20:43:37 -0800361 }
362 }
363 }
364
Yabin Cui294d1e22014-12-07 20:43:37 -0800365 ColoredPrintf(COLOR_GREEN, "[==========] ");
Yabin Cuibe837362015-01-02 18:45:37 -0800366 printf("%zu %s from %zu %s ran.", test_count, (test_count == 1) ? "test" : "tests",
367 testcase_count, (testcase_count == 1) ? "test case" : "test cases");
Yabin Cui294d1e22014-12-07 20:43:37 -0800368 if (testing::GTEST_FLAG(print_time)) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800369 printf(" (%" PRId64 " ms total)", elapsed_time_ns / 1000000);
Yabin Cui294d1e22014-12-07 20:43:37 -0800370 }
371 printf("\n");
Yabin Cui4a82ede2015-01-26 17:19:37 -0800372 ColoredPrintf(COLOR_GREEN, "[ PASS ] ");
Yabin Cuibe837362015-01-02 18:45:37 -0800373 printf("%zu %s.\n", success_test_count, (success_test_count == 1) ? "test" : "tests");
Yabin Cui294d1e22014-12-07 20:43:37 -0800374
375 // Print tests failed.
Yabin Cuibe837362015-01-02 18:45:37 -0800376 size_t fail_test_count = fail_test_name_list.size();
377 if (fail_test_count > 0) {
Yabin Cui4a82ede2015-01-26 17:19:37 -0800378 ColoredPrintf(COLOR_RED, "[ FAIL ] ");
Yabin Cuibe837362015-01-02 18:45:37 -0800379 printf("%zu %s, listed below:\n", fail_test_count, (fail_test_count == 1) ? "test" : "tests");
Yabin Cui294d1e22014-12-07 20:43:37 -0800380 for (const auto& name : fail_test_name_list) {
Yabin Cui4a82ede2015-01-26 17:19:37 -0800381 ColoredPrintf(COLOR_RED, "[ FAIL ] ");
Yabin Cui294d1e22014-12-07 20:43:37 -0800382 printf("%s\n", name.c_str());
383 }
384 }
385
386 // Print tests run timeout.
Yabin Cuibe837362015-01-02 18:45:37 -0800387 size_t timeout_test_count = timeout_test_list.size();
388 if (timeout_test_count > 0) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800389 ColoredPrintf(COLOR_RED, "[ TIMEOUT ] ");
Yabin Cuibe837362015-01-02 18:45:37 -0800390 printf("%zu %s, listed below:\n", timeout_test_count, (timeout_test_count == 1) ? "test" : "tests");
Yabin Cui294d1e22014-12-07 20:43:37 -0800391 for (const auto& timeout_pair : timeout_test_list) {
392 ColoredPrintf(COLOR_RED, "[ TIMEOUT ] ");
Yabin Cui657b1f92015-01-22 19:26:12 -0800393 printf("%s (stopped at %" PRId64 " ms)\n", timeout_pair.first.c_str(),
394 timeout_pair.second / 1000000);
Yabin Cui294d1e22014-12-07 20:43:37 -0800395 }
396 }
397
398 // Print tests run exceed warnline.
Yabin Cui4a82ede2015-01-26 17:19:37 -0800399 size_t slow_test_count = slow_test_list.size();
400 if (slow_test_count > 0) {
401 ColoredPrintf(COLOR_YELLOW, "[ SLOW ] ");
402 printf("%zu %s, listed below:\n", slow_test_count, (slow_test_count == 1) ? "test" : "tests");
403 for (const auto& slow_tuple : slow_test_list) {
404 ColoredPrintf(COLOR_YELLOW, "[ SLOW ] ");
405 printf("%s (%" PRId64 " ms, exceed warnline %d ms)\n", std::get<0>(slow_tuple).c_str(),
406 std::get<1>(slow_tuple) / 1000000, std::get<2>(slow_tuple));
Yabin Cui294d1e22014-12-07 20:43:37 -0800407 }
408 }
409
Yabin Cuibe837362015-01-02 18:45:37 -0800410 if (fail_test_count > 0) {
411 printf("\n%2zu FAILED %s\n", fail_test_count, (fail_test_count == 1) ? "TEST" : "TESTS");
Yabin Cui294d1e22014-12-07 20:43:37 -0800412 }
Yabin Cuibe837362015-01-02 18:45:37 -0800413 if (timeout_test_count > 0) {
414 printf("%2zu TIMEOUT %s\n", timeout_test_count, (timeout_test_count == 1) ? "TEST" : "TESTS");
Yabin Cui294d1e22014-12-07 20:43:37 -0800415 }
Yabin Cui4a82ede2015-01-26 17:19:37 -0800416 if (slow_test_count > 0) {
417 printf("%2zu SLOW %s\n", slow_test_count, (slow_test_count == 1) ? "TEST" : "TESTS");
Yabin Cui294d1e22014-12-07 20:43:37 -0800418 }
419 fflush(stdout);
420}
421
Yabin Cui657b1f92015-01-22 19:26:12 -0800422// Output xml file when --gtest_output is used, write this function as we can't reuse
423// gtest.cc:XmlUnitTestResultPrinter. The reason is XmlUnitTestResultPrinter is totally
424// defined in gtest.cc and not expose to outside. What's more, as we don't run gtest in
425// the parent process, we don't have gtest classes which are needed by XmlUnitTestResultPrinter.
426void OnTestIterationEndXmlPrint(const std::string& xml_output_filename,
427 const std::vector<TestCase>& testcase_list,
428 time_t epoch_iteration_start_time,
429 int64_t elapsed_time_ns) {
430 FILE* fp = fopen(xml_output_filename.c_str(), "w");
431 if (fp == NULL) {
432 fprintf(stderr, "failed to open '%s': %s\n", xml_output_filename.c_str(), strerror(errno));
433 exit(1);
434 }
435
436 size_t total_test_count = 0;
437 size_t total_failed_count = 0;
438 std::vector<size_t> failed_count_list(testcase_list.size(), 0);
439 std::vector<int64_t> elapsed_time_list(testcase_list.size(), 0);
440 for (size_t i = 0; i < testcase_list.size(); ++i) {
441 auto& testcase = testcase_list[i];
442 total_test_count += testcase.TestCount();
443 for (size_t j = 0; j < testcase.TestCount(); ++j) {
444 if (testcase.GetTestResult(j) != TEST_SUCCESS) {
445 ++failed_count_list[i];
446 }
447 elapsed_time_list[i] += testcase.GetTestTime(j);
448 }
449 total_failed_count += failed_count_list[i];
450 }
451
452 const tm* time_struct = localtime(&epoch_iteration_start_time);
453 char timestamp[40];
454 snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d",
455 time_struct->tm_year + 1900, time_struct->tm_mon + 1, time_struct->tm_mday,
456 time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);
457
458 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
459 fprintf(fp, "<testsuites tests=\"%zu\" failures=\"%zu\" disabled=\"0\" errors=\"0\"",
460 total_test_count, total_failed_count);
461 fprintf(fp, " timestamp=\"%s\" time=\"%.3lf\" name=\"AllTests\">\n", timestamp, elapsed_time_ns / 1e9);
462 for (size_t i = 0; i < testcase_list.size(); ++i) {
463 auto& testcase = testcase_list[i];
464 fprintf(fp, " <testsuite name=\"%s\" tests=\"%zu\" failures=\"%zu\" disabled=\"0\" errors=\"0\"",
465 testcase.GetName().c_str(), testcase.TestCount(), failed_count_list[i]);
466 fprintf(fp, " time=\"%.3lf\">\n", elapsed_time_list[i] / 1e9);
467
468 for (size_t j = 0; j < testcase.TestCount(); ++j) {
469 fprintf(fp, " <testcase name=\"%s\" status=\"run\" time=\"%.3lf\" classname=\"%s\"",
470 testcase.GetTest(j).GetName().c_str(), testcase.GetTestTime(j) / 1e9,
471 testcase.GetName().c_str());
472 if (testcase.GetTestResult(j) == TEST_SUCCESS) {
473 fputs(" />\n", fp);
474 } else {
475 fputs(">\n", fp);
476 const std::string& failure_message = testcase.GetTest(j).GetFailureMessage();
477 fprintf(fp, " <failure message=\"%s\" type=\"\">\n", failure_message.c_str());
478 fputs(" </failure>\n", fp);
479 fputs(" </testcase>\n", fp);
480 }
481 }
482
483 fputs(" </testsuite>\n", fp);
484 }
485 fputs("</testsuites>\n", fp);
486 fclose(fp);
487}
488
Yabin Cui294d1e22014-12-07 20:43:37 -0800489// Forked Child process, run the single test.
490static void ChildProcessFn(int argc, char** argv, const std::string& test_name) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800491 char** new_argv = new char*[argc + 2];
Yabin Cui294d1e22014-12-07 20:43:37 -0800492 memcpy(new_argv, argv, sizeof(char*) * argc);
493
494 char* filter_arg = new char [test_name.size() + 20];
495 strcpy(filter_arg, "--gtest_filter=");
496 strcat(filter_arg, test_name.c_str());
497 new_argv[argc] = filter_arg;
Yabin Cui657b1f92015-01-22 19:26:12 -0800498 new_argv[argc + 1] = NULL;
Yabin Cui294d1e22014-12-07 20:43:37 -0800499
500 int new_argc = argc + 1;
501 testing::InitGoogleTest(&new_argc, new_argv);
502 int result = RUN_ALL_TESTS();
503 exit(result);
504}
505
506struct ChildProcInfo {
507 pid_t pid;
Yabin Cui657b1f92015-01-22 19:26:12 -0800508 int64_t start_time_ns;
509 int64_t deadline_time_ns;
Yabin Cuibe837362015-01-02 18:45:37 -0800510 size_t testcase_id, test_id;
Yabin Cui294d1e22014-12-07 20:43:37 -0800511 bool done_flag;
Yabin Cuibe837362015-01-02 18:45:37 -0800512 bool timeout_flag;
513 int exit_status;
Yabin Cui657b1f92015-01-22 19:26:12 -0800514 int child_read_fd;
Yabin Cui294d1e22014-12-07 20:43:37 -0800515 ChildProcInfo() : pid(0) {}
516};
517
518static void WaitChildProcs(std::vector<ChildProcInfo>& child_proc_list) {
519 pid_t result;
Yabin Cuibe837362015-01-02 18:45:37 -0800520 int status;
Yabin Cui294d1e22014-12-07 20:43:37 -0800521 bool loop_flag = true;
522
523 while (true) {
Yabin Cuibe837362015-01-02 18:45:37 -0800524 while ((result = waitpid(-1, &status, WNOHANG)) == -1) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800525 if (errno != EINTR) {
526 break;
527 }
528 }
529
530 if (result == -1) {
531 perror("waitpid");
532 exit(1);
533 } else if (result == 0) {
534 // Check child timeout.
Yabin Cui657b1f92015-01-22 19:26:12 -0800535 int64_t current_time_ns = NanoTime();
Yabin Cui294d1e22014-12-07 20:43:37 -0800536 for (size_t i = 0; i < child_proc_list.size(); ++i) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800537 if (child_proc_list[i].deadline_time_ns <= current_time_ns) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800538 child_proc_list[i].done_flag = true;
Yabin Cuibe837362015-01-02 18:45:37 -0800539 child_proc_list[i].timeout_flag = true;
Yabin Cui294d1e22014-12-07 20:43:37 -0800540 loop_flag = false;
541 }
542 }
543 } else {
544 // Check child finish.
545 for (size_t i = 0; i < child_proc_list.size(); ++i) {
546 if (child_proc_list[i].pid == result) {
547 child_proc_list[i].done_flag = true;
Yabin Cuibe837362015-01-02 18:45:37 -0800548 child_proc_list[i].timeout_flag = false;
549 child_proc_list[i].exit_status = status;
Yabin Cui294d1e22014-12-07 20:43:37 -0800550 loop_flag = false;
551 break;
552 }
553 }
554 }
555
556 if (!loop_flag) break;
557 // sleep 1 ms to avoid busy looping.
558 timespec sleep_time;
559 sleep_time.tv_sec = 0;
560 sleep_time.tv_nsec = 1000000;
561 nanosleep(&sleep_time, NULL);
562 }
563}
564
565static TestResult WaitChildProc(pid_t pid) {
566 pid_t result;
567 int exit_status;
568
569 while ((result = waitpid(pid, &exit_status, 0)) == -1) {
570 if (errno != EINTR) {
571 break;
572 }
573 }
574
575 TestResult test_result = TEST_SUCCESS;
576 if (result != pid || WEXITSTATUS(exit_status) != 0) {
577 test_result = TEST_FAILED;
578 }
579 return test_result;
580}
581
582// We choose to use multi-fork and multi-wait here instead of multi-thread, because it always
583// makes deadlock to use fork in multi-thread.
584static void RunTestInSeparateProc(int argc, char** argv, std::vector<TestCase>& testcase_list,
Yabin Cui657b1f92015-01-22 19:26:12 -0800585 size_t iteration_count, size_t job_count,
586 const std::string& xml_output_filename) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800587 // Stop default result printer to avoid environment setup/teardown information for each test.
588 testing::UnitTest::GetInstance()->listeners().Release(
589 testing::UnitTest::GetInstance()->listeners().default_result_printer());
590 testing::UnitTest::GetInstance()->listeners().Append(new TestResultPrinter);
591
Yabin Cuibe837362015-01-02 18:45:37 -0800592 for (size_t iteration = 1; iteration <= iteration_count; ++iteration) {
593 OnTestIterationStartPrint(testcase_list, iteration, iteration_count);
Yabin Cui657b1f92015-01-22 19:26:12 -0800594 int64_t iteration_start_time_ns = NanoTime();
595 time_t epoch_iteration_start_time = time(NULL);
Yabin Cui294d1e22014-12-07 20:43:37 -0800596
Yabin Cuibe837362015-01-02 18:45:37 -0800597 // Run up to job_count tests in parallel, each test in a child process.
598 std::vector<ChildProcInfo> child_proc_list(job_count);
Yabin Cui294d1e22014-12-07 20:43:37 -0800599
Yabin Cuibe837362015-01-02 18:45:37 -0800600 // Next test to run is [next_testcase_id:next_test_id].
601 size_t next_testcase_id = 0;
602 size_t next_test_id = 0;
603
604 // Record how many tests are finished.
605 std::vector<size_t> finished_test_count_list(testcase_list.size(), 0);
606 size_t finished_testcase_count = 0;
607
608 while (finished_testcase_count < testcase_list.size()) {
609 // Fork up to job_count child processes.
Yabin Cui294d1e22014-12-07 20:43:37 -0800610 for (auto& child_proc : child_proc_list) {
Yabin Cuibe837362015-01-02 18:45:37 -0800611 if (child_proc.pid == 0 && next_testcase_id < testcase_list.size()) {
612 std::string test_name = testcase_list[next_testcase_id].GetTestName(next_test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800613 int pipefd[2];
614 int ret = pipe(pipefd);
615 if (ret == -1) {
616 perror("pipe2 in RunTestInSeparateProc");
617 exit(1);
618 }
Yabin Cui294d1e22014-12-07 20:43:37 -0800619 pid_t pid = fork();
620 if (pid == -1) {
621 perror("fork in RunTestInSeparateProc");
622 exit(1);
623 } else if (pid == 0) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800624 close(pipefd[0]);
625 child_output_fd = pipefd[1];
Yabin Cui294d1e22014-12-07 20:43:37 -0800626 // Run child process test, never return.
627 ChildProcessFn(argc, argv, test_name);
628 }
629 // Parent process
Yabin Cui657b1f92015-01-22 19:26:12 -0800630 close(pipefd[1]);
631 child_proc.child_read_fd = pipefd[0];
Yabin Cui294d1e22014-12-07 20:43:37 -0800632 child_proc.pid = pid;
Yabin Cui657b1f92015-01-22 19:26:12 -0800633 child_proc.start_time_ns = NanoTime();
634 child_proc.deadline_time_ns = child_proc.start_time_ns +
635 GetDeadlineInfo(test_name) * 1000000LL;
Yabin Cuibe837362015-01-02 18:45:37 -0800636 child_proc.testcase_id = next_testcase_id;
637 child_proc.test_id = next_test_id;
Yabin Cui294d1e22014-12-07 20:43:37 -0800638 child_proc.done_flag = false;
Yabin Cuibe837362015-01-02 18:45:37 -0800639 if (++next_test_id == testcase_list[next_testcase_id].TestCount()) {
640 next_test_id = 0;
641 ++next_testcase_id;
Yabin Cui294d1e22014-12-07 20:43:37 -0800642 }
643 }
644 }
645
646 // Wait for any child proc finish or timeout.
647 WaitChildProcs(child_proc_list);
648
649 // Collect result.
650 for (auto& child_proc : child_proc_list) {
651 if (child_proc.pid != 0 && child_proc.done_flag == true) {
Yabin Cuibe837362015-01-02 18:45:37 -0800652 size_t testcase_id = child_proc.testcase_id;
653 size_t test_id = child_proc.test_id;
654 TestCase& testcase = testcase_list[testcase_id];
Yabin Cui657b1f92015-01-22 19:26:12 -0800655 testcase.SetTestTime(test_id, NanoTime() - child_proc.start_time_ns);
Yabin Cuibe837362015-01-02 18:45:37 -0800656
Yabin Cui657b1f92015-01-22 19:26:12 -0800657 // Kill and wait the timeout child process before we read failure message.
Yabin Cuibe837362015-01-02 18:45:37 -0800658 if (child_proc.timeout_flag) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800659 kill(child_proc.pid, SIGKILL);
660 WaitChildProc(child_proc.pid);
Yabin Cui657b1f92015-01-22 19:26:12 -0800661 }
662
663 while (true) {
664 char buf[1024];
665 int ret = TEMP_FAILURE_RETRY(read(child_proc.child_read_fd, buf, sizeof(buf) - 1));
666 if (ret > 0) {
667 buf[ret] = '\0';
668 testcase.GetTest(test_id).AppendFailureMessage(buf);
669 } else if (ret == 0) {
670 break; // Read end.
671 } else {
672 perror("read child_read_fd in RunTestInSeparateProc");
673 exit(1);
674 }
675 }
676 close(child_proc.child_read_fd);
677
678 if (child_proc.timeout_flag) {
Yabin Cuibe837362015-01-02 18:45:37 -0800679 testcase.SetTestResult(test_id, TEST_TIMEOUT);
Yabin Cui657b1f92015-01-22 19:26:12 -0800680 char buf[1024];
681 snprintf(buf, sizeof(buf), "%s killed because of timeout at %" PRId64 " ms.\n",
682 testcase.GetTestName(test_id).c_str(),
683 testcase.GetTestTime(test_id) / 1000000);
684 testcase.GetTest(test_id).AppendFailureMessage(buf);
Yabin Cuibe837362015-01-02 18:45:37 -0800685
686 } else if (WIFSIGNALED(child_proc.exit_status)) {
687 // Record signal terminated test as failed.
688 testcase.SetTestResult(test_id, TEST_FAILED);
Yabin Cui657b1f92015-01-22 19:26:12 -0800689 char buf[1024];
690 snprintf(buf, sizeof(buf), "%s terminated by signal: %s.\n",
691 testcase.GetTestName(test_id).c_str(),
692 strsignal(WTERMSIG(child_proc.exit_status)));
693 testcase.GetTest(test_id).AppendFailureMessage(buf);
Yabin Cuibe837362015-01-02 18:45:37 -0800694
695 } else {
696 testcase.SetTestResult(test_id, WEXITSTATUS(child_proc.exit_status) == 0 ?
697 TEST_SUCCESS : TEST_FAILED);
Yabin Cui294d1e22014-12-07 20:43:37 -0800698 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800699 OnTestEndPrint(testcase, test_id);
Yabin Cuibe837362015-01-02 18:45:37 -0800700
701 if (++finished_test_count_list[testcase_id] == testcase.TestCount()) {
702 ++finished_testcase_count;
Yabin Cui294d1e22014-12-07 20:43:37 -0800703 }
704 child_proc.pid = 0;
705 child_proc.done_flag = false;
706 }
707 }
708 }
709
Yabin Cui657b1f92015-01-22 19:26:12 -0800710 int64_t elapsed_time_ns = NanoTime() - iteration_start_time_ns;
711 OnTestIterationEndPrint(testcase_list, iteration, elapsed_time_ns);
712 if (!xml_output_filename.empty()) {
713 OnTestIterationEndXmlPrint(xml_output_filename, testcase_list, epoch_iteration_start_time,
714 elapsed_time_ns);
715 }
Yabin Cui294d1e22014-12-07 20:43:37 -0800716 }
717}
718
Yabin Cuibe837362015-01-02 18:45:37 -0800719static size_t GetProcessorCount() {
720 return static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN));
Yabin Cui294d1e22014-12-07 20:43:37 -0800721}
722
Yabin Cui657b1f92015-01-22 19:26:12 -0800723struct IsolationTestOptions {
724 bool isolate;
725 size_t job_count;
726 int test_deadline_ms;
727 int test_warnline_ms;
728 std::string gtest_color;
729 bool gtest_print_time;
730 size_t gtest_repeat;
731 std::string gtest_output;
732};
733
734// Pick options not for gtest: There are two parts in args, one part is used in isolation test mode
Yabin Cuibe837362015-01-02 18:45:37 -0800735// as described in PrintHelpInfo(), the other part is handled by testing::InitGoogleTest() in
Yabin Cui657b1f92015-01-22 19:26:12 -0800736// gtest. PickOptions() picks the first part into IsolationTestOptions structure, leaving the second
737// part in args.
Yabin Cuibe837362015-01-02 18:45:37 -0800738// Arguments:
Yabin Cui657b1f92015-01-22 19:26:12 -0800739// args is used to pass in all command arguments, and pass out only the part of options for gtest.
740// options is used to pass out test options in isolation mode.
741// Return false if there is error in arguments.
742static bool PickOptions(std::vector<char*>& args, IsolationTestOptions& options) {
743 for (size_t i = 1; i < args.size(); ++i) {
744 if (strcmp(args[i], "--help") == 0 || strcmp(args[i], "-h") == 0) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800745 PrintHelpInfo();
Yabin Cui657b1f92015-01-22 19:26:12 -0800746 options.isolate = false;
Yabin Cui294d1e22014-12-07 20:43:37 -0800747 return true;
748 }
749 }
750
Yabin Cui657b1f92015-01-22 19:26:12 -0800751 // if --bionic-selftest argument is used, only enable self tests, otherwise remove self tests.
752 bool enable_selftest = false;
753 for (size_t i = 1; i < args.size(); ++i) {
754 if (strcmp(args[i], "--bionic-selftest") == 0) {
755 // This argument is to enable "bionic_selftest*" for self test, and is not shown in help info.
756 // Don't remove this option from arguments.
757 enable_selftest = true;
758 }
759 }
760 std::string gtest_filter_str;
761 for (size_t i = args.size() - 1; i >= 1; --i) {
762 if (strncmp(args[i], "--gtest_filter=", strlen("--gtest_filter=")) == 0) {
763 gtest_filter_str = std::string(args[i]);
764 args.erase(args.begin() + i);
Yabin Cui294d1e22014-12-07 20:43:37 -0800765 break;
766 }
767 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800768 if (enable_selftest == true) {
769 args.push_back(strdup("--gtest_filter=bionic_selftest*"));
770 } else {
771 if (gtest_filter_str == "") {
772 gtest_filter_str = "--gtest_filter=-bionic_selftest*";
773 } else {
774 gtest_filter_str += ":-bionic_selftest*";
775 }
776 args.push_back(strdup(gtest_filter_str.c_str()));
777 }
Yabin Cui294d1e22014-12-07 20:43:37 -0800778
Yabin Cui657b1f92015-01-22 19:26:12 -0800779 options.isolate = true;
780 // Parse arguments that make us can't run in isolation mode.
781 for (size_t i = 1; i < args.size(); ++i) {
782 if (strcmp(args[i], "--no-isolate") == 0) {
783 options.isolate = false;
784 } else if (strcmp(args[i], "--gtest_list_tests") == 0) {
785 options.isolate = false;
Yabin Cui294d1e22014-12-07 20:43:37 -0800786 }
787 }
788
Yabin Cui657b1f92015-01-22 19:26:12 -0800789 // Stop parsing if we will not run in isolation mode.
790 if (options.isolate == false) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800791 return true;
792 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800793
794 // Init default isolation test options.
795 options.job_count = GetProcessorCount();
796 options.test_deadline_ms = DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS;
797 options.test_warnline_ms = DEFAULT_GLOBAL_TEST_RUN_WARNLINE_MS;
798 options.gtest_color = testing::GTEST_FLAG(color);
799 options.gtest_print_time = testing::GTEST_FLAG(print_time);
800 options.gtest_repeat = testing::GTEST_FLAG(repeat);
801 options.gtest_output = testing::GTEST_FLAG(output);
802
803 // Parse arguments speficied for isolation mode.
804 for (size_t i = 1; i < args.size(); ++i) {
805 if (strncmp(args[i], "-j", strlen("-j")) == 0) {
806 char* p = args[i] + strlen("-j");
807 int count = 0;
808 if (*p != '\0') {
809 // Argument like -j5.
810 count = atoi(p);
811 } else if (args.size() > i + 1) {
812 // Arguments like -j 5.
813 count = atoi(args[i + 1]);
814 ++i;
815 }
816 if (count <= 0) {
817 fprintf(stderr, "invalid job count: %d\n", count);
818 return false;
819 }
820 options.job_count = static_cast<size_t>(count);
821 } else if (strncmp(args[i], "--deadline=", strlen("--deadline=")) == 0) {
822 int time_ms = atoi(args[i] + strlen("--deadline="));
823 if (time_ms <= 0) {
824 fprintf(stderr, "invalid deadline: %d\n", time_ms);
825 return false;
826 }
827 options.test_deadline_ms = time_ms;
828 } else if (strncmp(args[i], "--warnline=", strlen("--warnline=")) == 0) {
829 int time_ms = atoi(args[i] + strlen("--warnline="));
830 if (time_ms <= 0) {
831 fprintf(stderr, "invalid warnline: %d\n", time_ms);
832 return false;
833 }
834 options.test_warnline_ms = time_ms;
835 } else if (strncmp(args[i], "--gtest_color=", strlen("--gtest_color=")) == 0) {
836 options.gtest_color = args[i] + strlen("--gtest_color=");
837 } else if (strcmp(args[i], "--gtest_print_time=0") == 0) {
838 options.gtest_print_time = false;
839 } else if (strncmp(args[i], "--gtest_repeat=", strlen("--gtest_repeat=")) == 0) {
840 int repeat = atoi(args[i] + strlen("--gtest_repeat="));
841 if (repeat < 0) {
842 fprintf(stderr, "invalid gtest_repeat count: %d\n", repeat);
843 return false;
844 }
845 options.gtest_repeat = repeat;
846 // Remove --gtest_repeat=xx from arguments, so child process only run one iteration for a single test.
847 args.erase(args.begin() + i);
848 --i;
849 } else if (strncmp(args[i], "--gtest_output=", strlen("--gtest_output=")) == 0) {
850 std::string output = args[i] + strlen("--gtest_output=");
851 // generate output xml file path according to the strategy in gtest.
852 bool success = true;
853 if (strncmp(output.c_str(), "xml:", strlen("xml:")) == 0) {
854 output = output.substr(strlen("xml:"));
855 if (output.size() == 0) {
856 success = false;
857 }
858 // Make absolute path.
859 if (success && output[0] != '/') {
860 char* cwd = getcwd(NULL, 0);
861 if (cwd != NULL) {
862 output = std::string(cwd) + "/" + output;
863 free(cwd);
864 } else {
865 success = false;
866 }
867 }
868 // Add file name if output is a directory.
869 if (success && output.back() == '/') {
870 output += "test_details.xml";
871 }
872 }
873 if (success) {
874 options.gtest_output = output;
875 } else {
876 fprintf(stderr, "invalid gtest_output file: %s\n", args[i]);
877 return false;
878 }
879
880 // Remove --gtest_output=xxx from arguments, so child process will not write xml file.
881 args.erase(args.begin() + i);
882 --i;
883 }
884 }
885
886 // Add --no-isolate in args to prevent child process from running in isolation mode again.
887 // As DeathTest will try to call execve(), this argument should always be added.
888 args.insert(args.begin() + 1, strdup("--no-isolate"));
Yabin Cui294d1e22014-12-07 20:43:37 -0800889 return true;
890}
891
892int main(int argc, char** argv) {
Yabin Cuibe837362015-01-02 18:45:37 -0800893 std::vector<char*> arg_list;
894 for (int i = 0; i < argc; ++i) {
895 arg_list.push_back(argv[i]);
896 }
Yabin Cuibe837362015-01-02 18:45:37 -0800897
Yabin Cui657b1f92015-01-22 19:26:12 -0800898 IsolationTestOptions options;
899 if (PickOptions(arg_list, options) == false) {
900 return 1;
Yabin Cui294d1e22014-12-07 20:43:37 -0800901 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800902
903 if (options.isolate == true) {
904 // Set global variables.
905 global_test_run_deadline_ms = options.test_deadline_ms;
906 global_test_run_warnline_ms = options.test_warnline_ms;
907 testing::GTEST_FLAG(color) = options.gtest_color.c_str();
908 testing::GTEST_FLAG(print_time) = options.gtest_print_time;
909 std::vector<TestCase> testcase_list;
910
911 argc = static_cast<int>(arg_list.size());
912 arg_list.push_back(NULL);
913 if (EnumerateTests(argc, arg_list.data(), testcase_list) == false) {
914 return 1;
915 }
916 RunTestInSeparateProc(argc, arg_list.data(), testcase_list, options.gtest_repeat,
917 options.job_count, options.gtest_output);
918 } else {
919 argc = static_cast<int>(arg_list.size());
920 arg_list.push_back(NULL);
921 testing::InitGoogleTest(&argc, arg_list.data());
922 return RUN_ALL_TESTS();
923 }
924 return 0;
Yabin Cui294d1e22014-12-07 20:43:37 -0800925}
926
927//################################################################################
Yabin Cuibe837362015-01-02 18:45:37 -0800928// Bionic Gtest self test, run this by --bionic-selftest option.
Yabin Cui294d1e22014-12-07 20:43:37 -0800929
Yabin Cuibe837362015-01-02 18:45:37 -0800930TEST(bionic_selftest, test_success) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800931 ASSERT_EQ(1, 1);
932}
933
Yabin Cuibe837362015-01-02 18:45:37 -0800934TEST(bionic_selftest, test_fail) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800935 ASSERT_EQ(0, 1);
936}
937
Yabin Cuibe837362015-01-02 18:45:37 -0800938TEST(bionic_selftest, test_time_warn) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800939 sleep(4);
940}
941
Yabin Cuibe837362015-01-02 18:45:37 -0800942TEST(bionic_selftest, test_timeout) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800943 while (1) {}
944}
Yabin Cuibe837362015-01-02 18:45:37 -0800945
946TEST(bionic_selftest, test_signal_SEGV_terminated) {
947 char* p = reinterpret_cast<char*>(static_cast<intptr_t>(atoi("0")));
948 *p = 3;
949}
Yabin Cui657b1f92015-01-22 19:26:12 -0800950
951class bionic_selftest_DeathTest : public BionicDeathTest {};
952
953static void deathtest_helper_success() {
954 ASSERT_EQ(1, 1);
955 exit(0);
956}
957
958TEST_F(bionic_selftest_DeathTest, success) {
959 ASSERT_EXIT(deathtest_helper_success(), ::testing::ExitedWithCode(0), "");
960}
961
962static void deathtest_helper_fail() {
963 ASSERT_EQ(1, 0);
964}
965
966TEST_F(bionic_selftest_DeathTest, fail) {
967 ASSERT_EXIT(deathtest_helper_fail(), ::testing::ExitedWithCode(0), "");
968}