chgrp.test 3.7 KB

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