summaryrefslogtreecommitdiffstats
path: root/tests/winFile.test
blob: ffbb776f6b6fe15d458d3777ab2635991a1471c2 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# 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 (c) 1997 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: winFile.test,v 1.16 2004/11/08 19:19:27 davygrvy Exp $

if {[catch {package require tcltest 2.0.2}]} {
    puts stderr "Skipping tests in [info script]. tcltest 2.0.2 required."
    return
}
namespace import -force ::tcltest::*

if {[info commands ::testvolumetype] == ""} {
    tcltest::testConstraint notNTFS 0
} else {
    if {![string equal "NTFS" [testvolumetype]]} {
	tcltest::testConstraint notNTFS 0
    } else {
	tcltest::testConstraint notNTFS 1
    }
}
if {[string equal $::tcl_platform(os) "Windows NT"] &&
	    $::tcl_platform(osVersion) >= 5.0} {
    tcltest::testConstraint win2000 1
} else {
    tcltest::testConstraint win2000 0
}

test winFile-1.1 {TclpGetUserHome} {win} {
    list [catch {glob ~nosuchuser} msg] $msg
} {1 {user "nosuchuser" doesn't exist}}
test winFile-1.2 {TclpGetUserHome} {win nt nonPortable} {
    # The administrator account should always exist.

    catch {glob ~administrator}
} {0}
test winFile-1.3 {TclpGetUserHome} {win 95} {
    # Find some user in system.ini and then see if they have a home.

    set f [open $::env(windir)/system.ini]
    set x 0
    while {![eof $f]} {
	set line [gets $f]
	if {$line == "\[Password Lists]"} {
	    gets $f
	    set name [lindex [split [gets $f] =] 0]
	    if {$name != ""} {
		set x [catch {glob ~$name}]
		break
	    }
	}
    }
    close $f
    set x
} {0}
test winFile-1.4 {TclpGetUserHome} {win nt nonPortable} {
    catch {glob ~stanton@workgroup}
} {0}

test winFile-2.1 {TclpMatchFiles: case sensitivity} {win} {
    makeFile {} GlobCapS
    set result [list [glob -nocomplain GlobC*] [glob -nocomplain globc*]]
    removeFile GlobCapS
    set result
} {GlobCapS GlobCapS}
test winFile-2.2 {TclpMatchFiles: case sensitivity} {win} {
    makeFile {} globlower
    set result [list [glob -nocomplain globl*] [glob -nocomplain gLOBl*]]
    removeFile globlower
    set result
} {globlower globlower}

test winFile-3.1 {file system} {win} {
    set res "volume types ok"
    foreach vol [file volumes] {
	# Have to catch in case there is a removable drive (CDROM, floppy)
	# with nothing in it.
	catch {
	    if {![string equal [lindex [file system $vol] 1] [testvolumetype $vol]]} {
		set res "For $vol, we found [file system $vol]\
		  and [testvolumetype $vol] are different"
		break
	    }
	}
    }
    set res
} {volume types ok}

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
proc getuser {fname} {
    set tryname $fname
    if {[file isdirectory $fname]} {
	set tryname [file dirname $fname]
    }
    set tail [file tail $tryname]
    set dirtext [exec cmd /c dir /q [file nativename $fname]]
    set owner ""
    foreach line [split $dirtext "\n"] {
	if {[string match -nocase "* $tail" $line]} {
	    set attrs [string range $line \
	      0 end-[string length $tail]]
	    regexp { [A-Z]+\\.*$} $attrs owner
	    set owner [string trim $owner]
	}
    }
    if {"" == "$owner"} {
	error "getuser: Owner not found in output of dir/q"
    }
    return $owner
}

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

proc test_writ {fname} {
    if {[catch {set ofs [open $fname w]}]} {
	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 "Problem [join $problem \n]\nActual rights are: [cacls $fname]"
    } else {
	return ""
    }
}

# 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 nt notNTFS win2000
} -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 nt notNTFS
} -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 nt 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:W
    test_access $fname 0 1
} -result {}
test winFile-4.3 {
    Enhanced NTFS user/group permissions: test read+write
} -constraints {
    win nt 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 nt 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 {}

file delete $fname

# cleanup
::tcltest::cleanupTests
return