Debian Codegraph @

tests/unixFCmd.test

raw
1 # This file tests the tclUnixFCmd.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) 1996 Sun Microsystems, Inc.
8 #
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11
12 if {"::tcltest" ni [namespace children]} {
13 package require tcltest 2.5
14 namespace import -force ::tcltest::*
15 }
16
17 ::tcltest::loadTestedCommands
18 catch [list package require -exact Tcltest [info patchlevel]]
19
20 testConstraint testchmod [llength [info commands testchmod]]
21 # File permissions broken on wsl without some "exotic" wsl configuration
22 testConstraint notWsl [expr {[llength [array names ::env *WSL*]] == 0}]
23
24 # These tests really need to be run from a writable directory, which
25 # it is assumed [temporaryDirectory] is.
26 set oldcwd [pwd]
27 cd [temporaryDirectory]
28
29 # Several tests require need to match results against the Unix username
30 set user {}
31 if {[testConstraint unix]} {
32 catch {set user [exec whoami]}
33 if {$user eq ""} {
34 catch {regexp {^[^(]*\(([^)]*)\)} [exec id] dummy user}
35 }
36 if {$user eq ""} {
37 set user "root"
38 }
39 }
40
41 # Find a group that exists on this system, or else skip tests that require
42 # groups
43 testConstraint foundGroup 0
44 if {[testConstraint unix]} {
45 catch {
46 set groupList [exec groups]
47 set group [lindex $groupList 0]
48 testConstraint foundGroup 1
49 }
50 }
51
52 # check whether -readonly attribute is supported
53 testConstraint readonlyAttr 0
54 if {[testConstraint unix]} {
55 set f [makeFile "whatever" probe]
56 catch {
57 file attributes $f -readonly
58 testConstraint readonlyAttr 1
59 }
60 removeFile probe
61 }
62
63 proc openup {path} {
64 testchmod 0o777 $path
65 if {[file isdirectory $path]} {
66 catch {
67 foreach p [glob -directory $path *] {
68 openup $p
69 }
70 }
71 }
72 }
73
74 proc cleanup {args} {
75 foreach p ". $args" {
76 set x ""
77 catch {
78 set x [glob -directory $p tf* td*]
79 }
80 foreach file $x {
81 if {
82 [catch {file delete -force -- $file}]
83 && [testConstraint testchmod]
84 } then {
85 openup $file
86 file delete -force -- $file
87 }
88 }
89 }
90 }
91
92 if {[testConstraint unix] && [testConstraint notRoot]} {
93 testConstraint execMknod [expr {![catch {exec mknod tf1 p}]}]
94 cleanup
95 }
96
97 test unixFCmd-1.1 {TclpRenameFile: EACCES} -setup {
98 cleanup
99 } -constraints {unix notRoot notWsl} -body {
100 file mkdir td1/td2/td3
101 file attributes td1/td2 -permissions 0o000
102 file rename td1/td2/td3 td2
103 } -returnCodes error -cleanup {
104 file attributes td1/td2 -permissions 0o755
105 cleanup
106 } -result {error renaming "td1/td2/td3": permission denied}
107 test unixFCmd-1.2 {TclpRenameFile: EEXIST} -setup {
108 cleanup
109 } -constraints {unix notRoot} -body {
110 file mkdir td1/td2
111 file mkdir td2
112 file rename td2 td1
113 } -returnCodes error -cleanup {
114 cleanup
115 } -result {error renaming "td2" to "td1/td2": file already exists}
116 test unixFCmd-1.3 {TclpRenameFile: EINVAL} -setup {
117 cleanup
118 } -constraints {unix notRoot} -body {
119 file mkdir td1
120 file rename td1 td1
121 } -returnCodes error -cleanup {
122 cleanup
123 } -result {error renaming "td1" to "td1/td1": trying to rename a volume or move a directory into itself}
124 test unixFCmd-1.4 {TclpRenameFile: EISDIR} {emptyTest unix notRoot} {
125 # can't make it happen
126 } {}
127 test unixFCmd-1.5 {TclpRenameFile: ENOENT} -setup {
128 cleanup
129 } -constraints {unix notRoot} -body {
130 file mkdir td1
131 file rename td2 td1
132 } -returnCodes error -cleanup {
133 cleanup
134 } -result {error renaming "td2": no such file or directory}
135 test unixFCmd-1.6 {TclpRenameFile: ENOTDIR} {emptyTest unix notRoot} {
136 # can't make it happen
137 } {}
138 test unixFCmd-1.7 {TclpRenameFile: EXDEV} -setup {
139 cleanup
140 } -constraints {unix notRoot notWsl} -body {
141 file mkdir foo/bar
142 file attr foo -perm 0o40555
143 file rename foo/bar /tmp
144 } -returnCodes error -cleanup {
145 catch {file delete /tmp/bar}
146 catch {file attr foo -perm 0o40777}
147 catch {file delete -force foo}
148 } -match glob -result {*: permission denied}
149 test unixFCmd-1.8 {Checking EINTR Bug} {unix notRoot nonPortable} {
150 testalarm
151 after 2000
152 list [testgotsig] [testgotsig]
153 } {1 0}
154 test unixFCmd-1.9 {Checking EINTR Bug} -constraints {unix notRoot nonPortable} -setup {
155 cleanup
156 set f [open tfalarm w]
157 puts $f {
158 after 2000
159 puts "hello world"
160 exit 0
161 }
162 close $f
163 } -body {
164 testalarm
165 set pipe [open "|[info nameofexecutable] tfalarm" r+]
166 set line [read $pipe 1]
167 catch {close $pipe}
168 list $line [testgotsig]
169 } -cleanup {
170 cleanup
171 } -result {h 1}
172
173 test unixFCmd-2.1 {TclpCopyFile: target exists: lstat(dst) == 0} -setup {
174 cleanup
175 } -constraints {unix notRoot} -body {
176 close [open tf1 a]
177 close [open tf2 a]
178 file copy -force tf1 tf2
179 } -cleanup {
180 cleanup
181 } -result {}
182 test unixFCmd-2.2.1 {TclpCopyFile: src is symlink} -setup {
183 cleanup
184 } -constraints {unix notRoot dontCopyLinks} -body {
185 # copying links should end up with real files
186 close [open tf1 a]
187 file link -symbolic tf2 tf1
188 file copy tf2 tf3
189 file type tf3
190 } -cleanup {
191 cleanup
192 } -result file
193 test unixFCmd-2.2.2 {TclpCopyFile: src is symlink} -setup {
194 cleanup
195 } -constraints {unix notRoot} -body {
196 # copying links should end up with the links copied
197 close [open tf1 a]
198 file link -symbolic tf2 tf1
199 file copy tf2 tf3
200 file type tf3
201 } -cleanup {
202 cleanup
203 } -result link
204 test unixFCmd-2.3 {TclpCopyFile: src is block} -setup {
205 cleanup
206 } -constraints {unix notRoot} -body {
207 set null "/dev/null"
208 while {[file type $null] != "characterSpecial"} {
209 set null [file join [file dirname $null] [file readlink $null]]
210 }
211 # file copy $null tf1
212 } -result {}
213 test unixFCmd-2.4 {TclpCopyFile: src is fifo} -setup {
214 cleanup
215 } -constraints {unix notRoot execMknod} -body {
216 exec mknod tf1 p
217 file copy tf1 tf2
218 list [file type tf1] [file type tf2]
219 } -cleanup {
220 cleanup
221 } -result {fifo fifo}
222 test unixFCmd-2.5 {TclpCopyFile: copy attributes} -setup {
223 cleanup
224 } -constraints {unix notRoot notWsl} -body {
225 close [open tf1 a]
226 file attributes tf1 -permissions 0o472
227 file copy tf1 tf2
228 format 0o%03o [file attributes tf2 -permissions]
229 } -cleanup {
230 cleanup
231 } -result 0o472 ;# i.e. perms field of [exec ls -l tf2] is -r--rwx-w-
232
233 test unixFCmd-3.1 {CopyFile not done} {emptyTest unix notRoot} {
234 } {}
235
236 test unixFCmd-4.1 {TclpDeleteFile not done} {emptyTest unix notRoot} {
237 } {}
238
239 test unixFCmd-5.1 {TclpCreateDirectory not done} {emptyTest unix notRoot} {
240 } {}
241
242 test unixFCmd-6.1 {TclpCopyDirectory not done} {emptyTest unix notRoot} {
243 } {}
244
245 test unixFCmd-7.1 {TclpRemoveDirectory not done} {emptyTest unix notRoot} {
246 } {}
247
248 test unixFCmd-8.1 {TraverseUnixTree not done} {emptyTest unix notRoot} {
249 } {}
250
251 test unixFCmd-9.1 {TraversalCopy not done} {emptyTest unix notRoot} {
252 } {}
253
254 test unixFCmd-10.1 {TraversalDelete not done} {emptyTest unix notRoot} {
255 } {}
256
257 test unixFCmd-11.1 {CopyFileAttrs not done} {emptyTest unix notRoot} {
258 } {}
259
260 test unixFCmd-12.1 {GetGroupAttribute - file not found} -setup {
261 catch {file delete -force -- foo.test}
262 } -constraints {unix notRoot} -returnCodes error -body {
263 file attributes foo.test -group
264 } -result {could not read "foo.test": no such file or directory}
265 test unixFCmd-12.2 {GetGroupAttribute - file found} -setup {
266 catch {file delete -force -- foo.test}
267 } -constraints {unix notRoot} -body {
268 close [open foo.test w]
269 file attributes foo.test -group
270 } -cleanup {
271 file delete -force -- foo.test
272 } -match glob -result *
273
274 test unixFCmd-13.1 {GetOwnerAttribute - file not found} -setup {
275 catch {file delete -force -- foo.test}
276 } -constraints {unix notRoot} -returnCodes error -body {
277 file attributes foo.test -group
278 } -result {could not read "foo.test": no such file or directory}
279 test unixFCmd-13.2 {GetOwnerAttribute} -setup {
280 catch {file delete -force -- foo.test}
281 } -constraints {unix notRoot} -body {
282 close [open foo.test w]
283 file attributes foo.test -owner
284 } -cleanup {
285 file delete -force -- foo.test
286 } -result $user
287
288 test unixFCmd-14.1 {GetPermissionsAttribute - file not found} -setup {
289 catch {file delete -force -- foo.test}
290 } -constraints {unix notRoot} -returnCodes error -body {
291 file attributes foo.test -permissions
292 } -result {could not read "foo.test": no such file or directory}
293 test unixFCmd-14.2 {GetPermissionsAttribute} -setup {
294 catch {file delete -force -- foo.test}
295 } -constraints {unix notRoot} -body {
296 close [open foo.test w]
297 file attribute foo.test -permissions
298 } -cleanup {
299 file delete -force -- foo.test
300 } -match glob -result *
301
302 #groups hard to test
303 test unixFCmd-15.1 {SetGroupAttribute - invalid group} -setup {
304 catch {file delete -force -- foo.test}
305 } -constraints {unix notRoot} -body {
306 file attributes foo.test -group foozzz
307 } -returnCodes error -cleanup {
308 file delete -force -- foo.test
309 } -result {could not set group for file "foo.test": group "foozzz" does not exist}
310 test unixFCmd-15.2 {SetGroupAttribute - invalid file} -setup {
311 catch {file delete -force -- foo.test}
312 } -constraints {unix notRoot foundGroup} -returnCodes error -body {
313 file attributes foo.test -group $group
314 } -result {could not set group for file "foo.test": no such file or directory}
315
316 #changing owners hard to do
317 test unixFCmd-16.1 {SetOwnerAttribute - current owner} -setup {
318 catch {file delete -force -- foo.test}
319 } -constraints {unix notRoot} -body {
320 close [open foo.test w]
321 list [file attributes foo.test -owner $user] \
322 [file attributes foo.test -owner]
323 } -cleanup {
324 file delete -force -- foo.test
325 } -result [list {} $user]
326 test unixFCmd-16.2 {SetOwnerAttribute - invalid file} -setup {
327 catch {file delete -force -- foo.test}
328 } -constraints {unix notRoot} -returnCodes error -body {
329 file attributes foo.test -owner $user
330 } -result {could not set owner for file "foo.test": no such file or directory}
331 test unixFCmd-16.3 {SetOwnerAttribute - invalid owner} -setup {
332 catch {file delete -force -- foo.test}
333 } -constraints {unix notRoot} -returnCodes error -body {
334 file attributes foo.test -owner foozzz
335 } -result {could not set owner for file "foo.test": user "foozzz" does not exist}
336
337 test unixFCmd-17.1 {SetPermissionsAttribute} -setup {
338 catch {file delete -force -- foo.test}
339 } -constraints {unix notRoot notWsl} -body {
340 close [open foo.test w]
341 list [file attributes foo.test -permissions 0o000] \
342 [format 0o%03o [file attributes foo.test -permissions]]
343 } -cleanup {
344 file delete -force -- foo.test
345 } -result {{} 0o000}
346 test unixFCmd-17.2 {SetPermissionsAttribute} -setup {
347 catch {file delete -force -- foo.test}
348 } -constraints {unix notRoot} -returnCodes error -body {
349 file attributes foo.test -permissions 0o000
350 } -result {could not set permissions for file "foo.test": no such file or directory}
351 test unixFCmd-17.3 {SetPermissionsAttribute} -setup {
352 catch {file delete -force -- foo.test}
353 } -constraints {unix notRoot} -body {
354 close [open foo.test w]
355 file attributes foo.test -permissions foo
356 } -cleanup {
357 file delete -force -- foo.test
358 } -returnCodes error -result {unknown permission string format "foo"}
359 test unixFCmd-17.4 {SetPermissionsAttribute} -setup {
360 catch {file delete -force -- foo.test}
361 } -constraints {unix notRoot} -body {
362 close [open foo.test w]
363 file attributes foo.test -permissions ---rwx
364 } -cleanup {
365 file delete -force -- foo.test
366 } -returnCodes error -result {unknown permission string format "---rwx"}
367
368 close [open foo.test w]
369 set ::i 4
370 proc permcheck {testnum permList expected} {
371 test $testnum {SetPermissionsAttribute} {unix notRoot notWsl} {
372 set result {}
373 foreach permstr $permList {
374 file attributes foo.test -permissions $permstr
375 lappend result [format 0o%03o [file attributes foo.test -permissions]]
376 }
377 set result
378 } $expected
379 }
380 permcheck unixFCmd-17.5 rwxrwxrwx 0o777
381 permcheck unixFCmd-17.6 r--r---w- 0o442
382 permcheck unixFCmd-17.7 {0 u+rwx,g+r u-w o+rwx} {0o000 0o740 0o540 0o547}
383 permcheck unixFCmd-17.11 --x--x--x 0o111
384 permcheck unixFCmd-17.12 {0 a+rwx} {0o000 0o777}
385 file delete -force -- foo.test
386
387 test unixFCmd-18.1 {Unix pwd} -constraints {unix notRoot nonPortable} -setup {
388 set cd [pwd]
389 } -body {
390 # This test is non-portable because SunOS generates a weird error
391 # message when the current directory isn't readable.
392 set nd $cd/tstdir
393 file mkdir $nd
394 cd $nd
395 file attributes $nd -permissions 0o000
396 pwd
397 } -returnCodes error -cleanup {
398 cd $cd
399 file attributes $nd -permissions 0o755
400 file delete $nd
401 } -match glob -result {error getting working directory name:*}
402
403 test unixFCmd-19.1 {GetReadOnlyAttribute - file not found} -setup {
404 catch {file delete -force -- foo.test}
405 } -constraints {unix notRoot readonlyAttr} -returnCodes error -body {
406 file attributes foo.test -readonly
407 } -result {could not read "foo.test": no such file or directory}
408 test unixFCmd-19.2 {GetReadOnlyAttribute} -setup {
409 catch {file delete -force -- foo.test}
410 } -constraints {unix notRoot readonlyAttr} -body {
411 close [open foo.test w]
412 file attribute foo.test -readonly
413 } -cleanup {
414 file delete -force -- foo.test
415 } -result 0
416
417 test unixFCmd-20.1 {SetReadOnlyAttribute} -setup {
418 catch {file delete -force -- foo.test}
419 } -constraints {unix notRoot readonlyAttr} -body {
420 close [open foo.test w]
421 list [catch {file attributes foo.test -readonly 1} msg] $msg \
422 [catch {file attribute foo.test -readonly} msg] $msg \
423 [catch {file delete -force -- foo.test}] \
424 [catch {file attributes foo.test -readonly 0} msg] $msg \
425 [catch {file attribute foo.test -readonly} msg] $msg
426 } -cleanup {
427 file delete -force -- foo.test
428 } -result {0 {} 0 1 1 0 {} 0 0}
429 test unixFCmd-20.2 {SetReadOnlyAttribute} -setup {
430 catch {file delete -force -- foo.test}
431 } -constraints {unix notRoot readonlyAttr} -returnCodes error -body {
432 file attributes foo.test -readonly 1
433 } -result {could not read "foo.test": no such file or directory}
434
435 # cleanup
436 cleanup
437 cd $oldcwd
438 ::tcltest::cleanupTests
439 return
440
441 # Local Variables:
442 # mode: tcl
443 # End: