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
|
# -*- 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,2005 by Michael Schlenker <mic42@users.sourceforge.net>
# All rights reserved.
#
# RCS: @(#) $Id: asn.test,v 1.5 2005/02/15 17:50:21 mic42 Exp $
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest
namespace import ::tcltest::*
}
# -------------------------------------------------------------------------
# Ensure we test _this_ local copy and one installed somewhere else.
package forget asn
catch {namespace delete ::asn}
if {[catch {source [file join [file dirname [info script]] asn.tcl]} msg]} {
puts "skipped [file tail [info script]]: $msg"
return
}
package require struct
puts "- asn [package present asn]"
# -------------------------------------------------------------------------
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
} {
test asn-12.$n {getInteger} {
set data [binary format H2H2H* 02 $len $hex]
asn::asnGetInteger data int
set int
} $i ; # {}
}
# -------------------------------------------------------------------------
::tcltest::cleanupTests
|