blob: e199449c5fb047653b3b9480b676ba194cfec603 [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"
Yabin Cui11c43532015-01-28 14:28:14 -080094 " --gtest-filter=POSITIVE_PATTERNS[-NEGATIVE_PATTERNS]\n"
95 " Used as a synonym for --gtest_filter option in gtest.\n"
Yabin Cuibe837362015-01-02 18:45:37 -080096 "\nDefault bionic unit test option is -j.\n"
97 "\n");
98}
99
Yabin Cui294d1e22014-12-07 20:43:37 -0800100enum TestResult {
101 TEST_SUCCESS = 0,
102 TEST_FAILED,
103 TEST_TIMEOUT
104};
105
Yabin Cui657b1f92015-01-22 19:26:12 -0800106class Test {
107 public:
108 Test() {} // For std::vector<Test>.
109 explicit Test(const char* name) : name_(name) {}
110
111 const std::string& GetName() const { return name_; }
112
113 void SetResult(TestResult result) { result_ = result; }
114
115 TestResult GetResult() const { return result_; }
116
117 void SetTestTime(int64_t elapsed_time_ns) { elapsed_time_ns_ = elapsed_time_ns; }
118
119 int64_t GetTestTime() const { return elapsed_time_ns_; }
120
121 void AppendFailureMessage(const std::string& s) { failure_message_ += s; }
122
123 const std::string& GetFailureMessage() const { return failure_message_; }
124
125 private:
126 const std::string name_;
127 TestResult result_;
128 int64_t elapsed_time_ns_;
129 std::string failure_message_;
130};
131
Yabin Cui294d1e22014-12-07 20:43:37 -0800132class TestCase {
133 public:
134 TestCase() {} // For std::vector<TestCase>.
135 explicit TestCase(const char* name) : name_(name) {}
136
137 const std::string& GetName() const { return name_; }
138
Yabin Cui657b1f92015-01-22 19:26:12 -0800139 void AppendTest(const char* test_name) {
140 test_list_.push_back(Test(test_name));
Yabin Cui294d1e22014-12-07 20:43:37 -0800141 }
142
Yabin Cuibe837362015-01-02 18:45:37 -0800143 size_t TestCount() const { return test_list_.size(); }
Yabin Cui294d1e22014-12-07 20:43:37 -0800144
Yabin Cuibe837362015-01-02 18:45:37 -0800145 std::string GetTestName(size_t test_id) const {
Yabin Cui294d1e22014-12-07 20:43:37 -0800146 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800147 return name_ + "." + test_list_[test_id].GetName();
148 }
149
150 Test& GetTest(size_t test_id) {
151 VerifyTestId(test_id);
152 return test_list_[test_id];
153 }
154
155 const Test& GetTest(size_t test_id) const {
156 VerifyTestId(test_id);
157 return test_list_[test_id];
Yabin Cui294d1e22014-12-07 20:43:37 -0800158 }
159
Yabin Cuibe837362015-01-02 18:45:37 -0800160 void SetTestResult(size_t test_id, TestResult result) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800161 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800162 test_list_[test_id].SetResult(result);
Yabin Cui294d1e22014-12-07 20:43:37 -0800163 }
164
Yabin Cuibe837362015-01-02 18:45:37 -0800165 TestResult GetTestResult(size_t test_id) const {
Yabin Cui294d1e22014-12-07 20:43:37 -0800166 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800167 return test_list_[test_id].GetResult();
Yabin Cui294d1e22014-12-07 20:43:37 -0800168 }
169
Yabin Cui657b1f92015-01-22 19:26:12 -0800170 void SetTestTime(size_t test_id, int64_t elapsed_time_ns) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800171 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800172 test_list_[test_id].SetTestTime(elapsed_time_ns);
Yabin Cui294d1e22014-12-07 20:43:37 -0800173 }
174
Yabin Cuibe837362015-01-02 18:45:37 -0800175 int64_t GetTestTime(size_t test_id) const {
Yabin Cui294d1e22014-12-07 20:43:37 -0800176 VerifyTestId(test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800177 return test_list_[test_id].GetTestTime();
Yabin Cui294d1e22014-12-07 20:43:37 -0800178 }
179
180 private:
Yabin Cuibe837362015-01-02 18:45:37 -0800181 void VerifyTestId(size_t test_id) const {
182 if(test_id >= test_list_.size()) {
183 fprintf(stderr, "test_id %zu out of range [0, %zu)\n", test_id, test_list_.size());
Yabin Cui294d1e22014-12-07 20:43:37 -0800184 exit(1);
185 }
186 }
187
188 private:
189 const std::string name_;
Yabin Cui657b1f92015-01-22 19:26:12 -0800190 std::vector<Test> test_list_;
Yabin Cui294d1e22014-12-07 20:43:37 -0800191};
192
Yabin Cui657b1f92015-01-22 19:26:12 -0800193// This is the file descriptor used by the child process to write failure message.
194// The parent process will collect the information and dump to stdout / xml file.
195static int child_output_fd;
196
Yabin Cui294d1e22014-12-07 20:43:37 -0800197class TestResultPrinter : public testing::EmptyTestEventListener {
198 public:
199 TestResultPrinter() : pinfo_(NULL) {}
200 virtual void OnTestStart(const testing::TestInfo& test_info) {
201 pinfo_ = &test_info; // Record test_info for use in OnTestPartResult.
202 }
203 virtual void OnTestPartResult(const testing::TestPartResult& result);
Yabin Cui294d1e22014-12-07 20:43:37 -0800204
205 private:
206 const testing::TestInfo* pinfo_;
207};
208
209// Called after an assertion failure.
210void TestResultPrinter::OnTestPartResult(const testing::TestPartResult& result) {
211 // If the test part succeeded, we don't need to do anything.
212 if (result.type() == testing::TestPartResult::kSuccess)
213 return;
214
215 // Print failure message from the assertion (e.g. expected this and got that).
216 char buf[1024];
217 snprintf(buf, sizeof(buf), "%s:(%d) Failure in test %s.%s\n%s\n", result.file_name(),
218 result.line_number(),
219 pinfo_->test_case_name(),
220 pinfo_->name(),
221 result.message());
222
Yabin Cui294d1e22014-12-07 20:43:37 -0800223 int towrite = strlen(buf);
224 char* p = buf;
225 while (towrite > 0) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800226 ssize_t write_count = TEMP_FAILURE_RETRY(write(child_output_fd, p, towrite));
Yabin Cuibe837362015-01-02 18:45:37 -0800227 if (write_count == -1) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800228 fprintf(stderr, "failed to write child_output_fd: %s\n", strerror(errno));
229 exit(1);
Yabin Cui294d1e22014-12-07 20:43:37 -0800230 } else {
Yabin Cuibe837362015-01-02 18:45:37 -0800231 towrite -= write_count;
232 p += write_count;
Yabin Cui294d1e22014-12-07 20:43:37 -0800233 }
234 }
235}
236
Yabin Cui294d1e22014-12-07 20:43:37 -0800237static int64_t NanoTime() {
238 struct timespec t;
239 t.tv_sec = t.tv_nsec = 0;
240 clock_gettime(CLOCK_MONOTONIC, &t);
241 return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
242}
243
244static bool EnumerateTests(int argc, char** argv, std::vector<TestCase>& testcase_list) {
245 std::string command;
246 for (int i = 0; i < argc; ++i) {
247 command += argv[i];
248 command += " ";
249 }
250 command += "--gtest_list_tests";
251 FILE* fp = popen(command.c_str(), "r");
252 if (fp == NULL) {
253 perror("popen");
254 return false;
255 }
256
257 char buf[200];
258 while (fgets(buf, sizeof(buf), fp) != NULL) {
259 char* p = buf;
260
261 while (*p != '\0' && isspace(*p)) {
262 ++p;
263 }
264 if (*p == '\0') continue;
265 char* start = p;
266 while (*p != '\0' && !isspace(*p)) {
267 ++p;
268 }
269 char* end = p;
270 while (*p != '\0' && isspace(*p)) {
271 ++p;
272 }
273 if (*p != '\0') {
274 // This is not we want, gtest must meet with some error when parsing the arguments.
275 fprintf(stderr, "argument error, check with --help\n");
276 return false;
277 }
278 *end = '\0';
279 if (*(end - 1) == '.') {
280 *(end - 1) = '\0';
281 testcase_list.push_back(TestCase(start));
282 } else {
283 testcase_list.back().AppendTest(start);
284 }
285 }
286 int result = pclose(fp);
287 return (result != -1 && WEXITSTATUS(result) == 0);
288}
289
Yabin Cui294d1e22014-12-07 20:43:37 -0800290// Part of the following *Print functions are copied from external/gtest/src/gtest.cc:
291// PrettyUnitTestResultPrinter. The reason for copy is that PrettyUnitTestResultPrinter
292// is defined and used in gtest.cc, which is hard to reuse.
Yabin Cuibe837362015-01-02 18:45:37 -0800293static void OnTestIterationStartPrint(const std::vector<TestCase>& testcase_list, size_t iteration,
294 size_t iteration_count) {
295 if (iteration_count > 1) {
296 printf("\nRepeating all tests (iteration %zu) . . .\n\n", iteration);
Yabin Cui294d1e22014-12-07 20:43:37 -0800297 }
298 ColoredPrintf(COLOR_GREEN, "[==========] ");
299
Yabin Cuibe837362015-01-02 18:45:37 -0800300 size_t testcase_count = testcase_list.size();
301 size_t test_count = 0;
Yabin Cui294d1e22014-12-07 20:43:37 -0800302 for (const auto& testcase : testcase_list) {
Yabin Cuibe837362015-01-02 18:45:37 -0800303 test_count += testcase.TestCount();
Yabin Cui294d1e22014-12-07 20:43:37 -0800304 }
305
Yabin Cuibe837362015-01-02 18:45:37 -0800306 printf("Running %zu %s from %zu %s.\n",
307 test_count, (test_count == 1) ? "test" : "tests",
308 testcase_count, (testcase_count == 1) ? "test case" : "test cases");
Yabin Cui294d1e22014-12-07 20:43:37 -0800309 fflush(stdout);
310}
311
Yabin Cui657b1f92015-01-22 19:26:12 -0800312static void OnTestEndPrint(const TestCase& testcase, size_t test_id) {
313 TestResult result = testcase.GetTestResult(test_id);
314 if (result == TEST_SUCCESS) {
315 ColoredPrintf(COLOR_GREEN, "[ OK ] ");
316 } else if (result == TEST_FAILED) {
317 ColoredPrintf(COLOR_RED, "[ FAILED ] ");
318 } else if (result == TEST_TIMEOUT) {
319 ColoredPrintf(COLOR_RED, "[ TIMEOUT ] ");
320 }
Yabin Cuibe837362015-01-02 18:45:37 -0800321
Yabin Cui657b1f92015-01-22 19:26:12 -0800322 printf("%s", testcase.GetTestName(test_id).c_str());
323 if (testing::GTEST_FLAG(print_time)) {
324 printf(" (%" PRId64 " ms)\n", testcase.GetTestTime(test_id) / 1000000);
325 } else {
326 printf("\n");
327 }
328
329 const std::string& failure_message = testcase.GetTest(test_id).GetFailureMessage();
330 printf("%s", failure_message.c_str());
Yabin Cui294d1e22014-12-07 20:43:37 -0800331 fflush(stdout);
332}
333
Yabin Cuibe837362015-01-02 18:45:37 -0800334static void OnTestIterationEndPrint(const std::vector<TestCase>& testcase_list, size_t /*iteration*/,
Yabin Cui657b1f92015-01-22 19:26:12 -0800335 int64_t elapsed_time_ns) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800336
337 std::vector<std::string> fail_test_name_list;
338 std::vector<std::pair<std::string, int64_t>> timeout_test_list;
339
340 // For tests run exceed warnline but not timeout.
Yabin Cui4a82ede2015-01-26 17:19:37 -0800341 std::vector<std::tuple<std::string, int64_t, int>> slow_test_list;
Yabin Cuibe837362015-01-02 18:45:37 -0800342 size_t testcase_count = testcase_list.size();
343 size_t test_count = 0;
344 size_t success_test_count = 0;
Yabin Cui294d1e22014-12-07 20:43:37 -0800345
346 for (const auto& testcase : testcase_list) {
Yabin Cuibe837362015-01-02 18:45:37 -0800347 test_count += testcase.TestCount();
348 for (size_t i = 0; i < testcase.TestCount(); ++i) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800349 TestResult result = testcase.GetTestResult(i);
350 if (result == TEST_SUCCESS) {
Yabin Cuibe837362015-01-02 18:45:37 -0800351 ++success_test_count;
Yabin Cui294d1e22014-12-07 20:43:37 -0800352 } else if (result == TEST_FAILED) {
353 fail_test_name_list.push_back(testcase.GetTestName(i));
354 } else if (result == TEST_TIMEOUT) {
355 timeout_test_list.push_back(std::make_pair(testcase.GetTestName(i),
356 testcase.GetTestTime(i)));
357 }
358 if (result != TEST_TIMEOUT &&
359 testcase.GetTestTime(i) / 1000000 >= GetWarnlineInfo(testcase.GetTestName(i))) {
Yabin Cui4a82ede2015-01-26 17:19:37 -0800360 slow_test_list.push_back(std::make_tuple(testcase.GetTestName(i),
361 testcase.GetTestTime(i),
362 GetWarnlineInfo(testcase.GetTestName(i))));
Yabin Cui294d1e22014-12-07 20:43:37 -0800363 }
364 }
365 }
366
Yabin Cui294d1e22014-12-07 20:43:37 -0800367 ColoredPrintf(COLOR_GREEN, "[==========] ");
Yabin Cuibe837362015-01-02 18:45:37 -0800368 printf("%zu %s from %zu %s ran.", test_count, (test_count == 1) ? "test" : "tests",
369 testcase_count, (testcase_count == 1) ? "test case" : "test cases");
Yabin Cui294d1e22014-12-07 20:43:37 -0800370 if (testing::GTEST_FLAG(print_time)) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800371 printf(" (%" PRId64 " ms total)", elapsed_time_ns / 1000000);
Yabin Cui294d1e22014-12-07 20:43:37 -0800372 }
373 printf("\n");
Yabin Cui4a82ede2015-01-26 17:19:37 -0800374 ColoredPrintf(COLOR_GREEN, "[ PASS ] ");
Yabin Cuibe837362015-01-02 18:45:37 -0800375 printf("%zu %s.\n", success_test_count, (success_test_count == 1) ? "test" : "tests");
Yabin Cui294d1e22014-12-07 20:43:37 -0800376
377 // Print tests failed.
Yabin Cuibe837362015-01-02 18:45:37 -0800378 size_t fail_test_count = fail_test_name_list.size();
379 if (fail_test_count > 0) {
Yabin Cui4a82ede2015-01-26 17:19:37 -0800380 ColoredPrintf(COLOR_RED, "[ FAIL ] ");
Yabin Cuibe837362015-01-02 18:45:37 -0800381 printf("%zu %s, listed below:\n", fail_test_count, (fail_test_count == 1) ? "test" : "tests");
Yabin Cui294d1e22014-12-07 20:43:37 -0800382 for (const auto& name : fail_test_name_list) {
Yabin Cui4a82ede2015-01-26 17:19:37 -0800383 ColoredPrintf(COLOR_RED, "[ FAIL ] ");
Yabin Cui294d1e22014-12-07 20:43:37 -0800384 printf("%s\n", name.c_str());
385 }
386 }
387
388 // Print tests run timeout.
Yabin Cuibe837362015-01-02 18:45:37 -0800389 size_t timeout_test_count = timeout_test_list.size();
390 if (timeout_test_count > 0) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800391 ColoredPrintf(COLOR_RED, "[ TIMEOUT ] ");
Yabin Cuibe837362015-01-02 18:45:37 -0800392 printf("%zu %s, listed below:\n", timeout_test_count, (timeout_test_count == 1) ? "test" : "tests");
Yabin Cui294d1e22014-12-07 20:43:37 -0800393 for (const auto& timeout_pair : timeout_test_list) {
394 ColoredPrintf(COLOR_RED, "[ TIMEOUT ] ");
Yabin Cui657b1f92015-01-22 19:26:12 -0800395 printf("%s (stopped at %" PRId64 " ms)\n", timeout_pair.first.c_str(),
396 timeout_pair.second / 1000000);
Yabin Cui294d1e22014-12-07 20:43:37 -0800397 }
398 }
399
400 // Print tests run exceed warnline.
Yabin Cui4a82ede2015-01-26 17:19:37 -0800401 size_t slow_test_count = slow_test_list.size();
402 if (slow_test_count > 0) {
403 ColoredPrintf(COLOR_YELLOW, "[ SLOW ] ");
404 printf("%zu %s, listed below:\n", slow_test_count, (slow_test_count == 1) ? "test" : "tests");
405 for (const auto& slow_tuple : slow_test_list) {
406 ColoredPrintf(COLOR_YELLOW, "[ SLOW ] ");
407 printf("%s (%" PRId64 " ms, exceed warnline %d ms)\n", std::get<0>(slow_tuple).c_str(),
408 std::get<1>(slow_tuple) / 1000000, std::get<2>(slow_tuple));
Yabin Cui294d1e22014-12-07 20:43:37 -0800409 }
410 }
411
Yabin Cuibe837362015-01-02 18:45:37 -0800412 if (fail_test_count > 0) {
413 printf("\n%2zu FAILED %s\n", fail_test_count, (fail_test_count == 1) ? "TEST" : "TESTS");
Yabin Cui294d1e22014-12-07 20:43:37 -0800414 }
Yabin Cuibe837362015-01-02 18:45:37 -0800415 if (timeout_test_count > 0) {
416 printf("%2zu TIMEOUT %s\n", timeout_test_count, (timeout_test_count == 1) ? "TEST" : "TESTS");
Yabin Cui294d1e22014-12-07 20:43:37 -0800417 }
Yabin Cui4a82ede2015-01-26 17:19:37 -0800418 if (slow_test_count > 0) {
419 printf("%2zu SLOW %s\n", slow_test_count, (slow_test_count == 1) ? "TEST" : "TESTS");
Yabin Cui294d1e22014-12-07 20:43:37 -0800420 }
421 fflush(stdout);
422}
423
Yabin Cui657b1f92015-01-22 19:26:12 -0800424// Output xml file when --gtest_output is used, write this function as we can't reuse
425// gtest.cc:XmlUnitTestResultPrinter. The reason is XmlUnitTestResultPrinter is totally
426// defined in gtest.cc and not expose to outside. What's more, as we don't run gtest in
427// the parent process, we don't have gtest classes which are needed by XmlUnitTestResultPrinter.
428void OnTestIterationEndXmlPrint(const std::string& xml_output_filename,
429 const std::vector<TestCase>& testcase_list,
430 time_t epoch_iteration_start_time,
431 int64_t elapsed_time_ns) {
432 FILE* fp = fopen(xml_output_filename.c_str(), "w");
433 if (fp == NULL) {
434 fprintf(stderr, "failed to open '%s': %s\n", xml_output_filename.c_str(), strerror(errno));
435 exit(1);
436 }
437
438 size_t total_test_count = 0;
439 size_t total_failed_count = 0;
440 std::vector<size_t> failed_count_list(testcase_list.size(), 0);
441 std::vector<int64_t> elapsed_time_list(testcase_list.size(), 0);
442 for (size_t i = 0; i < testcase_list.size(); ++i) {
443 auto& testcase = testcase_list[i];
444 total_test_count += testcase.TestCount();
445 for (size_t j = 0; j < testcase.TestCount(); ++j) {
446 if (testcase.GetTestResult(j) != TEST_SUCCESS) {
447 ++failed_count_list[i];
448 }
449 elapsed_time_list[i] += testcase.GetTestTime(j);
450 }
451 total_failed_count += failed_count_list[i];
452 }
453
454 const tm* time_struct = localtime(&epoch_iteration_start_time);
455 char timestamp[40];
456 snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d",
457 time_struct->tm_year + 1900, time_struct->tm_mon + 1, time_struct->tm_mday,
458 time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);
459
460 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
461 fprintf(fp, "<testsuites tests=\"%zu\" failures=\"%zu\" disabled=\"0\" errors=\"0\"",
462 total_test_count, total_failed_count);
463 fprintf(fp, " timestamp=\"%s\" time=\"%.3lf\" name=\"AllTests\">\n", timestamp, elapsed_time_ns / 1e9);
464 for (size_t i = 0; i < testcase_list.size(); ++i) {
465 auto& testcase = testcase_list[i];
466 fprintf(fp, " <testsuite name=\"%s\" tests=\"%zu\" failures=\"%zu\" disabled=\"0\" errors=\"0\"",
467 testcase.GetName().c_str(), testcase.TestCount(), failed_count_list[i]);
468 fprintf(fp, " time=\"%.3lf\">\n", elapsed_time_list[i] / 1e9);
469
470 for (size_t j = 0; j < testcase.TestCount(); ++j) {
471 fprintf(fp, " <testcase name=\"%s\" status=\"run\" time=\"%.3lf\" classname=\"%s\"",
472 testcase.GetTest(j).GetName().c_str(), testcase.GetTestTime(j) / 1e9,
473 testcase.GetName().c_str());
474 if (testcase.GetTestResult(j) == TEST_SUCCESS) {
475 fputs(" />\n", fp);
476 } else {
477 fputs(">\n", fp);
478 const std::string& failure_message = testcase.GetTest(j).GetFailureMessage();
479 fprintf(fp, " <failure message=\"%s\" type=\"\">\n", failure_message.c_str());
480 fputs(" </failure>\n", fp);
481 fputs(" </testcase>\n", fp);
482 }
483 }
484
485 fputs(" </testsuite>\n", fp);
486 }
487 fputs("</testsuites>\n", fp);
488 fclose(fp);
489}
490
Yabin Cui294d1e22014-12-07 20:43:37 -0800491// Forked Child process, run the single test.
492static void ChildProcessFn(int argc, char** argv, const std::string& test_name) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800493 char** new_argv = new char*[argc + 2];
Yabin Cui294d1e22014-12-07 20:43:37 -0800494 memcpy(new_argv, argv, sizeof(char*) * argc);
495
496 char* filter_arg = new char [test_name.size() + 20];
497 strcpy(filter_arg, "--gtest_filter=");
498 strcat(filter_arg, test_name.c_str());
499 new_argv[argc] = filter_arg;
Yabin Cui657b1f92015-01-22 19:26:12 -0800500 new_argv[argc + 1] = NULL;
Yabin Cui294d1e22014-12-07 20:43:37 -0800501
502 int new_argc = argc + 1;
503 testing::InitGoogleTest(&new_argc, new_argv);
504 int result = RUN_ALL_TESTS();
505 exit(result);
506}
507
508struct ChildProcInfo {
509 pid_t pid;
Yabin Cui657b1f92015-01-22 19:26:12 -0800510 int64_t start_time_ns;
511 int64_t deadline_time_ns;
Yabin Cuibe837362015-01-02 18:45:37 -0800512 size_t testcase_id, test_id;
Yabin Cui294d1e22014-12-07 20:43:37 -0800513 bool done_flag;
Yabin Cuibe837362015-01-02 18:45:37 -0800514 bool timeout_flag;
515 int exit_status;
Yabin Cui657b1f92015-01-22 19:26:12 -0800516 int child_read_fd;
Yabin Cui294d1e22014-12-07 20:43:37 -0800517 ChildProcInfo() : pid(0) {}
518};
519
520static void WaitChildProcs(std::vector<ChildProcInfo>& child_proc_list) {
521 pid_t result;
Yabin Cuibe837362015-01-02 18:45:37 -0800522 int status;
Yabin Cui294d1e22014-12-07 20:43:37 -0800523 bool loop_flag = true;
524
525 while (true) {
Yabin Cuibe837362015-01-02 18:45:37 -0800526 while ((result = waitpid(-1, &status, WNOHANG)) == -1) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800527 if (errno != EINTR) {
528 break;
529 }
530 }
531
532 if (result == -1) {
533 perror("waitpid");
534 exit(1);
535 } else if (result == 0) {
536 // Check child timeout.
Yabin Cui657b1f92015-01-22 19:26:12 -0800537 int64_t current_time_ns = NanoTime();
Yabin Cui294d1e22014-12-07 20:43:37 -0800538 for (size_t i = 0; i < child_proc_list.size(); ++i) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800539 if (child_proc_list[i].deadline_time_ns <= current_time_ns) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800540 child_proc_list[i].done_flag = true;
Yabin Cuibe837362015-01-02 18:45:37 -0800541 child_proc_list[i].timeout_flag = true;
Yabin Cui294d1e22014-12-07 20:43:37 -0800542 loop_flag = false;
543 }
544 }
545 } else {
546 // Check child finish.
547 for (size_t i = 0; i < child_proc_list.size(); ++i) {
548 if (child_proc_list[i].pid == result) {
549 child_proc_list[i].done_flag = true;
Yabin Cuibe837362015-01-02 18:45:37 -0800550 child_proc_list[i].timeout_flag = false;
551 child_proc_list[i].exit_status = status;
Yabin Cui294d1e22014-12-07 20:43:37 -0800552 loop_flag = false;
553 break;
554 }
555 }
556 }
557
558 if (!loop_flag) break;
559 // sleep 1 ms to avoid busy looping.
560 timespec sleep_time;
561 sleep_time.tv_sec = 0;
562 sleep_time.tv_nsec = 1000000;
563 nanosleep(&sleep_time, NULL);
564 }
565}
566
567static TestResult WaitChildProc(pid_t pid) {
568 pid_t result;
569 int exit_status;
570
571 while ((result = waitpid(pid, &exit_status, 0)) == -1) {
572 if (errno != EINTR) {
573 break;
574 }
575 }
576
577 TestResult test_result = TEST_SUCCESS;
578 if (result != pid || WEXITSTATUS(exit_status) != 0) {
579 test_result = TEST_FAILED;
580 }
581 return test_result;
582}
583
584// We choose to use multi-fork and multi-wait here instead of multi-thread, because it always
585// makes deadlock to use fork in multi-thread.
586static void RunTestInSeparateProc(int argc, char** argv, std::vector<TestCase>& testcase_list,
Yabin Cui657b1f92015-01-22 19:26:12 -0800587 size_t iteration_count, size_t job_count,
588 const std::string& xml_output_filename) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800589 // Stop default result printer to avoid environment setup/teardown information for each test.
590 testing::UnitTest::GetInstance()->listeners().Release(
591 testing::UnitTest::GetInstance()->listeners().default_result_printer());
592 testing::UnitTest::GetInstance()->listeners().Append(new TestResultPrinter);
593
Yabin Cuibe837362015-01-02 18:45:37 -0800594 for (size_t iteration = 1; iteration <= iteration_count; ++iteration) {
595 OnTestIterationStartPrint(testcase_list, iteration, iteration_count);
Yabin Cui657b1f92015-01-22 19:26:12 -0800596 int64_t iteration_start_time_ns = NanoTime();
597 time_t epoch_iteration_start_time = time(NULL);
Yabin Cui294d1e22014-12-07 20:43:37 -0800598
Yabin Cuibe837362015-01-02 18:45:37 -0800599 // Run up to job_count tests in parallel, each test in a child process.
600 std::vector<ChildProcInfo> child_proc_list(job_count);
Yabin Cui294d1e22014-12-07 20:43:37 -0800601
Yabin Cuibe837362015-01-02 18:45:37 -0800602 // Next test to run is [next_testcase_id:next_test_id].
603 size_t next_testcase_id = 0;
604 size_t next_test_id = 0;
605
606 // Record how many tests are finished.
607 std::vector<size_t> finished_test_count_list(testcase_list.size(), 0);
608 size_t finished_testcase_count = 0;
609
610 while (finished_testcase_count < testcase_list.size()) {
611 // Fork up to job_count child processes.
Yabin Cui294d1e22014-12-07 20:43:37 -0800612 for (auto& child_proc : child_proc_list) {
Yabin Cuibe837362015-01-02 18:45:37 -0800613 if (child_proc.pid == 0 && next_testcase_id < testcase_list.size()) {
614 std::string test_name = testcase_list[next_testcase_id].GetTestName(next_test_id);
Yabin Cui657b1f92015-01-22 19:26:12 -0800615 int pipefd[2];
616 int ret = pipe(pipefd);
617 if (ret == -1) {
618 perror("pipe2 in RunTestInSeparateProc");
619 exit(1);
620 }
Yabin Cui294d1e22014-12-07 20:43:37 -0800621 pid_t pid = fork();
622 if (pid == -1) {
623 perror("fork in RunTestInSeparateProc");
624 exit(1);
625 } else if (pid == 0) {
Yabin Cui657b1f92015-01-22 19:26:12 -0800626 close(pipefd[0]);
627 child_output_fd = pipefd[1];
Yabin Cui294d1e22014-12-07 20:43:37 -0800628 // Run child process test, never return.
629 ChildProcessFn(argc, argv, test_name);
630 }
631 // Parent process
Yabin Cui657b1f92015-01-22 19:26:12 -0800632 close(pipefd[1]);
633 child_proc.child_read_fd = pipefd[0];
Yabin Cui294d1e22014-12-07 20:43:37 -0800634 child_proc.pid = pid;
Yabin Cui657b1f92015-01-22 19:26:12 -0800635 child_proc.start_time_ns = NanoTime();
636 child_proc.deadline_time_ns = child_proc.start_time_ns +
637 GetDeadlineInfo(test_name) * 1000000LL;
Yabin Cuibe837362015-01-02 18:45:37 -0800638 child_proc.testcase_id = next_testcase_id;
639 child_proc.test_id = next_test_id;
Yabin Cui294d1e22014-12-07 20:43:37 -0800640 child_proc.done_flag = false;
Yabin Cuibe837362015-01-02 18:45:37 -0800641 if (++next_test_id == testcase_list[next_testcase_id].TestCount()) {
642 next_test_id = 0;
643 ++next_testcase_id;
Yabin Cui294d1e22014-12-07 20:43:37 -0800644 }
645 }
646 }
647
648 // Wait for any child proc finish or timeout.
649 WaitChildProcs(child_proc_list);
650
651 // Collect result.
652 for (auto& child_proc : child_proc_list) {
653 if (child_proc.pid != 0 && child_proc.done_flag == true) {
Yabin Cuibe837362015-01-02 18:45:37 -0800654 size_t testcase_id = child_proc.testcase_id;
655 size_t test_id = child_proc.test_id;
656 TestCase& testcase = testcase_list[testcase_id];
Yabin Cui657b1f92015-01-22 19:26:12 -0800657 testcase.SetTestTime(test_id, NanoTime() - child_proc.start_time_ns);
Yabin Cuibe837362015-01-02 18:45:37 -0800658
Yabin Cui657b1f92015-01-22 19:26:12 -0800659 // Kill and wait the timeout child process before we read failure message.
Yabin Cuibe837362015-01-02 18:45:37 -0800660 if (child_proc.timeout_flag) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800661 kill(child_proc.pid, SIGKILL);
662 WaitChildProc(child_proc.pid);
Yabin Cui657b1f92015-01-22 19:26:12 -0800663 }
664
665 while (true) {
666 char buf[1024];
667 int ret = TEMP_FAILURE_RETRY(read(child_proc.child_read_fd, buf, sizeof(buf) - 1));
668 if (ret > 0) {
669 buf[ret] = '\0';
670 testcase.GetTest(test_id).AppendFailureMessage(buf);
671 } else if (ret == 0) {
672 break; // Read end.
673 } else {
674 perror("read child_read_fd in RunTestInSeparateProc");
675 exit(1);
676 }
677 }
678 close(child_proc.child_read_fd);
679
680 if (child_proc.timeout_flag) {
Yabin Cuibe837362015-01-02 18:45:37 -0800681 testcase.SetTestResult(test_id, TEST_TIMEOUT);
Yabin Cui657b1f92015-01-22 19:26:12 -0800682 char buf[1024];
683 snprintf(buf, sizeof(buf), "%s killed because of timeout at %" PRId64 " ms.\n",
684 testcase.GetTestName(test_id).c_str(),
685 testcase.GetTestTime(test_id) / 1000000);
686 testcase.GetTest(test_id).AppendFailureMessage(buf);
Yabin Cuibe837362015-01-02 18:45:37 -0800687
688 } else if (WIFSIGNALED(child_proc.exit_status)) {
689 // Record signal terminated test as failed.
690 testcase.SetTestResult(test_id, TEST_FAILED);
Yabin Cui657b1f92015-01-22 19:26:12 -0800691 char buf[1024];
692 snprintf(buf, sizeof(buf), "%s terminated by signal: %s.\n",
693 testcase.GetTestName(test_id).c_str(),
694 strsignal(WTERMSIG(child_proc.exit_status)));
695 testcase.GetTest(test_id).AppendFailureMessage(buf);
Yabin Cuibe837362015-01-02 18:45:37 -0800696
697 } else {
698 testcase.SetTestResult(test_id, WEXITSTATUS(child_proc.exit_status) == 0 ?
699 TEST_SUCCESS : TEST_FAILED);
Yabin Cui294d1e22014-12-07 20:43:37 -0800700 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800701 OnTestEndPrint(testcase, test_id);
Yabin Cuibe837362015-01-02 18:45:37 -0800702
703 if (++finished_test_count_list[testcase_id] == testcase.TestCount()) {
704 ++finished_testcase_count;
Yabin Cui294d1e22014-12-07 20:43:37 -0800705 }
706 child_proc.pid = 0;
707 child_proc.done_flag = false;
708 }
709 }
710 }
711
Yabin Cui657b1f92015-01-22 19:26:12 -0800712 int64_t elapsed_time_ns = NanoTime() - iteration_start_time_ns;
713 OnTestIterationEndPrint(testcase_list, iteration, elapsed_time_ns);
714 if (!xml_output_filename.empty()) {
715 OnTestIterationEndXmlPrint(xml_output_filename, testcase_list, epoch_iteration_start_time,
716 elapsed_time_ns);
717 }
Yabin Cui294d1e22014-12-07 20:43:37 -0800718 }
719}
720
Yabin Cuibe837362015-01-02 18:45:37 -0800721static size_t GetProcessorCount() {
722 return static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN));
Yabin Cui294d1e22014-12-07 20:43:37 -0800723}
724
Yabin Cui11c43532015-01-28 14:28:14 -0800725static void AddGtestFilterSynonym(std::vector<char*>& args) {
726 // Support --gtest-filter as a synonym for --gtest_filter.
727 for (size_t i = 1; i < args.size(); ++i) {
728 if (strncmp(args[i], "--gtest-filter", strlen("--gtest-filter")) == 0) {
729 args[i][7] = '_';
730 }
731 }
732}
733
Yabin Cui657b1f92015-01-22 19:26:12 -0800734struct IsolationTestOptions {
735 bool isolate;
736 size_t job_count;
737 int test_deadline_ms;
738 int test_warnline_ms;
739 std::string gtest_color;
740 bool gtest_print_time;
741 size_t gtest_repeat;
742 std::string gtest_output;
743};
744
745// 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 -0800746// as described in PrintHelpInfo(), the other part is handled by testing::InitGoogleTest() in
Yabin Cui657b1f92015-01-22 19:26:12 -0800747// gtest. PickOptions() picks the first part into IsolationTestOptions structure, leaving the second
748// part in args.
Yabin Cuibe837362015-01-02 18:45:37 -0800749// Arguments:
Yabin Cui657b1f92015-01-22 19:26:12 -0800750// args is used to pass in all command arguments, and pass out only the part of options for gtest.
751// options is used to pass out test options in isolation mode.
752// Return false if there is error in arguments.
753static bool PickOptions(std::vector<char*>& args, IsolationTestOptions& options) {
754 for (size_t i = 1; i < args.size(); ++i) {
755 if (strcmp(args[i], "--help") == 0 || strcmp(args[i], "-h") == 0) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800756 PrintHelpInfo();
Yabin Cui657b1f92015-01-22 19:26:12 -0800757 options.isolate = false;
Yabin Cui294d1e22014-12-07 20:43:37 -0800758 return true;
759 }
760 }
761
Yabin Cui11c43532015-01-28 14:28:14 -0800762 AddGtestFilterSynonym(args);
763
Yabin Cui657b1f92015-01-22 19:26:12 -0800764 // if --bionic-selftest argument is used, only enable self tests, otherwise remove self tests.
765 bool enable_selftest = false;
766 for (size_t i = 1; i < args.size(); ++i) {
767 if (strcmp(args[i], "--bionic-selftest") == 0) {
768 // This argument is to enable "bionic_selftest*" for self test, and is not shown in help info.
769 // Don't remove this option from arguments.
770 enable_selftest = true;
771 }
772 }
773 std::string gtest_filter_str;
774 for (size_t i = args.size() - 1; i >= 1; --i) {
775 if (strncmp(args[i], "--gtest_filter=", strlen("--gtest_filter=")) == 0) {
776 gtest_filter_str = std::string(args[i]);
777 args.erase(args.begin() + i);
Yabin Cui294d1e22014-12-07 20:43:37 -0800778 break;
779 }
780 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800781 if (enable_selftest == true) {
782 args.push_back(strdup("--gtest_filter=bionic_selftest*"));
783 } else {
784 if (gtest_filter_str == "") {
785 gtest_filter_str = "--gtest_filter=-bionic_selftest*";
786 } else {
Yabin Cui0bc4e962015-01-27 11:22:46 -0800787 // Find if '-' for NEGATIVE_PATTERNS exists.
788 if (gtest_filter_str.find(":-") != std::string::npos) {
789 gtest_filter_str += ":bionic_selftest*";
790 } else {
791 gtest_filter_str += ":-bionic_selftest*";
792 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800793 }
794 args.push_back(strdup(gtest_filter_str.c_str()));
795 }
Yabin Cui294d1e22014-12-07 20:43:37 -0800796
Yabin Cui657b1f92015-01-22 19:26:12 -0800797 options.isolate = true;
798 // Parse arguments that make us can't run in isolation mode.
799 for (size_t i = 1; i < args.size(); ++i) {
800 if (strcmp(args[i], "--no-isolate") == 0) {
801 options.isolate = false;
802 } else if (strcmp(args[i], "--gtest_list_tests") == 0) {
803 options.isolate = false;
Yabin Cui294d1e22014-12-07 20:43:37 -0800804 }
805 }
806
Yabin Cui657b1f92015-01-22 19:26:12 -0800807 // Stop parsing if we will not run in isolation mode.
808 if (options.isolate == false) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800809 return true;
810 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800811
812 // Init default isolation test options.
813 options.job_count = GetProcessorCount();
814 options.test_deadline_ms = DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS;
815 options.test_warnline_ms = DEFAULT_GLOBAL_TEST_RUN_WARNLINE_MS;
816 options.gtest_color = testing::GTEST_FLAG(color);
817 options.gtest_print_time = testing::GTEST_FLAG(print_time);
818 options.gtest_repeat = testing::GTEST_FLAG(repeat);
819 options.gtest_output = testing::GTEST_FLAG(output);
820
821 // Parse arguments speficied for isolation mode.
822 for (size_t i = 1; i < args.size(); ++i) {
823 if (strncmp(args[i], "-j", strlen("-j")) == 0) {
824 char* p = args[i] + strlen("-j");
825 int count = 0;
826 if (*p != '\0') {
827 // Argument like -j5.
828 count = atoi(p);
829 } else if (args.size() > i + 1) {
830 // Arguments like -j 5.
831 count = atoi(args[i + 1]);
832 ++i;
833 }
834 if (count <= 0) {
835 fprintf(stderr, "invalid job count: %d\n", count);
836 return false;
837 }
838 options.job_count = static_cast<size_t>(count);
839 } else if (strncmp(args[i], "--deadline=", strlen("--deadline=")) == 0) {
840 int time_ms = atoi(args[i] + strlen("--deadline="));
841 if (time_ms <= 0) {
842 fprintf(stderr, "invalid deadline: %d\n", time_ms);
843 return false;
844 }
845 options.test_deadline_ms = time_ms;
846 } else if (strncmp(args[i], "--warnline=", strlen("--warnline=")) == 0) {
847 int time_ms = atoi(args[i] + strlen("--warnline="));
848 if (time_ms <= 0) {
849 fprintf(stderr, "invalid warnline: %d\n", time_ms);
850 return false;
851 }
852 options.test_warnline_ms = time_ms;
853 } else if (strncmp(args[i], "--gtest_color=", strlen("--gtest_color=")) == 0) {
854 options.gtest_color = args[i] + strlen("--gtest_color=");
855 } else if (strcmp(args[i], "--gtest_print_time=0") == 0) {
856 options.gtest_print_time = false;
857 } else if (strncmp(args[i], "--gtest_repeat=", strlen("--gtest_repeat=")) == 0) {
858 int repeat = atoi(args[i] + strlen("--gtest_repeat="));
859 if (repeat < 0) {
860 fprintf(stderr, "invalid gtest_repeat count: %d\n", repeat);
861 return false;
862 }
863 options.gtest_repeat = repeat;
864 // Remove --gtest_repeat=xx from arguments, so child process only run one iteration for a single test.
865 args.erase(args.begin() + i);
866 --i;
867 } else if (strncmp(args[i], "--gtest_output=", strlen("--gtest_output=")) == 0) {
868 std::string output = args[i] + strlen("--gtest_output=");
869 // generate output xml file path according to the strategy in gtest.
870 bool success = true;
871 if (strncmp(output.c_str(), "xml:", strlen("xml:")) == 0) {
872 output = output.substr(strlen("xml:"));
873 if (output.size() == 0) {
874 success = false;
875 }
876 // Make absolute path.
877 if (success && output[0] != '/') {
878 char* cwd = getcwd(NULL, 0);
879 if (cwd != NULL) {
880 output = std::string(cwd) + "/" + output;
881 free(cwd);
882 } else {
883 success = false;
884 }
885 }
886 // Add file name if output is a directory.
887 if (success && output.back() == '/') {
888 output += "test_details.xml";
889 }
890 }
891 if (success) {
892 options.gtest_output = output;
893 } else {
894 fprintf(stderr, "invalid gtest_output file: %s\n", args[i]);
895 return false;
896 }
897
898 // Remove --gtest_output=xxx from arguments, so child process will not write xml file.
899 args.erase(args.begin() + i);
900 --i;
901 }
902 }
903
904 // Add --no-isolate in args to prevent child process from running in isolation mode again.
905 // As DeathTest will try to call execve(), this argument should always be added.
906 args.insert(args.begin() + 1, strdup("--no-isolate"));
Yabin Cui294d1e22014-12-07 20:43:37 -0800907 return true;
908}
909
910int main(int argc, char** argv) {
Yabin Cuibe837362015-01-02 18:45:37 -0800911 std::vector<char*> arg_list;
912 for (int i = 0; i < argc; ++i) {
913 arg_list.push_back(argv[i]);
914 }
Yabin Cuibe837362015-01-02 18:45:37 -0800915
Yabin Cui657b1f92015-01-22 19:26:12 -0800916 IsolationTestOptions options;
917 if (PickOptions(arg_list, options) == false) {
918 return 1;
Yabin Cui294d1e22014-12-07 20:43:37 -0800919 }
Yabin Cui657b1f92015-01-22 19:26:12 -0800920
921 if (options.isolate == true) {
922 // Set global variables.
923 global_test_run_deadline_ms = options.test_deadline_ms;
924 global_test_run_warnline_ms = options.test_warnline_ms;
925 testing::GTEST_FLAG(color) = options.gtest_color.c_str();
926 testing::GTEST_FLAG(print_time) = options.gtest_print_time;
927 std::vector<TestCase> testcase_list;
928
929 argc = static_cast<int>(arg_list.size());
930 arg_list.push_back(NULL);
931 if (EnumerateTests(argc, arg_list.data(), testcase_list) == false) {
932 return 1;
933 }
934 RunTestInSeparateProc(argc, arg_list.data(), testcase_list, options.gtest_repeat,
935 options.job_count, options.gtest_output);
936 } else {
937 argc = static_cast<int>(arg_list.size());
938 arg_list.push_back(NULL);
939 testing::InitGoogleTest(&argc, arg_list.data());
940 return RUN_ALL_TESTS();
941 }
942 return 0;
Yabin Cui294d1e22014-12-07 20:43:37 -0800943}
944
945//################################################################################
Yabin Cuibe837362015-01-02 18:45:37 -0800946// Bionic Gtest self test, run this by --bionic-selftest option.
Yabin Cui294d1e22014-12-07 20:43:37 -0800947
Yabin Cuibe837362015-01-02 18:45:37 -0800948TEST(bionic_selftest, test_success) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800949 ASSERT_EQ(1, 1);
950}
951
Yabin Cuibe837362015-01-02 18:45:37 -0800952TEST(bionic_selftest, test_fail) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800953 ASSERT_EQ(0, 1);
954}
955
Yabin Cuibe837362015-01-02 18:45:37 -0800956TEST(bionic_selftest, test_time_warn) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800957 sleep(4);
958}
959
Yabin Cuibe837362015-01-02 18:45:37 -0800960TEST(bionic_selftest, test_timeout) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800961 while (1) {}
962}
Yabin Cuibe837362015-01-02 18:45:37 -0800963
964TEST(bionic_selftest, test_signal_SEGV_terminated) {
965 char* p = reinterpret_cast<char*>(static_cast<intptr_t>(atoi("0")));
966 *p = 3;
967}
Yabin Cui657b1f92015-01-22 19:26:12 -0800968
969class bionic_selftest_DeathTest : public BionicDeathTest {};
970
971static void deathtest_helper_success() {
972 ASSERT_EQ(1, 1);
973 exit(0);
974}
975
976TEST_F(bionic_selftest_DeathTest, success) {
977 ASSERT_EXIT(deathtest_helper_success(), ::testing::ExitedWithCode(0), "");
978}
979
980static void deathtest_helper_fail() {
981 ASSERT_EQ(1, 0);
982}
983
984TEST_F(bionic_selftest_DeathTest, fail) {
985 ASSERT_EXIT(deathtest_helper_fail(), ::testing::ExitedWithCode(0), "");
986}