|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 let WSP = {}; |
|
5 subscriptLoader.loadSubScript("resource://gre/modules/WspPduHelper.jsm", WSP); |
|
6 WSP.debug = do_print; |
|
7 |
|
8 function run_test() { |
|
9 run_next_test(); |
|
10 } |
|
11 |
|
12 // |
|
13 // Test target: ensureHeader |
|
14 // |
|
15 |
|
16 add_test(function test_ensureHeader() { |
|
17 do_check_throws(function() { |
|
18 WSP.ensureHeader({}, "no-such-property"); |
|
19 }, "FatalCodeError" |
|
20 ); |
|
21 |
|
22 run_next_test(); |
|
23 }); |
|
24 |
|
25 // |
|
26 // Test target: skipValue() |
|
27 // |
|
28 |
|
29 add_test(function test_skipValue() { |
|
30 function func(data) { |
|
31 return WSP.skipValue(data); |
|
32 } |
|
33 |
|
34 // Test for zero-valued first octet: |
|
35 wsp_decode_test_ex(func, [0], null); |
|
36 // Test first octet < 31 |
|
37 wsp_decode_test_ex(func, [1, 2], [2]); |
|
38 // Test first octet = 31 |
|
39 wsp_decode_test_ex(func, [31, 0], null); |
|
40 wsp_decode_test_ex(func, [31, 1, 2], [2]); |
|
41 // Test first octet <= 127 |
|
42 wsp_decode_test_ex(func, strToCharCodeArray("Hello world!"), "Hello world!"); |
|
43 // Test first octet >= 128 |
|
44 wsp_decode_test_ex(func, [0x80 | 0x01], 0x01); |
|
45 |
|
46 run_next_test(); |
|
47 }); |
|
48 |
|
49 // |
|
50 // Test target: Octet |
|
51 // |
|
52 |
|
53 //// Octet.decode //// |
|
54 |
|
55 add_test(function test_Octet_decode() { |
|
56 wsp_decode_test(WSP.Octet, [1], 1); |
|
57 wsp_decode_test(WSP.Octet, [], null, "RangeError"); |
|
58 |
|
59 run_next_test(); |
|
60 }); |
|
61 |
|
62 //// Octet.decodeMultiple //// |
|
63 |
|
64 add_test(function test_Octet_decodeMultiple() { |
|
65 wsp_decode_test_ex(function(data) { |
|
66 return WSP.Octet.decodeMultiple(data, 3); |
|
67 }, [0, 1, 2], [0, 1, 2], null |
|
68 ); |
|
69 wsp_decode_test_ex(function(data) { |
|
70 return WSP.Octet.decodeMultiple(data, 3); |
|
71 }, new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2]), null |
|
72 ); |
|
73 wsp_decode_test_ex(function(data) { |
|
74 return WSP.Octet.decodeMultiple(data, 4); |
|
75 }, [0, 1, 2], null, "RangeError" |
|
76 ); |
|
77 |
|
78 run_next_test(); |
|
79 }); |
|
80 |
|
81 //// Octet.decodeEqualTo //// |
|
82 |
|
83 add_test(function test_Octet_decodeEqualTo() { |
|
84 wsp_decode_test_ex(function(data) { |
|
85 return WSP.Octet.decodeEqualTo(data, 1); |
|
86 }, [1], 1, null |
|
87 ); |
|
88 wsp_decode_test_ex(function(data) { |
|
89 return WSP.Octet.decodeEqualTo(data, 2); |
|
90 }, [1], null, "CodeError" |
|
91 ); |
|
92 wsp_decode_test_ex(function(data) { |
|
93 return WSP.Octet.decodeEqualTo(data, 2); |
|
94 }, [], null, "RangeError" |
|
95 ); |
|
96 |
|
97 run_next_test(); |
|
98 }); |
|
99 |
|
100 //// Octet.encode //// |
|
101 |
|
102 add_test(function test_Octet_encode() { |
|
103 for (let i = 0; i < 256; i++) { |
|
104 wsp_encode_test(WSP.Octet, i, [i]); |
|
105 } |
|
106 |
|
107 run_next_test(); |
|
108 }); |
|
109 |
|
110 //// Octet.encodeMultiple //// |
|
111 |
|
112 add_test(function test_Octet_encodeMultiple() { |
|
113 wsp_encode_test_ex(function(data, input) { |
|
114 WSP.Octet.encodeMultiple(data, input); |
|
115 return data.array; |
|
116 }, [0, 1, 2, 3], [0, 1, 2, 3]); |
|
117 |
|
118 run_next_test(); |
|
119 }); |
|
120 |
|
121 // |
|
122 // Test target: Text |
|
123 // |
|
124 |
|
125 //// Text.decode //// |
|
126 |
|
127 add_test(function test_Text_decode() { |
|
128 for (let i = 0; i < 256; i++) { |
|
129 if (i == 0) { |
|
130 wsp_decode_test(WSP.Text, [0], null, "NullCharError"); |
|
131 } else if ((i < WSP.CTLS) || (i == WSP.DEL)) { |
|
132 wsp_decode_test(WSP.Text, [i], null, "CodeError"); |
|
133 } else { |
|
134 wsp_decode_test(WSP.Text, [i], String.fromCharCode(i)); |
|
135 } |
|
136 } |
|
137 // Test \r\n(SP|HT)* sequence: |
|
138 wsp_decode_test(WSP.Text, strToCharCodeArray("\r\n \t \t \t", true), " "); |
|
139 wsp_decode_test(WSP.Text, strToCharCodeArray("\r\n \t \t \t"), " "); |
|
140 wsp_decode_test(WSP.Text, strToCharCodeArray("\r\n \t \t \tA"), " "); |
|
141 |
|
142 run_next_test(); |
|
143 }); |
|
144 |
|
145 //// Text.encode //// |
|
146 |
|
147 add_test(function test_Text_encode() { |
|
148 for (let i = 0; i < 256; i++) { |
|
149 if ((i < WSP.CTLS) || (i == WSP.DEL)) { |
|
150 wsp_encode_test(WSP.Text, String.fromCharCode(i), null, "CodeError"); |
|
151 } else { |
|
152 wsp_encode_test(WSP.Text, String.fromCharCode(i), [i]); |
|
153 } |
|
154 } |
|
155 |
|
156 run_next_test(); |
|
157 }); |
|
158 |
|
159 // |
|
160 // Test target: NullTerminatedTexts |
|
161 // |
|
162 |
|
163 //// NullTerminatedTexts.decode //// |
|
164 |
|
165 add_test(function test_NullTerminatedTexts_decode() { |
|
166 // Test incompleted string: |
|
167 wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray(" ", true), null, "RangeError"); |
|
168 // Test control char: |
|
169 wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray(" \n"), null, "CodeError"); |
|
170 // Test normal string: |
|
171 wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray(""), ""); |
|
172 wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray("oops"), "oops"); |
|
173 // Test \r\n(SP|HT)* sequence: |
|
174 wsp_decode_test(WSP.NullTerminatedTexts, strToCharCodeArray("A\r\n \t \t \tB"), "A B"); |
|
175 |
|
176 run_next_test(); |
|
177 }); |
|
178 |
|
179 //// NullTerminatedTexts.encode //// |
|
180 |
|
181 add_test(function test_NullTerminatedTexts_encode() { |
|
182 wsp_encode_test(WSP.NullTerminatedTexts, "", [0]); |
|
183 wsp_encode_test(WSP.NullTerminatedTexts, "Hello, World!", |
|
184 strToCharCodeArray("Hello, World!")); |
|
185 |
|
186 run_next_test(); |
|
187 }); |
|
188 |
|
189 // |
|
190 // Test target: Token |
|
191 // |
|
192 |
|
193 let TOKEN_SEPS = "()<>@,;:\\\"/[]?={} \t"; |
|
194 |
|
195 //// Token.decode //// |
|
196 |
|
197 add_test(function test_Token_decode() { |
|
198 for (let i = 0; i < 256; i++) { |
|
199 if (i == 0) { |
|
200 wsp_decode_test(WSP.Token, [i], null, "NullCharError"); |
|
201 } else if ((i < WSP.CTLS) || (i >= WSP.ASCIIS) |
|
202 || (TOKEN_SEPS.indexOf(String.fromCharCode(i)) >= 0)) { |
|
203 wsp_decode_test(WSP.Token, [i], null, "CodeError"); |
|
204 } else { |
|
205 wsp_decode_test(WSP.Token, [i], String.fromCharCode(i)); |
|
206 } |
|
207 } |
|
208 |
|
209 run_next_test(); |
|
210 }); |
|
211 |
|
212 //// Token.encode //// |
|
213 |
|
214 add_test(function test_Token_encode() { |
|
215 for (let i = 0; i < 256; i++) { |
|
216 if ((i < WSP.CTLS) || (i >= WSP.ASCIIS) |
|
217 || (TOKEN_SEPS.indexOf(String.fromCharCode(i)) >= 0)) { |
|
218 wsp_encode_test(WSP.Token, String.fromCharCode(i), null, "CodeError"); |
|
219 } else { |
|
220 wsp_encode_test(WSP.Token, String.fromCharCode(i), [i]); |
|
221 } |
|
222 } |
|
223 |
|
224 run_next_test(); |
|
225 }); |
|
226 |
|
227 // |
|
228 // Test target: URIC |
|
229 // |
|
230 |
|
231 //// URIC.decode //// |
|
232 |
|
233 add_test(function test_URIC_decode() { |
|
234 let uric = "!#$%&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMN" |
|
235 + "OPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~"; |
|
236 for (let i = 0; i < 256; i++) { |
|
237 if (i == 0) { |
|
238 wsp_decode_test(WSP.URIC, [i], null, "NullCharError"); |
|
239 } else if (uric.indexOf(String.fromCharCode(i)) >= 0) { |
|
240 wsp_decode_test(WSP.URIC, [i], String.fromCharCode(i)); |
|
241 } else { |
|
242 wsp_decode_test(WSP.URIC, [i], null, "CodeError"); |
|
243 } |
|
244 } |
|
245 |
|
246 run_next_test(); |
|
247 }); |
|
248 |
|
249 // |
|
250 // Test target: TextString |
|
251 // |
|
252 |
|
253 //// TextString.decode //// |
|
254 |
|
255 add_test(function test_TextString_decode() { |
|
256 // Test quoted string |
|
257 wsp_decode_test(WSP.TextString, [127, 128, 0], String.fromCharCode(128)); |
|
258 // Test illegal quoted string |
|
259 wsp_decode_test(WSP.TextString, [127, 32, 0], null, "CodeError"); |
|
260 // Test illegal unquoted string |
|
261 wsp_decode_test(WSP.TextString, [128, 0], null, "CodeError"); |
|
262 // Test normal string |
|
263 wsp_decode_test(WSP.TextString, [32, 0], " "); |
|
264 |
|
265 run_next_test(); |
|
266 }); |
|
267 |
|
268 //// TextString.encode //// |
|
269 |
|
270 add_test(function test_TextString_encode() { |
|
271 // Test quoted string |
|
272 wsp_encode_test(WSP.TextString, String.fromCharCode(128), [127, 128, 0]); |
|
273 // Test normal string |
|
274 wsp_encode_test(WSP.TextString, "Mozilla", strToCharCodeArray("Mozilla")); |
|
275 |
|
276 run_next_test(); |
|
277 }); |
|
278 |
|
279 // |
|
280 // Test target: TokenText |
|
281 // |
|
282 |
|
283 //// TokenText.decode //// |
|
284 |
|
285 add_test(function test_TokenText_decode() { |
|
286 wsp_decode_test(WSP.TokenText, [65], null, "RangeError"); |
|
287 wsp_decode_test(WSP.TokenText, [0], ""); |
|
288 wsp_decode_test(WSP.TokenText, [65, 0], "A"); |
|
289 |
|
290 run_next_test(); |
|
291 }); |
|
292 |
|
293 //// TokenText.encode //// |
|
294 |
|
295 add_test(function test_TokenText_encode() { |
|
296 wsp_encode_test(WSP.TokenText, "B2G", strToCharCodeArray("B2G")); |
|
297 |
|
298 run_next_test(); |
|
299 }); |
|
300 |
|
301 // |
|
302 // Test target: QuotedString |
|
303 // |
|
304 |
|
305 //// QuotedString.decode //// |
|
306 |
|
307 add_test(function test_QuotedString_decode() { |
|
308 // Test non-quoted string |
|
309 wsp_decode_test(WSP.QuotedString, [32, 0], null, "CodeError"); |
|
310 // Test incompleted string |
|
311 wsp_decode_test(WSP.QuotedString, [34, 32], null, "RangeError"); |
|
312 wsp_decode_test(WSP.QuotedString, [34, 32, 0], " "); |
|
313 |
|
314 run_next_test(); |
|
315 }); |
|
316 |
|
317 //// QuotedString.encode //// |
|
318 |
|
319 add_test(function test_QuotedString_encode() { |
|
320 wsp_encode_test(WSP.QuotedString, "B2G", [34].concat(strToCharCodeArray("B2G"))); |
|
321 |
|
322 run_next_test(); |
|
323 }); |
|
324 |
|
325 // |
|
326 // Test target: ShortInteger |
|
327 // |
|
328 |
|
329 //// ShortInteger.decode //// |
|
330 |
|
331 add_test(function test_ShortInteger_decode() { |
|
332 for (let i = 0; i < 256; i++) { |
|
333 if (i & 0x80) { |
|
334 wsp_decode_test(WSP.ShortInteger, [i], i & 0x7F); |
|
335 } else { |
|
336 wsp_decode_test(WSP.ShortInteger, [i], null, "CodeError"); |
|
337 } |
|
338 } |
|
339 |
|
340 run_next_test(); |
|
341 }); |
|
342 |
|
343 //// ShortInteger.encode //// |
|
344 |
|
345 add_test(function test_ShortInteger_encode() { |
|
346 for (let i = 0; i < 256; i++) { |
|
347 if (i & 0x80) { |
|
348 wsp_encode_test(WSP.ShortInteger, i, null, "CodeError"); |
|
349 } else { |
|
350 wsp_encode_test(WSP.ShortInteger, i, [0x80 | i]); |
|
351 } |
|
352 } |
|
353 |
|
354 run_next_test(); |
|
355 }); |
|
356 |
|
357 // |
|
358 // Test target: LongInteger |
|
359 // |
|
360 |
|
361 //// LongInteger.decode //// |
|
362 |
|
363 function LongInteger_decode_testcases(target) { |
|
364 // Test LongInteger of zero octet |
|
365 wsp_decode_test(target, [0, 0], null, "CodeError"); |
|
366 wsp_decode_test(target, [1, 0x80], 0x80); |
|
367 wsp_decode_test(target, [2, 0x80, 2], 0x8002); |
|
368 wsp_decode_test(target, [3, 0x80, 2, 3], 0x800203); |
|
369 wsp_decode_test(target, [4, 0x80, 2, 3, 4], 0x80020304); |
|
370 wsp_decode_test(target, [5, 0x80, 2, 3, 4, 5], 0x8002030405); |
|
371 wsp_decode_test(target, [6, 0x80, 2, 3, 4, 5, 6], 0x800203040506); |
|
372 // Test LongInteger of more than 6 octets |
|
373 wsp_decode_test(target, [7, 0x80, 2, 3, 4, 5, 6, 7], [0x80, 2, 3, 4, 5, 6, 7]); |
|
374 // Test LongInteger of more than 30 octets |
|
375 wsp_decode_test(target, [31], null, "CodeError"); |
|
376 } |
|
377 add_test(function test_LongInteger_decode() { |
|
378 LongInteger_decode_testcases(WSP.LongInteger); |
|
379 |
|
380 run_next_test(); |
|
381 }); |
|
382 |
|
383 //// LongInteger.encode //// |
|
384 |
|
385 function LongInteger_encode_testcases(target) { |
|
386 wsp_encode_test(target, 0x80, [1, 0x80]); |
|
387 wsp_encode_test(target, 0x8002, [2, 0x80, 2]); |
|
388 wsp_encode_test(target, 0x800203, [3, 0x80, 2, 3]); |
|
389 wsp_encode_test(target, 0x80020304, [4, 0x80, 2, 3, 4]); |
|
390 wsp_encode_test(target, 0x8002030405, [5, 0x80, 2, 3, 4, 5]); |
|
391 wsp_encode_test(target, 0x800203040506, [6, 0x80, 2, 3, 4, 5, 6]); |
|
392 // Test LongInteger of more than 6 octets |
|
393 wsp_encode_test(target, 0x1000000000000, null, "CodeError"); |
|
394 // Test input empty array |
|
395 wsp_encode_test(target, [], null, "CodeError"); |
|
396 // Test input octets array of length 1..30 |
|
397 let array = []; |
|
398 for (let i = 1; i <= 30; i++) { |
|
399 array.push(i); |
|
400 wsp_encode_test(target, array, [i].concat(array)); |
|
401 } |
|
402 // Test input octets array of 31 elements. |
|
403 array.push(31); |
|
404 wsp_encode_test(target, array, null, "CodeError"); |
|
405 } |
|
406 add_test(function test_LongInteger_encode() { |
|
407 wsp_encode_test(WSP.LongInteger, 0, [1, 0]); |
|
408 |
|
409 LongInteger_encode_testcases(WSP.LongInteger); |
|
410 |
|
411 run_next_test(); |
|
412 }); |
|
413 |
|
414 // |
|
415 // Test target: UintVar |
|
416 // |
|
417 |
|
418 //// UintVar.decode //// |
|
419 |
|
420 add_test(function test_UintVar_decode() { |
|
421 wsp_decode_test(WSP.UintVar, [0x80], null, "RangeError"); |
|
422 // Test up to max 53 bits integer |
|
423 wsp_decode_test(WSP.UintVar, [0x7F], 0x7F); |
|
424 wsp_decode_test(WSP.UintVar, [0xFF, 0x7F], 0x3FFF); |
|
425 wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0x7F], 0x1FFFFF); |
|
426 wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0xFF, 0x7F], 0xFFFFFFF); |
|
427 wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0xFF, 0xFF, 0x7F], 0x7FFFFFFFF); |
|
428 wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F], 0x3FFFFFFFFFF); |
|
429 wsp_decode_test(WSP.UintVar, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F], 0x1FFFFFFFFFFFF); |
|
430 wsp_decode_test(WSP.UintVar, [0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F], 0x1FFFFFFFFFFFFF); |
|
431 wsp_decode_test(WSP.UintVar, [0x01, 0x02], 1); |
|
432 wsp_decode_test(WSP.UintVar, [0x80, 0x01, 0x02], 1); |
|
433 wsp_decode_test(WSP.UintVar, [0x80, 0x80, 0x80, 0x01, 0x2], 1); |
|
434 |
|
435 run_next_test(); |
|
436 }); |
|
437 |
|
438 //// UintVar.encode //// |
|
439 |
|
440 add_test(function test_UintVar_encode() { |
|
441 // Test up to max 53 bits integer |
|
442 wsp_encode_test(WSP.UintVar, 0, [0]); |
|
443 wsp_encode_test(WSP.UintVar, 0x7F, [0x7F]); |
|
444 wsp_encode_test(WSP.UintVar, 0x3FFF, [0xFF, 0x7F]); |
|
445 wsp_encode_test(WSP.UintVar, 0x1FFFFF, [0xFF, 0xFF, 0x7F]); |
|
446 wsp_encode_test(WSP.UintVar, 0xFFFFFFF, [0xFF, 0xFF, 0xFF, 0x7F]); |
|
447 wsp_encode_test(WSP.UintVar, 0x7FFFFFFFF, [0xFF, 0xFF, 0xFF, 0xFF, 0x7F]); |
|
448 wsp_encode_test(WSP.UintVar, 0x3FFFFFFFFFF, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]); |
|
449 wsp_encode_test(WSP.UintVar, 0x1FFFFFFFFFFFF, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]); |
|
450 wsp_encode_test(WSP.UintVar, 0x1FFFFFFFFFFFFF, [0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]); |
|
451 |
|
452 run_next_test(); |
|
453 }); |
|
454 |
|
455 // |
|
456 // Test target: ConstrainedEncoding |
|
457 // |
|
458 |
|
459 //// ConstrainedEncoding.decode //// |
|
460 |
|
461 add_test(function test_ConstrainedEncoding_decode() { |
|
462 wsp_decode_test(WSP.ConstrainedEncoding, [0x80], 0); |
|
463 wsp_decode_test(WSP.ConstrainedEncoding, [32, 0], " "); |
|
464 |
|
465 run_next_test(); |
|
466 }); |
|
467 |
|
468 //// ConstrainedEncoding.encode //// |
|
469 |
|
470 add_test(function test_ConstrainedEncoding_encode() { |
|
471 wsp_encode_test(WSP.ConstrainedEncoding, 0, [0x80 | 0]); |
|
472 wsp_encode_test(WSP.ConstrainedEncoding, "A", [65, 0]); |
|
473 |
|
474 run_next_test(); |
|
475 }); |
|
476 |
|
477 // |
|
478 // Test target: ValueLength |
|
479 // |
|
480 |
|
481 //// ValueLength.decode //// |
|
482 |
|
483 add_test(function test_ValueLength_decode() { |
|
484 for (let i = 0; i < 256; i++) { |
|
485 if (i < 31) { |
|
486 wsp_decode_test(WSP.ValueLength, [i, 0x8F, 0x7F], i); |
|
487 } else if (i == 31) { |
|
488 wsp_decode_test(WSP.ValueLength, [i, 0x8F, 0x7F], 0x7FF); |
|
489 } else { |
|
490 wsp_decode_test(WSP.ValueLength, [i, 0x8F, 0x7F], null, "CodeError"); |
|
491 } |
|
492 } |
|
493 |
|
494 run_next_test(); |
|
495 }); |
|
496 |
|
497 //// ValueLength.encode //// |
|
498 |
|
499 add_test(function test_ValueLength_encode() { |
|
500 for (let i = 0; i < 256; i++) { |
|
501 if (i < 31) { |
|
502 wsp_encode_test(WSP.ValueLength, i, [i]); |
|
503 } else if (i < 128) { |
|
504 wsp_encode_test(WSP.ValueLength, i, [31, i]); |
|
505 } else { |
|
506 wsp_encode_test(WSP.ValueLength, i, [31, (0x80 | (i / 128)), i % 128]); |
|
507 } |
|
508 } |
|
509 |
|
510 run_next_test(); |
|
511 }); |
|
512 |
|
513 // |
|
514 // Test target: NoValue |
|
515 // |
|
516 |
|
517 //// NoValue.decode //// |
|
518 |
|
519 add_test(function test_NoValue_decode() { |
|
520 wsp_decode_test(WSP.NoValue, [0], null); |
|
521 for (let i = 1; i < 256; i++) { |
|
522 wsp_decode_test(WSP.NoValue, [i], null, "CodeError"); |
|
523 } |
|
524 |
|
525 run_next_test(); |
|
526 }); |
|
527 |
|
528 //// NoValue.encode //// |
|
529 |
|
530 add_test(function test_NoValue_encode() { |
|
531 wsp_encode_test(WSP.NoValue, undefined, [0]); |
|
532 wsp_encode_test(WSP.NoValue, null, [0]); |
|
533 wsp_encode_test(WSP.NoValue, 0, null, "CodeError"); |
|
534 wsp_encode_test(WSP.NoValue, "", null, "CodeError"); |
|
535 wsp_encode_test(WSP.NoValue, [], null, "CodeError"); |
|
536 wsp_encode_test(WSP.NoValue, {}, null, "CodeError"); |
|
537 |
|
538 run_next_test(); |
|
539 }); |
|
540 |
|
541 // |
|
542 // Test target: TextValue |
|
543 // |
|
544 |
|
545 //// TextValue.decode //// |
|
546 |
|
547 add_test(function test_TextValue_decode() { |
|
548 wsp_decode_test(WSP.TextValue, [0], null); |
|
549 wsp_decode_test(WSP.TextValue, [65, 0], "A"); |
|
550 wsp_decode_test(WSP.TextValue, [32, 0], null, "CodeError"); |
|
551 wsp_decode_test(WSP.TextValue, [34, 32, 0], " "); |
|
552 |
|
553 run_next_test(); |
|
554 }); |
|
555 |
|
556 //// TextValue.encode //// |
|
557 |
|
558 add_test(function test_TextValue_encode() { |
|
559 wsp_encode_test(WSP.TextValue, undefined, [0]); |
|
560 wsp_encode_test(WSP.TextValue, null, [0]); |
|
561 wsp_encode_test(WSP.TextValue, "", [0]); |
|
562 wsp_encode_test(WSP.TextValue, "A", [65, 0]); |
|
563 wsp_encode_test(WSP.TextValue, "\x80", [34, 128, 0]); |
|
564 |
|
565 run_next_test(); |
|
566 }); |
|
567 |
|
568 // |
|
569 // Test target: IntegerValue |
|
570 // |
|
571 |
|
572 //// IntegerValue.decode //// |
|
573 |
|
574 add_test(function test_IntegerValue_decode() { |
|
575 for (let i = 128; i < 256; i++) { |
|
576 wsp_decode_test(WSP.IntegerValue, [i], i & 0x7F); |
|
577 } |
|
578 |
|
579 LongInteger_decode_testcases(WSP.IntegerValue); |
|
580 |
|
581 run_next_test(); |
|
582 }); |
|
583 |
|
584 //// IntegerValue.decode //// |
|
585 |
|
586 add_test(function test_IntegerValue_encode() { |
|
587 for (let i = 0; i < 128; i++) { |
|
588 wsp_encode_test(WSP.IntegerValue, i, [0x80 | i]); |
|
589 } |
|
590 |
|
591 LongInteger_encode_testcases(WSP.IntegerValue); |
|
592 |
|
593 run_next_test(); |
|
594 }); |
|
595 |
|
596 // |
|
597 // Test target: DateValue |
|
598 // |
|
599 |
|
600 //// DateValue.decode //// |
|
601 |
|
602 add_test(function test_DateValue_decode() { |
|
603 wsp_decode_test(WSP.DateValue, [0, 0], null, "CodeError"); |
|
604 wsp_decode_test(WSP.DateValue, [1, 0x80], new Date(0x80 * 1000)); |
|
605 wsp_decode_test(WSP.DateValue, [31], null, "CodeError"); |
|
606 |
|
607 run_next_test(); |
|
608 }); |
|
609 |
|
610 //// DateValue.encode //// |
|
611 |
|
612 add_test(function test_DateValue_encode() { |
|
613 wsp_encode_test(WSP.DateValue, new Date(0x80 * 1000), [1, 0x80]); |
|
614 |
|
615 run_next_test(); |
|
616 }); |
|
617 |
|
618 // |
|
619 // Test target: DeltaSecondsValue |
|
620 // |
|
621 // DeltaSecondsValue is only an alias of IntegerValue. |
|
622 |
|
623 // |
|
624 // Test target: QValue |
|
625 // |
|
626 |
|
627 //// QValue.decode //// |
|
628 |
|
629 add_test(function test_QValue_decode() { |
|
630 wsp_decode_test(WSP.QValue, [0], null, "CodeError"); |
|
631 wsp_decode_test(WSP.QValue, [1], 0); |
|
632 wsp_decode_test(WSP.QValue, [100], 0.99); |
|
633 wsp_decode_test(WSP.QValue, [101], 0.001); |
|
634 wsp_decode_test(WSP.QValue, [0x88, 0x4B], 0.999); |
|
635 wsp_decode_test(WSP.QValue, [0x88, 0x4C], null, "CodeError"); |
|
636 |
|
637 run_next_test(); |
|
638 }); |
|
639 |
|
640 //// QValue.encode //// |
|
641 |
|
642 add_test(function test_QValue_encode() { |
|
643 wsp_encode_test(WSP.QValue, 0, [1]); |
|
644 wsp_encode_test(WSP.QValue, 0.99, [100]); |
|
645 wsp_encode_test(WSP.QValue, 0.001, [101]); |
|
646 wsp_encode_test(WSP.QValue, 0.999, [0x88, 0x4B]); |
|
647 wsp_encode_test(WSP.QValue, 1, null, "CodeError"); |
|
648 |
|
649 run_next_test(); |
|
650 }); |
|
651 |
|
652 // |
|
653 // Test target: VersionValue |
|
654 // |
|
655 |
|
656 //// VersionValue.decode //// |
|
657 |
|
658 add_test(function test_VersionValue_decode() { |
|
659 for (let major = 1; major < 8; major++) { |
|
660 let version = (major << 4) | 0x0F; |
|
661 wsp_decode_test(WSP.VersionValue, [0x80 | version], version); |
|
662 wsp_decode_test(WSP.VersionValue, [major + 0x30, 0], version); |
|
663 |
|
664 for (let minor = 0; minor < 15; minor++) { |
|
665 version = (major << 4) | minor; |
|
666 wsp_decode_test(WSP.VersionValue, [0x80 | version], version); |
|
667 if (minor >= 10) { |
|
668 wsp_decode_test(WSP.VersionValue, [major + 0x30, 0x2E, 0x31, (minor - 10) + 0x30, 0], version); |
|
669 } else { |
|
670 wsp_decode_test(WSP.VersionValue, [major + 0x30, 0x2E, minor + 0x30, 0], version); |
|
671 } |
|
672 } |
|
673 } |
|
674 |
|
675 run_next_test(); |
|
676 }); |
|
677 |
|
678 //// VersionValue.encode //// |
|
679 |
|
680 add_test(function test_VersionValue_encode() { |
|
681 for (let major = 1; major < 8; major++) { |
|
682 let version = (major << 4) | 0x0F; |
|
683 wsp_encode_test(WSP.VersionValue, version, [0x80 | version]); |
|
684 |
|
685 for (let minor = 0; minor < 15; minor++) { |
|
686 version = (major << 4) | minor; |
|
687 wsp_encode_test(WSP.VersionValue, version, [0x80 | version]); |
|
688 } |
|
689 } |
|
690 |
|
691 run_next_test(); |
|
692 }); |
|
693 |
|
694 // |
|
695 // Test target: UriValue |
|
696 // |
|
697 |
|
698 //// UriValue.decode //// |
|
699 |
|
700 add_test(function test_UriValue_decode() { |
|
701 wsp_decode_test(WSP.UriValue, [97], null, "RangeError"); |
|
702 wsp_decode_test(WSP.UriValue, [0], ""); |
|
703 wsp_decode_test(WSP.UriValue, [65, 0], "A"); |
|
704 |
|
705 run_next_test(); |
|
706 }); |
|
707 |
|
708 // |
|
709 // Test target: TypeValue |
|
710 // |
|
711 |
|
712 //// TypeValue.decode //// |
|
713 |
|
714 add_test(function test_TypeValue_decode() { |
|
715 // Test for string-typed return value from ConstrainedEncoding |
|
716 wsp_decode_test(WSP.TypeValue, [65, 0], "a"); |
|
717 // Test for number-typed return value from ConstrainedEncoding |
|
718 wsp_decode_test(WSP.TypeValue, [0x33 | 0x80], |
|
719 "application/vnd.wap.multipart.related"); |
|
720 wsp_decode_test(WSP.TypeValue, [0x1d | 0x80], "image/gif"); |
|
721 // Test for NotWellKnownEncodingError |
|
722 wsp_decode_test(WSP.TypeValue, [0x59 | 0x80], null, "NotWellKnownEncodingError"); |
|
723 |
|
724 run_next_test(); |
|
725 }); |
|
726 |
|
727 //// TypeValue.encode //// |
|
728 |
|
729 add_test(function test_TypeValue_encode() { |
|
730 wsp_encode_test(WSP.TypeValue, "no/such.type", |
|
731 [110, 111, 47, 115, 117, 99, 104, 46, 116, 121, 112, 101, 0]); |
|
732 wsp_encode_test(WSP.TypeValue, "application/vnd.wap.multipart.related", |
|
733 [0x33 | 0x80]); |
|
734 wsp_encode_test(WSP.TypeValue, "image/gif", |
|
735 [0x1d | 0x80]); |
|
736 |
|
737 run_next_test(); |
|
738 }); |
|
739 |
|
740 // |
|
741 // Test target: Parameter |
|
742 // |
|
743 |
|
744 //// Parameter.decodeTypedParameter //// |
|
745 |
|
746 add_test(function test_Parameter_decodeTypedParameter() { |
|
747 function func(data) { |
|
748 return WSP.Parameter.decodeTypedParameter(data); |
|
749 } |
|
750 |
|
751 // Test for array-typed return value from IntegerValue |
|
752 wsp_decode_test_ex(func, [7, 0, 0, 0, 0, 0, 0, 0], null, "CodeError"); |
|
753 // Test for number-typed return value from IntegerValue |
|
754 wsp_decode_test_ex(func, [1, 0, 0], {name: "q", value: null}); |
|
755 // Test for NotWellKnownEncodingError |
|
756 wsp_decode_test_ex(func, [1, 0xFF], null, "NotWellKnownEncodingError"); |
|
757 // Test for parameter specific decoder |
|
758 wsp_decode_test_ex(func, [1, 0, 100], {name: "q", value: 0.99}); |
|
759 // Test for TextValue |
|
760 wsp_decode_test_ex(func, [1, 0x10, 48, 46, 57, 57, 0], |
|
761 {name: "secure", value: "0.99"}); |
|
762 // Test for TextString |
|
763 wsp_decode_test_ex(func, [1, 0x0A, 60, 115, 109, 105, 108, 62, 0], |
|
764 {name: "start", value: "<smil>"}); |
|
765 // Test for skipValue |
|
766 wsp_decode_test_ex(func, [1, 0x0A, 128], null); |
|
767 |
|
768 run_next_test(); |
|
769 }); |
|
770 |
|
771 //// Parameter.decodeUntypedParameter //// |
|
772 |
|
773 add_test(function test_Parameter_decodeUntypedParameter() { |
|
774 function func (data) { |
|
775 return WSP.Parameter.decodeUntypedParameter(data); |
|
776 } |
|
777 |
|
778 wsp_decode_test_ex(func, [1], null, "CodeError"); |
|
779 wsp_decode_test_ex(func, [65, 0, 0], {name: "a", value: null}); |
|
780 // Test for IntegerValue |
|
781 wsp_decode_test_ex(func, [65, 0, 1, 0], {name: "a", value: 0}); |
|
782 // Test for TextValue |
|
783 wsp_decode_test_ex(func, [65, 0, 66, 0], {name: "a", value: "B"}); |
|
784 |
|
785 run_next_test(); |
|
786 }); |
|
787 |
|
788 //// Parameter.decode //// |
|
789 |
|
790 add_test(function test_Parameter_decode() { |
|
791 wsp_decode_test(WSP.Parameter, [1, 0x0A, 60, 115, 109, 105, 108, 62, 0], |
|
792 {name: "start", value: "<smil>"}); |
|
793 wsp_decode_test(WSP.Parameter, [65, 0, 66, 0], {name: "a", value: "B"}); |
|
794 |
|
795 run_next_test(); |
|
796 }); |
|
797 |
|
798 //// Parameter.decodeMultiple //// |
|
799 |
|
800 add_test(function test_Parameter_decodeMultiple() { |
|
801 wsp_decode_test_ex(function(data) { |
|
802 return WSP.Parameter.decodeMultiple(data, 13); |
|
803 }, [1, 0x0A, 60, 115, 109, 105, 108, 62, 0, 65, 0, 66, 0], {start: "<smil>", a: "B"} |
|
804 ); |
|
805 |
|
806 run_next_test(); |
|
807 }); |
|
808 |
|
809 //// Parameter.encodeTypedParameter //// |
|
810 |
|
811 add_test(function test_Parameter_encodeTypedParameter() { |
|
812 function func(data, input) { |
|
813 WSP.Parameter.encodeTypedParameter(data, input); |
|
814 return data.array; |
|
815 } |
|
816 |
|
817 // Test for NotWellKnownEncodingError |
|
818 wsp_encode_test_ex(func, {name: "xxx", value: 0}, null, "NotWellKnownEncodingError"); |
|
819 wsp_encode_test_ex(func, {name: "q", value: 0}, [0x80, 1]); |
|
820 wsp_encode_test_ex(func, {name: "name", value: "A"}, [0x85, 65, 0]); |
|
821 |
|
822 run_next_test(); |
|
823 }); |
|
824 |
|
825 //// Parameter.encodeUntypedParameter //// |
|
826 |
|
827 add_test(function test_Parameter_encodeUntypedParameter() { |
|
828 function func(data, input) { |
|
829 WSP.Parameter.encodeUntypedParameter(data, input); |
|
830 return data.array; |
|
831 } |
|
832 |
|
833 wsp_encode_test_ex(func, {name: "q", value: 0}, [113, 0, 0x80]); |
|
834 wsp_encode_test_ex(func, {name: "name", value: "A"}, [110, 97, 109, 101, 0, 65, 0]); |
|
835 |
|
836 run_next_test(); |
|
837 }); |
|
838 |
|
839 //// Parameter.encodeMultiple //// |
|
840 |
|
841 add_test(function test_Parameter_encodeMultiple() { |
|
842 function func(data, input) { |
|
843 WSP.Parameter.encodeMultiple(data, input); |
|
844 return data.array; |
|
845 } |
|
846 |
|
847 wsp_encode_test_ex(func, {q: 0, n: "A"}, [0x80, 1, 110, 0, 65, 0]); |
|
848 |
|
849 run_next_test(); |
|
850 }); |
|
851 |
|
852 //// Parameter.encode //// |
|
853 |
|
854 add_test(function test_Parameter_encode() { |
|
855 |
|
856 wsp_encode_test(WSP.Parameter, {name: "q", value: 0}, [0x80, 1]); |
|
857 wsp_encode_test(WSP.Parameter, {name: "n", value: "A"}, [110, 0, 65, 0]); |
|
858 |
|
859 run_next_test(); |
|
860 }); |
|
861 |
|
862 // |
|
863 // Test target: Header |
|
864 // |
|
865 |
|
866 //// Header.decode //// |
|
867 |
|
868 add_test(function test_Header_decode() { |
|
869 wsp_decode_test(WSP.Header, [0x34 | 0x80, 0x80], {name: "push-flag", value: 0}); |
|
870 wsp_decode_test(WSP.Header, [65, 0, 66, 0], {name: "a", value: "B"}); |
|
871 |
|
872 run_next_test(); |
|
873 }); |
|
874 |
|
875 // |
|
876 // Test target: WellKnownHeader |
|
877 // |
|
878 |
|
879 //// WellKnownHeader.decode //// |
|
880 |
|
881 add_test(function test_WellKnownHeader_decode() { |
|
882 wsp_decode_test(WSP.WellKnownHeader, [0xFF], null, "NotWellKnownEncodingError"); |
|
883 let (entry = WSP.WSP_HEADER_FIELDS["push-flag"]) { |
|
884 // Test for Short-Integer |
|
885 wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 0x80], |
|
886 {name: entry.name, value: 0}); |
|
887 // Test for NoValue |
|
888 wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 0], |
|
889 {name: entry.name, value: null}); |
|
890 // Test for TokenText |
|
891 wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 65, 0], |
|
892 {name: entry.name, value: "A"}); |
|
893 // Test for QuotedString |
|
894 wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 34, 128, 0], |
|
895 {name: entry.name, value: String.fromCharCode(128)}); |
|
896 // Test for skipValue |
|
897 wsp_decode_test(WSP.WellKnownHeader, [entry.number | 0x80, 2, 0, 0], null); |
|
898 } |
|
899 |
|
900 run_next_test(); |
|
901 }); |
|
902 |
|
903 // |
|
904 // Test target: ApplicationHeader |
|
905 // |
|
906 |
|
907 //// ApplicationHeader.decode //// |
|
908 |
|
909 add_test(function test_ApplicationHeader_decode() { |
|
910 wsp_decode_test(WSP.ApplicationHeader, [5, 0, 66, 0], null, "CodeError"); |
|
911 wsp_decode_test(WSP.ApplicationHeader, [65, 0, 66, 0], {name: "a", value: "B"}); |
|
912 // Test for skipValue |
|
913 wsp_decode_test(WSP.ApplicationHeader, [65, 0, 2, 0, 0], null); |
|
914 |
|
915 run_next_test(); |
|
916 }); |
|
917 |
|
918 //// ApplicationHeader.encode //// |
|
919 |
|
920 add_test(function test_ApplicationHeader_encode() { |
|
921 // Test invalid header name string: |
|
922 wsp_encode_test(WSP.ApplicationHeader, {name: undefined, value: "asdf"}, null, "CodeError"); |
|
923 wsp_encode_test(WSP.ApplicationHeader, {name: null, value: "asdf"}, null, "CodeError"); |
|
924 wsp_encode_test(WSP.ApplicationHeader, {name: "", value: "asdf"}, null, "CodeError"); |
|
925 wsp_encode_test(WSP.ApplicationHeader, {name: "a b", value: "asdf"}, null, "CodeError"); |
|
926 // Test value string: |
|
927 wsp_encode_test(WSP.ApplicationHeader, {name: "asdf", value: undefined}, |
|
928 strToCharCodeArray("asdf").concat([0])); |
|
929 wsp_encode_test(WSP.ApplicationHeader, {name: "asdf", value: null}, |
|
930 strToCharCodeArray("asdf").concat([0])); |
|
931 wsp_encode_test(WSP.ApplicationHeader, {name: "asdf", value: ""}, |
|
932 strToCharCodeArray("asdf").concat([0])); |
|
933 wsp_encode_test(WSP.ApplicationHeader, {name: "asdf", value: "fdsa"}, |
|
934 strToCharCodeArray("asdf").concat(strToCharCodeArray("fdsa"))); |
|
935 |
|
936 run_next_test(); |
|
937 }); |
|
938 |
|
939 // |
|
940 // Test target: FieldName |
|
941 // |
|
942 |
|
943 //// FieldName.decode //// |
|
944 |
|
945 add_test(function test_FieldName_decode() { |
|
946 wsp_decode_test(WSP.FieldName, [0], ""); |
|
947 wsp_decode_test(WSP.FieldName, [65, 0], "a"); |
|
948 wsp_decode_test(WSP.FieldName, [97, 0], "a"); |
|
949 let (entry = WSP.WSP_HEADER_FIELDS["content-length"]) { |
|
950 wsp_decode_test(WSP.FieldName, [entry.number | 0x80], entry.name); |
|
951 } |
|
952 wsp_decode_test(WSP.FieldName, [0xFF], null, "NotWellKnownEncodingError"); |
|
953 |
|
954 run_next_test(); |
|
955 }); |
|
956 |
|
957 //// FieldName.encode //// |
|
958 |
|
959 add_test(function test_FieldName_encode() { |
|
960 wsp_encode_test(WSP.FieldName, "", [0]); |
|
961 wsp_encode_test(WSP.FieldName, "date", [0x92]); |
|
962 |
|
963 run_next_test(); |
|
964 }); |
|
965 |
|
966 // |
|
967 // Test target: AcceptCharsetValue |
|
968 // |
|
969 |
|
970 //// AcceptCharsetValue.decode //// |
|
971 |
|
972 add_test(function test_AcceptCharsetValue_decode() { |
|
973 wsp_decode_test(WSP.AcceptCharsetValue, [0xFF], null, "CodeError"); |
|
974 // Test for Any-Charset |
|
975 wsp_decode_test(WSP.AcceptCharsetValue, [128], {charset: "*"}); |
|
976 // Test for Constrained-Charset |
|
977 wsp_decode_test(WSP.AcceptCharsetValue, [65, 0], {charset: "A"}); |
|
978 let (entry = WSP.WSP_WELL_KNOWN_CHARSETS["utf-8"]) { |
|
979 wsp_decode_test(WSP.AcceptCharsetValue, [entry.number | 0x80], {charset: entry.name}); |
|
980 } |
|
981 // Test for Accept-Charset-General-Form |
|
982 wsp_decode_test(WSP.AcceptCharsetValue, [1, 128], {charset: "*"}); |
|
983 let (entry = WSP.WSP_WELL_KNOWN_CHARSETS["utf-8"]) { |
|
984 wsp_decode_test(WSP.AcceptCharsetValue, [2, 1, entry.number], {charset: entry.name}); |
|
985 wsp_decode_test(WSP.AcceptCharsetValue, [1, entry.number | 0x80], {charset: entry.name}); |
|
986 } |
|
987 wsp_decode_test(WSP.AcceptCharsetValue, [3, 65, 0, 100], {charset: "A", q: 0.99}); |
|
988 |
|
989 run_next_test(); |
|
990 }); |
|
991 |
|
992 //// AcceptCharsetValue.encodeAnyCharset //// |
|
993 |
|
994 add_test(function test_AcceptCharsetValue_encodeAnyCharset() { |
|
995 function func(data, input) { |
|
996 WSP.AcceptCharsetValue.encodeAnyCharset(data, input); |
|
997 return data.array; |
|
998 } |
|
999 |
|
1000 wsp_encode_test_ex(func, null, [0x80]); |
|
1001 wsp_encode_test_ex(func, undefined, [0x80]); |
|
1002 wsp_encode_test_ex(func, {}, [0x80]); |
|
1003 wsp_encode_test_ex(func, {charset: null}, [0x80]); |
|
1004 wsp_encode_test_ex(func, {charset: "*"}, [0x80]); |
|
1005 wsp_encode_test_ex(func, {charset: "en"}, null, "CodeError"); |
|
1006 |
|
1007 run_next_test(); |
|
1008 }); |
|
1009 |
|
1010 // |
|
1011 // Test target: WellKnownCharset |
|
1012 // |
|
1013 |
|
1014 //// WellKnownCharset.decode //// |
|
1015 |
|
1016 add_test(function test_WellKnownCharset_decode() { |
|
1017 wsp_decode_test(WSP.WellKnownCharset, [0xFF], null, "NotWellKnownEncodingError"); |
|
1018 // Test for Any-Charset |
|
1019 wsp_decode_test(WSP.WellKnownCharset, [128], {charset: "*"}); |
|
1020 // Test for number-typed return value from IntegerValue |
|
1021 wsp_decode_test(WSP.WellKnownCharset, [1, 3], {charset: "us-ascii"}); |
|
1022 wsp_decode_test(WSP.WellKnownCharset, [1, 4], {charset: "iso-8859-1"}); |
|
1023 wsp_decode_test(WSP.WellKnownCharset, [1, 5], {charset: "iso-8859-2"}); |
|
1024 wsp_decode_test(WSP.WellKnownCharset, [1, 6], {charset: "iso-8859-3"}); |
|
1025 wsp_decode_test(WSP.WellKnownCharset, [1, 7], {charset: "iso-8859-4"}); |
|
1026 wsp_decode_test(WSP.WellKnownCharset, [1, 8], {charset: "iso-8859-5"}); |
|
1027 wsp_decode_test(WSP.WellKnownCharset, [1, 9], {charset: "iso-8859-6"}); |
|
1028 wsp_decode_test(WSP.WellKnownCharset, [1, 10], {charset: "iso-8859-7"}); |
|
1029 wsp_decode_test(WSP.WellKnownCharset, [1, 11], {charset: "iso-8859-8"}); |
|
1030 wsp_decode_test(WSP.WellKnownCharset, [1, 12], {charset: "iso-8859-9"}); |
|
1031 wsp_decode_test(WSP.WellKnownCharset, [1, 13], {charset: "iso-8859-10"}); |
|
1032 wsp_decode_test(WSP.WellKnownCharset, [1, 17], {charset: "shift_jis"}); |
|
1033 wsp_decode_test(WSP.WellKnownCharset, [1, 18], {charset: "euc-jp"}); |
|
1034 wsp_decode_test(WSP.WellKnownCharset, [1, 37], {charset: "iso-2022-kr"}); |
|
1035 wsp_decode_test(WSP.WellKnownCharset, [1, 38], {charset: "euc-kr"}); |
|
1036 wsp_decode_test(WSP.WellKnownCharset, [1, 39], {charset: "iso-2022-jp"}); |
|
1037 wsp_decode_test(WSP.WellKnownCharset, [1, 40], {charset: "iso-2022-jp-2"}); |
|
1038 wsp_decode_test(WSP.WellKnownCharset, [1, 81], {charset: "iso-8859-6-e"}); |
|
1039 wsp_decode_test(WSP.WellKnownCharset, [1, 82], {charset: "iso-8859-6-i"}); |
|
1040 wsp_decode_test(WSP.WellKnownCharset, [1, 84], {charset: "iso-8859-8-e"}); |
|
1041 wsp_decode_test(WSP.WellKnownCharset, [1, 85], {charset: "iso-8859-8-i"}); |
|
1042 wsp_decode_test(WSP.WellKnownCharset, [1, 1000], {charset: "iso-10646-ucs-2"}); |
|
1043 wsp_decode_test(WSP.WellKnownCharset, [1, 1015], {charset: "utf-16"}); |
|
1044 wsp_decode_test(WSP.WellKnownCharset, [1, 2025], {charset: "gb2312"}); |
|
1045 wsp_decode_test(WSP.WellKnownCharset, [1, 2026], {charset: "big5"}); |
|
1046 wsp_decode_test(WSP.WellKnownCharset, [1, 2084], {charset: "koi8-r"}); |
|
1047 wsp_decode_test(WSP.WellKnownCharset, [1, 2252], {charset: "windows-1252"}); |
|
1048 wsp_decode_test(WSP.WellKnownCharset, [2, 3, 247], {charset: "utf-16"}); |
|
1049 // Test for array-typed return value from IntegerValue |
|
1050 wsp_decode_test(WSP.WellKnownCharset, [7, 0, 0, 0, 0, 0, 0, 0, 3], null, "CodeError"); |
|
1051 |
|
1052 run_next_test(); |
|
1053 }); |
|
1054 |
|
1055 //// WellKnownCharset.encode //// |
|
1056 |
|
1057 add_test(function test_WellKnownCharset_encode() { |
|
1058 // Test for Any-charset |
|
1059 wsp_encode_test(WSP.WellKnownCharset, {charset: "*"}, [0x80]); |
|
1060 wsp_encode_test(WSP.WellKnownCharset, {charset: "us-ascii"}, [128 + 3]); |
|
1061 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-1"}, [128 + 4]); |
|
1062 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-2"}, [128 + 5]); |
|
1063 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-3"}, [128 + 6]); |
|
1064 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-4"}, [128 + 7]); |
|
1065 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-5"}, [128 + 8]); |
|
1066 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-6"}, [128 + 9]); |
|
1067 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-7"}, [128 + 10]); |
|
1068 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-8"}, [128 + 11]); |
|
1069 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-9"}, [128 + 12]); |
|
1070 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-10"}, [128 + 13]); |
|
1071 wsp_encode_test(WSP.WellKnownCharset, {charset: "shift_jis"}, [128 + 17]); |
|
1072 wsp_encode_test(WSP.WellKnownCharset, {charset: "euc-jp"}, [128 + 18]); |
|
1073 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-2022-kr"}, [128 + 37]); |
|
1074 wsp_encode_test(WSP.WellKnownCharset, {charset: "euc-kr"}, [128 + 38]); |
|
1075 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-2022-jp"}, [128 + 39]); |
|
1076 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-2022-jp-2"}, [128 + 40]); |
|
1077 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-6-e"}, [128 + 81]); |
|
1078 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-6-i"}, [128 + 82]); |
|
1079 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-8-e"}, [128 + 84]); |
|
1080 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-8859-8-i"}, [128 + 85]); |
|
1081 wsp_encode_test(WSP.WellKnownCharset, {charset: "UTF-8"}, [128 + 106]); |
|
1082 wsp_encode_test(WSP.WellKnownCharset, {charset: "iso-10646-ucs-2"}, [2, 0x3, 0xe8]); |
|
1083 wsp_encode_test(WSP.WellKnownCharset, {charset: "UTF-16"}, [2, 0x3, 0xf7]); |
|
1084 wsp_encode_test(WSP.WellKnownCharset, {charset: "gb2312"}, [2, 0x7, 0xe9]); |
|
1085 wsp_encode_test(WSP.WellKnownCharset, {charset: "big5"}, [2, 0x7, 0xea]); |
|
1086 wsp_encode_test(WSP.WellKnownCharset, {charset: "koi8-r"}, [2, 0x8, 0x24]); |
|
1087 wsp_encode_test(WSP.WellKnownCharset, {charset: "windows-1252"}, [2, 0x8, 0xcc]); |
|
1088 |
|
1089 run_next_test(); |
|
1090 }); |
|
1091 |
|
1092 // |
|
1093 // Test target: ContentTypeValue |
|
1094 // |
|
1095 |
|
1096 //// ContentTypeValue.decodeConstrainedMedia //// |
|
1097 |
|
1098 add_test(function test_ContentTypeValue_decodeConstrainedMedia() { |
|
1099 function func(data) { |
|
1100 return WSP.ContentTypeValue.decodeConstrainedMedia(data); |
|
1101 } |
|
1102 |
|
1103 // Test for string-typed return value from ConstrainedEncoding |
|
1104 wsp_decode_test_ex(func, [65, 0], {media: "a", params: null}); |
|
1105 // Test for number-typed return value from ConstrainedEncoding |
|
1106 for(let ix = 0; ix <WSP.WSP_WELL_KNOWN_CONTENT_TYPES.length ; ++ix){ |
|
1107 wsp_decode_test_ex(func, [WSP.WSP_WELL_KNOWN_CONTENT_TYPES[ix].number | 0x80], |
|
1108 {media: WSP.WSP_WELL_KNOWN_CONTENT_TYPES[ix].value, params: null}); |
|
1109 } |
|
1110 // Test for NotWellKnownEncodingError |
|
1111 wsp_decode_test_ex(func, [0x59 | 0x80], null, "NotWellKnownEncodingError"); |
|
1112 |
|
1113 run_next_test(); |
|
1114 }); |
|
1115 |
|
1116 //// ContentTypeValue.decodeMedia //// |
|
1117 |
|
1118 add_test(function test_ContentTypeValue_decodeMedia() { |
|
1119 function func(data) { |
|
1120 return WSP.ContentTypeValue.decodeMedia(data); |
|
1121 } |
|
1122 |
|
1123 // Test for NullTerminatedTexts |
|
1124 wsp_decode_test_ex(func, [65, 0], "a"); |
|
1125 // Test for IntegerValue |
|
1126 wsp_decode_test_ex(func, [0x3E | 0x80], "application/vnd.wap.mms-message"); |
|
1127 wsp_decode_test_ex(func, [0x59 | 0x80], null, "NotWellKnownEncodingError"); |
|
1128 |
|
1129 run_next_test(); |
|
1130 }); |
|
1131 |
|
1132 //// ContentTypeValue.decodeMediaType //// |
|
1133 |
|
1134 add_test(function test_ContentTypeValue_decodeMediaType() { |
|
1135 wsp_decode_test_ex(function(data) { |
|
1136 return WSP.ContentTypeValue.decodeMediaType(data, 1); |
|
1137 }, [0x3E | 0x80], |
|
1138 {media: "application/vnd.wap.mms-message", params: null} |
|
1139 ); |
|
1140 wsp_decode_test_ex(function(data) { |
|
1141 return WSP.ContentTypeValue.decodeMediaType(data, 14); |
|
1142 }, [0x3E | 0x80, 1, 0x0A, 60, 115, 109, 105, 108, 62, 0, 65, 0, 66, 0], |
|
1143 {media: "application/vnd.wap.mms-message", params: {start: "<smil>", a: "B"}} |
|
1144 ); |
|
1145 |
|
1146 run_next_test(); |
|
1147 }); |
|
1148 |
|
1149 //// ContentTypeValue.decodeContentGeneralForm //// |
|
1150 |
|
1151 add_test(function test_ContentTypeValue_decodeContentGeneralForm() { |
|
1152 wsp_decode_test_ex(function(data) { |
|
1153 return WSP.ContentTypeValue.decodeContentGeneralForm(data); |
|
1154 }, [14, 0x3E | 0x80, 1, 0x0A, 60, 115, 109, 105, 108, 62, 0, 65, 0, 66, 0], |
|
1155 {media: "application/vnd.wap.mms-message", params: {start: "<smil>", a: "B"}} |
|
1156 ); |
|
1157 |
|
1158 run_next_test(); |
|
1159 }); |
|
1160 |
|
1161 //// ContentTypeValue.decode //// |
|
1162 |
|
1163 add_test(function test_ContentTypeValue_decode() { |
|
1164 wsp_decode_test(WSP.ContentTypeValue, |
|
1165 [14, 0x3E | 0x80, 1, 0x0A, 60, 115, 109, 105, 108, 62, 0, 65, 0, 66, 0], |
|
1166 {media: "application/vnd.wap.mms-message", params: {start: "<smil>", a: "B"}} |
|
1167 ); |
|
1168 |
|
1169 wsp_decode_test(WSP.ContentTypeValue, [0x33 | 0x80], |
|
1170 {media: "application/vnd.wap.multipart.related", params: null} |
|
1171 ); |
|
1172 |
|
1173 run_next_test(); |
|
1174 }); |
|
1175 |
|
1176 //// ContentTypeValue.encodeConstrainedMedia //// |
|
1177 |
|
1178 add_test(function test_ContentTypeValue_encodeConstrainedMedia() { |
|
1179 function func(data, input) { |
|
1180 WSP.ContentTypeValue.encodeConstrainedMedia(data, input); |
|
1181 return data.array; |
|
1182 } |
|
1183 |
|
1184 // Test media type with additional parameters. |
|
1185 wsp_encode_test_ex(func, {media: "a", params: [{a: "b"}]}, null, "CodeError"); |
|
1186 wsp_encode_test_ex(func, {media: "no/such.type"}, |
|
1187 [110, 111, 47, 115, 117, 99, 104, 46, 116, 121, 112, 101, 0]); |
|
1188 for(let ix = 0; ix <WSP.WSP_WELL_KNOWN_CONTENT_TYPES.length ; ++ix){ |
|
1189 wsp_encode_test_ex(func, {media: WSP.WSP_WELL_KNOWN_CONTENT_TYPES[ix].value}, |
|
1190 [WSP.WSP_WELL_KNOWN_CONTENT_TYPES[ix].number | 0x80]); |
|
1191 } |
|
1192 wsp_encode_test_ex(func, {media: "TexT/X-hdml"}, [0x04 | 0x80]); |
|
1193 wsp_encode_test_ex(func, {media: "appLication/*"}, [0x10 | 0x80]); |
|
1194 |
|
1195 run_next_test(); |
|
1196 }); |
|
1197 |
|
1198 //// ContentTypeValue.encodeMediaType //// |
|
1199 |
|
1200 add_test(function test_ContentTypeValue_encodeMediaType() { |
|
1201 function func(data, input) { |
|
1202 WSP.ContentTypeValue.encodeMediaType(data, input); |
|
1203 return data.array; |
|
1204 } |
|
1205 |
|
1206 wsp_encode_test_ex(func, {media: "no/such.type"}, |
|
1207 [110, 111, 47, 115, 117, 99, 104, 46, 116, 121, 112, 101, 0]); |
|
1208 wsp_encode_test_ex(func, {media: "application/vnd.wap.multipart.related"}, |
|
1209 [0x33 | 0x80]); |
|
1210 wsp_encode_test_ex(func, {media: "a", params: {b: "c", q: 0}}, |
|
1211 [97, 0, 98, 0, 99, 0, 128, 1]); |
|
1212 |
|
1213 run_next_test(); |
|
1214 }); |
|
1215 |
|
1216 //// ContentTypeValue.encodeContentGeneralForm //// |
|
1217 |
|
1218 add_test(function test_ContentTypeValue_encodeContentGeneralForm() { |
|
1219 function func(data, input) { |
|
1220 WSP.ContentTypeValue.encodeContentGeneralForm(data, input); |
|
1221 return data.array; |
|
1222 } |
|
1223 |
|
1224 wsp_encode_test_ex(func, {media: "a", params: {b: "c", q: 0}}, |
|
1225 [8, 97, 0, 98, 0, 99, 0, 128, 1]); |
|
1226 |
|
1227 run_next_test(); |
|
1228 }); |
|
1229 |
|
1230 //// ContentTypeValue.encode //// |
|
1231 |
|
1232 add_test(function test_ContentTypeValue_encode() { |
|
1233 wsp_encode_test(WSP.ContentTypeValue, {media: "no/such.type"}, |
|
1234 [110, 111, 47, 115, 117, 99, 104, 46, 116, 121, 112, 101, 0]); |
|
1235 wsp_encode_test(WSP.ContentTypeValue, |
|
1236 {media: "application/vnd.wap.multipart.related"}, |
|
1237 [0x33 | 0x80]); |
|
1238 wsp_encode_test(WSP.ContentTypeValue, {media: "a", params: {b: "c", q: 0}}, |
|
1239 [8, 97, 0, 98, 0, 99, 0, 128, 1]); |
|
1240 |
|
1241 run_next_test(); |
|
1242 }); |
|
1243 |
|
1244 // |
|
1245 // Test target: ApplicationIdValue |
|
1246 // |
|
1247 |
|
1248 //// ApplicationIdValue.decode //// |
|
1249 |
|
1250 add_test(function test_ApplicationIdValue_decode() { |
|
1251 wsp_decode_test(WSP.ApplicationIdValue, [0], ""); |
|
1252 wsp_decode_test(WSP.ApplicationIdValue, [65, 0], "A"); |
|
1253 wsp_decode_test(WSP.ApplicationIdValue, [97, 0], "a"); |
|
1254 let (entry = WSP.OMNA_PUSH_APPLICATION_IDS["x-wap-application:mms.ua"]) { |
|
1255 wsp_decode_test(WSP.ApplicationIdValue, [entry.number | 0x80], entry.urn); |
|
1256 wsp_decode_test(WSP.ApplicationIdValue, [1, entry.number], entry.urn); |
|
1257 } |
|
1258 wsp_decode_test(WSP.ApplicationIdValue, [0xFF], null, "NotWellKnownEncodingError"); |
|
1259 |
|
1260 run_next_test(); |
|
1261 }); |
|
1262 |
|
1263 // |
|
1264 // Test target: PduHelper |
|
1265 // |
|
1266 |
|
1267 //// PduHelper.parseHeaders //// |
|
1268 |
|
1269 add_test(function test_PduHelper_parseHeaders() { |
|
1270 wsp_decode_test_ex(function(data) { |
|
1271 return WSP.PduHelper.parseHeaders(data, data.array.length); |
|
1272 }, [0x80 | 0x05, 2, 0x23, 0x28, 0x80 | 0x2F, 0x80 | 0x04], |
|
1273 {"age": 9000, "x-wap-application-id": "x-wap-application:mms.ua"} |
|
1274 ); |
|
1275 |
|
1276 run_next_test(); |
|
1277 }); |
|
1278 |
|
1279 //// PduHelper.decodeStringContent //// |
|
1280 |
|
1281 add_test(function StringContent_decode() { |
|
1282 //Test for utf-8 |
|
1283 let (entry = WSP.WSP_WELL_KNOWN_CHARSETS["utf-8"]) { |
|
1284 // "Mozilla" in full width. |
|
1285 let str = "\uff2d\uff4f\uff5a\uff49\uff4c\uff4c\uff41"; |
|
1286 |
|
1287 let conv = Cc["@mozilla.org/intl/scriptableunicodeconverter"] |
|
1288 .createInstance(Ci.nsIScriptableUnicodeConverter); |
|
1289 conv.charset = entry.converter; |
|
1290 |
|
1291 let raw = conv.convertToByteArray(str); |
|
1292 let data = {array: raw, offset: 0}; |
|
1293 let octetArray = WSP.Octet.decodeMultiple(data, data.array.length); |
|
1294 wsp_decode_test_ex(function(data) { |
|
1295 return WSP.PduHelper.decodeStringContent(data.array, "utf-8"); |
|
1296 }, octetArray, str); |
|
1297 } |
|
1298 |
|
1299 let (entry = WSP.WSP_WELL_KNOWN_CHARSETS["utf-16"]) { |
|
1300 // "Mozilla" in full width. |
|
1301 let str = "\u004d\u006F\u007A\u0069\u006C\u006C\u0061"; |
|
1302 |
|
1303 let conv = Cc["@mozilla.org/intl/scriptableunicodeconverter"] |
|
1304 .createInstance(Ci.nsIScriptableUnicodeConverter); |
|
1305 conv.charset = entry.converter; |
|
1306 |
|
1307 let raw = conv.convertToByteArray(str); |
|
1308 let data = {array: raw, offset: 0}; |
|
1309 let octetArray = WSP.Octet.decodeMultiple(data, data.array.length); |
|
1310 wsp_decode_test_ex(function(data) { |
|
1311 return WSP.PduHelper.decodeStringContent(data.array, "utf-16"); |
|
1312 }, raw, str); |
|
1313 } |
|
1314 |
|
1315 run_next_test(); |
|
1316 }); |
|
1317 |
|
1318 //// PduHelper.composeMultiPart //// |
|
1319 |
|
1320 add_test(function test_PduHelper_composeMultiPart() { |
|
1321 let multiStream = Components.classes["@mozilla.org/io/multiplex-input-stream;1"] |
|
1322 .createInstance(Ci.nsIMultiplexInputStream); |
|
1323 let uint8Array = new Uint8Array(5); |
|
1324 uint8Array[0] = 0x00; |
|
1325 uint8Array[1] = 0x01; |
|
1326 uint8Array[2] = 0x02; |
|
1327 uint8Array[3] = 0x03; |
|
1328 uint8Array[4] = 0x04; |
|
1329 |
|
1330 let parts = [ |
|
1331 { |
|
1332 content: "content", |
|
1333 headers: { |
|
1334 "content-type": { |
|
1335 media: "text/plain", |
|
1336 params: {} |
|
1337 } |
|
1338 } |
|
1339 }, |
|
1340 { |
|
1341 content: uint8Array, |
|
1342 headers: { |
|
1343 "content-type": { |
|
1344 media: "text/plain", |
|
1345 params: {} |
|
1346 } |
|
1347 } |
|
1348 } |
|
1349 ]; |
|
1350 |
|
1351 let beforeCompose = JSON.stringify(parts); |
|
1352 WSP.PduHelper.composeMultiPart(multiStream, parts); |
|
1353 let afterCompose = JSON.stringify(parts); |
|
1354 |
|
1355 do_check_eq(beforeCompose, afterCompose); |
|
1356 run_next_test(); |
|
1357 }); |