Revert "Avoid double escaping due to Serializer"

This reverts commit 4643ee1654907414c993876a5d2668533c1b3264.

Escape XML tags again in case test message has un-escaped xml tags.

Bug: 160932821

Change-Id: I255d32ac8f0fffe82557fecc2d493f7d549c17f6
Merged-In: I255d32ac8f0fffe82557fecc2d493f7d549c17f6
diff --git a/src/com/android/tradefed/result/suite/XmlSuiteResultFormatter.java b/src/com/android/tradefed/result/suite/XmlSuiteResultFormatter.java
index 06592b2..772075b 100644
--- a/src/com/android/tradefed/result/suite/XmlSuiteResultFormatter.java
+++ b/src/com/android/tradefed/result/suite/XmlSuiteResultFormatter.java
@@ -34,6 +34,7 @@
 import com.android.tradefed.util.proto.TfMetricProtoUtil;
 
 import com.google.common.base.Strings;
+import com.google.common.xml.XmlEscapers;
 import com.google.gson.Gson;
 
 import org.xmlpull.v1.XmlPullParser;
@@ -707,7 +708,6 @@
 
     @VisibleForTesting
     static String sanitizeXmlContent(String s) {
-        // Replace c++ \0 null since Serializer doesn't handle it.
-        return s.replace("\0", "[Null]");
+        return XmlEscapers.xmlContentEscaper().escape(s);
     }
 }
diff --git a/tests/src/com/android/tradefed/result/suite/XmlSuiteResultFormatterTest.java b/tests/src/com/android/tradefed/result/suite/XmlSuiteResultFormatterTest.java
index 67a623a..647db0b 100644
--- a/tests/src/com/android/tradefed/result/suite/XmlSuiteResultFormatterTest.java
+++ b/tests/src/com/android/tradefed/result/suite/XmlSuiteResultFormatterTest.java
@@ -170,7 +170,7 @@
         assertXmlContainsValue(
                 content,
                 "Result/Module/TestCase/Test/Failure/StackTrace",
-                "module1 failed.\nstack\n<null>stack[Null]");
+                XmlSuiteResultFormatter.sanitizeXmlContent("module1 failed.\nstack\nstack\0"));
         // Test that we can read back the informations
         SuiteResultHolder holder = mFormatter.parseResults(mResultDir, false);
         assertEquals(holder.completeModules, mResultHolder.completeModules);
@@ -224,7 +224,7 @@
         assertXmlContainsValue(
                 content,
                 "Result/Module/TestCase/Test/Failure/StackTrace",
-                "module1 failed.\nstack\n<null>stack[Null]");
+                XmlSuiteResultFormatter.sanitizeXmlContent("module1 failed.\nstack\nstack\0"));
         // Test that we can read back the informations
         SuiteResultHolder holder = mFormatter.parseResults(mResultDir, false);
         assertEquals(holder.completeModules, mResultHolder.completeModules);
@@ -477,7 +477,7 @@
         assertXmlContainsValue(
                 content,
                 "Result/Module/TestCase/Test/Failure/StackTrace",
-                "module1 failed.\nstack\n<null>stack[Null]");
+                XmlSuiteResultFormatter.sanitizeXmlContent("module1 failed.\nstack\nstack\0"));
         // Test that we can read back the informations
         SuiteResultHolder holder = mFormatter.parseResults(mResultDir, true);
         assertEquals(holder.completeModules, mResultHolder.completeModules);
@@ -671,7 +671,7 @@
                     new TestDescription("com.class." + runName, runName + ".failed" + i);
             fakeRes.testStarted(description);
             // Include a null character \0 that is not XML supported
-            fakeRes.testFailed(description, runName + " failed.\nstack\n<null>stack\0");
+            fakeRes.testFailed(description, runName + " failed.\nstack\nstack\0");
             HashMap<String, Metric> metrics = new HashMap<String, Metric>();
             if (withMetrics) {
                 metrics.put("metric0" + i, TfMetricProtoUtil.stringToMetric("value0" + i));