/
test.js
54 lines (47 loc) · 1.77 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
var assert = require('assert');
var trimOffNewlines = require('./');
it('should trim off \\r', function () {
assert.strictEqual(trimOffNewlines('\runicorns'), 'unicorns');
assert.strictEqual(trimOffNewlines('unicorns\r\r'), 'unicorns');
assert.strictEqual(trimOffNewlines('unicorns\r'), 'unicorns');
});
it('should trim off \\n', function () {
assert.strictEqual(trimOffNewlines('\nunicorns'), 'unicorns');
assert.strictEqual(trimOffNewlines('\n\n\n\nunicorns\n\n'), 'unicorns');
assert.strictEqual(trimOffNewlines('unicorns\n'), 'unicorns');
});
it('should trim off \\r\\n', function () {
assert.strictEqual(trimOffNewlines('\r\n\r\n\r\nunicorns'), 'unicorns');
assert.strictEqual(trimOffNewlines('\r\nunicorns\r\n'), 'unicorns');
assert.strictEqual(trimOffNewlines('unicorns\r\n\r\n\r\n\r\n\r\n\r\n'), 'unicorns');
});
it('should not be susceptible to exponential backtracking', function () {
var redosString = 'a';
var count = 1000;
while (count) {
redosString += '\r\n';
count--;
}
redosString += 'a';
var LongerRedosString = redosString;
var count = 1000;
while (count) {
LongerRedosString += redosString;
count--;
}
var start = Date.now();
trimOffNewlines(redosString);
trimOffNewlines(LongerRedosString);
var end = Date.now();
assert.ok(end - start < 1000, 'took too long, probably susceptible to ReDOS');
});
it('should leave newlines in the middle of a string alone', function () {
assert.strictEqual(trimOffNewlines('Come on,\nFhqwhgads.'), 'Come on,\nFhqwhgads.');
});
it('should leave spaces at start and end alone', function () {
assert.strictEqual(trimOffNewlines(' fhqwhgads '), ' fhqwhgads ');
});
it('should return an empty string if there are only \\r and \\n', function () {
assert.strictEqual(trimOffNewlines('\r\n\r\r\n\n'), '');
});