Debian Codegraph @

tests/socket.test

raw
1 # Commands tested in this file: socket.
2 #
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
8 # Copyright (c) 1998-2000 Ajuba Solutions.
9 #
10 # See the file "license.terms" for information on usage and redistribution of
11 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
13 # Running socket tests with a remote server:
14 # ------------------------------------------
15 #
16 # Some tests in socket.test depend on the existence of a remote server to
17 # which they connect. The remote server must be an instance of tcltest and it
18 # must run the script found in the file "remote.tcl" in this directory. You
19 # can start the remote server on any machine reachable from the machine on
20 # which you want to run the socket tests, by issuing:
21 #
22 # tcltest remote.tcl -port 2048 # Or choose another port number.
23 #
24 # If the machine you are running the remote server on has several IP
25 # interfaces, you can choose which interface the server listens on for
26 # connections by specifying the -address command line flag, so:
27 #
28 # tcltest remote.tcl -address your.machine.com
29 #
30 # These options can also be set by environment variables. On Unix, you can
31 # type these commands to the shell from which the remote server is started:
32 #
33 # shell% setenv serverPort 2048
34 # shell% setenv serverAddress your.machine.com
35 #
36 # and subsequently you can start the remote server with:
37 #
38 # tcltest remote.tcl
39 #
40 # to have it listen on port 2048 on the interface your.machine.com.
41 #
42 # When the server starts, it prints out a detailed message containing its
43 # configuration information, and it will block until killed with a Ctrl-C.
44 # Once the remote server exists, you can run the tests in socket.test with the
45 # server by setting two Tcl variables:
46 #
47 # % set remoteServerIP <name or address of machine on which server runs>
48 # % set remoteServerPort 2048
49 #
50 # These variables are also settable from the environment. On Unix, you can:
51 #
52 # shell% setenv remoteServerIP machine.where.server.runs
53 # shell% senetv remoteServerPort 2048
54 #
55 # The preamble of the socket.test file checks to see if the variables are set
56 # either in Tcl or in the environment; if they are, it attempts to connect to
57 # the server. If the connection is successful, the tests using the remote
58 # server will be performed; otherwise, it will attempt to start the remote
59 # server (via exec) on platforms that support this, on the local host,
60 # listening at port 2048. If all fails, a message is printed and the tests
61 # using the remote server are not performed.
62
63 if {"::tcltest" ni [namespace children]} {
64 package require tcltest 2.5
65 namespace import -force ::tcltest::*
66 }
67
68 ::tcltest::loadTestedCommands
69
70 # A bad interaction between socket creation, macOS, and unattended CI
71 # environments make this whole file impractical to run; too many weird hangs.
72 if {[info exists ::env(MAC_CI)]} {
73 return
74 }
75
76 # Some tests require the Thread package or exec command
77 testConstraint thread [expr {0 == [catch {package require Thread 2.7-}]}]
78 testConstraint exec [llength [info commands exec]]
79
80 # Produce a random port number in the Dynamic/Private range
81 # from 49152 through 65535.
82 proc randport {} {
83 # firstly try dynamic port via server-socket(0):
84 set port 0x7fffffff
85 catch {
86 set port [lindex [fconfigure [set s [socket -server {} 0]] -sockname] 2]
87 close $s
88 }
89 while {[catch {
90 close [socket -server {} $port]
91 } msg]} {
92 if {[incr i] > 1000} {return -code error "too many iterations to get free random port: $msg"}
93 # try random port:
94 set port [expr {int(rand()*16383+49152)}]
95 }
96 return $port
97 }
98
99 # Test the latency of tcp connections over the loopback interface. Some OSes
100 # (e.g. NetBSD) seem to use the Nagle algorithm and delayed ACKs, so it takes
101 # up to 200ms for a packet sent to localhost to arrive. We're measuring this
102 # here, so that OSes that don't have this problem can run the tests at full
103 # speed.
104 set server [socket -server {apply {{s a p} {set ::s1 $s}}} 0]
105 set s2 [socket localhost [lindex [fconfigure $server -sockname] 2]]
106 vwait s1; close $server
107 fconfigure $s1 -buffering line
108 fconfigure $s2 -buffering line
109 set t1 [clock milliseconds]
110 puts $s2 test1; gets $s1
111 puts $s2 test2; gets $s1
112 close $s1; close $s2
113 set t2 [clock milliseconds]
114 set lat1 [expr {($t2-$t1)*2}]; # doubled as a safety margin
115
116 # Test the latency of failed connection attempts over the loopback
117 # interface. They can take more than a second under Windows and requires
118 # additional [after]s in some tests that are not needed on systems that fail
119 # immediately.
120 set t1 [clock milliseconds]
121 catch {socket 127.0.0.1 [randport]}
122 set t2 [clock milliseconds]
123 set lat2 [expr {($t2-$t1)*3}]
124
125 # Use the maximum of the two latency calculations, but at least 200ms
126 set latency [expr {$lat1 > $lat2 ? $lat1 : $lat2}]
127 set latency [expr {$latency > 200 ? $latency : 200}]
128 unset t1 t2 s1 s2 lat1 lat2 server
129
130 # If remoteServerIP or remoteServerPort are not set, check in the environment
131 # variables for externally set values.
132 #
133
134 if {![info exists remoteServerIP]} {
135 if {[info exists env(remoteServerIP)]} {
136 set remoteServerIP $env(remoteServerIP)
137 }
138 }
139 if {![info exists remoteServerPort]} {
140 if {[info exists env(remoteServerPort)]} {
141 set remoteServerPort $env(remoteServerPort)
142 } else {
143 if {[info exists remoteServerIP]} {
144 set remoteServerPort 2048
145 }
146 }
147 }
148
149 if 0 {
150 # activate this to time the tests
151 proc test {args} {
152 set name [lindex $args 0]
153 puts "[lindex [time {uplevel [linsert $args 0 tcltest::test]}] 0] @@@ $name"
154 }
155 }
156
157 foreach {af localhost} {
158 inet 127.0.0.1
159 inet6 ::1
160 } {
161 # Check if the family is supported and set the constraint accordingly
162 testConstraint supported_$af [expr {![catch {socket -server foo -myaddr $localhost 0} sock]}]
163 catch {close $sock}
164 }
165
166 set sock [socket -server foo -myaddr localhost 0]
167 set sockname [fconfigure $sock -sockname]
168 close $sock
169 testConstraint localhost_v4 [expr {"127.0.0.1" in $sockname}]
170 testConstraint localhost_v6 [expr {"::1" in $sockname}]
171
172
173 foreach {af localhost} {
174 any 127.0.0.1
175 inet 127.0.0.1
176 inet6 ::1
177 } {
178 if {![testConstraint supported_$af]} {
179 continue
180 }
181 set ::tcl::unsupported::socketAF $af
182 #
183 # Check if we're supposed to do tests against the remote server
184 #
185
186 set doTestsWithRemoteServer 1
187 if {![info exists remoteServerIP]} {
188 set remoteServerIP $localhost
189 }
190 if {($doTestsWithRemoteServer == 1) && (![info exists remoteServerPort])} {
191 set remoteServerPort [randport]
192 }
193
194 # Attempt to connect to a remote server if one is already running. If it is
195 # not running or for some other reason the connect fails, attempt to start the
196 # remote server on the local host listening on port 2048. This is only done on
197 # platforms that support exec (i.e. not on the Mac). On platforms that do not
198 # support exec, the remote server must be started by the user before running
199 # the tests.
200
201 set remoteProcChan ""
202 set commandSocket ""
203 if {$doTestsWithRemoteServer} {
204 catch {close $commandSocket}
205 if {![catch {
206 set commandSocket [socket $remoteServerIP $remoteServerPort]
207 }]} then {
208 fconfigure $commandSocket -translation crlf -buffering line
209 } elseif {![testConstraint exec]} {
210 set noRemoteTestReason "can't exec"
211 set doTestsWithRemoteServer 0
212 } else {
213 set remoteServerIP $localhost
214 # Be *extra* careful in case this file is sourced from
215 # a directory other than the current one...
216 set remoteFile [file join [pwd] [file dirname [info script]] \
217 remote.tcl]
218 if {![catch {
219 set remoteProcChan [open "|[list \
220 [interpreter] $remoteFile -serverIsSilent \
221 -port $remoteServerPort -address $remoteServerIP]" w+]
222 } msg]} then {
223 gets $remoteProcChan
224 if {[catch {
225 set commandSocket [socket $remoteServerIP $remoteServerPort]
226 } msg] == 0} then {
227 fconfigure $commandSocket -translation crlf -buffering line
228 } else {
229 set noRemoteTestReason $msg
230 set doTestsWithRemoteServer 0
231 }
232 } else {
233 set noRemoteTestReason "$msg [interpreter]"
234 set doTestsWithRemoteServer 0
235 }
236 }
237 }
238
239 # Some tests are run only if we are doing testing against a remote server.
240 testConstraint doTestsWithRemoteServer $doTestsWithRemoteServer
241 if {!$doTestsWithRemoteServer} {
242 if {[string first s $::tcltest::verbose] >= 0} {
243 puts "Skipping tests with remote server. See tests/socket.test for"
244 puts "information on how to run remote server."
245 puts "Reason for not doing remote tests: $noRemoteTestReason"
246 }
247 }
248
249 #
250 # If we do the tests, define a command to send a command to the remote server.
251 #
252
253 if {[testConstraint doTestsWithRemoteServer]} {
254 proc sendCommand {c} {
255 global commandSocket
256
257 if {[eof $commandSocket]} {
258 error "remote server disappeared"
259 }
260 if {[catch {puts $commandSocket $c} msg]} {
261 error "remote server disappaered: $msg"
262 }
263 if {[catch {puts $commandSocket "--Marker--Marker--Marker--"} msg]} {
264 error "remote server disappeared: $msg"
265 }
266
267 while {1} {
268 set line [gets $commandSocket]
269 if {[eof $commandSocket]} {
270 error "remote server disappaered"
271 }
272 if {$line eq "--Marker--Marker--Marker--"} {
273 lassign $result code info value
274 return -code $code -errorinfo $info $value
275 }
276 append result $line "\n"
277 }
278 }
279 }
280
281 proc getPort sock {
282 lindex [fconfigure $sock -sockname] 2
283 }
284
285 # Some tests in this file are known to hang *occasionally* on OSX; stop the
286 # worst offenders.
287 testConstraint notOSX [expr {$::tcl_platform(os) ne "Darwin"}]
288 # Here "Windows" means derived platforms as Cygwin or Msys2 too.
289 testConstraint notWindows [expr {![regexp {^(Windows|MSYS|CYGWIN)} $::tcl_platform(os)]}]
290
291 # ----------------------------------------------------------------------
292
293 test socket_$af-1.1 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
294 socket -server
295 } -returnCodes error -result {no argument given for -server option}
296 test socket_$af-1.2 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
297 socket -server foo
298 } -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}
299 test socket_$af-1.3 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
300 socket -myaddr
301 } -returnCodes error -result {no argument given for -myaddr option}
302 test socket_$af-1.4 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
303 socket -myaddr $localhost
304 } -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}
305 test socket_$af-1.5 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
306 socket -myport
307 } -returnCodes error -result {no argument given for -myport option}
308 test socket_$af-1.6 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
309 socket -myport xxxx
310 } -returnCodes error -result {expected integer but got "xxxx"}
311 test socket_$af-1.7 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
312 socket -myport 2522
313 } -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}
314 test socket_$af-1.8 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
315 socket -froboz
316 } -returnCodes error -result {bad option "-froboz": must be -async, -myaddr, -myport, or -server}
317 test socket_$af-1.9 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
318 socket -server foo -myport 2521 3333
319 } -returnCodes error -result {option -myport is not valid for servers}
320 test socket_$af-1.10 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
321 socket host 2528 -junk
322 } -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}
323 test socket_$af-1.11 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
324 socket -server callback 2520 --
325 } -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}
326 test socket_$af-1.12 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
327 socket foo badport
328 } -returnCodes error -result {expected integer but got "badport"}
329 test socket_$af-1.13 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
330 socket -async -server
331 } -returnCodes error -result {cannot set -async option for server sockets}
332 test socket_$af-1.14 {arg parsing for socket command} -constraints [list socket supported_$af] -body {
333 socket -server foo -async
334 } -returnCodes error -result {cannot set -async option for server sockets}
335
336 set path(script) [makeFile {} script]
337
338 test socket_$af-2.1 {tcp connection} -constraints [list socket supported_$af stdio] -setup {
339 file delete $path(script)
340 set f [open $path(script) w]
341 puts $f {
342 set timer [after 10000 "set x timed_out"]
343 set f [socket -server accept 0]
344 proc accept {file addr port} {
345 global x
346 set x done
347 close $file
348 }
349 puts ready
350 puts [lindex [fconfigure $f -sockname] 2]
351 vwait x
352 after cancel $timer
353 close $f
354 puts $x
355 }
356 close $f
357 set f [open "|[list [interpreter] $path(script)]" r]
358 gets $f x
359 gets $f listen
360 } -body {
361 # $x == "ready" at this point
362 set sock [socket $localhost $listen]
363 lappend x [gets $f]
364 close $sock
365 lappend x [gets $f]
366 } -cleanup {
367 close $f
368 } -result {ready done {}}
369 test socket_$af-2.2 {tcp connection with client port specified} -setup {
370 set port [randport]
371 file delete $path(script)
372 set f [open $path(script) w]
373 puts $f {
374 set timer [after 10000 "set x timeout"]
375 set f [socket -server accept 0]
376 proc accept {file addr port} {
377 global x
378 puts "[gets $file] $port"
379 close $file
380 set x done
381 }
382 puts ready
383 puts [lindex [fconfigure $f -sockname] 2]
384 vwait x
385 after cancel $timer
386 close $f
387 }
388 close $f
389 set f [open "|[list [interpreter] $path(script)]" r]
390 gets $f x
391 gets $f listen
392 } -constraints [list socket supported_$af stdio] -body {
393 # $x == "ready" at this point
394 set sock [socket -myport $port $localhost $listen]
395 puts $sock hello
396 flush $sock
397 lappend x [expr {[gets $f] eq "hello $port"}]
398 close $sock
399 return $x
400 } -cleanup {
401 catch {close [socket $localhost $listen]}
402 close $f
403 } -result {ready 1}
404 test socket_$af-2.3 {tcp connection with client interface specified} -setup {
405 file delete $path(script)
406 set f [open $path(script) w]
407 puts $f {
408 set timer [after 2000 "set x done"]
409 set f [socket -server accept 0]
410 proc accept {file addr port} {
411 global x
412 puts "[gets $file] $addr"
413 close $file
414 set x done
415 }
416 puts [lindex [fconfigure $f -sockname] 2]
417 puts ready
418 vwait x
419 after cancel $timer
420 close $f
421 }
422 close $f
423 set f [open "|[list [interpreter] $path(script)]" r]
424 gets $f listen
425 gets $f x
426 } -constraints [list socket supported_$af stdio] -body {
427 # $x == "ready" at this point
428 set sock [socket -myaddr $localhost $localhost $listen]
429 puts $sock hello
430 flush $sock
431 lappend x [gets $f]
432 close $sock
433 return $x
434 } -cleanup {
435 close $f
436 } -result [list ready [list hello $localhost]]
437 test socket_$af-2.4 {tcp connection with server interface specified} -setup {
438 file delete $path(script)
439 set f [open $path(script) w]
440 puts $f [list set localhost $localhost]
441 puts $f {
442 set timer [after 2000 "set x done"]
443 set f [socket -server accept -myaddr $localhost 0]
444 proc accept {file addr port} {
445 global x
446 puts "[gets $file]"
447 close $file
448 set x done
449 }
450 puts ready
451 puts [lindex [fconfigure $f -sockname] 2]
452 vwait x
453 after cancel $timer
454 close $f
455 }
456 close $f
457 set f [open "|[list [interpreter] $path(script)]" r]
458 gets $f x
459 gets $f listen
460 } -constraints [list socket supported_$af stdio] -body {
461 # $x == "ready" at this point
462 set sock [socket $localhost $listen]
463 puts $sock hello
464 flush $sock
465 lappend x [gets $f]
466 close $sock
467 return $x
468 } -cleanup {
469 close $f
470 } -result {ready hello}
471 test socket_$af-2.5 {tcp connection with redundant server port} -setup {
472 file delete $path(script)
473 set f [open $path(script) w]
474 puts $f {
475 set timer [after 10000 "set x timeout"]
476 set f [socket -server accept 0]
477 proc accept {file addr port} {
478 global x
479 puts "[gets $file]"
480 close $file
481 set x done
482 }
483 puts ready
484 puts [lindex [fconfigure $f -sockname] 2]
485 vwait x
486 after cancel $timer
487 close $f
488 }
489 close $f
490 set f [open "|[list [interpreter] $path(script)]" r]
491 gets $f x
492 gets $f listen
493 } -constraints [list socket supported_$af stdio] -body {
494 # $x == "ready" at this point
495 set sock [socket $localhost $listen]
496 puts $sock hello
497 flush $sock
498 lappend x [gets $f]
499 close $sock
500 return $x
501 } -cleanup {
502 close $f
503 } -result {ready hello}
504 test socket_$af-2.6 {tcp connection} -constraints [list socket supported_$af] -body {
505 set status ok
506 if {![catch {set sock [socket $localhost [randport]]}]} {
507 if {![catch {gets $sock}]} {
508 set status broken
509 }
510 close $sock
511 }
512 set status
513 } -result ok
514 test socket_$af-2.7 {echo server, one line} -constraints [list socket supported_$af stdio] -setup {
515 file delete $path(script)
516 set f [open $path(script) w]
517 puts $f {
518 set timer [after 10000 "set x timeout"]
519 set f [socket -server accept 0]
520 proc accept {s a p} {
521 fileevent $s readable [list echo $s]
522 fconfigure $s -translation lf -buffering line
523 }
524 proc echo {s} {
525 set l [gets $s]
526 if {[eof $s]} {
527 global x
528 close $s
529 set x done
530 } else {
531 puts $s $l
532 }
533 }
534 puts ready
535 puts [lindex [fconfigure $f -sockname] 2]
536 vwait x
537 after cancel $timer
538 close $f
539 puts $x
540 }
541 close $f
542 set f [open "|[list [interpreter] $path(script)]" r]
543 gets $f
544 gets $f listen
545 } -body {
546 set s [socket $localhost $listen]
547 fconfigure $s -buffering line -translation lf
548 puts $s "hello abcdefghijklmnop"
549 set x [gets $s]
550 close $s
551 list $x [gets $f]
552 } -cleanup {
553 close $f
554 } -result {{hello abcdefghijklmnop} done}
555 removeFile script
556 test socket_$af-2.8 {echo server, loop 50 times, single connection} -setup {
557 set path(script) [makeFile {
558 set f [socket -server accept 0]
559 proc accept {s a p} {
560 fileevent $s readable [list echo $s]
561 fconfigure $s -buffering line
562 }
563 proc echo {s} {
564 global i
565 set l [gets $s]
566 if {[eof $s]} {
567 global x
568 close $s
569 set x done
570 } else {
571 incr i
572 puts $s $l
573 }
574 }
575 set i 0
576 puts ready
577 puts [lindex [fconfigure $f -sockname] 2]
578 set timer [after 20000 "set x done"]
579 vwait x
580 after cancel $timer
581 close $f
582 puts "done $i"
583 } script]
584 set f [open "|[list [interpreter] $path(script)]" r]
585 gets $f
586 gets $f listen
587 } -constraints [list socket supported_$af stdio] -body {
588 set s [socket $localhost $listen]
589 fconfigure $s -buffering line
590 catch {
591 for {set x 0} {$x < 50} {incr x} {
592 puts $s "hello abcdefghijklmnop"
593 gets $s
594 }
595 }
596 close $s
597 catch {set x [gets $f]}
598 return $x
599 } -cleanup {
600 close $f
601 removeFile script
602 } -result {done 50}
603 set path(script) [makeFile {} script]
604 test socket_$af-2.9 {socket conflict} -constraints [list socket supported_$af stdio] -body {
605 set s [socket -server accept 0]
606 file delete $path(script)
607 set f [open $path(script) w]
608 puts $f [list set ::tcl::unsupported::socketAF $::tcl::unsupported::socketAF]
609 puts $f "socket -server accept [lindex [fconfigure $s -sockname] 2]"
610 close $f
611 set f [open "|[list [interpreter] $path(script)]" r]
612 gets $f
613 after 100
614 close $f
615 } -returnCodes error -cleanup {
616 close $s
617 } -match glob -result {couldn't open socket: address already in use*}
618 test socket_$af-2.10 {close on accept, accepted socket lives} -setup {
619 set done 0
620 set timer [after 20000 "set done timed_out"]
621 } -constraints [list socket supported_$af] -body {
622 set ss [socket -server accept 0]
623 proc accept {s a p} {
624 global ss
625 close $ss
626 fileevent $s readable "readit $s"
627 fconfigure $s -trans lf
628 }
629 proc readit {s} {
630 global done
631 gets $s
632 close $s
633 set done 1
634 }
635 set cs [socket $localhost [lindex [fconfigure $ss -sockname] 2]]
636 puts $cs hello
637 close $cs
638 vwait done
639 return $done
640 } -cleanup {
641 after cancel $timer
642 } -result 1
643 test socket_$af-2.11 {detecting new data} -constraints [list socket supported_$af] -setup {
644 proc accept {s a p} {
645 global sock
646 set sock $s
647 }
648 set s [socket -server accept 0]
649 set sock ""
650 } -body {
651 set s2 [socket $localhost [lindex [fconfigure $s -sockname] 2]]
652 vwait sock
653 puts $s2 one
654 flush $s2
655 after $latency {set x 1}; # Spurious failures in Travis CI, if we do [after idle]
656 vwait x
657 fconfigure $sock -blocking 0
658 set result a:[gets $sock]
659 lappend result b:[gets $sock]
660 fconfigure $sock -blocking 1
661 puts $s2 two
662 flush $s2
663 after $latency {set x 1}; # NetBSD fails here if we do [after idle]
664 vwait x
665 fconfigure $sock -blocking 0
666 lappend result c:[gets $sock]
667 } -cleanup {
668 fconfigure $sock -blocking 1
669 close $s2
670 close $s
671 close $sock
672 } -result {a:one b: c:two}
673 test socket_$af-2.12 {} [list socket stdio supported_$af] {
674 file delete $path(script)
675 set f [open $path(script) w]
676 puts $f {
677 set server [socket -server accept_client 0]
678 puts [lindex [chan configure $server -sockname] 2]
679 proc accept_client { client host port } {
680 chan configure $client -blocking 0 -buffering line
681 write_line $client
682 }
683 proc write_line client {
684 if { [catch { chan puts $client [string repeat . 720000]}] } {
685 puts [catch {chan close $client}]
686 } else {
687 puts signal1
688 after 0 write_line $client
689 }
690 }
691 chan event stdin readable {set forever now}
692 vwait forever
693 exit
694 }
695 close $f
696 set f [open "|[list [interpreter] $path(script)]" r+]
697 gets $f port
698 set sock [socket $localhost $port]
699 chan event $sock readable [list read_lines $sock $f]
700 proc read_lines { sock pipe } {
701 gets $pipe
702 chan close $sock
703 chan event $pipe readable [list readpipe $pipe]
704 }
705 proc readpipe {pipe} {
706 while {![string is integer [set ::done [gets $pipe]]]} {}
707 }
708 vwait ::done
709 close $f
710 set ::done
711 } 0
712 test socket_$af-2.13 {Bug 1758a0b603} {socket stdio} {
713 file delete $path(script)
714 set f [open $path(script) w]
715 puts $f {
716 set server [socket -server accept 0]
717 puts [lindex [chan configure $server -sockname] 2]
718 proc accept { client host port } {
719 chan configure $client -blocking 0 -buffering line -buffersize 1
720 puts $client [string repeat . 720000]
721 puts ready
722 chan event $client writable [list setup $client]
723 }
724 proc setup client {
725 chan event $client writable {set forever write}
726 after 5 {set forever timeout}
727 }
728 vwait forever
729 puts $forever
730 }
731 close $f
732 set pipe [open |[list [interpreter] $path(script)] r]
733 gets $pipe port
734 set sock [socket $localhost $port]
735 chan configure $sock -blocking 0 -buffering line
736 chan event $sock readable [list read_lines $sock $pipe ]
737 proc read_lines { sock pipe } {
738 gets $pipe
739 gets $sock line
740 after idle [list stop $sock $pipe]
741 chan event $sock readable {}
742 }
743 proc stop {sock pipe} {
744 variable done
745 close $sock
746 set done [gets $pipe]
747 }
748 variable done
749 vwait [namespace which -variable done]
750 close $pipe
751 set done
752 } write
753
754 test socket_$af-3.1 {socket conflict} -constraints [list socket supported_$af stdio] -setup {
755 file delete $path(script)
756 set f [open $path(script) w]
757 puts $f [list set localhost $localhost]
758 puts $f {
759 set f [socket -server accept -myaddr $localhost 0]
760 puts ready
761 puts [lindex [fconfigure $f -sockname] 2]
762 gets stdin
763 close $f
764 }
765 close $f
766 set f [open "|[list [interpreter] $path(script)]" r+]
767 gets $f
768 gets $f listen
769 } -body {
770 socket -server accept -myaddr $localhost $listen
771 } -cleanup {
772 puts $f bye
773 close $f
774 } -returnCodes error -result {couldn't open socket: address already in use}
775 test socket_$af-3.2 {server with several clients} -setup {
776 file delete $path(script)
777 set f [open $path(script) w]
778 puts $f [list set localhost $localhost]
779 puts $f {
780 set t1 [after 30000 "set x timed_out"]
781 set t2 [after 31000 "set x timed_out"]
782 set t3 [after 32000 "set x timed_out"]
783 set counter 0
784 set s [socket -server accept -myaddr $localhost 0]
785 proc accept {s a p} {
786 fileevent $s readable [list echo $s]
787 fconfigure $s -buffering line
788 }
789 proc echo {s} {
790 global x
791 set l [gets $s]
792 if {[eof $s]} {
793 close $s
794 set x done
795 } else {
796 puts $s $l
797 }
798 }
799 puts ready
800 puts [lindex [fconfigure $s -sockname] 2]
801 vwait x
802 after cancel $t1
803 vwait x
804 after cancel $t2
805 vwait x
806 after cancel $t3
807 close $s
808 puts $x
809 }
810 close $f
811 set f [open "|[list [interpreter] $path(script)]" r+]
812 set x [gets $f]
813 gets $f listen
814 } -constraints [list socket supported_$af stdio] -body {
815 # $x == "ready" here
816 set s1 [socket $localhost $listen]
817 fconfigure $s1 -buffering line
818 set s2 [socket $localhost $listen]
819 fconfigure $s2 -buffering line
820 set s3 [socket $localhost $listen]
821 fconfigure $s3 -buffering line
822 for {set i 0} {$i < 100} {incr i} {
823 puts $s1 hello,s1
824 gets $s1
825 puts $s2 hello,s2
826 gets $s2
827 puts $s3 hello,s3
828 gets $s3
829 }
830 close $s1
831 close $s2
832 close $s3
833 lappend x [gets $f]
834 } -cleanup {
835 close $f
836 } -result {ready done}
837
838 test socket_$af-4.1 {server with several clients} -setup {
839 file delete $path(script)
840 set f [open $path(script) w]
841 puts $f [list set localhost $localhost]
842 puts $f {
843 set port [gets stdin]
844 set s [socket $localhost $port]
845 fconfigure $s -buffering line
846 for {set i 0} {$i < 100} {incr i} {
847 puts $s hello
848 gets $s
849 }
850 close $s
851 puts bye
852 gets stdin
853 }
854 close $f
855 set p1 [open "|[list [interpreter] $path(script)]" r+]
856 fconfigure $p1 -buffering line
857 set p2 [open "|[list [interpreter] $path(script)]" r+]
858 fconfigure $p2 -buffering line
859 set p3 [open "|[list [interpreter] $path(script)]" r+]
860 fconfigure $p3 -buffering line
861 } -constraints [list socket supported_$af stdio] -body {
862 proc accept {s a p} {
863 fconfigure $s -buffering line
864 fileevent $s readable [list echo $s]
865 }
866 proc echo {s} {
867 global x
868 set l [gets $s]
869 if {[eof $s]} {
870 close $s
871 set x done
872 } else {
873 puts $s $l
874 }
875 }
876 set t1 [after 30000 "set x timed_out"]
877 set t2 [after 31000 "set x timed_out"]
878 set t3 [after 32000 "set x timed_out"]
879 set s [socket -server accept -myaddr $localhost 0]
880 set listen [lindex [fconfigure $s -sockname] 2]
881 puts $p1 $listen
882 puts $p2 $listen
883 puts $p3 $listen
884 vwait x
885 vwait x
886 vwait x
887 after cancel $t1
888 after cancel $t2
889 after cancel $t3
890 close $s
891 set l ""
892 lappend l [list p1 [gets $p1] $x]
893 lappend l [list p2 [gets $p2] $x]
894 lappend l [list p3 [gets $p3] $x]
895 } -cleanup {
896 puts $p1 bye
897 puts $p2 bye
898 puts $p3 bye
899 close $p1
900 close $p2
901 close $p3
902 } -result {{p1 bye done} {p2 bye done} {p3 bye done}}
903 test socket_$af-4.2 {byte order problems, socket numbers, htons} -body {
904 close [socket -server dodo -myaddr $localhost 0x3000]
905 return ok
906 } -constraints [list socket supported_$af] -result ok
907
908 test socket_$af-5.1 {byte order problems, socket numbers, htons} -body {
909 if {![catch {socket -server dodo 0x1} msg]} {
910 close $msg
911 return {htons problem, should be disallowed, are you running as SU?}
912 }
913 return {couldn't open socket: not owner}
914 } -constraints [list socket supported_$af unix notRoot notOSX notWindows] -result {couldn't open socket: not owner}
915 test socket_$af-5.2 {byte order problems, socket numbers, htons} -body {
916 if {![catch {socket -server dodo 0x10000} msg]} {
917 close $msg
918 return {port resolution problem, should be disallowed}
919 }
920 return {couldn't open socket: port number too high}
921 } -constraints [list socket supported_$af] -result {couldn't open socket: port number too high}
922 test socket_$af-5.3 {byte order problems, socket numbers, htons} -body {
923 if {![catch {socket -server dodo 21} msg]} {
924 close $msg
925 return {htons problem, should be disallowed, are you running as SU?}
926 }
927 return {couldn't open socket: not owner}
928 } -constraints [list socket supported_$af unix notRoot notOSX notWindows] -result {couldn't open socket: not owner}
929
930 test socket_$af-6.1 {accept callback error} -constraints [list socket supported_$af stdio] -setup {
931 proc myHandler {msg options} {
932 variable x $msg
933 }
934 set handler [interp bgerror {}]
935 interp bgerror {} [namespace which myHandler]
936 file delete $path(script)
937 } -body {
938 set f [open $path(script) w]
939 puts $f [list set localhost $localhost]
940 puts $f {
941 gets stdin port
942 socket $localhost $port
943 }
944 close $f
945 set f [open "|[list [interpreter] $path(script)]" r+]
946 proc accept {s a p} {expr {10 / 0}}
947 set s [socket -server accept -myaddr $localhost 0]
948 puts $f [lindex [fconfigure $s -sockname] 2]
949 close $f
950 set timer [after 10000 "set x timed_out"]
951 vwait x
952 after cancel $timer
953 close $s
954 return $x
955 } -cleanup {
956 interp bgerror {} $handler
957 } -result {divide by zero}
958
959 test socket_$af-6.2 {
960 readable fileevent on server socket
961 } -setup {
962 set sock [socket -server dummy 0]
963 } -constraints [list socket supported_$af] -body {
964 fileevent $sock readable dummy
965 } -cleanup {
966 close $sock
967 } -returnCodes 1 -result "channel is not readable"
968
969 test socket_$af-6.3 {writable fileevent on server socket} -setup {
970 set sock [socket -server dummy 0]
971 } -constraints [list socket supported_$af] -body {
972 fileevent $sock writable dummy
973 } -cleanup {
974 close $sock
975 } -returnCodes 1 -result "channel is not writable"
976
977 test socket_$af-7.1 {testing socket specific options} -setup {
978 file delete $path(script)
979 set f [open $path(script) w]
980 puts $f {
981 set ss [socket -server accept 0]
982 proc accept args {
983 global x
984 set x done
985 }
986 puts ready
987 puts [lindex [fconfigure $ss -sockname] 2]
988 set timer [after 10000 "set x timed_out"]
989 vwait x
990 after cancel $timer
991 }
992 close $f
993 set f [open "|[list [interpreter] $path(script)]" r]
994 gets $f
995 gets $f listen
996 set l ""
997 } -constraints [list socket supported_$af stdio] -body {
998 set s [socket $localhost $listen]
999 set p [fconfigure $s -peername]
1000 close $s
1001 lappend l [string compare [lindex $p 0] $localhost]
1002 lappend l [string compare [lindex $p 2] $listen]
1003 lappend l [llength $p]
1004 } -cleanup {
1005 close $f
1006 } -result {0 0 3}
1007 test socket_$af-7.2 {testing socket specific options} -setup {
1008 file delete $path(script)
1009 set f [open $path(script) w]
1010 puts $f [list set ::tcl::unsupported::socketAF $::tcl::unsupported::socketAF]
1011 puts $f {
1012 set ss [socket -server accept 0]
1013 proc accept args {
1014 global x
1015 set x done
1016 }
1017 puts ready
1018 puts [lindex [fconfigure $ss -sockname] 2]
1019 set timer [after 10000 "set x timed_out"]
1020 vwait x
1021 after cancel $timer
1022 }
1023 close $f
1024 set f [open "|[list [interpreter] $path(script)]" r]
1025 gets $f
1026 gets $f listen
1027 } -constraints [list socket supported_$af stdio] -body {
1028 set s [socket $localhost $listen]
1029 set p [fconfigure $s -sockname]
1030 close $s
1031 list [llength $p] \
1032 [regexp {^(127\.0\.0\.1|0\.0\.0\.0|::1)$} [lindex $p 0]] \
1033 [expr {[lindex $p 2] == $listen}]
1034 } -cleanup {
1035 close $f
1036 } -result {3 1 0}
1037 test socket_$af-7.3 {testing socket specific options} -constraints [list socket supported_$af] -body {
1038 set s [socket -server accept -myaddr $localhost 0]
1039 set l [fconfigure $s]
1040 close $s
1041 update
1042 llength $l
1043 } -result 14
1044 test socket_$af-7.4 {testing socket specific options} -constraints [list socket supported_$af] -setup {
1045 set timer [after 10000 "set x timed_out"]
1046 set l ""
1047 } -body {
1048 set s [socket -server accept -myaddr $localhost 0]
1049 proc accept {s a p} {
1050 global x
1051 set x [fconfigure $s -sockname]
1052 close $s
1053 }
1054 set listen [lindex [fconfigure $s -sockname] 2]
1055 set s1 [socket $localhost $listen]
1056 vwait x
1057 lappend l [expr {[lindex $x 2] == $listen}] [llength $x]
1058 } -cleanup {
1059 after cancel $timer
1060 close $s
1061 close $s1
1062 } -result {1 3}
1063 test socket_$af-7.5 {testing socket specific options} -setup {
1064 set timer [after 10000 "set x timed_out"]
1065 set l ""
1066 } -constraints [list socket supported_$af unixOrWin] -body {
1067 set s [socket -server accept 0]
1068 proc accept {s a p} {
1069 global x
1070 set x [fconfigure $s -sockname]
1071 close $s
1072 }
1073 set listen [lindex [fconfigure $s -sockname] 2]
1074 set s1 [socket $localhost $listen]
1075 vwait x
1076 lappend l [lindex $x 0] [expr {[lindex $x 2] == $listen}] [llength $x]
1077 } -cleanup {
1078 after cancel $timer
1079 close $s
1080 close $s1
1081 } -result [list $localhost 1 3]
1082 test socket_$af-7.6 {testing socket specific options - bug e589d9bdab} -setup {
1083 set timer [after 10000 "set x timed_out"]
1084 set l ""
1085 } -constraints [list socket supported_$af unixOrWin] -body {
1086 set s [socket -server accept 0]
1087 proc accept {s a p} {
1088 global x
1089 set x [fconfigure $s -sockname]
1090 close $s
1091 }
1092 set listen [lindex [fconfigure $s -sockname] 2]
1093 set s1 [socket $localhost $listen]
1094 vwait x
1095 lsort [dict keys [fconfigure $s1]]
1096 } -cleanup {
1097 after cancel $timer
1098 close $s
1099 close $s1
1100 } -result {-blocking -buffering -buffersize -encoding -eofchar -peername -sockname -translation}
1101
1102 test socket_$af-8.1 {testing -async flag on sockets} -constraints [list socket supported_$af] -body {
1103 # NOTE: This test may fail on some Solaris 2.4 systems. If it does, check
1104 # that you have these patches installed (using showrev -p):
1105 #
1106 # 101907-05, 101925-02, 101945-14, 101959-03, 101969-05, 101973-03,
1107 # 101977-03, 101981-02, 101985-01, 102001-03, 102003-01, 102007-01,
1108 # 102011-02, 102024-01, 102039-01, 102044-01, 102048-01, 102062-03,
1109 # 102066-04, 102070-01, 102105-01, 102153-03, 102216-01, 102232-01,
1110 # 101878-03, 101879-01, 101880-03, 101933-01, 101950-01, 102030-01,
1111 # 102057-08, 102140-01, 101920-02, 101921-09, 101922-07, 101923-03
1112 #
1113 # If after installing these patches you are still experiencing a problem,
1114 # please email [email protected]. We have not observed this failure on
1115 # Solaris 2.5, so another option (instead of installing these patches) is
1116 # to upgrade to Solaris 2.5.
1117 set s [socket -server accept -myaddr $localhost 0]
1118 proc accept {s a p} {
1119 global x
1120 puts $s bye
1121 close $s
1122 set x done
1123 }
1124 set s1 [socket -async $localhost [lindex [fconfigure $s -sockname] 2]]
1125 vwait x
1126 gets $s1
1127 } -cleanup {
1128 close $s
1129 close $s1
1130 } -result bye
1131
1132 test socket_$af-9.1 {testing spurious events} -constraints [list socket supported_$af] -setup {
1133 set len 0
1134 set spurious 0
1135 set done 0
1136 set timer [after 10000 "set done timed_out"]
1137 } -body {
1138 proc readlittle {s} {
1139 global spurious done len
1140 set l [read $s 1]
1141 if {[string length $l] == 0} {
1142 if {![eof $s]} {
1143 incr spurious
1144 } else {
1145 close $s
1146 set done 1
1147 }
1148 } else {
1149 incr len [string length $l]
1150 }
1151 }
1152 proc accept {s a p} {
1153 fconfigure $s -buffering none -blocking off
1154 fileevent $s readable [list readlittle $s]
1155 }
1156 set s [socket -server accept -myaddr $localhost 0]
1157 set c [socket $localhost [lindex [fconfigure $s -sockname] 2]]
1158 puts -nonewline $c 01234567890123456789012345678901234567890123456789
1159 close $c
1160 vwait done
1161 close $s
1162 list $spurious $len
1163 } -cleanup {
1164 after cancel $timer
1165 } -result {0 50}
1166 test socket_$af-9.2 {testing async write, fileevents, flush on close} -constraints [list socket supported_$af] -setup {
1167 set firstblock ""
1168 for {set i 0} {$i < 5} {incr i} {set firstblock "a$firstblock$firstblock"}
1169 set secondblock ""
1170 for {set i 0} {$i < 16} {incr i} {
1171 set secondblock "b$secondblock$secondblock"
1172 }
1173 set timer [after 10000 "set done timed_out"]
1174 set l [socket -server accept -myaddr $localhost 0]
1175 proc accept {s a p} {
1176 fconfigure $s -blocking 0 -translation lf -buffersize 16384 \
1177 -buffering line
1178 fileevent $s readable "readable $s"
1179 }
1180 proc readable {s} {
1181 set l [gets $s]
1182 fileevent $s readable {}
1183 after idle respond $s
1184 }
1185 proc respond {s} {
1186 global firstblock
1187 puts -nonewline $s $firstblock
1188 after idle writedata $s
1189 }
1190 proc writedata {s} {
1191 global secondblock
1192 puts -nonewline $s $secondblock
1193 close $s
1194 }
1195 } -body {
1196 set s [socket $localhost [lindex [fconfigure $l -sockname] 2]]
1197 fconfigure $s -blocking 0 -trans lf -buffering line
1198 set count 0
1199 puts $s hello
1200 proc readit {s} {
1201 global count done
1202 set l [read $s]
1203 incr count [string length $l]
1204 if {[eof $s]} {
1205 close $s
1206 set done 1
1207 }
1208 }
1209 fileevent $s readable "readit $s"
1210 vwait done
1211 return $count
1212 } -cleanup {
1213 close $l
1214 after cancel $timer
1215 } -result 65566
1216 test socket_$af-9.3 {testing EOF stickyness} -constraints [list socket supported_$af] -setup {
1217 set count 0
1218 set done false
1219 proc write_then_close {s} {
1220 puts $s bye
1221 close $s
1222 }
1223 proc accept {s a p} {
1224 fconfigure $s -buffering line -translation lf
1225 fileevent $s writable "write_then_close $s"
1226 }
1227 set s [socket -server accept -myaddr $localhost 0]
1228 } -body {
1229 proc count_to_eof {s} {
1230 global count done
1231 set l [gets $s]
1232 if {[eof $s]} {
1233 incr count
1234 if {$count > 9} {
1235 close $s
1236 set done true
1237 set count {eof is sticky}
1238 }
1239 }
1240 }
1241 proc timerproc {s} {
1242 global done count
1243 set done true
1244 set count {timer went off, eof is not sticky}
1245 close $s
1246 }
1247 set c [socket $localhost [lindex [fconfigure $s -sockname] 2]]
1248 fconfigure $c -blocking off -buffering line -translation lf
1249 fileevent $c readable "count_to_eof $c"
1250 set timer [after 1000 timerproc $c]
1251 vwait done
1252 return $count
1253 } -cleanup {
1254 close $s
1255 after cancel $timer
1256 } -result {eof is sticky}
1257
1258 removeFile script
1259
1260 test socket_$af-10.1 {testing socket accept callback error handling} \
1261 -constraints [list socket supported_$af] -setup {
1262 variable goterror 0
1263 proc myHandler {msg options} {
1264 variable goterror 1
1265 }
1266 set handler [interp bgerror {}]
1267 interp bgerror {} [namespace which myHandler]
1268 } -body {
1269 set s [socket -server accept -myaddr $localhost 0]
1270 proc accept {s a p} {close $s; error}
1271 set c [socket $localhost [lindex [fconfigure $s -sockname] 2]]
1272 vwait goterror
1273 close $s
1274 close $c
1275 return $goterror
1276 } -cleanup {
1277 interp bgerror {} $handler
1278 } -result 1
1279
1280 test socket_$af-11.1 {tcp connection} -setup {
1281 set port [sendCommand {
1282 set server [socket -server accept 0]
1283 proc accept {s a p} {
1284 puts $s done
1285 close $s
1286 }
1287 getPort $server
1288 }]
1289 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1290 set s [socket $remoteServerIP $port]
1291 gets $s
1292 } -cleanup {
1293 close $s
1294 sendCommand {close $server}
1295 } -result done
1296 test socket_$af-11.2 {client specifies its port} -setup {
1297 set lport [randport]
1298 set rport [sendCommand {
1299 set server [socket -server accept 0]
1300 proc accept {s a p} {
1301 puts $s $p
1302 close $s
1303 }
1304 getPort $server
1305 }]
1306 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1307 set s [socket -myport $lport $remoteServerIP $rport]
1308 set r [gets $s]
1309 expr {$r==$lport ? "ok" : "broken: $r != $port"}
1310 } -cleanup {
1311 close $s
1312 sendCommand {close $server}
1313 } -result ok
1314 test socket_$af-11.3 {trying to connect, no server} -body {
1315 set status ok
1316 if {![catch {set s [socket $remoteServerIp [randport]]}]} {
1317 if {![catch {gets $s}]} {
1318 set status broken
1319 }
1320 close $s
1321 }
1322 return $status
1323 } -constraints [list socket supported_$af doTestsWithRemoteServer] -result ok
1324 test socket_$af-11.4 {remote echo, one line} -setup {
1325 set port [sendCommand {
1326 set server [socket -server accept 0]
1327 proc accept {s a p} {
1328 fileevent $s readable [list echo $s]
1329 fconfigure $s -buffering line -translation crlf
1330 }
1331 proc echo {s} {
1332 set l [gets $s]
1333 if {[eof $s]} {
1334 close $s
1335 } else {
1336 puts $s $l
1337 }
1338 }
1339 getPort $server
1340 }]
1341 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1342 set f [socket $remoteServerIP $port]
1343 fconfigure $f -translation crlf -buffering line
1344 puts $f hello
1345 gets $f
1346 } -cleanup {
1347 catch {close $f}
1348 sendCommand {close $server}
1349 } -result hello
1350 test socket_$af-11.5 {remote echo, 50 lines} -setup {
1351 set port [sendCommand {
1352 set server [socket -server accept 0]
1353 proc accept {s a p} {
1354 fileevent $s readable [list echo $s]
1355 fconfigure $s -buffering line -translation crlf
1356 }
1357 proc echo {s} {
1358 set l [gets $s]
1359 if {[eof $s]} {
1360 close $s
1361 } else {
1362 puts $s $l
1363 }
1364 }
1365 getPort $server
1366 }]
1367 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1368 set f [socket $remoteServerIP $port]
1369 fconfigure $f -translation crlf -buffering line
1370 for {set cnt 0} {$cnt < 50} {incr cnt} {
1371 puts $f "hello, $cnt"
1372 if {[gets $f] != "hello, $cnt"} {
1373 break
1374 }
1375 }
1376 return $cnt
1377 } -cleanup {
1378 close $f
1379 sendCommand {close $server}
1380 } -result 50
1381 test socket_$af-11.6 {socket conflict} -setup {
1382 set s1 [socket -server accept -myaddr $localhost 0]
1383 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1384 set s2 [socket -server accept -myaddr $localhost [getPort $s1]]
1385 list [getPort $s2] [close $s2]
1386 } -cleanup {
1387 close $s1
1388 } -returnCodes error -result {couldn't open socket: address already in use}
1389 test socket_$af-11.7 {server with several clients} -setup {
1390 set port [sendCommand {
1391 set server [socket -server accept 0]
1392 proc accept {s a p} {
1393 fconfigure $s -buffering line
1394 fileevent $s readable [list echo $s]
1395 }
1396 proc echo {s} {
1397 set l [gets $s]
1398 if {[eof $s]} {
1399 close $s
1400 } else {
1401 puts $s $l
1402 }
1403 }
1404 getPort $server
1405 }]
1406 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1407 set s1 [socket $remoteServerIP $port]
1408 fconfigure $s1 -buffering line
1409 set s2 [socket $remoteServerIP $port]
1410 fconfigure $s2 -buffering line
1411 set s3 [socket $remoteServerIP $port]
1412 fconfigure $s3 -buffering line
1413 for {set i 0} {$i < 100} {incr i} {
1414 puts $s1 hello,s1
1415 gets $s1
1416 puts $s2 hello,s2
1417 gets $s2
1418 puts $s3 hello,s3
1419 gets $s3
1420 }
1421 return $i
1422 } -cleanup {
1423 close $s1
1424 close $s2
1425 close $s3
1426 sendCommand {close $server}
1427 } -result 100
1428 test socket_$af-11.8 {client with several servers} -setup {
1429 lassign [sendCommand {
1430 set s1 [socket -server "accept server1" 0]
1431 set s2 [socket -server "accept server2" 0]
1432 set s3 [socket -server "accept server3" 0]
1433 proc accept {mp s a p} {
1434 puts $s $mp
1435 close $s
1436 }
1437 list [getPort $s1] [getPort $s2] [getPort $s3]
1438 }] p1 p2 p3
1439 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1440 set s1 [socket $remoteServerIP $p1]
1441 set s2 [socket $remoteServerIP $p2]
1442 set s3 [socket $remoteServerIP $p3]
1443 list [gets $s1] [gets $s1] [eof $s1] [gets $s2] [gets $s2] [eof $s2] \
1444 [gets $s3] [gets $s3] [eof $s3]
1445 } -cleanup {
1446 close $s1
1447 close $s2
1448 close $s3
1449 sendCommand {
1450 close $s1
1451 close $s2
1452 close $s3
1453 }
1454 } -result {server1 {} 1 server2 {} 1 server3 {} 1}
1455 test socket_$af-11.9 {accept callback error} -constraints [list socket supported_$af doTestsWithRemoteServer] -setup {
1456 proc myHandler {msg options} {
1457 variable x $msg
1458 }
1459 set handler [interp bgerror {}]
1460 interp bgerror {} [namespace which myHandler]
1461 set timer [after 10000 "set x timed_out"]
1462 } -body {
1463 set s [socket -server accept 0]
1464 proc accept {s a p} {expr {10 / 0}}
1465 sendCommand "set port [getPort $s]"
1466 if {[catch {
1467 sendCommand {
1468 set peername [fconfigure $callerSocket -peername]
1469 set s [socket [lindex $peername 0] $port]
1470 close $s
1471 }
1472 } msg]} then {
1473 close $s
1474 error $msg
1475 }
1476 vwait x
1477 return $x
1478 } -cleanup {
1479 close $s
1480 after cancel $timer
1481 interp bgerror {} $handler
1482 } -result {divide by zero}
1483 test socket_$af-11.10 {testing socket specific options} -setup {
1484 set port [sendCommand {
1485 set server [socket -server accept 0]
1486 proc accept {s a p} {close $s}
1487 getPort $server
1488 }]
1489 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1490 set s [socket $remoteServerIP $port]
1491 set p [fconfigure $s -peername]
1492 set n [fconfigure $s -sockname]
1493 list [expr {[lindex $p 2] == $port}] [llength $p] [llength $n]
1494 } -cleanup {
1495 close $s
1496 sendCommand {close $server}
1497 } -result {1 3 3}
1498 test socket_$af-11.11 {testing spurious events} -setup {
1499 set port [sendCommand {
1500 set server [socket -server accept 0]
1501 proc accept {s a p} {
1502 fconfigure $s -translation "auto lf"
1503 after idle writesome $s
1504 }
1505 proc writesome {s} {
1506 for {set i 0} {$i < 100} {incr i} {
1507 puts $s "line $i from remote server"
1508 }
1509 close $s
1510 }
1511 getPort $server
1512 }]
1513 set len 0
1514 set spurious 0
1515 set done 0
1516 set timer [after 40000 "set done timed_out"]
1517 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1518 proc readlittle {s} {
1519 global spurious done len
1520 set l [read $s 1]
1521 if {[string length $l] == 0} {
1522 if {![eof $s]} {
1523 incr spurious
1524 } else {
1525 close $s
1526 set done 1
1527 }
1528 } else {
1529 incr len [string length $l]
1530 }
1531 }
1532 set c [socket $remoteServerIP $port]
1533 fileevent $c readable "readlittle $c"
1534 vwait done
1535 list $spurious $len $done
1536 } -cleanup {
1537 after cancel $timer
1538 sendCommand {close $server}
1539 } -result {0 2690 1}
1540 test socket_$af-11.12 {testing EOF stickyness} -constraints [list socket supported_$af doTestsWithRemoteServer] -setup {
1541 set counter 0
1542 set done 0
1543 set port [sendCommand {
1544 set server [socket -server accept 0]
1545 proc accept {s a p} {
1546 after idle close $s
1547 }
1548 getPort $server
1549 }]
1550 proc timed_out {} {
1551 global c done
1552 set done {timed_out, EOF is not sticky}
1553 close $c
1554 }
1555 set after_id [after 1000 timed_out]
1556 } -body {
1557 proc count_up {s} {
1558 global counter done
1559 set l [gets $s]
1560 if {[eof $s]} {
1561 incr counter
1562 if {$counter > 9} {
1563 set done {EOF is sticky}
1564 close $s
1565 }
1566 }
1567 }
1568 set c [socket $remoteServerIP $port]
1569 fileevent $c readable [list count_up $c]
1570 vwait done
1571 return $done
1572 } -cleanup {
1573 after cancel $after_id
1574 sendCommand {close $server}
1575 } -result {EOF is sticky}
1576 test socket_$af-11.13 {testing async write, async flush, async close} -setup {
1577 set port [sendCommand {
1578 set firstblock ""
1579 for {set i 0} {$i < 5} {incr i} {
1580 set firstblock "a$firstblock$firstblock"
1581 }
1582 set secondblock ""
1583 for {set i 0} {$i < 16} {incr i} {
1584 set secondblock "b$secondblock$secondblock"
1585 }
1586 set l [socket -server accept 0]
1587 proc accept {s a p} {
1588 fconfigure $s -blocking 0 -translation lf -buffersize 16384 \
1589 -buffering line
1590 fileevent $s readable "readable $s"
1591 }
1592 proc readable {s} {
1593 set l [gets $s]
1594 fileevent $s readable {}
1595 after idle respond $s
1596 }
1597 proc respond {s} {
1598 global firstblock
1599 puts -nonewline $s $firstblock
1600 after idle writedata $s
1601 }
1602 proc writedata {s} {
1603 global secondblock
1604 puts -nonewline $s $secondblock
1605 close $s
1606 }
1607 getPort $l
1608 }]
1609 set timer [after 10000 "set done timed_out"]
1610 } -constraints [list socket supported_$af doTestsWithRemoteServer] -body {
1611 proc readit {s} {
1612 global count done
1613 set l [read $s]
1614 incr count [string length $l]
1615 if {[eof $s]} {
1616 close $s
1617 set done 1
1618 }
1619 }
1620 set s [socket $remoteServerIP $port]
1621 fconfigure $s -blocking 0 -trans lf -buffering line
1622 set count 0
1623 puts $s hello
1624 fileevent $s readable "readit $s"
1625 vwait done
1626 return $count
1627 } -cleanup {
1628 after cancel $timer
1629 sendCommand {close $l}
1630 } -result 65566
1631
1632 set path(script1) [makeFile {} script1]
1633 set path(script2) [makeFile {} script2]
1634
1635 test socket_$af-12.1 {testing inheritance of server sockets} -setup {
1636 file delete $path(script1)
1637 file delete $path(script2)
1638 # Script1 is just a 10 second delay. If the server socket is inherited, it
1639 # will be held open for 10 seconds
1640 set f [open $path(script1) w]
1641 puts $f {
1642 fileevent stdin readable exit
1643 after 10000 exit
1644 vwait forever
1645 }
1646 close $f
1647 # Script2 creates the server socket, launches script1, and exits.
1648 # The server socket will now be closed unless script1 inherited it.
1649 set f [open $path(script2) w]
1650 puts $f [list set tcltest [interpreter]]
1651 puts $f [list set delay $path(script1)]
1652 puts $f [list set localhost $localhost]
1653 puts $f {
1654 set f [socket -server accept -myaddr $localhost 0]
1655 proc accept { file addr port } {
1656 close $file
1657 }
1658 exec $tcltest $delay &
1659 puts [lindex [fconfigure $f -sockname] 2]
1660 close $f
1661 exit
1662 }
1663 close $f
1664 } -constraints [list socket supported_$af stdio exec] -body {
1665 # Launch script2 and wait 5 seconds
1666 ### exec [interpreter] script2 &
1667 set p [open "|[list [interpreter] $path(script2)]" r]
1668 # If we can still connect to the server, the socket got inherited.
1669 if {[catch {close [socket $localhost $listen]}]} {
1670 return {server socket was not inherited}
1671 } else {
1672 return {server socket was inherited}
1673 }
1674 } -cleanup {
1675 catch {close $p}
1676 } -result {server socket was not inherited}
1677 test socket_$af-12.2 {testing inheritance of client sockets} -setup {
1678 file delete $path(script1)
1679 file delete $path(script2)
1680 # Script1 is just a 20 second delay. If the server socket is inherited, it
1681 # will be held open for 20 seconds
1682 set f [open $path(script1) w]
1683 puts $f {
1684 fileevent stdin readable exit
1685 after 20000 exit
1686 vwait forever
1687 }
1688 close $f
1689 # Script2 opens the client socket and writes to it. It then launches
1690 # script1 and exits. If the child process inherited the client socket, the
1691 # socket will still be open.
1692 set f [open $path(script2) w]
1693 puts $f [list set tcltest [interpreter]]
1694 puts $f [list set delay $path(script1)]
1695 puts $f [list set localhost $localhost]
1696 puts $f {
1697 gets stdin port
1698 set f [socket $localhost $port]
1699 exec $tcltest $delay &
1700 puts $f testing
1701 flush $f
1702 exit
1703 }
1704 close $f
1705 # If the socket doesn't hit end-of-file in 10 seconds, the script1 process
1706 # must have inherited the client.
1707 set timeout 0
1708 set after [after 10000 {set x "client socket was inherited"}]
1709 } -constraints [list socket supported_$af stdio exec] -body {
1710 # Create the server socket
1711 set server [socket -server accept -myaddr $localhost 0]
1712 proc accept { file host port } {
1713 # When the client connects, establish the read handler
1714 global server
1715 close $server
1716 fileevent $file readable [list getdata $file]
1717 fconfigure $file -buffering line -blocking 0
1718 set ::f $file
1719 }
1720 proc getdata { file } {
1721 # Read handler on the accepted socket.
1722 global x
1723 set status [catch {read $file} data]
1724 if {$status != 0} {
1725 set x "read failed, error was $data"
1726 } elseif {$data ne ""} {
1727 } elseif {[fblocked $file]} {
1728 } elseif {[eof $file]} {
1729 set x "client socket was not inherited"
1730 } else {
1731 set x "impossible case"
1732 }
1733 }
1734 # Launch the script2 process
1735 ### exec [interpreter] script2 &
1736 set p [open "|[list [interpreter] $path(script2)]" w]
1737 puts $p [lindex [fconfigure $server -sockname] 2] ; flush $p
1738 vwait x
1739 return $x
1740 } -cleanup {
1741 fconfigure $f -blocking 1
1742 close $f
1743 after cancel $after
1744 close $p
1745 } -result {client socket was not inherited}
1746 test socket_$af-12.3 {testing inheritance of accepted sockets} -setup {
1747 file delete $path(script1)
1748 file delete $path(script2)
1749 set f [open $path(script1) w]
1750 puts $f {
1751 fileevent stdin readable exit
1752 after 10000 exit
1753 vwait forever
1754 }
1755 close $f
1756 set f [open $path(script2) w]
1757 puts $f [list set tcltest [interpreter]]
1758 puts $f [list set delay $path(script1)]
1759 puts $f [list set localhost $localhost]
1760 puts $f {
1761 set server [socket -server accept -myaddr $localhost 0]
1762 proc accept { file host port } {
1763 global tcltest delay
1764 puts $file {test data on socket}
1765 exec $tcltest $delay &
1766 after idle exit
1767 }
1768 puts stdout [lindex [fconfigure $server -sockname] 2]
1769 vwait forever
1770 }
1771 close $f
1772 } -constraints [list socket supported_$af stdio exec] -body {
1773 # Launch the script2 process and connect to it. See how long the socket
1774 # stays open
1775 ## exec [interpreter] script2 &
1776 set p [open "|[list [interpreter] $path(script2)]" r]
1777 gets $p listen
1778 set f [socket $localhost $listen]
1779 fconfigure $f -buffering full -blocking 0
1780 fileevent $f readable [list getdata $f]
1781 # If the socket is still open after 5 seconds, the script1 process must
1782 # have inherited the accepted socket.
1783 set failed 0
1784 set after [after 5000 [list set x "accepted socket was inherited"]]
1785 proc getdata { file } {
1786 # Read handler on the client socket.
1787 global x
1788 global failed
1789 set status [catch {read $file} data]
1790 if {$status != 0} {
1791 set x "read failed, error was $data"
1792 } elseif {[string compare {} $data]} {
1793 } elseif {[fblocked $file]} {
1794 } elseif {[eof $file]} {
1795 set x "accepted socket was not inherited"
1796 } else {
1797 set x "impossible case"
1798 }
1799 return
1800 }
1801 vwait x
1802 set x
1803 } -cleanup {
1804 fconfigure $f -blocking 1
1805 close $f
1806 after cancel $after
1807 close $p
1808 } -result {accepted socket was not inherited}
1809
1810 test socket_$af-13.1 {Testing use of shared socket between two threads} -body {
1811 # create a thread
1812 set serverthread [thread::create -preserved [string map [list @localhost@ $localhost] {
1813 set f [socket -server accept -myaddr @localhost@ 0]
1814 set listen [lindex [fconfigure $f -sockname] 2]
1815 proc accept {s a p} {
1816 fileevent $s readable [list echo $s]
1817 fconfigure $s -buffering line
1818 }
1819 proc echo {s} {
1820 global i
1821 set l [gets $s]
1822 if {[eof $s]} {
1823 global x
1824 close $s
1825 set x done
1826 } else {
1827 incr i
1828 puts $s $l
1829 }
1830 }
1831 set i 0
1832 vwait x
1833 close $f
1834 }]]
1835 set port [thread::send $serverthread {set listen}]
1836 set s [socket $localhost $port]
1837 fconfigure $s -buffering line
1838 catch {
1839 puts $s "hello"
1840 gets $s result
1841 }
1842 close $s
1843 thread::release $serverthread
1844 append result " " [llength [thread::names]]
1845 } -result {hello 1} -constraints [list socket supported_$af thread]
1846
1847 proc transf_test {{testmode transfer} {maxIter 1000} {maxTime 10000}} {
1848 try {
1849 set ::count 0
1850 set ::testmode $testmode
1851 set port 0
1852 set srvsock {}
1853 # if binding on port 0 is not possible (system related, blocked on ISPs etc):
1854 if {[catch {close [socket -async $::localhost $port]}]} {
1855 # simplest server on random port (immediately closing a connect):
1856 set port [randport]
1857 set srvsock [socket -server {apply {{ch args} {close $ch}}} -myaddr $::localhost $port]
1858 # socket on windows has some issues yet (e. g. bug [b6d0d8cc2c]), so we simply decrease iteration count (to 1/4):
1859 if {$::tcl_platform(platform) eq "windows" && $maxIter > 50} {
1860 set ::count [expr {$maxIter / 4 * 3 - 1}]; # bypass 3/4 iterations
1861 }
1862 }
1863 tcltest::DebugPuts 2 "== test \[$::localhost\]:$port $testmode =="
1864 set ::parent [thread::id]
1865 # helper thread creating async connection and initiating transfer (detach) to parent:
1866 set ::helper [thread::create]
1867 thread::send -async $::helper [list \
1868 lassign [list $::parent $::localhost $port $testmode] \
1869 ::parent ::localhost ::port ::testmode
1870 ]
1871 thread::send -async $::helper {
1872 set ::helper [thread::id]
1873 proc iteration {args} {
1874 set fd [socket -async $::localhost $::port]
1875 if {"helper-writable" in $::testmode} {;# to test both sides during connect
1876 fileevent $fd writable [list apply {{fd} {
1877 if {[thread::id] ne $::helper} {
1878 thread::send -async $::parent {set ::count "ERROR: invalid thread, $::helper is expecting"}
1879 close $fd
1880 return
1881 }
1882 }} $fd]
1883 };#
1884 thread::detach $fd
1885 thread::send -async $::parent [list transf_parent $fd {*}$args]
1886 }
1887 iteration first
1888 }
1889 # parent proc committing transfer attempt (attach) and checking acquire was successful:
1890 proc transf_parent {fd args} {
1891 tcltest::DebugPuts 2 "** trma / $::count ** $args **"
1892 thread::attach $fd
1893 if {"parent-close" in $::testmode} {;# to test close during connect
1894 set ::count $::count
1895 close $fd
1896 return
1897 };#
1898 fileevent $fd writable [list apply {{fd} {
1899 if {[thread::id] ne $::parent} {
1900 thread::send -async $::parent {set ::count "ERROR: invalid thread, $::parent is expecting"}
1901 close $fd
1902 return
1903 }
1904 set ::count $::count
1905 close $fd
1906 }} $fd]
1907 }
1908 # repeat maxIter times (up to maxTime ms as timeout):
1909 set tout [after $maxTime {set ::count "TIMEOUT"}]
1910 while 1 {
1911 vwait ::count
1912 if {![string is integer $::count]} {
1913 # if timeout just skip (test was successful until now):
1914 if {$::count eq "TIMEOUT"} {::tcltest::Skip "timing issue"}
1915 break
1916 }
1917 if {[incr ::count] >= $maxIter} break
1918 tcltest::DebugPuts 2 "** iter / $::count **"
1919 thread::send -async $::helper [list iteration nr $::count]
1920 }
1921 update
1922 set ::count
1923 } finally {
1924 catch {after cancel $tout}
1925 if {$srvsock ne {}} {close $srvsock}
1926 if {[info exists ::helper]} {thread::release -wait $::helper}
1927 tcltest::DebugPuts 2 "== stop / $::count =="
1928 unset -nocomplain ::count ::testmode ::parent ::helper
1929 }
1930 }
1931 test socket_$af-13.2.tr1 {Testing socket transfer between threads during async connect} -body {
1932 transf_test {transfer} 1000
1933 } -result 1000 -constraints [list socket supported_$af thread]
1934 test socket_$af-13.2.tr2 {Testing socket transfer between threads during async connect} -body {
1935 transf_test {transfer helper-writable} 100
1936 } -result 100 -constraints [list socket supported_$af thread]
1937 test socket_$af-13.2.cl1 {Testing socket transfer between threads during async connect} -body {
1938 transf_test {parent-close} 100
1939 } -result 100 -constraints [list socket supported_$af thread]
1940 test socket_$af-13.2.cl2 {Testing socket transfer between threads during async connect} -body {
1941 transf_test {parent-close helper-writable} 100
1942 } -result 100 -constraints [list socket supported_$af thread]
1943 catch {rename transf_parent {}}
1944 rename transf_test {}
1945
1946 # ----------------------------------------------------------------------
1947
1948 removeFile script1
1949 removeFile script2
1950
1951 # cleanup
1952 if {$remoteProcChan ne ""} {
1953 catch {sendCommand exit}
1954 }
1955 catch {close $commandSocket}
1956 catch {close $remoteProcChan}
1957 }
1958 unset ::tcl::unsupported::socketAF
1959 test socket-14.0.0 {[socket -async] when server only listens on IPv4} -setup {
1960 proc accept {s a p} {
1961 global x
1962 puts $s bye
1963 close $s
1964 set x ok
1965 }
1966 set server [socket -server accept -myaddr 127.0.0.1 0]
1967 set port [lindex [fconfigure $server -sockname] 2]
1968 } -constraints {socket supported_inet localhost_v4} -body {
1969 set client [socket -async localhost $port]
1970 set after [after $latency {set x [fconfigure $client -error]}]
1971 vwait x
1972 set x
1973 } -cleanup {
1974 catch {after cancel $after}
1975 catch {close $server}
1976 catch {close $client}
1977 unset -nocomplain x
1978 } -result ok
1979 test socket-14.0.1 {[socket -async] when server only listens on IPv6} -setup {
1980 proc accept {s a p} {
1981 global x
1982 puts $s bye
1983 close $s
1984 set x ok
1985 }
1986 set server [socket -server accept -myaddr ::1 0]
1987 set port [lindex [fconfigure $server -sockname] 2]
1988 } -constraints {socket supported_inet6 localhost_v6} -body {
1989 set client [socket -async localhost $port]
1990 set after [after $latency {set x [fconfigure $client -error]}]
1991 vwait x
1992 set x
1993 } -cleanup {
1994 catch {after cancel $after}
1995 catch {close $server}
1996 catch {close $client}
1997 unset -nocomplain x
1998 } -result ok
1999 test socket-14.1 {[socket -async] fileevent while still connecting} -setup {
2000 proc accept {s a p} {
2001 global x
2002 puts $s bye
2003 close $s
2004 lappend x ok
2005 }
2006 set server [socket -server accept -myaddr localhost 0]
2007 set port [lindex [fconfigure $server -sockname] 2]
2008 set x ""
2009 } -constraints socket -body {
2010 set client [socket -async localhost $port]
2011 fileevent $client writable {
2012 lappend x [fconfigure $client -error]
2013 fileevent $client writable {}
2014 }
2015 set after [after $latency {lappend x timeout}]
2016 while {[llength $x] < 2 && "timeout" ni $x} {
2017 vwait x
2018 }
2019 lsort $x; # we only want to see both events, the order doesn't matter
2020 } -cleanup {
2021 catch {after cancel $after}
2022 catch {close $server}
2023 catch {close $client}
2024 unset -nocomplain x
2025 } -result {{} ok}
2026 test socket-14.2 {[socket -async] fileevent connection refused} -setup {
2027 set after [after $latency set x timeout]
2028 } -body {
2029 set client [socket -async localhost [randport]]
2030 fileevent $client writable {set x ok}
2031 vwait x
2032 lappend x [fconfigure $client -error]
2033 } -constraints socket -cleanup {
2034 catch {after cancel $after}
2035 catch {close $client}
2036 unset -nocomplain x after client
2037 } -result {ok {connection refused}}
2038 test socket-14.3 {[socket -async] when server only listens on IPv6} -setup {
2039 proc accept {s a p} {
2040 global x
2041 puts $s bye
2042 close $s
2043 set x ok
2044 }
2045 set server [socket -server accept -myaddr ::1 0]
2046 set port [lindex [fconfigure $server -sockname] 2]
2047 } -constraints {socket supported_inet6 localhost_v6} -body {
2048 set client [socket -async localhost $port]
2049 set after [after $latency {set x [fconfigure $client -error]}]
2050 vwait x
2051 set x
2052 } -cleanup {
2053 catch {after cancel $after}
2054 catch {close $server}
2055 catch {close $client}
2056 unset -nocomplain x
2057 } -result ok
2058 test socket-14.4 {[socket -async] and both, readdable and writable fileevents} -setup {
2059 proc accept {s a p} {
2060 puts $s bye
2061 close $s
2062 }
2063 set server [socket -server accept -myaddr localhost 0]
2064 set port [lindex [fconfigure $server -sockname] 2]
2065 set x ""
2066 } -constraints socket -body {
2067 set client [socket -async localhost $port]
2068 fileevent $client writable {
2069 lappend x [fconfigure $client -error]
2070 fileevent $client writable {}
2071 }
2072 fileevent $client readable {lappend x [gets $client]}
2073 set after [after $latency {lappend x timeout}]
2074 while {[llength $x] < 2 && "timeout" ni $x} {
2075 vwait x
2076 }
2077 lsort $x
2078 } -cleanup {
2079 catch {after cancel $after}
2080 catch {close $client}
2081 catch {close $server}
2082 unset -nocomplain x
2083 } -result {{} bye}
2084 # FIXME: we should also have an IPv6 counterpart of this
2085 test socket-14.5 {[socket -async] which fails before any connect() can be made} -body {
2086 # address from rfc5737
2087 socket -async -myaddr 192.0.2.42 127.0.0.1 [randport]
2088 } -constraints {socket supported_inet notOSX} -returnCodes 1 \
2089 -result {couldn't open socket: cannot assign requested address}
2090 test socket-14.6.0 {[socket -async] with no event loop and server listening on IPv4} -setup {
2091 proc accept {s a p} {
2092 global x
2093 puts $s bye
2094 close $s
2095 set x ok
2096 }
2097 set server [socket -server accept -myaddr 127.0.0.1 0]
2098 set port [lindex [fconfigure $server -sockname] 2]
2099 set x ""
2100 } -constraints {socket supported_inet localhost_v4} -body {
2101 set client [socket -async localhost $port]
2102 for {set i 0} {$i < 50} {incr i } {
2103 update
2104 if {$x ne ""} {
2105 lappend x [gets $client]
2106 break
2107 }
2108 after 100
2109 }
2110 set x
2111 } -cleanup {
2112 catch {close $server}
2113 catch {close $client}
2114 unset -nocomplain x
2115 } -result {ok bye}
2116 test socket-14.6.1 {[socket -async] with no event loop and server listening on IPv6} -setup {
2117 proc accept {s a p} {
2118 global x
2119 puts $s bye
2120 close $s
2121 set x ok
2122 }
2123 set server [socket -server accept -myaddr ::1 0]
2124 set port [lindex [fconfigure $server -sockname] 2]
2125 set x ""
2126 } -constraints {socket supported_inet6 localhost_v6} -body {
2127 set client [socket -async localhost $port]
2128 for {set i 0} {$i < 50} {incr i } {
2129 update
2130 if {$x ne ""} {
2131 lappend x [gets $client]
2132 break
2133 }
2134 after 100
2135 }
2136 set x
2137 } -cleanup {
2138 catch {close $server}
2139 catch {close $client}
2140 unset -nocomplain x
2141 } -result {ok bye}
2142 test socket-14.7.0 {pending [socket -async] and blocking [gets], server is IPv4} -setup {
2143 makeFile {
2144 fileevent stdin readable exit
2145 set server [socket -server accept -myaddr 127.0.0.1 0]
2146 proc accept {s h p} {puts $s ok; close $s; set ::x 1}
2147 puts [lindex [fconfigure $server -sockname] 2]
2148 flush stdout
2149 vwait x
2150 } script
2151 set fd [open |[list [interpreter] script] RDWR]
2152 set port [gets $fd]
2153 } -constraints {socket supported_inet localhost_v4 notOSX} -body {
2154 set sock [socket -async localhost $port]
2155 list [fconfigure $sock -error] [gets $sock] [fconfigure $sock -error]
2156 } -cleanup {
2157 catch {close $fd}
2158 catch {close $sock}
2159 removeFile script
2160 } -result {{} ok {}}
2161 test socket-14.7.1 {pending [socket -async] and blocking [gets], server is IPv6} -setup {
2162 makeFile {
2163 fileevent stdin readable exit
2164 set server [socket -server accept -myaddr ::1 0]
2165 proc accept {s h p} {puts $s ok; close $s; set ::x 1}
2166 puts [lindex [fconfigure $server -sockname] 2]
2167 flush stdout
2168 vwait x
2169 } script
2170 set fd [open |[list [interpreter] script] RDWR]
2171 set port [gets $fd]
2172 } -constraints {socket supported_inet6 localhost_v6 notOSX} -body {
2173 set sock [socket -async localhost $port]
2174 list [fconfigure $sock -error] [gets $sock] [fconfigure $sock -error]
2175 } -cleanup {
2176 catch {close $fd}
2177 catch {close $sock}
2178 removeFile script
2179 } -result {{} ok {}}
2180 test socket-14.7.2 {pending [socket -async] and blocking [gets], no listener} -setup {
2181 set sock [socket -server error 0]
2182 set unusedPort [lindex [fconfigure $sock -sockname] 2]
2183 close $sock
2184 } -body {
2185 set sock [socket -async localhost $unusedPort]
2186 catch {gets $sock} x
2187 list $x [fconfigure $sock -error] [fconfigure $sock -error]
2188 } -constraints {socket notOSX} -cleanup {
2189 catch {close $sock}
2190 } -match glob -result {{error reading "sock*": socket is not connected} {connection refused} {}}
2191 test socket-14.8.0 {pending [socket -async] and nonblocking [gets], server is IPv4} -setup {
2192 makeFile {
2193 fileevent stdin readable exit
2194 set server [socket -server accept -myaddr 127.0.0.1 0]
2195 proc accept {s h p} {puts $s ok; close $s; set ::x 1}
2196 puts [lindex [fconfigure $server -sockname] 2]
2197 flush stdout
2198 vwait x
2199 } script
2200 set fd [open |[list [interpreter] script] RDWR]
2201 set port [gets $fd]
2202 } -constraints {socket supported_inet localhost_v4} -body {
2203 set sock [socket -async localhost $port]
2204 fconfigure $sock -blocking 0
2205 for {set i 0} {$i < 50} {incr i } {
2206 if {[catch {gets $sock} x] || $x ne "" || ![fblocked $sock]} break
2207 after 200
2208 }
2209 set x
2210 } -cleanup {
2211 catch {close $fd}
2212 catch {close $sock}
2213 removeFile script
2214 } -result {ok}
2215 test socket-14.8.1 {pending [socket -async] and nonblocking [gets], server is IPv6} -setup {
2216 makeFile {
2217 fileevent stdin readable exit
2218 set server [socket -server accept -myaddr ::1 0]
2219 proc accept {s h p} {puts $s ok; close $s; set ::x 1}
2220 puts [lindex [fconfigure $server -sockname] 2]
2221 flush stdout
2222 vwait x
2223 } script
2224 set fd [open |[list [interpreter] script] RDWR]
2225 set port [gets $fd]
2226 } -constraints {socket supported_inet6 localhost_v6} -body {
2227 set sock [socket -async localhost $port]
2228 fconfigure $sock -blocking 0
2229 for {set i 0} {$i < 50} {incr i } {
2230 if {[catch {gets $sock} x] || $x ne "" || ![fblocked $sock]} break
2231 after 200
2232 }
2233 set x
2234 } -cleanup {
2235 catch {close $fd}
2236 catch {close $sock}
2237 removeFile script
2238 } -result {ok}
2239 test socket-14.8.2 {pending [socket -async] and nonblocking [gets], no listener} -body {
2240 set sock [socket -async localhost [randport]]
2241 fconfigure $sock -blocking 0
2242 for {set i 0} {$i < 50} {incr i } {
2243 if {[catch {gets $sock} x] || $x ne "" || ![fblocked $sock]} break
2244 after 200
2245 }
2246 list $x [fconfigure $sock -error] [fconfigure $sock -error]
2247 } -constraints socket -cleanup {
2248 catch {close $sock}
2249 } -match glob -result {{error reading "sock*": socket is not connected} {connection refused} {}}
2250 test socket-14.9.0 {pending [socket -async] and blocking [puts], server is IPv4} -setup {
2251 makeFile {
2252 fileevent stdin readable exit
2253 after 10000 exit
2254 set server [socket -server accept -myaddr 127.0.0.1 0]
2255 proc accept {s h p} {set ::x $s}
2256 puts [lindex [fconfigure $server -sockname] 2]
2257 flush stdout
2258 vwait x
2259 puts [gets $x]
2260 } script
2261 set fd [open |[list [interpreter] script] RDWR]
2262 set port [gets $fd]
2263 } -constraints {socket supported_inet localhost_v4 notOSX} -body {
2264 set sock [socket -async localhost $port]
2265 puts $sock ok
2266 flush $sock
2267 list [fconfigure $sock -error] [gets $fd]
2268 } -cleanup {
2269 catch {close $fd}
2270 catch {close $sock}
2271 removeFile script
2272 } -result {{} ok}
2273 test socket-14.9.1 {pending [socket -async] and blocking [puts], server is IPv6} -setup {
2274 makeFile {
2275 fileevent stdin readable exit
2276 after 10000 exit
2277 set server [socket -server accept -myaddr ::1 0]
2278 proc accept {s h p} {set ::x $s}
2279 puts [lindex [fconfigure $server -sockname] 2]
2280 flush stdout
2281 vwait x
2282 puts [gets $x]
2283 } script
2284 set fd [open |[list [interpreter] script] RDWR]
2285 set port [gets $fd]
2286 } -constraints {socket supported_inet6 localhost_v6 notOSX} -body {
2287 set sock [socket -async localhost $port]
2288 puts $sock ok
2289 flush $sock
2290 list [fconfigure $sock -error] [gets $fd]
2291 } -cleanup {
2292 catch {close $fd}
2293 catch {close $sock}
2294 removeFile script
2295 } -result {{} ok}
2296 test socket-14.10.0 {pending [socket -async] and nonblocking [puts], server is IPv4} -setup {
2297 makeFile {
2298 fileevent stdin readable exit
2299 set server [socket -server accept -myaddr 127.0.0.1 0]
2300 proc accept {s h p} {set ::x $s}
2301 puts [lindex [fconfigure $server -sockname] 2]
2302 flush stdout
2303 vwait x
2304 puts [gets $x]
2305 } script
2306 set fd [open |[list [interpreter] script] RDWR]
2307 set port [gets $fd]
2308 set after [after $latency set x timeout]
2309 } -constraints {socket supported_inet localhost_v4} -body {
2310 set sock [socket -async localhost $port]
2311 fconfigure $sock -blocking 0
2312 puts $sock ok
2313 flush $sock
2314 fileevent $fd readable {set x 1}
2315 vwait x
2316 list [fconfigure $sock -error] [gets $fd]
2317 } -cleanup {
2318 after cancel $after
2319 catch {close $fd}
2320 catch {close $sock}
2321 removeFile script
2322 } -result {{} ok}
2323 test socket-14.10.1 {pending [socket -async] and nonblocking [puts], server is IPv6} -setup {
2324 makeFile {
2325 fileevent stdin readable exit
2326 set server [socket -server accept -myaddr ::1 0]
2327 proc accept {s h p} {set ::x $s}
2328 puts [lindex [fconfigure $server -sockname] 2]
2329 flush stdout
2330 vwait x
2331 puts [gets $x]
2332 } script
2333 set fd [open |[list [interpreter] script] RDWR]
2334 set port [gets $fd]
2335 set after [after $latency set x timeout]
2336 } -constraints {socket supported_inet6 localhost_v6} -body {
2337 set sock [socket -async localhost $port]
2338 fconfigure $sock -blocking 0
2339 puts $sock ok
2340 flush $sock
2341 fileevent $fd readable {set x 1}
2342 vwait x
2343 list [fconfigure $sock -error] [gets $fd]
2344 } -cleanup {
2345 after cancel $after
2346 catch {close $fd}
2347 catch {close $sock}
2348 removeFile script
2349 } -result {{} ok}
2350 test socket-14.11.0 {pending [socket -async] and nonblocking [puts], no listener, no flush} -setup {
2351 set after [after $latency set x timeout]
2352 } -body {
2353 set sock [socket -async localhost [randport]]
2354 fconfigure $sock -blocking 0
2355 puts $sock ok
2356 fileevent $sock writable {set x 1}
2357 vwait x
2358 close $sock
2359 } -constraints socket -cleanup {
2360 after cancel $after
2361 catch {close $sock}
2362 unset -nocomplain x
2363 } -result {socket is not connected} -returnCodes 1
2364 test socket-14.11.1 {pending [socket -async] and nonblocking [puts], no listener, flush} -setup {
2365 set after [after $latency set x timeout]
2366 } -body {
2367 set sock [socket -async localhost [randport]]
2368 fconfigure $sock -blocking 0
2369 puts $sock ok
2370 flush $sock
2371 fileevent $sock writable {set x 1}
2372 vwait x
2373 close $sock
2374 } -constraints {socket nonPortable} -cleanup {
2375 after cancel $timeout
2376 catch {close $sock}
2377 unset -nocomplain x
2378 } -result {socket is not connected} -returnCodes 1
2379 test socket-14.12 {[socket -async] background progress triggered by [fconfigure -error]} -body {
2380 set s [socket -async localhost [randport]]
2381 for {set i 0} {$i < 50} {incr i} {
2382 set x [fconfigure $s -error]
2383 if {$x ne ""} break
2384 after 200
2385 }
2386 set x
2387 } -constraints socket -cleanup {
2388 catch {close $s}
2389 unset -nocomplain x s
2390 } -result {connection refused}
2391 test socket-14.13 {testing writable event when quick failure} -body {
2392 # Test for bug 336441ed59 where a quick background fail was ignored
2393 #
2394 # Test only for windows as socket -async 255.255.255.255 fails
2395 # directly on Unix
2396 #
2397 # The following connect should fail very quickly
2398 set a1 [after $latency {set x timeout}]
2399 set s [socket -async 255.255.255.255 43434]
2400 fileevent $s writable {set x writable}
2401 vwait x
2402 set x
2403 } -constraints {socket win supported_inet} -cleanup {
2404 catch {close $s}
2405 after cancel $a1
2406 } -result writable
2407 test socket-14.14 {testing fileevent readable on failed async socket connect} -body {
2408 # Test for bug 581937ab1e
2409 set a1 [after $latency {set x timeout}]
2410 # This connect should fail
2411 set s [socket -async localhost [randport]]
2412 fileevent $s readable {set x readable}
2413 vwait x
2414 set x
2415 } -constraints socket -cleanup {
2416 catch {close $s}
2417 after cancel $a1
2418 } -result readable
2419 test socket-14.15 {blocking read on async socket should not trigger event handlers} -setup {
2420 set subprocess [open "|[list [interpreter]]" r+]
2421 fconfigure $subprocess -blocking 0 -buffering none
2422 } -constraints socket -body {
2423 puts $subprocess {
2424 set s [socket -async localhost [randport]]
2425 set x ok
2426 fileevent $s writable {set x fail}
2427 catch {read $s}
2428 close $s
2429 puts $x
2430 exit
2431 }
2432 set after [after $latency set x timeout]
2433 fileevent $subprocess readable [list gets $subprocess x]
2434 vwait x
2435 return $x
2436 } -cleanup {
2437 catch {after cancel $after}
2438 if {![testConstraint win]} {
2439 catch {exec kill [pid $subprocess]}
2440 }
2441 catch {close $subprocess}
2442 unset -nocomplain x
2443 } -result ok
2444 # v4 and v6 is required to prevent that the async connect does not terminate
2445 # before the fconfigure command. There is always an additional ip to try.
2446 test socket-14.16 {empty -peername while [socket -async] connecting} -body {
2447 set client [socket -async localhost [randport]]
2448 fconfigure $client -peername
2449 } -constraints {socket localhost_v4 localhost_v6 notOSX} -cleanup {
2450 catch {close $client}
2451 } -result {}
2452 # v4 and v6 is required to prevent that the async connect does not terminate
2453 # before the fconfigure command. There is always an additional ip to try.
2454 test socket-14.17 {empty -sockname while [socket -async] connecting} -body {
2455 set client [socket -async localhost [randport]]
2456 fconfigure $client -sockname
2457 } -constraints {socket localhost_v4 localhost_v6 notOSX} -cleanup {
2458 catch {close $client}
2459 } -result {}
2460 # test for bug c6ed4acfd8: running async socket connect with other connect
2461 # established will block tcl as it goes in an infinite loop in vwait
2462 test socket-14.18 {bug c6ed4acfd8: running async socket connect made other connect block} -body {
2463 proc accept {channel address port} {}
2464 set port [randport]
2465 set ssock [socket -server accept $port]
2466 set csock1 [socket -async localhost [randport]]
2467 set csock2 [socket localhost $port]
2468 after 1000 {set done ok}
2469 vwait done
2470 } -constraints {socket notOSX} -cleanup {
2471 catch {close $ssock}
2472 catch {close $csock1}
2473 catch {close $csock2}
2474 } -result {}
2475
2476 set num 0
2477
2478 set x {localhost {socket} 127.0.0.1 {supported_inet} ::1 {supported_inet6}}
2479 set resultok {-result "sock*" -match glob}
2480 set resulterr {
2481 -result {couldn't open socket: connection refused}
2482 -returnCodes 1
2483 }
2484 foreach {servip sc} $x {
2485 foreach {cliip cc} $x {
2486 set constraints [list socket $sc $cc]
2487 set result $resulterr
2488 switch -- [lsort -unique [list $servip $cliip]] {
2489 localhost - 127.0.0.1 - ::1 {
2490 set result $resultok
2491 }
2492 {127.0.0.1 localhost} {
2493 if {[testConstraint localhost_v4]} {
2494 set result $resultok
2495 }
2496 }
2497 {::1 localhost} {
2498 if {[testConstraint localhost_v6]} {
2499 set result $resultok
2500 }
2501 }
2502 }
2503 test socket-15.1.$num "Connect to $servip from $cliip" -setup {
2504 set server [socket -server accept -myaddr $servip 0]
2505 proc accept {s h p} { close $s }
2506 set port [lindex [fconfigure $server -sockname] 2]
2507 } -constraints $constraints -body {
2508 set s [socket $cliip $port]
2509 } -cleanup {
2510 close $server
2511 catch {close $s}
2512 } {*}$result
2513 incr num
2514 }
2515 }
2516
2517 ::tcltest::cleanupTests
2518 flush stdout
2519 return
2520
2521 # Local Variables:
2522 # mode: tcl
2523 # fill-column: 78
2524 # End: