Skip to content

Commit 32027d8

Browse files
committed
test: add atob wpt
Signed-off-by: James M Snell <[email protected]>
1 parent db090f0 commit 32027d8

File tree

5 files changed

+221
-29
lines changed

5 files changed

+221
-29
lines changed

test/fixtures/wpt/README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ See [test/wpt](../../wpt/README.md) for information on how these tests are run.
1010

1111
Last update:
1212

13+
- common: https://github.com/web-platform-tests/wpt/tree/841a51412f/common
1314
- console: https://github.com/web-platform-tests/wpt/tree/3b1f72e99a/console
15+
- dom/abort: https://github.com/web-platform-tests/wpt/tree/7caa3de747/dom/abort
1416
- encoding: https://github.com/web-platform-tests/wpt/tree/3c9820d1cc/encoding
15-
- url: https://github.com/web-platform-tests/wpt/tree/1783c9bccf/url
16-
- resources: https://github.com/web-platform-tests/wpt/tree/351a99782b/resources
17-
- interfaces: https://github.com/web-platform-tests/wpt/tree/b4be9a3fdf/interfaces
17+
- FileAPI: https://github.com/web-platform-tests/wpt/tree/3b279420d4/FileAPI
18+
- hr-time: https://github.com/web-platform-tests/wpt/tree/a5d1774ecf/hr-time
19+
- html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob
1820
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing
1921
- html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/264f12bc7b/html/webappapis/timers
20-
- hr-time: https://github.com/web-platform-tests/wpt/tree/a5d1774ecf/hr-time
21-
- common: https://github.com/web-platform-tests/wpt/tree/841a51412f/common
22-
- dom/abort: https://github.com/web-platform-tests/wpt/tree/7caa3de747/dom/abort
23-
- FileAPI: https://github.com/web-platform-tests/wpt/tree/3b279420d4/FileAPI
22+
- interfaces: https://github.com/web-platform-tests/wpt/tree/b4be9a3fdf/interfaces
23+
- resources: https://github.com/web-platform-tests/wpt/tree/351a99782b/resources
24+
- url: https://github.com/web-platform-tests/wpt/tree/1783c9bccf/url
2425

2526
[Web Platform Tests]: https://github.com/web-platform-tests/wpt
2627
[`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/master/docs/git-node.md#git-node-wpt
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

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
{
2+
"common": {
3+
"commit": "841a51412f236ad63655daca843d3b8e83e0777b",
4+
"path": "common"
5+
},
26
"console": {
37
"commit": "3b1f72e99a91d31551edd2147dc7b564eaf25d72",
48
"path": "console"
59
},
10+
"dom/abort": {
11+
"commit": "7caa3de7471cf19b78ee9efa313c7341a462b5e3",
12+
"path": "dom/abort"
13+
},
614
"encoding": {
715
"commit": "3c9820d1cc5d9d2627c26ef1268b6d54a35adf22",
816
"path": "encoding"
917
},
10-
"url": {
11-
"commit": "1783c9bccf48c426ec1017f36e1ccfcf90a8af47",
12-
"path": "url"
18+
"FileAPI": {
19+
"commit": "3b279420d40afea32506e823f9ac005448f4f3d8",
20+
"path": "FileAPI"
1321
},
14-
"resources": {
15-
"commit": "351a99782b9677706b5dc0dd78e85978fa4ab130",
16-
"path": "resources"
22+
"hr-time": {
23+
"commit": "a5d1774ecf41751d1c9357c27c709ee33bf3e279",
24+
"path": "hr-time"
1725
},
18-
"interfaces": {
19-
"commit": "b4be9a3fdf18459a924f88e49bc55d8b30faa93a",
20-
"path": "interfaces"
26+
"html/webappapis/atob": {
27+
"commit": "f267e1dca6f57a9f4d69f32a6920adfdb3268656",
28+
"path": "html/webappapis/atob"
2129
},
2230
"html/webappapis/microtask-queuing": {
2331
"commit": "2c5c3c4c27d27a419c1fdba3e9879c2d22037074",
@@ -27,20 +35,16 @@
2735
"commit": "264f12bc7bf5db0c6dd064842a5d39ccbf9208c5",
2836
"path": "html/webappapis/timers"
2937
},
30-
"hr-time": {
31-
"commit": "a5d1774ecf41751d1c9357c27c709ee33bf3e279",
32-
"path": "hr-time"
33-
},
34-
"common": {
35-
"commit": "841a51412f236ad63655daca843d3b8e83e0777b",
36-
"path": "common"
38+
"interfaces": {
39+
"commit": "b4be9a3fdf18459a924f88e49bc55d8b30faa93a",
40+
"path": "interfaces"
3741
},
38-
"dom/abort": {
39-
"commit": "7caa3de7471cf19b78ee9efa313c7341a462b5e3",
40-
"path": "dom/abort"
42+
"resources": {
43+
"commit": "351a99782b9677706b5dc0dd78e85978fa4ab130",
44+
"path": "resources"
4145
},
42-
"FileAPI": {
43-
"commit": "3b279420d40afea32506e823f9ac005448f4f3d8",
44-
"path": "FileAPI"
46+
"url": {
47+
"commit": "1783c9bccf48c426ec1017f36e1ccfcf90a8af47",
48+
"path": "url"
4549
}
4650
}
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)