File: asn.test

package info (click to toggle)
tcllib 2.0%2Bdfsg-4
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 83,572 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (960 lines) | stat: -rw-r--r-- 26,902 bytes parent folder | download | duplicates (2)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# -*- tcl -*-
# asn.test:  tests for the asn BER encoding/decoding module.
#
# Copyright (c) 2004 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
# Copyright (c) 2004-2007 by Michael Schlenker <mic42@users.sourceforge.net>
# All rights reserved.

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]
	
testsNeedTcl     8.5
testsNeedTcltest 1.0

testing {
    useLocal asn.tcl asn
}

# Converts binary encoded structure into hexadecimal dump
# which is more readable in test results
# Allows cut'n'paste both encoded and parsed OID from dumpasn1.cfg
#
proc bytes2hex {string} {
    foreach b [split $string ""] {
	lappend l [format %02X [scan $b %c]]
    }
    return [join $l " "]
}	

# -------------------------------------------------------------------------

test asn-1.0 {integer} {
    catch {asn::asnInteger} result
    set result
} [tcltest::wrongNumArgs {asn::asnInteger} {number} 0]

test asn-1.1 {integer} {
    catch {asn::asnInteger a b} result
    set result
} [tcltest::tooManyArgs {asn::asnInteger} {number}]

test asn-1.2 {integer} {
    catch {asn::asnInteger a} result
    set result
} {expected integer but got "a"}


test asn-3.0 {enum} {
    catch {asn::asnEnumeration} result
    set result
} [tcltest::wrongNumArgs {asn::asnEnumeration} {number} 0]

test asn-3.1 {enum} {
    catch {asn::asnEnumeration a b} result
    set result
} [tcltest::tooManyArgs {asn::asnEnumeration} {number}]

test asn-3.2 {enum} {
    catch {asn::asnEnumeration a} result
    set result
} {expected integer but got "a"}




foreach {n i len hex} {
     0  0        01 00
     1 -1        01 FF
     2  1        01 01
     3  127      01 7F
     4  128      02 0080
     5  129      02 0081
     6  256      02 0100
     7 -127      01 81
     8 -128      01 80
     9 -129      02 FF7F
    10  32766    02 7FFE
    11  32767    02 7FFF
    12  32768    03 008000
    13  32769    03 008001
    14 -32767    02 8001
    15 -32768    02 8000
    16 -32769    03 FF7FFF
    17  65536    03 010000
    18  8388607  03 7FFFFF
    19  8388608  04 00800000
    20  8388609  04 00800001
    21  16777216 04 01000000
    22 -8388607  03 800001
    23 -8388608  03 800000
    24 -8388609  04 FF7FFFFF
    25 -65536    03 FF0000
    26 -2147483648              04 80000000 
    27 2147483647               04 7FFFFFFF
    28 -549755813888            05 8000000000
    29 549755813887             05 7FFFFFFFFF
    30 -140737488355328         06 800000000000
    31 140737488355327          06 7FFFFFFFFFFF
    32 -36028797018963968       07 80000000000000
    33 36028797018963967        07 7FFFFFFFFFFFFF
    34 36028797018963968        08 0080000000000000
    35 -9223372036854775808     08 8000000000000000
    36 9223372036854775807      08 7FFFFFFFFFFFFFFF
} {
    test asn-2.$n {integer} {
    binary scan [asn::asnInteger $i] H* result
    list $i [string toupper $result]
    } [list $i 02$len$hex] ; # {}

    test asn-4.$n {enum} {
    binary scan [asn::asnEnumeration $i] H* result
    list $i [string toupper $result]
    } [list $i 0A$len$hex] ; # {}
}

test asn-5.0 {boolean} {
    catch {asn::asnBoolean} result
    set result
} [tcltest::wrongNumArgs {asn::asnBoolean} {bool} 0]

test asn-5.1 {boolean} {
    catch {asn::asnBoolean a b} result
    set result
} [tcltest::tooManyArgs {asn::asnBoolean} {bool}]

test asn-5.2 {boolean} {
    catch {asn::asnBoolean a} result
    set result
} {expected boolean value but got "a"}

test asn-5.3 {boolean - true} {
    binary scan [asn::asnBoolean 1] H* result
    string toupper $result
} {0101FF}

test asn-5.4 {boolean - false} {
    binary scan [asn::asnBoolean 0] H* result
    string toupper $result
} {010100}

test asn-6.0 {parse boolean} {
    catch {asn::asnGetBoolean} result
    set result
} [tcltest::wrongNumArgs {asn::asnGetBoolean} {data_var bool_var} 0]

test asn-6.1 {parse boolean} {
    catch {asn::asnGetBoolean a} result
    set result
} [tcltest::wrongNumArgs {asn::asnGetBoolean} {data_var bool_var} 1]

test asn-6.2 {parse boolean} {
    catch {asn::asnGetBoolean a b c} result
    set result
} [tcltest::tooManyArgs {asn::asnGetBoolean} {data_var bool_var}]

test asn-6.3 {parse boolean} {
    catch {asn::asnGetBoolean a b} result
    set result
} {can't read "data": no such variable}

test asn-6.4 {parse boolean - wrong tag} {
    set a \x02\x01\x00
    catch {asn::asnGetBoolean a b} result
    set result
} {Expected Boolean (0x01), but got 02}

test asn-6.5 {parse boolean - wrong length} {
    set a \x01\x02\x00
    catch {asn::asnGetBoolean a b} result
    list $result $b
} [list "" 0]

test asn-6.6 {parse boolean - true} {
    set a \x01\x01\xFF
    asn::asnGetBoolean a b
    set b
} 1

test asn-6.7 {parse boolean - true} {
    set a \x01\x01\x01
    asn::asnGetBoolean a b
    set b
} 1

test asn-6.8 {parse boolean - false} {
    set a \x01\x01\x00
    asn::asnGetBoolean a b
    set b
} 0

test asn-7.0 {null} {
    catch {asn::asnNull foo} result
    set result
} [tcltest::tooManyArgs {asn::asnNull} {}]

test asn-7.1 {null} {
    binary scan [asn::asnNull] H* result
    set result 
} {0500}

test asn-8.0 {parse null} {
    catch {asn::asnGetNull} result
    set result
} [tcltest::wrongNumArgs asn::asnGetNull {data_var} 0]

test asn-8.1 {parse null} {
    catch {asn::asnGetNull foo bar} result
    set result
} [tcltest::tooManyArgs {asn::asnGetNull} {data_var}]

test asn-8.2 {parse null} {
    set wrongtag \x01\x01
    catch {asn::asnGetNull wrongtag} result
    set result
} {Expected NULL (0x05), but got 01}

test asn-8.3 {parse null} {
    set wronglength \x05\x01
    catch {asn::asnGetNull wronglength} result
    set result
} {}

test asn-8.4 {parse null} {
    set null \x05\x00
    asn::asnGetNull null
} {}

package require math::bignum
foreach {n i len hex} {
     0  0        01 00
     1 -1        01 FF
     2  1        01 01
     3  127      01 7F
     4  128      02 0080
     5  129      02 0081
     6  256      02 0100
     7 -127      01 81
     8 -128      01 80
     9 -129      02 FF7F
    10  32766    02 7FFE
    11  32767    02 7FFF
    12  32768    03 008000
    13  32769    03 008001
    14 -32767    02 8001
    15 -32768    02 8000
    16 -32769    03 FF7FFF
    17  65536    03 010000
    18  8388607  03 7FFFFF
    19  8388608  04 00800000
    20  8388609  04 00800001
    21  16777216 04 01000000
    22 -8388607  03 800001
    23 -8388608  03 800000
    24 -8388609  04 FF7FFFFF
    25 -65536    03 FF0000
} {
    test asn-9.$n {big integer} {
    binary scan [asn::asnBigInteger [math::bignum::fromstr $i]] H* result
    list $i [string toupper $result]
    } [list $i 02$len$hex] ; # {}

}

foreach {n len hex} {
    0   0                   00
    1   1                   01
    2   127                 7F
    3   128                 8180
    4   129                 8181
    5   255                 81FF
    6   256                 820100
    7   32767               827FFF
    8   32768               828000
    9   32769               828001
    10  65535               82FFFF
    11  65536               83010000
    12  8388607             837FFFFF
    13  8388608             83800000
    14  8388609             83800001
    15  16777215            83FFFFFF
    16  16777216            8401000000
    17  4294967295          84FFFFFFFF
    18  4294967296          850100000000
    19  1099511627775       85FFFFFFFFFF
    20  1099511627776       86010000000000
    21  281474976710655     86FFFFFFFFFFFF
    22  281474976710656     8701000000000000
    23  72057594037927935   87FFFFFFFFFFFFFF
    24  72057594037927936   880100000000000000
    25  9223372036854775807 887FFFFFFFFFFFFFFF
    } {
    test asn-10.$n {asnLength encoding} {
    binary scan [asn::asnLength $len] H* result
    string toupper $result
    } $hex
}

foreach {n len hex} {
    0   0                   00
    1   1                   01
    2   127                 7F
    3   128                 8180
    4   129                 8181
    5   255                 81FF
    6   256                 820100
    7   32767               827FFF
    8   32768               828000
    9   32769               828001
    10  65535               82FFFF
    11  65536               83010000
    12  8388607             837FFFFF
    13  8388608             83800000
    14  8388609             83800001
    15  16777215            83FFFFFF
    16  16777216            8401000000
    17  4294967295          84FFFFFFFF
    18  4294967296          850100000000
    19  1099511627775       85FFFFFFFFFF
    20  1099511627776       86010000000000
    21  281474976710655     86FFFFFFFFFFFF
    22  281474976710656     8701000000000000
    23  72057594037927935   87FFFFFFFFFFFFFF
    24  72057594037927936   880100000000000000
    25  9223372036854775807 887FFFFFFFFFFFFFFF
    } {
    test asn-11.$n {asnGetLength decoding} {
    set data [binary format H* $hex ]
    asn::asnGetLength data length
    set length
    } $len
}

foreach {n i len hex} {
     0  0               01 00
     1 -1               01 FF
     2  1               01 01
     3  127             01 7F
     4  128             02 0080
     5  129             02 0081
     6  256             02 0100
     7 -127             01 81
     8 -128             01 80
     9 -129             02 FF7F
    10  32766           02 7FFE
    11  32767           02 7FFF
    12  32768           03 008000
    13  32769           03 008001
    14 -32767           02 8001
    15 -32768           02 8000
    16 -32769           03 FF7FFF
    17  65536           03 010000
    18  8388607         03 7FFFFF
    19  8388608         04 00800000
    20  8388609         04 00800001
    21  16777216        04 01000000
    22 -8388607         03 800001
    23 -8388608         03 800000
    24 -8388609         04 FF7FFFFF
    25 -65536           03 FF0000
    26 -2147483648      04 80000000 
    27 2147483647       04 7FFFFFFF
    28 -549755813888    05 8000000000
    29 549755813887     05 7FFFFFFFFF
    30 -140737488355328 06 800000000000
    31 140737488355327  06 7FFFFFFFFFFF
    32 -36028797018963968 07 80000000000000
    33 36028797018963967 07  7FFFFFFFFFFFFF
    34 36028797018963968 08  0080000000000000
    35 -9223372036854775808 08 8000000000000000
    36 9223372036854775807 08 7FFFFFFFFFFFFFFF
    37 65537            03 010001
    38 -8323071         03 810001 
} {
    test asn-12.$n {getInteger} {
    set data [binary format H2H2H* 02 $len $hex]
    asn::asnGetInteger data int
    set int
    }  $i  ; # {}
}

foreach {n i len hex} {
     0  0               01 00
     1 -1               01 FF
     2  1               01 01
     3  127             01 7F
     4  128             02 0080
     5  129             02 0081
     6  256             02 0100
     7 -127             01 81
     8 -128             01 80
     9 -129             02 FF7F
    10  32766           02 7FFE
    11  32767           02 7FFF
    12  32768           03 008000
    13  32769           03 008001
    14 -32767           02 8001
    15 -32768           02 8000
    16 -32769           03 FF7FFF
    17  65536           03 010000
    18  8388607         03 7FFFFF
    19  8388608         04 00800000
    20  8388609         04 00800001
    21  16777216        04 01000000
    22 -8388607         03 800001
    23 -8388608         03 800000
    24 -8388609         04 FF7FFFFF
    25 -65536           03 FF0000
    26 -2147483648      04 80000000 
    27 2147483647       04 7FFFFFFF
    28 -549755813888    05 8000000000
    29 549755813887     05 7FFFFFFFFF
    30 -140737488355328 06 800000000000
    31 140737488355327  06 7FFFFFFFFFFF
    32 -36028797018963968 07 80000000000000
    33 36028797018963967 07  7FFFFFFFFFFFFF
    34 36028797018963968 08  0080000000000000
    35 -9223372036854775808 08 8000000000000000
    36 9223372036854775807 08 7FFFFFFFFFFFFFFF
    37 65537            03 010001
    38 -8323071         03 810001 
} {
    test asn-12.[expr {$n+39}] {getBigInteger} {
    set data [binary format H2H2H* 02 $len $hex]
    asn::asnGetBigInteger data int
    math::bignum::tostr $int
    }  $i  ; # {}
}

test asn-13.0 {peekByte} {
    set data \x0d\x0a
    asn::asnPeekByte data byte
    list $byte [string length $data]
} {13 2}

test asn-14.0 {getByte} {
    set data \x0d\x0a
    asn::asnGetByte data byte
    list $byte [string length $data]
} {13 1}

test asn-15.0 {getBytes} {
    set data \x0d\x0d\x0d\x0d\x0a
    asn::asnGetBytes data 4 bytes
    list [string length $data] [string length $bytes]
} [list 1 4]

test asn-15.1 {getBytes} {
    set data \x0d\x0d\x0d\x0d\x0a
    asn::asnGetBytes data 4 bytes
    set expectedbytes \x0d\x0d\x0d\x0d
    set expecteddata \x0a
    list [expr {$data == $expecteddata}] [expr {$bytes == $expectedbytes}]
} [list 1 1]

# 16 ----------- string encoder/decoder invalid arguments
array set stringtag {
    NumericString   0x12
    PrintableString 0x13
    IA5String       0x16
    BMPString       0x1e
    UTF8String      0x0c
}
set i 0
foreach strtype {NumericString PrintableString IA5String BMPString UTF8String} {
	incr i
	test asn-16.$i $strtype {
	    catch {asn::asn$strtype} result
	    set result
	} [tcltest::wrongNumArgs asn::asn$strtype string 0]
	incr i
	test asn-16.$i $strtype {
		catch "asn::asn$strtype a b" result
		set result
	} [tcltest::tooManyArgs "asn::asn$strtype" string]
	incr i
	test asn-16.$i get$strtype {
		catch "asn::asnGet$strtype foo" result
		set result
	} [tcltest::wrongNumArgs "asn::asnGet$strtype" "data_var print_var" 0]	
	incr i
	test asn-16.$i get$strtype {
		catch "asn::asnGet$strtype foo bar baz" result
		set result
	} [tcltest::tooManyArgs "asn::asnGet$strtype" "data_var print_var"]	
	incr i
	test asn-16.$i "get$strtype parse sequence" {
		set data "\x30\x03abc"
		catch "asn::asnGet$strtype data print" result
		set result
	} "Expected [regsub String $strtype " String"] ($stringtag($strtype)), but got 30"	
}
incr i
# 17 ------------------- invalid string values


test asn-17.1 {numeric string with non-numbers} {
	catch {asn::asnNumericString this-is-not-a-number} result
	set result
} "Illegal character in Numeric String."

test asn-17.2 {numeric string with hexadecimals} {
	catch {asn::asnNumericString 09AB} result
	set result
} "Illegal character in Numeric String."

test asn-17.3 {numeric string with spaces - spaces are legal} {
	catch {asn::asnNumericString " 093"}
} 0 ;# TCL_OK

test asn-17.4 {numeric string with minus sign} {
	catch {asn::asnNumericString "-15"}  result
	set result
} "Illegal character in Numeric String."
	
test asn-17.5 {numeric string with tab (illegal)} {
	catch {asn::asnNumericString "\t093"} result
	set result
} "Illegal character in Numeric String."

#According to ITU-T X.680 37.4
set printablechars {[-A-Za-z0-9 '()+,./=?:]}

set i 6

for {set j 1} {$j<128} {incr j;incr i} {
    set data [format %c $j]
    if {[regexp "^$printablechars+\$" $data]} {
	test asn-17.$i "printable string valid char [format %02x $j]" {
	    catch {asn::asnPrintableString $data}
	} 0 ; # {}
    } else {
	test asn-17.$i "printable string invalid char [format %02x $j]" {
	    catch {asn::asnPrintableString $data} result
	    set result
	} "Illegal character in PrintableString." ; # {}
    }
}

test asn-17.134 {IA5String with Latin-1 char} {
	catch {asn::asnIA5String "\xD0"} result
	set result
} "Illegal character in IA5String"

test asn-17.135 {IA5String with Cyrillic chars} {
	catch {asn::asnIA5String "\u0420\u0443\u0441\u0441\u043a\u0438\u0439"} result
	set result
} "Illegal character in IA5String"

# 18 - correct encoding of string values

test asn-18.1 {encode numeric string} {
	catch {asn::asnNumericString 123} result
	set result
} "\x12\003123"

test asn-18.2 {encode numeric strin with space} {
	catch {asn::asnNumericString "1 2 3"} result
	set result
} "\x12\0051 2 3"	

test asn-18.3 {encode printable string} {
	catch {asn::asnPrintableString "printable string"} result
	set result
} "\x13\x10printable string"

test asn-18.4 {encode IA5 string} {
	catch {asn::asnIA5String "vitus@45.free.net"} result
	set result
} "\x16\x11vitus@45.free.net"	

test asn-18.5 {encode bmp string US-ASCII} {
	catch {asn::asnBMPString "US-ASCII"} result
	set result
} "\x1e\x10\0U\0S\0-\0A\0S\0C\0I\0I"	

test asn-18.6 {encode UTF8 string US-ASCII} {
	catch {asn::asnUTF8String "US-ASCII"} result
	set result
} "\x0c\x08US-ASCII"	

test asn-18.7 {encode bmp string latin-1} {
	catch {asn::asnBMPString "gar\xC7on"} result
	set result
} "\x1e\x0c\0g\0a\0r\0\xc7\0o\0n"

test asn-18.8 {encode utf-8 string latin-1} {
	catch {asn::asnUTF8String "gar\xC7on"} result
	set result
} "\x0c\x07gar\xc3\x87on"

test asn-18.9 {encode bmp string cyrillic} {
	catch {asn::asnBMPString "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"} result
	set result
} "\x1e\x0e\x04\x40\x04\x43\x04\x41\x04\x41\x04\x3a\x04\x38\x04\x39"

test asn-18.10 {encode UTF8 string cyrillic} {
	catch {asn::asnUTF8String "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"} result
	set result
} "\x0c\x0e\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9"

test asn-18.11 {decode numeric string} {
	set data "\x12\003123"
	asn::asnGetNumericString data print
	set print
} 123

test asn-18.12 {decode printable string} {
	set data "\x13\x10printable string"
	asn::asnGetPrintableString data print
	set print
} "printable string"

test asn-18.13 {decode IA5 string} {
	set data "\x16\x11vitus@45.free.net"
	asn::asnGetIA5String data print
	set print
} "vitus@45.free.net"

test asn-18.14 {decode BMP string US-ASCII} {
	set data "\x1e\x10\0U\0S\0-\0A\0S\0C\0I\0I"
	asn::asnGetBMPString data print
	set print
} "US-ASCII"

test asn-18.15 {decode UTF8 string US-ASCII} {
	set data "\x0c\x08US-ASCII"
	asn::asnGetUTF8String data print
	set print
} "US-ASCII"

test asn-18.16 {decode BMP string latin-1} {
	set data "\x1e\x0c\0g\0a\0r\0\xc7\0o\0n"
	asn::asnGetBMPString data print
	set print
} "gar\xC7on"

test asn-18.17 {decode UTF8 string latin-1} {
	set data "\x0c\x07gar\xc3\x87on"
	asn::asnGetUTF8String data print
	set print
} "gar\xC7on"

test asn-18.18 {decode BMP string cyrillic} {
	set data "\x1e\x0e\x04\x40\x04\x43\x04\x41\x04\x41\x04\x3a\x04\x38\x04\x39"
	asn::asnGetBMPString data print
	set print
} "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"

test asn-18.19 {decode UTF8 string cyrillic} {
	set data "\x0c\x0e\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9"
	asn::asnGetUTF8String data print
	set print
} "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"	

# 19 ------- multitype getString
set i 0
foreach {type encoded str} {

NumericString "\x12\003123" 123
PrintableString  "\x13\x10printable string" "printable string"
IA5String "\x16\x11vitus@45.free.net" "vitus@45.free.net"
BMPString "\x1e\x10\0U\0S\0-\0A\0S\0C\0I\0I" US-ASCII
UTF8String "\x0c\x08US-ASCII" US-ASCII
BMPString "\x1e\x0c\0g\0a\0r\0\xc7\0o\0n" "gar\xc7on"
UTF8String "\x0c\x07gar\xc3\x87on" "gar\xc7on"
BMPString "\x1e\x0e\x04\x40\x04\x43\x04\x41\x04\x41\x04\x3a\x04\x38\x04\x39"
"\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
UTF8String "\x0c\x0e\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9"
"\u0440\u0443\u0441\u0441\u043a\u0438\u0439"} {
incr i
	test asn-19.$i "getString - decode $type" {
		set data $encoded
		asn::asnGetString data print
		set print
	} $str
	incr i
	test asn-19.$i "getString - decode $type and get its type" {
		set data $encoded
		asn::asnGetString data print gotType
		list $data $print $gotType
	} [list {} $str $type]
}

# 20 ----- multitype String encoding

test asn-20.1 {Set default type to something wrong} {
	catch {asn::defaultStringType foo} result
	set result
} "Invalid default string type. Should be one of BMP, UTF8"	

test asn-20.2 {Set default value to string type which cannot hold any char} {
	catch {asn::defaultStringType IA5} result
	set result
} "Invalid default string type. Should be one of BMP, UTF8"	


test asn-20.3 {Set default type to UTF8} {
	asn::defaultStringType UTF8
} ""

test asn-20.4 {Get default string type} {
	asn::defaultStringType 
} UTF8	

test asn-20.5 {String - encode numeric value} {
	asn::asnString 123
} "\x12\003123"

test asn-20.6 {String - encode printable value} {
	asn::asnString "printable string"
} "\x13\x10printable string"

test asn-20.7 {String - encode ASCII value} {
	asn::asnString vitus@45.free.net
} "\x16\x11vitus@45.free.net" 

test asn-20.8 {String - encode Latin-1 value} {
	asn::asnString "gar\xc7on"
} "\x0c\x07gar\xc3\x87on"

test asn-20.9 {String - encode Cyrillic value} {
	asn::asnString "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
} "\x0c\x0e\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9"

test asn-20.10 {Set default string type to BMP} {
	asn::defaultStringType BMP
	asn::defaultStringType
} BMP

test asn-20.11 {String - encode numeric value} {
	asn::asnString 123
} "\x12\003123"

test asn-20.12 {String - encode printable value} {
	asn::asnString "printable string"
} "\x13\x10printable string"

test asn-20.13 {String - encode ASCII value} {
	asn::asnString vitus@45.free.net
} "\x16\x11vitus@45.free.net" 


test asn-20.14 {String - encode Latin-1 value} {
	asn::asnString "gar\xc7on"
} "\x1e\x0c\0g\0a\0r\0\xc7\0o\0n" 

test asn-20.15 {String - encode Cyrillic value} {
	asn::asnString "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
} "\x1e\x0e\x04\x40\x04\x43\x04\x41\x04\x41\x04\x3a\x04\x38\x04\x39" 

# 21 --------- Object identifier
#

test asn-21.1 {ObjectIdentifier start with 0} {
	bytes2hex [asn::asnObjectIdentifier {0 2 262 1 10}]	
} "06 05 02 82 06 01 0A"


test asn-21.2 {ObjectIdentifier start with 1} {
	bytes2hex [asn::asnObjectIdentifier {1 2 840 10045 2 1}]
} "06 07 2A 86 48 CE 3D 02 01"

test asn-21.3 {ObjectIdentifer field > 65536} {
	bytes2hex [asn::asnObjectIdentifier {1 2 840 113533 7 66 3}]
} "06 09 2A 86 48 86 F6 7D 07 42 03"	

test asn-21.4 {ObjectIdentifer 2.23.42.9.37} {
	bytes2hex [asn::asnObjectIdentifier {2 23 42 9 37}]
} "06 04 67 2A 09 25"	

test asn-21.5 {GetObjectIdentifier 0.2.262.1.10} {
	set data "\x06\x05\x02\x82\x06\x01\x0A"
	asn::asnGetObjectIdentifier data print
	set print
} {0 2 262 1 10}

test asn-21.6 {GetObjectIdentifier 1 2 840 10045 2 1} {
	set data "\x06\x07\x2A\x86\x48\xCE\x3D\x02\x01"
	asn::asnGetObjectIdentifier data print
	set print
} {1 2 840 10045 2 1}

test asn-21.7 {GetObjectIdentifier 1 2 840 113533 7 66 3} {
	set data "\x06\x09\x2A\x86\x48\x86\xF6\x7D\x07\x42\x03"
	asn::asnGetObjectIdentifier data print
	set print
} {1 2 840 113533 7 66 3}

# 22 --- Octet String

# smoke tests to check that we can at least call the commands
test asn-23.0 {asnContext smoke test} {
	set data "\x00"
	bytes2hex [asn::asnContext 1 $data]
} {81 01 00}

test asn-24.0 {asnSequence smoke test} {
	set data [asn::asnNull]
	bytes2hex [asn::asnSequence 1 $data]
} {30 03 31 05 00}

test asn-25.0 {asnSet smoke test} {
	set data [asn::asnNull]
	bytes2hex [asn::asnSet 1 $data]
} {31 03 31 05 00}

test asn-26.0 {asnApplicationConstr smoke test} {
	set data [asn::asnNull]
	bytes2hex [asn::asnApplicationConstr 1 $data]
} {61 02 05 00}

test asn-27.0 {asnApplication smoke test} {
	set data [asn::asnNull]
	bytes2hex [asn::asnApplication 1 $data]
} {41 02 05 00}

test asn-28.0 {asnContextConstr smoke test} {
	set data [asn::asnNull]
	bytes2hex [asn::asnContextConstr 1 $data]
} {A1 02 05 00}

test asn-29.0 {asnChoice smoke test} {
	set data [asn::asnNull]
	bytes2hex [asn::asnChoice 1 $data]
} {81 02 05 00}

test asn-30.0 {asnChoiceConstr smoke test} {
	set data [asn::asnNull]
	bytes2hex [asn::asnChoiceConstr 1 $data]
} {A1 02 05 00}

test asn-31.0 {asnPeekTag smoke test} {
	set data [asn::asnNull]
	asn::asnPeekTag data tag tagtype constr
	list $tag $tagtype $constr
} {5 UNIVERSAL 0}

foreach {n hex tag type constr} {
    1	05	    5	    UNIVERSAL	0
    2	1E	    30	    UNIVERSAL	0
    3	1F1F	    31	    UNIVERSAL	0
    4	5F1F	    31	    APPLICATION	0
    5   1F818000    16384   UNIVERSAL   0
    6	45	    5	    APPLICATION 0
    7	65	    5	    APPLICATION 1
    8	85	    5	    CONTEXT	0
    9	a5	    5	    CONTEXT	1
    10	c5	    5	    PRIVATE	0
    11	e5	    5	    PRIVATE	1
    12	25	    5	    UNIVERSAL	1
} {
    test asn-31.$n "asnPeekTag $tag $type $constr" \
	"set data \[binary format H* $hex\]
	 asn::asnPeekTag data tag tagtype constr
	 list \$tag \$tagtype \$constr" \
    [list $tag $type $constr]	 
}

test asn-32.0 {asnTag smoke test} {
	bytes2hex [asn::asnTag 5 UNIVERSAL P]
} {05}

test asn-32.1 {asnTag short tag} {
	bytes2hex [asn::asnTag 30 UNIVERSAL P]
} {1E} 

test asn-32.2 {asnTag long tag} {
	bytes2hex [asn::asnTag 31 UNIVERSAL P]
} {1F 1F}

test asn-32.3 {asnTag long tag} {
	bytes2hex [asn::asnTag 31 APPLICATION P]
} {5F 1F}

test asn-32.4 {asnTag long tag} {
	bytes2hex [asn::asnTag 127 UNIVERSAL P]
} {1F 7F}

test asn-32.5 {asnTag long tag} {
	bytes2hex [asn::asnTag 128 UNIVERSAL P]
} {1F 81 00}

test asn-32.6 {asnTag long tag} {
	bytes2hex [asn::asnTag 16384 UNIVERSAL P]
} {1F 81 80 00}

test asn-32.7 {asnTag long tag} {
	bytes2hex [asn::asnTag 16385 UNIVERSAL P]
} {1F 81 80 01}

test asn-32.8 {asnTag tag APPLICATION, PRIMITIVE} {
	bytes2hex [asn::asnTag 5 APPLICATION P]
} {45}

test asn-32.9 {asnTag tag APPLICATION, CONSTRUCTED} {
	bytes2hex [asn::asnTag 5 APPLICATION C]
} {65}

test asn-32.10 {asnTag tag CONTEXT, PRIMITIVE} {
	bytes2hex [asn::asnTag 5 CONTEXT P]
} {85}

test asn-32.11 {asnTag tag CONTEXT, CONSTRUCTED} {
	bytes2hex [asn::asnTag 5 CONTEXT C]
} {A5}

test asn-32.12 {asnTag tag PRIVATE,PRIMITIVE} {
	bytes2hex [asn::asnTag 5 PRIVATE P]
} {C5}

test asn-32.13 {asnTag tag PRIVATE,CONSTRUCTED} {
	bytes2hex [asn::asnTag 5 PRIVATE C]
} {E5}

test asn-32.14 {asnTag tag UNIVERSAL, CONSTRUCTED} {
	bytes2hex [asn::asnTag 5 UNIVERSAL C]
} {25}

foreach {n hex bin} {
    1	03020780    1
    2   030206c0    11
    3   03020680    10
    4   030200ff    11111111
    5   03020000    00000000
    6   0303078000  100000000
} {
    test asn-33.$n "asnBitstring $bin" \
	"binary scan \[asn::asnBitString $bin\] H* val
	 set val" \
	$hex
}

foreach {n hex bin} {
    1	03020780    1
    2   030206c0    11
    3   03020680    10
    4   030200ff    11111111
    5   03020000    00000000
    6   0303078000  100000000
} {
    test asn-34.$n "asnGetBitstring $bin" \
	"set data \[binary format H* $hex\] 
	 asn::asnGetBitString data bits 
	 set bits " \
	 $bin
}

test asn-35.0 {asnOctetString} {
    bytes2hex [asn::asnOctetString 1.3.6.1.4.1.1466.20037] 
} {04 16 31 2E 33 2E 36 2E 31 2E 34 2E 31 2E 31 34 36 36 2E 32 30 30 33 37}

test asn-36.0 {asnApplicationConstr + asnOctetString / ldap} {
    bytes2hex [asn::asnApplicationConstr 23 [asn::asnOctetString 1.3.6.1.4.1.1466.20037]]
} {77 18 04 16 31 2E 33 2E 36 2E 31 2E 34 2E 31 2E 31 34 36 36 2E 32 30 30 33 37}

# -------------------------------------------------------------------------
testsuiteCleanup
return