Bug 1885337 - Part 3: Import tests from test262 PR. r=spidermonkey-reviewers,dminor
[gecko.git] / js / src / tests / test262 / prs / 3994 / built-ins / Uint8Array / fromBase64 / last-chunk-handling.js
blobc78d15e8694831014dbd4eda9213214a08cf4a2f
1 // |reftest| shell-option(--enable-uint8array-base64) skip-if(!Uint8Array.fromBase64||!xulRuntime.shell) -- uint8array-base64 is not enabled unconditionally, requires shell-options
2 // Copyright (C) 2024 Kevin Gibbons. 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: Handling of final chunks in Uint8Array.fromBase64
7 includes: [compareArray.js]
8 features: [uint8array-base64, TypedArray]
9 ---*/
11 // padding
12 assert.compareArray(Uint8Array.fromBase64('ZXhhZg=='), [101, 120, 97, 102]);
13 assert.compareArray(Uint8Array.fromBase64('ZXhhZg==', { lastChunkHandling: 'loose' }), [101, 120, 97, 102]);
14 assert.compareArray(Uint8Array.fromBase64('ZXhhZg==', { lastChunkHandling: 'stop-before-partial' }), [101, 120, 97, 102]);
15 assert.compareArray(Uint8Array.fromBase64('ZXhhZg==', { lastChunkHandling: 'strict' }), [101, 120, 97, 102]);
17 // no padding
18 assert.compareArray(Uint8Array.fromBase64('ZXhhZg'), [101, 120, 97, 102]);
19 assert.compareArray(Uint8Array.fromBase64('ZXhhZg', { lastChunkHandling: 'loose' }), [101, 120, 97, 102]);
20 assert.compareArray(Uint8Array.fromBase64('ZXhhZg', { lastChunkHandling: 'stop-before-partial' }), [101, 120, 97]);
21 assert.throws(SyntaxError, function() {
22   Uint8Array.fromBase64('ZXhhZg', { lastChunkHandling: 'strict' });
23 });
25 // non-zero padding bits
26 assert.compareArray(Uint8Array.fromBase64('ZXhhZh=='), [101, 120, 97, 102]);
27 assert.compareArray(Uint8Array.fromBase64('ZXhhZh==', { lastChunkHandling: 'loose' }), [101, 120, 97, 102]);
28 assert.compareArray(Uint8Array.fromBase64('ZXhhZh==', { lastChunkHandling: 'stop-before-partial' }), [101, 120, 97, 102]);
29 assert.throws(SyntaxError, function() {
30   Uint8Array.fromBase64('ZXhhZh==', { lastChunkHandling: 'strict' });
31 });
33 // non-zero padding bits, no padding
34 assert.compareArray(Uint8Array.fromBase64('ZXhhZh'), [101, 120, 97, 102]);
35 assert.compareArray(Uint8Array.fromBase64('ZXhhZh', { lastChunkHandling: 'loose' }), [101, 120, 97, 102]);
36 assert.compareArray(Uint8Array.fromBase64('ZXhhZh', { lastChunkHandling: 'stop-before-partial' }), [101, 120, 97]);
37 assert.throws(SyntaxError, function() {
38   Uint8Array.fromBase64('ZXhhZh', { lastChunkHandling: 'strict' });
39 });
41 // partial padding
42 assert.throws(SyntaxError, function() {
43   Uint8Array.fromBase64('ZXhhZg=');
44 });
45 assert.throws(SyntaxError, function() {
46   Uint8Array.fromBase64('ZXhhZg=', { lastChunkHandling: 'loose' });
47 });
48 assert.compareArray(Uint8Array.fromBase64('ZXhhZg=', { lastChunkHandling: 'stop-before-partial' }), [101, 120, 97]);
49 assert.throws(SyntaxError, function() {
50   Uint8Array.fromBase64('ZXhhZg=', { lastChunkHandling: 'strict' });
51 });
53 // excess padding
54 assert.throws(SyntaxError, function() {
55   Uint8Array.fromBase64('ZXhhZg===');
56 });
57 assert.throws(SyntaxError, function() {
58   Uint8Array.fromBase64('ZXhhZg===', { lastChunkHandling: 'loose' });
59 });
60 assert.throws(SyntaxError, function() {
61   Uint8Array.fromBase64('ZXhhZg===', { lastChunkHandling: 'stop-before-partial' });
62 });
63 assert.throws(SyntaxError, function() {
64   Uint8Array.fromBase64('ZXhhZg===', { lastChunkHandling: 'strict' });
65 });
67 reportCompare(0, 0);