summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/sasl/sasl.test
blob: 8d6790e101466a9009efe117d5bac4a96efa626c (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
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
# sasl.test - Copyright (C) 2002 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Tests for the Tcllib SASL package
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: sasl.test,v 1.10 2008/01/29 00:51:39 patthoyts Exp $

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

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.2
testsNeedTcltest 1.0

testing {
    useLocal sasl.tcl SASL
}

# -------------------------------------------------------------------------
# Tests
# -------------------------------------------------------------------------

proc SASLCallback {clientblob context command args} {
    upvar #0 $context ctx
    switch -exact -- $command {
        login    { return "" }
        username { return "tester" }
        password { return "secret" }
        realm    { return "tcllib.sourceforge.net" }
        hostname { return [info host] }
        default {
            return -code error "oops: client needs to write $command"
        }
    }
}

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

test SASL-1.0 {Check mechanisms preference sorting} {
    list [catch {
        set M $::SASL::mechanisms
        set ::SASL::mechanisms {}
        SASL::register TEST-1 10 client server
        SASL::register TEST-3 100 client
        SASL::register TEST-2 50 client
        set r [SASL::mechanisms]
        set ::SASL::mechanisms $M
        set r
    } res] $res
} [list 0 [list TEST-3 TEST-2 TEST-1]]

test SASL-1.1 {Check mechanisms type parameter} {
    list [catch {
        set M $::SASL::mechanisms
        set ::SASL::mechanisms {}
        SASL::register TEST-1 10 client server
        SASL::register TEST-3 100 client
        SASL::register TEST-2 50 client
        set r [list [SASL::mechanisms client] [SASL::mechanisms server]]
        set ::SASL::mechanisms $M
        set r
    } res] $res
} [list 0 [list [list TEST-3 TEST-2 TEST-1] [list TEST-1]]]

test SASL-1.2 {Check mechanisms preference minimum} {
    list [catch {
        set M $::SASL::mechanisms
        set ::SASL::mechanisms {}
        SASL::register TEST-1 10 client server
        SASL::register TEST-3 100 client
        SASL::register TEST-2 50 client
        set r [list [SASL::mechanisms client 50] [SASL::mechanisms client 80]]
        set ::SASL::mechanisms $M
        set r
    } res] $res
} [list 0 [list [list TEST-3 TEST-2] [list TEST-3]]]

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

test SASL-PLAIN-1.0 {} {
    list [catch {
        set ctx [SASL::new -mechanism PLAIN \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx ""
        set r [SASL::response $ctx]
        SASL::cleanup $ctx
        set r
    } res] $res
} [list 0 "\x00tester\x00secret"]

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

test SASL-LOGIN-2.0 {Check basic LOGIN operation} {
    list [catch {
        set r {}
        set ctx [SASL::new -mechanism LOGIN \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "VXNlcm5hbWU6"
        lappend r [SASL::response $ctx]
        SASL::step $ctx "UGFzc3dvcmQ6"
        lappend r [SASL::response $ctx]
        SASL::cleanup $ctx
        set r
    } res] $res
} [list 0 [list tester secret]]

test SASL-LOGIN-2.1 {Check initial NULL challenge is ignored.} {
    list [catch {
        set r {}
        set ctx [SASL::new -mechanism LOGIN \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx ""
        lappend r [SASL::response $ctx]
        SASL::step $ctx "VXNlcm5hbWU6"
        lappend r [SASL::response $ctx]
        SASL::step $ctx "UGFzc3dvcmQ6"
        lappend r [SASL::response $ctx]
        SASL::cleanup $ctx
        set r
    } res] $res
} [list 0 [list {} tester secret]]

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

test SASL-CRAMMD5-3.0 {} {
    list [catch {
        set ctx [SASL::new -mechanism CRAM-MD5 \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "<1234.987654321@tcllib.sourceforge.net>"
        set r [SASL::response $ctx]
        SASL::cleanup $ctx
        set r
    } res] $res
} [list 0 [list tester c7e3043702b782d70716bd1e21d6e2f7]]

test SASL-CRAMMD5-3.1 {} {
    list [catch {
        set ctx [SASL::new -mechanism CRAM-MD5 \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx ""
        set r1 [SASL::response $ctx]
        SASL::step $ctx ""
        set r2 [SASL::response $ctx]
        SASL::cleanup $ctx
        list $r1 $r2
    } res] $res
} {0 {{} {}}}

test SASL-CRAMMD5-3.2 {} {
    list [catch {
        set ctx [SASL::new -mechanism CRAM-MD5 \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "<1234.987654321@tcllib.sourceforge.net>"
        set r [SASL::response $ctx]
        SASL::step $ctx ""
        set r2 [SASL::response $ctx]
        SASL::cleanup $ctx
        list $r $r2
    } res] $res
} [list 1 "unexpected state: CRAM-MD5 has only 1 step"]

test SASL-CRAMMD5-3.3 {} {
    list [catch {
        set ctx [SASL::new -mechanism CRAM-MD5 \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "<1234.987654321@tcllib.sourceforge.net>"
        set r1 [SASL::response $ctx]
        SASL::step $ctx ""
        set r2 [SASL::response $ctx]
        SASL::cleanup $ctx
        list $r1 $r2
    } res] $res
} [list 1 "unexpected state: CRAM-MD5 has only 1 step"]

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

test SASL-DIGESTMD5-4.0 {Basic check of DIGEST-MD5 operation} {
    list [catch {
        set ctx [SASL::new -mechanism DIGEST-MD5 \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "nonce=\"0123456789\",realm=\"tcllib.sourceforge.net\""
        set r [split [SASL::response $ctx] ,]
        SASL::cleanup $ctx
        foreach thing $r { 
            set x [split $thing =]
            set R([lindex $x 0]) [lindex [lindex $x 1] 0]
        }
        set A1 [SASL::md5_bin "tester:tcllib.sourceforge.net:secret"]
        set A2 "AUTHENTICATE:smtp/tcllib.sourceforge.net"
        set A3 [SASL::md5_hex "$A1:$R(nonce):$R(cnonce)"]
        set A4 [SASL::md5_hex $A2]
        set r [SASL::md5_hex "$A3:0123456789:$R(nc):$R(cnonce):auth:$A4"]
        string compare $r $R(response)
    } res] $res
} [list 0 0]

test SASL-DIGESTMD5-4.1 {Check initial empty challenge is accepted.} {
    list [catch {
        set ctx [SASL::new -mechanism DIGEST-MD5 \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx ""
        SASL::step $ctx "nonce=\"0123456789\",realm=\"tcllib.sourceforge.net\""
        set r [split [SASL::response $ctx] ,]
        SASL::cleanup $ctx
        foreach thing $r { 
            set x [split $thing =]
            set R([lindex $x 0]) [lindex [lindex $x 1] 0]
        }
        set A1 [SASL::md5_bin "tester:tcllib.sourceforge.net:secret"]
        set A2 "AUTHENTICATE:smtp/tcllib.sourceforge.net"
        set A3 [SASL::md5_hex "$A1:$R(nonce):$R(cnonce)"]
        set A4 [SASL::md5_hex $A2]
        set r [SASL::md5_hex "$A3:0123456789:$R(nc):$R(cnonce):auth:$A4"]
        string compare $r $R(response)
    } res] $res
} [list 0 0]

test SASL-DIGESTMD5-4.2 "bug #1412021: ensure service used correctly" {
    list [catch {
        set service xmpp
        set ctx [SASL::new -mechanism DIGEST-MD5 -service $service \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "nonce=\"0123456789\",realm=\"tcllib.sourceforge.net\""
        set r [split [SASL::response $ctx] ,]
        SASL::cleanup $ctx
        foreach thing $r { 
            set x [split $thing =]
            set R([lindex $x 0]) [lindex [lindex $x 1] 0]
        }
        set A1 [SASL::md5_bin "tester:tcllib.sourceforge.net:secret"]
        set A2 "AUTHENTICATE:$service/tcllib.sourceforge.net"
        set A3 [SASL::md5_hex "$A1:$R(nonce):$R(cnonce)"]
        set A4 [SASL::md5_hex $A2]
        set r [SASL::md5_hex "$A3:0123456789:$R(nc):$R(cnonce):auth:$A4"]
        string compare $r $R(response)
    } res] $res
} [list 0 0]

test SASL-DIGESTMD5-4.3 "check for support of charset parameter" {
    list [catch {
        set service xmpp
        set ctx [SASL::new -mechanism DIGEST-MD5 -service $service \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "nonce=\"0123456789\",realm=\"tcllib.sourceforge.net\",charset=utf-8"
        array set p [SASL::DigestParameters [SASL::response $ctx]]
        SASL::cleanup $ctx
        info exists p(charset)
    } res] $res
} [list 0 1]
unset p ;# TODO convert to tcltest 2

test SASL-DIGESTMD5-4.4 "check parsing of spaces in params" {
    list [catch {
        set service xmpp
        set ctx [SASL::new -mechanism DIGEST-MD5 -service $service \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "nonce=\"0123456789\", realm=\"tcllib.sourceforge.net\", charset=utf-8"
        set r {}
        foreach {k v} [SASL::DigestParameters [SASL::response $ctx]] { lappend r $k }
        SASL::cleanup $ctx
        lsort $r
    } res] $res
} [list 0 {charset cnonce digest-uri nc nonce qop realm response username}]

test SASL-OTP-5.0 {Check basic OTP (otp-md5) operation} {
    list [catch {
        set r {}
        set ctx [SASL::new -mechanism OTP \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx ""
        lappend r [SASL::response $ctx]
        SASL::step $ctx "otp-md5 5 test5 ext"
        lappend r [SASL::response $ctx]
        SASL::cleanup $ctx
        set r
    } res] $res
} [list 0 [list "\x00tester" "word:RIG ACRE TALL CALL OAR NEIL"]]

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

testsuiteCleanup

# Local Variables:
#  mode: tcl
#  indent-tabs-mode: nil
# End: