#!/bin/sh
+# We use the something && ok || fail pattern quite a lot; it is not correct in general,
+# which makes shellcheck complain about it, but in our case it's ok to use it since
+# the ok function will never fail.
+# shellcheck disable=SC2015
+
# stuff for tracking test case counts
FAILS=0
OKS=0
[ "$UTIL_PID" -ne -1 ] && kill "$UTIL_PID" > /dev/null 2>&1
[ "$MT_TEST" -ne -1 ] && kill "$MT_TEST" > /dev/null 2>&1
[ "$LOGGER" -ne -1 ] && kill "$LOGGER" > /dev/null 2>&1
+ # Shellcheck doesn't like this because the rm call is bad if $TESTDIR is empty.
+ # However, such cases would be caught by the conditional.
+ # shellcheck disable=SC2115
[ -d "$TESTDIR" ] && rm -rf "$TESTDIR"/*
[ -d "$RUNTIME_FILTERS_DIR" ] && rm -rf "$RUNTIME_FILTERS_DIR"
}
# -g correctness
LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
- [ "$(dlogctl -g | grep '*:*' | grep -c 100)" -eq 1 ] && ok || fail
+ [ "$(dlogctl -g | grep -F '*:*' | grep -c 100)" -eq 1 ] && ok || fail
# -c correctness
LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
SEED=$(head -c 6 /dev/urandom | base64)
dlogsend -k "$SEED"
-# -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes
-while ps -o args | grep "dlogsend" | grep -v grep > /dev/null || ps -o args | grep "dlog-log-critical" | grep -v grep > /dev/null; do :; done
+# -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
+while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
LOG_DETAILS="testing if critical logging works"
cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
[ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
LOG_DETAILS="testing if filters work (1/3)"
-[ "$(dlogutil -b main -d *:E | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
+[ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
LOG_DETAILS="testing if filters work (2/3)"
-[ "$(dlogutil -b main -d *:W | wc -l)" -eq 5 ] && ok || fail
+[ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
LOG_DETAILS="testing if filters work (3/3)"
-[ "$(dlogutil -b main -d *:I | wc -l)" -eq 10 ] && ok || fail
+[ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
LOG_DETAILS="testing if exact filters work (1/3)"
-[ "$(dlogutil -b main -d *:=E | wc -l)" -eq 5 ] && ok || fail
+[ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
LOG_DETAILS="testing if exact filters work (2/3)"
-[ "$(dlogutil -b main -d *:=W | wc -l)" -eq 0 ] && ok || fail
+[ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
LOG_DETAILS="testing if exact filters work (3/3)"
-[ "$(dlogutil -b main -d *:=I | wc -l)" -eq 5 ] && ok || fail
+[ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
LOG_DETAILS="testing if adding \"silent\" filter works"
[ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
LOG_DETAILS="testing the size of log files"
-if [ $(du "$TESTDIR"/dlog_rotating_file.3 | sed "s#$TESTDIR/dlog_rotating_file.3##g") -eq 16 ]; then ok; else fail; fi # the actual size is one sector more (so 12 -> 16) because the limit is checked after reaching it, not before
+if [ "$(du "$TESTDIR"/dlog_rotating_file.3 | sed "s#$TESTDIR/dlog_rotating_file.3##g")" -eq 16 ]; then ok; else fail; fi # the actual size is one sector more (so 12 -> 16) because the limit is checked after reaching it, not before
LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
fi
# $(eval) is needed since `$COMMAND` may contain `"`
- line=$(eval $COMMAND | tr '\033\n' '~@')
- [[ $(echo "$line" | sed -re "$REGEX") == "1" ]] && ok || fail
+ line="$(eval "$COMMAND" | tr '\033\n' '~@')"
+ [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
done
done
done
REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
LOG_DETAILS="testing if \"$format\" print format works"
line=$($cmd_prefix $format)
-[[ $(echo "$line" | tr '\n' '@' | sed -re "$REGEX") == "1" ]] && ok || fail
+[ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
format="brief"
REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
dlogutil -c
- dlogsend -b main $log
+ dlogsend -b main "$log"
line=$($cmd_prefix $format | tr '\n' '@')
case $format in
[ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
LOG_DETAILS="testing proper SMACK label for dynamic config file location"
- ORIG_FILTERS_DIR=$(cat "$ORIGINAL_CONFIG_PATH" | grep dynamic_config_path | awk -F "=" '{print $2}')
+ ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
+ # Running ls on an arbitrary directory and trying to make sense of results is problematic.
+ # However, this is not a problem in our usecase, as we only care about the first field, the security context.
+ # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
+ # shellcheck disable=SC2012
[ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
if [ "$quick" -ne 1 ]; then
(
for i in $(seq 1 $SPAWN_CNT); do
delay=$(shuf -i 1-5 -n 1)
- ( sleep $delay && dlogsend -b main -t DLOG_TESTSUITE $delay ) &
+ ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
done
wait
)
LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
# collect data and analyze timestamps
- dlogutil -b main -d -v $format | (
+ dlogutil -b main -d -v "$format" | (
prev_ts=0
prev_line=""