Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for later base64 changes #4133

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-uint8array.prototype.setfrombase64
description: Uint8Array.prototype.setFromBase64 decodes and writes chunks which occur prior to bad data
features: [uint8array-base64, TypedArray]
---*/

var target = new Uint8Array([255, 255, 255, 255, 255]);
assert.throws(SyntaxError, function() {
target.setFromBase64("MjYyZm.9v");
}, "illegal character in second chunk");
assert.compareArray(target, [50, 54, 50, 255, 255], "decoding from MjYyZm.9v should only write the valid chunks");

target = new Uint8Array([255, 255, 255, 255, 255]);
assert.throws(SyntaxError, function() {
target.setFromBase64("MjYyZg", { lastChunkHandling: "strict" });
}, "padding omitted with lastChunkHandling: strict");
assert.compareArray(target, [50, 54, 50, 255, 255], "decoding from MjYyZg should only write the valid chunks");

target = new Uint8Array([255, 255, 255, 255, 255]);
assert.throws(SyntaxError, function() {
target.setFromBase64("MjYyZg===");
}, "extra characters after padding");
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 number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-uint8array.prototype.setfromhex
description: Uint8Array.prototype.setFromHex decodes and writes pairs which occur prior to bad data
features: [uint8array-base64, TypedArray]
---*/

var illegal = [
'aaa ',
'aaag',
];
illegal.forEach(function(value) {
var target = new Uint8Array([255, 255, 255, 255, 255]);
assert.throws(SyntaxError, function() {
target.setFromHex(value);
});
assert.compareArray(target, [170, 255, 255, 255, 255], "decoding from " + value);
});

var target = new Uint8Array([255, 255, 255, 255, 255]);
assert.throws(SyntaxError, function() {
target.setFromHex('aaa');
});
assert.compareArray(target, [255, 255, 255, 255, 255], "when length is odd no data is written");
23 changes: 23 additions & 0 deletions test/built-ins/Uint8Array/prototype/toBase64/omit-padding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-uint8array.prototype.tobase64
description: Conversion of Uint8Arrays to base64 strings exercising the omitPadding option
features: [uint8array-base64, TypedArray]
---*/

// works with default alphabet
assert.sameValue((new Uint8Array([199, 239])).toBase64(), "x+8=");
assert.sameValue((new Uint8Array([199, 239])).toBase64({ omitPadding: false }), "x+8=");
assert.sameValue((new Uint8Array([199, 239])).toBase64({ omitPadding: true }), "x+8");
assert.sameValue((new Uint8Array([255])).toBase64({ omitPadding: true }), "/w");

// works with base64url alphabet
assert.sameValue((new Uint8Array([199, 239])).toBase64({ alphabet: "base64url" }), "x-8=");
assert.sameValue((new Uint8Array([199, 239])).toBase64({ alphabet: "base64url", omitPadding: false }), "x-8=");
assert.sameValue((new Uint8Array([199, 239])).toBase64({ alphabet: "base64url", omitPadding: true }), "x-8");
assert.sameValue((new Uint8Array([255])).toBase64({ alphabet: "base64url", omitPadding: true }), "_w");

// performs ToBoolean on the argument
assert.sameValue((new Uint8Array([255])).toBase64({ omitPadding: 0 }), "/w==");
assert.sameValue((new Uint8Array([255])).toBase64({ omitPadding: 1 }), "/w");
Loading