PageRenderTime 70ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/gnu/tcl/tests/socket.test

https://bitbucket.org/staceyoi/bb101repo
Unknown | 1381 lines | 1348 code | 33 blank | 0 comment | 0 complexity | 6e7772a8a143a5045d3a625bafdb3538 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, AGPL-3.0
  1. # Commands tested in this file: socket.
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands. Sourcing this file into Tcl runs the tests and
  5. # generates output for errors. No output means no errors were found.
  6. #
  7. # Copyright (c) 1994-1996 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. # Running socket tests with a remote server:
  13. # ------------------------------------------
  14. #
  15. # Some tests in socket.test depend on the existence of a remote server to
  16. # which they connect. The remote server must be an instance of tcltest and it
  17. # must run the script found in the file "remote.tcl" in this directory. You
  18. # can start the remote server on any machine reachable from the machine on
  19. # which you want to run the socket tests, by issuing:
  20. #
  21. # tcltest remote.tcl -port 2048 # Or choose another port number.
  22. #
  23. # If the machine you are running the remote server on has several IP
  24. # interfaces, you can choose which interface the server listens on for
  25. # connections by specifying the -address command line flag, so:
  26. #
  27. # tcltest remote.tcl -address your.machine.com
  28. #
  29. # These options can also be set by environment variables. On Unix, you can
  30. # type these commands to the shell from which the remote server is started:
  31. #
  32. # shell% setenv serverPort 2048
  33. # shell% setenv serverAddress your.machine.com
  34. #
  35. # and subsequently you can start the remote server with:
  36. #
  37. # tcltest remote.tcl
  38. #
  39. # to have it listen on port 2048 on the interface your.machine.com.
  40. #
  41. # When the server starts, it prints out a detailed message containing its
  42. # configuration information, and it will block until killed with a Ctrl-C.
  43. # Once the remote server exists, you can run the tests in socket.test with
  44. # the server by setting two Tcl variables:
  45. #
  46. # % set remoteServerIP <name or address of machine on which server runs>
  47. # % set remoteServerPort 2048
  48. #
  49. # These variables are also settable from the environment. On Unix, you can:
  50. #
  51. # shell% setenv remoteServerIP machine.where.server.runs
  52. # shell% senetv remoteServerPort 2048
  53. #
  54. # The preamble of the socket.test file checks to see if the variables are set
  55. # either in Tcl or in the environment; if they are, it attempts to connect to
  56. # the server. If the connection is successful, the tests using the remote
  57. # server will be performed; otherwise, it will attempt to start the remote
  58. # server (via exec) on platforms that support this, on the local host,
  59. # listening at port 2048. If all fails, a message is printed and the tests
  60. # using the remote server are not performed.
  61. #
  62. # RCS: @(#) $Id: socket.test,v 1.12 1999/01/26 03:53:34 jingham Exp $
  63. if {[string compare test [info procs test]] == 1} then {source defs}
  64. if {$testConfig(socket) == 0} {
  65. return
  66. }
  67. #
  68. # If remoteServerIP or remoteServerPort are not set, check in the
  69. # environment variables for externally set values.
  70. #
  71. if {![info exists remoteServerIP]} {
  72. if {[info exists env(remoteServerIP)]} {
  73. set remoteServerIP $env(remoteServerIP)
  74. }
  75. }
  76. if {![info exists remoteServerPort]} {
  77. if {[info exists env(remoteServerIP)]} {
  78. set remoteServerPort $env(remoteServerPort)
  79. } else {
  80. if {[info exists remoteServerIP]} {
  81. set remoteServerPort 2048
  82. }
  83. }
  84. }
  85. #
  86. # Check if we're supposed to do tests against the remote server
  87. #
  88. set doTestsWithRemoteServer 1
  89. if {![info exists remoteServerIP] && ($tcl_platform(platform) != "macintosh")} {
  90. set remoteServerIP localhost
  91. }
  92. if {($doTestsWithRemoteServer == 1) && (![info exists remoteServerPort])} {
  93. set remoteServerPort 2048
  94. }
  95. # Attempt to connect to a remote server if one is already running. If it
  96. # is not running or for some other reason the connect fails, attempt to
  97. # start the remote server on the local host listening on port 2048. This
  98. # is only done on platforms that support exec (i.e. not on the Mac). On
  99. # platforms that do not support exec, the remote server must be started
  100. # by the user before running the tests.
  101. set remoteProcChan ""
  102. set commandSocket ""
  103. if {$doTestsWithRemoteServer} {
  104. catch {close $commandSocket}
  105. if {[catch {set commandSocket [socket $remoteServerIP \
  106. $remoteServerPort]}] != 0} {
  107. if {[info commands exec] == ""} {
  108. set noRemoteTestReason "can't exec"
  109. set doTestsWithRemoteServer 0
  110. } elseif {$testConfig(win32s)} {
  111. set noRemoteTestReason "\ncan't run multiple instances of tcltest under win32s."
  112. set doTestsWithRemoteServer 0
  113. } else {
  114. set remoteServerIP localhost
  115. if {[catch {set remoteProcChan \
  116. [open "|[list $tcltest remote.tcl \
  117. -serverIsSilent \
  118. -port $remoteServerPort \
  119. -address $remoteServerIP]" \
  120. w+]} \
  121. msg] == 0} {
  122. after 1000
  123. if {[catch {set commandSocket [socket $remoteServerIP \
  124. $remoteServerPort]} msg] == 0} {
  125. fconfigure $commandSocket -translation crlf -buffering line
  126. } else {
  127. set noRemoteTestReason $msg
  128. set doTestsWithRemoteServer 0
  129. }
  130. } else {
  131. set noRemoteTestReason "$msg $tcltest"
  132. set doTestsWithRemoteServer 0
  133. }
  134. }
  135. } else {
  136. fconfigure $commandSocket -translation crlf -buffering line
  137. }
  138. }
  139. if {$doTestsWithRemoteServer == 0} {
  140. puts "Skipping tests with remote server. See tests/socket.test for"
  141. puts "information on how to run remote server."
  142. if {[info exists VERBOSE] && ($VERBOSE != 0)} {
  143. puts "Reason for not doing remote tests: $noRemoteTestReason"
  144. }
  145. }
  146. #
  147. # If we do the tests, define a command to send a command to the
  148. # remote server.
  149. #
  150. if {$doTestsWithRemoteServer == 1} {
  151. proc sendCommand {c} {
  152. global commandSocket
  153. if {[eof $commandSocket]} {
  154. error "remote server disappeared"
  155. }
  156. if {[catch {puts $commandSocket $c} msg]} {
  157. error "remote server disappaered: $msg"
  158. }
  159. if {[catch {puts $commandSocket "--Marker--Marker--Marker--"} msg]} {
  160. error "remote server disappeared: $msg"
  161. }
  162. set resp ""
  163. while {1} {
  164. set line [gets $commandSocket]
  165. if {[eof $commandSocket]} {
  166. error "remote server disappaered"
  167. }
  168. if {[string compare $line "--Marker--Marker--Marker--"] == 0} {
  169. if {[string compare [lindex $resp 0] error] == 0} {
  170. error [lindex $resp 1]
  171. } else {
  172. return [lindex $resp 1]
  173. }
  174. } else {
  175. append resp $line "\n"
  176. }
  177. }
  178. }
  179. }
  180. test socket-1.1 {arg parsing for socket command} {
  181. list [catch {socket -server} msg] $msg
  182. } {1 {no argument given for -server option}}
  183. test socket-1.2 {arg parsing for socket command} {
  184. list [catch {socket -server foo} msg] $msg
  185. } {1 {wrong # args: should be either:
  186. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  187. socket -server command ?-myaddr addr? port}}
  188. test socket-1.3 {arg parsing for socket command} {
  189. list [catch {socket -myaddr} msg] $msg
  190. } {1 {no argument given for -myaddr option}}
  191. test socket-1.4 {arg parsing for socket command} {
  192. list [catch {socket -myaddr 127.0.0.1} msg] $msg
  193. } {1 {wrong # args: should be either:
  194. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  195. socket -server command ?-myaddr addr? port}}
  196. test socket-1.5 {arg parsing for socket command} {
  197. list [catch {socket -myport} msg] $msg
  198. } {1 {no argument given for -myport option}}
  199. test socket-1.6 {arg parsing for socket command} {
  200. list [catch {socket -myport xxxx} msg] $msg
  201. } {1 {expected integer but got "xxxx"}}
  202. test socket-1.7 {arg parsing for socket command} {
  203. list [catch {socket -myport 2522} msg] $msg
  204. } {1 {wrong # args: should be either:
  205. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  206. socket -server command ?-myaddr addr? port}}
  207. test socket-1.8 {arg parsing for socket command} {
  208. list [catch {socket -froboz} msg] $msg
  209. } {1 {bad option "-froboz", must be -async, -myaddr, -myport, or -server}}
  210. test socket-1.9 {arg parsing for socket command} {
  211. list [catch {socket -server foo -myport 2521 3333} msg] $msg
  212. } {1 {Option -myport is not valid for servers}}
  213. test socket-1.10 {arg parsing for socket command} {
  214. list [catch {socket host 2528 -junk} msg] $msg
  215. } {1 {wrong # args: should be either:
  216. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  217. socket -server command ?-myaddr addr? port}}
  218. test socket-1.11 {arg parsing for socket command} {
  219. list [catch {socket -server callback 2520 --} msg] $msg
  220. } {1 {wrong # args: should be either:
  221. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  222. socket -server command ?-myaddr addr? port}}
  223. test socket-1.12 {arg parsing for socket command} {
  224. list [catch {socket foo badport} msg] $msg
  225. } {1 {expected integer but got "badport"}}
  226. test socket-2.1 {tcp connection} {stdio} {
  227. removeFile script
  228. set f [open script w]
  229. puts $f {
  230. set timer [after 2000 "set x timed_out"]
  231. set f [socket -server accept 2828]
  232. proc accept {file addr port} {
  233. global x
  234. set x done
  235. close $file
  236. }
  237. puts ready
  238. vwait x
  239. after cancel $timer
  240. close $f
  241. puts $x
  242. }
  243. close $f
  244. set f [open "|[list $tcltest script]" r]
  245. gets $f x
  246. if {[catch {socket localhost 2828} msg]} {
  247. set x $msg
  248. } else {
  249. lappend x [gets $f]
  250. close $msg
  251. }
  252. lappend x [gets $f]
  253. close $f
  254. set x
  255. } {ready done {}}
  256. if [info exists port] {
  257. incr port
  258. } else {
  259. set port [expr 2048 + [pid]%1024]
  260. }
  261. test socket-2.2 {tcp connection with client port specified} {stdio} {
  262. removeFile script
  263. set f [open script w]
  264. puts $f {
  265. set timer [after 2000 "set x done"]
  266. set f [socket -server accept 2828]
  267. proc accept {file addr port} {
  268. global x
  269. puts "[gets $file] $port"
  270. close $file
  271. set x done
  272. }
  273. puts ready
  274. vwait x
  275. after cancel $timer
  276. close $f
  277. }
  278. close $f
  279. set f [open "|[list $tcltest script]" r]
  280. gets $f x
  281. global port
  282. if {[catch {socket -myport $port localhost 2828} sock]} {
  283. set x $sock
  284. close [socket localhost 2828]
  285. puts stderr $sock
  286. } else {
  287. puts $sock hello
  288. flush $sock
  289. lappend x [gets $f]
  290. close $sock
  291. }
  292. close $f
  293. set x
  294. } [list ready "hello $port"]
  295. test socket-2.3 {tcp connection with client interface specified} {stdio} {
  296. removeFile script
  297. set f [open script w]
  298. puts $f {
  299. set timer [after 2000 "set x done"]
  300. set f [socket -server accept 2828]
  301. proc accept {file addr port} {
  302. global x
  303. puts "[gets $file] $addr"
  304. close $file
  305. set x done
  306. }
  307. puts ready
  308. vwait x
  309. after cancel $timer
  310. close $f
  311. }
  312. close $f
  313. set f [open "|[list $tcltest script]" r]
  314. gets $f x
  315. if {[catch {socket -myaddr localhost localhost 2828} sock]} {
  316. set x $sock
  317. } else {
  318. puts $sock hello
  319. flush $sock
  320. lappend x [gets $f]
  321. close $sock
  322. }
  323. close $f
  324. set x
  325. } {ready {hello 127.0.0.1}}
  326. test socket-2.4 {tcp connection with server interface specified} {stdio} {
  327. removeFile script
  328. set f [open script w]
  329. puts $f {
  330. set timer [after 2000 "set x done"]
  331. set f [socket -server accept -myaddr [info hostname] 2828]
  332. proc accept {file addr port} {
  333. global x
  334. puts "[gets $file]"
  335. close $file
  336. set x done
  337. }
  338. puts ready
  339. vwait x
  340. after cancel $timer
  341. close $f
  342. }
  343. close $f
  344. set f [open "|[list $tcltest script]" r]
  345. gets $f x
  346. if {[catch {socket [info hostname] 2828} sock]} {
  347. set x $sock
  348. } else {
  349. puts $sock hello
  350. flush $sock
  351. lappend x [gets $f]
  352. close $sock
  353. }
  354. close $f
  355. set x
  356. } {ready hello}
  357. test socket-2.5 {tcp connection with redundant server port} {stdio} {
  358. removeFile script
  359. set f [open script w]
  360. puts $f {
  361. set timer [after 2000 "set x done"]
  362. set f [socket -server accept 2828]
  363. proc accept {file addr port} {
  364. global x
  365. puts "[gets $file]"
  366. close $file
  367. set x done
  368. }
  369. puts ready
  370. vwait x
  371. after cancel $timer
  372. close $f
  373. }
  374. close $f
  375. set f [open "|[list $tcltest script]" r]
  376. gets $f x
  377. if {[catch {socket localhost 2828} sock]} {
  378. set x $sock
  379. } else {
  380. puts $sock hello
  381. flush $sock
  382. lappend x [gets $f]
  383. close $sock
  384. }
  385. close $f
  386. set x
  387. } {ready hello}
  388. test socket-2.6 {tcp connection} {} {
  389. set status ok
  390. if {![catch {set sock [socket localhost 2828]}]} {
  391. if {![catch {gets $sock}]} {
  392. set status broken
  393. }
  394. close $sock
  395. }
  396. set status
  397. } ok
  398. test socket-2.7 {echo server, one line} {stdio} {
  399. removeFile script
  400. set f [open script w]
  401. puts $f {
  402. set timer [after 2000 "set x done"]
  403. set f [socket -server accept 2828]
  404. proc accept {s a p} {
  405. fileevent $s readable [list echo $s]
  406. fconfigure $s -translation lf -buffering line
  407. }
  408. proc echo {s} {
  409. set l [gets $s]
  410. if {[eof $s]} {
  411. global x
  412. close $s
  413. set x done
  414. } else {
  415. puts $s $l
  416. }
  417. }
  418. puts ready
  419. vwait x
  420. after cancel $timer
  421. close $f
  422. puts done
  423. }
  424. close $f
  425. set f [open "|[list $tcltest script]" r]
  426. gets $f
  427. set s [socket localhost 2828]
  428. fconfigure $s -buffering line -translation lf
  429. puts $s "hello abcdefghijklmnop"
  430. set x [gets $s]
  431. close $s
  432. set y [gets $f]
  433. close $f
  434. list $x $y
  435. } {{hello abcdefghijklmnop} done}
  436. test socket-2.8 {echo server, loop 50 times, single connection} {stdio} {
  437. removeFile script
  438. set f [open script w]
  439. puts $f {
  440. set f [socket -server accept 2828]
  441. proc accept {s a p} {
  442. fileevent $s readable [list echo $s]
  443. fconfigure $s -buffering line
  444. }
  445. proc echo {s} {
  446. global i
  447. set l [gets $s]
  448. if {[eof $s]} {
  449. global x
  450. close $s
  451. set x done
  452. } else {
  453. incr i
  454. puts $s $l
  455. }
  456. }
  457. set i 0
  458. puts ready
  459. set timer [after 20000 "set x done"]
  460. vwait x
  461. after cancel $timer
  462. close $f
  463. puts "done $i"
  464. }
  465. close $f
  466. set f [open "|[list $tcltest script]" r]
  467. gets $f
  468. set s [socket localhost 2828]
  469. fconfigure $s -buffering line
  470. for {set x 0} {$x < 50} {incr x} {
  471. puts $s "hello abcdefghijklmnop"
  472. gets $s
  473. }
  474. close $s
  475. set x [gets $f]
  476. close $f
  477. set x
  478. } {done 50}
  479. test socket-2.9 {socket conflict} {stdio} {
  480. set s [socket -server accept 2828]
  481. removeFile script
  482. set f [open script w]
  483. puts $f {set f [socket -server accept 2828]}
  484. close $f
  485. set f [open "|[list $tcltest script]" r]
  486. gets $f
  487. after 100
  488. set x [list [catch {close $f} msg] $msg]
  489. close $s
  490. set x
  491. } {1 {couldn't open socket: address already in use
  492. while executing
  493. "socket -server accept 2828"
  494. (file "script" line 1)}}
  495. test socket-2.10 {close on accept, accepted socket lives} {
  496. set done 0
  497. set timer [after 20000 "set done timed_out"]
  498. set ss [socket -server accept 2830]
  499. proc accept {s a p} {
  500. global ss
  501. close $ss
  502. fileevent $s readable "readit $s"
  503. fconfigure $s -trans lf
  504. }
  505. proc readit {s} {
  506. global done
  507. gets $s
  508. close $s
  509. set done 1
  510. }
  511. set cs [socket [info hostname] 2830]
  512. puts $cs hello
  513. close $cs
  514. vwait done
  515. after cancel $timer
  516. set done
  517. } 1
  518. test socket-2.11 {detecting new data} {
  519. proc accept {s a p} {
  520. global sock
  521. set sock $s
  522. }
  523. set s [socket -server accept 2400]
  524. set sock ""
  525. set s2 [socket localhost 2400]
  526. vwait sock
  527. puts $s2 one
  528. flush $s2
  529. after 500
  530. fconfigure $sock -blocking 0
  531. set result [gets $sock]
  532. lappend result [gets $sock]
  533. fconfigure $sock -blocking 1
  534. puts $s2 two
  535. flush $s2
  536. fconfigure $sock -blocking 0
  537. lappend result [gets $sock]
  538. fconfigure $sock -blocking 1
  539. close $s2
  540. close $s
  541. close $sock
  542. set result
  543. } {one {} two}
  544. test socket-3.1 {socket conflict} {stdio} {
  545. removeFile script
  546. set f [open script w]
  547. puts $f {
  548. set f [socket -server accept 2828]
  549. puts ready
  550. gets stdin
  551. close $f
  552. }
  553. close $f
  554. set f [open "|[list $tcltest script]" r+]
  555. gets $f
  556. set x [list [catch {socket -server accept 2828} msg] \
  557. $msg]
  558. puts $f bye
  559. close $f
  560. set x
  561. } {1 {couldn't open socket: address already in use}}
  562. test socket-3.2 {server with several clients} {stdio} {
  563. removeFile script
  564. set f [open script w]
  565. puts $f {
  566. set t1 [after 30000 "set x timed_out"]
  567. set t2 [after 31000 "set x timed_out"]
  568. set t3 [after 32000 "set x timed_out"]
  569. set counter 0
  570. set s [socket -server accept 2828]
  571. proc accept {s a p} {
  572. fileevent $s readable [list echo $s]
  573. fconfigure $s -buffering line
  574. }
  575. proc echo {s} {
  576. global x
  577. set l [gets $s]
  578. if {[eof $s]} {
  579. close $s
  580. set x done
  581. } else {
  582. puts $s $l
  583. }
  584. }
  585. puts ready
  586. vwait x
  587. after cancel $t1
  588. vwait x
  589. after cancel $t2
  590. vwait x
  591. after cancel $t3
  592. close $s
  593. puts $x
  594. }
  595. close $f
  596. set f [open "|[list $tcltest script]" r+]
  597. set x [gets $f]
  598. set s1 [socket localhost 2828]
  599. fconfigure $s1 -buffering line
  600. set s2 [socket localhost 2828]
  601. fconfigure $s2 -buffering line
  602. set s3 [socket localhost 2828]
  603. fconfigure $s3 -buffering line
  604. for {set i 0} {$i < 100} {incr i} {
  605. puts $s1 hello,s1
  606. gets $s1
  607. puts $s2 hello,s2
  608. gets $s2
  609. puts $s3 hello,s3
  610. gets $s3
  611. }
  612. close $s1
  613. close $s2
  614. close $s3
  615. lappend x [gets $f]
  616. close $f
  617. set x
  618. } {ready done}
  619. test socket-4.1 {server with several clients} {stdio} {
  620. removeFile script
  621. set f [open script w]
  622. puts $f {
  623. gets stdin
  624. set s [socket localhost 2828]
  625. fconfigure $s -buffering line
  626. for {set i 0} {$i < 100} {incr i} {
  627. puts $s hello
  628. gets $s
  629. }
  630. close $s
  631. puts bye
  632. gets stdin
  633. }
  634. close $f
  635. set p1 [open "|[list $tcltest script]" r+]
  636. fconfigure $p1 -buffering line
  637. set p2 [open "|[list $tcltest script]" r+]
  638. fconfigure $p2 -buffering line
  639. set p3 [open "|[list $tcltest script]" r+]
  640. fconfigure $p3 -buffering line
  641. proc accept {s a p} {
  642. fconfigure $s -buffering line
  643. fileevent $s readable [list echo $s]
  644. }
  645. proc echo {s} {
  646. global x
  647. set l [gets $s]
  648. if {[eof $s]} {
  649. close $s
  650. set x done
  651. } else {
  652. puts $s $l
  653. }
  654. }
  655. set t1 [after 30000 "set x timed_out"]
  656. set t2 [after 31000 "set x timed_out"]
  657. set t3 [after 32000 "set x timed_out"]
  658. set s [socket -server accept 2828]
  659. puts $p1 open
  660. puts $p2 open
  661. puts $p3 open
  662. vwait x
  663. vwait x
  664. vwait x
  665. after cancel $t1
  666. after cancel $t2
  667. after cancel $t3
  668. close $s
  669. set l ""
  670. lappend l [list p1 [gets $p1] $x]
  671. lappend l [list p2 [gets $p2] $x]
  672. lappend l [list p3 [gets $p3] $x]
  673. puts $p1 bye
  674. puts $p2 bye
  675. puts $p3 bye
  676. close $p1
  677. close $p2
  678. close $p3
  679. set l
  680. } {{p1 bye done} {p2 bye done} {p3 bye done}}
  681. test socket-4.2 {byte order problems, socket numbers, htons} {
  682. set x ok
  683. if {[catch {socket -server dodo 0x3000} msg]} {
  684. set x $msg
  685. } else {
  686. close $msg
  687. }
  688. set x
  689. } ok
  690. test socket-5.1 {byte order problems, socket numbers, htons} {unixOnly} {
  691. #
  692. # THIS TEST WILL FAIL if you are running as superuser.
  693. #
  694. set x {couldn't open socket: not owner}
  695. if {![catch {socket -server dodo 0x1} msg]} {
  696. set x {htons problem, should be disallowed, are you running as SU?}
  697. close $msg
  698. }
  699. set x
  700. } {couldn't open socket: not owner}
  701. test socket-5.2 {byte order problems, socket numbers, htons} {
  702. set x {couldn't open socket: port number too high}
  703. if {![catch {socket -server dodo 0x10000} msg]} {
  704. set x {port resolution problem, should be disallowed}
  705. close $msg
  706. }
  707. set x
  708. } {couldn't open socket: port number too high}
  709. test socket-5.3 {byte order problems, socket numbers, htons} {unixOnly} {
  710. #
  711. # THIS TEST WILL FAIL if you are running as superuser.
  712. #
  713. set x {couldn't open socket: not owner}
  714. if {![catch {socket -server dodo 21} msg]} {
  715. set x {htons problem, should be disallowed, are you running as SU?}
  716. close $msg
  717. }
  718. set x
  719. } {couldn't open socket: not owner}
  720. test socket-6.1 {accept callback error} {stdio} {
  721. removeFile script
  722. set f [open script w]
  723. puts $f {
  724. gets stdin
  725. socket localhost 2848
  726. }
  727. close $f
  728. set f [open "|[list $tcltest script]" r+]
  729. proc bgerror args {
  730. global x
  731. set x $args
  732. }
  733. proc accept {s a p} {expr 10 / 0}
  734. set s [socket -server accept 2848]
  735. puts $f hello
  736. close $f
  737. set timer [after 10000 "set x timed_out"]
  738. vwait x
  739. after cancel $timer
  740. close $s
  741. rename bgerror {}
  742. set x
  743. } {{divide by zero}}
  744. test socket-7.1 {testing socket specific options} {stdio} {
  745. removeFile script
  746. set f [open script w]
  747. puts $f {
  748. socket -server accept 2820
  749. proc accept args {
  750. global x
  751. set x done
  752. }
  753. puts ready
  754. set timer [after 10000 "set x timed_out"]
  755. vwait x
  756. after cancel $timer
  757. }
  758. close $f
  759. set f [open "|[list $tcltest script]" r]
  760. gets $f
  761. set s [socket localhost 2820]
  762. set p [fconfigure $s -peername]
  763. close $s
  764. close $f
  765. set l ""
  766. lappend l [string compare [lindex $p 0] 127.0.0.1]
  767. lappend l [string compare [lindex $p 2] 2820]
  768. lappend l [llength $p]
  769. } {0 0 3}
  770. test socket-7.2 {testing socket specific options} {stdio} {
  771. removeFile script
  772. set f [open script w]
  773. puts $f {
  774. socket -server accept 2821
  775. proc accept args {
  776. global x
  777. set x done
  778. }
  779. puts ready
  780. set timer [after 10000 "set x timed_out"]
  781. vwait x
  782. after cancel $timer
  783. }
  784. close $f
  785. set f [open "|[list $tcltest script]" r]
  786. gets $f
  787. set s [socket localhost 2821]
  788. set p [fconfigure $s -sockname]
  789. close $s
  790. close $f
  791. set l ""
  792. lappend l [llength $p]
  793. lappend l [lindex $p 0]
  794. lappend l [expr [lindex $p 2] == 2821]
  795. } {3 127.0.0.1 0}
  796. test socket-7.3 {testing socket specific options} {
  797. set s [socket -server accept 2822]
  798. set l [fconfigure $s]
  799. close $s
  800. update
  801. llength $l
  802. } 10
  803. test socket-7.4 {testing socket specific options} {
  804. set s [socket -server accept 2823]
  805. proc accept {s a p} {
  806. global x
  807. set x [fconfigure $s -sockname]
  808. close $s
  809. }
  810. set s1 [socket [info hostname] 2823]
  811. set timer [after 10000 "set x timed_out"]
  812. vwait x
  813. after cancel $timer
  814. close $s
  815. close $s1
  816. set l ""
  817. lappend l [lindex $x 2] [llength $x]
  818. } {2823 3}
  819. test socket-7.5 {testing socket specific options} {unixOrPc} {
  820. set s [socket -server accept 2829]
  821. proc accept {s a p} {
  822. global x
  823. set x [fconfigure $s -sockname]
  824. close $s
  825. }
  826. set s1 [socket localhost 2829]
  827. set timer [after 10000 "set x timed_out"]
  828. vwait x
  829. after cancel $timer
  830. close $s
  831. close $s1
  832. set l ""
  833. lappend l [lindex $x 0] [lindex $x 2] [llength $x]
  834. } {127.0.0.1 2829 3}
  835. test socket-8.1 {testing -async flag on sockets} {
  836. # NOTE: This test may fail on some Solaris 2.4 systems. If it does,
  837. # check that you have these patches installed (using showrev -p):
  838. #
  839. # 101907-05, 101925-02, 101945-14, 101959-03, 101969-05, 101973-03,
  840. # 101977-03, 101981-02, 101985-01, 102001-03, 102003-01, 102007-01,
  841. # 102011-02, 102024-01, 102039-01, 102044-01, 102048-01, 102062-03,
  842. # 102066-04, 102070-01, 102105-01, 102153-03, 102216-01, 102232-01,
  843. # 101878-03, 101879-01, 101880-03, 101933-01, 101950-01, 102030-01,
  844. # 102057-08, 102140-01, 101920-02, 101921-09, 101922-07, 101923-03
  845. #
  846. # If after installing these patches you are still experiencing a
  847. # problem, please email jyl@eng.sun.com. We have not observed this
  848. # failure on Solaris 2.5, so another option (instead of installing
  849. # these patches) is to upgrade to Solaris 2.5.
  850. set s [socket -server accept 2830]
  851. proc accept {s a p} {
  852. global x
  853. puts $s bye
  854. close $s
  855. set x done
  856. }
  857. set s1 [socket -async [info hostname] 2830]
  858. vwait x
  859. set z [gets $s1]
  860. close $s
  861. close $s1
  862. set z
  863. } bye
  864. test socket-9.1 {testing spurious events} {
  865. set len 0
  866. set spurious 0
  867. set done 0
  868. proc readlittle {s} {
  869. global spurious done len
  870. set l [read $s 1]
  871. if {[string length $l] == 0} {
  872. if {![eof $s]} {
  873. incr spurious
  874. } else {
  875. close $s
  876. set done 1
  877. }
  878. } else {
  879. incr len [string length $l]
  880. }
  881. }
  882. proc accept {s a p} {
  883. fconfigure $s -buffering none -blocking off
  884. fileevent $s readable [list readlittle $s]
  885. }
  886. set s [socket -server accept 2831]
  887. set c [socket [info hostname] 2831]
  888. puts -nonewline $c 01234567890123456789012345678901234567890123456789
  889. close $c
  890. set timer [after 10000 "set done timed_out"]
  891. vwait done
  892. after cancel $timer
  893. close $s
  894. list $spurious $len
  895. } {0 50}
  896. test socket-9.2 {testing async write, fileevents, flush on close} {} {
  897. set firstblock ""
  898. for {set i 0} {$i < 5} {incr i} {set firstblock "a$firstblock$firstblock"}
  899. set secondblock ""
  900. for {set i 0} {$i < 16} {incr i} {
  901. set secondblock "b$secondblock$secondblock"
  902. }
  903. set l [socket -server accept 2832]
  904. proc accept {s a p} {
  905. fconfigure $s -blocking 0 -translation lf -buffersize 16384 \
  906. -buffering line
  907. fileevent $s readable "readable $s"
  908. }
  909. proc readable {s} {
  910. set l [gets $s]
  911. fileevent $s readable {}
  912. after 1000 respond $s
  913. }
  914. proc respond {s} {
  915. global firstblock
  916. puts -nonewline $s $firstblock
  917. after 1000 writedata $s
  918. }
  919. proc writedata {s} {
  920. global secondblock
  921. puts -nonewline $s $secondblock
  922. close $s
  923. }
  924. set s [socket [info hostname] 2832]
  925. fconfigure $s -blocking 0 -trans lf -buffering line
  926. set count 0
  927. puts $s hello
  928. proc readit {s} {
  929. global count done
  930. set l [read $s]
  931. incr count [string length $l]
  932. if {[eof $s]} {
  933. close $s
  934. set done 1
  935. }
  936. }
  937. fileevent $s readable "readit $s"
  938. set timer [after 10000 "set done timed_out"]
  939. vwait done
  940. after cancel $timer
  941. close $l
  942. set count
  943. } 65566
  944. test socket-9.3 {testing EOF stickyness} {
  945. proc count_to_eof {s} {
  946. global count done timer
  947. set l [gets $s]
  948. if {[eof $s]} {
  949. incr count
  950. if {$count > 9} {
  951. close $s
  952. set done true
  953. set count {eof is sticky}
  954. after cancel $timer
  955. }
  956. }
  957. }
  958. proc timerproc {} {
  959. global done count c
  960. set done true
  961. set count {timer went off, eof is not sticky}
  962. close $c
  963. }
  964. set count 0
  965. set done false
  966. proc write_then_close {s} {
  967. puts $s bye
  968. close $s
  969. }
  970. proc accept {s a p} {
  971. fconfigure $s -buffering line -translation lf
  972. fileevent $s writable "write_then_close $s"
  973. }
  974. set s [socket -server accept 2833]
  975. set c [socket [info hostname] 2833]
  976. fconfigure $c -blocking off -buffering line -translation lf
  977. fileevent $c readable "count_to_eof $c"
  978. set timer [after 1000 timerproc]
  979. vwait done
  980. close $s
  981. set count
  982. } {eof is sticky}
  983. test socket-10.1 {testing socket accept callback error handling} {
  984. set goterror 0
  985. proc bgerror args {global goterror; set goterror 1}
  986. set s [socket -server accept 2898]
  987. proc accept {s a p} {close $s; error}
  988. set c [socket localhost 2898]
  989. vwait goterror
  990. close $s
  991. close $c
  992. set goterror
  993. } 1
  994. removeFile script
  995. #
  996. # The rest of the tests are run only if we are doing testing against
  997. # a remote server.
  998. #
  999. if {$doTestsWithRemoteServer == 0} {
  1000. return
  1001. }
  1002. test socket-11.1 {tcp connection} {
  1003. sendCommand {
  1004. set socket9_1_test_server [socket -server accept 2834]
  1005. proc accept {s a p} {
  1006. puts $s done
  1007. close $s
  1008. }
  1009. }
  1010. set s [socket $remoteServerIP 2834]
  1011. set r [gets $s]
  1012. close $s
  1013. sendCommand {close $socket9_1_test_server}
  1014. set r
  1015. } done
  1016. test socket-11.2 {client specifies its port} {
  1017. if {[info exists port]} {
  1018. incr port
  1019. } else {
  1020. set port [expr 2048 + [pid]%1024]
  1021. }
  1022. sendCommand {
  1023. set socket9_2_test_server [socket -server accept 2835]
  1024. proc accept {s a p} {
  1025. puts $s $p
  1026. close $s
  1027. }
  1028. }
  1029. set s [socket -myport $port $remoteServerIP 2835]
  1030. set r [gets $s]
  1031. close $s
  1032. sendCommand {close $socket9_2_test_server}
  1033. if {$r == $port} {
  1034. set result ok
  1035. } else {
  1036. set result broken
  1037. }
  1038. set result
  1039. } ok
  1040. test socket-11.3 {trying to connect, no server} {
  1041. set status ok
  1042. if {![catch {set s [socket $remoteServerIp 2836]}]} {
  1043. if {![catch {gets $s}]} {
  1044. set status broken
  1045. }
  1046. close $s
  1047. }
  1048. set status
  1049. } ok
  1050. test socket-11.4 {remote echo, one line} {
  1051. sendCommand {
  1052. set socket10_6_test_server [socket -server accept 2836]
  1053. proc accept {s a p} {
  1054. fileevent $s readable [list echo $s]
  1055. fconfigure $s -buffering line -translation crlf
  1056. }
  1057. proc echo {s} {
  1058. set l [gets $s]
  1059. if {[eof $s]} {
  1060. close $s
  1061. } else {
  1062. puts $s $l
  1063. }
  1064. }
  1065. }
  1066. set f [socket $remoteServerIP 2836]
  1067. fconfigure $f -translation crlf -buffering line
  1068. puts $f hello
  1069. set r [gets $f]
  1070. close $f
  1071. sendCommand {close $socket10_6_test_server}
  1072. set r
  1073. } hello
  1074. test socket-11.5 {remote echo, 50 lines} {
  1075. sendCommand {
  1076. set socket10_7_test_server [socket -server accept 2836]
  1077. proc accept {s a p} {
  1078. fileevent $s readable [list echo $s]
  1079. fconfigure $s -buffering line -translation crlf
  1080. }
  1081. proc echo {s} {
  1082. set l [gets $s]
  1083. if {[eof $s]} {
  1084. close $s
  1085. } else {
  1086. puts $s $l
  1087. }
  1088. }
  1089. }
  1090. set f [socket $remoteServerIP 2836]
  1091. fconfigure $f -translation crlf -buffering line
  1092. for {set cnt 0} {$cnt < 50} {incr cnt} {
  1093. puts $f "hello, $cnt"
  1094. if {[string compare [gets $f] "hello, $cnt"] != 0} {
  1095. break
  1096. }
  1097. }
  1098. close $f
  1099. sendCommand {close $socket10_7_test_server}
  1100. set cnt
  1101. } 50
  1102. # Macintosh sockets can have more than one server per port
  1103. if {$tcl_platform(platform) == "macintosh"} {
  1104. set conflictResult {0 2836}
  1105. } else {
  1106. set conflictResult {1 {couldn't open socket: address already in use}}
  1107. }
  1108. test socket-11.6 {socket conflict} {
  1109. set s1 [socket -server accept 2836]
  1110. if {[catch {set s2 [socket -server accept 2836]} msg]} {
  1111. set result [list 1 $msg]
  1112. } else {
  1113. set result [list 0 [lindex [fconfigure $s2 -sockname] 2]]
  1114. close $s2
  1115. }
  1116. close $s1
  1117. set result
  1118. } $conflictResult
  1119. test socket-11.7 {server with several clients} {
  1120. sendCommand {
  1121. set socket10_9_test_server [socket -server accept 2836]
  1122. proc accept {s a p} {
  1123. fconfigure $s -buffering line
  1124. fileevent $s readable [list echo $s]
  1125. }
  1126. proc echo {s} {
  1127. set l [gets $s]
  1128. if {[eof $s]} {
  1129. close $s
  1130. } else {
  1131. puts $s $l
  1132. }
  1133. }
  1134. }
  1135. set s1 [socket $remoteServerIP 2836]
  1136. fconfigure $s1 -buffering line
  1137. set s2 [socket $remoteServerIP 2836]
  1138. fconfigure $s2 -buffering line
  1139. set s3 [socket $remoteServerIP 2836]
  1140. fconfigure $s3 -buffering line
  1141. for {set i 0} {$i < 100} {incr i} {
  1142. puts $s1 hello,s1
  1143. gets $s1
  1144. puts $s2 hello,s2
  1145. gets $s2
  1146. puts $s3 hello,s3
  1147. gets $s3
  1148. }
  1149. close $s1
  1150. close $s2
  1151. close $s3
  1152. sendCommand {close $socket10_9_test_server}
  1153. set i
  1154. } 100
  1155. test socket-11.8 {client with several servers} {
  1156. sendCommand {
  1157. set s1 [socket -server "accept 4003" 4003]
  1158. set s2 [socket -server "accept 4004" 4004]
  1159. set s3 [socket -server "accept 4005" 4005]
  1160. proc accept {mp s a p} {
  1161. puts $s $mp
  1162. close $s
  1163. }
  1164. }
  1165. set s1 [socket $remoteServerIP 4003]
  1166. set s2 [socket $remoteServerIP 4004]
  1167. set s3 [socket $remoteServerIP 4005]
  1168. set l ""
  1169. lappend l [gets $s1] [gets $s1] [eof $s1] [gets $s2] [gets $s2] [eof $s2] \
  1170. [gets $s3] [gets $s3] [eof $s3]
  1171. close $s1
  1172. close $s2
  1173. close $s3
  1174. sendCommand {
  1175. close $s1
  1176. close $s2
  1177. close $s3
  1178. }
  1179. set l
  1180. } {4003 {} 1 4004 {} 1 4005 {} 1}
  1181. test socket-11.9 {accept callback error} {
  1182. set s [socket -server accept 2836]
  1183. proc accept {s a p} {expr 10 / 0}
  1184. proc bgerror args {
  1185. global x
  1186. set x $args
  1187. }
  1188. if {[catch {sendCommand {
  1189. set peername [fconfigure $callerSocket -peername]
  1190. set s [socket [lindex $peername 0] 2836]
  1191. close $s
  1192. }} msg]} {
  1193. close $s
  1194. error $msg
  1195. }
  1196. set timer [after 10000 "set x timed_out"]
  1197. vwait x
  1198. after cancel $timer
  1199. close $s
  1200. rename bgerror {}
  1201. set x
  1202. } {{divide by zero}}
  1203. test socket-11.10 {testing socket specific options} {
  1204. sendCommand {
  1205. set socket10_12_test_server [socket -server accept 2836]
  1206. proc accept {s a p} {close $s}
  1207. }
  1208. set s [socket $remoteServerIP 2836]
  1209. set p [fconfigure $s -peername]
  1210. set n [fconfigure $s -sockname]
  1211. set l ""
  1212. lappend l [lindex $p 2] [llength $p] [llength $p]
  1213. close $s
  1214. sendCommand {close $socket10_12_test_server}
  1215. set l
  1216. } {2836 3 3}
  1217. test socket-11.11 {testing spurious events} {
  1218. sendCommand {
  1219. set socket10_13_test_server [socket -server accept 2836]
  1220. proc accept {s a p} {
  1221. fconfigure $s -translation "auto lf"
  1222. after 100 writesome $s
  1223. }
  1224. proc writesome {s} {
  1225. for {set i 0} {$i < 100} {incr i} {
  1226. puts $s "line $i from remote server"
  1227. }
  1228. close $s
  1229. }
  1230. }
  1231. set len 0
  1232. set spurious 0
  1233. set done 0
  1234. proc readlittle {s} {
  1235. global spurious done len
  1236. set l [read $s 1]
  1237. if {[string length $l] == 0} {
  1238. if {![eof $s]} {
  1239. incr spurious
  1240. } else {
  1241. close $s
  1242. set done 1
  1243. }
  1244. } else {
  1245. incr len [string length $l]
  1246. }
  1247. }
  1248. set c [socket $remoteServerIP 2836]
  1249. fileevent $c readable "readlittle $c"
  1250. set timer [after 10000 "set done timed_out"]
  1251. vwait done
  1252. after cancel $timer
  1253. sendCommand {close $socket10_13_test_server}
  1254. list $spurious $len
  1255. } {0 2690}
  1256. test socket-11.12 {testing EOF stickyness} {
  1257. set counter 0
  1258. set done 0
  1259. proc count_up {s} {
  1260. global counter done after_id
  1261. set l [gets $s]
  1262. if {[eof $s]} {
  1263. incr counter
  1264. if {$counter > 9} {
  1265. set done {EOF is sticky}
  1266. after cancel $after_id
  1267. close $s
  1268. }
  1269. }
  1270. }
  1271. proc timed_out {} {
  1272. global c done
  1273. set done {timed_out, EOF is not sticky}
  1274. close $c
  1275. }
  1276. sendCommand {
  1277. set socket10_14_test_server [socket -server accept 2836]
  1278. proc accept {s a p} {
  1279. after 100 close $s
  1280. }
  1281. }
  1282. set c [socket $remoteServerIP 2836]
  1283. fileevent $c readable "count_up $c"
  1284. set after_id [after 1000 timed_out]
  1285. vwait done
  1286. sendCommand {close $socket10_14_test_server}
  1287. set done
  1288. } {EOF is sticky}
  1289. test socket-11.13 {testing async write, async flush, async close} {
  1290. proc readit {s} {
  1291. global count done
  1292. set l [read $s]
  1293. incr count [string length $l]
  1294. if {[eof $s]} {
  1295. close $s
  1296. set done 1
  1297. }
  1298. }
  1299. sendCommand {
  1300. set firstblock ""
  1301. for {set i 0} {$i < 5} {incr i} {
  1302. set firstblock "a$firstblock$firstblock"
  1303. }
  1304. set secondblock ""
  1305. for {set i 0} {$i < 16} {incr i} {
  1306. set secondblock "b$secondblock$secondblock"
  1307. }
  1308. set l [socket -server accept 2845]
  1309. proc accept {s a p} {
  1310. fconfigure $s -blocking 0 -translation lf -buffersize 16384 \
  1311. -buffering line
  1312. fileevent $s readable "readable $s"
  1313. }
  1314. proc readable {s} {
  1315. set l [gets $s]
  1316. fileevent $s readable {}
  1317. after 1000 respond $s
  1318. }
  1319. proc respond {s} {
  1320. global firstblock
  1321. puts -nonewline $s $firstblock
  1322. after 1000 writedata $s
  1323. }
  1324. proc writedata {s} {
  1325. global secondblock
  1326. puts -nonewline $s $secondblock
  1327. close $s
  1328. }
  1329. }
  1330. set s [socket $remoteServerIP 2845]
  1331. fconfigure $s -blocking 0 -trans lf -buffering line
  1332. set count 0
  1333. puts $s hello
  1334. fileevent $s readable "readit $s"
  1335. set timer [after 10000 "set done timed_out"]
  1336. vwait done
  1337. after cancel $timer
  1338. sendCommand {close $l}
  1339. set count
  1340. } 65566
  1341. if {[string match sock* $commandSocket] == 1} {
  1342. puts $commandSocket exit
  1343. flush $commandSocket
  1344. }
  1345. catch {close $commandSocket}
  1346. catch {close $remoteProcChan}
  1347. set x ""
  1348. unset x