File: sasl.test

package info (click to toggle)
tcllib 1.8-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,628 kB
  • ctags: 4,897
  • sloc: tcl: 88,012; sh: 7,856; ansic: 4,174; xml: 1,765; yacc: 753; perl: 84; f90: 84; makefile: 60; python: 33; ruby: 13; php: 11
file content (131 lines) | stat: -rw-r--r-- 4,307 bytes parent folder | download
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
# 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.2 2005/10/05 15:22:10 patthoyts Exp $

# -------------------------------------------------------------------------
# Initialise the test package
#
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 SASL
catch {namespace delete ::SASL}
if {[catch {source [file join [file dirname [info script]] sasl.tcl]} msg]} {
    puts "skipped [file tail [info script]]: $msg"
    return
}

puts "- SASL [package present 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-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 {} {
    list [catch {
        set ctx [SASL::new -mechanism LOGIN \
                     -callback [list SASLCallback 0]]
        SASL::step $ctx "VXNlcm5hbWU6"
        set r1 [SASL::response $ctx]
        SASL::step $ctx "UGFzc3dvcmQ6"
        set r2 [SASL::response $ctx]
        SASL::cleanup $ctx
        list $r1 $r2
    } 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
} [list 1 "unexpected state: CRAM-MD5 has only 1 step"]

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

test SASL-DIGESTMD5-4.0 {} {
    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]


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

::tcltest::cleanupTests

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