-
Notifications
You must be signed in to change notification settings - Fork 19
/
EnDeTest.js
286 lines (271 loc) · 10.6 KB
/
EnDeTest.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* ========================================================================= //
// vi: ts=4: encoding=utf-8
// vim: ts=4: encoding=utf-8
#?
#? NAME
#? EnDeTest.js
#?
#? SYNOPSIS
#? <SCRIPT language="JavaScript1.3" type="text/javascript" src="EnDe.js"></SCRIPT>
#? <SCRIPT language="JavaScript1.5" type="text/javascript" src="EnDeTest.js" charset="utf-8"></SCRIPT>
#? <SCRIPT language="JavaScript1.3" type="text/javascript" src="EnDeMaps.js"></SCRIPT>
#?
#? Note that it should be used with language="JavaScript1.5" because
#? try{..}catch(){..} is used herein.
#?
#? DESCRIPTION
#? Perform tests for encoding, decoding and converting functions.
#? Provided object:
#? EnDe.Test
#? Provided object methods:
#? EnDe.Test.sid() - return version string
#? EnDe.Test.test() - return array with test results
#? EnDe.Test.b64_test() - performance tests for EnDe.B64.EN.b64()
#?
#? SEE ALSO
#? EnDe.js
#? EnDeTest-Sample.txt
#?
# HACKER's INFO
# This file conatins UTF-8 characters!
#
# The syntax of the test data is described in EnDeTest-Sample.txt.
# The objects and functions herein should be part of EnDe.js but are
# separated into its own file to keep EnDe.js as small as possible.
# All functions return an object or an array of objects. They do not
# change any data.
#
# The test simply calls the encoding function, then the corresponding
# decoding function with the result from the encoding function.
# Finally the result of the encoding is compared with the expected
# result and the the result of the decoding with the original string.
#?
#?
#? VERSION
#? @(#) EnDeTest.js 3.10 13/06/16 23:47:30
#?
#? AUTHOR
#? 22-jun-07 Achim Hoffmann, mailto: EnDe (at) my (dash) stp (dot) net
#?
* ========================================================================= */
// ========================================================================= //
// Object with data and methods for testing Encoding, Decoding functions //
// ========================================================================= //
if (typeof(EnDe)==='undefined') { EnDe = new function() {}; }
EnDe.Test = new function() {
this.SID = '3.10';
this.sid = function() { return '@(#) EnDeTest.js 3.10 13/06/16 23:47:30 EnDe.Test'; },
// ===================================================================== //
// global variables //
// ===================================================================== //
this.text = 'EnDe: Tu was du willst!';// not yet used
this.mite = 'Ückück'; // not yet used
this.cipher = 'Mondenkind'; // not yet used
this.maxloop= 999;
this.reverseMap = {
/* Results of some functions cannot be reverted simply, we need to use
* the corresponding function to get the correct result.
* This table contains the corresponding decoding() functions.
* If the value is empty string, then no decoding() function exists.
*/
'lc': 'uc',
'uc': 'lc',
// 'JSlc': 'JSuc', // don't use, otherwise reverse test fails
'JSuc': 'JSlc',
// 'JS2char': 'JS2code', // fails 'cause encoding fails first
'JS2code': 'JS2char',
'b64tou64': 'u64tob64',
'u64tob64': 'b64tou64',
// 'urlHEX': 'urlCHR',
'urlUTF8c': '',
// 'ucsHALFw': 'ucsHALFw', // if we feed half-width characters
// 'ucsHALFw': 'ucsFULLw', // if we feed full-width characters
'ucsFULLw': 'ucsHALFw', // if we feed half-width characters
'ucsUTF7_': 'ucsUTF7',
'splitArg': 'joinArg',
'splitKey': 'joinKey',
'splitDel': 'joinDel',
'joinArg': 'splitArg',
'joinKey': 'splitKey',
'joinDel': 'splitDel'
};
// ===================================================================== //
// debug functions //
// ===================================================================== //
this.trace = 1; // enabled by default
this.dbx = function(src,nl) {
//#? wrapper for EnDe.dbx()
if(this.trace<=0) { return false; }
return EnDe.dbx(src,nl);
}; // dbx
// ===================================================================== //
// testing functions //
// ===================================================================== //
this.b64_test = function(src) {
//#? performance tests for EnDe.B64.EN.b64()
var b = '';
// var t1 = new Date(); // #dbx performance tests
// var t3 = new Date(); // #dbx performance tests
// this.dbx('\n'+t3+'\n '+len); // #dbx performance tests
EnDe.CONST.CST.blocksize = 120;// < 25k
b += EnDe.B64.EN.b64('','','',src,_n5_,_n6_,linewrap) + '\n';
EnDe.CONST.CST.blocksize = 250;
b += EnDe.B64.EN.b64('','','',src,_n5_,_n6_,linewrap) + '\n';
EnDe.CONST.CST.blocksize = 500;
b += EnDe.B64.EN.b64('','','',src,_n5_,_n6_,linewrap) + '\n';
EnDe.CONST.CST.blocksize = 1010;
b += EnDe.B64.EN.b64('','','',src,_n5_,_n6_,linewrap) + '\n';
EnDe.CONST.CST.blocksize = 2020;
b += EnDe.B64.EN.b64('','','',src,_n5_,_n6_,linewrap) + '\n';
return b;
}; // b64_test
this.test = function(src) {
//#? run test for data given in src; returns array with results
// ToDo: need to describe format of returned array
var sid = 'EnDe.Test.test';
_dpr(sid+'(\n'+src+'\n)\n');
function _txt(src) {
// #? check if plain text, convert \uHHHH and \xHH to text; return raw text enclosed in ' or "
var bux = src;
if (src[0]===src[src.length-1]) {
if ((src[0]==='"') || (src[0]==="'")) {
return src.substr(1,src.length-2);
}
}
bux = src.replace(/\\u([a-f0-9]{4})/ig,function(c,d){return String.fromCharCode(parseInt(d,16))});
bux = bux.replace(/\\x([a-f0-9]{2})/ig,function(c,d){return String.fromCharCode(parseInt(d,16))});
return bux;
};
var bux = new Array();
var uppercase = false;
var delimiter = ',';
var prefix = '';
var suffix = '';
var cipher = '';
var isHex = false;
var size = 2;
var i = 0;
var r = null;
var max = this.maxloop;
var skip = true;
var mode = 'en-/decode';
var func = '';
var title = '';
var expect = '';
var except = '';
var fileerr = '';
var tmp = '';
var txt = '';
var err = null;
var enc = '';
var dec = '';
var key = '';
var val = '';
var line = '';
var oldmode = '';
var oldtitle= '';
var oldtxt = '';
var data = src.split(/\n/);
while((line=data.shift())!==undefined) {
i++;
if (i>max) { fileerr += '**ERROR: too much data: ' + i + ' lines; aborted'; break; }
if (line.search(/^__DATA/)>=0) { skip = false; continue; } // start of data
if (skip===true) { continue; }//
if (line.search(/^\s*$/)>=0) { continue; }// skip empty lines
if (line.search(/^\s*#/)>=0) { continue; }// skip comment lines
if (line.search(/^__END/)>=0) { break; } // end of data
// don't split on tab like:
// kkk = line.replace(/\t\t*/, '\t').split(/\t/);
// as this would destroy tabs in the value part (right column)
// instead we extract key left to first tab, and value anything
// right to all continous left tabs following key
key = line.replace(/^([^\t]+)\t+.*$/,"$1");
val = line.replace(/^[^\t]+\t+(.*)$/,"$1");
if (val===line) {
fileerr += '**ERROR: unknown syntax in line ' + i + ': ' + line;
continue;
}
switch (key) { // keyword or alias
case '_data': txt = _txt(val); continue; break;
case '_title': title = val; continue; break;
case '_mode': mode = val; continue; break;
case '_size': size = val; continue; break;
case '_salt': /* same as _cipher; no break; */
case '_cipher': cipher = _txt(val); continue; break;
case '_prefix': prefix = _txt(val); continue; break;
case '_suffix': suffix = _txt(val); continue; break;
case '_delimiter': delimiter = _txt(val); continue; break;
case '_uppercase': uppercase = (val==='true')?true:false; continue; break;
default: expect = _txt(val); func = key; break;
}
// got a function
if ((txt!==oldtxt) || (title!==oldtitle) || (mode!==oldmode)) { // new _data line
oldtxt = txt;
oldtitle= title;
oldmode = mode;
bux.push(new Array('_data', title, mode, txt, null));
// bux.push(new Array('_parm', null, 'mode', mode, null)); // same as _func below
bux.push(new Array('_parm', null, 'uppercase', uppercase, null));
bux.push(new Array('_parm', null, 'prefix', prefix, null));
bux.push(new Array('_parm', null, 'suffix', suffix, null));
bux.push(new Array('_parm', null, 'cipher', cipher, null));
bux.push(new Array('_parm', null, 'delimiter', delimiter, null));
bux.push(new Array('_func', title, mode, null, null));
}
// reset results
enc = '';
dec = '';
err = null;
try { // run test
_dpr(sid+': func='+func+', mode='+mode);
if ((mode==='encode') || (mode==='en-/decode')) {
enc = EnDe.EN.dispatch(func,'strict',uppercase,txt,prefix,suffix,delimiter);
_dpr(sid+': (enc==expect)='+(enc===expect));
//#dbx# alert('.'+expect+'.\n.'+enc);
if (enc!==expect) { // failed, no more tests possible
bux.push(new Array(func,expect,enc,((mode==='en-/decode')?'-undef-':null),err));
continue;
}
if (mode!=='en-/decode') {
if (enc===expect) {
bux.push(new Array(func,null,null,'-undef-',err));
}
continue;
}
tmp = txt;
} else {
enc = txt; // in mode=decode no enc set yet
txt = expect;
}
if ((mode==='decode') || (mode==='en-/decode')) {
r = func;
if (this.reverseMap[func]!==undefined) {
r = this.reverseMap[func];
if (r==='') { // no proper decoding available, ready
bux.push(new Array(func,null,null,null,err));
continue;
}
}
if (r!=='') {
dec = EnDe.DE.dispatch(r,'strict',false,enc,prefix,suffix,delimiter);
}
//#dbx# alert('.'+dec+'.\n.'+txt);
_dpr(sid+': (dec==txt)='+(dec===txt));
if (dec!==txt) { // failed, no more tests possible
bux.push(new Array(func,expect,null,dec,err));
continue;
}
bux.push(new Array(func,null,null,null,err));
}
} catch(e) {
bux.push(new Array(func,expect,enc,dec,e));
}
_dpr(sid);
} // while line
if (fileerr!=='') {
bux.push(new Array('_error','errors found in ' + src + ':\n' + fileerr,null,null,null));
}
_dpr(sid+' {\n'+bux.join('\n')+'\n}');
return bux;
}; // text
}; // EnDe.Test