1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7if [ "$(id -u)" -ne 0 ]
8then
9  echo "$SHOWSKIP: chattr (not root)"
10  continue 2>/dev/null
11  exit
12fi
13
14# chattr - Testcases
15
16mkdir testattr
17IN="cd testattr"
18OUT="cd .."
19_t="abcdefghijklmnopqrstuvwxyz"
20
21testing "[-/+]i FILE[write]" "$IN && echo "$_t" > testFile &&
22         chattr +i testFile && lsattr testFile && echo "$_t" > testFile; 
23         chattr -i testFile; rm -rf testFile; $OUT " "----i-------- testFile\n" "" ""
24testing "[-/+]i FILE[re-write]" "$IN && echo "$_t" > testFile && 
25         chattr +i testFile && echo \"$_t\" > testFile || chattr -i testFile && 
26         echo \"$_t\" > testFile && lsattr testFile; rm -rf testFile; $OUT" \
27         "------------- testFile\n" "" ""
28testing "[-/+]i FILE[append]" "$IN && echo "$_t" > testFile && 
29         chattr +i testFile && echo \"$_t\" >> testFile || lsattr testFile && 
30         chattr -i testFile; rm -rf testFile; $OUT" "----i-------- testFile\n" "" ""
31testing "[-/+]i FILE[move]" "$IN && echo "$_t" > testFile && 
32         chattr +i testFile && mv testFile testFile1 || lsattr testFile && 
33         chattr -i testFile; rm -rf testFile; $OUT" "----i-------- testFile\n" "" ""
34testing "[-/+]i FILE[delete]" "$IN && echo "$_t" > testFile && 
35         chattr +i testFile && rm -f testFile || lsattr testFile && 
36         chattr -i testFile; rm -rf testFile; $OUT" "----i-------- testFile\n" "" ""
37testing "[-/+]i FILE[read]" "$IN && echo "$_t" > testFile && 
38         chattr +i testFile && cat testFile && lsattr testFile && 
39         chattr -i testFile; rm -rf testFile; $OUT" "$_t\n----i-------- testFile\n" "" ""
40testing "[-/+]a FILE[write]" "$IN && echo "$_t" > testFile && 
41         chattr +a testFile && echo $_t > testFile || lsattr testFile && 
42         chattr -a testFile; rm -rf testFile; $OUT" "-----a------- testFile\n" "" ""
43testing "[-/+]a FILE[re-write]" "$IN && echo "$_t" > testFile && 
44         chattr +a testFile && echo $_t > testFile || lsattr testFile && 
45         chattr -a testFile && echo $_t > testFile && cat testFile && 
46         lsattr testFile; rm -rf testFile; 
47         $OUT" "-----a------- testFile\n$_t\n------------- testFile\n" "" ""
48testing "[-/+]a FILE[append]" "$IN && echo "$_t" > testFile && 
49         chattr +a testFile && echo $_t >> testFile && cat testFile && 
50         lsattr testFile && chattr -a testFile; rm -rf testFile; $OUT" \
51         "$_t\n$_t\n-----a------- testFile\n" "" ""
52testing "[-/+]a FILE[move]" "$IN && echo "$_t" > testFile && 
53         chattr +a testFile && mv testFile testFile1 || lsattr testFile && 
54         chattr -a testFile; rm -rf testFile; $OUT" "-----a------- testFile\n" "" ""
55testing "[-/+]a FILE[delete]" "$IN && echo "$_t" > testFile && 
56         chattr +a testFile && rm -f testFile || lsattr testFile && 
57         chattr -a testFile; rm -rf testFile; $OUT" "-----a------- testFile\n" "" ""
58testing "[-/+]a FILE[read]" "$IN && echo "$_t" > testFile && 
59         chattr +a testFile && cat testFile && lsattr testFile && chattr -a testFile; 
60         rm -rf testFile; $OUT" "$_t\n-----a------- testFile\n" "" ""
61
62for attr in "A" "a" "c" "D" "d" "i" "j" "s" "S" "t" "T" "u"
63do
64  testing "[-/+]$attr FILE" "$IN && echo "$_t" > testFile && 
65           chattr +$attr testFile && cat testFile && chattr -$attr testFile && 
66           lsattr testFile; rm -rf testFile; $OUT" "$_t\n------------- testFile\n" "" ""
67done
68
69for attr in "A" "a" "c" "D" "d" "i" "j" "s" "S" "t" "T" "u"
70do
71  testing "-$attr FILE" "$IN && echo "$_t" > testFile && chattr -$attr testFile &&
72           cat testFile && lsattr testFile; rm -rf testFile; $OUT" "$_t\n------------- testFile\n" "" ""
73done
74
75testing "[-/+]AacDdijsStTu FILE" "$IN && echo "$_t" > testFile && 
76         chattr +AacDdijsStTu testFile && cat testFile && chattr -AacDdijsStTu testFile && 
77         lsattr testFile; rm -rf testFile; $OUT" "$_t\n------------- testFile\n" "" ""
78testing "[-/+]AacDdijsStTu(random) FILE" \
79        "$IN && echo "$_t" > testFile && 
80        chattr +AacDdijsStTu testFile && cat testFile && chattr -A testFile &&
81        chattr -a testFile && chattr -c testFile && chattr -D testFile &&
82        chattr -d testFile && chattr -i testFile && chattr -j testFile &&
83        chattr -s testFile && chattr -S testFile && chattr -t testFile &&
84        chattr -T testFile && chattr -u testFile && lsattr testFile &&
85        chattr -AacDdijsStTu testFile; rm -rf testFile; $OUT" \
86        "$_t\n------------- testFile\n" "" ""
87testing "[-/+]AacDdijsStTu FILE*" "$IN && 
88        echo "$_t" > testFile && echo "$_t" > testFile1 &&
89        echo "$_t" > testFile2 && echo "$_t" > testFile3 &&
90        echo "$_t" > testFile4 && echo "$_t" > testFile5 &&
91        echo "$_t" > testFile6 && echo "$_t" > testFile7 &&
92        echo "$_t" > testFile8 && echo "$_t" > testFile9 &&
93        echo "$_t" > testFile10 && echo "$_t" > testFile11 &&
94       chattr +AacDdijsStTu testFile* &&
95       cat testFile9 && chattr -AacDdijsStTu testFile* && lsattr testFile*; rm -rf testFile*; $OUT" \
96       "$_t\n------------- testFile\n------------- testFile1\n------------- testFile10\n------------- testFile11\n------------- testFile2\n------------- testFile3\n------------- testFile4\n------------- testFile5\n------------- testFile6\n------------- testFile7\n------------- testFile8\n------------- testFile9\n" "" ""
97testing "[-/+]AacDdijsStTu(random) FILE*" \
98        "$IN && echo "$_t" > testFile &&
99        chattr +AacDdijsStTu testFile* && cat testFile && chattr -A testFile* &&
100	chattr -a testFile* && chattr -c testFile* && chattr -D testFile* &&
101	chattr -d testFile* && chattr -i testFile* && chattr -j testFile* &&
102	chattr -s testFile* && chattr -S testFile* && chattr -t testFile* &&
103	chattr -T testFile* && chattr -u testFile* && lsattr testFile;
104        rm -rf testFile; $OUT" \
105	"$_t\n------------- testFile\n" "" ""
106testing "[-/+]i FILE[write]" \
107	"$IN && echo "$_t" > testFile &&
108	chattr +i testFile &&
109	echo \"$_t\" > testFile || lsattr testFile && chattr -i testFile;
110	rm -rf testFile; $OUT" "----i-------- testFile\n" "" ""
111testing "[-/+]A FILE[write]" \
112	"$IN && echo "$_t" > testFile && chattr +A testFile &&
113	echo \"$_t\" > testFile && lsattr testFile && chattr -A testFile;
114	rm -rf testFile; $OUT" "-------A----- testFile\n" "" ""
115testing "[-/+]s FILE[write]" \
116	"$IN && echo "$_t" > testFile && chattr +s testFile &&
117	echo \"$_t\" > testFile && lsattr testFile && chattr -s testFile
118	rm -rf testFile; $OUT" "s------------ testFile\n" "" ""
119testing "-v version FILE[write]" \
120	"$IN && echo "$_t" > testFile &&
121	chattr -v 1234 testFile && echo \"$_t\" > testFile && 
122	lsattr -v testFile; rm -rf testFile" \
123	" 1234 ------------- testFile\n" "" ""
124
125_a="-------A-----"
126testing "-R [-/+]a FILE" "$IN && touch aa && chattr -R +A aa && lsattr aa &&
127	chattr -R -A aa; rm -rf aa; $OUT" "$_a aa\n" "" ""
128testing "-R [-/+]a FILE.." "$IN && touch aa bb &&
129	chattr -R +A aa bb && lsattr aa bb &&
130	chattr -R -A aa bb; rm -rf aa bb; $OUT" "$_a aa\n$_a bb\n" "" ""
131
132# Clean up
133rm -rf testattr
134