last-chunk-invalid.js (2925B)
1 // |reftest| skip-if(!Uint8Array.fromBase64) -- uint8array-base64 is not enabled unconditionally 2 // Copyright (C) 2025 Nikita Skovoroda. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 /*--- 5 esid: sec-uint8array.frombase64 6 description: Uint8Array.fromBase64 throws a SyntaxError when chunk size is invalid or padding is invalid 7 features: [uint8array-base64, TypedArray] 8 includes: [compareArray.js] 9 ---*/ 10 11 // Non-padded incomplete chunk 'A' 12 assert.throws(SyntaxError, function() { 13 Uint8Array.fromBase64('A'); 14 }); 15 assert.throws(SyntaxError, function() { 16 Uint8Array.fromBase64('A', { lastChunkHandling: 'loose' }); 17 }); 18 assert.throws(SyntaxError, function() { 19 Uint8Array.fromBase64('A', { lastChunkHandling: 'strict' }); 20 }); 21 assert.compareArray(Uint8Array.fromBase64('A', { lastChunkHandling: 'stop-before-partial' }), []); 22 23 // Non-padded incomplete chunk 'ABCDA' 24 assert.throws(SyntaxError, function() { 25 Uint8Array.fromBase64('ABCDA'); 26 }); 27 assert.throws(SyntaxError, function() { 28 Uint8Array.fromBase64('ABCDA', { lastChunkHandling: 'loose' }); 29 }); 30 assert.throws(SyntaxError, function() { 31 Uint8Array.fromBase64('ABCDA', { lastChunkHandling: 'strict' }); 32 }); 33 assert.compareArray(Uint8Array.fromBase64('ABCDA', { lastChunkHandling: 'stop-before-partial' }), [0, 16, 131]); 34 35 // Incomplete padding in chunk 'AA=' is allowed but skipped in 'stop-before-partial', but not other modes 36 assert.throws(SyntaxError, function() { 37 Uint8Array.fromBase64('AA='); 38 }); 39 assert.throws(SyntaxError, function() { 40 Uint8Array.fromBase64('AA=', { lastChunkHandling: 'loose' }); 41 }); 42 assert.throws(SyntaxError, function() { 43 Uint8Array.fromBase64('AA=', { lastChunkHandling: 'strict' }); 44 }); 45 assert.compareArray(Uint8Array.fromBase64('AA=', { lastChunkHandling: 'stop-before-partial' }), []); 46 assert.compareArray(Uint8Array.fromBase64('aQ=', { lastChunkHandling: 'stop-before-partial' }), []); 47 assert.compareArray(Uint8Array.fromBase64('ABCDAA=', { lastChunkHandling: 'stop-before-partial' }), [0, 16, 131]); 48 49 // Padded chunks always throw when incomplete before padding 50 var illegal = [ 51 '=', 52 '==', 53 '===', 54 '====', 55 '=====', 56 'A=', 57 'A==', 58 'A===', 59 'A====', 60 'A=====', 61 'AA====', 62 'AA=====', 63 'AAA==', 64 'AAA===', 65 'AAA====', 66 'AAA=====', 67 'AAAA=', 68 'AAAA==', 69 'AAAA===', 70 'AAAA====', 71 'AAAA=====', 72 'AAAAA=', 73 'AAAAA==', 74 'AAAAA===', 75 'AAAAA====', 76 'AAAAA=====', 77 ]; 78 79 illegal.forEach(function(value) { 80 assert.throws(SyntaxError, function() { 81 Uint8Array.fromBase64(value); 82 }); 83 assert.throws(SyntaxError, function() { 84 Uint8Array.fromBase64(value, { lastChunkHandling: 'loose' }); 85 }); 86 assert.throws(SyntaxError, function() { 87 Uint8Array.fromBase64(value, { lastChunkHandling: 'strict' }); 88 }); 89 assert.throws(SyntaxError, function() { 90 Uint8Array.fromBase64(value, { lastChunkHandling: 'stop-before-partial' }); 91 }); 92 }); 93 94 reportCompare(0, 0);