From 6c01e297b76fd1a4c5e46f2c234d845ea4b4a7e2 Mon Sep 17 00:00:00 2001 From: Mateusz Majewski Date: Wed, 16 Sep 2020 10:20:58 +0200 Subject: [PATCH] Finish improving the dlog_test.in script Change-Id: I37ee9aa8a0a9bc78cfea48d7f3ed0f1b9873907f --- tests/dlog_test.in | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/tests/dlog_test.in b/tests/dlog_test.in index 5284cac..689ff16 100644 --- a/tests/dlog_test.in +++ b/tests/dlog_test.in @@ -1,5 +1,10 @@ #!/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 @@ -52,6 +57,9 @@ cleanup() { [ "$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" } @@ -281,7 +289,7 @@ if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then # -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)" @@ -305,8 +313,8 @@ fi 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 @@ -359,22 +367,22 @@ test_libdlog 10 [ "$(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 @@ -448,7 +456,7 @@ LOG_DETAILS="testing if rotating file is properly created (4/4)" 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 @@ -544,8 +552,8 @@ for PARAM in always auto none never; do 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 @@ -603,7 +611,7 @@ format="long" 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" @@ -646,7 +654,7 @@ if [ "$type" = "pipe" ]; then 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 @@ -741,7 +749,11 @@ if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then [ "$(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 @@ -912,7 +924,7 @@ 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 ) @@ -942,7 +954,7 @@ if [ "$quick" -ne 1 ]; then 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="" -- 2.7.4