Files
palemoon27/dom/base/test/unit/test_chromeutils_base64.js
T
roytam1 c2bcdec5c9 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1261009 - Remove the Data Store API, r=fabrice (b22e580107)
- Bug 1268393 - Some compilation issues in ServiceWorker code, r=ehsan (d9c2f2554b)
- Bug 1209095 - Accept opaqueredirection fetch results if the request redirection type is manual. r=bkelly (6fe92d1368)
- Bug 1267733 P2 Pass ServiceWorkerRegistrationInfo down to CancelChannelRunnable. r=jdm (0ec51f09ef)
- Bug 1267733 P3 Trigger service worker update after failed interception. r=jdm (f89a7998d4)
- Bug 1267733 P4 Add a wpt test that verifies a service worker update can recover from a broken navigation interception. r=jdm (9dc0ce97bd)
- Bug 1267691: Assert on failed attempts to shutdown a thread from itself r=froyd (0cbd1e458c)
- Bug 1180533 - Disable BackgroundHangMonitor on gonk (a2d666e741)
- Bug 1121216 - disable BackgroundHangMonitor for TSan builds; r=jchen (ef15d1016f)
- Bug 1265621 - Use StaticRefPtr in Omnijar.cpp; r=froydnj (81bc32836e)
- Bug 1265621 - Expose outer zip readers in Omnijar::GetReader; r=froydnj (ce3f82929e)
- Bug 1267021 - Use fallible allocation and move semantics for Push events. r=wchen (3a1ae23d8d)
- Bug 1222899 - Handle geolocation-device-events callback. r=kchen (a33bcf4297)
- Bug 1237831 - Update GonkGPSGeolocationProvider.cpp to use B2G-style. r=jst (d389eedf47)
- Bug 1245033 - Build break in dom/system/gonk/GonkGPSGeolocationProvider.cpp:541:126: error: format '%d' expects argument of type 'int', but argument 5 has type 'nsresult'. r=fabrice (ecde789edf)
- Bug 1264287: Convert Wifi to use |UniquePtr|, r=nfroyd (9bad7792bf)
- Bug 1267577 - Move nsRunnable to mozilla::Runnable. r=gsvelto (f58e2161f2)
- Bug 1210370 - Close wpa_supplicant before we shutdown nsIWifiProxyService. r=mrbkap (5cd4dce58f)
- Bug 1218629 - Save audio volume for each device to setting db r=alwu (2f1847dd6f)
- Bug 1249437 - Remove workaround of volume control r=alwu (13cd144a89)
- Bug 1268432: Replace |Task| with |Runnable| in B2G code r=fabrice (bcc768e9cb)
- Bug 1226483 - Add ASSERT check to AudioManager::SelectDeviceFromDevices() r=alwu (446e8f634e)
- Bug 1229234 - Enable audio_is_output_device() on ICS r=alwu (84aae07f23)
- Bug 1267369 - Only generate typelib data for scriptable interfaces; r=khuey (e49b44c9ce)
- Bug 1155969 - Make runtests.py flake8 compliant. r=ted (1de456b206)
- Bug 1266569 - Avoid including the ChromeUtils binding in Base64.h. r=froydnj (7ba39a7687)
2024-08-29 00:00:08 +08:00

106 lines
3.2 KiB
JavaScript

"use strict";
function run_test() {
test_base64URLEncode();
test_base64URLDecode();
}
// Test vectors from RFC 4648, section 10.
let textTests = {
"": "",
"f": "Zg",
"fo": "Zm8",
"foo": "Zm9v",
"foob": "Zm9vYg",
"fooba": "Zm9vYmE",
"foobar": "Zm9vYmFy",
}
// Examples from RFC 4648, section 9.
let binaryTests = [{
decoded: new Uint8Array([0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e]),
encoded: "FPucA9l-",
}, {
decoded: new Uint8Array([0x14, 0xfb, 0x9c, 0x03, 0xd9]),
encoded: "FPucA9k",
}, {
decoded: new Uint8Array([0x14, 0xfb, 0x9c, 0x03]),
encoded: "FPucAw",
}];
function padEncodedValue(value) {
switch (value.length % 4) {
case 0:
return value;
case 2:
return value + "==";
case 3:
return value + "=";
default:
throw new TypeError("Invalid encoded value");
}
}
function testEncode(input, encoded) {
equal(ChromeUtils.base64URLEncode(input, { pad: false }),
encoded, encoded + " without padding");
equal(ChromeUtils.base64URLEncode(input, { pad: true }),
padEncodedValue(encoded), encoded + " with padding");
}
function test_base64URLEncode() {
throws(_ => ChromeUtils.base64URLEncode(new Uint8Array(0)), /TypeError/,
"Should require encoding options");
throws(_ => ChromeUtils.base64URLEncode(new Uint8Array(0), {}), /TypeError/,
"Encoding should require the padding option");
for (let {decoded, encoded} of binaryTests) {
testEncode(decoded, encoded);
}
let textEncoder = new TextEncoder("utf-8");
for (let decoded of Object.keys(textTests)) {
let input = textEncoder.encode(decoded);
testEncode(input, textTests[decoded]);
}
}
function testDecode(input, decoded) {
let buffer = ChromeUtils.base64URLDecode(input, { padding: "reject" });
deepEqual(new Uint8Array(buffer), decoded, input + " with padding rejected");
let paddedValue = padEncodedValue(input);
buffer = ChromeUtils.base64URLDecode(paddedValue, { padding: "ignore" });
deepEqual(new Uint8Array(buffer), decoded, input + " with padding ignored");
if (paddedValue.length > input.length) {
throws(_ => ChromeUtils.base64URLDecode(paddedValue, { padding: "reject" }),
paddedValue + " with padding rejected should throw");
throws(_ => ChromeUtils.base64URLDecode(input, { padding: "require" }),
input + " with padding required should throw");
buffer = ChromeUtils.base64URLDecode(paddedValue, { padding: "require" });
deepEqual(new Uint8Array(buffer), decoded, paddedValue + " with padding required");
}
}
function test_base64URLDecode() {
throws(_ => ChromeUtils.base64URLDecode(""), /TypeError/,
"Should require decoding options");
throws(_ => ChromeUtils.base64URLDecode("", {}), /TypeError/,
"Decoding should require the padding option");
throws(_ => ChromeUtils.base64URLDecode("", { padding: "chocolate" }),
"Decoding should throw for invalid padding policy");
for (let {decoded, encoded} of binaryTests) {
testDecode(encoded, decoded);
}
let textEncoder = new TextEncoder("utf-8");
for (let decoded of Object.keys(textTests)) {
let expectedBuffer = textEncoder.encode(decoded);
testDecode(textTests[decoded], expectedBuffer);
}
}