forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64_test.js
59 lines (53 loc) · 1.64 KB
/
base64_test.js
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
var asciiToArray = function (str) {
var arr = Base64.newBinary(str.length);
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
if (c > 0xFF) {
throw new Error("Not ascii");
}
arr[i] = c;
}
return arr;
};
var arrayToAscii = function (arr) {
var res = [];
for (var i = 0; i < arr.length; i++) {
res.push(String.fromCharCode(arr[i]));
}
return res.join("");
};
Tinytest.add("base64 - testing the test", function (test) {
test.equal(arrayToAscii(asciiToArray("The quick brown fox jumps over the lazy dog")),
"The quick brown fox jumps over the lazy dog");
});
Tinytest.add("base64 - empty", function (test) {
test.equal(Base64.encode(EJSON.newBinary(0)), "");
test.equal(Base64.decode(""), EJSON.newBinary(0));
});
Tinytest.add("base64 - wikipedia examples", function (test) {
var tests = [
{txt: "pleasure.", res: "cGxlYXN1cmUu"},
{txt: "leasure.", res: "bGVhc3VyZS4="},
{txt: "easure.", res: "ZWFzdXJlLg=="},
{txt: "asure.", res: "YXN1cmUu"},
{txt: "sure.", res: "c3VyZS4="}
];
_.each(tests, function(t) {
test.equal(Base64.encode(asciiToArray(t.txt)), t.res);
test.equal(arrayToAscii(Base64.decode(t.res)), t.txt);
});
});
Tinytest.add("base64 - non-text examples", function (test) {
var tests = [
{array: [0, 0, 0], b64: "AAAA"},
{array: [0, 0, 1], b64: "AAAB"}
];
_.each(tests, function(t) {
test.equal(Base64.encode(t.array), t.b64);
var expectedAsBinary = EJSON.newBinary(t.array.length);
_.each(t.array, function (val, i) {
expectedAsBinary[i] = val;
});
test.equal(Base64.decode(t.b64), expectedAsBinary);
});
});