Escape failure messages in XML test output.
The gtest XML format requires escaped HTML characters in the test
results.
Change-Id: Ieb9519a55cb52093dfb10a88e883b569bc372cdb
diff --git a/tests/gtest_main.cpp b/tests/gtest_main.cpp
index a662c73..7360f12 100644
--- a/tests/gtest_main.cpp
+++ b/tests/gtest_main.cpp
@@ -442,6 +442,36 @@
fflush(stdout);
}
+std::string XmlEscape(const std::string& xml) {
+ std::string escaped;
+ escaped.reserve(xml.size());
+
+ for (auto c : xml) {
+ switch (c) {
+ case '<':
+ escaped.append("<");
+ break;
+ case '>':
+ escaped.append(">");
+ break;
+ case '&':
+ escaped.append("&");
+ break;
+ case '\'':
+ escaped.append("'");
+ break;
+ case '"':
+ escaped.append(""");
+ break;
+ default:
+ escaped.append(1, c);
+ break;
+ }
+ }
+
+ return escaped;
+}
+
// Output xml file when --gtest_output is used, write this function as we can't reuse
// gtest.cc:XmlUnitTestResultPrinter. The reason is XmlUnitTestResultPrinter is totally
// defined in gtest.cc and not expose to outside. What's more, as we don't run gtest in
@@ -497,7 +527,8 @@
} else {
fputs(">\n", fp);
const std::string& test_output = testcase.GetTest(j).GetTestOutput();
- fprintf(fp, " <failure message=\"%s\" type=\"\">\n", test_output.c_str());
+ const std::string escaped_test_output = XmlEscape(test_output);
+ fprintf(fp, " <failure message=\"%s\" type=\"\">\n", escaped_test_output.c_str());
fputs(" </failure>\n", fp);
fputs(" </testcase>\n", fp);
}