am e0e5790: AI 148153: CTS: Strip control characters from failure string

Merge commit 'e0e579051414248e1b74fbfe5029d07b7edcd456' into donut

* commit 'e0e579051414248e1b74fbfe5029d07b7edcd456':
  AI 148153: CTS: Strip control characters from failure strings before converting to XML
diff --git a/tools/host/src/com/android/cts/HostUtils.java b/tools/host/src/com/android/cts/HostUtils.java
index 7450dae..11736e9 100644
--- a/tools/host/src/com/android/cts/HostUtils.java
+++ b/tools/host/src/com/android/cts/HostUtils.java
@@ -289,4 +289,12 @@
         return buf.toString();
     }
     
+    /**
+     * Strip control characters from the given string.
+     */
+    public static String replaceControlChars(String s) {
+        // Replace any character < 0x20, except for tab, lf and cr
+        return s.replaceAll("[\\x00-\\x1f&&[^\t\n\r]]", "?");
+    }
+
 }
diff --git a/tools/host/src/com/android/cts/TestSessionLog.java b/tools/host/src/com/android/cts/TestSessionLog.java
index acb155d..6600a05 100644
--- a/tools/host/src/com/android/cts/TestSessionLog.java
+++ b/tools/host/src/com/android/cts/TestSessionLog.java
@@ -393,6 +393,9 @@
                 String failedMessage = result.getFailedMessage();
 
                 if (failedMessage != null) {
+                    // failure message may contain control characters < 0x20 that get translated
+                    // into illegal XML character entities. Replace them first.
+                    failedMessage = HostUtils.replaceControlChars(failedMessage);
                     Node failedMessageNode = doc.createElement(TAG_FAILED_SCENE);
                     testNode.appendChild(failedMessageNode);
                     setAttribute(doc, failedMessageNode,TAG_FAILED_MESSAGE, failedMessage);