Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/unixFCmd.test @ 68

Last change on this file since 68 was 25, checked in by landauf, 16 years ago

added tcl to libs

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