aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2020-02-05 15:37:16 -0800
committerRob Landley <rob@landley.net>2020-02-11 09:00:43 -0600
commit8cfcf6d01d86655efec09353fb8f4db0c65e420a (patch)
tree11714cc1c2a1c11fbdc351711505c47328986b59 /tests
parent7d15b37b5c23c628929f161dc7df9121c6070cec (diff)
downloadtoybox-8cfcf6d01d86655efec09353fb8f4db0c65e420a.tar.gz
chattr: fix exit status, redo the tests.
The e2fsprogs chattr(1) returns failure when it fails to do what was asked of it, and so should we. Fixing this then reveals a lot of issues with the tests that were being accidentally swept under the carpet. The bulk of this patch is going through all the tests, removing the duplicates and making the remaining tests more thorough. I've tested this on ext4 and f2fs on a variety of 4.x and 5.x kernel versions (but nothing older). We might need to reduce the list of attribtues we try to toggle, but the more thorough tests use well-supported attributes. I've also fixed the -R test to actually involve a directory hierarchy.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/chattr.test155
1 files changed, 40 insertions, 115 deletions
diff --git a/tests/chattr.test b/tests/chattr.test
index d7dec0bc..cefc84b9 100755
--- a/tests/chattr.test
+++ b/tests/chattr.test
@@ -28,131 +28,56 @@ _t="abcdefghijklmnopqrstuvwxyz"
_empty="--------------------"
-# 'i' -- immutable
+# Check +i (immutable) works by trying to write to and remove the file.
_i="----i---------------"
-testing "[-/+]i FILE[write]" "$IN && touch testFile &&
+testing "immutable" "$IN && echo "$_t" > testFile &&
chattr +i testFile && lsattr testFile | clean &&
- date > testFile 2>/dev/null; chattr -i testFile;
- rm -rf testFile; $OUT " "$_i testFile\n" "" ""
-testing "[-/+]i FILE[re-write]" "$IN && touch testFile &&
- chattr +i testFile && date > testFile 2>/dev/null ||
- chattr -i testFile && date > testFile 2>/dev/null &&
- lsattr testFile | clean; rm -rf testFile 2>/dev/null; $OUT" \
- "$_empty testFile\n" "" ""
-testing "[-/+]i FILE[append]" "$IN && date > testFile &&
- chattr +i testFile && date >> testFile 2>/dev/null ||
- lsattr testFile | clean && chattr -i testFile; rm -rf testFile; $OUT" \
- "$_i testFile\n" "" ""
-testing "[-/+]i FILE[move]" "$IN && date > testFile &&
- chattr +i testFile && mv testFile testFile1 2>/dev/null ||
- lsattr testFile | clean && chattr -i testFile; rm -rf testFile; $OUT" \
- "$_i testFile\n" "" ""
-testing "[-/+]i FILE[delete]" "$IN && touch testFile && chattr +i testFile &&
- rm -f testFile 2>/dev/null || lsattr testFile | clean &&
- chattr -i testFile; rm -rf testFile; $OUT" "$_i testFile\n" "" ""
-testing "[-/+]i FILE[read]" "$IN && echo "$_t" > testFile &&
- chattr +i testFile && cat testFile && lsattr testFile | clean &&
- chattr -i testFile; rm -rf testFile; $OUT" "$_t\n$_i testFile\n" "" ""
+ date > testFile 2>/dev/null; rm testFile; chattr -i testFile;
+ cat testFile; rm -rf testFile; $OUT " "$_i testFile\n$_t\n" "" ""
-# 'a' --- append-only
+# Check +a (append-only) works by failing to write and succeeding in appending.
_a="-----a--------------"
-testing "[-/+]a FILE[write]" "$IN && echo "$_t" > testFile &&
- chattr +a testFile && echo $_t > testFile || lsattr testFile | clean &&
- chattr -a testFile; rm -rf testFile; $OUT" "$_a testFile\n" "" ""
-testing "[-/+]a FILE[re-write]" "$IN && echo "$_t" > testFile &&
- chattr +a testFile && echo $_t > testFile || lsattr testFile | clean &&
- chattr -a testFile && echo $_t > testFile && cat testFile &&
- lsattr testFile | clean; rm -rf testFile;
- $OUT" "$_a testFile\n$_t\n$_empty testFile\n" "" ""
-testing "[-/+]a FILE[append]" "$IN && echo "$_t" > testFile &&
- chattr +a testFile && echo $_t >> testFile && cat testFile &&
- lsattr testFile | clean && chattr -a testFile; rm -rf testFile; $OUT" \
- "$_t\n$_t\n$_a testFile\n" "" ""
-testing "[-/+]a FILE[move]" "$IN && echo "$_t" > testFile &&
- chattr +a testFile && mv testFile testFile1 ||
- lsattr testFile | clean && chattr -a testFile; rm -rf testFile; $OUT" \
- "$_a testFile\n" "" ""
-testing "[-/+]a FILE[delete]" "$IN && echo "$_t" > testFile &&
- chattr +a testFile && rm -f testFile || lsattr testFile | clean &&
- chattr -a testFile; rm -rf testFile; $OUT" "$_a testFile\n" "" ""
-testing "[-/+]a FILE[read]" "$IN && echo "$_t" > testFile &&
- chattr +a testFile && cat testFile && lsattr testFile | clean &&
- chattr -a testFile; rm -rf testFile; $OUT" "$_t\n$_a testFile\n" "" ""
+testing "append-only" "$IN && echo "$_t" > testFile &&
+ chattr +a testFile &&
+ echo $_t >> testFile &&
+ date > testFile || lsattr testFile | clean &&
+ chattr -a testFile; cat testFile; rm -rf testFile; $OUT" \
+ "$_a testFile\n$_t\n$_t\n" "" ""
-for attr in "A" "a" "c" "D" "d" "i" "j" "s" "S" "t" "T" "u"
-do
- testing "[-/+]$attr FILE" "$IN && echo "$_t" > testFile &&
- chattr +$attr testFile && cat testFile && chattr -$attr testFile &&
- lsattr testFile | clean; rm -rf testFile; $OUT" "$_t\n$_empty testFile\n" "" ""
+# For the rest, just toggle the bits back and forth (where supported).
+# Note that some file system/kernel combinations do return success but
+# silently ignore your request: +T on 4.19 f2fs, or +F on 5.2i ext4,
+# for example, so we're deliberately a bit selective here.
+for attr in "A" "c" "d" "e" "j" "P" "S" "s" "t" "u"; do
+ echo "$_t" > testFile && chattr +$attr testFile 2>/dev/null || SKIPNEXT=1
+ # Check that $attr is in the lsattr output, then that - turns it back off.
+ testing "toggle $attr" "lsattr testFile | awk '{print \$1}' > attrs;
+ grep -q $attr attrs || cat attrs; cat testFile && chattr -$attr testFile &&
+ lsattr testFile | clean; rm -rf testFile" "$_t\n$_empty testFile\n" "" ""
done
-for attr in "A" "a" "c" "D" "d" "i" "j" "s" "S" "t" "T" "u"
-do
- testing "-$attr FILE" "$IN && echo "$_t" > testFile && chattr -$attr testFile &&
- cat testFile && lsattr testFile | clean; rm -rf testFile; $OUT" "$_t\n$_empty testFile\n" "" ""
-done
+_aA="-----a-A------------"
+testing "multiple bits" "$IN && touch testFile &&
+ chattr +Aa testFile && lsattr testFile | clean &&
+ chattr -Aa testFile && lsattr testFile | clean;
+ rm -rf testFile; $OUT" "$_aA testFile\n$_empty testFile\n" "" ""
-testing "[-/+]AacDdijsStTu FILE" "$IN && echo "$_t" > testFile &&
- chattr +AacDdijsStTu testFile && cat testFile && chattr -AacDdijsStTu testFile &&
- lsattr testFile | clean; rm -rf testFile; $OUT" "$_t\n$_empty testFile\n" "" ""
-testing "[-/+]AacDdijsStTu(random) FILE" \
- "$IN && echo "$_t" > testFile &&
- chattr +AacDdijsStTu testFile && cat testFile && chattr -A testFile &&
- chattr -a testFile && chattr -c testFile && chattr -D testFile &&
- chattr -d testFile && chattr -i testFile && chattr -j testFile &&
- chattr -s testFile && chattr -S testFile && chattr -t testFile &&
- chattr -T testFile && chattr -u testFile && lsattr testFile | clean &&
- chattr -AacDdijsStTu testFile; rm -rf testFile; $OUT" \
- "$_t\n$_empty testFile\n" "" ""
-testing "[-/+]AacDdijsStTu FILE*" "$IN &&
- echo "$_t" > testFile && echo "$_t" > testFile1 &&
- echo "$_t" > testFile2 && echo "$_t" > testFile3 &&
- echo "$_t" > testFile4 && echo "$_t" > testFile5 &&
- echo "$_t" > testFile6 && echo "$_t" > testFile7 &&
- echo "$_t" > testFile8 && echo "$_t" > testFile9 &&
- echo "$_t" > testFile10 && echo "$_t" > testFile11 &&
- chattr +AacDdijsStTu testFile* &&
- cat testFile9 && chattr -AacDdijsStTu testFile* &&
- lsattr testFile* | clean; rm -rf testFile*; $OUT" \
- "$_t\n$_empty testFile\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n$_empty testFile_\n" "" ""
+testing "multiple files" "$IN && touch fileA && touch fileB &&
+ chattr +Aa fileA fileB && lsattr fileA fileB | clean &&
+ chattr -Aa fileA fileB && lsattr fileA fileB | clean;
+ rm -rf testFile*; $OUT" \
+ "$_aA fileA\n$_aA fileB\n$_empty fileA\n$_empty fileB\n" "" ""
-# 'A' --- no-atime
-_A="-------A------------"
-# 's' --- secure erase
-_s="s-------------------"
-testing "[-/+]AacDdijsStTu(random) FILE*" \
- "$IN && echo "$_t" > testFile &&
- chattr +AacDdijsStTu testFile* && cat testFile && chattr -A testFile* &&
- chattr -a testFile* && chattr -c testFile* && chattr -D testFile* &&
- chattr -d testFile* && chattr -i testFile* && chattr -j testFile* &&
- chattr -s testFile* && chattr -S testFile* && chattr -t testFile* &&
- chattr -T testFile* && chattr -u testFile* && lsattr testFile | clean;
- rm -rf testFile; $OUT" \
- "$_t\n$_empty testFile\n" "" ""
-testing "[-/+]i FILE[write]" \
- "$IN && echo "$_t" > testFile &&
- chattr +i testFile &&
- echo \"$_t\" > testFile || lsattr testFile | clean && chattr -i testFile;
- rm -rf testFile; $OUT" "$_i testFile\n" "" ""
-testing "[-/+]A FILE[write]" \
- "$IN && echo "$_t" > testFile && chattr +A testFile &&
- echo \"$_t\" > testFile && lsattr testFile | clean && chattr -A testFile;
- rm -rf testFile; $OUT" "$_A testFile\n" "" ""
-testing "[-/+]s FILE[write]" \
- "$IN && echo "$_t" > testFile && chattr +s testFile &&
- echo \"$_t\" > testFile && lsattr testFile | clean && chattr -s testFile
- rm -rf testFile; $OUT" "$_s testFile\n" "" ""
-NOSPACE=1 testing "-v version FILE[write]" \
- "$IN && echo "$_t" > testFile &&
- chattr -v 1234 testFile && echo \"$_t\" > testFile &&
- lsattr -v testFile | clean; rm -rf testFile" \
- "_ $_empty testFile\n" "" ""
+touch testFile; chattr -v 1234 testFile 2>/dev/null || SKIPNEXT=1
+testing "-v version" "lsattr -v testFile | awk '{print \$1}' &&
+ chattr -v 4567 testFile && lsattr -v testFile | awk '{print \$1}';
+ rm -rf testFile" "1234\n4567\n" "" ""
-testing "-R [-/+]a FILE" "$IN && touch aa && chattr -R +A aa &&
- lsattr aa | clean && chattr -R -A aa; rm -rf aa; $OUT" "$_A aa\n" "" ""
-testing "-R [-/+]a FILE.." "$IN && touch aa bb &&
- chattr -R +A aa bb && lsattr aa bb | clean &&
- chattr -R -A aa bb; rm -rf aa bb; $OUT" "$_A aa\n$_A bb\n" "" ""
+mkdir -p a/b/c && touch a/b/c/fA a/b/c/fB
+testing "-R" "chattr -R +a a && lsattr a/b/c/fA a/b/c/fB | clean &&
+ chattr -R -a a && rm -rf a" \
+ "$_a a/b/c/fA\n$_a a/b/c/fB\n" "" ""
+rm -rf a
# Clean up
rm -rf testattr