Debian Codegraph @

tests/unixInit.test

raw
1 # The file tests the functions in the tclUnixInit.c file.
2 #
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1997 by Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 by Scriptics Corporation.
9 #
10 # See the file "license.terms" for information on usage and redistribution of
11 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
13 package require tcltest 2.5
14 namespace import ::tcltest::*
15 unset -nocomplain path
16 catch {set oldlang $env(LANG)}
17 set env(LANG) C
18
19 test unixInit-1.1 {TclpInitPlatform: ignore SIGPIPE} {unix stdio} {
20 set x {}
21 # Watch out for a race condition here. If tcltest is too slow to start
22 # then we'll kill it before it has a chance to set up its signal handler.
23 set f [open "|[list [interpreter]]" w+]
24 puts $f "puts hi"
25 flush $f
26 gets $f
27 exec kill -PIPE [pid $f]
28 lappend x [catch {close $f}]
29 set f [open "|[list [interpreter]]" w+]
30 puts $f "puts hi"
31 flush $f
32 gets $f
33 exec kill [pid $f]
34 lappend x [catch {close $f}]
35 set x
36 } {0 1}
37 # This test is really a test of code in tclUnixChan.c, but the channels are
38 # set up as part of initialisation of the interpreter so the test seems to me
39 # to fit here as well as anywhere else.
40 test unixInit-1.2 {initialisation: standard channel type deduction} {unix stdio} {
41 # pipe1 is a connection to a server that reports what port it starts on,
42 # and delivers a constant string to the first client to connect to that
43 # port before exiting.
44 set pipe1 [open "|[list [interpreter]]" r+]
45 puts $pipe1 {
46 proc accept {channel host port} {
47 puts $channel {puts [chan configure stdin -peername]; exit}
48 close $channel
49 exit
50 }
51 puts [chan configure [socket -server accept -myaddr 127.0.0.1 0] -sockname]
52 vwait forever \
53 }
54 # Note the backslash above; this is important to make sure that the whole
55 # string is read before an [exit] can happen...
56 flush $pipe1
57 set port [lindex [gets $pipe1] 2]
58 set sock [socket localhost $port]
59 # pipe2 is a connection to a Tcl interpreter that takes its orders from
60 # the socket we hand it (i.e. the server we create above.) These orders
61 # will tell it to print out the details about the socket it is taking
62 # instructions from, hopefully identifying it as a socket. Which is what
63 # this test is all about.
64 set pipe2 [open "|[list [interpreter] <@$sock]" r]
65 set result [gets $pipe2]
66 # Clear any pending data; stops certain kinds of (non-important) errors
67 chan configure $pipe1 -blocking 0; gets $pipe1
68 chan configure $pipe2 -blocking 0; gets $pipe2
69 # Close the pipes and the socket.
70 close $pipe2
71 close $pipe1
72 catch {close $sock}
73 # Can't use normal comparison, as hostname varies due to some
74 # installations having a messed up /etc/hosts file.
75 if {
76 "127.0.0.1" eq [lindex $result 0] && $port == [lindex $result 2]
77 } then {
78 subst "OK"
79 } else {
80 subst "Expected: `[list 127.0.0.1 localhost $port]', Got `$result'"
81 }
82 } {OK}
83
84 test unixInit-3.1 {TclpSetInitialEncodings} -constraints {
85 unix stdio
86 } -body {
87 set env(LANG) C
88 set f [open "|[list [interpreter]]" w+]
89 chan configure $f -buffering none
90 puts $f {puts [encoding system]; exit}
91 set enc [gets $f]
92 close $f
93 set enc
94 } -cleanup {
95 unset -nocomplain env(LANG)
96 } -match regexp -result {^(iso8859-15?|utf-8)$}
97 test unixInit-3.2 {TclpSetInitialEncodings} -setup {
98 catch {set oldlc_all $env(LC_ALL)}
99 } -constraints {unix stdio} -body {
100 set env(LANG) japanese
101 set env(LC_ALL) japanese
102 set f [open "|[list [interpreter]]" w+]
103 chan configure $f -buffering none
104 puts $f {puts [encoding system]; exit}
105 set enc [gets $f]
106 close $f
107 set validEncodings [list euc-jp]
108 if {[string match HP-UX $tcl_platform(os)]} {
109 # Some older HP-UX systems need us to accept this as valid Bug 453883
110 # reports that newer HP-UX systems report euc-jp like everybody else.
111 lappend validEncodings shiftjis
112 }
113 expr {$enc ni $validEncodings}
114 } -cleanup {
115 unset -nocomplain env(LANG) env(LC_ALL)
116 catch {set env(LC_ALL) $oldlc_all}
117 } -result 0
118
119 test unixInit-4.1 {TclpSetVariables} {unix} {
120 # just make sure they exist
121 set a [list $tcl_library $tcl_pkgPath $tcl_platform(os)]
122 set a [list $tcl_platform(osVersion) $tcl_platform(machine)]
123 set tcl_platform(platform)
124 } "unix"
125
126 test unixInit-5.1 {Tcl_Init} {emptyTest unix} {
127 # test initScript
128 } {}
129
130 test unixInit-6.1 {Tcl_SourceRCFile} {emptyTest unix} {
131 } {}
132
133 test unixInit-7.1 {closed standard channel: Bug 772288} -constraints {
134 unix stdio
135 } -body {
136 set tclsh [interpreter]
137 set crash [makeFile {puts [open /dev/null]} crash.tcl]
138 set crashtest [makeFile "
139 close stdin
140 [list exec $tclsh $crash]
141 " crashtest.tcl]
142 exec $tclsh $crashtest
143 } -cleanup {
144 removeFile crash.tcl
145 removeFile crashtest.tcl
146 } -returnCodes 0
147
148 # cleanup
149 unset -nocomplain env(LANG)
150 catch {set env(LANG) $oldlang}
151 unset -nocomplain path
152 ::tcltest::cleanupTests
153 return
154
155 # Local Variables:
156 # mode: tcl
157 # fill-column: 78
158 # End: