-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·177 lines (145 loc) · 4.41 KB
/
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
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
/*
* Copyright (c) 2011, Ilya Bobyr <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
load("imageDataMD5.js");
var unitTests = [];
function simpleUnitTest(stringData, expectedMD5)
{
if (stringData.length % 3 != 0)
{
var err = new Error();
err.message = "stringData value length should always be a multiple of 3.\n"
+ "Got string of length: " + stringData.length;
throw err;
}
var imageData =
{
width: stringData.length / 3,
height: 1,
data: null
};
var data = [];
for (var i = 0; i < stringData.length; i += 3)
data.push(
stringData.charCodeAt(i) & 0xff,
stringData.charCodeAt(i + 1) & 0xff,
stringData.charCodeAt(i + 2) & 0xff,
/* Alpha should be ignored. */
(i % 0xff - i)
);
imageData.data = data;
var md5 = (new ImageDataMD5(imageData)) + "";
if (md5!= expectedMD5)
{
var err = new Error();
err.got = md5;
err.expected = expectedMD5;
throw err;
}
}
/*
* As we should always have our string data length to be a multiple of 3 I have
* padded all the string that have different lengths with spaces. This will of
* cause change their MD5s, but I do not see any other way. 2 tests still test
* the same values as in the RFC.
*/
function unitTest1()
{
simpleUnitTest("", "d41d8cd98f00b204e9800998ecf8427e");
}
unitTest1.title = "RFC.1: Empty string";
unitTests.push(unitTest1);
function unitTest2()
{
simpleUnitTest("a ", "d4ac0334c4130de05b4a37a87590ccc4");
}
unitTest2.title = "RFC.2: 'a'";
unitTests.push(unitTest2);
function unitTest3()
{
simpleUnitTest("abc", "900150983cd24fb0d6963f7d28e17f72");
}
unitTest3.title = "RFC.3: 'abc'";
unitTests.push(unitTest3);
function unitTest4()
{
simpleUnitTest("message digest ", "5c33b66cec053762ad6f2cb06bcd598b");
}
unitTest4.title = "RFC.4: 'message digest'";
unitTests.push(unitTest4);
function unitTest5()
{
simpleUnitTest("abcdefghijklmnopqrstuvwxyz ",
"cb20bf9177e73d5ffa71e95d22389d6d");
}
unitTest5.title = "RFC.5: alphabet";
unitTests.push(unitTest5);
function unitTest6()
{
simpleUnitTest("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ "0123456789 ",
"009e1cff4fa0bf640ed9b1de7ff6c4a3");
}
unitTest6.title = "RFC.6: alphabet and digits";
unitTests.push(unitTest6);
function unitTest7()
{
simpleUnitTest("12345678901234567890123456789012345678901234567890123456789"
+ "012345678901234567890 ",
"d5a01d2d92d9026419f2c4bb5a35b08a");
}
unitTest7.title = "RFC.7: lots of digits";
unitTests.push(unitTest7);
print("Unit tests:");
for (var i = 0; i < unitTests.length; ++i)
{
var failed = true;
var msg, got, expected;
try
{
unitTests[i]();
failed = false;
}
catch (e)
{
msg = e.message;
got = e.got;
expected = e.expected;
if (!msg && !got && ! expected)
msg = JSON.stringify(e);
}
print(i + ". " + unitTests[i].title + " : " + (!failed ? "OK" : "fail"));
if (failed)
{
if (msg)
print(" " + msg);
if (got)
print("Got: " + JSON.stringify(got, null, 2));
if (expected)
print("Expected: " + JSON.stringify(expected, null, 2));
}
}
/* vim: set et sts=2 sw=2 tw=80 spell spl=en: */