1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="base64.js"></script>
<link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="qunit.js"></script>
<script type="text/javascript">
var use_window = false;
if (use_window) {
encode = window.btoa;
decode = window.atob;
} else {
encode = base64.encode;
decode = base64.decode;
}
$(document).ready(function(){
module("Error Handling");
test("Encode empty input", function() {
equals(encode(''), '')
});
test("Encode non-stringinput", function() {
equals(encode(2), 'Mg==')
});
test("Encode undefined input", function() {
try {
encode();
ok(false, "expected exception");
} catch (e) {
ok(true, "got exception: " + e);
}
});
test("Encode invalid binary input", function() {
try {
encode("\u0100");
ok(false, "expected exception");
} catch (e) {
ok(true, "got exception: " + e);
}
});
test("Decode empty input", function() {
equals(decode(''), '')
});
// this is actually the same as "2" without padding
test("Decode non-string input1", function() {
try {
decode(2);
} catch (e) {
ok(true, "Got exception: " + e);
}
});
// same as "2222"
test("Decode non-string input2", function() {
equals(decode(2222).length, 3)
});
test("Decode non-multiple of 4 input length", function() {
try {
decode('AAAAA');
ok(false, "Excepted exception");
} catch (e) {
ok(true, "Got exception: " + e);
}
});
test("Decode invalid base64 input", function() {
try {
decode("\u0100");
ok(false, "expected exception");
} catch (e) {
ok(true, "got exception: " + e);
}
});
module("Correctness");
test("Encoding Length", function() {
var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis lorem at tellus sollicitudin pretium eu vitae ante. Maecenas tempus urna nec lacus vestibulum pharetra. Nunc in ullamcorper lectus. Nunc eu libero augue. Aliquam pellentesque libero ac nunc imperdiet vitae consequat dui eleifend. Nulla et magna id lacus iaculis sollicitudin id et leo. Vestibulum interdum neque ut sapien luctus malesuada eget aliquet m";
for (var i = 0; i < text.length; ++i) {
var s = text.substr(0,i);
var b64 = window.btoa(s)
equals(b64, base64.encode(s));
equals(s, base64.decode(b64));
}
});
test("Random Bytes", function() {
// random 3 bytes
// doing increment of 1 makes browser angry
for (var i = 0; i < 256; i += 7) {
for (var j = 0; j < 256; j += 9) {
for (var k = 0; k < 256; k += 11) {
var s = String.fromCharCode(i,j,k);
var b64 = window.btoa(s)
equals(b64,base64.encode(s));
equals(s,base64.decode(b64));
}
}
}
});
module("Exceptions");
// make sure what is thrown matches the types
test("Syntax Error", function() {
var e1, e2,x;
try {
base64.encode();
ok(false, "expected exception");
} catch (x) {
// odd scoping issue, have to copy exception object
e1 = x;
}
try {
window.btoa()
ok(false, "expected exception");
} catch (x) {
// odd scoping issue, have to copy exception object
e2 = x;
}
equals(typeof e1, typeof e2);
ok(e1 instanceof SyntaxError, "Our error is an instance of SyntaxError");
ok(e2 instanceof SyntaxError, "Native error is an instance of SyntaxError");
equals(e1.toString(), e2.toString());
});
test("DOM Exception", function() {
var e1, e2,x;
try {
base64.encode('\u1000');
ok(false, "expected exception");
} catch (x) {
// odd scoping issue, have to copy exception object
e1 = x;
}
try {
window.btoa('\u1000')
ok(false, "expected exception");
} catch (x) {
// odd scoping issue, have to copy exception object
e2 = x;
}
equals(typeof e1, typeof e2);
equals(e1.toString(), e2.toString());
equals(e1.code, e2.code);
});
test("Decode Exception", function() {
var e1, e2,x;
try {
base64.decode('\u1000');
ok(false, "expected exception");
} catch (x) {
// odd scoping issue, have to copy exception object
e1 = x;
}
try {
window.atob('\u1000')
ok(false, "expected exception");
} catch (x) {
// odd scoping issue, have to copy exception object
e2 = x;
}
equals(typeof e1, typeof e2);
ok(e1 instanceof Error, "Our error is an instance of Error");
// there is no spec for this, just a test to see if it is
// this fails!
ok(e2 instanceof Error, "Native error is an instance of Error");
// this passes
ok(e2 instanceof DOMException, "Native error is an instance of DOMException");
equals(e1.toString(), e2.toString());
});
});
</script>
</head>
<body>
<h1 id="qunit-header">Base64 window.[atob|btoa] tests</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
|