Skip to content

Commit 08e1aa8

Browse files
bakkotljharb
andauthored
Tests for later base64 changes (#4133)
Co-authored-by: Jordan Harband <[email protected]>
1 parent 9976008 commit 08e1aa8

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-uint8array.prototype.setfrombase64
5+
description: Uint8Array.prototype.setFromBase64 decodes and writes chunks which occur prior to bad data
6+
features: [uint8array-base64, TypedArray]
7+
---*/
8+
9+
var target = new Uint8Array([255, 255, 255, 255, 255]);
10+
assert.throws(SyntaxError, function() {
11+
target.setFromBase64("MjYyZm.9v");
12+
}, "illegal character in second chunk");
13+
assert.compareArray(target, [50, 54, 50, 255, 255], "decoding from MjYyZm.9v should only write the valid chunks");
14+
15+
target = new Uint8Array([255, 255, 255, 255, 255]);
16+
assert.throws(SyntaxError, function() {
17+
target.setFromBase64("MjYyZg", { lastChunkHandling: "strict" });
18+
}, "padding omitted with lastChunkHandling: strict");
19+
assert.compareArray(target, [50, 54, 50, 255, 255], "decoding from MjYyZg should only write the valid chunks");
20+
21+
target = new Uint8Array([255, 255, 255, 255, 255]);
22+
assert.throws(SyntaxError, function() {
23+
target.setFromBase64("MjYyZg===");
24+
}, "extra characters after padding");
25+
assert.compareArray(target, [50, 54, 50, 255, 255], "decoding from MjYyZg=== should not write the last chunk because it has extra padding");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-uint8array.prototype.setfromhex
5+
description: Uint8Array.prototype.setFromHex decodes and writes pairs which occur prior to bad data
6+
features: [uint8array-base64, TypedArray]
7+
---*/
8+
9+
var illegal = [
10+
'aaa ',
11+
'aaag',
12+
];
13+
illegal.forEach(function(value) {
14+
var target = new Uint8Array([255, 255, 255, 255, 255]);
15+
assert.throws(SyntaxError, function() {
16+
target.setFromHex(value);
17+
});
18+
assert.compareArray(target, [170, 255, 255, 255, 255], "decoding from " + value);
19+
});
20+
21+
var target = new Uint8Array([255, 255, 255, 255, 255]);
22+
assert.throws(SyntaxError, function() {
23+
target.setFromHex('aaa');
24+
});
25+
assert.compareArray(target, [255, 255, 255, 255, 255], "when length is odd no data is written");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-uint8array.prototype.tobase64
5+
description: Conversion of Uint8Arrays to base64 strings exercising the omitPadding option
6+
features: [uint8array-base64, TypedArray]
7+
---*/
8+
9+
// works with default alphabet
10+
assert.sameValue((new Uint8Array([199, 239])).toBase64(), "x+8=");
11+
assert.sameValue((new Uint8Array([199, 239])).toBase64({ omitPadding: false }), "x+8=");
12+
assert.sameValue((new Uint8Array([199, 239])).toBase64({ omitPadding: true }), "x+8");
13+
assert.sameValue((new Uint8Array([255])).toBase64({ omitPadding: true }), "/w");
14+
15+
// works with base64url alphabet
16+
assert.sameValue((new Uint8Array([199, 239])).toBase64({ alphabet: "base64url" }), "x-8=");
17+
assert.sameValue((new Uint8Array([199, 239])).toBase64({ alphabet: "base64url", omitPadding: false }), "x-8=");
18+
assert.sameValue((new Uint8Array([199, 239])).toBase64({ alphabet: "base64url", omitPadding: true }), "x-8");
19+
assert.sameValue((new Uint8Array([255])).toBase64({ alphabet: "base64url", omitPadding: true }), "_w");
20+
21+
// performs ToBoolean on the argument
22+
assert.sameValue((new Uint8Array([255])).toBase64({ omitPadding: 0 }), "/w==");
23+
assert.sameValue((new Uint8Array([255])).toBase64({ omitPadding: 1 }), "/w");

0 commit comments

Comments
 (0)