| 1 |
|
| 2 |
'use strict'; |
| 3 |
|
| 4 |
|
| 5 |
var assert = require('assert'); |
| 6 |
var decode = require('../decode'); |
| 7 |
|
| 8 |
function encodeBinary(str) { |
| 9 |
var result = ''; |
| 10 |
|
| 11 |
str = str.replace(/\s+/g, ''); |
| 12 |
while (str.length) { |
| 13 |
result = '%' + ('0' + parseInt(str.slice(-8), 2).toString(16)).slice(-2) + result; |
| 14 |
str = str.slice(0, -8); |
| 15 |
} |
| 16 |
|
| 17 |
return result; |
| 18 |
} |
| 19 |
|
| 20 |
var samples = { |
| 21 |
'00000000': true, |
| 22 |
'01010101': true, |
| 23 |
'01111111': true, |
| 24 |
|
| 25 |
// invalid as 1st byte |
| 26 |
'10000000': true, |
| 27 |
'10111111': true, |
| 28 |
|
| 29 |
// invalid sequences, 2nd byte should be >= 0x80 |
| 30 |
'11000111 01010101': false, |
| 31 |
'11100011 01010101': false, |
| 32 |
'11110001 01010101': false, |
| 33 |
|
| 34 |
// invalid sequences, 2nd byte should be < 0xc0 |
| 35 |
'11000111 11000000': false, |
| 36 |
'11100011 11000000': false, |
| 37 |
'11110001 11000000': false, |
| 38 |
|
| 39 |
// invalid 3rd byte |
| 40 |
'11100011 10010101 01010101': false, |
| 41 |
'11110001 10010101 01010101': false, |
| 42 |
|
| 43 |
// invalid 4th byte |
| 44 |
'11110001 10010101 10010101 01010101': false, |
| 45 |
|
| 46 |
// valid sequences |
| 47 |
'11000111 10101010': true, |
| 48 |
'11100011 10101010 10101010': true, |
| 49 |
'11110001 10101010 10101010 10101010': true, |
| 50 |
|
| 51 |
// minimal chars with given length |
| 52 |
'11000010 10000000': true, |
| 53 |
'11100000 10100000 10000000': true, |
| 54 |
|
| 55 |
// impossible sequences |
| 56 |
'11000001 10111111': false, |
| 57 |
'11100000 10011111 10111111': false, |
| 58 |
'11000001 10000000': false, |
| 59 |
'11100000 10010000 10000000': false, |
| 60 |
|
| 61 |
// maximum chars with given length |
| 62 |
'11011111 10111111': true, |
| 63 |
'11101111 10111111 10111111': true, |
| 64 |
|
| 65 |
'11110000 10010000 10000000 10000000': true, |
| 66 |
'11110000 10010000 10001111 10001111': true, |
| 67 |
'11110100 10001111 10110000 10000000': true, |
| 68 |
'11110100 10001111 10111111 10111111': true, |
| 69 |
|
| 70 |
// too low |
| 71 |
'11110000 10001111 10111111 10111111': false, |
| 72 |
|
| 73 |
// too high |
| 74 |
'11110100 10010000 10000000 10000000': false, |
| 75 |
'11110100 10011111 10111111 10111111': false, |
| 76 |
|
| 77 |
// surrogate range |
| 78 |
'11101101 10011111 10111111': true, |
| 79 |
'11101101 10100000 10000000': false, |
| 80 |
'11101101 10111111 10111111': false, |
| 81 |
'11101110 10000000 10000000': true |
| 82 |
}; |
| 83 |
|
| 84 |
describe('decode', function() { |
| 85 |
it('should decode %xx', function() { |
| 86 |
assert.equal(decode('x%20xx%20%2520'), 'x xx %20'); |
| 87 |
}); |
| 88 |
|
| 89 |
it('should not decode invalid sequences', function() { |
| 90 |
assert.equal(decode('%2g%z1%%'), '%2g%z1%%'); |
| 91 |
}); |
| 92 |
|
| 93 |
it('should not decode reservedSet', function() { |
| 94 |
assert.equal(decode('%20%25%20', '%'), ' %25 '); |
| 95 |
assert.equal(decode('%20%25%20', ' '), '%20%%20'); |
| 96 |
assert.equal(decode('%20%25%20', ' %'), '%20%25%20'); |
| 97 |
}); |
| 98 |
|
| 99 |
describe('utf8', function() { |
| 100 |
Object.keys(samples).forEach(function(k) { |
| 101 |
it(k, function() { |
| 102 |
var res1, res2, |
| 103 |
er = null, |
| 104 |
str = encodeBinary(k); |
| 105 |
|
| 106 |
try { |
| 107 |
res1 = decodeURIComponent(str); |
| 108 |
} catch(e) { |
| 109 |
er = e; |
| 110 |
} |
| 111 |
|
| 112 |
res2 = decode(str); |
| 113 |
|
| 114 |
if (er) { |
| 115 |
assert.notEqual(res2.indexOf('\ufffd'), -1); |
| 116 |
} else { |
| 117 |
assert.equal(res1, res2); |
| 118 |
assert.equal(res2.indexOf('\ufffd'), -1); |
| 119 |
} |
| 120 |
}); |
| 121 |
}); |
| 122 |
}); |
| 123 |
}); |