1 source [file dirname [info script]]/testing.tcl
7 catch {[socket -ipv6 stream ::1:5000]} res
9 if {[string match "*not supported" $res]} {
12 # Also, if we can't bind an IPv6 socket, don't run IPv6 tests
14 [socket -ipv6 stream.server ::1:5000] close
19 testConstraint ipv6 $ipv6
21 test socket-1.1 {stream} -body {
22 # Let the system choose a port
23 set s [socket stream.server 127.0.0.1:0]
27 set c [socket stream [$s sockname]]
40 test socket-1.2 {dgram - connected} -body {
41 # Let the system choose a port
42 set s [socket dgram.server 127.0.0.1:0]
43 set c [socket dgram [$s sockname]]
46 $c puts -nonewline hello
47 set buf [$s recv 1000]
53 test socket-1.3 {dgram - unconnected} -body {
54 # Let the system choose a port
55 set s [socket dgram.server 127.0.0.1:0]
59 $c sendto hello [$s sockname]
60 set buf [$s recv 1000]
66 test socket-1.4 {unix} -body {
67 set path [file tempfile]
69 set s [socket unix.server $path]
73 set c [socket unix [$s sockname]]
86 test socket-1.5 {unix.dgram} -body {
87 set path [file tempfile]
89 set s [socket unix.dgram.server $path]
90 set c [socket unix.dgram [$s sockname]]
93 $c puts -nonewline hello
94 set buf [$s recv 1000]
100 test socket-1.6 {pipe} -body {
101 lassign [socket pipe] r w
103 if {[os.fork] == 0} {
115 test socket-1.7 {socketpair} -body {
116 lassign [socket pair] s1 s2
118 if {[os.fork] == 0} {
120 # Read data and send it back
133 test socket-1.8 {stream - ipv6} -constraints ipv6 -body {
134 # Let the system choose a port
135 set s [socket -ipv6 stream.server localhost:0]
137 if {[os.fork] == 0} {
139 set c [socket -ipv6 stream [$s sockname]]
152 test socket-1.9 {dgram - ipv6 - unconnected} -constraints ipv6 -body {
153 # Let the system choose a port
154 set s [socket -ipv6 dgram.server localhost:0]
155 set c [socket -ipv6 dgram]
158 $c sendto hello [$s sockname]
159 set buf [$s recv 1000]
165 test socket-1.10 {stream - port only} -body {
166 set s [socket stream.server 0]
168 if {[os.fork] == 0} {
170 set c [socket stream [$s sockname]]
183 test socket-1.11 {stream - ipv6 - port only} -constraints ipv6 -body {
184 # Let the system choose a port
185 set s [socket -ipv6 stream.server 0]
187 if {[os.fork] == 0} {
189 set c [socket -ipv6 stream [$s sockname]]
202 test socket-2.1 {read 1} -body {
203 lassign [socket pipe] r w
204 $w puts -nonewline hello
216 } -result {h e l l o}
218 test socket-2.2 {read to EOF} -body {
219 lassign [socket pipe] r w
220 $w puts -nonewline hello
227 test socket-2.3 {read -nonewline} -body {
228 lassign [socket pipe] r w
231 set buf [$r read -nonewline]
236 test socket-2.4 {isatty} -body {
237 lassign [socket pipe] r w
238 set result [list [$r isatty] [$w isatty]]
244 test socket-2.5 {peername} -body {
245 set s [socket stream.server 0]
247 if {[os.fork] == 0} {
249 set c [socket stream [$s sockname]]
251 $c puts [list [$c sockname] [$c peername]]
259 lassign [$cs gets] c_sockname c_peername
260 if {$c_sockname ne [$cs peername]} {
261 error "client sockname=$c_sockname not equal to server peername=[$cs peername]"
263 if {$c_peername ne [$cs sockname]} {
264 error "client peername=$c_peername not equal to server sockname=[$cs sockname]"
270 test socket-3.1 {listen} {
271 set s [socket stream.server 0]
276 test socket-3.2 {listen usage} -body {
277 set s [socket stream.server 0]
279 } -returnCodes error -match glob -result {wrong # args: should be "* listen backlog"} -cleanup {
283 test socket-3.3 {listen usage} -body {
284 set s [socket stream.server 0]
286 } -returnCodes error -match glob -result {expected integer but got "blah"} -cleanup {
290 test socket-3.4 {listen not a socket} -body {
291 set f [open [info script]]
293 } -returnCodes error -match regexp -result {^(Socket operation on non-socket|Not a socket)$} -cleanup {
297 test socket-4.1 {invalid ipv6 address} -constraints ipv6 -body {
298 socket -ipv6 stream "- invalid - address -"
299 } -returnCodes error -result {Not a valid address: :::- invalid - address -}
301 test socket-4.2 {invalid ipv4 address} -body {
302 socket stream {9.9.9.9.9:0}
303 } -returnCodes error -result {Not a valid address: 9.9.9.9.9:0}
305 test socket-4.3 {sockname on non-socket} -body {
306 set f [open [info script]]
308 } -returnCodes error -match regexp -result {^(Socket operation on non-socket|Not a socket)$} -cleanup {
312 test socket-4.4 {peername on non-socket} -body {
313 set f [open [info script]]
315 } -returnCodes error -match regexp -result {^(Socket operation on non-socket|Not a socket)$} -cleanup {
319 # For the eventloop tests, let's set up a client and a server where the client
320 # simply echos everything back to the server
322 set s [socket stream.server 0]
323 if {[os.fork] == 0} {
325 set c [socket stream [$s sockname]]
326 # Note: We have to disable buffering here, otherwise
327 # when we read data in $c readable {} we many leave buffered
328 # data and readable won't retrigger.
332 # when we read we need to also read any pending data,
333 # otherwise readable won't retrigger
335 if {[string length $buf] == 0} {
339 $c puts -nonewline $buf
346 # Now set up the server
347 set cs [$s accept addr]
353 # At this point, $cs is the server connection to the client in the child process
355 test eventloop-1.1 {puts/gets} {
360 test eventloop-1.2 {puts/gets} {
361 $cs puts -nonewline again
368 test sockopt-1.1 {sockopt} -body {
369 lsort [dict keys [$cs sockopt]]
370 } -match glob -result {*tcp_nodelay*}
372 test sockopt-1.2 {sockopt set} {
373 $cs sockopt tcp_nodelay 1
374 dict get [$cs sockopt] tcp_nodelay
377 test sockopt-1.3 {sockopt set invalid} -body {
378 $cs sockopt tcp_nodelay badbool
379 } -returnCodes error -result {expected boolean but got "badbool"}