From 701b220976776948e693834150945f210b4be5eb Mon Sep 17 00:00:00 2001 From: Rezvan Mahdavi Hezaveh Date: Tue, 21 Jan 2025 14:20:45 -0800 Subject: [PATCH] [base64] Add FromBase64 This CL adds `Uint8Array.fromBase64()`. Bug: 42204568 Change-Id: Ib68683a2d5ead9720077197c0f895787214b183e Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6026354 Commit-Queue: Rezvan Mahdavi Hezaveh Reviewed-by: Shu-yu Guo Cr-Commit-Position: refs/heads/main@{#98239} --- .../Uint8Array/fromBase64/invalid-options.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/staging/Uint8Array/fromBase64/invalid-options.js diff --git a/test/staging/Uint8Array/fromBase64/invalid-options.js b/test/staging/Uint8Array/fromBase64/invalid-options.js new file mode 100644 index 0000000000..0f120249ba --- /dev/null +++ b/test/staging/Uint8Array/fromBase64/invalid-options.js @@ -0,0 +1,55 @@ +// Copyright (C) 2025 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-uint8array.frombase64 +description: > + Uint8Array.fromBase64 throws a TypeError when alphabet and + lastChunkHandling are invalid string values. +includes: [compareArray.js] +features: [uint8array-base64, TypedArray] +---*/ + +let string = 'SGVsbG8gV29ybGQ='; +assert.compareArray( + Uint8Array.fromBase64(string), + [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]) + +// invalid alphabet ----- + +// shorter length +assert.throws(TypeError, function() { + Uint8Array.fromBase64(string, {alphabet: 'base'}); +}); +// same length but invalid value +assert.throws(TypeError, function() { + Uint8Array.fromBase64(string, {alphabet: 'base65'}); +}); +// longer length +assert.throws(TypeError, function() { + Uint8Array.fromBase64(string, {alphabet: 'base64urlurl'}); +}); +// invalid two-byte value +assert.throws(TypeError, function() { + Uint8Array.fromBase64(string, {alphabet: '☉‿⊙'}); + }); + +// invalid lastChunkHandling ----- + +// shorter length +assert.throws(TypeError, function() { + Uint8Array.fromBase64(string, {lastChunkHandling: 'stric'}); +}); +// same length but invalid value +assert.throws(TypeError, function() { + Uint8Array.fromBase64(string, {lastChunkHandling: 'looss'}); +}); +// longer length +assert.throws(TypeError, function() { + Uint8Array.fromBase64( + string, {lastChunkHandling: 'stop-before-partial-partial'}); +}); +// invalid two-byte value +assert.throws(TypeError, function() { + Uint8Array.fromBase64(string, {lastChunkHandling: '☉‿⊙'}); + });