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