File: autoproxytls.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 (237 lines) | stat: -rw-r--r-- 7,045 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
source [file join \
            [file dirname [file dirname [file join [pwd] [info script]]]] \
            devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2.0

# Constraints depending on package availability
tcltest::testConstraint have_tls   [expr {![catch {package require tls}]}]
tcltest::testConstraint have_twapi [expr {![catch {package require twapi}]}]

puts "- have_tls   = [expr {![catch {package require tls}]}]"
puts "- have_twapi = [expr {![catch {package require twapi}]}]"

# uri and base64

testing {
    useLocal autoproxy.tcl autoproxy
}

package require tcl::chan::memchan
package require http

# a custom memchan that simulates the connection to the proxy
# the channel records the tls options and the http request info
# these options are later checked in the test suites
oo::class create proxy-simu {
    superclass ::tcl::chan::memchan::implementation

    variable options response

    constructor {args} {
        array set options {
            -tls-command {}
            -http-request {}
            -error {}
            -connect-used 0
        }
        set response "hi there"
        next
    }

    method read {c n} {
        if {[regexp -nocase ^connect $options(-http-request)]} {
            set response "HTTP/1.1 200 Connection established\r\nProxy-Agent: tcllib test proxy\r\n\r\n"
            set options(-connect-used) 1
            set options(-http-request) ""
        }
        set ans $response 
        set response ""
        return $ans
    }

    method write {c newbytes} {
        foreach line [split $newbytes \n] {
            if {[regexp -nocase {^(get|post|connect).*http/} $line]} {
                set options(-http-request) [string trim $line]
            }
            if {[regexp -nocase {^([a-z_-]+):\s+(.*)} $line -> hdr val]} {
                set options(-http-$hdr) [string trim $val]
            }
        }
        set txt [array get options]
        set response "HTTP/1.1 200 OK\r\nContent-Length: [string length $txt]\r\n\r\n$txt"
        return [string length $newbytes]
    }

    method configure {ch opt value} {
        set options($opt) $value
    }

    method cget {ch opt} {
        return $options($opt)
    }

    method cgetall {ch} {
        array get options
    }
}

proc optiondict {arglist} {
    set ans {}
    foreach opt $arglist val [lrange $arglist 1 end] {
        if {[string index $opt 0] eq "-"} {
            lappend ans $opt $val
        }
    }
    return $ans
}

tcltest::customMatch keyMatch keyMatch
proc keyMatch {in out} {
    foreach {k v} $in {
        if {![dict exists $out {*}$k]} {
            puts "missing key '$k'"
            return false
        } elseif {![regexp $v [dict get $out {*}$k]]} {
            puts "bad value for key '$k'\n have >[dict get $out {*}$k]<\n must match $v"
            return false
        }
    }
    return true
}

proc extract-options {dict keys} {
    set ans {}
    foreach k $keys {}
}

if {![catch {package require tls}]} {
    rename socket socket.orig
    proc socket {args} {
        set ans [::chan create {read write} [proxy-simu new $args]]
        fconfigure $ans -socket-command $args
        return $ans
    }
    rename tls::socket tls::socket.orig
    proc tls::socket args {
        set ans [::socket {*}$args]
        fconfigure $ans -tls-command [list tls::socket {*}$args] 
        foreach {opt val} [optiondict $args] {
            fconfigure $ans -tls$opt $val
        }
        return $ans
    }
    rename tls::import tls::import.orig
    proc tls::import {ch args} {
        fconfigure $ch -tls-command [list tls::import {*}$args]
        foreach {opt val} [optiondict $args] {
            fconfigure $ch -tls$opt $val
        }
    }
    rename tls::handshake tls::handshake.orig
    proc tls::handshake {s} {}
    rename tls::status tls::status.orig
    proc tls::status {args} {}
}

# Clear the autoproxy package state for each test
proc packageReset {} {
    array set ::autoproxy::options {
        authProc "" basic "" no_proxy "" proxy_host "" proxy_port ""
        tls_package tls
    }
    package require http
    http::config -proxyhost "" -proxyport ""
}

test autoproxy-tls-1.1.0 "autoproxy::tls_socket (tls) without proxy" -constraints {
    have_tls
} -setup {
    packageReset
} -body {
    http::register https 443 autoproxy::tls_socket
    set tok [http::geturl https://direct-connection.tcllib.test/bla]
    http::data $tok
} -cleanup {
    http::cleanup $tok
} -match keyMatch -result {
    -tls-command ^tls::socket
    -tls-servername direct-connection.tcllib.test
    -http-Host direct-connection.tcllib.test
    -http-request {^GET /bla HTTP/1.1}
    -connect-used 0
    -socket-command direct-connection.tcllib.test
}

test autoproxy-tls-1.1.1 "autoproxy::tls_socket (tls) with proxy" -constraints {
    have_tls
} -setup {
    packageReset
    autoproxy::configure -proxy_host proxy.tcllib.test -proxy_port 4319
} -body {
    http::register https 443 autoproxy::tls_socket
    set tok [http::geturl https://proxied-connection.tcllib.test/bla]
    http::data $tok
} -cleanup {
    http::cleanup $tok
} -match keyMatch -result {
    -tls-command ^tls::import
    -tls-servername proxied-connection.tcllib.test
    -http-Host proxied-connection.tcllib.test
    -http-request {^GET /bla HTTP/1.1}
    -connect-used 1
    -socket-command {proxy.tcllib.test 4319}
}

test autoproxy-tls-1.1.1 "autoproxy::tls_socket (tls) with proxy and http proxy config" -constraints {
    have_tls
} -setup {
    packageReset
    autoproxy::configure -proxy_host proxy.tcllib.test -proxy_port 4319
} -body {
    # this test targets bug 
    #   https://core.tcl-lang.org/tcl/tktview/2d9b31130be2084e206524536978a37c17d2f887
    # if we add a regular http proxy it messes up the GET HTTP header (even though the
    # pure HTTP proxy is not actually used).
    http::config -proxyhost oxymoron.tcllib.test -proxyport 1943
    http::register https 443 autoproxy::tls_socket
    set tok [http::geturl https://proxied-connection.tcllib.test/bla]
    http::data $tok
} -cleanup {
    http::cleanup $tok
} -match keyMatch -result {
    -tls-command ^tls::import
    -tls-servername proxied-connection.tcllib.test
    -http-Host proxied-connection.tcllib.test
    -http-request {^GET /bla HTTP/1.1}
    -connect-used 1
    -socket-command {proxy.tcllib.test 4319}
}

test autoproxy-tls-1.1.3 "autoproxy::socket (no tls) with proxy" -constraints {
    have_tls
} -setup {
    packageReset
    http::config -proxyhost proxy.tcllib.test -proxyport 4319
} -body {
    http::register https 443 autoproxy::tls_socket
    set tok [http::geturl http://proxied-connection.tcllib.test/bla]
    http::data $tok
} -cleanup {
    http::cleanup $tok
} -match keyMatch -result {
    -tls-command ^$
    -http-Host proxied-connection.tcllib.test
    -http-request {^GET http://proxied-connection.tcllib.test/bla HTTP/1.1}
    -connect-used 0
    -socket-command {proxy.tcllib.test 4319}
}

testsuiteCleanup

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