Skip to content

Commit cc5aa8f

Browse files
committed
ima_violations.sh: Another fix of condition evaluation
c0c3550 was not enough to fix evaluation against empty $expected_violations: ima_violations 1 TINFO: verify open writers violation /opt/ltp/testcases/bin/ima_violations.sh: line 96: [: 0: unary operator expected Therefore split checks into two if. Also improvements (readability) * shorten line length with saving subtraction into variable * evaluate empty variable with ${:-} Fixes: 726ed71 ("ima_violations.sh: Update validate() to support multiple violations") Link: https://lore.kernel.org/ltp/20251211111046.87297-1-pvorel@suse.cz/ Reported-by: Martin Doucha <mdoucha@suse.cz> Signed-off-by: Petr Vorel <pvorel@suse.cz>
1 parent 6c67d40 commit cc5aa8f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

testcases/kernel/security/integrity/ima/tests/ima_violations.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,29 @@ validate()
8787
local search="$3"
8888
local expected_violations="$4"
8989
local max_attempt=3
90-
local count2 i num_violations_new
90+
local count2 diff i num_violations_new pass
9191

9292
for i in $(seq 1 $max_attempt); do
9393
read num_violations_new < $IMA_VIOLATIONS
9494
count2="$(get_count $search)"
95-
if [ -z "$expected_violations" -a $(($num_violations_new - $num_violations)) -gt 0 ] || \
96-
[ $(($num_violations_new - $num_violations)) -eq $expected_violations ]; then
97-
[ -z "$expected_violations" ] && expected_violations=1
95+
diff=$(($num_violations_new - $num_violations))
96+
97+
if [ "$expected_violations" ]; then
98+
[ $diff -eq $expected_violations ] && pass=1
99+
else
100+
[ $diff -gt 0 ] && pass=1
101+
fi
102+
103+
if [ "$pass" = 1 ]; then
98104
if [ $count2 -gt $count ]; then
99-
tst_res TPASS "$expected_violations $search violation(s) added"
105+
tst_res TPASS "${expected_violations:-1} $search violation(s) added"
100106
return
101107
else
102108
tst_res TINFO "$search not found in $LOG ($i/$max_attempt attempt)..."
103109
tst_sleep 1s
104110
fi
105-
elif [ $(($num_violations_new - $num_violations)) -gt 0 ]; then
106-
tst_res $IMA_FAIL "$search too many violations added: $num_violations_new - $num_violations"
111+
elif [ $diff -gt 0 ]; then
112+
tst_res $IMA_FAIL "$search too many violations added: $diff ($num_violations_new - $num_violations)"
107113
return
108114
else
109115
tst_res $IMA_FAIL "$search violation not added"

0 commit comments

Comments
 (0)