File: base64.test

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 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 (242 lines) | stat: -rw-r--r-- 9,076 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
# Tests for the base64 module.                              -*- tcl -*-
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1998-2000 by Ajuba Solutions.
# 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 base64.tcl base64
}

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

tcltest::testConstraint tcl86 0
tcltest::testConstraint trf   0

if {[package vsatisfies [package present Tcl] 8.6 9]} {
    puts "> Core based"
    tcltest::testConstraint tcl86 1
} elseif {![catch {package present Trf}]} {
    puts "> Trf based"
    tcltest::testConstraint trf 1
} else {
    puts "> pure Tcl"
}

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

test base64-1.1 {base64::encode} {
    base64::encode "this is a test\n"
} "dGhpcyBpcyBhIHRlc3QK"
test base64-1.2 {base64::encode wraps lines at 76 characters} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode $str
} "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3JlZW4gZmllbGQgYW5k
IGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
test base64-1.3 {base64::encode with wrap length set to 60} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 60 $str
} "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3Jl
ZW4gZmllbGQgYW5kIGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
test base64-1.4 {base64::encode with wrap length set to 0} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 0 $str
} "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3JlZW4gZmllbGQgYW5kIGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
test base64-1.5 {base64::encode with wrap length set to 76, wrapchar to newline+space} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 76 -wrapchar "\n " $str
} "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3JlZW4gZmllbGQgYW5k
 IGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"

test base64-1.6 {base64::encode, errors} !tcl86 {
    list [catch {base64::encode} msg] $msg
} {1 {wrong # args: should be "base64::encode ?-maxlen maxlen? ?-wrapchar wrapchar? string"}}

test base64-1.6 {base64::encode, errors} tcl86 {
    list [catch {base64::encode} msg] $msg
} {1 {wrong # args: should be "binary encode base64 ?-maxlen len? ?-wrapchar char? data"}}

test base64-1.7 {base64::encode, errors} !tcl86 {
    list [catch {base64::encode -maxlen foo} msg] $msg
} {1 {value for "-maxlen" missing}}

test base64-1.7 {base64::encode, errors} tcl86 {
    list [catch {base64::encode -maxlen foo} msg] $msg
} {1 {wrong # args: should be "binary encode base64 ?-maxlen len? ?-wrapchar char? data"}}

test base64-1.8 {base64::encode, errors} {
    list [catch {base64::encode -maxlen foo bar} msg] $msg
} [list 1 "expected integer but got \"foo\""]

test base64-1.9 {base64::encode, errors} !tcl86 {
    list [catch {base64::encode -maxlen foo -wrapchar bar} msg] $msg
} {1 {value for "-wrapchar" missing}}

test base64-1.9 {base64::encode, errors} tcl86 {
    list [catch {base64::encode -maxlen foo -wrapchar bar} msg] $msg
} {1 {wrong # args: should be "binary encode base64 ?-maxlen len? ?-wrapchar char? data"}}

test base64-1.10 {base64::encode, errors} !tcl86 {
    list [catch {base64::encode -foo bar} msg] $msg
} {1 {unknown option "-foo": must be -maxlen or -wrapchar}}

test base64-1.10 {base64::encode, errors} tcl86 {
    list [catch {base64::encode -foo bar} msg] $msg
} {1 {wrong # args: should be "binary encode base64 ?-maxlen len? ?-wrapchar char? data"}}

test base64-1.11 {base64::encode with bogus wrap length (< 0)} !tcl86 {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    list [catch { base64::encode -maxlen -3 $str } msg] $msg
} {1 {expected positive integer but got "-3"}}

test base64-1.11 {base64::encode with bogus wrap length (< 0)} tcl86 {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    list [catch { base64::encode -maxlen -3 $str } msg] $msg
} {1 {line length out of range}}

test base64-1.12 {base64::encode with bogus wrap length (non-numeric)} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    list [catch { base64::encode -maxlen foo $str } msg] $msg
} {1 {expected integer but got "foo"}}
test base64-1.13 {base64::encode with bogus wrap length (non-integer)} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    list [catch { base64::encode -maxlen 1.5 $str } msg] $msg
} {1 {expected integer but got "1.5"}}
test base64-1.14 {base64::encode with wrap length set to 20} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 20 $str
} "VGhlIHNob3J0IHJlZCBm
b3ggcmFuIHF1aWNrbHkg
dGhyb3VnaCB0aGUgZ3Jl
ZW4gZmllbGQgYW5kIGp1
bXBlZCBvdmVyIHRoZSB0
YWxsIGJyb3duIGJlYXIK"
test base64-1.15 {base64::encode with wrap length set to 23 (prime)} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 23 $str
} "VGhlIHNob3J0IHJlZCBmb3g
gcmFuIHF1aWNrbHkgdGhyb3
VnaCB0aGUgZ3JlZW4gZmllb
GQgYW5kIGp1bXBlZCBvdmVy
IHRoZSB0YWxsIGJyb3duIGJ
lYXIK"


test base64-2.1 {base64::decode} {
    base64::decode "dGhpcyBpcyBhIHRlc3QK"
} "this is a test\n"
test base64-2.2 {base64::decode ignores newlines} {
    set str "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3Jl\n"
    append str "ZW4gZmllbGQgYW5kIGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
    base64::decode $str
} "The short red fox ran quickly through the green field and jumped over the tall brown bear\n"
test base64-2.3 {base64::decode handles equal sign padding} {
    # decode the encoding of a string that will be padded in the encoding with
    # one padding char
    base64::decode [base64::encode "01234"]
} "01234"
test base64-2.4 {base64::decode handles equal sign padding} {
    # decode the encoding of a string that will be padded in the encoding with
    # two padding chars
    base64::decode [base64::encode "0123"]
} "0123"


test base64-2.5 {base64::decode} {
    base64::decode ""
} ""
test base64-2.6 {base64::decode} {
    base64::decode " "
} ""

test base64-3.1 {base64 identity test} {
	base64::decode [base64::encode "this is a test"]
} "this is a test"
test base64-3.2 {base64 identity test} {
    # This test fails on version 1.5 because of the format %04x bug
    # when handling the last characters
    set x \f\xee
    set y [base64::decode [base64::encode $x]]
    string compare $x $y
} 0

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

test base64-4.0 {base64 -- sf bug 2976290} {!tcl86 !trf} {
    list [catch {
	::base64::decode s=
    } msg] $msg
} {1 {Not enough data to process padding}}

test base64-4.0 {base64 -- sf bug 2976290} {tcl86} {
    list [catch {
	::base64::decode s=
    } msg] [regsub { \(U\+000073\)} $msg {} msg; set msg]
} {1 {invalid base64 character "s" at position 0}}

test base64-4.0 {base64 -- sf bug 2976290} {trf} {
    list [catch {
	::base64::decode s=
    } msg] $msg
} {1 {illegal character found in input}}

test base64-4.1 {base64 -- sf bug 2976290} {!tcl86 !trf} {
    list [catch {
	::base64::decode s
    } msg] $msg
} {1 {Not enough data to process padding}}

test base64-4.1 {base64 -- sf bug 2976290} {tcl86} {
    list [catch {
	::base64::decode s
    } msg] [regsub { \(U\+000073\)} $msg {} msg; set msg]
} {1 {invalid base64 character "s" at position 0}}

test base64-4.1 {base64 -- sf bug 2976290} {trf} {
    list [catch {
	::base64::decode s
    } msg] $msg
} {1 {illegal character found in input}}

# -------------------------------------------------------------------------
## Higher unicode

set text    "\uFFFE\u0000\u0001\u0002"
set encoded "77++AAEC"

test base64-5.0 "Encode \"$text\"" -body {
    ::base64::encode [encoding convertto utf-8 $text]
} -result $encoded ; # {}

test base64-6.0 "Decode \"$encoded\"" -body {
    encoding convertfrom utf-8 [::base64::decode $encoded]
} -result $text ; # {}

unset text encoded

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

testsuiteCleanup
return