1#!/usr/local/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7if [ "$(id -u)" -ne 0 ]; then
8  echo "$SHOWSKIP: chattr (not root)"
9  return 2>/dev/null
10  exit
11fi
12
13function clean()
14{
15  # We don't know whether the fs will have extents (e, typically true on the
16  # desktop) or be encrypted (E, typically true on Android), or have data
17  # inlined in the inode (N), or use indexed directories, so strip those out.
18  # We also don't want to rely on chattr(1) to set a known version number or
19  # project number, so blank out any numbers.
20  sed -E -e 's/, (Encrypted|Extents|Indexed_directory|Inline_Data)//g;' \
21         -e 's/[EeIN]-/--/g; s/[0-9]+/_/g'
22}
23
24mkdir testattr
25IN="cd testattr"
26OUT="cd .."
27_t="abcdefghijklmnopqrstuvwxyz"
28
29_empty="--------------------"
30
31# Check +i (immutable) works by trying to write to and remove the file.
32_i="----i---------------"
33testing "immutable" "$IN && echo "$_t" > testFile &&
34         chattr +i testFile && lsattr testFile | clean &&
35         date > testFile 2>/dev/null; rm testFile; chattr -i testFile;
36         cat testFile; rm -rf testFile; $OUT " "$_i testFile\n$_t\n" "" ""
37
38# Check +a (append-only) works by failing to write and succeeding in appending.
39_a="-----a--------------"
40testing "append-only" "$IN && echo "$_t" > testFile &&
41         chattr +a testFile &&
42         echo $_t >> testFile &&
43         date > testFile || lsattr testFile | clean &&
44         chattr -a testFile; cat testFile; rm -rf testFile; $OUT" \
45         "$_a testFile\n$_t\n$_t\n" "" ""
46
47# For the rest, just toggle the bits back and forth (where supported).
48# Note that some file system/kernel combinations do return success but
49# silently ignore your request: +T on 4.19 f2fs, or +F on 5.2 ext4,
50# for example, so we're deliberately a bit selective here.
51# f2fs in 5.6+ kernels supports compression, but you can only enable
52# compression on a file while it's still empty, so we skip +c too.
53for attr in "A" "d" "e" "j" "P" "S" "s" "t" "u"; do
54  echo "$_t" > testFile && chattr +$attr testFile 2>/dev/null || SKIPNEXT=1
55  # Check that $attr is in the lsattr output, then that - turns it back off.
56  testing "toggle $attr" "lsattr testFile | awk '{print \$1}' > attrs;
57    grep -q $attr attrs || cat attrs; cat testFile && chattr -$attr testFile &&
58    lsattr testFile | clean; rm -rf testFile" "$_t\n$_empty testFile\n" "" ""
59done
60
61_aA="-----a-A------------"
62testing "multiple bits" "$IN && touch testFile &&
63  chattr +Aa testFile && lsattr testFile | clean &&
64  chattr -Aa testFile && lsattr testFile | clean;
65  rm -rf testFile; $OUT" "$_aA testFile\n$_empty testFile\n" "" ""
66
67testing "multiple files" "$IN && touch fileA && touch fileB &&
68  chattr +Aa fileA fileB && lsattr fileA fileB | clean &&
69  chattr -Aa fileA fileB && lsattr fileA fileB | clean;
70  rm -rf testFile*; $OUT" \
71  "$_aA fileA\n$_aA fileB\n$_empty fileA\n$_empty fileB\n" "" ""
72
73touch testFile; chattr -v 1234 testFile 2>/dev/null || SKIPNEXT=1
74testing "-v version" "lsattr -v testFile | awk '{print \$1}' &&
75  chattr -v 4567 testFile && lsattr -v testFile | awk '{print \$1}';
76  rm -rf testFile" "1234\n4567\n" "" ""
77
78mkdir -p a/b/c && touch a/b/c/fA a/b/c/fB
79testing "-R" "chattr -R +a a && lsattr a/b/c/fA a/b/c/fB | clean &&
80  chattr -R -a a && rm -rf a" \
81  "$_a a/b/c/fA\n$_a a/b/c/fB\n" "" ""
82rm -rf a
83
84# Clean up
85rm -rf testattr
86