|
| 1 | +// Copyright (C) 2025 the V8 project authors. All rights reserved. |
| 2 | +// This code is governed by the BSD license found in the LICENSE file. |
| 3 | + |
| 4 | +/*--- |
| 5 | +esid: sec-uint8array.frombase64 |
| 6 | +description: > |
| 7 | + Uint8Array.fromBase64 throws a TypeError when alphabet and |
| 8 | + lastChunkHandling are invalid string values. |
| 9 | +includes: [compareArray.js] |
| 10 | +features: [uint8array-base64, TypedArray] |
| 11 | +---*/ |
| 12 | + |
| 13 | +let string = 'SGVsbG8gV29ybGQ='; |
| 14 | +assert.compareArray( |
| 15 | + Uint8Array.fromBase64(string), |
| 16 | + [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]) |
| 17 | + |
| 18 | +// invalid alphabet ----- |
| 19 | + |
| 20 | +// shorter length |
| 21 | +assert.throws(TypeError, function() { |
| 22 | + Uint8Array.fromBase64(string, {alphabet: 'base'}); |
| 23 | +}); |
| 24 | +// same length but invalid value |
| 25 | +assert.throws(TypeError, function() { |
| 26 | + Uint8Array.fromBase64(string, {alphabet: 'base65'}); |
| 27 | +}); |
| 28 | +// longer length |
| 29 | +assert.throws(TypeError, function() { |
| 30 | + Uint8Array.fromBase64(string, {alphabet: 'base64urlurl'}); |
| 31 | +}); |
| 32 | +// invalid two-byte value |
| 33 | +assert.throws(TypeError, function() { |
| 34 | + Uint8Array.fromBase64(string, {alphabet: '☉‿⊙'}); |
| 35 | + }); |
| 36 | + |
| 37 | +// invalid lastChunkHandling ----- |
| 38 | + |
| 39 | +// shorter length |
| 40 | +assert.throws(TypeError, function() { |
| 41 | + Uint8Array.fromBase64(string, {lastChunkHandling: 'stric'}); |
| 42 | +}); |
| 43 | +// same length but invalid value |
| 44 | +assert.throws(TypeError, function() { |
| 45 | + Uint8Array.fromBase64(string, {lastChunkHandling: 'looss'}); |
| 46 | +}); |
| 47 | +// longer length |
| 48 | +assert.throws(TypeError, function() { |
| 49 | + Uint8Array.fromBase64( |
| 50 | + string, {lastChunkHandling: 'stop-before-partial-partial'}); |
| 51 | +}); |
| 52 | +// invalid two-byte value |
| 53 | +assert.throws(TypeError, function() { |
| 54 | + Uint8Array.fromBase64(string, {lastChunkHandling: '☉‿⊙'}); |
| 55 | + }); |
0 commit comments