summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/base32/base32hex.testsuite
blob: bb7b08b8ded40cfb35a8058a451a80b817740e07 (plain)
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
# -*- tcl -*-
# base32hex.testsuite:  tests for hex extended base32.
#
# Public domain
#
# RCS: @(#) $Id: base32hex.testsuite,v 1.1 2006/05/27 20:44:36 andreas_kupries Exp $

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

::tcltest::testConstraint base32hex_critcl [string equal $impl critcl]

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

test base32-hex-${impl}-1.0 {Encode, wrong#args} -body {
    ::base32::hex::encode
} -returnCodes error -result {wrong # args: should be "::base32::hex::encode bitstring"}

test base32-hex-${impl}-1.1 {Encode, wrong#args} -body {
    ::base32::hex::encode a b
} -returnCodes error -result {wrong # args: should be "::base32::hex::encode bitstring"}

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

test base32-hex-${impl}-2.0 {Decode, wrong#args} -body {
    ::base32::hex::decode
} -returnCodes error -result {wrong # args: should be "::base32::hex::decode estring"}

test base32-hex-${impl}-2.1 {Decode, wrong#args} -body {
    ::base32::hex::decode a b
} -returnCodes error -result {wrong # args: should be "::base32::hex::decode estring"}

# -------------------------------------------------------------------------
# 0 .. 6 are the official test vectors from RFC 3548
# 7 .. 12 are the vectors I fot previous, non-conforming,
#         implementation, updated for conformance.
# 13 ..   are new vectors to cover the whole range of bytes

# 4-0              00100 000
# 4-0-16-0         00100 00000 10000 0
# 4-0-16-2-0       00100 00000 10000 00010 0000
# 4-0-16-2-0-8-0   00100 00000 10000 00010 00000 01000 00
# 4-0-16-2-0-8-1-0 00100 00000 10000 00010 00000 01000 00001 00000

foreach {n text encoded} {
    0    {}       {}
    1    f        CO======
    2    fo       CPNG====
    3    foo      CPNMU===
    4    foob     CPNMUOG=
    5    fooba    CPNMUOJ1
    6    foobar   CPNMUOJ1E8======
    -    -        -
    7    { }      40======
    8    {  }     40G0====
    9    {   }    40G20===
    10   {    }   40G2080=
    11   {     }  40G20810
    12   {      } 40G2081040======
    -  -    -         -  -    -         -  -    -         -  -    -       
    20 \000 00======  28 \010 10======  36 \020 20======  44 \030 30======
    21 \001 04======  29 \011 14======  37 \021 24======  45 \031 34======
    22 \002 08======  30 \012 18======  38 \022 28======  46 \032 38======
    23 \003 0C======  31 \013 1C======  39 \023 2C======  47 \033 3C======
    24 \004 0G======  32 \014 1G======  40 \024 2G======  48 \034 3G======
    25 \005 0K======  33 \015 1K======  41 \025 2K======  49 \035 3K======
    26 \006 0O======  34 \016 1O======  42 \026 2O======  50 \036 3O======
    27 \007 0S======  35 \017 1S======  43 \027 2S======  51 \037 3S======
    -  -    -         -  -    -         -  -    -         -  -    -       
    52 \040 40======  60 \050 50======  68 \060 60======  76 \070 70======
    53 \041 44======  61 \051 54======  69 \061 64======  77 \071 74======
    54 \042 48======  62 \052 58======  70 \062 68======  78 \072 78======
    55 \043 4C======  63 \053 5C======  71 \063 6C======  79 \073 7C======
    56 \044 4G======  64 \054 5G======  72 \064 6G======  80 \074 7G======
    57 \045 4K======  65 \055 5K======  73 \065 6K======  81 \075 7K======
    58 \046 4O======  66 \056 5O======  74 \066 6O======  82 \076 7O======
    59 \047 4S======  67 \057 5S======  75 \067 6S======  83 \077 7S======
    -  -    -         -  -    -         -  -    -         -  -    -       
    a0 \100 80======  a8 \110 90======  b6 \120 A0======  c4 \130 B0======
    a1 \101 84======  a9 \111 94======  b7 \121 A4======  c5 \131 B4======
    a2 \102 88======  b0 \112 98======  b8 \122 A8======  c6 \132 B8======
    a3 \103 8C======  b1 \113 9C======  b9 \123 AC======  c7 \133 BC======
    a4 \104 8G======  b2 \114 9G======  c0 \124 AG======  c8 \134 BG======
    a5 \105 8K======  b3 \115 9K======  c1 \125 AK======  c9 \135 BK======
    a6 \106 8O======  b4 \116 9O======  c2 \126 AO======  d0 \136 BO======
    a7 \107 8S======  b5 \117 9S======  c3 \127 AS======  d1 \137 BS======
    -  -    -         -  -    -         -  -    -         -  -    -       
    d2 \140 C0======  e0 \150 D0======  e8 \160 E0======  f6 \170 F0======
    d3 \141 C4======  e1 \151 D4======  e9 \161 E4======  f7 \171 F4======
    d4 \142 C8======  e2 \152 D8======  f0 \162 E8======  f8 \172 F8======
    d5 \143 CC======  e3 \153 DC======  f1 \163 EC======  f9 \173 FC======
    d6 \144 CG======  e4 \154 DG======  f2 \164 EG======  g0 \174 FG======
    d7 \145 CK======  e5 \155 DK======  f3 \165 EK======  g1 \175 FK======
    d8 \146 CO======  e6 \156 DO======  f4 \166 EO======  g2 \176 FO======
    d9 \147 CS======  e7 \157 DS======  f5 \167 ES======  g3 \177 FS======
    -  -    -         -  -    -         -  -    -         -  -    -       
    h0 \200 G0======  h8 \210 H0======  i6 \220 I0======  j4 \230 J0======
    h1 \201 G4======  h9 \211 H4======  i7 \221 I4======  j5 \231 J4======
    h2 \202 G8======  i0 \212 H8======  i8 \222 I8======  j6 \232 J8======
    h3 \203 GC======  i1 \213 HC======  i9 \223 IC======  j7 \233 JC======
    h4 \204 GG======  i2 \214 HG======  j0 \224 IG======  j8 \234 JG======
    h5 \205 GK======  i3 \215 HK======  j1 \225 IK======  j9 \235 JK======
    h6 \206 GO======  i4 \216 HO======  j2 \226 IO======  k0 \236 JO======
    h7 \207 GS======  i5 \217 HS======  j3 \227 IS======  k1 \237 JS======
    -  -    -         -  -    -         -  -    -         -  -    -       
    k2 \240 K0======  l0 \250 L0======  l8 \260 M0======  m6 \270 N0======
    k3 \241 K4======  l1 \251 L4======  l9 \261 M4======  m7 \271 N4======
    k4 \242 K8======  l2 \252 L8======  m0 \262 M8======  m8 \272 N8======
    k5 \243 KC======  l3 \253 LC======  m1 \263 MC======  m9 \273 NC======
    k6 \244 KG======  l4 \254 LG======  m2 \264 MG======  n0 \274 NG======
    k7 \245 KK======  l5 \255 LK======  m3 \265 MK======  n1 \275 NK======
    k8 \246 KO======  l6 \256 LO======  m4 \266 MO======  n2 \276 NO======
    k9 \247 KS======  l7 \257 LS======  m5 \267 MS======  n3 \277 NS======
    -  -    -         -  -    -         -  -    -         -  -    -       
    o0 \300 O0======  o8 \310 P0======  p6 \320 Q0======  q4 \330 R0======
    o1 \301 O4======  o9 \311 P4======  p7 \321 Q4======  q5 \331 R4======
    o2 \302 O8======  p0 \312 P8======  p8 \322 Q8======  q6 \332 R8======
    o3 \303 OC======  p1 \313 PC======  p9 \323 QC======  q7 \333 RC======
    o4 \304 OG======  p2 \314 PG======  q0 \324 QG======  q8 \334 RG======
    o5 \305 OK======  p3 \315 PK======  q1 \325 QK======  q9 \335 RK======
    o6 \306 OO======  p4 \316 PO======  q2 \326 QO======  r0 \336 RO======
    o7 \307 OS======  p5 \317 PS======  q3 \327 QS======  r1 \337 RS======
    -  -    -         -  -    -         -  -    -         -  -    -       
    r2 \340 S0======  s0 \350 T0======  s8 \360 U0======  t6 \370 V0======
    r3 \341 S4======  s1 \351 T4======  s9 \361 U4======  t7 \371 V4======
    r4 \342 S8======  s2 \352 T8======  t0 \362 U8======  t8 \372 V8======
    r5 \343 SC======  s3 \353 TC======  t1 \363 UC======  t9 \373 VC======
    r6 \344 SG======  s4 \354 TG======  t2 \364 UG======  u0 \374 VG======
    r7 \345 SK======  s5 \355 TK======  t3 \365 UK======  u1 \375 VK======
    r8 \346 SO======  s6 \356 TO======  t4 \366 UO======  u2 \376 VO======
    r9 \347 SS======  s7 \357 TS======  t5 \367 US======  u3 \377 VS======
} {
    if {$n == "-"} continue
    test base32-hex-${impl}-3.$n "Encode \"$text\"" -body {
	::base32::hex::encode $text
    } -result $encoded ; # {}

    test base32-hex-${impl}-4.$n "Decode \"$encoded\"" -body {
	::base32::hex::decode $encoded
    } -result $text ; # {}
}

# -------------------------------------------------------------------------
# Decoder stress testing bad input

foreach {n input message} {
    0 abcdeZaa {Invalid character at index 5: "Z"}
    1 A         {Length is not a multiple of 8}
    2 ABCDEFG   {Length is not a multiple of 8}
    3 A=======  {Invalid padding of length 7}
    4 ACA=====  {Invalid padding of length 5}
    5 A=CA====  {Invalid character at index 1: "=" (padding found in the middle of the input)}
} {
    test base32-hex-${impl}-5.$n "Decode, bad input \"$input\"" -body {
	::base32::hex::decode $input
    } -returnCodes error -result $message ; # {}
}