chmod.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. # Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
  3. # Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
  4. #testing "name" "command" "result" "infile" "stdin"
  5. umask 022
  6. PERM="---""--x""-w-""-wx""r--""r-x""rw-""rwx"
  7. num2perm()
  8. {
  9. for i in 0 1 2
  10. do
  11. num=${1:$i:1}
  12. printf "%s" ${PERM:$(($num*3)):3}
  13. done
  14. echo
  15. }
  16. # Creating test files to test chmod command
  17. mkdir dir
  18. touch file
  19. # We don't need to test all 512 permissions
  20. for U in $(seq 0 7); do for G in 0 3 6; do for O in 0 7; do for T in dir file; do
  21. chmod 777 $T 2>/dev/null
  22. rm -rf $T
  23. if [ "$T" == file ]; then
  24. touch file
  25. C=-
  26. else
  27. mkdir dir
  28. C=d
  29. fi
  30. testing "$U$G$O $T" "chmod $U$G$O $T && ls -ld $T | cut -d' ' -f 1" \
  31. "${C}$(num2perm $U$G$O)\n" "" ""
  32. done; done; done; done
  33. unset U G O T C
  34. rm -rf dir file && mkdir dir && touch file 640
  35. testing "750 dir 640 file" "chmod 750 dir 640 file &&
  36. ls -ld 640 dir file | cut -d' ' -f 1 | cut -d. -f 1" \
  37. "-rwxr-x---\ndrwxr-x---\n-rwxr-x---\n" "" ""
  38. chtest()
  39. {
  40. chmod -fR 700 dir file 2>/dev/null
  41. rm -rf dir file && mkdir dir && touch file
  42. testing "$1 dir file" \
  43. "chmod $1 dir file && ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" \
  44. "$2" "" ""
  45. }
  46. chtest 666 "drw-rw-rw-\n-rw-rw-rw-\n"
  47. chtest 765 "drwxrw-r-x\n-rwxrw-r-x\n"
  48. chtest u=r "dr--r-xr-x\n-r--r--r--\n"
  49. chtest u=w "d-w-r-xr-x\n--w-r--r--\n"
  50. chtest u=x "d--xr-xr-x\n---xr--r--\n"
  51. chtest u+r "drwxr-xr-x\n-rw-r--r--\n"
  52. chtest u+w "drwxr-xr-x\n-rw-r--r--\n"
  53. chtest u+x "drwxr-xr-x\n-rwxr--r--\n"
  54. chtest u-r "d-wxr-xr-x\n--w-r--r--\n"
  55. chtest u-w "dr-xr-xr-x\n-r--r--r--\n"
  56. chtest u-x "drw-r-xr-x\n-rw-r--r--\n"
  57. chtest g=r "drwxr--r-x\n-rw-r--r--\n"
  58. chtest g=w "drwx-w-r-x\n-rw--w-r--\n"
  59. chtest g=x "drwx--xr-x\n-rw---xr--\n"
  60. chtest g+r "drwxr-xr-x\n-rw-r--r--\n"
  61. chtest g+w "drwxrwxr-x\n-rw-rw-r--\n"
  62. chtest g+x "drwxr-xr-x\n-rw-r-xr--\n"
  63. chtest g-r "drwx--xr-x\n-rw----r--\n"
  64. chtest g-w "drwxr-xr-x\n-rw-r--r--\n"
  65. chtest g-x "drwxr--r-x\n-rw-r--r--\n"
  66. chtest o=r "drwxr-xr--\n-rw-r--r--\n"
  67. chtest o=w "drwxr-x-w-\n-rw-r---w-\n"
  68. chtest o=x "drwxr-x--x\n-rw-r----x\n"
  69. chtest o+r "drwxr-xr-x\n-rw-r--r--\n"
  70. chtest o+w "drwxr-xrwx\n-rw-r--rw-\n"
  71. chtest o+x "drwxr-xr-x\n-rw-r--r-x\n"
  72. chtest o-r "drwxr-x--x\n-rw-r-----\n"
  73. chtest o-w "drwxr-xr-x\n-rw-r--r--\n"
  74. chtest o-x "drwxr-xr--\n-rw-r--r--\n"
  75. chtest a=r "dr--r--r--\n-r--r--r--\n"
  76. chtest a=w "d-w--w--w-\n--w--w--w-\n"
  77. chtest a=x "d--x--x--x\n---x--x--x\n"
  78. chtest a+r "drwxr-xr-x\n-rw-r--r--\n"
  79. chtest a+w "drwxrwxrwx\n-rw-rw-rw-\n"
  80. chtest a+x "drwxr-xr-x\n-rwxr-xr-x\n"
  81. chtest a-r "d-wx--x--x\n--w-------\n"
  82. chtest a-w "dr-xr-xr-x\n-r--r--r--\n"
  83. chtest a-x "drw-r--r--\n-rw-r--r--\n"
  84. chtest =r "dr--r--r--\n-r--r--r--\n"
  85. chtest =w "d-w-------\n--w-------\n"
  86. chtest =x "d--x--x--x\n---x--x--x\n"
  87. chtest +r "drwxr-xr-x\n-rw-r--r--\n"
  88. chtest +w "drwxr-xr-x\n-rw-r--r--\n"
  89. chtest +x "drwxr-xr-x\n-rwxr-xr-x\n"
  90. chtest -r "d-wx--x--x\n--w-------\n"
  91. chtest -w "dr-xr-xr-x\n-r--r--r--\n"
  92. chtest -x "drw-r--r--\n-rw-r--r--\n"
  93. chtest a-w,a+x "dr-xr-xr-x\n-r-xr-xr-x\n"
  94. # macOS doesn't allow +s in /tmp
  95. touch s-supported
  96. chmod +s s-supported 2>/dev/null || SKIP=1
  97. rm s-supported
  98. chtest g+s "drwxr-sr-x\n-rw-r-Sr--\n"
  99. chtest u+s "drwsr-xr-x\n-rwSr--r--\n"
  100. chtest +s "drwsr-sr-x\n-rwSr-Sr--\n"
  101. chtest o+s "drwxr-xr-x\n-rw-r--r--\n"
  102. unset SKIP
  103. chtest +t "drwxr-xr-t\n-rw-r--r-T\n"
  104. chtest a=r+w+x "drwxrwxrwx\n-rwxrwxrwx\n"
  105. # (chtest starts off with a directory that's +x...)
  106. testing "+X" \
  107. "mkdir -m 000 Xd && touch Xf && chmod +X Xd Xf && ls -ld Xd Xf | cut -d' ' -f 1" \
  108. "d--x--x--x\n-rw-r--r--\n" "" ""
  109. mkdir foo
  110. ln -s bar foo/baz
  111. # If you explicitly ask us, we'll try (and fail) to chmod a symlink.
  112. testing "-R symlink arg" 'chmod -R 750 foo/baz 2>/dev/null; echo $?' "1\n" "" ""
  113. # If you only imply that you might want us to do that, we'll skip it.
  114. testing "-R symlink recurse" 'chmod -R 750 foo; echo $?' "0\n" "" ""
  115. # Removing test files for cleanup purpose
  116. rm -rf dir file