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