summaryrefslogtreecommitdiff
path: root/frontend/gamma/tests/tests/Clipperz/Base.test.js
Unidiff
Diffstat (limited to 'frontend/gamma/tests/tests/Clipperz/Base.test.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/tests/tests/Clipperz/Base.test.js367
1 files changed, 367 insertions, 0 deletions
diff --git a/frontend/gamma/tests/tests/Clipperz/Base.test.js b/frontend/gamma/tests/tests/Clipperz/Base.test.js
new file mode 100644
index 0000000..e5c7caf
--- a/dev/null
+++ b/frontend/gamma/tests/tests/Clipperz/Base.test.js
@@ -0,0 +1,367 @@
1/*
2
3Copyright 2008-2011 Clipperz Srl
4
5This file is part of Clipperz's Javascript Crypto Library.
6Javascript Crypto Library provides web developers with an extensive
7and efficient set of cryptographic functions. The library aims to
8obtain maximum execution speed while preserving modularity and
9reusability.
10For further information about its features and functionalities please
11refer to http://www.clipperz.com
12
13* Javascript Crypto Library is free software: you can redistribute
14 it and/or modify it under the terms of the GNU Affero General Public
15 License as published by the Free Software Foundation, either version
16 3 of the License, or (at your option) any later version.
17
18* Javascript Crypto Library is distributed in the hope that it will
19 be useful, but WITHOUT ANY WARRANTY; without even the implied
20 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU Affero General Public License for more details.
22
23* You should have received a copy of the GNU Affero General Public
24 License along with Javascript Crypto Library. If not, see
25 <http://www.gnu.org/licenses/>.
26
27*/
28
29
30//=============================================================================
31
32testEvalJSON = function(aString, shouldFail, aDescription) {
33 var result;
34
35 if (shouldFail == true) {
36 try {
37 result = Clipperz.Base.evalJSON(aString);
38 is(true, false, aDescription + ": vulnerability not caught");
39//console.log(result);
40 } catch(exception) {
41 is(true, true, aDescription + ": vulnerability correctly caught");
42 }
43 } else {
44 try {
45 result = Clipperz.Base.evalJSON(aString);
46 is(true, true, aDescription + ": configuration correctly checked");
47 } catch(exception) {
48 is(true, false, aDescription + ": configuration wrongly caught as malicious");
49 // console.log(exception);
50 }
51 }
52
53 return result;
54}
55
56//=============================================================================
57
58var tests = {
59
60 '001_test': function () {
61 var stringToSplit;
62 varsplittedString;
63
64 stringToSplit = "stringToSplit";
65 splittedString = Clipperz.Base.splitStringAtFixedTokenSize(stringToSplit, 2);
66 is(splittedString.length, 7);
67 is(splittedString[0], 'st');
68 is(splittedString[1], 'ri');
69 is(splittedString[2], 'ng');
70 is(splittedString[3], 'To');
71 is(splittedString[4], 'Sp');
72 is(splittedString[5], 'li');
73 is(splittedString[6], 't', "test that should pass");
74
75 stringToSplit = "stringToSplit";
76 splittedString = Clipperz.Base.splitStringAtFixedTokenSize(stringToSplit, 20);
77 is(splittedString.length, 1);
78 is(splittedString[0], 'stringToSplit');
79
80 stringToSplit = null;
81 splittedString = Clipperz.Base.splitStringAtFixedTokenSize(stringToSplit, 20);
82 is(splittedString.length, 0);
83 },
84
85 //-------------------------------------------------------------------------
86
87 '002_test': function () {
88 var anObject;
89
90 anObject = "String";
91 is(Clipperz.Base.objectType(anObject), 'string', "test on strings (1)");
92 anObject = new String("String");
93 is(Clipperz.Base.objectType(anObject), 'string', "test on strings (2)");
94
95 anObject = 120;
96 is(Clipperz.Base.objectType(anObject), 'number', "test on numbers (1)");
97 anObject = new Number(120);
98 is(Clipperz.Base.objectType(anObject), 'number', "test on numbers (2)");
99
100 anObject = true;
101 is(Clipperz.Base.objectType(anObject), 'boolean', "test on booleans (1)");
102 anObject = new Boolean(true);
103 is(Clipperz.Base.objectType(anObject), 'boolean', "test on booleans (2)");
104
105 anObject = new Date;
106 is(Clipperz.Base.objectType(anObject), 'date', "test on dates");
107
108 anObject = new Error("test");
109 is(Clipperz.Base.objectType(anObject), 'error', "test on errors");
110
111 anObject = function() {};
112 is(Clipperz.Base.objectType(anObject), 'function', "test on functions");
113
114 anObject = new Object();
115 is(Clipperz.Base.objectType(anObject), 'object', "test on objects");
116
117
118 anObject = [1, 2, 3];
119 is(Clipperz.Base.objectType(anObject), 'array', "test on arrays");
120 },
121
122 //-------------------------------------------------------------------------
123
124 '003_test': function () {
125 var original, clone;
126
127 original = {
128 a: "a",
129 b: "b"
130 };
131
132 clone = Clipperz.Base.deepClone(original);
133 is(MochiKit.Base.compare(original, clone), 0, "simple cloning return two equal objects");
134
135 clone.c = "c";
136 is(MochiKit.Base.compare(original, clone), -1, "changing an object leave the original object unchanged");
137
138 original = {
139 a: "a",
140 b: "b",
141 nested: {
142 a1: "a1",
143 b1: "b1"
144 }
145 };
146
147 clone = Clipperz.Base.deepClone(original);
148 is(MochiKit.Base.compare(original, clone), 0, "cloning of an object with nested values return two equal objects");
149
150 clone.nested.c1 = "c1";
151 is(MochiKit.Base.compare(original, clone), -1, "changing a nested value leave the original object unchanged");
152 },
153
154 //-------------------------------------------------------------------------
155
156 '004_test': function () {
157 var jsonString;
158
159 jsonString = '{"page": {"title": "Example Attack"},"form": { "attributes": { "action": "javascript:opener.document.body.innerHTML = \'hacked!\';close();", "style": "-moz-binding:url(\'http://ha.ckers.org/xssmoz.xml#xss\')", "method": null }, "inputs": [{"type": "text", "name": "username", "value": ""}, {"type": "password", "name": "password", "value": ""}]},"version": "0.2.3" }';
160 testEvalJSON(jsonString, false, "");
161
162 //jsonString = '{"0":{"label":"<script>alert(\"Ciao Marco\")< /script>","key":"ebc9782019bf9aa757e9c4d716ab303e2050b60c4b9a06b18ab09a417e0ddf00"}, "1":{"label":"<iframe><script>alert(\\"Ciao ragazzi\\")< /script></iframe>", "key":"413cfb122a1601c50e0f9462978ba77a36fdcecb49dda7550ee129dc114ba328"}}';
163 jsonString = new Clipperz.ByteArray().appendBase64String("eyIwIjp7ImxhYmVsIjoiPHNjcmlwdD5hbGVydChcIkNpYW8gTWFyY29cIik8L3NjcmlwdD4iLCAia2V5IjoiZWJjOTc4MjAxOWJmOWFhNzU3ZTljNGQ3MTZhYjMwM2UyMDUwYjYwYzRiOWEwNmIxOGFiMDlhNDE3ZTBkZGYwMCJ9LCAiMSI6eyJsYWJlbCI6IjxpZnJhbWU+PHNjcmlwdD5hbGVydChcIkNpYW8gcmFnYXp6aVwiKTwvc2NyaXB0PjwvaWZyYW1lPiIsICJrZXkiOiI0MTNjZmIxMjJhMTYwMWM1MGUwZjk0NjI5NzhiYTc3YTM2ZmRjZWNiNDlkZGE3NTUwZWUxMjlkYzExNGJhMzI4In19").asString();
164 testEvalJSON(jsonString, false);
165
166 jsonString = 'alert("foobar");';
167 testEvalJSON(jsonString, true);
168
169 //jsonString = '<script>alert("foobar");< /script>';
170 jsonString = new Clipperz.ByteArray().appendBase64String("PHNjcmlwdD5hbGVydCgiZm9vYmFyIik7PC9zY3JpcHQ+").asString();
171 testEvalJSON(jsonString, true);
172
173 jsonString = '{"xss": alert("XSS!")}';
174 testEvalJSON(jsonString, true);
175
176 jsonString = '{"inner": {"xss": alert("XSS!")}}';
177 testEvalJSON(jsonString, true);
178 },
179
180 //-------------------------------------------------------------------------
181
182 'Clipperz.Base.itemgetter_test': function () {
183 varanObject;
184
185 anObject = {
186 key1: 'value1',
187 key2: {
188 key2_1: 'value2_1',
189 key2_2: 'value2_2'
190 },
191 key3: {
192 key3_1: {
193 key3_1_1: 'value3_1_1',
194 key3_1_2: 'value3_1_2'
195 },
196 key3_2: {
197 key3_2_1: 'value3_2_1',
198 key3_2_2: 'value3_2_2'
199 }
200 }
201 };
202
203 SimpleTest.is(Clipperz.Base.itemgetter('key1')(anObject), "value1", "Clipperz.Base.itemgetter works as MochiKit.Base.itemgetter");
204 SimpleTest.is(Clipperz.Base.itemgetter('key2.key2_1')(anObject), "value2_1", "Clipperz.Base.itemgetter works also with keypaths");
205 SimpleTest.is(Clipperz.Base.itemgetter('key3.key3_2.key3_2_2')(anObject), "value3_2_2", "Clipperz.Base.itemgetter works also with 'long' keypaths");
206 },
207
208 //-------------------------------------------------------------------------
209
210 'Clipperz.Base.caseInsensitiveCompare_test': function () {
211 varcomparator;
212 var objects;
213
214 comparator = Clipperz.Base.caseInsensitiveCompare;
215 objects = [ 'ccc', 'aaa', 'bbb', 'eee', 'ddd'];
216 SimpleTest.isDeeply(['aaa', 'bbb', 'ccc', 'ddd', 'eee'], objects.sort(comparator), "caseInsensitiveCompare works with all lowercase values");
217
218 comparator = MochiKit.Base.compare;
219 objects = [ 'ccc', 'AAA', 'bbb', 'EEE', 'ddd'];
220 SimpleTest.isDeeply(['AAA', 'EEE', 'bbb', 'ccc', 'ddd'], objects.sort(comparator), "caseInsensitiveCompare works with all lowercase values");
221
222 comparator = Clipperz.Base.caseInsensitiveCompare;
223 objects = [ 'ccc', 'AAA', 'bbb', 'EEE', 'ddd'];
224 SimpleTest.isDeeply(['AAA', 'bbb', 'ccc', 'ddd', 'EEE'], objects.sort(comparator), "caseInsensitiveCompare works with all lowercase values");
225 },
226
227 //-------------------------------------------------------------------------
228
229 'Clipperz.Base.reverseComparator_test': function () {
230 varcomparator;
231 var objects;
232
233 comparator = MochiKit.Base.compare;
234 objects = [5, 3, 2, 4, 1];
235 SimpleTest.isDeeply([1, 2, 3, 4, 5], objects.sort(comparator), "a regular comparator works fine");
236
237 comparator = Clipperz.Base.reverseComparator(MochiKit.Base.compare);
238 objects = [5, 3, 2, 4, 1];
239 SimpleTest.isDeeply([5, 4, 3, 2 ,1], objects.sort(comparator), "a reversed comparator works fine");
240
241 comparator = MochiKit.Base.keyComparator('label');
242 objects = [ {label:"5"}, {label:"3"}, {label:"1"}, {label:"4"}, {label:"2"}];
243 SimpleTest.isDeeply([ {label:"1"}, {label:"2"}, {label:"3"}, {label:"4"}, {label:"5"}], objects.sort(comparator), "a regular keyComparator works fine");
244
245 comparator = Clipperz.Base.reverseComparator(MochiKit.Base.keyComparator('label'));
246 objects = [ {label:"5"}, {label:"3"}, {label:"1"}, {label:"4"}, {label:"2"}];
247 SimpleTest.isDeeply([ {label:"5"}, {label:"4"}, {label:"3"}, {label:"2"}, {label:"1"}], objects.sort(comparator), "a reversed keyComparator works fine");
248 },
249
250 //-------------------------------------------------------------------------
251
252 'Clipperz.Base.map_test': function () {
253 var objects;
254 varcomputedObjecs;
255
256 objects = [5, 3, 2, 4, 1];
257 computedObjecs = Clipperz.Base.map(function (aValue) { return aValue * 2;}, objects);
258 SimpleTest.isDeeply(computedObjecs, [10, 6, 4, 8, 2], "the mapped values of the array do match");
259
260 objects = {
261 'five': 5,
262 'three':3,
263 'two': 2,
264 'four': 4,
265 'one': 1
266 };
267 computedObjecs = Clipperz.Base.map(function (aValue) { return aValue * 2;}, objects);
268 SimpleTest.isDeeply(computedObjecs, {
269 'five': 10,
270 'three':6,
271 'two': 4,
272 'four': 8,
273 'one': 2
274 }, "the mapped values of the object do match");
275 },
276
277 //-------------------------------------------------------------------------
278
279 'Clipperz.Base.isUrl_test': function () {
280 varurlTestCases;
281
282 urlTestCases = [
283 {url:'http://foo.com/blah_blah', expectedResult:true},
284 {url:'http://foo.com/blah_blah', expectedResult:true},
285 {url:'http://foo.com/blah_blah/', expectedResult:true},
286 {url:'http://foo.com/blah_blah_(wikipedia)', expectedResult:true},
287 {url:'http://foo.com/blah_blah.', expectedResult:true},
288 {url:'http://foo.com/blah_blah/.', expectedResult:true},
289 {url:'http://foo.com/blah_blah,', expectedResult:true},
290 {url:'http://✪df.ws/123', expectedResult:true},
291 {url:'http://➡.ws/䨹', expectedResult:true},
292 {url:'www.➡.ws/䨹', expectedResult:true},
293 {url:'http://www.example.com/wpstyle/?p=364.', expectedResult:true},
294 {url:'www.clipperz.com', expectedResult:true},
295 {url:'http://www.clipperz.com', expectedResult:true},
296 {url:'http://clipperz.com', expectedResult:true},
297
298 {url:'clipperz.com', expectedResult:false},
299 // {url:'www.clipperz', expectedResult:false},
300 // {url:'www.abc', expectedResult:false},
301 {url:'joe@clipperz.com', expectedResult:false},
302 {url:'<http://foo.com/blah_blah>', expectedResult:false},
303 {url:'<http://foo.com/blah_blah/>', expectedResult:false},
304 {}
305 ];
306
307 MochiKit.Base.map(function (someValues) {
308 if (typeof(someValues['url']) != 'undefined') {
309 SimpleTest.is(Clipperz.Base.isUrl(someValues['url']), someValues['expectedResult'], "testing url '" + someValues['url'] + "' - expected result: " + someValues['expectedResult']);
310 }
311 }, urlTestCases);
312
313/*
314 //RegExp and test strings courtesy of John Gruber: http://daringfireball.net/2009/11/liberal_regex_for_matching_urls
315 SimpleTest.is(Clipperz.Base.isUrl('http://foo.com/blah_blah'), true,"url test +1");
316 SimpleTest.is(Clipperz.Base.isUrl('http://foo.com/blah_blah/'), true,"url test +2");
317 SimpleTest.is(Clipperz.Base.isUrl('http://foo.com/blah_blah_(wikipedia)'), true,"url test +3");
318 SimpleTest.is(Clipperz.Base.isUrl('http://foo.com/blah_blah.'), true,"url test +4");
319 SimpleTest.is(Clipperz.Base.isUrl('http://foo.com/blah_blah/.'), true,"url test +5");
320 SimpleTest.is(Clipperz.Base.isUrl('<http://foo.com/blah_blah>'), true,"url test +6");
321 SimpleTest.is(Clipperz.Base.isUrl('<http://foo.com/blah_blah/>'), true,"url test +7");
322 SimpleTest.is(Clipperz.Base.isUrl('http://foo.com/blah_blah,'), true,"url test +8");
323 SimpleTest.is(Clipperz.Base.isUrl('http://✪df.ws/123'), true,"url test +9");
324 SimpleTest.is(Clipperz.Base.isUrl('http://➡.ws/䨹'), true,"url test +10");
325 SimpleTest.is(Clipperz.Base.isUrl('www.➡.ws/䨹'), true,"url test +11");
326 SimpleTest.is(Clipperz.Base.isUrl('http://www.example.com/wpstyle/?p=364.'),true,"url test +12");
327 SimpleTest.is(Clipperz.Base.isUrl('www.clipperz.com'), true,"url test +13");
328 SimpleTest.is(Clipperz.Base.isUrl('http://www.clipperz.com'), true,"url test +14");
329
330 // SimpleTest.is(Clipperz.Base.isUrl('http://userid@example.com'), true, "url test +13");// FAIL
331 // SimpleTest.is(Clipperz.Base.isUrl('http://userid@example.com:8080'), true, "url test +14");// FAIL
332 // SimpleTest.is(Clipperz.Base.isUrl('http://userid:password@example.com'), true, "url test +15");// FAIL
333 // SimpleTest.is(Clipperz.Base.isUrl('http://userid:password@example.com:8080'), true, "url test +16");// FAIL
334
335
336 SimpleTest.is(Clipperz.Base.isUrl('joe@clipperz.com'), false,"url test -1");
337 SimpleTest.is(Clipperz.Base.isUrl('rdar://1234'), false,"url test -2");
338 SimpleTest.is(Clipperz.Base.isUrl('rdar:/1234'), false,"url test -3");
339 SimpleTest.is(Clipperz.Base.isUrl('http://example.com:8080 x-yojimbo-item://6303E4C1-xxxx-45A6-AB9D-3A908F59AE0E'), false,"url test -4");
340 SimpleTest.is(Clipperz.Base.isUrl('message://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e'), false,"url test -5");
341*/
342 },
343
344 //-------------------------------------------------------------------------
345
346 'Clipperz.Base.isEmail_test': function () {
347 varemailTestCases;
348
349 emailTestCases = [
350 {email:'joe@clipperz.com', expectedResult:true},
351
352 {email:'http://foo.com/blah_blah', expectedResult:false}
353 ];
354
355 MochiKit.Base.map(function (someValues) {
356 SimpleTest.is(Clipperz.Base.isEmail(someValues['email']), someValues['expectedResult'], "testing email '" + someValues['email'] + "' - expected result: " + someValues['expectedResult']);
357 }, emailTestCases);
358 },
359
360 //-------------------------------------------------------------------------
361
362 'syntaxFix': MochiKit.Base.noop
363}
364
365//=============================================================================
366
367SimpleTest.runDeferredTests("Clipperz.Base", tests, {trace:false});