test_ownership.tc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. # description: Test file and directory ownership changes for eventfs
  4. # requires: "[gid=<gid>]":README
  5. original_group=`stat -c "%g" .`
  6. original_owner=`stat -c "%u" .`
  7. local mount_point=$(get_mount_point)
  8. mount_options=$(get_mnt_options "$mount_point")
  9. # find another owner and group that is not the original
  10. other_group=`tac /etc/group | grep -v ":$original_group:" | head -1 | cut -d: -f3`
  11. other_owner=`tac /etc/passwd | grep -v ":$original_owner:" | head -1 | cut -d: -f3`
  12. # Remove any group ownership already
  13. new_options=`echo "$mount_options" | sed -e "s/gid=[0-9]*/gid=$other_group/"`
  14. if [ "$new_options" = "$mount_options" ]; then
  15. new_options="$mount_options,gid=$other_group"
  16. mount_options="$mount_options,gid=$original_group"
  17. fi
  18. canary="events/timer events/timer/timer_cancel events/timer/timer_cancel/format"
  19. test() {
  20. file=$1
  21. test_group=$2
  22. owner=`stat -c "%u" $file`
  23. group=`stat -c "%g" $file`
  24. echo "testing $file $owner=$original_owner and $group=$test_group"
  25. if [ $owner -ne $original_owner ]; then
  26. exit_fail
  27. fi
  28. if [ $group -ne $test_group ]; then
  29. exit_fail
  30. fi
  31. # Note, the remount does not update ownership so test going to and from owner
  32. echo "test owner $file to $other_owner"
  33. chown $other_owner $file
  34. owner=`stat -c "%u" $file`
  35. if [ $owner -ne $other_owner ]; then
  36. exit_fail
  37. fi
  38. chown $original_owner $file
  39. owner=`stat -c "%u" $file`
  40. if [ $owner -ne $original_owner ]; then
  41. exit_fail
  42. fi
  43. }
  44. run_tests() {
  45. for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
  46. test "$d" $other_group
  47. done
  48. chgrp $original_group events
  49. test "events" $original_group
  50. for d in "." "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
  51. test "$d" $other_group
  52. done
  53. chgrp $original_group events/sched
  54. test "events/sched" $original_group
  55. for d in "." "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
  56. test "$d" $other_group
  57. done
  58. chgrp $original_group events/sched/sched_switch
  59. test "events/sched/sched_switch" $original_group
  60. for d in "." "events/sched/sched_switch/enable" $canary; do
  61. test "$d" $other_group
  62. done
  63. chgrp $original_group events/sched/sched_switch/enable
  64. test "events/sched/sched_switch/enable" $original_group
  65. for d in "." $canary; do
  66. test "$d" $other_group
  67. done
  68. }
  69. # Run the tests twice as leftovers can cause issues
  70. for loop in 1 2 ; do
  71. echo "Running iteration $loop"
  72. mount -o remount,"$new_options" .
  73. run_tests
  74. mount -o remount,"$mount_options" .
  75. for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
  76. test "$d" $original_group
  77. done
  78. # check instances as well
  79. chgrp $other_group instances
  80. instance="$(mktemp -u test-XXXXXX)"
  81. mkdir instances/$instance
  82. cd instances/$instance
  83. run_tests
  84. cd ../..
  85. rmdir instances/$instance
  86. chgrp $original_group instances
  87. done
  88. exit 0