blob: 3e345e9cadb99a01fb9d2f05d632f3de76e4342f [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_) {
81 case TEST_PASS:
82 ColoredPrintf(COLOR_GREEN, "[ OK ]");
83 break;
84 case TEST_XPASS:
85 ColoredPrintf(COLOR_RED, "[ XPASS ]");
86 break;
87 case TEST_FAIL:
88 ColoredPrintf(COLOR_RED, "[ FAILED ]");
89 break;
90 case TEST_XFAIL:
91 ColoredPrintf(COLOR_YELLOW, "[ XFAIL ]");
92 break;
93 case TEST_TIMEOUT:
94 ColoredPrintf(COLOR_RED, "[ TIMEOUT ]");
95 break;
96 case TEST_NONE:
97 LOG(FATAL) << "Test result is TEST_NONE, this should not be possible.";
98 }
99
100 printf(" %s", name_.c_str());
101 if (::testing::GTEST_FLAG(print_time)) {
102 printf(" (%" PRId64 " ms)", (end_ns_ - start_ns_) / kNsPerMs);
103 }
104 printf("\n");
105
106 printf("%s", output_.c_str());
107 fflush(stdout);
108}
109
110bool Test::Read() {
111 char buffer[2048];
112 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd_, buffer, sizeof(buffer) - 1));
113 if (bytes < 0) {
114 if (errno == EAGAIN || errno == EWOULDBLOCK) {
115 // Reading would block. Since this is not an error keep going.
116 return true;
117 }
118 PLOG(FATAL) << "Unexpected failure from read";
119 return false;
120 }
121
122 if (bytes == 0) {
123 return false;
124 }
125 buffer[bytes] = '\0';
126 output_ += buffer;
127 return true;
128}
129
130void Test::ReadUntilClosed() {
131 uint64_t start_ns = NanoTime();
132 while (fd_ != -1) {
133 if (!Read()) {
134 CloseFd();
135 break;
136 }
137 if (NanoTime() - start_ns > 2 * kNsPerS) {
138 printf("Reading of done process did not finish after 2 seconds.\n");
139 CloseFd();
140 break;
141 }
142 }
143}
144
145} // namespace gtest_extras
146} // namespace android