os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/unixInit.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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
     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) 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
    11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    12 #
    13 # RCS: @(#) $Id: unixInit.test,v 1.30.2.12 2005/04/27 21:07:52 dgp Exp $
    14 
    15 package require tcltest 2
    16 namespace import -force ::tcltest::*
    17 unset -nocomplain path
    18 if {[info exists env(TCL_LIBRARY)]} {
    19     set oldlibrary $env(TCL_LIBRARY)
    20     unset env(TCL_LIBRARY)
    21 }
    22 catch {set oldlang $env(LANG)}
    23 set env(LANG) C
    24 
    25 test unixInit-1.1 {TclpInitPlatform: ignore SIGPIPE} {unixOnly stdio} {
    26     set x {}
    27 
    28     # Watch out for a race condition here.  If tcltest is too slow to start
    29     # then we'll kill it before it has a chance to set up its signal handler.
    30     
    31     set f [open "|[list [interpreter]]" w+]
    32     puts $f "puts hi"
    33     flush $f
    34     gets $f
    35     exec kill -PIPE [pid $f]
    36     lappend x [catch {close $f}]
    37 
    38     set f [open "|[list [interpreter]]" w+]
    39     puts $f "puts hi"
    40     flush $f
    41     gets $f
    42     exec kill [pid $f]
    43     lappend x [catch {close $f}]
    44 
    45     set x
    46 } {0 1}
    47 
    48 # This test is really a test of code in tclUnixChan.c, but the
    49 # channels are set up as part of initialisation of the interpreter so
    50 # the test seems to me to fit here as well as anywhere else.
    51 test unixInit-1.2 {initialisation: standard channel type deduction} {unixOnly stdio} {
    52     # pipe1 is a connection to a server that reports what port it
    53     # starts on, and delivers a constant string to the first client to
    54     # connect to that port before exiting.
    55     set pipe1 [open "|[list [interpreter]]" r+]
    56     puts $pipe1 {
    57 	proc accept {channel host port} {
    58 	    puts $channel {puts [fconfigure stdin -peername]; exit}
    59 	    close $channel
    60 	    exit
    61 	}
    62 	puts [fconfigure [socket -server accept 0] -sockname]
    63 	vwait forever \
    64 	    }
    65     # Note the backslash above; this is important to make sure that the
    66     # whole string is read before an [exit] can happen...
    67     flush $pipe1
    68     set port [lindex [gets $pipe1] 2]
    69     set sock [socket localhost $port]
    70     # pipe2 is a connection to a Tcl interpreter that takes its orders
    71     # from the socket we hand it (i.e. the server we create above.)
    72     # These orders will tell it to print out the details about the
    73     # socket it is taking instructions from, hopefully identifying it
    74     # as a socket.  Which is what this test is all about.
    75     set pipe2 [open "|[list [interpreter] <@$sock]" r]
    76     set result [gets $pipe2]
    77 
    78     # Clear any pending data; stops certain kinds of (non-important) errors
    79     fconfigure $pipe1 -blocking 0; gets $pipe1
    80     fconfigure $pipe2 -blocking 0; gets $pipe2
    81 
    82     # Close the pipes and the socket.
    83     close $pipe2
    84     close $pipe1
    85     catch {close $sock}
    86 
    87     # Can't use normal comparison, as hostname varies due to some
    88     # installations having a messed up /etc/hosts file.
    89     if {
    90 	[string equal 127.0.0.1 [lindex $result 0]] &&
    91 	[string equal $port     [lindex $result 2]]
    92     } then {
    93 	subst "OK"
    94     } else {
    95 	subst "Expected: `[list 127.0.0.1 localhost $port]', Got `$result'"
    96     }
    97 } {OK}
    98 
    99 proc getlibpath [list [list program [interpreter]]] {
   100     set f [open "|[list $program]" w+]
   101     fconfigure $f -buffering none
   102     puts $f {puts $tcl_libPath; exit}
   103     set path [gets $f]
   104     close $f
   105     return $path
   106 }
   107 
   108 # Some tests require the testgetdefenc command
   109 
   110 testConstraint testgetdefenc [llength [info commands testgetdefenc]]
   111 
   112 test unixInit-2.0 {TclpInitLibraryPath: setting tclDefaultEncodingDir} \
   113 	{unixOnly testgetdefenc} {
   114     set origDir [testgetdefenc]
   115     testsetdefenc slappy
   116     set path [testgetdefenc]
   117     testsetdefenc $origDir
   118     set path
   119 } {slappy}
   120 test unixInit-2.1 {TclpInitLibraryPath: value of installLib, developLib} \
   121 	{unixOnly stdio} {
   122     set path [getlibpath]
   123 
   124     set installLib lib/tcl[info tclversion]
   125     set developLib tcl[info patchlevel]/library
   126     set prefix [file dirname [file dirname [interpreter]]]
   127 
   128     set x {}
   129     lappend x [string compare [lindex $path 0] $prefix/$installLib]
   130     lappend x [string compare [lindex $path 4] [file dirname $prefix]/$developLib]
   131     set x
   132 } {0 0}
   133 test unixInit-2.2 {TclpInitLibraryPath: TCL_LIBRARY} {unixOnly stdio} {
   134     # ((str != NULL) && (str[0] != '\0')) 
   135 
   136     set env(TCL_LIBRARY) sparkly
   137     set path [getlibpath]
   138     unset env(TCL_LIBRARY)
   139 
   140     lindex $path 0
   141 } "sparkly"
   142 test unixInit-2.3 {TclpInitLibraryPath: TCL_LIBRARY wrong version} \
   143 	{unixOnly stdio} {
   144     # ((pathc > 0) && (strcasecmp(installLib + 4, pathv[pathc - 1]) != 0))
   145 
   146     set env(TCL_LIBRARY) /a/b/tcl1.7
   147     set path [getlibpath]
   148     unset env(TCL_LIBRARY)
   149 
   150     lrange $path 0 1
   151 } [list /a/b/tcl1.7 /a/b/tcl[info tclversion]]
   152 test unixInit-2.4 {TclpInitLibraryPath: TCL_LIBRARY: INTL} \
   153 	{unixOnly stdio} {
   154     # Child process translates env variable from native encoding.
   155 
   156     set env(TCL_LIBRARY) "\xa7"
   157     set x [lindex [getlibpath] 0]
   158     unset env(TCL_LIBRARY)
   159     unset env(LANG)
   160 
   161     set x
   162 } "\xa7"
   163 test unixInit-2.5 {TclpInitLibraryPath: compiled-in library path} \
   164 	{emptyTest unixOnly} {
   165     # cannot test
   166 } {}
   167 test unixInit-2.6 {TclpInitLibraryPath: executable relative} \
   168 	{unixOnly stdio} {
   169     makeDirectory tmp
   170     makeDirectory [file join tmp sparkly]
   171     makeDirectory [file join tmp sparkly bin]
   172     file copy [interpreter] [file join [temporaryDirectory] tmp sparkly \
   173 	    bin tcltest]
   174     makeDirectory [file join tmp sparkly lib]
   175     makeDirectory [file join tmp sparkly lib tcl[info tclversion]]
   176     makeFile {} [file join tmp sparkly lib tcl[info tclversion] init.tcl]
   177 
   178     set x [lrange [getlibpath [file join [temporaryDirectory] tmp sparkly \
   179 	    bin tcltest]] 0 1]
   180     removeFile [file join tmp sparkly lib tcl[info tclversion] init.tcl]
   181     removeDirectory [file join tmp sparkly lib tcl[info tclversion]]
   182     removeDirectory [file join tmp sparkly lib]
   183     removeDirectory [file join tmp sparkly bin]
   184     removeDirectory [file join tmp sparkly]
   185     removeDirectory tmp
   186     set x
   187 } [list [temporaryDirectory]/tmp/sparkly/lib/tcl[info tclversion] [temporaryDirectory]/tmp/lib/tcl[info tclversion]]
   188 test unixInit-2.7 {TclpInitLibraryPath: compiled-in library path} \
   189 	{emptyTest unixOnly} {
   190     # would need test command to get defaultLibDir and compare it to
   191     # [lindex $auto_path end]
   192 } {}
   193 #
   194 # The following two tests write to the directory /tmp/sparkly instead
   195 # of to [temporaryDirectory].  This is because the failures tested by
   196 # these tests need paths near the "root" of the file system to present
   197 # themselves.
   198 #
   199 testConstraint noSparkly [expr {![file exists [file join /tmp sparkly]]}]
   200 testConstraint noTmpInstall [expr {![file exists \
   201 				[file join /tmp lib tcl[info tclversion]]]}]
   202 test unixInit-2.8 {TclpInitLibraryPath: all absolute pathtype} {unix noSparkly noTmpInstall} {
   203     # Checking for Bug 219416
   204     # When a program that embeds the Tcl library, like tcltest, is
   205     # installed near the "root" of the file system, there was a problem
   206     # constructing directories relative to the executable.  When a 
   207     # relative ".." went past the root, relative path names were created
   208     # rather than absolute pathnames.  In some cases, accessing past the
   209     # root caused memory access violations too.
   210     #
   211     # The bug is now fixed, but here we check for it by making sure that
   212     # the directories constructed relative to the executable are all
   213     # absolute pathnames, even when the executable is installed near
   214     # the root of the filesystem.
   215     #
   216     # The only directory near the root we are likely to have write access
   217     # to is /tmp.
   218     file delete -force /tmp/sparkly
   219     file delete -force /tmp/lib/tcl[info tclversion]
   220     file mkdir /tmp/sparkly
   221     file copy [interpreter] /tmp/sparkly/tcltest
   222 
   223     # Keep any existing /tmp/lib directory
   224     set deletelib 1
   225     if {[file exists /tmp/lib]} {
   226 	if {[file isdirectory /tmp/lib]} {
   227 	    set deletelib 0
   228 	} else {
   229 	    file delete -force /tmp/lib
   230 	}
   231     }
   232 
   233     # For a successful Tcl_Init, we need a [source]-able init.tcl in
   234     # ../lib/tcl$version relative to the executable.
   235     file mkdir /tmp/lib/tcl[info tclversion]
   236     close [open /tmp/lib/tcl[info tclversion]/init.tcl w]
   237 
   238     # Check that all directories in the library path are absolute pathnames
   239     set allAbsolute 1
   240     foreach dir [getlibpath /tmp/sparkly/tcltest] {
   241 	set allAbsolute [expr {$allAbsolute \
   242 		&& [string equal absolute [file pathtype $dir]]}]
   243     }
   244 
   245     # Clean up temporary installation
   246     file delete -force /tmp/sparkly
   247     file delete -force /tmp/lib/tcl[info tclversion]
   248     if {$deletelib} {file delete -force /tmp/lib}
   249     set allAbsolute
   250 } 1
   251 testConstraint noTmpBuild [expr {![file exists [file join /tmp library]]}]
   252 test unixInit-2.9 {TclpInitLibraryPath: paths relative to executable} {unix noSparkly noTmpBuild} {
   253     # Checking for Bug 438014
   254     file delete -force /tmp/sparkly
   255     file delete -force /tmp/library
   256     file mkdir /tmp/sparkly
   257     file copy [interpreter] /tmp/sparkly/tcltest
   258 
   259     file mkdir /tmp/library/
   260     close [open /tmp/library/init.tcl w]
   261 
   262     set x [lrange [getlibpath /tmp/sparkly/tcltest] 0 4]
   263 
   264     file delete -force /tmp/sparkly
   265     file delete -force /tmp/library
   266     set x
   267 } [list /tmp/lib/tcl[info tclversion] /lib/tcl[info tclversion] \
   268         /tmp/library /library /tcl[info patchlevel]/library]
   269 
   270 test unixInit-2.10 {TclpInitLibraryPath: executable relative} -constraints {
   271 	unixOnly stdio
   272 } -setup {
   273     set tmpDir [makeDirectory tmp]
   274     set sparklyDir [makeDirectory sparkly $tmpDir]
   275     set execPath [file join [makeDirectory bin $sparklyDir] tcltest]
   276     file copy [interpreter] $execPath
   277     set libDir [makeDirectory lib $sparklyDir]
   278     set scriptDir [makeDirectory tcl[info tclversion] $libDir]
   279     makeFile {} init.tcl $scriptDir
   280     set saveDir [pwd]
   281     cd $libDir
   282 } -body {
   283     # Checking for Bug 832657
   284     set x [lrange [getlibpath [file join .. bin tcltest]] 2 3]
   285     foreach p $x {
   286       lappend y [file normalize $p]
   287     }
   288     set y
   289 } -cleanup {
   290     cd $saveDir
   291     unset saveDir
   292     removeFile init.tcl $scriptDir
   293     unset scriptDir
   294     removeDirectory tcl[info tclversion] $libDir
   295     unset libDir
   296     file delete $execPath
   297     unset execPath
   298     removeDirectory bin $sparklyDir
   299     removeDirectory lib $sparklyDir
   300     unset sparklyDir
   301     removeDirectory sparkly $tmpDir
   302     unset tmpDir
   303     removeDirectory tmp
   304     unset x p y
   305 } -result [list [file join [temporaryDirectory] tmp sparkly library] \
   306 	[file join [temporaryDirectory] tmp library] ]
   307 
   308 test unixInit-3.1 {TclpSetInitialEncodings} -constraints {
   309 	unixOnly stdio
   310 } -body {
   311     set env(LANG) C
   312 
   313     set f [open "|[list [interpreter]]" w+]
   314     fconfigure $f -buffering none
   315     puts $f {puts [encoding system]; exit}
   316     set enc [gets $f]
   317     close $f
   318     unset env(LANG)
   319 
   320     set enc
   321 } -match regexp -result [expr {
   322 	($tcl_platform(os) eq "Darwin") ? "^utf-8$" : "^iso8859-15?$"}]
   323 
   324 test unixInit-3.2 {TclpSetInitialEncodings} {unixOnly stdio} {
   325     set env(LANG) japanese
   326     catch {set oldlc_all $env(LC_ALL)}
   327     set env(LC_ALL) japanese
   328 
   329     set f [open "|[list [interpreter]]" w+]
   330     fconfigure $f -buffering none
   331     puts $f {puts [encoding system]; exit}
   332     set enc [gets $f]
   333     close $f
   334     unset env(LANG)
   335     unset env(LC_ALL)
   336     catch {set env(LC_ALL) $oldlc_all}
   337 
   338     set validEncodings [list euc-jp]
   339     if {[string match HP-UX $tcl_platform(os)]} {
   340 	# Some older HP-UX systems need us to accept this as valid
   341 	# Bug 453883 reports that newer HP-UX systems report euc-jp
   342 	# like everybody else.
   343 	lappend validEncodings shiftjis
   344     }
   345     expr {[lsearch -exact $validEncodings $enc] < 0}
   346 } 0
   347 
   348 test unixInit-4.1 {TclpSetVariables} {unixOnly} {
   349     # just make sure they exist
   350 
   351     set a [list $tcl_library $tcl_pkgPath $tcl_platform(os)]
   352     set a [list $tcl_platform(osVersion) $tcl_platform(machine)]
   353     set tcl_platform(platform)
   354 } "unix"
   355 
   356 test unixInit-5.1 {Tcl_Init} {emptyTest unixOnly} {
   357     # test initScript
   358 } {}
   359 
   360 test unixInit-6.1 {Tcl_SourceRCFile} {emptyTest unixOnly} {
   361 } {}
   362 
   363 test unixInit-7.1 {closed standard channel: Bug 772288} -constraints {
   364 	unixOnly stdio
   365 } -body {
   366     set tclsh [interpreter]
   367     set crash [makeFile {puts [open /dev/null]} crash.tcl]
   368     set crashtest [makeFile "
   369 	close stdin
   370 	[list exec $tclsh $crash]
   371     " crashtest.tcl]
   372     exec $tclsh $crashtest
   373 } -cleanup {
   374     removeFile crash.tcl
   375     removeFile crashtest.tcl
   376 } -returnCodes 0
   377 
   378 # cleanup
   379 if {[info exists oldlibrary]} {
   380     set env(TCL_LIBRARY) $oldlibrary
   381 }
   382 catch {unset env(LANG)}
   383 catch {set env(LANG) $oldlang}
   384 unset -nocomplain path
   385 ::tcltest::cleanupTests
   386 return