diff options
Diffstat (limited to 'tests/socket.test')
-rw-r--r-- | tests/socket.test | 1352 |
1 files changed, 762 insertions, 590 deletions
diff --git a/tests/socket.test b/tests/socket.test index 0ae5abd..5542c09 100644 --- a/tests/socket.test +++ b/tests/socket.test @@ -1,14 +1,14 @@ # Commands tested in this file: socket. # -# This file contains a collection of tests for one or more of the Tcl -# built-in commands. Sourcing this file into Tcl runs the tests and -# generates output for errors. No output means no errors were found. +# This file contains a collection of tests for one or more of the Tcl built-in +# commands. Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. # # Copyright (c) 1994-1996 Sun Microsystems, Inc. # Copyright (c) 1998-2000 Ajuba Solutions. # -# See the file "license.terms" for information on usage and redistribution -# of this file, and for a DISCLAIMER OF ALL WARRANTIES. +# See the file "license.terms" for information on usage and redistribution of +# this file, and for a DISCLAIMER OF ALL WARRANTIES. # Running socket tests with a remote server: # ------------------------------------------ @@ -41,8 +41,8 @@ # # When the server starts, it prints out a detailed message containing its # configuration information, and it will block until killed with a Ctrl-C. -# Once the remote server exists, you can run the tests in socket.test with -# the server by setting two Tcl variables: +# Once the remote server exists, you can run the tests in socket.test with the +# server by setting two Tcl variables: # # % set remoteServerIP <name or address of machine on which server runs> # % set remoteServerPort 2048 @@ -63,12 +63,34 @@ package require tcltest 2 namespace import -force ::tcltest::* -# Some tests require the testthread and exec commands -testConstraint testthread [llength [info commands testthread]] +# Some tests require the Thread package or exec command +testConstraint thread [expr {0 == [catch {package require Thread 2.7-}]}] testConstraint exec [llength [info commands exec]] -# If remoteServerIP or remoteServerPort are not set, check in the -# environment variables for externally set values. +# Produce a random port number in the Dynamic/Private range +# from 49152 through 65535. +proc randport {} { expr {int(rand()*16383+49152)} } + +# Test the latency of tcp connections over the loopback interface. Some OSes +# (e.g. NetBSD) seem to use the Nagle algorithm and delayed ACKs, so it takes +# up to 200ms for a packet sent to localhost to arrive. We're measuring this +# here, so that OSes that don't have this problem can run the tests at full +# speed. +set server [socket -server {apply {{s a p} {set ::s1 $s}}} 0] +set s2 [socket localhost [lindex [fconfigure $server -sockname] 2]] +vwait s1; close $server +fconfigure $s1 -buffering line +fconfigure $s2 -buffering line +set t1 [clock milliseconds] +puts $s2 test1; gets $s1 +puts $s2 test2; gets $s1 +close $s1; close $s2 +set t2 [clock milliseconds] +set latency [expr {($t2-$t1)*2}]; # doubled as a safety margin +unset t1 t2 s1 s2 server + +# If remoteServerIP or remoteServerPort are not set, check in the environment +# variables for externally set values. # if {![info exists remoteServerIP]} { @@ -77,7 +99,7 @@ if {![info exists remoteServerIP]} { } } if {![info exists remoteServerPort]} { - if {[info exists env(remoteServerIP)]} { + if {[info exists env(remoteServerPort)]} { set remoteServerPort $env(remoteServerPort) } else { if {[info exists remoteServerIP]} { @@ -86,24 +108,55 @@ if {![info exists remoteServerPort]} { } } +if 0 { + # activate this to time the tests + proc test {args} { + set name [lindex $args 0] + puts "[lindex [time {uplevel [linsert $args 0 tcltest::test]}] 0] @@@ $name" + } +} + +foreach {af localhost} { + inet 127.0.0.1 + inet6 ::1 +} { + # Check if the family is supported and set the constraint accordingly + testConstraint supported_$af [expr {![catch {socket -server foo -myaddr $localhost 0} sock]}] + catch {close $sock} +} +testConstraint supported_any [expr {[testConstraint supported_inet] || [testConstraint supported_inet6]}] + +set sock [socket -server foo -myaddr localhost 0] +set sockname [fconfigure $sock -sockname] +close $sock +testConstraint localhost_v4 [expr {"127.0.0.1" in $sockname}] +testConstraint localhost_v6 [expr {"::1" in $sockname}] + + +foreach {af localhost} { + any 127.0.0.1 + inet 127.0.0.1 + inet6 ::1 +} { + set ::tcl::unsupported::socketAF $af # # Check if we're supposed to do tests against the remote server # set doTestsWithRemoteServer 1 if {![info exists remoteServerIP]} { - set remoteServerIP 127.0.0.1 + set remoteServerIP $localhost } if {($doTestsWithRemoteServer == 1) && (![info exists remoteServerPort])} { - set remoteServerPort 2048 + set remoteServerPort [randport] } -# Attempt to connect to a remote server if one is already running. If it -# is not running or for some other reason the connect fails, attempt to -# start the remote server on the local host listening on port 2048. This -# is only done on platforms that support exec (i.e. not on the Mac). On -# platforms that do not support exec, the remote server must be started -# by the user before running the tests. +# Attempt to connect to a remote server if one is already running. If it is +# not running or for some other reason the connect fails, attempt to start the +# remote server on the local host listening on port 2048. This is only done on +# platforms that support exec (i.e. not on the Mac). On platforms that do not +# support exec, the remote server must be started by the user before running +# the tests. set remoteProcChan "" set commandSocket "" @@ -117,7 +170,7 @@ if {$doTestsWithRemoteServer} { set noRemoteTestReason "can't exec" set doTestsWithRemoteServer 0 } else { - set remoteServerIP 127.0.0.1 + set remoteServerIP $localhost # Be *extra* careful in case this file is sourced from # a directory other than the current one... set remoteFile [file join [pwd] [file dirname [info script]] \ @@ -127,7 +180,7 @@ if {$doTestsWithRemoteServer} { [interpreter] $remoteFile -serverIsSilent \ -port $remoteServerPort -address $remoteServerIP]" w+] } msg]} then { - after 1000 + gets $remoteProcChan if {[catch { set commandSocket [socket $remoteServerIP $remoteServerPort] } msg] == 0} then { @@ -154,8 +207,7 @@ if {!$doTestsWithRemoteServer} { } # -# If we do the tests, define a command to send a command to the -# remote server. +# If we do the tests, define a command to send a command to the remote server. # if {[testConstraint doTestsWithRemoteServer]} { @@ -172,71 +224,73 @@ if {[testConstraint doTestsWithRemoteServer]} { error "remote server disappeared: $msg" } - set resp "" while {1} { set line [gets $commandSocket] if {[eof $commandSocket]} { error "remote server disappaered" } - if {[string compare $line "--Marker--Marker--Marker--"] == 0} { - if {[string compare [lindex $resp 0] error] == 0} { - error [lindex $resp 1] - } else { - return [lindex $resp 1] - } - } else { - append resp $line "\n" + if {$line eq "--Marker--Marker--Marker--"} { + lassign $result code info value + return -code $code -errorinfo $info $value } + append result $line "\n" } } } -test socket-1.1 {arg parsing for socket command} {socket} { - list [catch {socket -server} msg] $msg -} {1 {no argument given for -server option}} -test socket-1.2 {arg parsing for socket command} {socket} { - list [catch {socket -server foo} msg] $msg -} {1 {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}} -test socket-1.3 {arg parsing for socket command} {socket} { - list [catch {socket -myaddr} msg] $msg -} {1 {no argument given for -myaddr option}} -test socket-1.4 {arg parsing for socket command} {socket} { - list [catch {socket -myaddr 127.0.0.1} msg] $msg -} {1 {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}} -test socket-1.5 {arg parsing for socket command} {socket} { - list [catch {socket -myport} msg] $msg -} {1 {no argument given for -myport option}} -test socket-1.6 {arg parsing for socket command} {socket} { - list [catch {socket -myport xxxx} msg] $msg -} {1 {expected integer but got "xxxx"}} -test socket-1.7 {arg parsing for socket command} {socket} { - list [catch {socket -myport 2522} msg] $msg -} {1 {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}} -test socket-1.8 {arg parsing for socket command} {socket} { - list [catch {socket -froboz} msg] $msg -} {1 {bad option "-froboz": must be -async, -myaddr, -myport, or -server}} -test socket-1.9 {arg parsing for socket command} {socket} { - list [catch {socket -server foo -myport 2521 3333} msg] $msg -} {1 {option -myport is not valid for servers}} -test socket-1.10 {arg parsing for socket command} {socket} { - list [catch {socket host 2528 -junk} msg] $msg -} {1 {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}} -test socket-1.11 {arg parsing for socket command} {socket} { - list [catch {socket -server callback 2520 --} msg] $msg -} {1 {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"}} -test socket-1.12 {arg parsing for socket command} {socket} { - list [catch {socket foo badport} msg] $msg -} {1 {expected integer but got "badport"}} -test socket-1.13 {arg parsing for socket command} {socket} { -list [catch {socket -async -server} msg] $msg -} {1 {cannot set -async option for server sockets}} -test socket-1.14 {arg parsing for socket command} {socket} { -list [catch {socket -server foo -async} msg] $msg -} {1 {cannot set -async option for server sockets}} +proc getPort sock { + lindex [fconfigure $sock -sockname] 2 +} + + +# ---------------------------------------------------------------------- + +test socket_$af-1.1 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -server +} -returnCodes error -result {no argument given for -server option} +test socket_$af-1.2 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -server foo +} -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"} +test socket_$af-1.3 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -myaddr +} -returnCodes error -result {no argument given for -myaddr option} +test socket_$af-1.4 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -myaddr $localhost +} -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"} +test socket_$af-1.5 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -myport +} -returnCodes error -result {no argument given for -myport option} +test socket_$af-1.6 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -myport xxxx +} -returnCodes error -result {expected integer but got "xxxx"} +test socket_$af-1.7 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -myport 2522 +} -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"} +test socket_$af-1.8 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -froboz +} -returnCodes error -result {bad option "-froboz": must be -async, -myaddr, -myport, or -server} +test socket_$af-1.9 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -server foo -myport 2521 3333 +} -returnCodes error -result {option -myport is not valid for servers} +test socket_$af-1.10 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket host 2528 -junk +} -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"} +test socket_$af-1.11 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -server callback 2520 -- +} -returnCodes error -result {wrong # args: should be "socket ?-myaddr addr? ?-myport myport? ?-async? host port" or "socket -server command ?-myaddr addr? port"} +test socket_$af-1.12 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket foo badport +} -returnCodes error -result {expected integer but got "badport"} +test socket_$af-1.13 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -async -server +} -returnCodes error -result {cannot set -async option for server sockets} +test socket_$af-1.14 {arg parsing for socket command} -constraints [list socket supported_$af] -body { + socket -server foo -async +} -returnCodes error -result {cannot set -async option for server sockets} set path(script) [makeFile {} script] -test socket-2.1 {tcp connection} {socket stdio} { +test socket_$af-2.1 {tcp connection} -constraints [list socket supported_$af stdio] -setup { file delete $path(script) set f [open $path(script) w] puts $f { @@ -258,23 +312,17 @@ test socket-2.1 {tcp connection} {socket stdio} { set f [open "|[list [interpreter] $path(script)]" r] gets $f x gets $f listen - if {[catch {socket 127.0.0.1 $listen} msg]} { - set x $msg - } else { - lappend x [gets $f] - close $msg - } +} -body { + # $x == "ready" at this point + set sock [socket $localhost $listen] + lappend x [gets $f] + close $sock lappend x [gets $f] +} -cleanup { close $f - set x -} {ready done {}} - -if [info exists port] { - incr port -} else { - set port [expr 2048 + [pid]%1024] -} -test socket-2.2 {tcp connection with client port specified} {socket stdio} { +} -result {ready done {}} +test socket_$af-2.2 {tcp connection with client port specified} -setup { + set port [randport] file delete $path(script) set f [open $path(script) w] puts $f { @@ -296,32 +344,31 @@ test socket-2.2 {tcp connection with client port specified} {socket stdio} { set f [open "|[list [interpreter] $path(script)]" r] gets $f x gets $f listen - global port - if {[catch {socket -myport $port 127.0.0.1 $listen} sock]} { - set x $sock - close [socket 127.0.0.1 $listen] - puts stderr $sock - } else { - puts $sock hello - flush $sock - lappend x [gets $f] - close $sock - } +} -constraints [list socket supported_$af stdio] -body { + # $x == "ready" at this point + set sock [socket -myport $port $localhost $listen] + puts $sock hello + flush $sock + lappend x [expr {[gets $f] eq "hello $port"}] + close $sock + return $x +} -cleanup { + catch {close [socket $localhost $listen]} close $f - set x -} [list ready "hello $port"] -test socket-2.3 {tcp connection with client interface specified} {socket stdio} { +} -result {ready 1} +test socket_$af-2.3 {tcp connection with client interface specified} -setup { file delete $path(script) set f [open $path(script) w] puts $f { set timer [after 2000 "set x done"] - set f [socket -server accept 2830] + set f [socket -server accept 0] proc accept {file addr port} { global x puts "[gets $file] $addr" close $file set x done } + puts [lindex [fconfigure $f -sockname] 2] puts ready vwait x after cancel $timer @@ -329,24 +376,26 @@ test socket-2.3 {tcp connection with client interface specified} {socket stdio} } close $f set f [open "|[list [interpreter] $path(script)]" r] + gets $f listen gets $f x - if {[catch {socket -myaddr 127.0.0.1 127.0.0.1 2830} sock]} { - set x $sock - } else { - puts $sock hello - flush $sock - lappend x [gets $f] - close $sock - } +} -constraints [list socket supported_$af stdio] -body { + # $x == "ready" at this point + set sock [socket -myaddr $localhost $localhost $listen] + puts $sock hello + flush $sock + lappend x [gets $f] + close $sock + return $x +} -cleanup { close $f - set x -} {ready {hello 127.0.0.1}} -test socket-2.4 {tcp connection with server interface specified} {socket stdio} { +} -result [list ready [list hello $localhost]] +test socket_$af-2.4 {tcp connection with server interface specified} -setup { file delete $path(script) set f [open $path(script) w] + puts $f [list set localhost $localhost] puts $f { set timer [after 2000 "set x done"] - set f [socket -server accept -myaddr 127.0.0.1 0] + set f [socket -server accept -myaddr $localhost 0] proc accept {file addr port} { global x puts "[gets $file]" @@ -363,18 +412,18 @@ test socket-2.4 {tcp connection with server interface specified} {socket stdio} set f [open "|[list [interpreter] $path(script)]" r] gets $f x gets $f listen - if {[catch {socket 127.0.0.1 $listen} sock]} { - set x $sock - } else { - puts $sock hello - flush $sock - lappend x [gets $f] - close $sock - } +} -constraints [list socket supported_$af stdio] -body { + # $x == "ready" at this point + set sock [socket $localhost $listen] + puts $sock hello + flush $sock + lappend x [gets $f] + close $sock + return $x +} -cleanup { close $f - set x -} {ready hello} -test socket-2.5 {tcp connection with redundant server port} {socket stdio} { +} -result {ready hello} +test socket_$af-2.5 {tcp connection with redundant server port} -setup { file delete $path(script) set f [open $path(script) w] puts $f { @@ -396,28 +445,28 @@ test socket-2.5 {tcp connection with redundant server port} {socket stdio} { set f [open "|[list [interpreter] $path(script)]" r] gets $f x gets $f listen - if {[catch {socket 127.0.0.1 $listen} sock]} { - set x $sock - } else { - puts $sock hello - flush $sock - lappend x [gets $f] - close $sock - } +} -constraints [list socket supported_$af stdio] -body { + # $x == "ready" at this point + set sock [socket $localhost $listen] + puts $sock hello + flush $sock + lappend x [gets $f] + close $sock + return $x +} -cleanup { close $f - set x -} {ready hello} -test socket-2.6 {tcp connection} {socket} { +} -result {ready hello} +test socket_$af-2.6 {tcp connection} -constraints [list socket supported_$af] -body { set status ok - if {![catch {set sock [socket 127.0.0.1 2833]}]} { + if {![catch {set sock [socket $localhost [randport]]}]} { if {![catch {gets $sock}]} { set status broken } close $sock } set status -} ok -test socket-2.7 {echo server, one line} {socket stdio} { +} -result ok +test socket_$af-2.7 {echo server, one line} -constraints [list socket supported_$af stdio] -setup { file delete $path(script) set f [open $path(script) w] puts $f { @@ -448,18 +497,18 @@ test socket-2.7 {echo server, one line} {socket stdio} { set f [open "|[list [interpreter] $path(script)]" r] gets $f gets $f listen - set s [socket 127.0.0.1 $listen] +} -body { + set s [socket $localhost $listen] fconfigure $s -buffering line -translation lf puts $s "hello abcdefghijklmnop" - after 1000 set x [gets $s] close $s - set y [gets $f] + list $x [gets $f] +} -cleanup { close $f - list $x $y -} {{hello abcdefghijklmnop} done} +} -result {{hello abcdefghijklmnop} done} removeFile script -test socket-2.8 {echo server, loop 50 times, single connection} -constraints {socket stdio} -setup { +test socket_$af-2.8 {echo server, loop 50 times, single connection} -setup { set path(script) [makeFile { set f [socket -server accept 0] proc accept {s a p} { @@ -487,11 +536,11 @@ test socket-2.8 {echo server, loop 50 times, single connection} -constraints {so close $f puts "done $i" } script] -} -body { set f [open "|[list [interpreter] $path(script)]" r] gets $f gets $f listen - set s [socket 127.0.0.1 $listen] +} -constraints [list socket supported_$af stdio] -body { + set s [socket $localhost $listen] fconfigure $s -buffering line catch { for {set x 0} {$x < 50} {incr x} { @@ -501,30 +550,30 @@ test socket-2.8 {echo server, loop 50 times, single connection} -constraints {so } close $s catch {set x [gets $f]} - close $f - set x + return $x } -cleanup { + close $f removeFile script } -result {done 50} set path(script) [makeFile {} script] -test socket-2.9 {socket conflict} {socket stdio} { +test socket_$af-2.9 {socket conflict} -constraints [list socket supported_$af stdio] -body { set s [socket -server accept 0] file delete $path(script) set f [open $path(script) w] - puts -nonewline $f "socket -server accept [lindex [fconfigure $s -sockname] 2]" + puts $f [list set ::tcl::unsupported::socketAF $::tcl::unsupported::socketAF] + puts $f "socket -server accept [lindex [fconfigure $s -sockname] 2]" close $f set f [open "|[list [interpreter] $path(script)]" r] gets $f after 100 - set x [list [catch {close $f} msg]] - regsub "\n.*$" $msg {} msg ; # cut part of the error message containing the port number - lappend x $msg + close $f +} -returnCodes error -cleanup { close $s - set x -} {1 {couldn't open socket: address already in use}} -test socket-2.10 {close on accept, accepted socket lives} {socket} { +} -match glob -result {couldn't open socket: address already in use*} +test socket_$af-2.10 {close on accept, accepted socket lives} -setup { set done 0 set timer [after 20000 "set done timed_out"] +} -constraints [list socket supported_$af] -body { set ss [socket -server accept 0] proc accept {s a p} { global ss @@ -538,48 +587,51 @@ test socket-2.10 {close on accept, accepted socket lives} {socket} { close $s set done 1 } - set cs [socket [info hostname] [lindex [fconfigure $ss -sockname] 2]] + set cs [socket $localhost [lindex [fconfigure $ss -sockname] 2]] puts $cs hello close $cs vwait done + return $done +} -cleanup { after cancel $timer - set done -} 1 -test socket-2.11 {detecting new data} {socket} { +} -result 1 +test socket_$af-2.11 {detecting new data} -constraints [list socket supported_$af] -setup { proc accept {s a p} { global sock set sock $s } - set s [socket -server accept 0] set sock "" - set s2 [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]] +} -body { + set s2 [socket $localhost [lindex [fconfigure $s -sockname] 2]] vwait sock puts $s2 one flush $s2 - after 500 + after idle {set x 1} + vwait x fconfigure $sock -blocking 0 set result a:[gets $sock] lappend result b:[gets $sock] fconfigure $sock -blocking 1 puts $s2 two flush $s2 - after 500 + after $latency {set x 1}; # NetBSD fails here if we do [after idle] + vwait x fconfigure $sock -blocking 0 lappend result c:[gets $sock] +} -cleanup { fconfigure $sock -blocking 1 close $s2 close $s close $sock - set result -} {a:one b: c:two} +} -result {a:one b: c:two} - -test socket-3.1 {socket conflict} {socket stdio} { +test socket_$af-3.1 {socket conflict} -constraints [list socket supported_$af stdio] -setup { file delete $path(script) set f [open $path(script) w] + puts $f [list set localhost $localhost] puts $f { - set f [socket -server accept -myaddr 127.0.0.1 0] + set f [socket -server accept -myaddr $localhost 0] puts ready puts [lindex [fconfigure $f -sockname] 2] gets stdin @@ -589,21 +641,22 @@ test socket-3.1 {socket conflict} {socket stdio} { set f [open "|[list [interpreter] $path(script)]" r+] gets $f gets $f listen - set x [list [catch {socket -server accept -myaddr 127.0.0.1 $listen} msg] \ - $msg] +} -body { + socket -server accept -myaddr $localhost $listen +} -cleanup { puts $f bye close $f - set x -} {1 {couldn't open socket: address already in use}} -test socket-3.2 {server with several clients} {socket stdio} { +} -returnCodes error -result {couldn't open socket: address already in use} +test socket_$af-3.2 {server with several clients} -setup { file delete $path(script) set f [open $path(script) w] + puts $f [list set localhost $localhost] puts $f { set t1 [after 30000 "set x timed_out"] set t2 [after 31000 "set x timed_out"] set t3 [after 32000 "set x timed_out"] set counter 0 - set s [socket -server accept -myaddr 127.0.0.1 0] + set s [socket -server accept -myaddr $localhost 0] proc accept {s a p} { fileevent $s readable [list echo $s] fconfigure $s -buffering line @@ -633,11 +686,13 @@ test socket-3.2 {server with several clients} {socket stdio} { set f [open "|[list [interpreter] $path(script)]" r+] set x [gets $f] gets $f listen - set s1 [socket 127.0.0.1 $listen] +} -constraints [list socket supported_$af stdio] -body { + # $x == "ready" here + set s1 [socket $localhost $listen] fconfigure $s1 -buffering line - set s2 [socket 127.0.0.1 $listen] + set s2 [socket $localhost $listen] fconfigure $s2 -buffering line - set s3 [socket 127.0.0.1 $listen] + set s3 [socket $localhost $listen] fconfigure $s3 -buffering line for {set i 0} {$i < 100} {incr i} { puts $s1 hello,s1 @@ -651,16 +706,17 @@ test socket-3.2 {server with several clients} {socket stdio} { close $s2 close $s3 lappend x [gets $f] +} -cleanup { close $f - set x -} {ready done} +} -result {ready done} -test socket-4.1 {server with several clients} {socket stdio} { +test socket_$af-4.1 {server with several clients} -setup { file delete $path(script) set f [open $path(script) w] + puts $f [list set localhost $localhost] puts $f { set port [gets stdin] - set s [socket 127.0.0.1 $port] + set s [socket $localhost $port] fconfigure $s -buffering line for {set i 0} {$i < 100} {incr i} { puts $s hello @@ -677,6 +733,7 @@ test socket-4.1 {server with several clients} {socket stdio} { fconfigure $p2 -buffering line set p3 [open "|[list [interpreter] $path(script)]" r+] fconfigure $p3 -buffering line +} -constraints [list socket supported_$af stdio] -body { proc accept {s a p} { fconfigure $s -buffering line fileevent $s readable [list echo $s] @@ -694,7 +751,7 @@ test socket-4.1 {server with several clients} {socket stdio} { set t1 [after 30000 "set x timed_out"] set t2 [after 31000 "set x timed_out"] set t3 [after 32000 "set x timed_out"] - set s [socket -server accept -myaddr 127.0.0.1 0] + set s [socket -server accept -myaddr $localhost 0] set listen [lindex [fconfigure $s -sockname] 2] puts $p1 $listen puts $p2 $listen @@ -710,52 +767,42 @@ test socket-4.1 {server with several clients} {socket stdio} { lappend l [list p1 [gets $p1] $x] lappend l [list p2 [gets $p2] $x] lappend l [list p3 [gets $p3] $x] +} -cleanup { puts $p1 bye puts $p2 bye puts $p3 bye close $p1 close $p2 close $p3 - set l -} {{p1 bye done} {p2 bye done} {p3 bye done}} -test socket-4.2 {byte order problems, socket numbers, htons} {socket} { - set x ok - if {[catch {socket -server dodo -myaddr 127.0.0.1 0x3000} msg]} { - set x $msg - } else { - close $msg - } - set x -} ok +} -result {{p1 bye done} {p2 bye done} {p3 bye done}} +test socket_$af-4.2 {byte order problems, socket numbers, htons} -body { + close [socket -server dodo -myaddr $localhost 0x3000] + return ok +} -constraints [list socket supported_$af] -result ok -test socket-5.1 {byte order problems, socket numbers, htons} \ - {socket unix notRoot} { - set x {couldn't open socket: not owner} +test socket_$af-5.1 {byte order problems, socket numbers, htons} -body { if {![catch {socket -server dodo 0x1} msg]} { - set x {htons problem, should be disallowed, are you running as SU?} close $msg + return {htons problem, should be disallowed, are you running as SU?} } - set x -} {couldn't open socket: not owner} -test socket-5.2 {byte order problems, socket numbers, htons} {socket} { - set x {couldn't open socket: port number too high} + return {couldn't open socket: not owner} +} -constraints [list socket supported_$af unix notRoot] -result {couldn't open socket: not owner} +test socket_$af-5.2 {byte order problems, socket numbers, htons} -body { if {![catch {socket -server dodo 0x10000} msg]} { - set x {port resolution problem, should be disallowed} close $msg + return {port resolution problem, should be disallowed} } - set x -} {couldn't open socket: port number too high} -test socket-5.3 {byte order problems, socket numbers, htons} \ - {socket unix notRoot} { - set x {couldn't open socket: not owner} + return {couldn't open socket: port number too high} +} -constraints [list socket supported_$af] -result {couldn't open socket: port number too high} +test socket_$af-5.3 {byte order problems, socket numbers, htons} -body { if {![catch {socket -server dodo 21} msg]} { - set x {htons problem, should be disallowed, are you running as SU?} close $msg + return {htons problem, should be disallowed, are you running as SU?} } - set x -} {couldn't open socket: not owner} + return {couldn't open socket: not owner} +} -constraints [list socket supported_$af unix notRoot] -result {couldn't open socket: not owner} -test socket-6.1 {accept callback error} -constraints {socket stdio} -setup { +test socket_$af-6.1 {accept callback error} -constraints [list socket supported_$af stdio] -setup { proc myHandler {msg options} { variable x $msg } @@ -764,26 +811,45 @@ test socket-6.1 {accept callback error} -constraints {socket stdio} -setup { file delete $path(script) } -body { set f [open $path(script) w] + puts $f [list set localhost $localhost] puts $f { gets stdin port - socket 127.0.0.1 $port + socket $localhost $port } close $f set f [open "|[list [interpreter] $path(script)]" r+] proc accept {s a p} {expr 10 / 0} - set s [socket -server accept -myaddr 127.0.0.1 0] + set s [socket -server accept -myaddr $localhost 0] puts $f [lindex [fconfigure $s -sockname] 2] close $f set timer [after 10000 "set x timed_out"] vwait x after cancel $timer close $s - set x + return $x } -cleanup { interp bgerror {} $handler } -result {divide by zero} -test socket-7.1 {testing socket specific options} {socket stdio} { +test socket_$af-6.2 { + readable fileevent on server socket +} -setup { + set sock [socket -server dummy 0] +} -constraints [list socket supported_$af] -body { + fileevent $sock readable dummy +} -cleanup { + close $sock +} -returnCodes 1 -result "channel is not readable" + +test socket_$af-6.3 {writable fileevent on server socket} -setup { + set sock [socket -server dummy 0] +} -constraints [list socket supported_$af] -body { + fileevent $sock writable dummy +} -cleanup { + close $sock +} -returnCodes 1 -result "channel is not writable" + +test socket_$af-7.1 {testing socket specific options} -setup { file delete $path(script) set f [open $path(script) w] puts $f { @@ -802,20 +868,23 @@ test socket-7.1 {testing socket specific options} {socket stdio} { set f [open "|[list [interpreter] $path(script)]" r] gets $f gets $f listen - set s [socket 127.0.0.1 $listen] + set l "" +} -constraints [list socket supported_$af stdio] -body { + set s [socket $localhost $listen] set p [fconfigure $s -peername] close $s - close $f - set l "" - lappend l [string compare [lindex $p 0] 127.0.0.1] + lappend l [string compare [lindex $p 0] $localhost] lappend l [string compare [lindex $p 2] $listen] lappend l [llength $p] -} {0 0 3} -test socket-7.2 {testing socket specific options} {socket stdio} { +} -cleanup { + close $f +} -result {0 0 3} +test socket_$af-7.2 {testing socket specific options} -setup { file delete $path(script) set f [open $path(script) w] + puts $f [list set ::tcl::unsupported::socketAF $::tcl::unsupported::socketAF] puts $f { - set ss [socket -server accept 2821] + set ss [socket -server accept 0] proc accept args { global x set x done @@ -830,39 +899,46 @@ test socket-7.2 {testing socket specific options} {socket stdio} { set f [open "|[list [interpreter] $path(script)]" r] gets $f gets $f listen - set s [socket 127.0.0.1 $listen] +} -constraints [list socket supported_$af stdio] -body { + set s [socket $localhost $listen] set p [fconfigure $s -sockname] close $s - close $f list [llength $p] \ - [regexp {^(127\.0\.0\.1|0\.0\.0\.0)$} [lindex $p 0]] \ + [regexp {^(127\.0\.0\.1|0\.0\.0\.0|::1)$} [lindex $p 0]] \ [expr {[lindex $p 2] == $listen}] -} {3 1 0} -test socket-7.3 {testing socket specific options} {socket} { - set s [socket -server accept -myaddr 127.0.0.1 0] +} -cleanup { + close $f +} -result {3 1 0} +test socket_$af-7.3 {testing socket specific options} -constraints [list socket supported_$af] -body { + set s [socket -server accept -myaddr $localhost 0] set l [fconfigure $s] close $s update llength $l -} 14 -test socket-7.4 {testing socket specific options} {socket} { - set s [socket -server accept -myaddr 127.0.0.1 0] +} -result 14 +test socket_$af-7.4 {testing socket specific options} -constraints [list socket supported_$af] -setup { + set timer [after 10000 "set x timed_out"] + set l "" +} -body { + set s [socket -server accept -myaddr $localhost 0] proc accept {s a p} { global x set x [fconfigure $s -sockname] close $s } set listen [lindex [fconfigure $s -sockname] 2] - set s1 [socket 127.0.0.1 $listen] - set timer [after 10000 "set x timed_out"] + set s1 [socket $localhost $listen] vwait x + lappend l [expr {[lindex $x 2] == $listen}] [llength $x] +} -cleanup { after cancel $timer close $s close $s1 +} -result {1 3} +test socket_$af-7.5 {testing socket specific options} -setup { + set timer [after 10000 "set x timed_out"] set l "" - lappend l [expr {[lindex $x 2] == $listen}] [llength $x] -} {1 3} -test socket-7.5 {testing socket specific options} {socket unixOrPc} { +} -constraints [list socket supported_$af unixOrPc] -body { set s [socket -server accept 0] proc accept {s a p} { global x @@ -870,19 +946,18 @@ test socket-7.5 {testing socket specific options} {socket unixOrPc} { close $s } set listen [lindex [fconfigure $s -sockname] 2] - set s1 [socket 127.0.0.1 $listen] - set timer [after 10000 "set x timed_out"] + set s1 [socket $localhost $listen] vwait x + lappend l [lindex $x 0] [expr {[lindex $x 2] == $listen}] [llength $x] +} -cleanup { after cancel $timer close $s close $s1 - set l "" - lappend l [lindex $x 0] [expr {[lindex $x 2] == $listen}] [llength $x] -} {127.0.0.1 1 3} +} -result [list $localhost 1 3] -test socket-8.1 {testing -async flag on sockets} {socket} { - # NOTE: This test may fail on some Solaris 2.4 systems. If it does, - # check that you have these patches installed (using showrev -p): +test socket_$af-8.1 {testing -async flag on sockets} -constraints [list socket supported_$af] -body { + # NOTE: This test may fail on some Solaris 2.4 systems. If it does, check + # that you have these patches installed (using showrev -p): # # 101907-05, 101925-02, 101945-14, 101959-03, 101969-05, 101973-03, # 101977-03, 101981-02, 101985-01, 102001-03, 102003-01, 102007-01, @@ -891,29 +966,31 @@ test socket-8.1 {testing -async flag on sockets} {socket} { # 101878-03, 101879-01, 101880-03, 101933-01, 101950-01, 102030-01, # 102057-08, 102140-01, 101920-02, 101921-09, 101922-07, 101923-03 # - # If after installing these patches you are still experiencing a - # problem, please email jyl@eng.sun.com. We have not observed this - # failure on Solaris 2.5, so another option (instead of installing - # these patches) is to upgrade to Solaris 2.5. - set s [socket -server accept -myaddr 127.0.0.1 0] + # If after installing these patches you are still experiencing a problem, + # please email jyl@eng.sun.com. We have not observed this failure on + # Solaris 2.5, so another option (instead of installing these patches) is + # to upgrade to Solaris 2.5. + set s [socket -server accept -myaddr $localhost 0] proc accept {s a p} { global x puts $s bye close $s set x done } - set s1 [socket -async 127.0.0.1 [lindex [fconfigure $s -sockname] 2]] + set s1 [socket -async $localhost [lindex [fconfigure $s -sockname] 2]] vwait x - set z [gets $s1] + gets $s1 +} -cleanup { close $s close $s1 - set z -} bye +} -result bye -test socket-9.1 {testing spurious events} {socket} { +test socket_$af-9.1 {testing spurious events} -constraints [list socket supported_$af] -setup { set len 0 set spurious 0 set done 0 + set timer [after 10000 "set done timed_out"] +} -body { proc readlittle {s} { global spurious done len set l [read $s 1] @@ -932,24 +1009,25 @@ test socket-9.1 {testing spurious events} {socket} { fconfigure $s -buffering none -blocking off fileevent $s readable [list readlittle $s] } - set s [socket -server accept -myaddr 127.0.0.1 0] - set c [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]] + set s [socket -server accept -myaddr $localhost 0] + set c [socket $localhost [lindex [fconfigure $s -sockname] 2]] puts -nonewline $c 01234567890123456789012345678901234567890123456789 close $c - set timer [after 10000 "set done timed_out"] vwait done - after cancel $timer close $s list $spurious $len -} {0 50} -test socket-9.2 {testing async write, fileevents, flush on close} {socket} { +} -cleanup { + after cancel $timer +} -result {0 50} +test socket_$af-9.2 {testing async write, fileevents, flush on close} -constraints [list socket supported_$af] -setup { set firstblock "" for {set i 0} {$i < 5} {incr i} {set firstblock "a$firstblock$firstblock"} set secondblock "" for {set i 0} {$i < 16} {incr i} { set secondblock "b$secondblock$secondblock" } - set l [socket -server accept -myaddr 127.0.0.1 0] + set timer [after 10000 "set done timed_out"] + set l [socket -server accept -myaddr $localhost 0] proc accept {s a p} { fconfigure $s -blocking 0 -translation lf -buffersize 16384 \ -buffering line @@ -958,19 +1036,20 @@ test socket-9.2 {testing async write, fileevents, flush on close} {socket} { proc readable {s} { set l [gets $s] fileevent $s readable {} - after 1000 respond $s + after idle respond $s } proc respond {s} { global firstblock puts -nonewline $s $firstblock - after 1000 writedata $s + after idle writedata $s } proc writedata {s} { global secondblock puts -nonewline $s $secondblock close $s } - set s [socket 127.0.0.1 [lindex [fconfigure $l -sockname] 2]] +} -body { + set s [socket $localhost [lindex [fconfigure $l -sockname] 2]] fconfigure $s -blocking 0 -trans lf -buffering line set count 0 puts $s hello @@ -984,15 +1063,27 @@ test socket-9.2 {testing async write, fileevents, flush on close} {socket} { } } fileevent $s readable "readit $s" - set timer [after 10000 "set done timed_out"] vwait done - after cancel $timer + return $count +} -cleanup { close $l - set count -} 65566 -test socket-9.3 {testing EOF stickyness} {socket} { + after cancel $timer +} -result 65566 +test socket_$af-9.3 {testing EOF stickyness} -constraints [list socket supported_$af] -setup { + set count 0 + set done false + proc write_then_close {s} { + puts $s bye + close $s + } + proc accept {s a p} { + fconfigure $s -buffering line -translation lf + fileevent $s writable "write_then_close $s" + } + set s [socket -server accept -myaddr $localhost 0] +} -body { proc count_to_eof {s} { - global count done timer + global count done set l [gets $s] if {[eof $s]} { incr count @@ -1000,41 +1091,30 @@ test socket-9.3 {testing EOF stickyness} {socket} { close $s set done true set count {eof is sticky} - after cancel $timer } } } - proc timerproc {} { - global done count c + proc timerproc {s} { + global done count set done true set count {timer went off, eof is not sticky} - close $c - } - set count 0 - set done false - proc write_then_close {s} { - puts $s bye close $s } - proc accept {s a p} { - fconfigure $s -buffering line -translation lf - fileevent $s writable "write_then_close $s" - } - set s [socket -server accept -myaddr 127.0.0.1 0] - set c [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]] + set c [socket $localhost [lindex [fconfigure $s -sockname] 2]] fconfigure $c -blocking off -buffering line -translation lf fileevent $c readable "count_to_eof $c" - set timer [after 1000 timerproc] + set timer [after 1000 timerproc $c] vwait done + return $count +} -cleanup { close $s - set count -} {eof is sticky} + after cancel $timer +} -result {eof is sticky} removeFile script -test socket-10.1 {testing socket accept callback error handling} -constraints { - socket -} -setup { +test socket_$af-10.1 {testing socket accept callback error handling} \ + -constraints [list socket supported_$af] -setup { variable goterror 0 proc myHandler {msg options} { variable goterror 1 @@ -1042,68 +1122,64 @@ test socket-10.1 {testing socket accept callback error handling} -constraints { set handler [interp bgerror {}] interp bgerror {} [namespace which myHandler] } -body { - set s [socket -server accept -myaddr 127.0.0.1 0] + set s [socket -server accept -myaddr $localhost 0] proc accept {s a p} {close $s; error} - set c [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]] + set c [socket $localhost [lindex [fconfigure $s -sockname] 2]] vwait goterror close $s close $c - set goterror + return $goterror } -cleanup { interp bgerror {} $handler } -result 1 -test socket-11.1 {tcp connection} {socket doTestsWithRemoteServer} { - sendCommand { - set socket9_1_test_server [socket -server accept 2834] +test socket_$af-11.1 {tcp connection} -setup { + set port [sendCommand { + set server [socket -server accept 0] proc accept {s a p} { puts $s done close $s } - } - set s [socket $remoteServerIP 2834] - set r [gets $s] + getPort $server + }] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + set s [socket $remoteServerIP $port] + gets $s +} -cleanup { close $s - sendCommand {close $socket9_1_test_server} - set r -} done -test socket-11.2 {client specifies its port} {socket doTestsWithRemoteServer} { - if {[info exists port]} { - incr port - } else { - set port [expr 2048 + [pid]%1024] - } - sendCommand { - set socket9_2_test_server [socket -server accept 2835] + sendCommand {close $server} +} -result done +test socket_$af-11.2 {client specifies its port} -setup { + set lport [randport] + set rport [sendCommand { + set server [socket -server accept 0] proc accept {s a p} { puts $s $p close $s } - } - set s [socket -myport $port $remoteServerIP 2835] + getPort $server + }] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + set s [socket -myport $lport $remoteServerIP $rport] set r [gets $s] + expr {$r==$lport ? "ok" : "broken: $r != $port"} +} -cleanup { close $s - sendCommand {close $socket9_2_test_server} - if {$r == $port} { - set result ok - } else { - set result broken - } - set result -} ok -test socket-11.3 {trying to connect, no server} {socket doTestsWithRemoteServer} { + sendCommand {close $server} +} -result ok +test socket_$af-11.3 {trying to connect, no server} -body { set status ok - if {![catch {set s [socket $remoteServerIp 2836]}]} { + if {![catch {set s [socket $remoteServerIp [randport]]}]} { if {![catch {gets $s}]} { set status broken } close $s } - set status -} ok -test socket-11.4 {remote echo, one line} {socket doTestsWithRemoteServer} { - sendCommand { - set socket10_6_test_server [socket -server accept 2836] + return $status +} -constraints [list socket supported_$af doTestsWithRemoteServer] -result ok +test socket_$af-11.4 {remote echo, one line} -setup { + set port [sendCommand { + set server [socket -server accept 0] proc accept {s a p} { fileevent $s readable [list echo $s] fconfigure $s -buffering line -translation crlf @@ -1116,18 +1192,20 @@ test socket-11.4 {remote echo, one line} {socket doTestsWithRemoteServer} { puts $s $l } } - } - set f [socket $remoteServerIP 2836] + getPort $server + }] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + set f [socket $remoteServerIP $port] fconfigure $f -translation crlf -buffering line puts $f hello - set r [gets $f] - close $f - sendCommand {close $socket10_6_test_server} - set r -} hello -test socket-11.5 {remote echo, 50 lines} {socket doTestsWithRemoteServer} { - sendCommand { - set socket10_7_test_server [socket -server accept 2836] + gets $f +} -cleanup { + catch {close $f} + sendCommand {close $server} +} -result hello +test socket_$af-11.5 {remote echo, 50 lines} -setup { + set port [sendCommand { + set server [socket -server accept 0] proc accept {s a p} { fileevent $s readable [list echo $s] fconfigure $s -buffering line -translation crlf @@ -1140,33 +1218,33 @@ test socket-11.5 {remote echo, 50 lines} {socket doTestsWithRemoteServer} { puts $s $l } } - } - set f [socket $remoteServerIP 2836] + getPort $server + }] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + set f [socket $remoteServerIP $port] fconfigure $f -translation crlf -buffering line for {set cnt 0} {$cnt < 50} {incr cnt} { puts $f "hello, $cnt" - if {[string compare [gets $f] "hello, $cnt"] != 0} { + if {[gets $f] != "hello, $cnt"} { break } } + return $cnt +} -cleanup { close $f - sendCommand {close $socket10_7_test_server} - set cnt -} 50 -test socket-11.6 {socket conflict} {socket doTestsWithRemoteServer} { - set s1 [socket -server accept -myaddr 127.0.0.1 2836] - if {[catch {set s2 [socket -server accept -myaddr 127.0.0.1 2836]} msg]} { - set result [list 1 $msg] - } else { - set result [list 0 [lindex [fconfigure $s2 -sockname] 2]] - close $s2 - } + sendCommand {close $server} +} -result 50 +test socket_$af-11.6 {socket conflict} -setup { + set s1 [socket -server accept -myaddr $localhost 0] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + set s2 [socket -server accept -myaddr $localhost [getPort $s1]] + list [getPort $s2] [close $s2] +} -cleanup { close $s1 - set result -} {1 {couldn't open socket: address already in use}} -test socket-11.7 {server with several clients} {socket doTestsWithRemoteServer} { - sendCommand { - set socket10_9_test_server [socket -server accept 2836] +} -returnCodes error -result {couldn't open socket: address already in use} +test socket_$af-11.7 {server with several clients} -setup { + set port [sendCommand { + set server [socket -server accept 0] proc accept {s a p} { fconfigure $s -buffering line fileevent $s readable [list echo $s] @@ -1179,12 +1257,14 @@ test socket-11.7 {server with several clients} {socket doTestsWithRemoteServer} puts $s $l } } - } - set s1 [socket $remoteServerIP 2836] + getPort $server + }] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + set s1 [socket $remoteServerIP $port] fconfigure $s1 -buffering line - set s2 [socket $remoteServerIP 2836] + set s2 [socket $remoteServerIP $port] fconfigure $s2 -buffering line - set s3 [socket $remoteServerIP 2836] + set s3 [socket $remoteServerIP $port] fconfigure $s3 -buffering line for {set i 0} {$i < 100} {incr i} { puts $s1 hello,s1 @@ -1194,28 +1274,31 @@ test socket-11.7 {server with several clients} {socket doTestsWithRemoteServer} puts $s3 hello,s3 gets $s3 } + return $i +} -cleanup { close $s1 close $s2 close $s3 - sendCommand {close $socket10_9_test_server} - set i -} 100 -test socket-11.8 {client with several servers} {socket doTestsWithRemoteServer} { - sendCommand { - set s1 [socket -server "accept 4003" 4003] - set s2 [socket -server "accept 4004" 4004] - set s3 [socket -server "accept 4005" 4005] + sendCommand {close $server} +} -result 100 +test socket_$af-11.8 {client with several servers} -setup { + lassign [sendCommand { + set s1 [socket -server "accept server1" 0] + set s2 [socket -server "accept server2" 0] + set s3 [socket -server "accept server3" 0] proc accept {mp s a p} { puts $s $mp close $s } - } - set s1 [socket $remoteServerIP 4003] - set s2 [socket $remoteServerIP 4004] - set s3 [socket $remoteServerIP 4005] - set l "" - lappend l [gets $s1] [gets $s1] [eof $s1] [gets $s2] [gets $s2] [eof $s2] \ + list [getPort $s1] [getPort $s2] [getPort $s3] + }] p1 p2 p3 +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + set s1 [socket $remoteServerIP $p1] + set s2 [socket $remoteServerIP $p2] + set s3 [socket $remoteServerIP $p3] + list [gets $s1] [gets $s1] [eof $s1] [gets $s2] [gets $s2] [eof $s2] \ [gets $s3] [gets $s3] [eof $s3] +} -cleanup { close $s1 close $s2 close $s3 @@ -1224,55 +1307,56 @@ test socket-11.8 {client with several servers} {socket doTestsWithRemoteServer} close $s2 close $s3 } - set l -} {4003 {} 1 4004 {} 1 4005 {} 1} -test socket-11.9 {accept callback error} -constraints { - socket doTestsWithRemoteServer -} -setup { +} -result {server1 {} 1 server2 {} 1 server3 {} 1} +test socket_$af-11.9 {accept callback error} -constraints [list socket supported_$af doTestsWithRemoteServer] -setup { proc myHandler {msg options} { variable x $msg } set handler [interp bgerror {}] interp bgerror {} [namespace which myHandler] + set timer [after 10000 "set x timed_out"] } -body { - set s [socket -server accept 2836] - proc accept {s a p} {expr 10 / 0} - if {[catch {sendCommand { + set s [socket -server accept 0] + proc accept {s a p} {expr {10 / 0}} + sendCommand "set port [getPort $s]" + if {[catch { + sendCommand { set peername [fconfigure $callerSocket -peername] - set s [socket [lindex $peername 0] 2836] + set s [socket [lindex $peername 0] $port] close $s - }} msg]} { + } + } msg]} then { close $s error $msg } - set timer [after 10000 "set x timed_out"] vwait x - after cancel $timer - close $s - set x + return $x } -cleanup { + close $s + after cancel $timer interp bgerror {} $handler } -result {divide by zero} -test socket-11.10 {testing socket specific options} {socket doTestsWithRemoteServer} { - sendCommand { - set socket10_12_test_server [socket -server accept 2836] +test socket_$af-11.10 {testing socket specific options} -setup { + set port [sendCommand { + set server [socket -server accept 0] proc accept {s a p} {close $s} - } - set s [socket $remoteServerIP 2836] + getPort $server + }] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + set s [socket $remoteServerIP $port] set p [fconfigure $s -peername] set n [fconfigure $s -sockname] - set l "" - lappend l [lindex $p 2] [llength $p] [llength $p] + list [expr {[lindex $p 2] == $port}] [llength $p] [llength $n] +} -cleanup { close $s - sendCommand {close $socket10_12_test_server} - set l -} {2836 3 3} -test socket-11.11 {testing spurious events} {socket doTestsWithRemoteServer} { - sendCommand { - set socket10_13_test_server [socket -server accept 2836] + sendCommand {close $server} +} -result {1 3 3} +test socket_$af-11.11 {testing spurious events} -setup { + set port [sendCommand { + set server [socket -server accept 0] proc accept {s a p} { fconfigure $s -translation "auto lf" - after 100 writesome $s + after idle writesome $s } proc writesome {s} { for {set i 0} {$i < 100} {incr i} { @@ -1280,10 +1364,13 @@ test socket-11.11 {testing spurious events} {socket doTestsWithRemoteServer} { } close $s } - } + getPort $server + }] set len 0 set spurious 0 set done 0 + set timer [after 40000 "set done timed_out"] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { proc readlittle {s} { global spurious done len set l [read $s 1] @@ -1298,59 +1385,52 @@ test socket-11.11 {testing spurious events} {socket doTestsWithRemoteServer} { incr len [string length $l] } } - set c [socket $remoteServerIP 2836] + set c [socket $remoteServerIP $port] fileevent $c readable "readlittle $c" - set timer [after 40000 "set done timed_out"] vwait done - after cancel $timer - sendCommand {close $socket10_13_test_server} list $spurious $len $done -} {0 2690 1} -test socket-11.12 {testing EOF stickyness} {socket doTestsWithRemoteServer} { +} -cleanup { + after cancel $timer + sendCommand {close $server} +} -result {0 2690 1} +test socket_$af-11.12 {testing EOF stickyness} -constraints [list socket supported_$af doTestsWithRemoteServer] -setup { set counter 0 set done 0 + set port [sendCommand { + set server [socket -server accept 0] + proc accept {s a p} { + after idle close $s + } + getPort $server + }] + proc timed_out {} { + global c done + set done {timed_out, EOF is not sticky} + close $c + } + set after_id [after 1000 timed_out] +} -body { proc count_up {s} { - global counter done after_id + global counter done set l [gets $s] if {[eof $s]} { incr counter if {$counter > 9} { set done {EOF is sticky} - after cancel $after_id close $s } } } - proc timed_out {} { - global c done - set done {timed_out, EOF is not sticky} - close $c - } - sendCommand { - set socket10_14_test_server [socket -server accept 2836] - proc accept {s a p} { - after 100 close $s - } - } - set c [socket $remoteServerIP 2836] + set c [socket $remoteServerIP $port] fileevent $c readable [list count_up $c] - set after_id [after 1000 timed_out] vwait done - sendCommand {close $socket10_14_test_server} - set done -} {EOF is sticky} -test socket-11.13 {testing async write, async flush, async close} \ - {socket doTestsWithRemoteServer} { - proc readit {s} { - global count done - set l [read $s] - incr count [string length $l] - if {[eof $s]} { - close $s - set done 1 - } - } - sendCommand { + return $done +} -cleanup { + after cancel $after_id + sendCommand {close $server} +} -result {EOF is sticky} +test socket_$af-11.13 {testing async write, async flush, async close} -setup { + set port [sendCommand { set firstblock "" for {set i 0} {$i < 5} {incr i} { set firstblock "a$firstblock$firstblock" @@ -1359,7 +1439,7 @@ test socket-11.13 {testing async write, async flush, async close} \ for {set i 0} {$i < 16} {incr i} { set secondblock "b$secondblock$secondblock" } - set l [socket -server accept 2845] + set l [socket -server accept 0] proc accept {s a p} { fconfigure $s -blocking 0 -translation lf -buffersize 16384 \ -buffering line @@ -1368,143 +1448,138 @@ test socket-11.13 {testing async write, async flush, async close} \ proc readable {s} { set l [gets $s] fileevent $s readable {} - after 1000 respond $s + after idle respond $s } proc respond {s} { global firstblock puts -nonewline $s $firstblock - after 1000 writedata $s + after idle writedata $s } proc writedata {s} { global secondblock puts -nonewline $s $secondblock close $s } + getPort $l + }] + set timer [after 10000 "set done timed_out"] +} -constraints [list socket supported_$af doTestsWithRemoteServer] -body { + proc readit {s} { + global count done + set l [read $s] + incr count [string length $l] + if {[eof $s]} { + close $s + set done 1 + } } - set s [socket $remoteServerIP 2845] + set s [socket $remoteServerIP $port] fconfigure $s -blocking 0 -trans lf -buffering line set count 0 puts $s hello fileevent $s readable "readit $s" - set timer [after 10000 "set done timed_out"] vwait done + return $count +} -cleanup { after cancel $timer sendCommand {close $l} - set count -} 65566 +} -result 65566 set path(script1) [makeFile {} script1] set path(script2) [makeFile {} script2] -test socket-12.1 {testing inheritance of server sockets} {socket stdio exec} { +test socket_$af-12.1 {testing inheritance of server sockets} -setup { file delete $path(script1) file delete $path(script2) - - # Script1 is just a 10 second delay. If the server socket - # is inherited, it will be held open for 10 seconds - + # Script1 is just a 10 second delay. If the server socket is inherited, it + # will be held open for 10 seconds set f [open $path(script1) w] puts $f { + fileevent stdin readable exit after 10000 exit vwait forever } close $f - - # Script2 creates the server socket, launches script1, - # waits a second, and exits. The server socket will now - # be closed unless script1 inherited it. - + # Script2 creates the server socket, launches script1, and exits. + # The server socket will now be closed unless script1 inherited it. set f [open $path(script2) w] puts $f [list set tcltest [interpreter]] - puts -nonewline $f { - set f [socket -server accept -myaddr 127.0.0.1 0] - puts [lindex [fconfigure $f -sockname] 2] + puts $f [list set delay $path(script1)] + puts $f [list set localhost $localhost] + puts $f { + set f [socket -server accept -myaddr $localhost 0] proc accept { file addr port } { close $file } - exec $tcltest } - puts $f [list $path(script1) &] - puts $f { + exec $tcltest $delay & + puts [lindex [fconfigure $f -sockname] 2] close $f - after 1000 exit - vwait forever + exit } close $f - +} -constraints [list socket supported_$af stdio exec] -body { # Launch script2 and wait 5 seconds - ### exec [interpreter] script2 & set p [open "|[list [interpreter] $path(script2)]" r] - gets $p listen - - after 5000 { set ok_to_proceed 1 } - vwait ok_to_proceed - # If we can still connect to the server, the socket got inherited. - - if {[catch {socket 127.0.0.1 $listen} msg]} { - set x {server socket was not inherited} + if {[catch {close [socket $localhost $listen]}]} { + return {server socket was not inherited} } else { - close $msg - set x {server socket was inherited} + return {server socket was inherited} } - - close $p - set x -} {server socket was not inherited} -test socket-12.2 {testing inheritance of client sockets} {socket stdio exec} { +} -cleanup { + catch {close $p} +} -result {server socket was not inherited} +test socket_$af-12.2 {testing inheritance of client sockets} -setup { file delete $path(script1) file delete $path(script2) - - # Script1 is just a 20 second delay. If the server socket - # is inherited, it will be held open for 10 seconds - + # Script1 is just a 20 second delay. If the server socket is inherited, it + # will be held open for 20 seconds set f [open $path(script1) w] puts $f { + fileevent stdin readable exit after 20000 exit vwait forever } close $f - - # Script2 opens the client socket and writes to it. It then - # launches script1 and exits. If the child process inherited the - # client socket, the socket will still be open. - + # Script2 opens the client socket and writes to it. It then launches + # script1 and exits. If the child process inherited the client socket, the + # socket will still be open. set f [open $path(script2) w] puts $f [list set tcltest [interpreter]] - puts -nonewline $f { - gets stdin port - set f [socket 127.0.0.1 $port] - exec $tcltest } - puts $f [list $path(script1) &] + puts $f [list set delay $path(script1)] + puts $f [list set localhost $localhost] puts $f { + gets stdin port + set f [socket $localhost $port] + exec $tcltest $delay & puts $f testing flush $f - after 1000 exit - vwait forever + exit } close $f - + # If the socket doesn't hit end-of-file in 10 seconds, the script1 process + # must have inherited the client. + set failed 0 + set after [after 10000 [list set failed 1]] +} -constraints [list socket supported_$af stdio exec] -body { # Create the server socket - - set server [socket -server accept -myaddr 127.0.0.1 0] + set server [socket -server accept -myaddr $localhost 0] proc accept { file host port } { # When the client connects, establish the read handler global server close $server fileevent $file readable [list getdata $file] fconfigure $file -buffering line -blocking 0 - return } proc getdata { file } { # Read handler on the accepted socket. - global x - global failed + global x failed set status [catch {read $file} data] if {$status != 0} { set x {read failed, error was $data} catch { close $file } - } elseif {[string compare {} $data]} { + } elseif {$data ne ""} { } elseif {[fblocked $file]} { } elseif {[eof $file]} { if {$failed} { @@ -1517,80 +1592,56 @@ test socket-12.2 {testing inheritance of client sockets} {socket stdio exec} { set x {impossible case} catch { close $file } } - return } - - # If the socket doesn't hit end-of-file in 10 seconds, the - # script1 process must have inherited the client. - - set failed 0 - after 10000 [list set failed 1] - # Launch the script2 process ### exec [interpreter] script2 & - set p [open "|[list [interpreter] $path(script2)]" w] puts $p [lindex [fconfigure $server -sockname] 2] ; flush $p - vwait x - if {!$failed} { - vwait failed - } + return $x +} -cleanup { + after cancel $after close $p - set x -} {client socket was not inherited} -test socket-12.3 {testing inheritance of accepted sockets} {socket stdio exec} { +} -result {client socket was not inherited} +test socket_$af-12.3 {testing inheritance of accepted sockets} -setup { file delete $path(script1) file delete $path(script2) - set f [open $path(script1) w] puts $f { + fileevent stdin readable exit after 10000 exit vwait forever } close $f - set f [open $path(script2) w] puts $f [list set tcltest [interpreter]] - puts -nonewline $f { - set server [socket -server accept -myaddr 127.0.0.1 0] - puts stdout [lindex [fconfigure $server -sockname] 2] - proc accept { file host port } } - puts $f \{ - puts -nonewline $f { - global tcltest - puts $file {test data on socket} - exec $tcltest } - puts $f [list $path(script1) &] + puts $f [list set delay $path(script1)] + puts $f [list set localhost $localhost] puts $f { - after 1000 exit + set server [socket -server accept -myaddr $localhost 0] + proc accept { file host port } { + global tcltest delay + puts $file {test data on socket} + exec $tcltest $delay & + after idle exit } - puts $f \} - puts $f { + puts stdout [lindex [fconfigure $server -sockname] 2] vwait forever } close $f - - # Launch the script2 process and connect to it. See how long - # the socket stays open - +} -constraints [list socket supported_$af stdio exec] -body { + # Launch the script2 process and connect to it. See how long the socket + # stays open ## exec [interpreter] script2 & set p [open "|[list [interpreter] $path(script2)]" r] gets $p listen - - after 1000 set ok_to_proceed 1 - vwait ok_to_proceed - - set f [socket 127.0.0.1 $listen] + set f [socket $localhost $listen] fconfigure $f -buffering full -blocking 0 fileevent $f readable [list getdata $f] - - # If the socket is still open after 5 seconds, the script1 process - # must have inherited the accepted socket. - + # If the socket is still open after 5 seconds, the script1 process must + # have inherited the accepted socket. set failed 0 - after 5000 set failed 1 - + set after [after 5000 [list set failed 1]] proc getdata { file } { # Read handler on the client socket. global x @@ -1614,18 +1665,17 @@ test socket-12.3 {testing inheritance of accepted sockets} {socket stdio exec} { } return } - vwait x + return $x +} -cleanup { + after cancel $after + catch {close $p} +} -result {accepted socket was not inherited} - close $p - set x -} {accepted socket was not inherited} - -test socket-13.1 {Testing use of shared socket between two threads} \ - -constraints {socket testthread} -setup { - threadReap - set path(script) [makeFile { - set f [socket -server accept -myaddr 127.0.0.1 0] +test socket_$af-13.1 {Testing use of shared socket between two threads} -body { + # create a thread + set serverthread [thread::create -preserved [string map [list @localhost@ $localhost] { + set f [socket -server accept -myaddr @localhost@ 0] set listen [lindex [fconfigure $f -sockname] 2] proc accept {s a p} { fileevent $s readable [list echo $s] @@ -1646,43 +1696,165 @@ test socket-13.1 {Testing use of shared socket between two threads} \ set i 0 vwait x close $f - # thread cleans itself up. - testthread exit - } script] -} -body { - # create a thread - set serverthread [testthread create [list source $path(script) ] ] - update - set port [testthread send $serverthread {set listen}] - update - - after 1000 - set s [socket 127.0.0.1 $port] + thread::wait + }]] + set port [thread::send $serverthread {set listen}] + set s [socket $localhost $port] fconfigure $s -buffering line - catch { puts $s "hello" gets $s result } close $s - update - - after 2000 - lappend result [threadReap] -} -cleanup { - removeFile script -} -result {hello 1} + thread::release $serverthread + append result " " [llength [thread::names]] +} -result {hello 1} -constraints [list socket supported_$af thread] + +# ---------------------------------------------------------------------- removeFile script1 removeFile script2 # cleanup -if {[string match sock* $commandSocket] == 1} { - puts $commandSocket exit - flush $commandSocket +if {$remoteProcChan ne ""} { + catch {sendCommand exit} } catch {close $commandSocket} catch {close $remoteProcChan} +} +unset ::tcl::unsupported::socketAF +test socket-14.0 {[socket -async] when server only listens on IPv4} \ + -constraints [list socket supported_any localhost_v4] \ + -setup { + proc accept {s a p} { + global x + puts $s bye + close $s + set x ok + } + set server [socket -server accept -myaddr 127.0.0.1 0] + set port [lindex [fconfigure $server -sockname] 2] + } -body { + set client [socket -async localhost $port] + set after [after 1000 {set x [fconfigure $client -error]}] + vwait x + set x + } -cleanup { + after cancel $after + close $server + close $client + unset x + } -result ok +test socket-14.1 {[socket -async] fileevent while still connecting} \ + -constraints [list socket supported_any] \ + -setup { + proc accept {s a p} { + global x + puts $s bye + close $s + lappend x ok + } + set server [socket -server accept -myaddr localhost 0] + set port [lindex [fconfigure $server -sockname] 2] + set x "" + } -body { + set client [socket -async localhost $port] + fileevent $client writable { + lappend x [fconfigure $client -error] + fileevent $client writable {} + } + set after [after 1000 {lappend x timeout}] + while {[llength $x] < 2 && "timeout" ni $x} { + vwait x + } + lsort $x; # we only want to see both events, the order doesn't matter + } -cleanup { + after cancel $after + close $server + close $client + unset x + } -result {{} ok} +test socket-14.2 {[socket -async] fileevent connection refused} \ + -constraints [list socket supported_any] \ + -body { + if {[catch {socket -async localhost [randport]} client]} { + regexp {[^:]*: (.*)} $client -> x + } else { + fileevent $client writable {set x [fconfigure $client -error]} + set after [after 1000 {set x timeout}] + vwait x + after cancel $after + if {$x eq "timeout"} { + append x ": [fconfigure $client -error]" + } + close $client + } + set x + } -cleanup { + unset x + } -result "connection refused" +test socket-14.3 {[socket -async] when server only listens on IPv6} \ + -constraints [list socket supported_any localhost_v6] \ + -setup { + proc accept {s a p} { + global x + puts $s bye + close $s + set x ok + } + set server [socket -server accept -myaddr ::1 0] + set port [lindex [fconfigure $server -sockname] 2] + } -body { + set client [socket -async localhost $port] + set after [after 1000 {set x [fconfigure $client -error]}] + vwait x + set x + } -cleanup { + after cancel $after + close $server + close $client + unset x + } -result ok +test socket-14.4 {[socket -async] and both, readdable and writable fileevents} \ + -constraints [list socket supported_any] \ + -setup { + proc accept {s a p} { + puts $s bye + close $s + } + set server [socket -server accept -myaddr localhost 0] + set port [lindex [fconfigure $server -sockname] 2] + set x "" + } -body { + set client [socket -async localhost $port] + fileevent $client writable { + lappend x [fconfigure $client -error] + fileevent $client writable {} + } + fileevent $client readable {lappend x [gets $client]} + set after [after 1000 {lappend x timeout}] + while {[llength $x] < 2 && "timeout" ni $x} { + vwait x + } + lsort $x + } -cleanup { + after cancel $after + close $client + close $server + } -result {{} bye} +test socket-14.5 {[socket -async] which fails before any connect() can be made} \ + -constraints [list socket supported_any] \ + -body { + # address from rfc5737 + socket -async -myaddr 192.0.2.42 127.0.0.1 [randport] + } \ + -returnCodes 1 \ + -result {couldn't open socket: cannot assign requested address} ::tcltest::cleanupTests flush stdout return + +# Local Variables: +# mode: tcl +# fill-column: 78 +# End: |