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.
5 esid: sec-uint8array.prototype.setfrombase64
6 description: Handling of final chunks in target.setFromBase64
7 includes: [compareArray.js]
8 features: [uint8array-base64, TypedArray]
12 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
13 var result = target.setFromBase64('ZXhhZg==');
14 assert.sameValue(result.read, 8);
15 assert.sameValue(result.written, 4);
16 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
18 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
19 var result = target.setFromBase64('ZXhhZg==', { lastChunkHandling: 'loose' });
20 assert.sameValue(result.read, 8);
21 assert.sameValue(result.written, 4);
22 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
24 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
25 var result = target.setFromBase64('ZXhhZg==', { lastChunkHandling: 'stop-before-partial' });
26 assert.sameValue(result.read, 8);
27 assert.sameValue(result.written, 4);
28 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
30 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
31 var result = target.setFromBase64('ZXhhZg==', { lastChunkHandling: 'strict' });
32 assert.sameValue(result.read, 8);
33 assert.sameValue(result.written, 4);
34 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
38 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
39 var result = target.setFromBase64('ZXhhZg');
40 assert.sameValue(result.read, 6);
41 assert.sameValue(result.written, 4);
42 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
44 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
45 var result = target.setFromBase64('ZXhhZg', { lastChunkHandling: 'loose' });
46 assert.sameValue(result.read, 6);
47 assert.sameValue(result.written, 4);
48 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
50 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
51 var result = target.setFromBase64('ZXhhZg', { lastChunkHandling: 'stop-before-partial' });
52 assert.sameValue(result.read, 4);
53 assert.sameValue(result.written, 3);
54 assert.compareArray(target, [101, 120, 97, 255, 255, 255]);
56 assert.throws(SyntaxError, function() {
57 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
58 target.setFromBase64('ZXhhZg', { lastChunkHandling: 'strict' });
62 // non-zero padding bits
63 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
64 var result = target.setFromBase64('ZXhhZh==');
65 assert.sameValue(result.read, 8);
66 assert.sameValue(result.written, 4);
67 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
69 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
70 var result = target.setFromBase64('ZXhhZh==', { lastChunkHandling: 'loose' });
71 assert.sameValue(result.read, 8);
72 assert.sameValue(result.written, 4);
73 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
75 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
76 var result = target.setFromBase64('ZXhhZh==', { lastChunkHandling: 'stop-before-partial' });
77 assert.sameValue(result.read, 8);
78 assert.sameValue(result.written, 4);
79 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
81 assert.throws(SyntaxError, function() {
82 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
83 target.setFromBase64('ZXhhZh==', { lastChunkHandling: 'strict' });
87 // non-zero padding bits, no padding
88 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
89 var result = target.setFromBase64('ZXhhZh');
90 assert.sameValue(result.read, 6);
91 assert.sameValue(result.written, 4);
92 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
94 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
95 var result = target.setFromBase64('ZXhhZh', { lastChunkHandling: 'loose' });
96 assert.sameValue(result.read, 6);
97 assert.sameValue(result.written, 4);
98 assert.compareArray(target, [101, 120, 97, 102, 255, 255]);
100 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
101 var result = target.setFromBase64('ZXhhZh', { lastChunkHandling: 'stop-before-partial' });
102 assert.sameValue(result.read, 4);
103 assert.sameValue(result.written, 3);
104 assert.compareArray(target, [101, 120, 97, 255, 255, 255]);
106 assert.throws(SyntaxError, function() {
107 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
108 target.setFromBase64('ZXhhZh', { lastChunkHandling: 'strict' });
113 assert.throws(SyntaxError, function() {
114 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
115 target.setFromBase64('ZXhhZg=');
118 assert.throws(SyntaxError, function() {
119 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
120 target.setFromBase64('ZXhhZg=', { lastChunkHandling: 'loose' });
123 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
124 var result = target.setFromBase64('ZXhhZg=', { lastChunkHandling: 'stop-before-partial' });
125 assert.sameValue(result.read, 4);
126 assert.sameValue(result.written, 3);
127 assert.compareArray(target, [101, 120, 97, 255, 255, 255]);
129 assert.throws(SyntaxError, function() {
130 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
131 target.setFromBase64('ZXhhZg=', { lastChunkHandling: 'strict' });
136 assert.throws(SyntaxError, function() {
137 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
138 target.setFromBase64('ZXhhZg===');
141 assert.throws(SyntaxError, function() {
142 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
143 target.setFromBase64('ZXhhZg===', { lastChunkHandling: 'loose' });
146 assert.throws(SyntaxError, function() {
147 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
148 target.setFromBase64('ZXhhZg===', { lastChunkHandling: 'stop-before-partial' });
151 assert.throws(SyntaxError, function() {
152 var target = new Uint8Array([255, 255, 255, 255, 255, 255]);
153 target.setFromBase64('ZXhhZg===', { lastChunkHandling: 'strict' });