diff --git a/lib/sanitize-string.js b/lib/sanitize-string.js new file mode 100644 index 0000000..5cb8b9f --- /dev/null +++ b/lib/sanitize-string.js @@ -0,0 +1,4 @@ +var NOT_SAFE_IN_XML_1_0 = /[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm; +module.exports = function sanitizeString(str) { + return str.replace(NOT_SAFE_IN_XML_1_0, ''); +} diff --git a/lib/serialize.js b/lib/serialize.js index 2a4f667..c41aa00 100644 --- a/lib/serialize.js +++ b/lib/serialize.js @@ -1,4 +1,5 @@ var xmlbuilder = require('xmlbuilder'); +var sanitizeString = require('./sanitize-string'); module.exports = function serialize (testCases) { var rootXml = xmlbuilder.create('testsuites'); @@ -33,8 +34,8 @@ module.exports = function serialize (testCases) { } } if(i === suite.asserts.length -1) { - suite.extra.forEach(function (e) { - testCaseElement.ele('system-out', e); + suite.extra.forEach(function (extraContent) { + testCaseElement.ele('system-out', sanitizeString(extraContent)); }); } }); diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..22eb60a Binary files /dev/null and b/test.txt differ diff --git a/unit-tests/serialization-tests.js b/unit-tests/serialization-tests.js index 674c447..02232a7 100644 --- a/unit-tests/serialization-tests.js +++ b/unit-tests/serialization-tests.js @@ -112,6 +112,30 @@ var serialize = require('../lib/serialize'); }] } } + }, + { + name: 'removes null characters from log', + input: [{ + extra: [ '\u0000' ], + asserts: [ + { ok: true, id: 1, name: 'should be equal' }, + { ok: true, id: 2, name: 'should be equal' } + ] + }], + expected: { + testsuites: { + testsuite: [{ + '$': { tests: '2', failures: '0', name: '', errors: '0' }, + testcase: [ + { '$': { name: '#1 should be equal' } }, + { + '$': { name: '#2 should be equal' }, + 'system-out': [ '' ] + }, + ], + }] + } + } } ].forEach(function(testCase) { test('serializes: ' + testCase.name, function (assert) { diff --git a/unit-tests/xml-sanitization-tests.js b/unit-tests/xml-sanitization-tests.js new file mode 100644 index 0000000..641f1ba --- /dev/null +++ b/unit-tests/xml-sanitization-tests.js @@ -0,0 +1,27 @@ +var test = require('tape'); +var sanitizeString = require('../lib/sanitize-string'); + +[ + '\u0000', + '\u0001' +].forEach(function(invalidCharacter){ + test( + 'it strips the invalid character:' + invalidCharacter.charCodeAt(0), + function(assert){ + assert.equal(sanitizeString(invalidCharacter), ''); + assert.end(); + } + ); +}); + +[ + '\u000A', +].forEach(function(validCharacter){ + test( + 'it doesn\'t strip the valid character:' + validCharacter.charCodeAt(0), + function(assert){ + assert.equal(sanitizeString(validCharacter), validCharacter); + assert.end(); + } + ); +});