1# -*- tcl -*-
2# asn.test:  tests for the asn BER encoding/decoding module.
3#
4# Copyright (c) 2004 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
5# Copyright (c) 2004-2007 by Michael Schlenker <mic42@users.sourceforge.net>
6# All rights reserved.
7#
8# RCS: @(#) $Id: asn.test,v 1.19 2011/01/05 22:33:33 mic42 Exp $
9
10# -------------------------------------------------------------------------
11
12
13source [file join \
14	[file dirname [file dirname [file join [pwd] [info script]]]] \
15	devtools testutilities.tcl]
16
17
18testsNeedTcl     8.4
19testsNeedTcltest 1.0
20
21testing {
22    useLocal asn.tcl asn
23}
24
25# Converts binary encoded structure into hexadecimal dump
26# which is more readable in test results
27# Allows cut'n'paste both encoded and parsed OID from dumpasn1.cfg
28#
29proc bytes2hex {string} {
30	foreach b [split $string ""] {
31		lappend l [format %02X [scan $b %c]]
32	}
33	return [join $l " "]
34}
35
36# -------------------------------------------------------------------------
37
38test asn-1.0 {integer} {
39    catch {asn::asnInteger} result
40    set result
41} [tcltest::wrongNumArgs {asn::asnInteger} {number} 0]
42
43test asn-1.1 {integer} {
44    catch {asn::asnInteger a b} result
45    set result
46} [tcltest::tooManyArgs {asn::asnInteger} {number}]
47
48test asn-1.2 {integer} {
49    catch {asn::asnInteger a} result
50    set result
51} {expected integer but got "a"}
52
53
54test asn-3.0 {enum} {
55    catch {asn::asnEnumeration} result
56    set result
57} [tcltest::wrongNumArgs {asn::asnEnumeration} {number} 0]
58
59test asn-3.1 {enum} {
60    catch {asn::asnEnumeration a b} result
61    set result
62} [tcltest::tooManyArgs {asn::asnEnumeration} {number}]
63
64test asn-3.2 {enum} {
65    catch {asn::asnEnumeration a} result
66    set result
67} {expected integer but got "a"}
68
69
70
71
72foreach {n i len hex} {
73     0  0        01 00
74     1 -1        01 FF
75     2  1        01 01
76     3  127      01 7F
77     4  128      02 0080
78     5  129      02 0081
79     6  256      02 0100
80     7 -127      01 81
81     8 -128      01 80
82     9 -129      02 FF7F
83    10  32766    02 7FFE
84    11  32767    02 7FFF
85    12  32768    03 008000
86    13  32769    03 008001
87    14 -32767    02 8001
88    15 -32768    02 8000
89    16 -32769    03 FF7FFF
90    17  65536    03 010000
91    18  8388607  03 7FFFFF
92    19  8388608  04 00800000
93    20  8388609  04 00800001
94    21  16777216 04 01000000
95    22 -8388607  03 800001
96    23 -8388608  03 800000
97    24 -8388609  04 FF7FFFFF
98    25 -65536    03 FF0000
99    26 -2147483648              04 80000000
100    27 2147483647               04 7FFFFFFF
101    28 -549755813888            05 8000000000
102    29 549755813887             05 7FFFFFFFFF
103    30 -140737488355328         06 800000000000
104    31 140737488355327          06 7FFFFFFFFFFF
105    32 -36028797018963968       07 80000000000000
106    33 36028797018963967        07 7FFFFFFFFFFFFF
107    34 36028797018963968        08 0080000000000000
108    35 -9223372036854775808     08 8000000000000000
109    36 9223372036854775807      08 7FFFFFFFFFFFFFFF
110} {
111    test asn-2.$n {integer} {
112    binary scan [asn::asnInteger $i] H* result
113    list $i [string toupper $result]
114    } [list $i 02$len$hex] ; # {}
115
116    test asn-4.$n {enum} {
117    binary scan [asn::asnEnumeration $i] H* result
118    list $i [string toupper $result]
119    } [list $i 0A$len$hex] ; # {}
120}
121
122test asn-5.0 {boolean} {
123    catch {asn::asnBoolean} result
124    set result
125} [tcltest::wrongNumArgs {asn::asnBoolean} {bool} 0]
126
127test asn-5.1 {boolean} {
128    catch {asn::asnBoolean a b} result
129    set result
130} [tcltest::tooManyArgs {asn::asnBoolean} {bool}]
131
132test asn-5.2 {boolean} {
133    catch {asn::asnBoolean a} result
134    set result
135} {expected boolean value but got "a"}
136
137test asn-5.3 {boolean - true} {
138    binary scan [asn::asnBoolean 1] H* result
139    string toupper $result
140} {0101FF}
141
142test asn-5.4 {boolean - false} {
143    binary scan [asn::asnBoolean 0] H* result
144    string toupper $result
145} {010100}
146
147test asn-6.0 {parse boolean} {
148    catch {asn::asnGetBoolean} result
149    set result
150} [tcltest::wrongNumArgs {asn::asnGetBoolean} {data_var bool_var} 0]
151
152test asn-6.1 {parse boolean} {
153    catch {asn::asnGetBoolean a} result
154    set result
155} [tcltest::wrongNumArgs {asn::asnGetBoolean} {data_var bool_var} 1]
156
157test asn-6.2 {parse boolean} {
158    catch {asn::asnGetBoolean a b c} result
159    set result
160} [tcltest::tooManyArgs {asn::asnGetBoolean} {data_var bool_var}]
161
162test asn-6.3 {parse boolean} {
163    catch {asn::asnGetBoolean a b} result
164    set result
165} {can't read "data": no such variable}
166
167test asn-6.4 {parse boolean - wrong tag} {
168    set a \x02\x01\x00
169    catch {asn::asnGetBoolean a b} result
170    set result
171} {Expected Boolean (0x01), but got 02}
172
173test asn-6.5 {parse boolean - wrong length} {
174    set a \x01\x02\x00
175    catch {asn::asnGetBoolean a b} result
176    list $result $b
177} [list "" 0]
178
179test asn-6.6 {parse boolean - true} {
180    set a \x01\x01\xFF
181    asn::asnGetBoolean a b
182    set b
183} 1
184
185test asn-6.7 {parse boolean - true} {
186    set a \x01\x01\x01
187    asn::asnGetBoolean a b
188    set b
189} 1
190
191test asn-6.8 {parse boolean - false} {
192    set a \x01\x01\x00
193    asn::asnGetBoolean a b
194    set b
195} 0
196
197test asn-7.0 {null} {
198    catch {asn::asnNull foo} result
199    set result
200} [tcltest::tooManyArgs {asn::asnNull} {}]
201
202test asn-7.1 {null} {
203    binary scan [asn::asnNull] H* result
204    set result
205} {0500}
206
207test asn-8.0 {parse null} {
208    catch {asn::asnGetNull} result
209    set result
210} [tcltest::wrongNumArgs asn::asnGetNull {data_var} 0]
211
212test asn-8.1 {parse null} {
213    catch {asn::asnGetNull foo bar} result
214    set result
215} [tcltest::tooManyArgs {asn::asnGetNull} {data_var}]
216
217test asn-8.2 {parse null} {
218    set wrongtag \x01\x01
219    catch {asn::asnGetNull wrongtag} result
220    set result
221} {Expected NULL (0x05), but got 01}
222
223test asn-8.3 {parse null} {
224    set wronglength \x05\x01
225    catch {asn::asnGetNull wronglength} result
226    set result
227} {}
228
229test asn-8.4 {parse null} {
230    set null \x05\x00
231    asn::asnGetNull null
232} {}
233
234package require math::bignum
235foreach {n i len hex} {
236     0  0        01 00
237     1 -1        01 FF
238     2  1        01 01
239     3  127      01 7F
240     4  128      02 0080
241     5  129      02 0081
242     6  256      02 0100
243     7 -127      01 81
244     8 -128      01 80
245     9 -129      02 FF7F
246    10  32766    02 7FFE
247    11  32767    02 7FFF
248    12  32768    03 008000
249    13  32769    03 008001
250    14 -32767    02 8001
251    15 -32768    02 8000
252    16 -32769    03 FF7FFF
253    17  65536    03 010000
254    18  8388607  03 7FFFFF
255    19  8388608  04 00800000
256    20  8388609  04 00800001
257    21  16777216 04 01000000
258    22 -8388607  03 800001
259    23 -8388608  03 800000
260    24 -8388609  04 FF7FFFFF
261    25 -65536    03 FF0000
262} {
263    test asn-9.$n {big integer} {
264    binary scan [asn::asnBigInteger [math::bignum::fromstr $i]] H* result
265    list $i [string toupper $result]
266    } [list $i 02$len$hex] ; # {}
267
268}
269
270foreach {n len hex} {
271    0   0                   00
272    1   1                   01
273    2   127                 7F
274    3   128                 8180
275    4   129                 8181
276    5   255                 81FF
277    6   256                 820100
278    7   32767               827FFF
279    8   32768               828000
280    9   32769               828001
281    10  65535               82FFFF
282    11  65536               83010000
283    12  8388607             837FFFFF
284    13  8388608             83800000
285    14  8388609             83800001
286    15  16777215            83FFFFFF
287    16  16777216            8401000000
288    17  4294967295          84FFFFFFFF
289    18  4294967296          850100000000
290    19  1099511627775       85FFFFFFFFFF
291    20  1099511627776       86010000000000
292    21  281474976710655     86FFFFFFFFFFFF
293    22  281474976710656     8701000000000000
294    23  72057594037927935   87FFFFFFFFFFFFFF
295    24  72057594037927936   880100000000000000
296    25  9223372036854775807 887FFFFFFFFFFFFFFF
297    } {
298    test asn-10.$n {asnLength encoding} {
299    binary scan [asn::asnLength $len] H* result
300    string toupper $result
301    } $hex
302}
303
304foreach {n len hex} {
305    0   0                   00
306    1   1                   01
307    2   127                 7F
308    3   128                 8180
309    4   129                 8181
310    5   255                 81FF
311    6   256                 820100
312    7   32767               827FFF
313    8   32768               828000
314    9   32769               828001
315    10  65535               82FFFF
316    11  65536               83010000
317    12  8388607             837FFFFF
318    13  8388608             83800000
319    14  8388609             83800001
320    15  16777215            83FFFFFF
321    16  16777216            8401000000
322    17  4294967295          84FFFFFFFF
323    18  4294967296          850100000000
324    19  1099511627775       85FFFFFFFFFF
325    20  1099511627776       86010000000000
326    21  281474976710655     86FFFFFFFFFFFF
327    22  281474976710656     8701000000000000
328    23  72057594037927935   87FFFFFFFFFFFFFF
329    24  72057594037927936   880100000000000000
330    25  9223372036854775807 887FFFFFFFFFFFFFFF
331    } {
332    test asn-11.$n {asnGetLength decoding} {
333    set data [binary format H* $hex ]
334    asn::asnGetLength data length
335    set length
336    } $len
337}
338
339foreach {n i len hex} {
340     0  0               01 00
341     1 -1               01 FF
342     2  1               01 01
343     3  127             01 7F
344     4  128             02 0080
345     5  129             02 0081
346     6  256             02 0100
347     7 -127             01 81
348     8 -128             01 80
349     9 -129             02 FF7F
350    10  32766           02 7FFE
351    11  32767           02 7FFF
352    12  32768           03 008000
353    13  32769           03 008001
354    14 -32767           02 8001
355    15 -32768           02 8000
356    16 -32769           03 FF7FFF
357    17  65536           03 010000
358    18  8388607         03 7FFFFF
359    19  8388608         04 00800000
360    20  8388609         04 00800001
361    21  16777216        04 01000000
362    22 -8388607         03 800001
363    23 -8388608         03 800000
364    24 -8388609         04 FF7FFFFF
365    25 -65536           03 FF0000
366    26 -2147483648      04 80000000
367    27 2147483647       04 7FFFFFFF
368    28 -549755813888    05 8000000000
369    29 549755813887     05 7FFFFFFFFF
370    30 -140737488355328 06 800000000000
371    31 140737488355327  06 7FFFFFFFFFFF
372    32 -36028797018963968 07 80000000000000
373    33 36028797018963967 07  7FFFFFFFFFFFFF
374    34 36028797018963968 08  0080000000000000
375    35 -9223372036854775808 08 8000000000000000
376    36 9223372036854775807 08 7FFFFFFFFFFFFFFF
377    37 65537            03 010001
378    38 -8323071         03 810001
379} {
380    test asn-12.$n {getInteger} {
381    set data [binary format H2H2H* 02 $len $hex]
382    asn::asnGetInteger data int
383    set int
384    }  $i  ; # {}
385}
386
387foreach {n i len hex} {
388     0  0               01 00
389     1 -1               01 FF
390     2  1               01 01
391     3  127             01 7F
392     4  128             02 0080
393     5  129             02 0081
394     6  256             02 0100
395     7 -127             01 81
396     8 -128             01 80
397     9 -129             02 FF7F
398    10  32766           02 7FFE
399    11  32767           02 7FFF
400    12  32768           03 008000
401    13  32769           03 008001
402    14 -32767           02 8001
403    15 -32768           02 8000
404    16 -32769           03 FF7FFF
405    17  65536           03 010000
406    18  8388607         03 7FFFFF
407    19  8388608         04 00800000
408    20  8388609         04 00800001
409    21  16777216        04 01000000
410    22 -8388607         03 800001
411    23 -8388608         03 800000
412    24 -8388609         04 FF7FFFFF
413    25 -65536           03 FF0000
414    26 -2147483648      04 80000000
415    27 2147483647       04 7FFFFFFF
416    28 -549755813888    05 8000000000
417    29 549755813887     05 7FFFFFFFFF
418    30 -140737488355328 06 800000000000
419    31 140737488355327  06 7FFFFFFFFFFF
420    32 -36028797018963968 07 80000000000000
421    33 36028797018963967 07  7FFFFFFFFFFFFF
422    34 36028797018963968 08  0080000000000000
423    35 -9223372036854775808 08 8000000000000000
424    36 9223372036854775807 08 7FFFFFFFFFFFFFFF
425    37 65537            03 010001
426    38 -8323071         03 810001
427} {
428    test asn-12.[expr {$n+39}] {getBigInteger} {
429    set data [binary format H2H2H* 02 $len $hex]
430    asn::asnGetBigInteger data int
431    math::bignum::tostr $int
432    }  $i  ; # {}
433}
434
435test asn-13.0 {peekByte} {
436    set data \x0d\x0a
437    asn::asnPeekByte data byte
438    list $byte [string length $data]
439} {13 2}
440
441test asn-14.0 {getByte} {
442    set data \x0d\x0a
443    asn::asnGetByte data byte
444    list $byte [string length $data]
445} {13 1}
446
447test asn-15.0 {getBytes} {
448    set data \x0d\x0d\x0d\x0d\x0a
449    asn::asnGetBytes data 4 bytes
450    list [string length $data] [string length $bytes]
451} [list 1 4]
452
453test asn-15.1 {getBytes} {
454    set data \x0d\x0d\x0d\x0d\x0a
455    asn::asnGetBytes data 4 bytes
456    set expectedbytes \x0d\x0d\x0d\x0d
457    set expecteddata \x0a
458    list [expr {$data == $expecteddata}] [expr {$bytes == $expectedbytes}]
459} [list 1 1]
460
461# 16 ----------- string encoder/decoder invalid arguments
462array set stringtag {
463    NumericString   0x12
464    PrintableString 0x13
465    IA5String       0x16
466    BMPString       0x1e
467    UTF8String      0x0c
468}
469set i 0
470foreach strtype {NumericString PrintableString IA5String BMPString UTF8String} {
471	incr i
472	test asn-16.$i $strtype {
473	    catch {asn::asn$strtype} result
474	    set result
475	} [tcltest::wrongNumArgs asn::asn$strtype string 0]
476	incr i
477	test asn-16.$i $strtype {
478		catch "asn::asn$strtype a b" result
479		set result
480	} [tcltest::tooManyArgs "asn::asn$strtype" string]
481	incr i
482	test asn-16.$i get$strtype {
483		catch "asn::asnGet$strtype foo" result
484		set result
485	} [tcltest::wrongNumArgs "asn::asnGet$strtype" "data_var print_var" 0]
486	incr i
487	test asn-16.$i get$strtype {
488		catch "asn::asnGet$strtype foo bar baz" result
489		set result
490	} [tcltest::tooManyArgs "asn::asnGet$strtype" "data_var print_var"]
491	incr i
492	test asn-16.$i "get$strtype parse sequence" {
493		set data "\x30\x03abc"
494		catch "asn::asnGet$strtype data print" result
495		set result
496	} "Expected [regsub String $strtype " String"] ($stringtag($strtype)), but got 30"
497}
498incr i
499# 17 ------------------- invalid string values
500
501
502test asn-17.1 {numeric string with non-numbers} {
503	catch {asn::asnNumericString this-is-not-a-number} result
504	set result
505} "Illegal character in Numeric String."
506
507test asn-17.2 {numeric string with hexadecimals} {
508	catch {asn::asnNumericString 09AB} result
509	set result
510} "Illegal character in Numeric String."
511
512test asn-17.3 {numeric string with spaces - spaces are legal} {
513	catch {asn::asnNumericString " 093"}
514} 0 ;# TCL_OK
515
516test asn-17.4 {numeric string with minus sign} {
517	catch {asn::asnNumericString "-15"}  result
518	set result
519} "Illegal character in Numeric String."
520
521test asn-17.5 {numeric string with tab (illegal)} {
522	catch {asn::asnNumericString "\t093"} result
523	set result
524} "Illegal character in Numeric String."
525
526#According to ITU-T X.680 37.4
527set printablechars {[-A-Za-z0-9 '()+,./=?:]}
528
529set i 6
530
531for {set j 1} {$j<128} {incr j;incr i} {
532    set data [format %c $j]
533    if {[regexp "^$printablechars+\$" $data]} {
534	test asn-17.$i "printable string valid char [format %02x $j]" {
535	    catch {asn::asnPrintableString $data}
536	} 0 ; # {}
537    } else {
538	test asn-17.$i "printable string invalid char [format %02x $j]" {
539	    catch {asn::asnPrintableString $data} result
540	    set result
541	} "Illegal character in PrintableString." ; # {}
542    }
543}
544
545test asn-17.134 {IA5String with Latin-1 char} {
546	catch {asn::asnIA5String "\xD0"} result
547	set result
548} "Illegal character in IA5String"
549
550test asn-17.135 {IA5String with Cyrillic chars} {
551	catch {asn::asnIA5String "\u0420\u0443\u0441\u0441\u043a\u0438\u0439"} result
552	set result
553} "Illegal character in IA5String"
554
555# 18 - correct encoding of string values
556
557test asn-18.1 {encode numeric string} {
558	catch {asn::asnNumericString 123} result
559	set result
560} "\x12\003123"
561
562test asn-18.2 {encode numeric strin with space} {
563	catch {asn::asnNumericString "1 2 3"} result
564	set result
565} "\x12\0051 2 3"
566
567test asn-18.3 {encode printable string} {
568	catch {asn::asnPrintableString "printable string"} result
569	set result
570} "\x13\x10printable string"
571
572test asn-18.4 {encode IA5 string} {
573	catch {asn::asnIA5String "vitus@45.free.net"} result
574	set result
575} "\x16\x11vitus@45.free.net"
576
577test asn-18.5 {encode bmp string US-ASCII} {
578	catch {asn::asnBMPString "US-ASCII"} result
579	set result
580} "\x1e\x10\0U\0S\0-\0A\0S\0C\0I\0I"
581
582test asn-18.6 {encode UTF8 string US-ASCII} {
583	catch {asn::asnUTF8String "US-ASCII"} result
584	set result
585} "\x0c\x08US-ASCII"
586
587test asn-18.7 {encode bmp string latin-1} {
588	catch {asn::asnBMPString "gar\xC7on"} result
589	set result
590} "\x1e\x0c\0g\0a\0r\0\xc7\0o\0n"
591
592test asn-18.8 {encode utf-8 string latin-1} {
593	catch {asn::asnUTF8String "gar\xC7on"} result
594	set result
595} "\x0c\x07gar\xc3\x87on"
596
597test asn-18.9 {encode bmp string cyrillic} {
598	catch {asn::asnBMPString "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"} result
599	set result
600} "\x1e\x0e\x04\x40\x04\x43\x04\x41\x04\x41\x04\x3a\x04\x38\x04\x39"
601
602test asn-18.10 {encode UTF8 string cyrillic} {
603	catch {asn::asnUTF8String "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"} result
604	set result
605} "\x0c\x0e\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9"
606
607test asn-18.11 {decode numeric string} {
608	set data "\x12\003123"
609	asn::asnGetNumericString data print
610	set print
611} 123
612
613test asn-18.12 {decode printable string} {
614	set data "\x13\x10printable string"
615	asn::asnGetPrintableString data print
616	set print
617} "printable string"
618
619test asn-18.13 {decode IA5 string} {
620	set data "\x16\x11vitus@45.free.net"
621	asn::asnGetIA5String data print
622	set print
623} "vitus@45.free.net"
624
625test asn-18.14 {decode BMP string US-ASCII} {
626	set data "\x1e\x10\0U\0S\0-\0A\0S\0C\0I\0I"
627	asn::asnGetBMPString data print
628	set print
629} "US-ASCII"
630
631test asn-18.15 {decode UTF8 string US-ASCII} {
632	set data "\x0c\x08US-ASCII"
633	asn::asnGetUTF8String data print
634	set print
635} "US-ASCII"
636
637test asn-18.16 {decode BMP string latin-1} {
638	set data "\x1e\x0c\0g\0a\0r\0\xc7\0o\0n"
639	asn::asnGetBMPString data print
640	set print
641} "gar\xC7on"
642
643test asn-18.17 {decode UTF8 string latin-1} {
644	set data "\x0c\x07gar\xc3\x87on"
645	asn::asnGetUTF8String data print
646	set print
647} "gar\xC7on"
648
649test asn-18.18 {decode BMP string cyrillic} {
650	set data "\x1e\x0e\x04\x40\x04\x43\x04\x41\x04\x41\x04\x3a\x04\x38\x04\x39"
651	asn::asnGetBMPString data print
652	set print
653} "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
654
655test asn-18.19 {decode UTF8 string cyrillic} {
656	set data "\x0c\x0e\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9"
657	asn::asnGetUTF8String data print
658	set print
659} "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
660
661# 19 ------- multitype getString
662set i 0
663foreach {type encoded str} {
664
665NumericString "\x12\003123" 123
666PrintableString  "\x13\x10printable string" "printable string"
667IA5String "\x16\x11vitus@45.free.net" "vitus@45.free.net"
668BMPString "\x1e\x10\0U\0S\0-\0A\0S\0C\0I\0I" US-ASCII
669UTF8String "\x0c\x08US-ASCII" US-ASCII
670BMPString "\x1e\x0c\0g\0a\0r\0\xc7\0o\0n" "gar\xc7on"
671UTF8String "\x0c\x07gar\xc3\x87on" "gar\xc7on"
672BMPString "\x1e\x0e\x04\x40\x04\x43\x04\x41\x04\x41\x04\x3a\x04\x38\x04\x39"
673"\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
674UTF8String "\x0c\x0e\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9"
675"\u0440\u0443\u0441\u0441\u043a\u0438\u0439"} {
676incr i
677	test asn-19.$i "getString - decode $type" {
678		set data $encoded
679		asn::asnGetString data print
680		set print
681	} $str
682	incr i
683	test asn-19.$i "getString - decode $type and get its type" {
684		set data $encoded
685		asn::asnGetString data print gotType
686		list $data $print $gotType
687	} [list {} $str $type]
688}
689
690# 20 ----- multitype String encoding
691
692test asn-20.1 {Set default type to something wrong} {
693	catch {asn::defaultStringType foo} result
694	set result
695} "Invalid default string type. Should be one of BMP, UTF8"
696
697test asn-20.2 {Set default value to string type which cannot hold any char} {
698	catch {asn::defaultStringType IA5} result
699	set result
700} "Invalid default string type. Should be one of BMP, UTF8"
701
702
703test asn-20.3 {Set default type to UTF8} {
704	asn::defaultStringType UTF8
705} ""
706
707test asn-20.4 {Get default string type} {
708	asn::defaultStringType
709} UTF8
710
711test asn-20.5 {String - encode numeric value} {
712	asn::asnString 123
713} "\x12\003123"
714
715test asn-20.6 {String - encode printable value} {
716	asn::asnString "printable string"
717} "\x13\x10printable string"
718
719test asn-20.7 {String - encode ASCII value} {
720	asn::asnString vitus@45.free.net
721} "\x16\x11vitus@45.free.net"
722
723test asn-20.8 {String - encode Latin-1 value} {
724	asn::asnString "gar\xc7on"
725} "\x0c\x07gar\xc3\x87on"
726
727test asn-20.9 {String - encode Cyrillic value} {
728	asn::asnString "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
729} "\x0c\x0e\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9"
730
731test asn-20.10 {Set default string type to BMP} {
732	asn::defaultStringType BMP
733	asn::defaultStringType
734} BMP
735
736test asn-20.11 {String - encode numeric value} {
737	asn::asnString 123
738} "\x12\003123"
739
740test asn-20.12 {String - encode printable value} {
741	asn::asnString "printable string"
742} "\x13\x10printable string"
743
744test asn-20.13 {String - encode ASCII value} {
745	asn::asnString vitus@45.free.net
746} "\x16\x11vitus@45.free.net"
747
748
749test asn-20.14 {String - encode Latin-1 value} {
750	asn::asnString "gar\xc7on"
751} "\x1e\x0c\0g\0a\0r\0\xc7\0o\0n"
752
753test asn-20.15 {String - encode Cyrillic value} {
754	asn::asnString "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
755} "\x1e\x0e\x04\x40\x04\x43\x04\x41\x04\x41\x04\x3a\x04\x38\x04\x39"
756
757# 21 --------- Object identifier
758#
759
760test asn-21.1 {ObjectIdentifier start with 0} {
761	bytes2hex [asn::asnObjectIdentifier {0 2 262 1 10}]
762} "06 05 02 82 06 01 0A"
763
764
765test asn-21.2 {ObjectIdentifier start with 1} {
766	bytes2hex [asn::asnObjectIdentifier {1 2 840 10045 2 1}]
767} "06 07 2A 86 48 CE 3D 02 01"
768
769test asn-21.3 {ObjectIdentifer field > 65536} {
770	bytes2hex [asn::asnObjectIdentifier {1 2 840 113533 7 66 3}]
771} "06 09 2A 86 48 86 F6 7D 07 42 03"
772
773test asn-21.4 {ObjectIdentifer 2.23.42.9.37} {
774	bytes2hex [asn::asnObjectIdentifier {2 23 42 9 37}]
775} "06 04 67 2A 09 25"
776
777test asn-21.5 {GetObjectIdentifier 0.2.262.1.10} {
778	set data "\x06\x05\x02\x82\x06\x01\x0A"
779	asn::asnGetObjectIdentifier data print
780	set print
781} {0 2 262 1 10}
782
783test asn-21.6 {GetObjectIdentifier 1 2 840 10045 2 1} {
784	set data "\x06\x07\x2A\x86\x48\xCE\x3D\x02\x01"
785	asn::asnGetObjectIdentifier data print
786	set print
787} {1 2 840 10045 2 1}
788
789test asn-21.7 {GetObjectIdentifier 1 2 840 113533 7 66 3} {
790	set data "\x06\x09\x2A\x86\x48\x86\xF6\x7D\x07\x42\x03"
791	asn::asnGetObjectIdentifier data print
792	set print
793} {1 2 840 113533 7 66 3}
794
795# 22 --- Octet String
796
797# smoke tests to check that we can at least call the commands
798test asn-23.0 {asnContext smoke test} {
799	set data "\x00"
800	bytes2hex [asn::asnContext 1 $data]
801} {81 01 00}
802
803test asn-24.0 {asnSequence smoke test} {
804	set data [asn::asnNull]
805	bytes2hex [asn::asnSequence 1 $data]
806} {30 03 31 05 00}
807
808test asn-25.0 {asnSet smoke test} {
809	set data [asn::asnNull]
810	bytes2hex [asn::asnSet 1 $data]
811} {31 03 31 05 00}
812
813test asn-26.0 {asnApplicationConstr smoke test} {
814	set data [asn::asnNull]
815	bytes2hex [asn::asnApplicationConstr 1 $data]
816} {61 02 05 00}
817
818test asn-27.0 {asnApplication smoke test} {
819	set data [asn::asnNull]
820	bytes2hex [asn::asnApplication 1 $data]
821} {41 02 05 00}
822
823test asn-28.0 {asnContextConstr smoke test} {
824	set data [asn::asnNull]
825	bytes2hex [asn::asnContextConstr 1 $data]
826} {A1 02 05 00}
827
828test asn-29.0 {asnChoice smoke test} {
829	set data [asn::asnNull]
830	bytes2hex [asn::asnChoice 1 $data]
831} {81 02 05 00}
832
833test asn-30.0 {asnChoiceConstr smoke test} {
834	set data [asn::asnNull]
835	bytes2hex [asn::asnChoiceConstr 1 $data]
836} {A1 02 05 00}
837
838test asn-31.0 {asnPeekTag smoke test} {
839	set data [asn::asnNull]
840	asn::asnPeekTag data tag tagtype constr
841	list $tag $tagtype $constr
842} {5 UNIVERSAL 0}
843
844foreach {n hex tag type constr} {
845    1	05	    5	    UNIVERSAL	0
846    2	1E	    30	    UNIVERSAL	0
847    3	1F1F	    31	    UNIVERSAL	0
848    4	5F1F	    31	    APPLICATION	0
849    5   1F818000    16384   UNIVERSAL   0
850    6	45	    5	    APPLICATION 0
851    7	65	    5	    APPLICATION 1
852    8	85	    5	    CONTEXT	0
853    9	a5	    5	    CONTEXT	1
854    10	c5	    5	    PRIVATE	0
855    11	e5	    5	    PRIVATE	1
856    12	25	    5	    UNIVERSAL	1
857} {
858    test asn-31.$n "asnPeekTag $tag $type $constr" \
859	"set data \[binary format H* $hex\]
860	 asn::asnPeekTag data tag tagtype constr
861	 list \$tag \$tagtype \$constr" \
862    [list $tag $type $constr]
863}
864
865test asn-32.0 {asnTag smoke test} {
866	bytes2hex [asn::asnTag 5 UNIVERSAL P]
867} {05}
868
869test asn-32.1 {asnTag short tag} {
870	bytes2hex [asn::asnTag 30 UNIVERSAL P]
871} {1E}
872
873test asn-32.2 {asnTag long tag} {
874	bytes2hex [asn::asnTag 31 UNIVERSAL P]
875} {1F 1F}
876
877test asn-32.3 {asnTag long tag} {
878	bytes2hex [asn::asnTag 31 APPLICATION P]
879} {5F 1F}
880
881test asn-32.4 {asnTag long tag} {
882	bytes2hex [asn::asnTag 127 UNIVERSAL P]
883} {1F 7F}
884
885test asn-32.5 {asnTag long tag} {
886	bytes2hex [asn::asnTag 128 UNIVERSAL P]
887} {1F 81 00}
888
889test asn-32.6 {asnTag long tag} {
890	bytes2hex [asn::asnTag 16384 UNIVERSAL P]
891} {1F 81 80 00}
892
893test asn-32.7 {asnTag long tag} {
894	bytes2hex [asn::asnTag 16385 UNIVERSAL P]
895} {1F 81 80 01}
896
897test asn-32.8 {asnTag tag APPLICATION, PRIMITIVE} {
898	bytes2hex [asn::asnTag 5 APPLICATION P]
899} {45}
900
901test asn-32.9 {asnTag tag APPLICATION, CONSTRUCTED} {
902	bytes2hex [asn::asnTag 5 APPLICATION C]
903} {65}
904
905test asn-32.10 {asnTag tag CONTEXT, PRIMITIVE} {
906	bytes2hex [asn::asnTag 5 CONTEXT P]
907} {85}
908
909test asn-32.11 {asnTag tag CONTEXT, CONSTRUCTED} {
910	bytes2hex [asn::asnTag 5 CONTEXT C]
911} {A5}
912
913test asn-32.12 {asnTag tag PRIVATE,PRIMITIVE} {
914	bytes2hex [asn::asnTag 5 PRIVATE P]
915} {C5}
916
917test asn-32.13 {asnTag tag PRIVATE,CONSTRUCTED} {
918	bytes2hex [asn::asnTag 5 PRIVATE C]
919} {E5}
920
921test asn-32.14 {asnTag tag UNIVERSAL, CONSTRUCTED} {
922	bytes2hex [asn::asnTag 5 UNIVERSAL C]
923} {25}
924
925foreach {n hex bin} {
926    1	03020780    1
927    2   030206c0    11
928    3   03020680    10
929    4   030200ff    11111111
930    5   03020000    00000000
931    6   0303078000  100000000
932} {
933    test asn-33.$n "asnBitstring $bin" \
934	"binary scan \[asn::asnBitString $bin\] H* val
935	 set val" \
936	$hex
937}
938
939foreach {n hex bin} {
940    1	03020780    1
941    2   030206c0    11
942    3   03020680    10
943    4   030200ff    11111111
944    5   03020000    00000000
945    6   0303078000  100000000
946} {
947    test asn-34.$n "asnGetBitstring $bin" \
948	"set data \[binary format H* $hex\]
949	 asn::asnGetBitString data bits
950	 set bits " \
951	 $bin
952}
953
954
955testsuiteCleanup
956
957