blob: 5fbaa6745f2cbf7cb5deff47b2e01d99a5f65228 [file] [log] [blame]
Christopher Ferris1a993562018-08-21 12:43:50 -07001/*
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 <inttypes.h>
18#include <stdio.h>
19#include <unistd.h>
20
21#include <string>
22#include <tuple>
23#include <vector>
24
25#include <android-base/logging.h>
26#include <gtest/gtest.h>
27
28#include "Color.h"
29#include "NanoTime.h"
30#include "Test.h"
31
32namespace android {
33namespace gtest_extras {
34
35Test::Test(std::tuple<std::string, std::string>& test, size_t index, size_t run_index, int fd)
36 : case_name_(std::get<0>(test)),
37 test_name_(std::get<1>(test)),
38 name_(case_name_ + test_name_),
39 test_index_(index),
40 run_index_(run_index),
41 fd_(fd),
42 start_ns_(NanoTime()) {}
43
44void Test::Stop() {
45 end_ns_ = NanoTime();
46}
47
48void Test::CloseFd() {
49 fd_.reset();
50}
51
52void Test::PrintGtestFormat() {
53 ColoredPrintf(COLOR_GREEN, "[ RUN ]");
54 printf(" %s\n", name_.c_str());
55 printf("%s", output_.c_str());
56
57 switch (result_) {
58 case TEST_PASS:
59 case TEST_XFAIL:
60 ColoredPrintf(COLOR_GREEN, "[ OK ]");
61 break;
62 default:
63 ColoredPrintf(COLOR_RED, "[ FAILED ]");
64 break;
65 }
66 printf(" %s", name_.c_str());
67 if (::testing::GTEST_FLAG(print_time)) {
68 printf(" (%" PRId64 " ms)", RunTimeNs() / kNsPerMs);
69 }
70 printf("\n");
71 fflush(stdout);
72}
73
74void Test::Print(bool gtest_format) {
75 if (gtest_format) {
76 PrintGtestFormat();
77 return;
78 }
79
80 switch (result_) {
Christopher Ferrisef982172018-09-27 15:19:07 -070081 case TEST_XFAIL:
Christopher Ferris1a993562018-08-21 12:43:50 -070082 case TEST_PASS:
83 ColoredPrintf(COLOR_GREEN, "[ OK ]");
84 break;
85 case TEST_XPASS:
Christopher Ferris1a993562018-08-21 12:43:50 -070086 case TEST_FAIL:
87 ColoredPrintf(COLOR_RED, "[ FAILED ]");
88 break;
Christopher Ferris1a993562018-08-21 12:43:50 -070089 case TEST_TIMEOUT:
90 ColoredPrintf(COLOR_RED, "[ TIMEOUT ]");
91 break;
92 case TEST_NONE:
93 LOG(FATAL) << "Test result is TEST_NONE, this should not be possible.";
94 }
95
96 printf(" %s", name_.c_str());
97 if (::testing::GTEST_FLAG(print_time)) {
98 printf(" (%" PRId64 " ms)", (end_ns_ - start_ns_) / kNsPerMs);
99 }
100 printf("\n");
101
102 printf("%s", output_.c_str());
103 fflush(stdout);
104}
105
106bool Test::Read() {
107 char buffer[2048];
108 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd_, buffer, sizeof(buffer) - 1));
109 if (bytes < 0) {
110 if (errno == EAGAIN || errno == EWOULDBLOCK) {
111 // Reading would block. Since this is not an error keep going.
112 return true;
113 }
114 PLOG(FATAL) << "Unexpected failure from read";
115 return false;
116 }
117
118 if (bytes == 0) {
119 return false;
120 }
121 buffer[bytes] = '\0';
122 output_ += buffer;
123 return true;
124}
125
126void Test::ReadUntilClosed() {
127 uint64_t start_ns = NanoTime();
128 while (fd_ != -1) {
129 if (!Read()) {
130 CloseFd();
131 break;
132 }
133 if (NanoTime() - start_ns > 2 * kNsPerS) {
134 printf("Reading of done process did not finish after 2 seconds.\n");
135 CloseFd();
136 break;
137 }
138 }
139}
140
141} // namespace gtest_extras
142} // namespace android