| 1 | # This file tests the tclWinFile.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 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 | # RCS: @(#) $Id: winFile.test,v 1.20 2007/12/14 13:52:55 patthoyts Exp $ | 
|---|
| 14 |  | 
|---|
| 15 | if {[catch {package require tcltest 2.0.2}]} { | 
|---|
| 16 |     puts stderr "Skipping tests in [info script]. tcltest 2.0.2 required." | 
|---|
| 17 |     return | 
|---|
| 18 | } | 
|---|
| 19 | namespace import -force ::tcltest::* | 
|---|
| 20 |  | 
|---|
| 21 | testConstraint testvolumetype [llength [info commands testvolumetype]] | 
|---|
| 22 | testConstraint notNTFS 0 | 
|---|
| 23 | testConstraint win2000 0 | 
|---|
| 24 |  | 
|---|
| 25 | if {[testConstraint testvolumetype]} { | 
|---|
| 26 |     testConstraint notNTFS [expr {[testvolumetype] eq "NTFS"}] | 
|---|
| 27 | } | 
|---|
| 28 | if {[testConstraint nt] && $::tcl_platform(osVersion) >= 5.0} { | 
|---|
| 29 |     testConstraint win2000 1 | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | test winFile-1.1 {TclpGetUserHome} {win} { | 
|---|
| 33 |     list [catch {glob ~nosuchuser} msg] $msg | 
|---|
| 34 | } {1 {user "nosuchuser" doesn't exist}} | 
|---|
| 35 | test winFile-1.2 {TclpGetUserHome} {win nt nonPortable} { | 
|---|
| 36 |     # The administrator account should always exist. | 
|---|
| 37 |  | 
|---|
| 38 |     catch {glob ~administrator} | 
|---|
| 39 | } {0} | 
|---|
| 40 | test winFile-1.3 {TclpGetUserHome} {win 95} { | 
|---|
| 41 |     # Find some user in system.ini and then see if they have a home. | 
|---|
| 42 |  | 
|---|
| 43 |     set f [open $::env(windir)/system.ini] | 
|---|
| 44 |     set x 0 | 
|---|
| 45 |     while {![eof $f]} { | 
|---|
| 46 |         set line [gets $f] | 
|---|
| 47 |         if {$line == "\[Password Lists]"} { | 
|---|
| 48 |             gets $f | 
|---|
| 49 |             set name [lindex [split [gets $f] =] 0] | 
|---|
| 50 |             if {$name != ""} { | 
|---|
| 51 |                 set x [catch {glob ~$name}] | 
|---|
| 52 |                 break | 
|---|
| 53 |             } | 
|---|
| 54 |         } | 
|---|
| 55 |     } | 
|---|
| 56 |     close $f | 
|---|
| 57 |     set x | 
|---|
| 58 | } {0} | 
|---|
| 59 | test winFile-1.4 {TclpGetUserHome} {win nt nonPortable} { | 
|---|
| 60 |     catch {glob ~stanton@workgroup} | 
|---|
| 61 | } {0} | 
|---|
| 62 |  | 
|---|
| 63 | test winFile-2.1 {TclpMatchFiles: case sensitivity} {win} { | 
|---|
| 64 |     makeFile {} GlobCapS | 
|---|
| 65 |     set result [list [glob -nocomplain GlobC*] [glob -nocomplain globc*]] | 
|---|
| 66 |     removeFile GlobCapS | 
|---|
| 67 |     set result | 
|---|
| 68 | } {GlobCapS GlobCapS} | 
|---|
| 69 | test winFile-2.2 {TclpMatchFiles: case sensitivity} {win} { | 
|---|
| 70 |     makeFile {} globlower | 
|---|
| 71 |     set result [list [glob -nocomplain globl*] [glob -nocomplain gLOBl*]] | 
|---|
| 72 |     removeFile globlower | 
|---|
| 73 |     set result | 
|---|
| 74 | } {globlower globlower} | 
|---|
| 75 |  | 
|---|
| 76 | test winFile-3.1 {file system} {win testvolumetype} { | 
|---|
| 77 |     set res "volume types ok" | 
|---|
| 78 |     foreach vol [file volumes] { | 
|---|
| 79 |         # Have to catch in case there is a removable drive (CDROM, floppy) | 
|---|
| 80 |         # with nothing in it. | 
|---|
| 81 |         catch { | 
|---|
| 82 |             if {![string equal [lindex [file system $vol] 1] [testvolumetype $vol]]} { | 
|---|
| 83 |                 set res "For $vol, we found [file system $vol]\ | 
|---|
| 84 |                   and [testvolumetype $vol] are different" | 
|---|
| 85 |                 break | 
|---|
| 86 |             } | 
|---|
| 87 |         } | 
|---|
| 88 |     } | 
|---|
| 89 |     set res | 
|---|
| 90 | } {volume types ok} | 
|---|
| 91 |  | 
|---|
| 92 | proc cacls {fname args} { | 
|---|
| 93 |     string trim [eval [list exec cacls [file nativename $fname]] $args <<y] | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | # dir/q output: | 
|---|
| 97 | # 2003-11-03  20:36                  598 OCTAVIAN\benny         filename.txt | 
|---|
| 98 | # Note this output from a german win2k machine: | 
|---|
| 99 | # 14.12.2007  14:26                   30 VORDEFINIERT\Administratest.dat | 
|---|
| 100 | # | 
|---|
| 101 | # Modified to cope with Msys environment and use ls -l. | 
|---|
| 102 | proc getuser {fname} { | 
|---|
| 103 |     global env | 
|---|
| 104 |     set tryname $fname | 
|---|
| 105 |     if {[file isdirectory $fname]} { | 
|---|
| 106 |         set tryname [file dirname $fname] | 
|---|
| 107 |     } | 
|---|
| 108 |     set owner "" | 
|---|
| 109 |     set tail [file tail $tryname] | 
|---|
| 110 |     if {[info exists env(OSTYPE)] && [string equal $env(OSTYPE) "msys"]} { | 
|---|
| 111 |         set dirtext [exec ls -l $fname] | 
|---|
| 112 |         foreach line [split $dirtext "\n"] { | 
|---|
| 113 |             set owner [lindex $line 2] | 
|---|
| 114 |         } | 
|---|
| 115 |     } else { | 
|---|
| 116 |         set dirtext [exec cmd /c dir /q [file nativename $fname]] | 
|---|
| 117 |         foreach line [split $dirtext "\n"] { | 
|---|
| 118 |             if {[string match -nocase "*$tail" $line]} { | 
|---|
| 119 |                 set attrs [string range $line \ | 
|---|
| 120 |                                0 end-[string length $tail]] | 
|---|
| 121 |                 regexp { [^ \\]+\\.*$} $attrs owner | 
|---|
| 122 |                 set owner [string trim $owner] | 
|---|
| 123 |             } | 
|---|
| 124 |         } | 
|---|
| 125 |     } | 
|---|
| 126 |     if {[string length $owner] == 0} { | 
|---|
| 127 |         error "getuser: Owner not found in output of dir/q" | 
|---|
| 128 |     } | 
|---|
| 129 |     return $owner | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | proc test_read {fname} { | 
|---|
| 133 |     if {[catch {set ifs [open $fname r]}]} { | 
|---|
| 134 |         return 0 | 
|---|
| 135 |     } | 
|---|
| 136 |     set readfailed [catch {read $ifs}] | 
|---|
| 137 |     return [expr {![catch {close $ifs}] && !$readfailed}] | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | proc test_writ {fname} { | 
|---|
| 141 |     if {[catch {set ofs [open $fname w]}]} { | 
|---|
| 142 |         return 0 | 
|---|
| 143 |     } | 
|---|
| 144 |     set writefailed [catch {puts $ofs "Hello"}] | 
|---|
| 145 |     return [expr {![catch {close $ofs}] && !$writefailed}] | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | proc test_access {fname read writ} { | 
|---|
| 149 |     set problem {} | 
|---|
| 150 |     foreach type {read writ} { | 
|---|
| 151 |         if {[set $type] != [file ${type}able $fname]} { | 
|---|
| 152 |             lappend problem "[set $type] != \[file ${type}able $fname\]" | 
|---|
| 153 |         } | 
|---|
| 154 |         if {[set $type] != [test_${type} $fname]} { | 
|---|
| 155 |             lappend problem "[set $type] != \[test_${type} $fname\]" | 
|---|
| 156 |         } | 
|---|
| 157 |     } | 
|---|
| 158 |     if {[llength $problem]} { | 
|---|
| 159 |         return "Problem [join $problem \n]\nActual rights are: [cacls $fname]" | 
|---|
| 160 |     } else { | 
|---|
| 161 |         return "" | 
|---|
| 162 |     } | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | # Create the test file | 
|---|
| 166 | # NOTE: [tcltest::makeFile] not used.  Presumably to force file | 
|---|
| 167 | # creation in a particular filesystem?  If not, try [makeFile] | 
|---|
| 168 | # in a -setup script. | 
|---|
| 169 | set fname test.dat | 
|---|
| 170 | file delete $fname | 
|---|
| 171 | close [open $fname w] | 
|---|
| 172 |  | 
|---|
| 173 | test winFile-4.0 { | 
|---|
| 174 |     Enhanced NTFS user/group permissions: test no acccess | 
|---|
| 175 | } -constraints { | 
|---|
| 176 |     win nt notNTFS win2000 | 
|---|
| 177 | } -setup { | 
|---|
| 178 |     set owner [getuser $fname] | 
|---|
| 179 |     set user $::env(USERDOMAIN)\\$::env(USERNAME) | 
|---|
| 180 | } -body { | 
|---|
| 181 |     # Clean out all well-known ACLs | 
|---|
| 182 |     catch {cacls $fname /E /R "Everyone"} result | 
|---|
| 183 |     catch {cacls $fname /E /R $user} result | 
|---|
| 184 |     catch {cacls $fname /E /R $owner} result | 
|---|
| 185 |     cacls $fname /E /P $user:N | 
|---|
| 186 |     test_access $fname 0 0 | 
|---|
| 187 | } -result {} | 
|---|
| 188 | test winFile-4.1 { | 
|---|
| 189 |     Enhanced NTFS user/group permissions: test readable only | 
|---|
| 190 | } -constraints { | 
|---|
| 191 |     win nt notNTFS | 
|---|
| 192 | } -setup { | 
|---|
| 193 |     set user $::env(USERDOMAIN)\\$::env(USERNAME) | 
|---|
| 194 | } -body { | 
|---|
| 195 |     cacls $fname /E /P $user:N | 
|---|
| 196 |     cacls $fname /E /G $user:R | 
|---|
| 197 |     test_access $fname 1 0 | 
|---|
| 198 | } -result {} | 
|---|
| 199 | test winFile-4.2 { | 
|---|
| 200 |     Enhanced NTFS user/group permissions: test writable only | 
|---|
| 201 | } -constraints { | 
|---|
| 202 |     win nt notNTFS | 
|---|
| 203 | } -setup { | 
|---|
| 204 |     set user $::env(USERDOMAIN)\\$::env(USERNAME) | 
|---|
| 205 | } -body { | 
|---|
| 206 |     catch {cacls $fname /E /R $user} result | 
|---|
| 207 |     cacls $fname /E /P $user:N | 
|---|
| 208 |     cacls $fname /E /G $user:W | 
|---|
| 209 |     test_access $fname 0 1 | 
|---|
| 210 | } -result {} | 
|---|
| 211 | test winFile-4.3 { | 
|---|
| 212 |     Enhanced NTFS user/group permissions: test read+write | 
|---|
| 213 | } -constraints { | 
|---|
| 214 |     win nt notNTFS | 
|---|
| 215 | } -setup { | 
|---|
| 216 |     set user $::env(USERDOMAIN)\\$::env(USERNAME) | 
|---|
| 217 | } -body { | 
|---|
| 218 |     catch {cacls $fname /E /R $user} result | 
|---|
| 219 |     cacls $fname /E /P $user:N | 
|---|
| 220 |     cacls $fname /E /G $user:R | 
|---|
| 221 |     cacls $fname /E /G $user:W | 
|---|
| 222 |     test_access $fname 1 1 | 
|---|
| 223 | } -result {} | 
|---|
| 224 | test winFile-4.4 { | 
|---|
| 225 |     Enhanced NTFS user/group permissions: test full access | 
|---|
| 226 | } -constraints { | 
|---|
| 227 |     win nt notNTFS | 
|---|
| 228 | } -setup { | 
|---|
| 229 |     set user $::env(USERDOMAIN)\\$::env(USERNAME) | 
|---|
| 230 | } -body { | 
|---|
| 231 |     catch {cacls $fname /E /R $user} result | 
|---|
| 232 |     cacls $fname /E /P $user:N | 
|---|
| 233 |     cacls $fname /E /G $user:F | 
|---|
| 234 |     test_access $fname 1 1 | 
|---|
| 235 | } -result {} | 
|---|
| 236 |  | 
|---|
| 237 | file delete $fname | 
|---|
| 238 |  | 
|---|
| 239 | # cleanup | 
|---|
| 240 | cleanupTests | 
|---|
| 241 | return | 
|---|