summaryrefslogtreecommitdiffstats
path: root/tests/unixInit.test
blob: 899779c59fcb974e7c6a09acaa430572a5da864b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# The file tests the functions in the tclUnixInit.c file.
#
# 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 © 1997 Sun Microsystems, Inc.
# Copyright © 1998-1999 Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {"::tcltest" ni [namespace children]} {
    package require tcltest 2.5
    namespace import -force ::tcltest::*
}
unset -nocomplain path
catch {set oldlang $env(LANG)}
set env(LANG) C

test unixInit-1.1 {TclpInitPlatform: ignore SIGPIPE} {unix stdio} {
    set x {}
    # Watch out for a race condition here.  If tcltest is too slow to start
    # then we'll kill it before it has a chance to set up its signal handler.
    set f [open "|[list [interpreter]]" w+]
    puts $f "puts hi"
    flush $f
    gets $f
    exec kill -PIPE [pid $f]
    lappend x [catch {close $f}]
    set f [open "|[list [interpreter]]" w+]
    puts $f "puts hi"
    flush $f
    gets $f
    exec kill [pid $f]
    lappend x [catch {close $f}]
    set x
} {0 1}
# This test is really a test of code in tclUnixChan.c, but the channels are
# set up as part of initialisation of the interpreter so the test seems to me
# to fit here as well as anywhere else.
test unixInit-1.2 {initialisation: standard channel type deduction} {unix stdio} {
    # pipe1 is a connection to a server that reports what port it starts on,
    # and delivers a constant string to the first client to connect to that
    # port before exiting.
    set pipe1 [open "|[list [interpreter]]" r+]
    puts $pipe1 {
	proc accept {channel host port} {
	    puts $channel {puts [chan configure stdin -peername]; exit}
	    close $channel
	    exit
	}
	puts [chan configure [socket -server accept -myaddr 127.0.0.1 0] -sockname]
	vwait forever \
	    }
    # Note the backslash above; this is important to make sure that the whole
    # string is read before an [exit] can happen...
    flush $pipe1
    set port [lindex [gets $pipe1] 2]
    set sock [socket localhost $port]
    # pipe2 is a connection to a Tcl interpreter that takes its orders from
    # the socket we hand it (i.e. the server we create above.)  These orders
    # will tell it to print out the details about the socket it is taking
    # instructions from, hopefully identifying it as a socket.  Which is what
    # this test is all about.
    set pipe2 [open "|[list [interpreter] <@$sock]" r]
    set result [gets $pipe2]
    # Clear any pending data; stops certain kinds of (non-important) errors
    chan configure $pipe1 -blocking 0; gets $pipe1
    chan configure $pipe2 -blocking 0; gets $pipe2
    # Close the pipes and the socket.
    close $pipe2
    close $pipe1
    catch {close $sock}
    # Can't use normal comparison, as hostname varies due to some
    # installations having a messed up /etc/hosts file.
    if {
	"127.0.0.1" eq [lindex $result 0] && $port == [lindex $result 2]
    } then {
	subst "OK"
    } else {
	subst "Expected: `[list 127.0.0.1 localhost $port]', Got `$result'"
    }
} {OK}

test unixInit-3.1 {TclpSetInitialEncodings} -constraints {
	unix stdio
} -body {
    set env(LANG) C
    set f [open "|[list [interpreter]]" w+]
    chan configure $f -buffering none
    puts $f {puts [encoding system]; exit}
    set enc [gets $f]
    close $f
    set enc
} -cleanup {
    unset -nocomplain env(LANG)
} -match regexp -result {^(iso8859-15?|utf-8)$}

# unixInit-3.2 depends on the *spawned* [interpreter] being able to locate
# tcl_library without setting of TCL_LIBRARY env. This in turn depends on
# Tcl's "library" directory being under the parent or grandparent of the
# executable directory (the initScript search path in tclInterp.c).
# Thus this constraint. On GiuHub CI, the only time this is not true
# is for the XCode builds.
if {[string match [zipfs root]* [info library]] ||
    [file isfile [file normalize [file join [info nameofexecutable] .. .. library init.tcl]]] ||
    [file isfile [file normalize [file join [info nameofexecutable] .. .. .. library init.tcl]]]
} {
    tcltest::testConstraint enableUnixInit32 1
} else {
    tcltest::testConstraint enableUnixInit32 0
}
test unixInit-3.2 {TclpSetInitialEncodings} -setup {
    catch {set oldlc_all $env(LC_ALL)}
    catch {set oldtcl_library $env(TCL_LIBRARY)}
    unset -nocomplain env(TCL_LIBRARY)
} -constraints {unix stdio enableUnixInit32} -body {
    set env(LANG) japanese
    set env(LC_ALL) japanese
    set f [open "|[list [interpreter]]" w+]
    chan configure $f -buffering none
    puts $f {puts [encoding system]; exit}
    set enc [gets $f]
    close $f
    set validEncodings [list euc-jp]
    if {[string match HP-UX $tcl_platform(os)]} {
	# Some older HP-UX systems need us to accept this as valid Bug 453883
	# reports that newer HP-UX systems report euc-jp like everybody else.
	lappend validEncodings shiftjis
    }
    expr {$enc ni $validEncodings}
} -cleanup {
    unset -nocomplain env(LANG) env(LC_ALL)
    catch {set env(LC_ALL) $oldlc_all}
    catch {set env(TCL_LIBRARY) $oldtcl_library}
} -result 0

test unixInit-4.1 {TclpSetVariables} {unix} {
    # just make sure they exist
    set a [list $tcl_library $tcl_pkgPath $tcl_platform(os)]
    set a [list $tcl_platform(osVersion) $tcl_platform(machine)]
    set tcl_platform(platform)
} "unix"

test unixInit-5.1 {Tcl_Init} {emptyTest unix} {
    # test initScript
} {}

test unixInit-6.1 {Tcl_SourceRCFile} {emptyTest unix} {
} {}

test unixInit-7.1 {closed standard channel: Bug 772288} -constraints {
    unix stdio
} -body {
    set tclsh [interpreter]
    set crash [makeFile {puts [open /dev/null]} crash.tcl]
    set crashtest [makeFile "
	close stdin
	[list exec $tclsh $crash]
    " crashtest.tcl]
    exec $tclsh $crashtest
} -cleanup {
    removeFile crash.tcl
    removeFile crashtest.tcl
} -returnCodes 0

# cleanup
unset -nocomplain env(LANG)
catch {set env(LANG) $oldlang}
unset -nocomplain path
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# fill-column: 78
# End: