📄 socket.test
字号:
# 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.## Copyright (c) 1994-1996 Sun Microsystems, Inc.## 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:# ------------------------------------------# # Some tests in socket.test depend on the existence of a remote server to# which they connect. The remote server must be an instance of tcltest and it# must run the script found in the file "remote.tcl" in this directory. You# can start the remote server on any machine reachable from the machine on# which you want to run the socket tests, by issuing:# # tcltest remote.tcl -port 2048 # Or choose another port number.# # If the machine you are running the remote server on has several IP# interfaces, you can choose which interface the server listens on for# connections by specifying the -address command line flag, so:# # tcltest remote.tcl -address your.machine.com# # These options can also be set by environment variables. On Unix, you can# type these commands to the shell from which the remote server is started:# # shell% setenv serverPort 2048# shell% setenv serverAddress your.machine.com# # and subsequently you can start the remote server with:# # tcltest remote.tcl# # to have it listen on port 2048 on the interface your.machine.com.# # 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:# # % set remoteServerIP <name or address of machine on which server runs># % set remoteServerPort 2048# # These variables are also settable from the environment. On Unix, you can:# # shell% setenv remoteServerIP machine.where.server.runs# shell% senetv remoteServerPort 2048# # The preamble of the socket.test file checks to see if the variables are set# either in Tcl or in the environment; if they are, it attempts to connect to# the server. If the connection is successful, the tests using the remote# server will be performed; otherwise, it will attempt to start the remote# server (via exec) on platforms that support this, on the local host,# listening at port 2048. If all fails, a message is printed and the tests# using the remote server are not performed.## SCCS: @(#) socket.test 1.83 97/09/15 16:29:47if {[string compare test [info procs test]] == 1} then {source defs}if {$testConfig(socket) == 0} { return}## If remoteServerIP or remoteServerPort are not set, check in the# environment variables for externally set values.#if {![info exists remoteServerIP]} { if {[info exists env(remoteServerIP)]} { set remoteServerIP $env(remoteServerIP) }}if {![info exists remoteServerPort]} { if {[info exists env(remoteServerIP)]} { set remoteServerPort $env(remoteServerPort) } else { if {[info exists remoteServerIP]} { set remoteServerPort 2048 } }}## Check if we're supposed to do tests against the remote server#set doTestsWithRemoteServer 1if {![info exists remoteServerIP] && ($tcl_platform(platform) != "macintosh")} { set remoteServerIP localhost}if {($doTestsWithRemoteServer == 1) && (![info exists remoteServerPort])} { set remoteServerPort 2048}# 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 ""if {$doTestsWithRemoteServer} { catch {close $commandSocket} if {[catch {set commandSocket [socket $remoteServerIP \ $remoteServerPort]}] != 0} { if {[info commands exec] == ""} { set noRemoteTestReason "can't exec" set doTestsWithRemoteServer 0 } elseif {$testConfig(win32s)} { set noRemoteTestReason "\ncan't run multiple instances of tcltest under win32s." set doTestsWithRemoteServer 0 } else { set remoteServerIP localhost if {[catch {set remoteProcChan \ [open "|[list $tcltest remote.tcl \ -serverIsSilent \ -port $remoteServerPort \ -address $remoteServerIP]" \ w+]} \ msg] == 0} { after 1000 if {[catch {set commandSocket [socket $remoteServerIP \ $remoteServerPort]} msg] == 0} { fconfigure $commandSocket -translation crlf -buffering line } else { set noRemoteTestReason $msg set doTestsWithRemoteServer 0 } } else { set noRemoteTestReason "$msg $tcltest" set doTestsWithRemoteServer 0 } } } else { fconfigure $commandSocket -translation crlf -buffering line }}if {$doTestsWithRemoteServer == 0} { puts "Skipping tests with remote server. See tests/socket.test for" puts "information on how to run remote server." if {[info exists VERBOSE] && ($VERBOSE != 0)} { puts "Reason for not doing remote tests: $noRemoteTestReason" }}## If we do the tests, define a command to send a command to the# remote server.#if {$doTestsWithRemoteServer == 1} { proc sendCommand {c} { global commandSocket if {[eof $commandSocket]} { error "remote server disappeared" } if {[catch {puts $commandSocket $c} msg]} { error "remote server disappaered: $msg" } if {[catch {puts $commandSocket "--Marker--Marker--Marker--"} msg]} { 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" } } }}test socket-1.1 {arg parsing for socket command} { list [catch {socket -server} msg] $msg} {1 {no argument given for -server option}}test socket-1.2 {arg parsing for socket command} { list [catch {socket -server foo} msg] $msg} {1 {wrong # args: should be either:socket ?-myaddr addr? ?-myport myport? ?-async? host portsocket -server command ?-myaddr addr? port}}test socket-1.3 {arg parsing for socket command} { list [catch {socket -myaddr} msg] $msg} {1 {no argument given for -myaddr option}}test socket-1.4 {arg parsing for socket command} { list [catch {socket -myaddr 127.0.0.1} msg] $msg} {1 {wrong # args: should be either:socket ?-myaddr addr? ?-myport myport? ?-async? host portsocket -server command ?-myaddr addr? port}}test socket-1.5 {arg parsing for socket command} { list [catch {socket -myport} msg] $msg} {1 {no argument given for -myport option}}test socket-1.6 {arg parsing for socket command} { list [catch {socket -myport xxxx} msg] $msg} {1 {expected integer but got "xxxx"}}test socket-1.7 {arg parsing for socket command} { list [catch {socket -myport 2522} msg] $msg} {1 {wrong # args: should be either:socket ?-myaddr addr? ?-myport myport? ?-async? host portsocket -server command ?-myaddr addr? port}}test socket-1.8 {arg parsing for socket command} { 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} { 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} { list [catch {socket host 2528 -junk} msg] $msg} {1 {wrong # args: should be either:socket ?-myaddr addr? ?-myport myport? ?-async? host portsocket -server command ?-myaddr addr? port}}test socket-1.11 {arg parsing for socket command} { list [catch {socket -server callback 2520 --} msg] $msg} {1 {wrong # args: should be either:socket ?-myaddr addr? ?-myport myport? ?-async? host portsocket -server command ?-myaddr addr? port}}test socket-1.12 {arg parsing for socket command} { list [catch {socket foo badport} msg] $msg} {1 {expected integer but got "badport"}}test socket-2.1 {tcp connection} {stdio} { removeFile script set f [open script w] puts $f { set timer [after 2000 "set x timed_out"] set f [socket -server accept 2828] proc accept {file addr port} { global x set x done close $file } puts ready vwait x after cancel $timer close $f puts $x } close $f set f [open "|[list $tcltest script]" r] gets $f x if {[catch {socket localhost 2828} msg]} { set x $msg } else { lappend x [gets $f] close $msg } lappend x [gets $f] 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} {stdio} { removeFile script set f [open script w] puts $f { set timer [after 2000 "set x done"] set f [socket -server accept 2828] proc accept {file addr port} { global x puts "[gets $file] $port" close $file set x done } puts ready vwait x after cancel $timer close $f } close $f set f [open "|[list $tcltest script]" r] gets $f x global port if {[catch {socket -myport $port localhost 2828} sock]} { set x $sock close [socket localhost 2828] puts stderr $sock } else { puts $sock hello flush $sock lappend x [gets $f] close $sock } close $f set x} [list ready "hello $port"]test socket-2.3 {tcp connection with client interface specified} {stdio} { removeFile script set f [open script w] puts $f { set timer [after 2000 "set x done"] set f [socket -server accept 2828] proc accept {file addr port} { global x puts "[gets $file] $addr" close $file set x done } puts ready vwait x after cancel $timer close $f } close $f set f [open "|[list $tcltest script]" r] gets $f x if {[catch {socket -myaddr localhost localhost 2828} sock]} { set x $sock } else { puts $sock hello flush $sock lappend x [gets $f] close $sock } close $f set x} {ready {hello 127.0.0.1}}test socket-2.4 {tcp connection with server interface specified} {stdio} { removeFile script set f [open script w] puts $f { set timer [after 2000 "set x done"] set f [socket -server accept -myaddr [info hostname] 2828] proc accept {file addr port} { global x puts "[gets $file]" close $file set x done } puts ready vwait x after cancel $timer close $f } close $f set f [open "|[list $tcltest script]" r] gets $f x if {[catch {socket [info hostname] 2828} sock]} { set x $sock } else { puts $sock hello flush $sock lappend x [gets $f] close $sock } close $f set x} {ready hello}test socket-2.5 {tcp connection with redundant server port} {stdio} { removeFile script set f [open script w] puts $f { set timer [after 2000 "set x done"] set f [socket -server accept 2828] proc accept {file addr port} { global x puts "[gets $file]" close $file set x done } puts ready vwait x after cancel $timer close $f } close $f set f [open "|[list $tcltest script]" r] gets $f x if {[catch {socket localhost 2828} sock]} { set x $sock } else { puts $sock hello flush $sock lappend x [gets $f] close $sock } close $f set x} {ready hello}test socket-2.6 {tcp connection} {} { set status ok if {![catch {set sock [socket localhost 2828]}]} { if {![catch {gets $sock}]} { set status broken } close $sock } set status} oktest socket-2.7 {echo server, one line} {stdio} { removeFile script set f [open script w] puts $f { set timer [after 2000 "set x done"] set f [socket -server accept 2828] proc accept {s a p} { fileevent $s readable [list echo $s] fconfigure $s -translation lf -buffering line } proc echo {s} { set l [gets $s] if {[eof $s]} { global x close $s set x done } else { puts $s $l } } puts ready vwait x after cancel $timer close $f puts done } close $f set f [open "|[list $tcltest script]" r] gets $f set s [socket localhost 2828] fconfigure $s -buffering line -translation lf puts $s "hello abcdefghijklmnop" set x [gets $s] close $s set y [gets $f] close $f list $x $y} {{hello abcdefghijklmnop} done}test socket-2.8 {echo server, loop 50 times, single connection} {stdio} { removeFile script set f [open script w] puts $f { set f [socket -server accept 2828] proc accept {s a p} { fileevent $s readable [list echo $s] fconfigure $s -buffering line }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -