# # chmod.test # # Tests for the chmod, chown and chgrp commands. #--------------------------------------------------------------------------- # Copyright 1992-1999 Karl Lehenbauer and Mark Diekhans. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose and without fee is hereby granted, provided # that the above copyright notice appear in all copies. Karl Lehenbauer and # Mark Diekhans make no representations about the suitability of this # software for any purpose. It is provided "as is" without express or # implied warranty. #------------------------------------------------------------------------------ # $Id: chmod.test,v 1.2 2002/04/02 02:29:43 hobbs Exp $ #------------------------------------------------------------------------------ # if {[lsearch [namespace children] ::tcltest] == -1} { package require tcltest namespace import ::tcltest::* } if {[cequal [info procs Test] {}]} { source [file join [file dirname [info script]] testlib.tcl] } if [cequal $tcl_platform(platform) windows] { echo " * The chmod, chown and chgrp commands have not been ported to Win32" return } #----------------------------------------------------------------------------- # This routine to the the mode of a file. It is returned formated in octal. proc GetMode {filename} { file stat $filename stat return [format "%o" [expr {$stat(mode) & 07777}]] } #----------------------------------------------------------------------------- # Certain Unix systems don't handle chmod the same. This routine test if the # system chmod produces the expected results. # o mode - symbolic mode to set the file to. # o args - list of valid expected result from ls. # proc CheckChmod {mode args} { global chmodTestsSkipped set stat 0 if {[catch { chmod 000 CHECK.TMP exec chmod $mode CHECK.TMP set sysMode [lindex [exec ls -l CHECK.TMP] 0] }] == 0} { if {[lsearch -exact $args $sysMode] >= 0} { set stat 1 } } if !$stat { incr chmodTestsSkipped } return $stat } global chmodTestsSkipped set chmodTestsSkipped 0 #----------------------------------------------------------------------------- # Procedure to return the uid of a file. proc GetUID {file} { file stat $file stat return $stat(uid) } #----------------------------------------------------------------------------- # Procedure to return the gid of a file. proc GetGID {file} { file stat $file stat return $stat(gid) } #----------------------------------------------------------------------------- # Procedure to return the uid and gid of a file. proc GetUIDGID {file} { file stat $file stat return [list $stat(uid) $stat(gid)] } #----------------------------------------------------------------------------- # If a user does not have a group name assigned, then some tests will not work, # just blow off the tests and let the user make things right. if {[catch {id group}] != 0} { echo "User '[id user]' does not have group name. Chmod tests skipped" return } #----------------------------------------------------------------------------- # Purge existing test files and recreate them. # proc SetUpTestFiles {} { foreach f {CHECK.TMP CHMOD.TMP CHMOD2.TMP} { catch { chmod a+w $f file delete $f } close [open $f w] } } SetUpTestFiles # Set the umask so that no bits are masked. Some system chmods use umask # if u, g, o or a are not specified in a symbolic chmod. umask 000 test chmod-1.1 {chmod absolute mode tests} { chmod 0000 CHMOD.TMP chmod 0101 CHMOD.TMP GetMode CHMOD.TMP } {101} test chmod-1.2 {chmod absolute mode tests} { chmod 0000 CHMOD.TMP chmod 0010 CHMOD.TMP GetMode CHMOD.TMP } {10} test chmod-1.3 {chmod absolute mode tests} { chmod 0000 CHMOD.TMP chmod 0777 CHMOD.TMP GetMode CHMOD.TMP } {777} test chmod-1.4 {chmod absolute mode tests} { chmod 0000 CHMOD.TMP chmod 0666 CHMOD.TMP GetMode CHMOD.TMP } {666} test chmod-1.5 {chmod absolute mode tests} { chmod 0000 CHMOD.TMP chmod 0705 CHMOD.TMP GetMode CHMOD.TMP } {705} test chmod-1.7 {chmod absolute mode tests} { chmod 0000 CHMOD.TMP chmod 04111 CHMOD.TMP GetMode CHMOD.TMP } {4111} test chmod-1.9 {chmod absolute mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 0000 $fh chmod -fileid 0101 $fh close $fh GetMode CHMOD.TMP } {101} test chmod-1.10 {chmod absolute mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 0000 $fh chmod -fileid 0010 $fh close $fh GetMode CHMOD.TMP } {10} test chmod-1.11 {chmod absolute mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 0000 $fh chmod -fileid 0777 $fh close $fh GetMode CHMOD.TMP } {777} test chmod-1.12 {chmod absolute mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 0000 $fh chmod -fileid 0666 $fh close $fh GetMode CHMOD.TMP } {666} test chmod-1.13 {chmod absolute mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 0000 $fh chmod -fileid 0705 $fh close $fh GetMode CHMOD.TMP } {705} test chmod-1.14 {chmod absolute mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 0000 $fh chmod -fileid 04111 $fh close $fh GetMode CHMOD.TMP } {4111} test chmod-2.1 {chmod absolute integer mode tests} { chmod 0 {CHMOD.TMP CHMOD2.TMP} chmod 65 {CHMOD.TMP CHMOD2.TMP} list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {101 101} test chmod-2.2 {chmod absolute integer mode tests} { chmod 0 {CHMOD.TMP CHMOD2.TMP} chmod 8 {CHMOD.TMP CHMOD2.TMP} list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {10 10} test chmod-2.3 {chmod absolute integer mode tests} { chmod 0 {CHMOD.TMP CHMOD2.TMP} chmod 511 {CHMOD.TMP CHMOD2.TMP} list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {777 777} test chmod-2.4 {chmod absolute integer mode tests} { chmod 0 {CHMOD.TMP CHMOD2.TMP} chmod 438 {CHMOD.TMP CHMOD2.TMP} list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {666 666} test chmod-2.5 {chmod absolute integer mode tests} { chmod 0 {CHMOD.TMP CHMOD2.TMP} chmod 453 {CHMOD.TMP CHMOD2.TMP} list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {705 705} test chmod-2.6 {chmod absolute integer mode tests} { chmod 0 CHMOD.TMP chmod 2121 CHMOD.TMP GetMode CHMOD.TMP } {4111} test chmod-2.7 {chmod absolute integer mode tests} {need_fchmod} { chmod 0600 {CHMOD.TMP CHMOD2.TMP} set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chmod -fileid 0 [list $fh1 $fh2] chmod -fileid 65 [list $fh1 $fh2] close $fh1 close $fh2 list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {101 101} test chmod-2.8 {chmod absolute integer mode tests} {need_fchmod} { chmod 0600 {CHMOD.TMP CHMOD2.TMP} set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chmod -fileid 0 [list $fh1 $fh2] chmod -fileid 8 [list $fh1 $fh2] close $fh1 close $fh2 list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {10 10} test chmod-2.9 {chmod absolute integer mode tests} {need_fchmod} { chmod 0600 {CHMOD.TMP CHMOD2.TMP} set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chmod -fileid 0 [list $fh1 $fh2] chmod -fileid 511 [list $fh1 $fh2] close $fh1 close $fh2 list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {777 777} test chmod-2.10 {chmod absolute integer mode tests} {need_fchmod} { chmod 0600 {CHMOD.TMP CHMOD2.TMP} set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chmod -fileid 0 [list $fh1 $fh2] chmod -fileid 438 [list $fh1 $fh2] close $fh1 close $fh2 list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {666 666} test chmod-2.11 {chmod absolute integer mode tests} {need_fchmod} { chmod 0600 {CHMOD.TMP CHMOD2.TMP} set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chmod -fileid 0 [list $fh1 $fh2] chmod -fileid 453 [list $fh1 $fh2] close $fh1 close $fh2 list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {705 705} test chmod-2.12 {chmod absolute integer mode tests} {need_fchmod} { chmod 0600 {CHMOD.TMP CHMOD2.TMP} set fh [open CHMOD.TMP w] chmod -fileid 0 $fh chmod -fileid 2121 $fh close $fh GetMode CHMOD.TMP } {4111} # Test symbolic mode. test chmod-3.1 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod +r CHMOD.TMP GetMode CHMOD.TMP } {444} test chmod-3.2 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod +r CHMOD.TMP chmod +w CHMOD.TMP GetMode CHMOD.TMP } {666} test chmod-3.3 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod +r CHMOD.TMP chmod +w CHMOD.TMP chmod +x CHMOD.TMP GetMode CHMOD.TMP } {777} test chmod-3.4 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod +r CHMOD.TMP chmod +w CHMOD.TMP chmod +x CHMOD.TMP chmod -r CHMOD.TMP GetMode CHMOD.TMP } {333} test chmod-3.5 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod +r CHMOD.TMP chmod +w CHMOD.TMP chmod +x CHMOD.TMP chmod -r CHMOD.TMP chmod -w CHMOD.TMP GetMode CHMOD.TMP } {111} test chmod-3.6 {chmod symbolic mode tests} { chmod 000 {CHMOD.TMP CHMOD2.TMP} chmod +r {CHMOD.TMP CHMOD2.TMP} chmod +w {CHMOD.TMP CHMOD2.TMP} chmod +x {CHMOD.TMP CHMOD2.TMP} chmod -r {CHMOD.TMP CHMOD2.TMP} chmod -w {CHMOD.TMP CHMOD2.TMP} chmod -x {CHMOD.TMP CHMOD2.TMP} list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {0 0} test chmod-3.7 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod u+x,g+x CHMOD.TMP GetMode CHMOD.TMP } {110} test chmod-3.8 {chmod symbolic mode tests} { chmod 000 {CHMOD.TMP CHMOD2.TMP} chmod u+x,g+x {CHMOD.TMP CHMOD2.TMP} chmod u-x,g-x {CHMOD.TMP CHMOD2.TMP} list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP] } {0 0} # Can't +s on some systems if [CheckChmod "ugo+x,ug+s" "---s--s--x"] { test chmod-3.9 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod ugo+x,ug+s CHMOD.TMP GetMode CHMOD.TMP } {6111} } test chmod-3.10 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod a+rwx CHMOD.TMP GetMode CHMOD.TMP } {777} test chmod-3.11 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod a+rwx CHMOD.TMP chmod a-rw CHMOD.TMP GetMode CHMOD.TMP } {111} test chmod-3.12 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod a=rwx CHMOD.TMP GetMode CHMOD.TMP } {777} test chmod-3.13 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod u=rwx,go=rx CHMOD.TMP GetMode CHMOD.TMP } {755} test chmod-3.14 {chmod symbolic mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 000 $fh chmod -fileid +r $fh close $fh GetMode CHMOD.TMP } {444} test chmod-3.2 {chmod symbolic mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 000 $fh chmod -fileid +r $fh chmod -fileid +w $fh close $fh GetMode CHMOD.TMP } {666} test chmod-3.15 {chmod symbolic mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 000 $fh chmod -fileid +r $fh chmod -fileid +w $fh chmod -fileid +x $fh close $fh GetMode CHMOD.TMP } {777} test chmod-3.16 {chmod symbolic mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 000 $fh chmod -fileid +r $fh chmod -fileid +w $fh chmod -fileid +x $fh chmod -fileid -r $fh close $fh GetMode CHMOD.TMP } {333} test chmod-3.17 {chmod symbolic mode tests} {need_fchmod} { chmod 0600 CHMOD.TMP set fh [open CHMOD.TMP w] chmod -fileid 000 $fh chmod -fileid +r $fh chmod -fileid +w $fh chmod -fileid +x $fh chmod -fileid -r $fh chmod -fileid -w $fh close $fh GetMode CHMOD.TMP } {111} # +t is dificult to test if not root, just make sure it execute and hope # for the best. Doesn't even work on some systems if [CheckChmod "u+t" "----------" "---------T"] { test chmod-3.14 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod u+x CHMOD.TMP chmod u+t CHMOD.TMP set mode [GetMode CHMOD.TMP] expr "($mode == 100) || ($mode == 1100)" } {1} test chmod-3.14 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod u+x CHMOD.TMP chmod u+t CHMOD.TMP } {} test chmod-3.15 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod u+x CHMOD.TMP chmod u+t CHMOD.TMP chmod u-t CHMOD.TMP } {} } test chmod-3.16 {chmod symbolic mode tests} { chmod 000 CHMOD.TMP chmod a+rwx CHMOD.TMP chmod u-r,g-w,o-x CHMOD.TMP GetMode CHMOD.TMP } {356} test chmod-4.1 {chmod error tests} { list [catch {chmod +z CHMOD.TMP} msg] $msg } {1 {invalid file mode "+z"}} test chmod-4.2 {chmod error tests} { list [catch {chmod} msg] $msg } {1 {wrong # args: chmod [-fileid] mode filelist}} test chmod-4.3 {chmod error tests} {need_fchmod} { list [catch {chmod -fileid +w BAD} msg] $msg } {1 {can not find channel named "BAD"}} test chmod-4.4 {chmod error tests} {need_fchmod} { list [catch {chmod -file -w CHMOD.TMP} msg] $msg } {1 {wrong # args: chmod [-fileid] mode filelist}} # chown and chgrp tests set myUID [id userid] set myGID [id groupid] # # Some machines have problems with changing group ids on files (even to your # own) if you are not root. On some it works for chown but not fchown (Ultix). # SetUpTestFiles set ::tcltest::testConstraints(chownWorks) 1 if {[catch {chown [id userid] CHMOD.TMP} msg ] || [catch {chgrp [id groupid] CHMOD.TMP} msg ]} { puts "********************************************************************" puts "Can't do chown or chgrp even when current and new group id are" puts "ours. You probably have to be root. Some chown/chgrp tests skipped" puts "on this system." puts " $msg" puts "********************************************************************" set ::tcltest::testConstraints(chownWorks) 0 } if [infox have_fchown] { set ::tcltest::testConstraints(fchownWorks) 1 set fh [open CHMOD.TMP w] if {[catch {chown -fileid [id userid] $fh} msg ] || [catch {chgrp -fileid [id groupid] $fh} msg ]} { puts "****************************************************************" puts "Can't do chown or chgrp with -fileid even when current and new" puts "groupid are ours. You probably have to be root. Some chown/chgrp" puts "tests skipped on this system." puts " $msg" puts "****************************************************************" set ::tcltest::testConstraints(fchownWorks) 0 } close $fh } test chmod-5.1 {chown tests} {chownWorks} { SetUpTestFiles chown [id user] {CHMOD.TMP CHMOD2.TMP} list [GetUID CHMOD.TMP] [GetUID CHMOD2.TMP] } [list $myUID $myUID] test chmod-5.2 {chown tests} {chownWorks} { chown [id userid] {CHMOD.TMP CHMOD2.TMP} list [GetUID CHMOD.TMP] [GetUID CHMOD2.TMP] } [list $myUID $myUID] test chmod-5.3 {chown tests} {chownWorks} { chown [list [id userid] [id groupid]] {CHMOD.TMP CHMOD2.TMP} list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP] } [list [list $myUID $myGID] [list $myUID $myGID]] test chmod-5.4 {chown tests} {chownWorks} { chown [list [id user] [id group]] {CHMOD.TMP CHMOD2.TMP} list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP] } [list [list $myUID $myGID] [list $myUID $myGID]] test chmod-5.5 {chown tests} {chownWorks} { chown [list [id user] [id group]] {CHMOD.TMP CHMOD2.TMP} list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP] } [list [list $myUID $myGID] [list $myUID $myGID]] test chmod-5.6 {chown tests} {need_fchown && fchownWorks} { SetUpTestFiles set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chown -fileid [id user] [list $fh1 $fh2] close $fh1 close $fh2 list [GetUID CHMOD.TMP] [GetUID CHMOD2.TMP] } [list $myUID $myUID] test chmod-5.7 {chown tests} {need_fchown && fchownWorks} { set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chown -fileid [id userid] [list $fh1 $fh2] close $fh1 close $fh2 list [GetUID CHMOD.TMP] [GetUID CHMOD2.TMP] } [list $myUID $myUID] test chmod-5.8 {chown tests} {need_fchown && fchownWorks} { set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chown -fileid [list [id userid] [id groupid]] [list $fh1 $fh2] close $fh1 close $fh2 list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP] } [list [list $myUID $myGID] [list $myUID $myGID]] test chmod-5.9 {chown tests} {need_fchown && fchownWorks} { set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chown -fileid [list [id user] [id group]] [list $fh1 $fh2] close $fh1 close $fh2 list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP] } [list [list $myUID $myGID] [list $myUID $myGID]] test chmod-5.10 {chown tests} {need_fchown && fchownWorks} { set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chown -fileid [list [id user] [id group]] [list $fh1 $fh2] close $fh1 close $fh2 list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP] } [list [list $myUID $myGID] [list $myUID $myGID]] SetUpTestFiles test chmod-6.1 {chown error tests} { list [catch {chown XXXXXXXXX CHMOD.TMP} msg] $msg } {1 {unknown user id: XXXXXXXXX}} test chmod-6.2 {chown error tests} { list [catch {chown [list XXXXXXXXX [id groupid]] CHMOD.TMP} msg] $msg } {1 {unknown user id: XXXXXXXXX}} test chmod-6.3 {chown error tests} { list [catch {chown [list [id user] XXXXXXXXX] CHMOD.TMP} msg] $msg } {1 {unknown group id: XXXXXXXXX}} test chmod-6.4 {chown error tests} { list [catch {chown {XXXXXXXXX YYYY} CHMOD.TMP} msg] $msg } {1 {unknown user id: XXXXXXXXX}} test chmod-6.5 {chown error tests} { list [catch {chown} msg] $msg } {1 {wrong # args: chown [-fileid] user|{user group} filelist}} test chmod-6.6 {chown error tests} {need_fchown} { list [catch {chown -fileid $myUID BAD} msg] $msg } {1 {can not find channel named "BAD"}} test chmod-6.6 {chown error tests} {need_fchown} { list [catch {chown -fid $myUID CHMOD.TMP} msg] $msg } {1 {Invalid option "-fid", expected "-fileid"}} test chmod-7.1 {chgrp tests} {chownWorks} { chgrp [id group] {CHMOD.TMP CHMOD2.TMP} list [GetGID CHMOD.TMP] [GetGID CHMOD2.TMP] } [list $myGID $myGID] test chmod-7.2 {chgrp tests} {chownWorks} { chgrp [id groupid] {CHMOD.TMP CHMOD2.TMP} list [GetGID CHMOD.TMP] [GetGID CHMOD2.TMP] } [list $myGID $myGID] test chmod-7.3 {chgrp tests} {need_fchown && fchownWorks} { set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chgrp -fileid [id group] [list $fh1 $fh2] close $fh1 close $fh2 list [GetGID CHMOD.TMP] [GetGID CHMOD2.TMP] } [list $myGID $myGID] test chmod-7.4 {chgrp tests} {need_fchown && fchownWorks} { set fh1 [open CHMOD.TMP w] set fh2 [open CHMOD2.TMP w] chgrp -fileid [id groupid] [list $fh1 $fh2] close $fh1 close $fh2 list [GetGID CHMOD.TMP] [GetGID CHMOD2.TMP] } [list $myGID $myGID] test chmod-8.1 {chgrp error tests} { list [catch {chgrp} msg] $msg } {1 {wrong # args: chgrp [-fileid] group filelist}} test chmod-8.2 {chgrp error tests} { list [catch {chgrp XXXXXXXXX CHMOD.TMP} msg] $msg } {1 {unknown group id: XXXXXXXXX}} test chmod-8.3 {chown error tests} {need_fchown} { list [catch {chgrp -fileid $myGID BAD} msg] $msg } {1 {can not find channel named "BAD"}} test chmod-8.4 {chown error tests} {need_fchown} { list [catch {chgrp -fid $myGID CHMOD.TMP} msg] $msg } {1 {Invalid option "-fid", expected "-fileid"}} TestRemove CHECK.TMP CHMOD.TMP CHMOD2.TMP # cleanup ::tcltest::cleanupTests return