Skip to content

Commit 505f9c9

Browse files
jasnellruyadorno
authored andcommitted
test: app atob web platform tests
Signed-off-by: James M Snell <[email protected]> PR-URL: #37529 Fixes: #3462 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent dc9cd43 commit 505f9c9

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

test/fixtures/wpt/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Last update:
1616
- encoding: https://github.com/web-platform-tests/wpt/tree/35f70910d3/encoding
1717
- FileAPI: https://github.com/web-platform-tests/wpt/tree/3b279420d4/FileAPI
1818
- hr-time: https://github.com/web-platform-tests/wpt/tree/9910784394/hr-time
19+
- html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob
1920
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing
2021
- html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/5873f2d8f1/html/webappapis/timers
2122
- interfaces: https://github.com/web-platform-tests/wpt/tree/79fa4cf76e/interfaces
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* btoa() as defined by the HTML5 spec, which mostly just references RFC4648.
3+
*/
4+
function mybtoa(s) {
5+
// String conversion as required by WebIDL.
6+
s = String(s);
7+
8+
// "The btoa() method must throw an INVALID_CHARACTER_ERR exception if the
9+
// method's first argument contains any character whose code point is
10+
// greater than U+00FF."
11+
for (var i = 0; i < s.length; i++) {
12+
if (s.charCodeAt(i) > 255) {
13+
return "INVALID_CHARACTER_ERR";
14+
}
15+
}
16+
17+
var out = "";
18+
for (var i = 0; i < s.length; i += 3) {
19+
var groupsOfSix = [undefined, undefined, undefined, undefined];
20+
groupsOfSix[0] = s.charCodeAt(i) >> 2;
21+
groupsOfSix[1] = (s.charCodeAt(i) & 0x03) << 4;
22+
if (s.length > i + 1) {
23+
groupsOfSix[1] |= s.charCodeAt(i + 1) >> 4;
24+
groupsOfSix[2] = (s.charCodeAt(i + 1) & 0x0f) << 2;
25+
}
26+
if (s.length > i + 2) {
27+
groupsOfSix[2] |= s.charCodeAt(i + 2) >> 6;
28+
groupsOfSix[3] = s.charCodeAt(i + 2) & 0x3f;
29+
}
30+
for (var j = 0; j < groupsOfSix.length; j++) {
31+
if (typeof groupsOfSix[j] == "undefined") {
32+
out += "=";
33+
} else {
34+
out += btoaLookup(groupsOfSix[j]);
35+
}
36+
}
37+
}
38+
return out;
39+
}
40+
41+
/**
42+
* Lookup table for mybtoa(), which converts a six-bit number into the
43+
* corresponding ASCII character.
44+
*/
45+
function btoaLookup(idx) {
46+
if (idx < 26) {
47+
return String.fromCharCode(idx + 'A'.charCodeAt(0));
48+
}
49+
if (idx < 52) {
50+
return String.fromCharCode(idx - 26 + 'a'.charCodeAt(0));
51+
}
52+
if (idx < 62) {
53+
return String.fromCharCode(idx - 52 + '0'.charCodeAt(0));
54+
}
55+
if (idx == 62) {
56+
return '+';
57+
}
58+
if (idx == 63) {
59+
return '/';
60+
}
61+
// Throw INVALID_CHARACTER_ERR exception here -- won't be hit in the tests.
62+
}
63+
64+
function btoaException(input) {
65+
input = String(input);
66+
for (var i = 0; i < input.length; i++) {
67+
if (input.charCodeAt(i) > 255) {
68+
return true;
69+
}
70+
}
71+
return false;
72+
}
73+
74+
function testBtoa(input) {
75+
// "The btoa() method must throw an INVALID_CHARACTER_ERR exception if the
76+
// method's first argument contains any character whose code point is
77+
// greater than U+00FF."
78+
var normalizedInput = String(input);
79+
for (var i = 0; i < normalizedInput.length; i++) {
80+
if (normalizedInput.charCodeAt(i) > 255) {
81+
assert_throws_dom("InvalidCharacterError", function() { btoa(input); },
82+
"Code unit " + i + " has value " + normalizedInput.charCodeAt(i) + ", which is greater than 255");
83+
return;
84+
}
85+
}
86+
assert_equals(btoa(input), mybtoa(input));
87+
assert_equals(atob(btoa(input)), String(input), "atob(btoa(input)) must be the same as String(input)");
88+
}
89+
90+
var tests = ["עברית", "", "ab", "abc", "abcd", "abcde",
91+
// This one is thrown in because IE9 seems to fail atob(btoa()) on it. Or
92+
// possibly to fail btoa(). I actually can't tell what's happening here,
93+
// but it doesn't hurt.
94+
"\xff\xff\xc0",
95+
// Is your DOM implementation binary-safe?
96+
"\0a", "a\0b",
97+
// WebIDL tests.
98+
undefined, null, 7, 12, 1.5, true, false, NaN, +Infinity, -Infinity, 0, -0,
99+
{toString: function() { return "foo" }},
100+
];
101+
for (var i = 0; i < 258; i++) {
102+
tests.push(String.fromCharCode(i));
103+
}
104+
tests.push(String.fromCharCode(10000));
105+
tests.push(String.fromCharCode(65534));
106+
tests.push(String.fromCharCode(65535));
107+
108+
// This is supposed to be U+10000.
109+
tests.push(String.fromCharCode(0xd800, 0xdc00));
110+
tests = tests.map(
111+
function(elem) {
112+
var expected = mybtoa(elem);
113+
if (expected === "INVALID_CHARACTER_ERR") {
114+
return ["btoa(" + format_value(elem) + ") must raise INVALID_CHARACTER_ERR", elem];
115+
}
116+
return ["btoa(" + format_value(elem) + ") == " + format_value(mybtoa(elem)), elem];
117+
}
118+
);
119+
120+
var everything = "";
121+
for (var i = 0; i < 256; i++) {
122+
everything += String.fromCharCode(i);
123+
}
124+
tests.push(["btoa(first 256 code points concatenated)", everything]);
125+
126+
generate_tests(testBtoa, tests);
127+
128+
promise_test(() => fetch("../../../fetch/data-urls/resources/base64.json").then(res => res.json()).then(runAtobTests), "atob() setup.");
129+
130+
const idlTests = [
131+
[undefined, null],
132+
[null, [158, 233, 101]],
133+
[7, null],
134+
[12, [215]],
135+
[1.5, null],
136+
[true, [182, 187]],
137+
[false, null],
138+
[NaN, [53, 163]],
139+
[+Infinity, [34, 119, 226, 158, 43, 114]],
140+
[-Infinity, null],
141+
[0, null],
142+
[-0, null],
143+
[{toString: function() { return "foo" }}, [126, 138]],
144+
[{toString: function() { return "abcd" }}, [105, 183, 29]]
145+
];
146+
147+
function runAtobTests(tests) {
148+
const allTests = tests.concat(idlTests);
149+
for(let i = 0; i < allTests.length; i++) {
150+
const input = allTests[i][0],
151+
output = allTests[i][1];
152+
test(() => {
153+
if(output === null) {
154+
assert_throws_dom("InvalidCharacterError", () => globalThis.atob(input));
155+
} else {
156+
const result = globalThis.atob(input);
157+
for(let ii = 0; ii < output.length; ii++) {
158+
assert_equals(result.charCodeAt(ii), output[ii]);
159+
}
160+
}
161+
}, "atob(" + format_value(input) + ")");
162+
}
163+
}

test/fixtures/wpt/versions.json

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"commit": "9910784394858a8e34d9eb4e5d00788765abf837",
2424
"path": "hr-time"
2525
},
26+
"html/webappapis/atob": {
27+
"commit": "f267e1dca6f57a9f4d69f32a6920adfdb3268656",
28+
"path": "html/webappapis/atob"
29+
},
2630
"html/webappapis/microtask-queuing": {
2731
"commit": "2c5c3c4c27d27a419c1fdba3e9879c2d22037074",
2832
"path": "html/webappapis/microtask-queuing"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"base64.any.js": {
3+
"fail": "promise_test: Unhandled rejection with value: object \"Error: ENOENT: no such file or directory, open '/root/node/node/fetch/data-urls/resources/base64.json'\""
4+
}
5+
}

test/wpt/test-atob.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { WPTRunner } = require('../common/wpt');
5+
6+
const runner = new WPTRunner('html/webappapis/atob');
7+
8+
// Needed to access to DOMException.
9+
runner.setFlags(['--expose-internals']);
10+
11+
// Set a script that will be executed in the worker before running the tests.
12+
runner.setInitScript(`
13+
const { internalBinding } = require('internal/test/binding');
14+
const { atob, btoa } = require('buffer');
15+
const { DOMException } = internalBinding('messaging');
16+
global.DOMException = DOMException;
17+
`);
18+
19+
runner.runJsTests();

0 commit comments

Comments
 (0)