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

Update Aryeh's base64 test. #11

Merged
merged 1 commit into from
Dec 20, 2012
Merged
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
57 changes: 19 additions & 38 deletions html/webappapis/atob/base64.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<title>atob()/btoa() tests</title>
<meta charset=utf-8>
<div id=log></div>
<script src="/resources/testharness.js"></script>
<script /resources/testharnessreport.js</script>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
// From WebIDL:
//
Expand Down Expand Up @@ -92,13 +92,17 @@
}

/**
* Implementation of atob() according to the HTML spec.
* Implementation of atob() according to the HTML spec, except that instead of
* throwing INVALID_CHARACTER_ERR we return null.
*/
function myatob(input) {
// WebIDL requires DOMStrings to just be converted using ECMAScript
// ToString, which in our case amounts to calling String().
input = String(input);

// "Remove all space characters from input."
input = input.replace(/[ \t\n\f\r]/g, "");

// "If the length of input divides by 4 leaving no remainder, then: if
// input ends with one or two U+003D EQUALS SIGN (=) characters, remove
// them from input."
Expand All @@ -120,7 +124,7 @@
// U+0061 LATIN SMALL LETTER A to U+007A LATIN SMALL LETTER Z"
if (input.length % 4 == 1
|| !/^[+/0-9A-Za-z]*$/.test(input)) {
return "INVALID_CHARACTER_ERR";
return null;
}

// "Let output be a string, initially empty."
Expand Down Expand Up @@ -222,7 +226,7 @@
var normalizedInput = String(input);
for (var i = 0; i < normalizedInput.length; i++) {
if (normalizedInput.charCodeAt(i) > 255) {
assert_throws("INVALID_CHARACTER_ERR", function() { btoa(input); },
assert_throws("InvalidCharacterError", function() { btoa(input); },
"Code unit " + i + " has value " + normalizedInput.charCodeAt(i) + ", which is greater than 255");
return;
}
Expand Down Expand Up @@ -270,38 +274,13 @@
generate_tests(testBtoa, tests);

function testAtob(input) {
// "If the length of input divides by 4 leaving no remainder, then: if
// input ends with one or two U+003D EQUALS SIGN (=) characters, remove
// them from input."
var normalizedInput = String(input);
if (normalizedInput.length % 4 == 0) {
normalizedInput = normalizedInput.replace(/==?$/, "");
}

// "If the length of input divides by 4 leaving a remainder of 1, throw an
// INVALID_CHARACTER_ERR exception and abort these steps."
if (normalizedInput.length % 4 == 1) {
assert_throws("INVALID_CHARACTER_ERR", function() { atob(input) },
"After stripping one or two trailing equals signs (if appropriate), input has length with remainder 1 when divided by 4");
return;
}

// "If input contains a character that is not in the following list of
// characters and character ranges, throw an INVALID_CHARACTER_ERR
// exception and abort these steps:
//
// U+002B PLUS SIGN (+)
// U+002F SOLIDUS (/)
// U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9)
// U+0041 LATIN CAPITAL LETTER A to U+005A LATIN CAPITAL LETTER Z
// U+0061 LATIN SMALL LETTER A to U+007A LATIN SMALL LETTER Z"
if (!/^[+/0-9A-Za-z]*$/.test(normalizedInput)) {
assert_throws("INVALID_CHARACTER_ERR", function() { atob(input) },
"After stripping one or two trailing equals signs (if appropriate), input contains an invalid character");
var expected = myatob(input);
if (expected === null) {
assert_throws("InvalidCharacterError", function() { atob(input) });
return;
}

assert_equals(atob(input), myatob(input));
assert_equals(atob(input), expected);
}

var tests = ["", "abcd", " abcd", "abcd ", "abcd===", " abcd===", "abcd=== ",
Expand All @@ -313,7 +292,10 @@
"abcd=", "abcd==", "abcd===", "abcd====", "abcd=====",
"abcde=", "abcde==", "abcde===", "abcde====", "abcde=====",
"=a", "=a=", "a=b", "a=b=", "ab=c", "ab=c=", "abc=d", "abc=d=",
"ab\ncd",
// With whitespace
"ab\tcd", "ab\ncd", "ab\fcd", "ab\rcd", "ab cd", "ab\u00a0cd",
"ab\t\n\f\r cd", " \t\n\f\r ab\t\n\f\r cd\t\n\f\r ",
"ab\t\n\f\r =\t\n\f\r =\t\n\f\r ",
// Test if any bits are set at the end. These should all be fine, since
// they end with A, which becomes 0:
"A", "/A", "//A", "///A", "////A",
Expand All @@ -334,9 +316,8 @@
];
tests = tests.map(
function(elem) {
var expected = myatob(elem);
if (expected === "INVALID_CHARACTER_ERR") {
return ["atob(" + format_value(elem) + ") must raise INVALID_CHARACTER_ERR", elem];
if (myatob(elem) === null) {
return ["atob(" + format_value(elem) + ") must raise InvalidCharacterError", elem];
}
return ["atob(" + format_value(elem) + ") == " + format_value(myatob(elem)), elem];
}
Expand Down