[Password Manager] Relax matching for username overrides.
[chromium-blink-merge.git] / base / test / gtest_xml_util.cc
blobdb8cc2d48df70c29e467aaf2fbe1443c0f43ae49
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/test/gtest_xml_util.h"
7 #include "base/files/file_util.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/test/gtest_util.h"
11 #include "base/test/launcher/test_launcher.h"
12 #include "third_party/libxml/chromium/libxml_utils.h"
14 namespace base {
16 namespace {
18 // This is used for the xml parser to report errors. This assumes the context
19 // is a pointer to a std::string where the error message should be appended.
20 static void XmlErrorFunc(void *context, const char *message, ...) {
21 va_list args;
22 va_start(args, message);
23 std::string* error = static_cast<std::string*>(context);
24 base::StringAppendV(error, message, args);
25 va_end(args);
28 } // namespace
30 XmlUnitTestResultPrinter::XmlUnitTestResultPrinter() : output_file_(NULL) {
33 XmlUnitTestResultPrinter::~XmlUnitTestResultPrinter() {
34 if (output_file_) {
35 fprintf(output_file_, "</testsuites>\n");
36 fflush(output_file_);
37 base::CloseFile(output_file_);
41 bool XmlUnitTestResultPrinter::Initialize(const FilePath& output_file_path) {
42 DCHECK(!output_file_);
43 output_file_ = OpenFile(output_file_path, "w");
44 if (!output_file_)
45 return false;
47 fprintf(output_file_,
48 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n");
49 fflush(output_file_);
51 return true;
54 void XmlUnitTestResultPrinter::OnTestCaseStart(
55 const testing::TestCase& test_case) {
56 fprintf(output_file_, " <testsuite>\n");
57 fflush(output_file_);
60 void XmlUnitTestResultPrinter::OnTestStart(const testing::TestInfo& test_info) {
61 // This is our custom extension - it helps to recognize which test was running
62 // when the test binary crashed. Note that we cannot even open the <testcase>
63 // tag here - it requires e.g. run time of the test to be known.
64 fprintf(output_file_,
65 " <x-teststart name=\"%s\" classname=\"%s\" />\n",
66 test_info.name(),
67 test_info.test_case_name());
68 fflush(output_file_);
71 void XmlUnitTestResultPrinter::OnTestEnd(const testing::TestInfo& test_info) {
72 fprintf(output_file_,
73 " <testcase name=\"%s\" status=\"run\" time=\"%.3f\""
74 " classname=\"%s\">\n",
75 test_info.name(),
76 static_cast<double>(test_info.result()->elapsed_time()) /
77 Time::kMillisecondsPerSecond,
78 test_info.test_case_name());
79 if (test_info.result()->Failed())
80 fprintf(output_file_, " <failure message=\"\" type=\"\"></failure>\n");
81 fprintf(output_file_, " </testcase>\n");
82 fflush(output_file_);
85 void XmlUnitTestResultPrinter::OnTestCaseEnd(
86 const testing::TestCase& test_case) {
87 fprintf(output_file_, " </testsuite>\n");
88 fflush(output_file_);
91 bool ProcessGTestOutput(const base::FilePath& output_file,
92 std::vector<TestResult>* results,
93 bool* crashed) {
94 DCHECK(results);
96 std::string xml_contents;
97 if (!ReadFileToString(output_file, &xml_contents))
98 return false;
100 // Silence XML errors - otherwise they go to stderr.
101 std::string xml_errors;
102 ScopedXmlErrorFunc error_func(&xml_errors, &XmlErrorFunc);
104 XmlReader xml_reader;
105 if (!xml_reader.Load(xml_contents))
106 return false;
108 enum {
109 STATE_INIT,
110 STATE_TESTSUITE,
111 STATE_TESTCASE,
112 STATE_FAILURE,
113 STATE_END,
114 } state = STATE_INIT;
116 while (xml_reader.Read()) {
117 xml_reader.SkipToElement();
118 std::string node_name(xml_reader.NodeName());
120 switch (state) {
121 case STATE_INIT:
122 if (node_name == "testsuites" && !xml_reader.IsClosingElement())
123 state = STATE_TESTSUITE;
124 else
125 return false;
126 break;
127 case STATE_TESTSUITE:
128 if (node_name == "testsuites" && xml_reader.IsClosingElement())
129 state = STATE_END;
130 else if (node_name == "testsuite" && !xml_reader.IsClosingElement())
131 state = STATE_TESTCASE;
132 else
133 return false;
134 break;
135 case STATE_TESTCASE:
136 if (node_name == "testsuite" && xml_reader.IsClosingElement()) {
137 state = STATE_TESTSUITE;
138 } else if (node_name == "x-teststart" &&
139 !xml_reader.IsClosingElement()) {
140 // This is our custom extension that helps recognize which test was
141 // running when the test binary crashed.
142 TestResult result;
144 std::string test_case_name;
145 if (!xml_reader.NodeAttribute("classname", &test_case_name))
146 return false;
147 std::string test_name;
148 if (!xml_reader.NodeAttribute("name", &test_name))
149 return false;
150 result.full_name = FormatFullTestName(test_case_name, test_name);
152 result.elapsed_time = TimeDelta();
154 // Assume the test crashed - we can correct that later.
155 result.status = TestResult::TEST_CRASH;
157 results->push_back(result);
158 } else if (node_name == "testcase" && !xml_reader.IsClosingElement()) {
159 std::string test_status;
160 if (!xml_reader.NodeAttribute("status", &test_status))
161 return false;
163 if (test_status != "run" && test_status != "notrun")
164 return false;
165 if (test_status != "run")
166 break;
168 TestResult result;
170 std::string test_case_name;
171 if (!xml_reader.NodeAttribute("classname", &test_case_name))
172 return false;
173 std::string test_name;
174 if (!xml_reader.NodeAttribute("name", &test_name))
175 return false;
176 result.full_name = test_case_name + "." + test_name;
178 std::string test_time_str;
179 if (!xml_reader.NodeAttribute("time", &test_time_str))
180 return false;
181 result.elapsed_time = TimeDelta::FromMicroseconds(
182 static_cast<int64>(strtod(test_time_str.c_str(), NULL) *
183 Time::kMicrosecondsPerSecond));
185 result.status = TestResult::TEST_SUCCESS;
187 if (!results->empty() &&
188 results->at(results->size() - 1).full_name == result.full_name &&
189 results->at(results->size() - 1).status ==
190 TestResult::TEST_CRASH) {
191 // Erase the fail-safe "crashed" result - now we know the test did
192 // not crash.
193 results->pop_back();
196 results->push_back(result);
197 } else if (node_name == "failure" && !xml_reader.IsClosingElement()) {
198 std::string failure_message;
199 if (!xml_reader.NodeAttribute("message", &failure_message))
200 return false;
202 DCHECK(!results->empty());
203 results->at(results->size() - 1).status = TestResult::TEST_FAILURE;
205 state = STATE_FAILURE;
206 } else if (node_name == "testcase" && xml_reader.IsClosingElement()) {
207 // Deliberately empty.
208 } else {
209 return false;
211 break;
212 case STATE_FAILURE:
213 if (node_name == "failure" && xml_reader.IsClosingElement())
214 state = STATE_TESTCASE;
215 else
216 return false;
217 break;
218 case STATE_END:
219 // If we are here and there are still XML elements, the file has wrong
220 // format.
221 return false;
225 *crashed = (state != STATE_END);
226 return true;
229 } // namespace base