blob: 77106a69ff24ee7a289d5271515d0992896b32f3 [file] [log] [blame]
Divya Kotharief0ed682014-07-04 21:20:02 -05001#!/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
Rob Landleyd6f8c412014-07-04 21:21:45 -05008PERM="---""--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}
Divya Kotharief0ed682014-07-04 21:20:02 -050019
20# Creating test files to test chmod command
21mkdir dir
22touch file
23
Rob Landleyd6f8c412014-07-04 21:21:45 -050024# We don't need to test all 511 permissions
25for u in 0 1 2 3 4 5 6 7
Divya Kotharief0ed682014-07-04 21:20:02 -050026do
Rob Landleyd6f8c412014-07-04 21:21:45 -050027 for g in 0 3 6
Divya Kotharief0ed682014-07-04 21:20:02 -050028 do
Rob Landleyd6f8c412014-07-04 21:21:45 -050029 for o in 0 7
Divya Kotharief0ed682014-07-04 21:20:02 -050030 do
Rob Landleyd6f8c412014-07-04 21:21:45 -050031 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 &&
Divya Kothari207b6a62014-09-03 13:32:32 -050043 ls -ld $type | cut -d' ' -f 1 | cut -d. -f 1" "$DASH$DASHES\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050044 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 ||
Divya Kothari207b6a62014-09-03 13:32:32 -050051 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-x---\n-rwxr-x---\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050052
53rm -rf dir file && mkdir dir && touch file
54testing "chmod 666 dir file" \
55 "chmod 666 dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050056 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drw-rw-rw-\n-rw-rw-rw-\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050057
58rm -rf dir file && mkdir dir && touch file
59testing "chmod 765 *" "chmod 765 * &&
Divya Kothari207b6a62014-09-03 13:32:32 -050060 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxrw-r-x\n-rwxrw-r-x\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050061
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 &&
Divya Kothari207b6a62014-09-03 13:32:32 -050065 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "dr--r-xr-x\n-r--r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050066
67rm -rf dir file && mkdir dir && touch file
68testing "chmod u=w dir file" "chmod u=w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050069 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d-w-r-xr-x\n--w-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050070
71rm -rf dir file && mkdir dir && touch file
72testing "chmod u=x dir file" "chmod u=x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050073 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d--xr-xr-x\n---xr--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050074
75rm -rf dir file && mkdir dir && touch file
76testing "chmod u+r dir file" "chmod u+r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050077 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050078
79rm -rf dir file && mkdir dir && touch file
80testing "chmod u+w dir file" "chmod u+w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050081 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050082
83rm -rf dir file && mkdir dir && touch file
84testing "chmod u+x dir file" "chmod u+x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050085 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rwxr--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050086
87rm -rf dir file && mkdir dir && touch file
88testing "chmod u-r dir file" "chmod u-r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050089 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d-wxr-xr-x\n--w-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050090
91rm -rf dir file && mkdir dir && touch file
92testing "chmod u-w dir file" "chmod u-w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050093 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "dr-xr-xr-x\n-r--r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050094
95rm -rf dir file && mkdir dir && touch file
96testing "chmod u-x dir file" "chmod u-x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -050097 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drw-r-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -050098
99rm -rf dir file && mkdir dir && touch file
100testing "chmod g=r dir file" "chmod g=r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500101 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr--r-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500102
103rm -rf dir file && mkdir dir && touch file
104testing "chmod g=w dir file" "chmod g=w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500105 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwx-w-r-x\n-rw--w-r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500106
107rm -rf dir file && mkdir dir && touch file
108testing "chmod g=x dir file" "chmod g=x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500109 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwx--xr-x\n-rw---xr--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500110
111rm -rf dir file && mkdir dir && touch file
112testing "chmod g+r dir file" "chmod g+r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500113 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500114
115rm -rf dir file && mkdir dir && touch file
116testing "chmod g+w dir file" "chmod g+w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500117 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxrwxr-x\n-rw-rw-r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500118
119rm -rf dir file && mkdir dir && touch file
120testing "chmod g+x dir file" "chmod g+x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500121 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r-xr--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500122
123rm -rf dir file && mkdir dir && touch file
124testing "chmod g-r dir file" "chmod g-r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500125 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwx--xr-x\n-rw----r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500126
127rm -rf dir file && mkdir dir && touch file
128testing "chmod g-w dir file" "chmod g-w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500129 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500130
131rm -rf dir file && mkdir dir && touch file
132testing "chmod g-x dir file" "chmod g-x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500133 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr--r-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500134
135rm -rf dir file && mkdir dir && touch file
136testing "chmod o=r dir file" "chmod o=r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500137 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr--\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500138
139rm -rf dir file && mkdir dir && touch file
140testing "chmod o=w dir file" "chmod o=w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500141 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-x-w-\n-rw-r---w-\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500142
143rm -rf dir file && mkdir dir && touch file
144testing "chmod o=x dir file" "chmod o=x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500145 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-x--x\n-rw-r----x\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500146
147rm -rf dir file && mkdir dir && touch file
148testing "chmod o+r dir file" "chmod o+r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500149 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500150
151rm -rf dir file && mkdir dir && touch file
152testing "chmod o+w dir file" "chmod o+w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500153 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xrwx\n-rw-r--rw-\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500154
155rm -rf dir file && mkdir dir && touch file
156testing "chmod o+x dir file" "chmod o+x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500157 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r-x\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500158
159rm -rf dir file && mkdir dir && touch file
160testing "chmod o-r dir file" "chmod o-r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500161 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-x--x\n-rw-r-----\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500162
163rm -rf dir file && mkdir dir && touch file
164testing "chmod o-w dir file" "chmod o-w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500165 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500166
167rm -rf dir file && mkdir dir && touch file
168testing "chmod o-x dir file" "chmod o-x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500169 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr--\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500170
171rm -rf dir file && mkdir dir && touch file
172testing "chmod a=r dir file" "chmod a=r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500173 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "dr--r--r--\n-r--r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500174
175rm -rf dir file && mkdir dir && touch file
176testing "chmod a=w dir file" "chmod a=w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500177 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d-w--w--w-\n--w--w--w-\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500178
179rm -rf dir file && mkdir dir && touch file
180testing "chmod a=x dir file" "chmod a=x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500181 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d--x--x--x\n---x--x--x\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500182
183rm -rf dir file && mkdir dir && touch file
184testing "chmod a+r dir file" "chmod a+r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500185 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500186
187rm -rf dir file && mkdir dir && touch file
188testing "chmod a+w dir file" "chmod a+w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500189 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxrwxrwx\n-rw-rw-rw-\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500190
191rm -rf dir file && mkdir dir && touch file
192testing "chmod a+x dir file" "chmod a+x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500193 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rwxr-xr-x\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500194
195rm -rf dir file && mkdir dir && touch file
196testing "chmod a-r dir file" "chmod a-r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500197 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d-wx--x--x\n--w-------\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500198
199rm -rf dir file && mkdir dir && touch file
200testing "chmod a-w dir file" "chmod a-w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500201 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "dr-xr-xr-x\n-r--r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500202
203rm -rf dir file && mkdir dir && touch file
204testing "chmod a-x dir file" "chmod a-x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500205 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drw-r--r--\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500206
207rm -rf dir file && mkdir dir && touch file
208testing "chmod =r dir file" "chmod =r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500209 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "dr--r--r--\n-r--r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500210
211rm -rf dir file && mkdir dir && touch file
212testing "chmod =w dir file" "chmod =w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500213 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d-w-------\n--w-------\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500214
215rm -rf dir file && mkdir dir && touch file
216testing "chmod =x dir file" "chmod =x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500217 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d--x--x--x\n---x--x--x\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500218
219rm -rf dir file && mkdir dir && touch file
220testing "chmod +r dir file" "chmod +r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500221 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500222
223rm -rf dir file && mkdir dir && touch file
224testing "chmod +w dir file" "chmod +w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500225 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500226
227rm -rf dir file && mkdir dir && touch file
228testing "chmod +x dir file" "chmod +x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500229 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drwxr-xr-x\n-rwxr-xr-x\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500230
231rm -rf dir file && mkdir dir && touch file
232testing "chmod -r dir file" "chmod -r dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500233 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "d-wx--x--x\n--w-------\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500234
235rm -rf dir file && mkdir dir && touch file
236testing "chmod -w dir file" "chmod -w dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500237 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "dr-xr-xr-x\n-r--r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500238
239rm -rf dir file && mkdir dir && touch file
240testing "chmod -x dir file" "chmod -x dir file &&
Divya Kothari207b6a62014-09-03 13:32:32 -0500241 ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" "drw-r--r--\n-rw-r--r--\n" "" ""
Divya Kotharief0ed682014-07-04 21:20:02 -0500242
243# Removing test files for cleanup purpose
244rm -rf dir file