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