Skip to content

Commit

Permalink
Strip invalid xml 1.0 chars (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Munn authored and aghassemi committed Jan 3, 2017
1 parent fee75bb commit b233746
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/sanitize-string.js
Original file line number Diff line number Diff line change
@@ -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, '');
}
5 changes: 3 additions & 2 deletions lib/serialize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var xmlbuilder = require('xmlbuilder');
var sanitizeString = require('./sanitize-string');

module.exports = function serialize (testCases) {
var rootXml = xmlbuilder.create('testsuites');
Expand Down Expand Up @@ -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));
});
}
});
Expand Down
Binary file added test.txt
Binary file not shown.
24 changes: 24 additions & 0 deletions unit-tests/serialization-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions unit-tests/xml-sanitization-tests.js
Original file line number Diff line number Diff line change
@@ -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();
}
);
});

0 comments on commit b233746

Please sign in to comment.