1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7# chattr - Testcases
8
9mkdir testattr
10IN="cd testattr"
11OUT="cd .."
12_t="abcdefghijklmnopqrstuvwxyz"
13
14testing "chattr [-/+]i FILE[write]" "$IN && echo "$_t" > testFile &&
15         chattr +i testFile && lsattr testFile && echo "$_t" > testFile; 
16         chattr -i testFile; rm -rf testFile; $OUT " "----i-------- testFile\n" "" ""
17testing "chattr [-/+]i FILE[re-write]" "$IN && echo "$_t" > testFile && 
18         chattr +i testFile && echo \"$_t\" > testFile || chattr -i testFile && 
19         echo \"$_t\" > testFile && lsattr testFile; rm -rf testFile; $OUT" \
20         "------------- testFile\n" "" ""
21testing "chattr [-/+]i FILE[append]" "$IN && echo "$_t" > testFile && 
22         chattr +i testFile && echo \"$_t\" >> testFile || lsattr testFile && 
23         chattr -i testFile; rm -rf testFile; $OUT" "----i-------- testFile\n" "" ""
24testing "chattr [-/+]i FILE[move]" "$IN && echo "$_t" > testFile && 
25         chattr +i testFile && mv testFile testFile1 || lsattr testFile && 
26         chattr -i testFile; rm -rf testFile; $OUT" "----i-------- testFile\n" "" ""
27testing "chattr [-/+]i FILE[delete]" "$IN && echo "$_t" > testFile && 
28         chattr +i testFile && rm -f testFile || lsattr testFile && 
29         chattr -i testFile; rm -rf testFile; $OUT" "----i-------- testFile\n" "" ""
30testing "chattr [-/+]i FILE[read]" "$IN && echo "$_t" > testFile && 
31         chattr +i testFile && cat testFile && lsattr testFile && 
32         chattr -i testFile; rm -rf testFile; $OUT" "$_t\n----i-------- testFile\n" "" ""
33testing "chattr [-/+]a FILE[write]" "$IN && echo "$_t" > testFile && 
34         chattr +a testFile && echo $_t > testFile || lsattr testFile && 
35         chattr -a testFile; rm -rf testFile; $OUT" "-----a------- testFile\n" "" ""
36testing "chattr [-/+]a FILE[re-write]" "$IN && echo "$_t" > testFile && 
37         chattr +a testFile && echo $_t > testFile || lsattr testFile && 
38         chattr -a testFile && echo $_t > testFile && cat testFile && 
39         lsattr testFile; rm -rf testFile; 
40         $OUT" "-----a------- testFile\n$_t\n------------- testFile\n" "" ""
41testing "chattr [-/+]a FILE[append]" "$IN && echo "$_t" > testFile && 
42         chattr +a testFile && echo $_t >> testFile && cat testFile && 
43         lsattr testFile && chattr -a testFile; rm -rf testFile; $OUT" \
44         "$_t\n$_t\n-----a------- testFile\n" "" ""
45testing "chattr [-/+]a FILE[move]" "$IN && echo "$_t" > testFile && 
46         chattr +a testFile && mv testFile testFile1 || lsattr testFile && 
47         chattr -a testFile; rm -rf testFile; $OUT" "-----a------- testFile\n" "" ""
48testing "chattr [-/+]a FILE[delete]" "$IN && echo "$_t" > testFile && 
49         chattr +a testFile && rm -f testFile || lsattr testFile && 
50         chattr -a testFile; rm -rf testFile; $OUT" "-----a------- testFile\n" "" ""
51testing "chattr [-/+]a FILE[read]" "$IN && echo "$_t" > testFile && 
52         chattr +a testFile && cat testFile && lsattr testFile && chattr -a testFile; 
53         rm -rf testFile; $OUT" "$_t\n-----a------- testFile\n" "" ""
54
55for attr in "A" "a" "c" "D" "d" "i" "j" "s" "S" "t" "T" "u"
56do
57  testing "chattr [-/+]$attr FILE" "$IN && echo "$_t" > testFile && 
58           chattr +$attr testFile && cat testFile && chattr -$attr testFile && 
59           lsattr testFile; rm -rf testFile; $OUT" "$_t\n------------- testFile\n" "" ""
60done
61
62for attr in "A" "a" "c" "D" "d" "i" "j" "s" "S" "t" "T" "u"
63do
64  testing "chattr -$attr FILE" "$IN && echo "$_t" > testFile && chattr -$attr testFile &&
65           cat testFile && lsattr testFile; rm -rf testFile; $OUT" "$_t\n------------- testFile\n" "" ""
66done
67
68testing "chattr [-/+]AacDdijsStTu FILE" "$IN && echo "$_t" > testFile && 
69         chattr +AacDdijsStTu testFile && cat testFile && chattr -AacDdijsStTu testFile && 
70         lsattr testFile; rm -rf testFile; $OUT" "$_t\n------------- testFile\n" "" ""
71testing "chattr [-/+]AacDdijsStTu(random) FILE" \
72        "$IN && echo "$_t" > testFile && 
73        chattr +AacDdijsStTu testFile && cat testFile && chattr -A testFile &&
74        chattr -a testFile && chattr -c testFile && chattr -D testFile &&
75        chattr -d testFile && chattr -i testFile && chattr -j testFile &&
76        chattr -s testFile && chattr -S testFile && chattr -t testFile &&
77        chattr -T testFile && chattr -u testFile && lsattr testFile &&
78        chattr -AacDdijsStTu testFile; rm -rf testFile; $OUT" \
79        "$_t\n------------- testFile\n" "" ""
80testing "chattr [-/+]AacDdijsStTu FILE*" "$IN && 
81        echo "$_t" > testFile && echo "$_t" > testFile1 &&
82        echo "$_t" > testFile2 && echo "$_t" > testFile3 &&
83        echo "$_t" > testFile4 && echo "$_t" > testFile5 &&
84        echo "$_t" > testFile6 && echo "$_t" > testFile7 &&
85        echo "$_t" > testFile8 && echo "$_t" > testFile9 &&
86        echo "$_t" > testFile10 && echo "$_t" > testFile11 &&
87       chattr +AacDdijsStTu testFile* &&
88       cat testFile9 && chattr -AacDdijsStTu testFile* && lsattr testFile*; rm -rf testFile*; $OUT" \
89       "$_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" "" ""
90testing "chattr [-/+]AacDdijsStTu(random) FILE*" \
91        "$IN && echo "$_t" > testFile &&
92        chattr +AacDdijsStTu testFile* && cat testFile && chattr -A testFile* &&
93	chattr -a testFile* && chattr -c testFile* && chattr -D testFile* &&
94	chattr -d testFile* && chattr -i testFile* && chattr -j testFile* &&
95	chattr -s testFile* && chattr -S testFile* && chattr -t testFile* &&
96	chattr -T testFile* && chattr -u testFile* && lsattr testFile;
97        rm -rf testFile; $OUT" \
98	"$_t\n------------- testFile\n" "" ""
99testing "chattr [-/+]i FILE[write]" \
100	"$IN && echo "$_t" > testFile &&
101	chattr +i testFile &&
102	echo \"$_t\" > testFile || lsattr testFile && chattr -i testFile;
103	rm -rf testFile; $OUT" "----i-------- testFile\n" "" ""
104testing "chattr [-/+]A FILE[write]" \
105	"$IN && echo "$_t" > testFile && chattr +A testFile &&
106	echo \"$_t\" > testFile && lsattr testFile && chattr -A testFile;
107	rm -rf testFile; $OUT" "-------A----- testFile\n" "" ""
108testing "chattr [-/+]s FILE[write]" \
109	"$IN && echo "$_t" > testFile && chattr +s testFile &&
110	echo \"$_t\" > testFile && lsattr testFile && chattr -s testFile
111	rm -rf testFile; $OUT" "s------------ testFile\n" "" ""
112testing "chattr -v version FILE[write]" \
113	"$IN && echo "$_t" > testFile &&
114	chattr -v 1234 testFile && echo \"$_t\" > testFile && 
115	lsattr -v testFile; rm -rf testFile" \
116	" 1234 ------------- testFile\n" "" ""
117
118_a="-------A-----"
119testing "chattr -R [-/+]a FILE" "$IN && touch aa && chattr -R +A aa && lsattr aa &&
120	chattr -R -A aa; rm -rf aa; $OUT" "$_a aa\n" "" ""
121testing "chattr -R [-/+]a FILE.." "$IN && touch aa bb &&
122	chattr -R +A aa bb && lsattr aa bb &&
123	chattr -R -A aa bb; rm -rf aa bb; $OUT" "$_a aa\n$_a bb\n" "" ""
124
125# Clean up
126rm -rf testattr
127
128# lsattr - Testcases
129mkdir dir && cd dir && touch file
130chattr +A file &>/dev/null
131
132_p=$PWD
133_b="-------------"
134_A="-------A-----"
135
136testing "lsattr file" "lsattr file" "$_A file\n" "" ""
137testing "lsattr file_path" "lsattr $_p/file" "$_A $_p/file\n" "" ""
138testing "lsattr -R file" "lsattr -R file" "$_A file\n" "" ""
139testing "lsattr -R file_path" "lsattr -R $_p/file" "$_A $_p/file\n" "" ""
140testing "lsattr -a file" "lsattr -a file" "$_A file\n" "" ""
141testing "lsattr -a file_path" "lsattr -a $_p/file" "$_A $_p/file\n" "" ""
142testing "lsattr -d ." "lsattr -d ." "$_b .\n" "" ""
143testing "lsattr -d dir_path" "lsattr -d $_p" "$_b $_p\n" "" ""
144testing "lsattr -d file" "lsattr -d file" "$_A file\n" "" ""
145testing "lsattr -d file_path" "lsattr -d $_p/file" "$_A $_p/file\n" "" ""
146sp_44="                                            "
147testing "lsattr -l file" "lsattr -l file" "file  $sp_44 No_Atime\n" "" ""
148_v="12345"
149testing "lsattr -v file" "chattr -v $_v * && lsattr -v file" \
150  "$_v $_A file\n" "" ""
151testing "lsattr -v file_path" "chattr -v $_v * && lsattr -v $_p/file" \
152  "$_v $_A $_p/file\n" "" ""
153testing "lsattr -Radlv file1 file2" "chattr -v $_v * &&
154   lsattr -Radlv file input" \
155  "$_v file  $sp_44 No_Atime\n$_v input $sp_44 ---\n" "" ""
156
157# Cleanup
158chattr -AacDdijsStTu file && cd ..
159rm -rf dir
160