diff mbox

report: encode XML Character Entities in xUnit report

Message ID 20170930034621.3036-1-tytso@mit.edu (mailing list archive)
State New, archived
Headers show

Commit Message

Theodore Ts'o Sept. 30, 2017, 3:46 a.m. UTC
Since the xUnit report is an XML document, special XML characters such
as '<', '>', '&', etc. have to be encoded as "&lt;", "&gt;", etc.
Otherwise programs parsing something like this:

	<testcase classname="xfstests.global" name="generic/450" time="0">
		<skipped message="Only test on sector size < half of block size" />
	</testcase>

Will get choke the unescaped '<' character in the skipped message.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Cc: Dmitry Monakhov <dmonakhov@openvz.org>
---
 common/report | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

Comments

Theodore Ts'o Sept. 30, 2017, 3:58 a.m. UTC | #1
On Fri, Sep 29, 2017 at 11:46:21PM -0400, Theodore Ts'o wrote:
> Since the xUnit report is an XML document, special XML characters such
> as '<', '>', '&', etc. have to be encoded as "&lt;", "&gt;", etc.
> Otherwise programs parsing something like this:
> 
> 	<testcase classname="xfstests.global" name="generic/450" time="0">
> 		<skipped message="Only test on sector size < half of block size" />
> 	</testcase>
> 
> Will get choke the unescaped '<' character in the skipped message.

The last line should be replaced by:

  ... will choke on the unescaped '<' character in the 'messages'
  attribute of the 'skipped' element.


--
To unsubscribe from this list: send the line "unsubscribe fstests" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/common/report b/common/report
index 15a63db8..bb689836 100644
--- a/common/report
+++ b/common/report
@@ -25,6 +25,15 @@  REPORT_ENV_LIST="$REPORT_ENV_LIST OVL_UPPER"
 REPORT_ENV_LIST="$REPORT_ENV_LIST OVL_LOWER"
 REPORT_ENV_LIST="$REPORT_ENV_LIST OVL_WORK"
 
+encode_xml()
+{
+    sed -e 's/&/\&amp;/g' \
+	-e 's/>/\&gt;/g' \
+	-e 's/</\&lt;/g' \
+	-e "s/'/\&apos;/g" \
+	-e 's/"/\&quot;/g'
+}
+
 #
 # Xunit format report functions
 _xunit_add_property()
@@ -86,7 +95,7 @@  _xunit_make_testcase_report()
 		;;
 	"notrun")
 		if [ -f $seqres.notrun ]; then
-			local msg=`cat $seqres.notrun`
+			local msg=`cat $seqres.notrun | encode_xml`
 			echo -e "\t\t<skipped message=\"$msg\" />" >> $report
 		else
 			echo -e "\t\t<skipped/>" >> $report
@@ -103,20 +112,20 @@  _xunit_make_testcase_report()
 		if [ -s $seqres.full ]; then
 			echo -e "\t\t<system-out>" >> $report
 			printf	'<![CDATA[\n' >>$report
-			cat $seqres.full | tr -dc '[:print:][:space:]' >>$report
+			cat $seqres.full | tr -dc '[:print:][:space:]' | encode_xml >>$report
 			printf ']]>\n'	>>$report
 			echo -e "\t\t</system-out>" >> $report
 		fi
 		if [ -f $seqres.dmesg ]; then
 			echo -e "\t\t<system-err>" >> $report
 			printf	'<![CDATA[\n' >>$report
-			cat $seqres.dmesg | tr -dc '[:print:][:space:]' >>$report
+			cat $seqres.dmesg | tr -dc '[:print:][:space:]' | encode_xml >>$report
 			printf ']]>\n'	>>$report
 			echo -e "\t\t</system-err>" >> $report
 		elif [ -s $seqres.out.bad ]; then
 			echo -e "\t\t<system-err>" >> $report
 			printf	'<![CDATA[\n' >>$report
-			$diff $seq.out $seqres.out.bad >>$report
+			$diff $seq.out $seqres.out.bad | encode_xml >>$report
 			printf ']]>\n'	>>$report
 			echo -e "\t\t</system-err>" >> $report
 		fi