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 640 51testing "750 dir 640 file" "chmod 750 dir 640 file && 52 ls -ld 640 dir file | cut -d' ' -f 1 | cut -d. -f 1" \ 53 "-rwxr-x---\ndrwxr-x---\n-rwxr-x---\n" "" "" 54 55chtest() 56{ 57 rm -rf dir file && mkdir dir && touch file 58 testing "$1 dir file" \ 59 "chmod $1 dir file && ls -ld dir file | cut -d' ' -f 1 | cut -d. -f 1" \ 60 "$2" "" "" 61} 62 63chtest 666 "drw-rw-rw-\n-rw-rw-rw-\n" 64chtest 765 "drwxrw-r-x\n-rwxrw-r-x\n" 65chtest u=r "dr--r-xr-x\n-r--r--r--\n" 66chtest u=w "d-w-r-xr-x\n--w-r--r--\n" 67chtest u=x "d--xr-xr-x\n---xr--r--\n" 68chtest u+r "drwxr-xr-x\n-rw-r--r--\n" 69chtest u+w "drwxr-xr-x\n-rw-r--r--\n" 70chtest u+x "drwxr-xr-x\n-rwxr--r--\n" 71chtest u-r "d-wxr-xr-x\n--w-r--r--\n" 72chtest u-w "dr-xr-xr-x\n-r--r--r--\n" 73chtest u-x "drw-r-xr-x\n-rw-r--r--\n" 74chtest g=r "drwxr--r-x\n-rw-r--r--\n" 75chtest g=w "drwx-w-r-x\n-rw--w-r--\n" 76chtest g=x "drwx--xr-x\n-rw---xr--\n" 77chtest g+r "drwxr-xr-x\n-rw-r--r--\n" 78chtest g+w "drwxrwxr-x\n-rw-rw-r--\n" 79chtest g+x "drwxr-xr-x\n-rw-r-xr--\n" 80chtest g-r "drwx--xr-x\n-rw----r--\n" 81chtest g-w "drwxr-xr-x\n-rw-r--r--\n" 82chtest g-x "drwxr--r-x\n-rw-r--r--\n" 83chtest o=r "drwxr-xr--\n-rw-r--r--\n" 84chtest o=w "drwxr-x-w-\n-rw-r---w-\n" 85chtest o=x "drwxr-x--x\n-rw-r----x\n" 86chtest o+r "drwxr-xr-x\n-rw-r--r--\n" 87chtest o+w "drwxr-xrwx\n-rw-r--rw-\n" 88chtest o+x "drwxr-xr-x\n-rw-r--r-x\n" 89chtest o-r "drwxr-x--x\n-rw-r-----\n" 90chtest o-w "drwxr-xr-x\n-rw-r--r--\n" 91chtest o-x "drwxr-xr--\n-rw-r--r--\n" 92chtest a=r "dr--r--r--\n-r--r--r--\n" 93chtest a=w "d-w--w--w-\n--w--w--w-\n" 94chtest a=x "d--x--x--x\n---x--x--x\n" 95chtest a+r "drwxr-xr-x\n-rw-r--r--\n" 96chtest a+w "drwxrwxrwx\n-rw-rw-rw-\n" 97chtest a+x "drwxr-xr-x\n-rwxr-xr-x\n" 98chtest a-r "d-wx--x--x\n--w-------\n" 99chtest a-w "dr-xr-xr-x\n-r--r--r--\n" 100chtest a-x "drw-r--r--\n-rw-r--r--\n" 101chtest =r "dr--r--r--\n-r--r--r--\n" 102chtest =w "d-w-------\n--w-------\n" 103chtest =x "d--x--x--x\n---x--x--x\n" 104chtest +r "drwxr-xr-x\n-rw-r--r--\n" 105chtest +w "drwxr-xr-x\n-rw-r--r--\n" 106chtest +x "drwxr-xr-x\n-rwxr-xr-x\n" 107chtest -r "d-wx--x--x\n--w-------\n" 108chtest -w "dr-xr-xr-x\n-r--r--r--\n" 109chtest -x "drw-r--r--\n-rw-r--r--\n" 110chtest g+s "drwxr-sr-x\n-rw-r-Sr--\n" 111chtest u+s "drwsr-xr-x\n-rwSr--r--\n" 112chtest o+s "drwxr-xr-x\n-rw-r--r--\n" 113chtest +t "drwxr-xr-t\n-rw-r--r-T\n" 114 115# Removing test files for cleanup purpose 116rm -rf dir file 117