Skip to content
Tcl Source Code Artifact [3c29a595c8]
Login

Artifact [3c29a595c8]

Artifact 3c29a595c86018190e14dd32d9ac3d2152bfb99b976f783410cc5ccb87bf8bda:


# This file tests the tclWinFile.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::*
}

::tcltest::loadTestedCommands
catch [list package require -exact tcl::test [info patchlevel]]

testConstraint testvolumetype [llength [info commands testvolumetype]]
testConstraint notNTFS 0

if {[testConstraint testvolumetype]} {
    testConstraint notNTFS [expr {[testvolumetype] eq "NTFS"}]
}
testConstraint notWine [expr {![info exists ::env(CI_USING_WINE)]}]

test winFile-1.1 {TclpGetUserHome} -constraints {win} -body {
    glob ~nosuchuser
} -result {}
test winFile-1.2 {TclpGetUserHome} -constraints {win nonPortable} -body {
    # The administrator account should always exist.
    glob ~administrator
} -match glob -result *
test winFile-1.4 {TclpGetUserHome} {win nonPortable} {
    catch {glob ~stanton@workgroup}
} {0}

test winFile-2.1 {TclpMatchFiles: case sensitivity} -constraints {win} -body {
    makeFile {} GlobCapS
    set args [list -nocomplain -tails -directory [temporaryDirectory]]
    list [glob {*}$args GlobC*] [glob {*}$args globc*]} -cleanup {
    removeFile GlobCapS
} -result {GlobCapS GlobCapS}
test winFile-2.2 {TclpMatchFiles: case sensitivity} -constraints {win} -body {
    makeFile {} globlower
    set args [list -nocomplain -tails -directory [temporaryDirectory]]
    list [glob {*}$args globl*] [glob {*}$args gLOBl*]
} -cleanup {
    removeFile globlower
} -result {globlower globlower}

test winFile-3.1 {file system} -constraints {win testvolumetype} -setup {
    set res ""
} -body {
    foreach vol [file volumes] {
	# Have to catch in case there is a removable drive (CDROM, floppy)
	# with nothing in it.
	catch {
	    if {[lindex [file system $vol] 1] ne [testvolumetype $vol]} {
		append res "For $vol, we found [file system $vol]\
			and [testvolumetype $vol] are different\n"
	    }
	}
    }
    set res
} -result {}

proc cacls {fname args} {
    string trim [eval [list exec cacls [file nativename $fname]] $args <<y]
}

# dir/q output:
# 2003-11-03  20:36                  598 OCTAVIAN\benny         filename.txt
# Note this output from a german win2k machine:
# 14.12.2007  14:26                   30 VORDEFINIERT\Administratest.dat
#
# Modified to cope with Msys environment and use ls -l.
proc getuser {fname} {
    global env
    set tryname $fname
    if {[file isdirectory $fname]} {
	set tryname [file dirname $fname]
    }
    set owner ""
    set tail [file tail $tryname]
    if {[info exists env(OSTYPE)] && $env(OSTYPE) eq "msys"} {
	set dirtext [exec ls -l $fname]
	foreach line [split $dirtext "\n"] {
	    set owner [lindex $line 2]
	}
    } else {
	set dirtext [exec cmd /c dir /q [file nativename $fname]]
	foreach line [split $dirtext "\n"] {
	    if {[string match -nocase "*$tail" $line]} {
		set attrs [string range $line 0 end-[string length $tail]]
		regexp { [^ \\]+\\.*$} $attrs owner
		set owner [string trim $owner]
	    }
	}
    }
    if {$owner eq ""} {
	error "getuser: Owner not found in output of dir/q"
    }
    return $owner
}

proc test_read {fname} {
    if {[catch {open $fname r} ifs]} {
	return 0
    }
    set readfailed [catch {read $ifs}]
    return [expr {![catch {close $ifs}] && !$readfailed}]
}

proc test_writ {fname} {
    if {[catch {open $fname w} ofs]} {
	return 0
    }
    set writefailed [catch {puts $ofs "Hello"}]
    return [expr {![catch {close $ofs}] && !$writefailed}]
}

proc test_access {fname read writ} {
    set problem {}
    foreach type {read writ} {
	if {[set $type] != [file ${type}able $fname]} {
	    lappend problem "[set $type] != \[file ${type}able $fname\]"
	}
	if {[set $type] != [test_${type} $fname]} {
	    lappend problem "[set $type] != \[test_${type} $fname\]"
	}
    }
    if {![llength $problem]} {
	return
    }
    return "Problem [join $problem \n]\nActual rights are: [cacls $fname]"
}

if {[testConstraint win]} {
    # Create the test file
    # NOTE: [tcltest::makeFile] not used.  Presumably to force file
    # creation in a particular filesystem?  If not, try [makeFile]
    # in a -setup script.
    set fname test.dat
    file delete $fname
    close [open $fname w]
}

test winFile-4.0 {
    Enhanced NTFS user/group permissions: test no acccess
} -constraints {
    win notNTFS notWine
} -setup {
    set owner [getuser $fname]
    set user $::env(USERDOMAIN)\\$::env(USERNAME)
} -body {
    # Clean out all well-known ACLs
    catch {cacls $fname /E /R "Everyone"} result
    catch {cacls $fname /E /R $user} result
    catch {cacls $fname /E /R $owner} result
    cacls $fname /E /P $user:N
    test_access $fname 0 0
} -result {}
test winFile-4.1 {
    Enhanced NTFS user/group permissions: test readable only
} -constraints {
    win notNTFS notWine
} -setup {
    set user $::env(USERDOMAIN)\\$::env(USERNAME)
} -body {
    cacls $fname /E /P $user:N
    cacls $fname /E /G $user:R
    test_access $fname 1 0
} -result {}
test winFile-4.2 {
    Enhanced NTFS user/group permissions: test writable only
} -constraints {
    win notNTFS notWine
} -setup {
    set user $::env(USERDOMAIN)\\$::env(USERNAME)
} -body {
    catch {cacls $fname /E /R $user} result
    cacls $fname /E /P $user:N
    cacls $fname /E /G $user:W
    test_access $fname 0 1
} -result {}
test winFile-4.3 {
    Enhanced NTFS user/group permissions: test read+write
} -constraints {
    win notNTFS
} -setup {
    set user $::env(USERDOMAIN)\\$::env(USERNAME)
} -body {
    catch {cacls $fname /E /R $user} result
    cacls $fname /E /P $user:N
    cacls $fname /E /G $user:R
    cacls $fname /E /G $user:W
    test_access $fname 1 1
} -result {}
test winFile-4.4 {
    Enhanced NTFS user/group permissions: test full access
} -constraints {
    win notNTFS
} -setup {
    set user $::env(USERDOMAIN)\\$::env(USERNAME)
} -body {
    catch {cacls $fname /E /R $user} result
    cacls $fname /E /P $user:N
    cacls $fname /E /G $user:F
    test_access $fname 1 1
} -result {}

if {[testConstraint win]} {
    file delete $fname
}

# Symbolic links: creating them needs administrator rights or Developer Mode.
proc winSymLinksWork {} {
    set dir [makeDirectory winsymlinkprobe]
    set target [makeFile {} target $dir]
    set link [file join $dir link]
    set ok [expr {![catch {exec {*}[auto_execok mklink] \
	    [file nativename $link] [file nativename $target]}]
	    && [file type $link] eq "link"}]
    catch {file delete $link}
    catch {removeDirectory winsymlinkprobe}
    return $ok
}
testConstraint winSymLinks [expr {[testConstraint win] && [winSymLinksWork]}]
rename winSymLinksWork {}

proc mklink {args} {
    exec {*}[auto_execok mklink] {*}$args
}
proc setupLinks {} {
    set dir [makeDirectory winlinks]
    file mkdir [file join $dir sub]
    makeFile {} a.txt $dir
    makeFile {} f [file join $dir sub]
    set n [file nativename $dir]
    mklink $n\\filelink $n\\a.txt
    mklink /D $n\\dirlink $n\\sub
    # relative targets, as mklink stores them
    exec {*}[auto_execok cmd] /c "cd /d $n && mklink relfile a.txt && mklink /D reldir sub"
    return $dir
}
proc cleanupLinks {} {
    set n [file nativename [file join [temporaryDirectory] winlinks]]
    foreach l {filelink relfile} {catch {file delete $n\\$l}}
    foreach l {dirlink reldir} {catch {exec {*}[auto_execok rmdir] $n\\$l}}
    catch {removeDirectory winlinks}
}

test winFile-5.1 {file readlink: symbolic links, bug [c8d4f01a54]} -constraints {
    win winSymLinks
} -setup {
    set dir [setupLinks]
} -body {
    list [string equal -nocase [file readlink [file join $dir filelink]] \
	    [file nativename [file join $dir a.txt]]] \
	[string equal -nocase [file readlink [file join $dir dirlink]] \
	    [file nativename [file join $dir sub]]] \
	[file readlink [file join $dir relfile]] \
	[file readlink [file join $dir reldir]]
} -cleanup {
    cleanupLinks
} -result {1 1 a.txt sub}
test winFile-5.2 {file readlink: junction} -constraints {win} -setup {
    set dir [makeDirectory winlinks]
    file mkdir [file join $dir sub]
} -body {
    file link -symbolic [file join $dir junction] [file join $dir sub]
    string equal -nocase [file readlink [file join $dir junction]] \
	[file nativename [file join $dir sub]]
} -cleanup {
    catch {exec {*}[auto_execok rmdir] [file nativename [file join $dir junction]]}
    removeDirectory winlinks
} -result 1
test winFile-5.3 {file normalize through symbolic links, bug [c8d4f01a54]} -constraints {
    win winSymLinks
} -setup {
    set dir [setupLinks]
} -body {
    set expected [file normalize [file join $dir sub f]]
    list [string equal -nocase [file normalize [file join $dir dirlink f]] $expected] \
	[string equal -nocase [file normalize [file join $dir reldir f]] $expected]
} -cleanup {
    cleanupLinks
} -result {1 1}
test winFile-5.4 {file readlink: not a link} -constraints {win} -setup {
    set f [makeFile {} winnotalink]
} -body {
    list [catch {file readlink $f} msg] [string match {*not a directory} $msg]
} -cleanup {
    removeFile winnotalink
} -result {1 1}

# The same local directory seen through the administrative share.
proc adminSharePath {path} {
    set n [file nativename [file normalize $path]]
    if {![regexp {^([A-Za-z]):(.*)$} $n -> drive rest]} {
	return ""
    }
    return "\\\\localhost\\$drive\$$rest"
}
testConstraint winAdminShare [expr {[testConstraint win]
	&& [file isdirectory [adminSharePath [temporaryDirectory]]]}]

test winFile-5.5 {file readlink: symbolic link to UNC path, bug [c8d4f01a54]} -constraints {
    win winSymLinks winAdminShare
} -setup {
    set dir [setupLinks]
    set unc [adminSharePath [file join $dir sub]]
    mklink /D [file nativename [file join $dir unclink]] $unc
} -body {
    list [string equal -nocase [file readlink [file join $dir unclink]] $unc] \
	[string equal -nocase [file normalize [file join $dir unclink f]] \
	    [file normalize [file join $unc f]]]
} -cleanup {
    catch {exec {*}[auto_execok rmdir] [file nativename [file join $dir unclink]]}
    cleanupLinks
} -result {1 1}
rename mklink {}
rename setupLinks {}
rename cleanupLinks {}
rename adminSharePath {}

# cleanup
cleanupTests
return