1 #!/bin/bash 2 3 [ -f testing.sh ] && . testing.sh 4 5 if [ "$(id -u)" -ne 0 ] 6 then 7 echo "$SHOWSKIP: chgrp (not root)" 8 continue 2>/dev/null 9 exit 10 fi 11 12 # We chgrp between "root" and the last group in /etc/group. 13 14 GRP="$(sed -n '$s/:.*//p' /etc/group)" 15 16 # Set up a little testing hierarchy 17 18 rm -rf testdir && 19 mkdir -p testdir/dir/dir/dir testdir/dir2 && 20 touch testdir/dir/file && 21 ln -s ../dir/dir testdir/dir2/dir && 22 ln -s ../dir/file testdir/dir2/file || exit 1 23 24 # Wrapper to reset groups and return results 25 26 IN="cd testdir && chgrp -R $GRP dir dir2 &&" 27 OUT="&& cd .. && echo \$(ls -lR testdir | awk '{print \$4}')" 28 29 # The groups returned by $OUT are, in order: 30 # dir dir2 dir/dir dir/file dir/dir/dir dir2/dir dir2/file 31 32 #testing "name" "command" "result" "infile" "stdin" 33 34 # Basic smoketest 35 testing "chgrp dir" "$IN chgrp root dir $OUT" \ 36 "root $GRP $GRP $GRP $GRP $GRP $GRP\n" "" "" 37 testing "chgrp file" "$IN chgrp root dir/file $OUT" \ 38 "$GRP $GRP $GRP root $GRP $GRP $GRP\n" "" "" 39 testing "chgrp dir and file" "$IN chgrp root dir dir/file $OUT" \ 40 "root $GRP $GRP root $GRP $GRP $GRP\n" "" "" 41 42 # symlinks (affect target, not symlink) 43 testing "chgrp symlink->file" "$IN chgrp root dir2/file $OUT" \ 44 "$GRP $GRP $GRP root $GRP $GRP $GRP\n" "" "" 45 testing "chgrp symlink->dir" "$IN chgrp root dir2/dir $OUT" \ 46 "$GRP $GRP root $GRP $GRP $GRP $GRP\n" "" "" 47 testing "chgrp -h symlink->dir" "$IN chgrp -h root dir2/dir $OUT" \ 48 "$GRP $GRP $GRP $GRP $GRP root $GRP\n" "" "" 49 50 # What does -h do (affect symlink, not target) 51 testing "chgrp -h symlink->file" "$IN chgrp -h root dir2/file $OUT" \ 52 "$GRP $GRP $GRP $GRP $GRP $GRP root\n" "" "" 53 testing "chgrp -h symlink->dir" "$IN chgrp -h root dir2/dir $OUT" \ 54 "$GRP $GRP $GRP $GRP $GRP root $GRP\n" "" "" 55 56 # chgrp -R (note, -h is implied by -R) 57 58 testing "chgrp -R dir" "$IN chgrp -R root dir $OUT" \ 59 "root $GRP root root root $GRP $GRP\n" "" "" 60 testing "chgrp -R dir2" "$IN chgrp -R root dir2 $OUT" \ 61 "$GRP root $GRP $GRP $GRP root root\n" "" "" 62 testing "chgrp -R symlink->dir" "$IN chgrp -R root dir2/dir $OUT" \ 63 "$GRP $GRP $GRP $GRP $GRP root $GRP\n" "" "" 64 testing "chgrp -R symlink->file" "$IN chgrp -R root dir2/file $OUT" \ 65 "$GRP $GRP $GRP $GRP $GRP $GRP root\n" "" "" 66 67 # chgrp -RP (same as -R by itself) 68 69 testing "chgrp -RP dir2" "$IN chgrp -RP root dir2 $OUT" \ 70 "$GRP root $GRP $GRP $GRP root root\n" "" "" 71 testing "chgrp -RP symlink->dir" "$IN chgrp -RP root dir2/dir $OUT" \ 72 "$GRP $GRP $GRP $GRP $GRP root $GRP\n" "" "" 73 testing "chgrp -RP symlink->file" "$IN chgrp -RP root dir2/file $OUT" \ 74 "$GRP $GRP $GRP $GRP $GRP $GRP root\n" "" "" 75 76 # chgrp -RH (change target but only recurse through symlink->dir on cmdline) 77 78 testing "chgrp -RH dir2" "$IN chgrp -RH root dir2 $OUT" \ 79 "$GRP root root root $GRP $GRP $GRP\n" "" "" 80 testing "chgrp -RH symlink->dir" "$IN chgrp -RH root dir2/dir $OUT" \ 81 "$GRP $GRP root $GRP root $GRP $GRP\n" "" "" 82 testing "chgrp -RH symlink->file" "$IN chgrp -RH root dir2/file $OUT" \ 83 "$GRP $GRP $GRP root $GRP $GRP $GRP\n" "" "" 84 85 # chgrp -RL (change target and always recurse through symlink->dir) 86 87 testing "chgrp -RL dir2" "$IN chgrp -RL root dir2 $OUT" \ 88 "$GRP root root root root $GRP $GRP\n" "" "" 89 testing "chgrp -RL symlink->dir" "$IN chgrp -RL root dir2/dir $OUT" \ 90 "$GRP $GRP root $GRP root $GRP $GRP\n" "" "" 91 testing "chgrp -RL symlink->file" "$IN chgrp -RL root dir2/file $OUT" \ 92 "$GRP $GRP $GRP root $GRP $GRP $GRP\n" "" "" 93 94 # -HLP are NOPs without -R 95 testing "chgrp -H without -R" "$IN chgrp -H root dir2/dir $OUT" \ 96 "$GRP $GRP root $GRP $GRP $GRP $GRP\n" "" "" 97 testing "chgrp -L without -R" "$IN chgrp -L root dir2/dir $OUT" \ 98 "$GRP $GRP root $GRP $GRP $GRP $GRP\n" "" "" 99 testing "chgrp -P without -R" "$IN chgrp -P root dir2/dir $OUT" \ 100 "$GRP $GRP root $GRP $GRP $GRP $GRP\n" "" "" 101 102 rm -rf testdir 103