chmod.test revision d6f8c41e25428c8a0caf6f52fbe665d3baa06adc
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
8PERM="---""--x""-w-""-wx""r--""r-x""rw-""rwx"
9
10num2perm()
11{
12  for i in 0 1 2
13  do
14    num=${1:$i:1}
15    printf "%s" ${PERM:$(($num*3)):3}
16  done
17  echo
18}
19
20# Creating test files to test chmod command
21mkdir dir
22touch file
23
24# We don't need to test all 511 permissions
25for u in 0 1 2 3 4 5 6 7
26do
27  for g in 0 3 6
28  do
29    for o in 0 7
30    do
31      if [ "$type" == file ]
32      then
33        type=dir
34        rm -rf "./$type" && mkdir $type
35        DASH=d
36      else
37        type=file
38        rm -f "./$type" && touch $type
39        DASH=-
40      fi
41      DASHES=$(num2perm $u$g$o)
42      testing "chmod $u$g$o $type" "chmod $u$g$o $type && 
43        ls -ld $type | cut -d' ' -f 1" "$DASH$DASHES\n" "" ""
44    done
45  done
46done
47
48rm -rf dir file && mkdir dir && touch file
49testing "chmod 750 dir 640 file" \
50  "chmod 750 dir 640 file 2>/dev/null ||
51   ls -ld dir file | cut -d' ' -f 1" "drwxr-x---\n-rwxr-x---\n" "" ""
52
53rm -rf dir file && mkdir dir && touch file
54testing "chmod 666 dir file" \
55  "chmod 666 dir file &&
56   ls -ld dir file | cut -d' ' -f 1" "drw-rw-rw-\n-rw-rw-rw-\n" "" ""
57
58rm -rf dir file && mkdir dir && touch file
59testing "chmod 765 *" "chmod 765 * &&
60   ls -ld dir file | cut -d' ' -f 1" "drwxrw-r-x\n-rwxrw-r-x\n" "" ""
61
62##### u,g,o,a=r,w,x
63rm -rf dir file && mkdir dir && touch file
64testing "chmod u=r dir file" "chmod u=r dir file &&
65   ls -ld dir file | cut -d' ' -f 1" "dr--r-xr-x\n-r--r--r--\n" "" ""
66
67rm -rf dir file && mkdir dir && touch file
68testing "chmod u=w dir file" "chmod u=w dir file &&
69   ls -ld dir file | cut -d' ' -f 1" "d-w-r-xr-x\n--w-r--r--\n" "" ""
70
71rm -rf dir file && mkdir dir && touch file
72testing "chmod u=x dir file" "chmod u=x dir file &&
73   ls -ld dir file | cut -d' ' -f 1" "d--xr-xr-x\n---xr--r--\n" "" ""
74
75rm -rf dir file && mkdir dir && touch file
76testing "chmod u+r dir file" "chmod u+r dir file &&
77   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
78
79rm -rf dir file && mkdir dir && touch file
80testing "chmod u+w dir file" "chmod u+w dir file &&
81   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
82
83rm -rf dir file && mkdir dir && touch file
84testing "chmod u+x dir file" "chmod u+x dir file &&
85   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rwxr--r--\n" "" ""
86
87rm -rf dir file && mkdir dir && touch file
88testing "chmod u-r dir file" "chmod u-r dir file &&
89   ls -ld dir file | cut -d' ' -f 1" "d-wxr-xr-x\n--w-r--r--\n" "" ""
90
91rm -rf dir file && mkdir dir && touch file
92testing "chmod u-w dir file" "chmod u-w dir file &&
93   ls -ld dir file | cut -d' ' -f 1" "dr-xr-xr-x\n-r--r--r--\n" "" ""
94
95rm -rf dir file && mkdir dir && touch file
96testing "chmod u-x dir file" "chmod u-x dir file &&
97   ls -ld dir file | cut -d' ' -f 1" "drw-r-xr-x\n-rw-r--r--\n" "" ""
98
99rm -rf dir file && mkdir dir && touch file
100testing "chmod g=r dir file" "chmod g=r dir file &&
101   ls -ld dir file | cut -d' ' -f 1" "drwxr--r-x\n-rw-r--r--\n" "" ""
102
103rm -rf dir file && mkdir dir && touch file
104testing "chmod g=w dir file" "chmod g=w dir file &&
105   ls -ld dir file | cut -d' ' -f 1" "drwx-w-r-x\n-rw--w-r--\n" "" ""
106
107rm -rf dir file && mkdir dir && touch file
108testing "chmod g=x dir file" "chmod g=x dir file &&
109   ls -ld dir file | cut -d' ' -f 1" "drwx--xr-x\n-rw---xr--\n" "" ""
110
111rm -rf dir file && mkdir dir && touch file
112testing "chmod g+r dir file" "chmod g+r dir file &&
113   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
114
115rm -rf dir file && mkdir dir && touch file
116testing "chmod g+w dir file" "chmod g+w dir file &&
117   ls -ld dir file | cut -d' ' -f 1" "drwxrwxr-x\n-rw-rw-r--\n" "" ""
118
119rm -rf dir file && mkdir dir && touch file
120testing "chmod g+x dir file" "chmod g+x dir file &&
121   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r-xr--\n" "" ""
122
123rm -rf dir file && mkdir dir && touch file
124testing "chmod g-r dir file" "chmod g-r dir file &&
125   ls -ld dir file | cut -d' ' -f 1" "drwx--xr-x\n-rw----r--\n" "" ""
126
127rm -rf dir file && mkdir dir && touch file
128testing "chmod g-w dir file" "chmod g-w dir file &&
129   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
130
131rm -rf dir file && mkdir dir && touch file
132testing "chmod g-x dir file" "chmod g-x dir file &&
133   ls -ld dir file | cut -d' ' -f 1" "drwxr--r-x\n-rw-r--r--\n" "" ""
134
135rm -rf dir file && mkdir dir && touch file
136testing "chmod o=r dir file" "chmod o=r dir file &&
137   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr--\n-rw-r--r--\n" "" ""
138
139rm -rf dir file && mkdir dir && touch file
140testing "chmod o=w dir file" "chmod o=w dir file &&
141   ls -ld dir file | cut -d' ' -f 1" "drwxr-x-w-\n-rw-r---w-\n" "" ""
142
143rm -rf dir file && mkdir dir && touch file
144testing "chmod o=x dir file" "chmod o=x dir file &&
145   ls -ld dir file | cut -d' ' -f 1" "drwxr-x--x\n-rw-r----x\n" "" ""
146
147rm -rf dir file && mkdir dir && touch file
148testing "chmod o+r dir file" "chmod o+r dir file &&
149   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
150
151rm -rf dir file && mkdir dir && touch file
152testing "chmod o+w dir file" "chmod o+w dir file &&
153   ls -ld dir file | cut -d' ' -f 1" "drwxr-xrwx\n-rw-r--rw-\n" "" ""
154
155rm -rf dir file && mkdir dir && touch file
156testing "chmod o+x dir file" "chmod o+x dir file &&
157   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r-x\n" "" ""
158
159rm -rf dir file && mkdir dir && touch file
160testing "chmod o-r dir file" "chmod o-r dir file &&
161   ls -ld dir file | cut -d' ' -f 1" "drwxr-x--x\n-rw-r-----\n" "" ""
162
163rm -rf dir file && mkdir dir && touch file
164testing "chmod o-w dir file" "chmod o-w dir file &&
165   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
166
167rm -rf dir file && mkdir dir && touch file
168testing "chmod o-x dir file" "chmod o-x dir file &&
169   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr--\n-rw-r--r--\n" "" ""
170
171rm -rf dir file && mkdir dir && touch file
172testing "chmod a=r dir file" "chmod a=r dir file &&
173   ls -ld dir file | cut -d' ' -f 1" "dr--r--r--\n-r--r--r--\n" "" ""
174
175rm -rf dir file && mkdir dir && touch file
176testing "chmod a=w dir file" "chmod a=w dir file &&
177   ls -ld dir file | cut -d' ' -f 1" "d-w--w--w-\n--w--w--w-\n" "" ""
178
179rm -rf dir file && mkdir dir && touch file
180testing "chmod a=x dir file" "chmod a=x dir file &&
181   ls -ld dir file | cut -d' ' -f 1" "d--x--x--x\n---x--x--x\n" "" ""
182
183rm -rf dir file && mkdir dir && touch file
184testing "chmod a+r dir file" "chmod a+r dir file &&
185   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
186
187rm -rf dir file && mkdir dir && touch file
188testing "chmod a+w dir file" "chmod a+w dir file &&
189   ls -ld dir file | cut -d' ' -f 1" "drwxrwxrwx\n-rw-rw-rw-\n" "" ""
190
191rm -rf dir file && mkdir dir && touch file
192testing "chmod a+x dir file" "chmod a+x dir file &&
193   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rwxr-xr-x\n" "" ""
194
195rm -rf dir file && mkdir dir && touch file
196testing "chmod a-r dir file" "chmod a-r dir file &&
197   ls -ld dir file | cut -d' ' -f 1" "d-wx--x--x\n--w-------\n" "" ""
198
199rm -rf dir file && mkdir dir && touch file
200testing "chmod a-w dir file" "chmod a-w dir file &&
201   ls -ld dir file | cut -d' ' -f 1" "dr-xr-xr-x\n-r--r--r--\n" "" ""
202
203rm -rf dir file && mkdir dir && touch file
204testing "chmod a-x dir file" "chmod a-x dir file &&
205   ls -ld dir file | cut -d' ' -f 1" "drw-r--r--\n-rw-r--r--\n" "" ""
206
207rm -rf dir file && mkdir dir && touch file
208testing "chmod =r dir file" "chmod =r dir file &&
209   ls -ld dir file | cut -d' ' -f 1" "dr--r--r--\n-r--r--r--\n" "" ""
210
211rm -rf dir file && mkdir dir && touch file
212testing "chmod =w dir file" "chmod =w dir file &&
213   ls -ld dir file | cut -d' ' -f 1" "d-w-------\n--w-------\n" "" ""
214
215rm -rf dir file && mkdir dir && touch file
216testing "chmod =x dir file" "chmod =x dir file &&
217   ls -ld dir file | cut -d' ' -f 1" "d--x--x--x\n---x--x--x\n" "" ""
218
219rm -rf dir file && mkdir dir && touch file
220testing "chmod +r dir file" "chmod +r dir file &&
221   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
222
223rm -rf dir file && mkdir dir && touch file
224testing "chmod +w dir file" "chmod +w dir file &&
225   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
226
227rm -rf dir file && mkdir dir && touch file
228testing "chmod +x dir file" "chmod +x dir file &&
229   ls -ld dir file | cut -d' ' -f 1" "drwxr-xr-x\n-rwxr-xr-x\n" "" ""
230
231rm -rf dir file && mkdir dir && touch file
232testing "chmod -r dir file" "chmod -r dir file &&
233   ls -ld dir file | cut -d' ' -f 1" "d-wx--x--x\n--w-------\n" "" ""
234
235rm -rf dir file && mkdir dir && touch file
236testing "chmod -w dir file" "chmod -w dir file &&
237   ls -ld dir file | cut -d' ' -f 1" "dr-xr-xr-x\n-r--r--r--\n" "" ""
238
239rm -rf dir file && mkdir dir && touch file
240testing "chmod -x dir file" "chmod -x dir file &&
241   ls -ld dir file | cut -d' ' -f 1" "drw-r--r--\n-rw-r--r--\n" "" ""
242
243# Removing test files for cleanup purpose
244rm -rf dir file
245