JavaScript generic ASN.1 parser (mirror)
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add Base64 encoding tests.

+39 -11
+1 -1
asn1.js
··· 58 58 59 59 /** Class to manage a stream of bytes, with a zero-copy approach. 60 60 * It uses an existing array or binary string and advances a position index. */ 61 - class Stream { 61 + export class Stream { 62 62 63 63 /** 64 64 * @param {Stream|array|string} enc data (will not be copied)
+38 -10
test.js
··· 1 1 #!/usr/bin/env node 2 2 3 - import { ASN1 } from './asn1.js'; 3 + import { ASN1, Stream } from './asn1.js'; 4 4 import { Hex } from './hex.js'; 5 + import { Base64 } from './base64.js'; 5 6 6 7 const 7 8 all = (process.argv[2] == 'all'); ··· 90 91 ['171E83C1B251803F86DD01E9CFA886BE89A7316D8372649AC2231EC669F81A84', /^Exception:\nError: Unrecognized time: /, 'Invalid UTCTime'], // GitHub issue #79 91 92 ]; 92 93 94 + const testsB64 = [ 95 + 'AA==', 96 + 'ABA=', 97 + 'ABCD', 98 + 'ABCDEA==', 99 + 'ABCDEFE=', 100 + 'ABCDEFGH', 101 + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQR\nSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456w==', 102 + ]; 103 + 104 + function check(result, expected, comment) { 105 + if (!result || result == expected) { 106 + if (all) console.log('\x1B[1m\x1B[32mOK \x1B[39m\x1B[22m ' + comment); 107 + return true; 108 + } else { 109 + console.log('\x1B[1m\x1B[31mERR\x1B[39m\x1B[22m ' + comment); 110 + console.log(' \x1B[1m\x1B[34mEXP\x1B[39m\x1B[22m ' + expected.toString().replace(/\n/g, '\n ')); 111 + console.log(' \x1B[1m\x1B[33mGOT\x1B[39m\x1B[22m ' + result.replace(/\n/g, '\n ')); 112 + return false; 113 + } 114 + } 115 + 93 116 let 94 117 run = 0, 95 118 expErr = 0, 96 119 error = 0; 97 - tests.forEach(function (t) { 120 + for (let t of tests) { 98 121 const input = t[0], 99 122 expected = t[1], 100 123 comment = t[2]; ··· 112 135 if (expected instanceof RegExp) 113 136 result = expected.test(result) ? null : 'does not match'; 114 137 ++run; 115 - if (!result || result == expected) { 116 - if (all) console.log('\x1B[1m\x1B[32mOK \x1B[39m\x1B[22m ' + comment); 117 - } else { 138 + if (!check(result, expected, comment)) 139 + ++error; 140 + } 141 + for (let t of testsB64) { 142 + let bin = Base64.decode(t); 143 + let url = new Stream(bin, 0).b64Dump(0, bin.length); 144 + ++run; 145 + if (!check(url, t.replace(/\n/g, '').replace(/=*$/g, ''), 'Base64url: ' + bin.length + ' bytes')) 146 + ++error; 147 + let std = Base64.pretty(url); 148 + ++run; 149 + if (!check(std, t, 'Base64: ' + bin.length + ' bytes')) 118 150 ++error; 119 - console.log('\x1B[1m\x1B[31mERR\x1B[39m\x1B[22m ' + comment); 120 - console.log(' \x1B[1m\x1B[34mEXP\x1B[39m\x1B[22m ' + expected.toString().replace(/\n/g, '\n ')); 121 - console.log(' \x1B[1m\x1B[33mGOT\x1B[39m\x1B[22m ' + result.replace(/\n/g, '\n ')); 122 - } 123 - }); 151 + } 124 152 console.log(run + ' tested, ' + expErr + ' expected, ' + error + ' errors.'); 125 153 process.exit(error ? 1 : 0);