3 # stuff for tracking test case counts
9 #relevant pids default vals
15 TEST_DYNAMIC_FILTERS="@DYNAMIC_FILTERS@"
16 TESTDIR=/var/lib/dlog-tests
22 time=$(echo "$2" | awk -F '[ +]' '{print $2}')
23 ts=$(date +%s%N -d "$time")
26 time=$(echo "$2" | awk -F '[ +.]' '{print $3}')
27 ms=$(echo "$2" | awk -F '[ +.]' '{print $4}')
28 sec=$(date +%s%N -d "$time")
29 ns=$((10#$ms * 1000000))
33 time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
34 ts=$(date +%s%N -d "$time")
37 time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
38 ts=$(date +%s%N -d "$time")
41 time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
42 ts=$(date +%s%N -d "$time")
45 ts=$(echo "$2" | awk -F '[ +]' '{print $1}' | sed -e 's/\.//g')
52 [ "$UTIL_PID" -ne -1 ] && kill "$UTIL_PID" > /dev/null 2>&1
53 [ "$MT_TEST" -ne -1 ] && kill "$MT_TEST" > /dev/null 2>&1
54 [ "$LOGGER" -ne -1 ] && kill "$LOGGER" > /dev/null 2>&1
55 [ -d "$TESTDIR" ] && rm -rf "$TESTDIR"/*
56 [ -d "$RUNTIME_FILTERS_DIR" ] && rm -rf "$RUNTIME_FILTERS_DIR"
63 if [ "$LOGGER" -ne -1 ] && [ -z "$(ps -o pid= -p "$LOGGER")" ]; then
70 check_daemon && daemon_status="[logger daemon not running]"
74 printf "[%02d] FAILED: %s %s\n" "$TOTAL" "$LOG_DETAILS" "$daemon_status"
79 check_daemon && daemon_status="[logger daemon not running]"
83 printf "[%02d] PASSED: %s %s\n" "$TOTAL" "$LOG_DETAILS" "$daemon_status"
87 USAGE_MESSAGE="usage: $0 [--verbose] [--quick] pipe|logger"
89 OPTS=$(getopt --shell sh --options "" --long verbose,quick --quiet -- "$@")
90 if [ "$?" -eq 1 ]; then
111 echo "This should never happen :)"
117 if [ "$1" = "pipe" ]; then
119 elif [ "$1" = "logger" ]; then
121 # We still accept the legacy {pipe,logger}_quick syntax because there's no reason not to.
122 elif [ "$1" = "pipe_quick" ]; then
125 elif [ "$1" = "logger_quick" ]; then
129 echo "$USAGE_MESSAGE"
133 # supress stderr messages from subcommands
134 if [ "$verbose" -eq 0 ]; then
139 capsh --print | grep Current | grep cap_syslog > /dev/null || { echo "*** ERROR: cap_syslog missing"; NEEDS_TO_QUIT=1; }
140 mount | grep ' / ' | grep rw > /dev/null || { echo "*** ERROR: root not mounted read-write"; NEEDS_TO_QUIT=1; }
141 [ "$NEEDS_TO_QUIT" -eq 0 ] || exit 1
143 if [ -z "$DLOG_CONFIG_PATH" ]; then
144 ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
146 ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
149 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
150 PATH=$PATH:@libexecdir@/libdlog/
152 #create dir for runtime filters
153 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
154 mkdir -p "$RUNTIME_FILTERS_DIR"
157 if [ "$type" = "pipe" ]; then
163 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
166 dlogutil -c -b radio -b system -b main
167 LOG_DETAILS="dlogctl --disable (no args)"
169 [ "$(dlogctl -g | grep -c ENABLED)" -eq 0 ] && ok || fail
170 LOG_DETAILS="testing if filters were applied"
171 dlogsend -b system -t TEST test
172 dlogsend -b main -t TEST test
173 dlogsend -b radio -t TEST test
174 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
176 LOG_DETAILS="dlogctl --enable (no args)"
178 [ "$(dlogctl -g | grep -c DISABLED)" -eq 0 ] && ok || fail
179 LOG_DETAILS="testing if filters were applied"
180 dlogutil -c -b radio -b system -b main
181 dlogsend -b system -t TEST test
182 dlogsend -b main -t TEST test
183 dlogsend -b radio -t TEST test
184 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
186 LOG_DETAILS="dlogctl --disable (1 arg)"
187 dlogctl --disable -b system
188 [ "$(dlogctl -g | grep DISABLED | grep -c system)" -eq 1 ] &&
189 [ "$(dlogctl -g | grep -c DISABLED )" -eq 1 ] && ok || fail
190 LOG_DETAILS="testing if filters were applied"
191 dlogutil -c -b radio -b system -b main
192 dlogsend -b system -t TEST test
193 dlogsend -b main -t TEST test
194 dlogsend -b radio -t TEST test
195 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
197 LOG_DETAILS="dlogctl --disable (multiple args)"
198 dlogctl --disable -b main -b radio
199 [ "$(dlogctl -g | grep DISABLED | grep -cv system)" -eq 2 ] &&
200 [ "$(dlogctl -g | grep DISABLED | grep -c radio)" -eq 1 ] &&
201 [ "$(dlogctl -g | grep DISABLED | grep -c main)" -eq 1 ] && ok || fail
202 LOG_DETAILS="testing if filters were applied"
203 dlogutil -c -b radio -b system -b main
204 dlogsend -b system -t TEST test
205 dlogsend -b main -t TEST test
206 dlogsend -b radio -t TEST test
207 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
209 LOG_DETAILS="dlogctl --enable (multiple args)"
210 dlogctl --enable -b radio -b system
211 [ "$(dlogctl -g | grep -c DISABLED )" -eq 1 ] &&
212 [ "$(dlogctl -g | grep DISABLED | grep -c main)" -eq 1 ] &&
213 [ "$(dlogctl -g | grep ENABLED | grep -c system)" -eq 1 ] &&
214 [ "$(dlogctl -g | grep ENABLED | grep -c radio)" -eq 1 ] && ok || fail
215 LOG_DETAILS="testing if filters were applied"
216 dlogutil -c -b radio -b system -b main
217 dlogsend -b system -t TEST test
218 dlogsend -b main -t TEST test
219 dlogsend -b radio -t TEST test
220 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
222 LOG_DETAILS="dlogctl --enable (1 arg)"
223 dlogctl --enable -b main
224 [ "$(dlogctl -g | grep -c DISABLED)" -eq 0 ] && ok || fail
225 LOG_DETAILS="testing if filters were applied"
226 dlogutil -c -b radio -b system -b main
227 dlogsend -b system -t TEST test
228 dlogsend -b main -t TEST test
229 dlogsend -b radio -t TEST test
230 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
232 LOG_DETAILS="dlogctl -s allow"
233 dlogutil -c -b radio -b system -b main
236 dlogctl -t TEST_TAG -s allow
237 dlogsend -b main -t TEST_TAG test
238 dlogsend -b main -t TEST test
239 [ "$(dlogutil -d -b main | wc -l)" -eq 1 ] && ok || fail
241 LOG_DETAILS="dlogctl -s [N]"
242 dlogutil -c -b radio -b system -b main
245 dlogctl -t TEST_TAG -s 3
246 dlogsend -b main -c 10 -t TEST_TAG test
247 dlogsend -b main -c 10 -t TEST test
248 [ "$(dlogutil -d -b main | wc -l)" -eq 4 ] && ok || fail
250 LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
251 dlogctl -s invalid > /dev/null && fail || ok
253 LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
254 dlogctl -p X -g > /dev/null && fail || ok
256 LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
257 dlogctl -b invalid --disable > /dev/null && fail || ok
259 LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
260 dlogctl -s allow -g -t xyz -p E > /dev/null && fail || ok
262 LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
263 dlogctl -s allow -c > /dev/null && fail || ok
265 LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
266 dlogctl -c -g -t xyz -p E > /dev/null && fail || ok
269 LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
270 dlogctl -s -10 > /dev/null && fail || ok
272 LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
273 dlogctl -s 999999 > /dev/null && fail || ok
276 LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
277 dlogctl -s 100 && ok || fail
279 LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
280 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 1 ] && ok || fail
283 LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
284 [ "$(dlogctl -g | grep '*:*' | grep -c 100)" -eq 1 ] && ok || fail
287 LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
288 dlogctl -c -t '*' -p '*' && ok || fail
290 LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
291 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 0 ] && ok || fail
294 LOG_DETAILS="testing if the whole dynamic config directory is respected"
296 cp $RUNTIME_FILTERS_DIR/05-logctl.conf $RUNTIME_FILTERS_DIR/10-other.conf
298 dlogutil -c -b radio -b system -b main
299 dlogsend -b system -t TEST test
300 dlogsend -b main -t TEST test
301 dlogsend -b radio -t TEST test
302 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
303 rm $RUNTIME_FILTERS_DIR/10-other.conf
306 SEED=$(head -c 6 /dev/urandom | base64)
308 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes
309 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
310 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
311 LOG_DETAILS="testing if critical logging works"
312 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
314 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
315 dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
318 kill -s PIPE $DLOGSEND_PID
320 LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
321 [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
324 # put 100 log entries in the "main" buffer
330 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
331 dlogutil -d > /dev/null && ok || fail
333 LOG_DETAILS="testing if -t argument is parsed properly"
334 dlogutil -t dummy > /dev/null && fail || ok
336 LOG_DETAILS="testing if -u argument is parsed properly"
337 dlogutil -du dummy > /dev/null && fail || ok
339 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
340 [ "$(dlogutil -b main -t 20 | wc -l)" -eq 20 ] && ok || fail # less logs than currently in buffer
342 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
343 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
345 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
346 [ "$(dlogutil -b main -d | wc -l)" -eq 100 ] && ok || fail # exactly
348 LOG_DETAILS="testing if reading from dummy buffer returns an error as expected"
349 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
351 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
352 [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
354 LOG_DETAILS="testing if dlogutil -c empties all buffers"
355 dlogutil -cb main && ok || fail
357 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
359 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
361 LOG_DETAILS="testing if filters work (1/3)"
362 [ "$(dlogutil -b main -d *:E | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
364 LOG_DETAILS="testing if filters work (2/3)"
365 [ "$(dlogutil -b main -d *:W | wc -l)" -eq 5 ] && ok || fail
367 LOG_DETAILS="testing if filters work (3/3)"
368 [ "$(dlogutil -b main -d *:I | wc -l)" -eq 10 ] && ok || fail
370 LOG_DETAILS="testing if exact filters work (1/3)"
371 [ "$(dlogutil -b main -d *:=E | wc -l)" -eq 5 ] && ok || fail
373 LOG_DETAILS="testing if exact filters work (2/3)"
374 [ "$(dlogutil -b main -d *:=W | wc -l)" -eq 0 ] && ok || fail
376 LOG_DETAILS="testing if exact filters work (3/3)"
377 [ "$(dlogutil -b main -d *:=I | wc -l)" -eq 5 ] && ok || fail
379 LOG_DETAILS="testing if adding \"silent\" filter works"
380 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
382 LOG_DETAILS="testing if reading buffer size returns proper exit code"
383 dlogutil -gb main > /dev/null && ok || fail
385 LOG_DETAILS="testing if writing all entries to single file works (-f)"
386 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
388 if [ "$quick" -ne 1 ]; then
389 LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
390 dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
393 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
395 [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
397 LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
398 dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
401 dlogsend -b main "Hi!"
403 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
405 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
407 LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
408 dlogutil -mb main -f $TESTDIR/dlog_monitor1_file &
411 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
413 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
415 LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
416 dlogutil -mb main -f $TESTDIR/dlog_monitor2_file &
419 dlogsend -b main "Hi!"
421 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
423 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
426 test_libdlog 100000 &
427 LIBDLOGUTIL_CORRECT_PID=$!
428 wait $LIBDLOGUTIL_CORRECT_PID
430 # NB: rotation only really makes sense in the continuous mode
431 # from a user PoV, but the logic doesn't care about dumping
432 # so we do it to simplify things
433 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
435 LOG_DETAILS="testing if single file is properly created"
436 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
438 LOG_DETAILS="testing if rotating file is properly created (1/4)"
439 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
441 LOG_DETAILS="testing if rotating file is properly created (2/4)"
442 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
444 LOG_DETAILS="testing if rotating file is properly created (3/4)"
445 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
447 LOG_DETAILS="testing if rotating file is properly created (4/4)"
448 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
450 LOG_DETAILS="testing the size of log files"
451 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
453 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
454 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
455 if [ "$quick" -ne 1 ]; then
456 LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
457 test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
459 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
460 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
461 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
462 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
463 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
464 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
465 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
466 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
467 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
468 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
469 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
470 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
471 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
472 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
473 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
474 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
475 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
476 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
477 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
478 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
479 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
480 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
481 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
482 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
483 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
484 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
485 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
486 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
487 LOG_DETAILS="testing if libdlogutil aliasing works"
488 if [ "$type" = "pipe" ]; then
489 test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
491 DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
493 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
494 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
496 cmd_prefix="dlogutil -t 1 -u 0 -v"
499 regex_prio="[VDIWEFS]{1}"
500 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
501 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
502 regex_timezone="[\+-]{1}[0-9]{4}"
504 for PARAM in always auto none never; do
505 for PRIO in info error; do
506 for OUTPUT in pipe tty; do
507 if [ "$PARAM" = "always" ]; then
509 elif [ "$PARAM" = "never" ]; then
511 elif [ "$OUTPUT" = "tty" ]; then
517 if [ "$PRIO" = "info" ]; then
518 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
519 elif [ "$COLOR_EXPECTED" -eq 0 ]; then
520 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
522 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m \([[:print:]]*\)@~\[0m$/1/g"
525 # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
526 COMMAND="$cmd_prefix $format"
528 if [ "$PARAM" = "none" ]; then
529 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
531 COMMAND="$COMMAND --color=$PARAM"
532 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
535 if [ "$PRIO" = "info" ]; then
536 COMMAND="$COMMAND '*:=I'"
538 COMMAND="$COMMAND '*:=E'"
541 if [ "$OUTPUT" = "tty" ]; then
542 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
543 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
546 # $(eval) is needed since `$COMMAND` may contain `"`
547 line=$(eval $COMMAND | tr '\033\n' '~@')
548 [[ $(echo "$line" | sed -re "$REGEX") == "1" ]] && ok || fail
552 # TODO: It would be cool to also test warning messages (which have a different color),
553 # but this would require deeper changes to this script, which is already janky enough.
555 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
556 LOG_DETAILS="testing if \"$format\" print format works"
557 line=$($cmd_prefix $format)
558 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
561 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
562 LOG_DETAILS="testing if \"$format\" print format works"
563 line=$($cmd_prefix $format)
564 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
567 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
568 LOG_DETAILS="testing if \"$format\" print format works"
569 line=$($cmd_prefix $format)
570 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
573 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
574 LOG_DETAILS="testing if \"$format\" print format works"
575 line=$($cmd_prefix $format)
576 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
579 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
580 LOG_DETAILS="testing if \"$format\" print format works"
581 line=$($cmd_prefix $format)
582 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
585 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
586 LOG_DETAILS="testing if \"$format\" print format works"
587 line=$($cmd_prefix $format)
588 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
590 format="recv_realtime"
591 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
592 LOG_DETAILS="testing if \"$format\" print format works"
593 line=$($cmd_prefix $format)
594 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
597 REGEX="s/^$regex_time\s{1}\[[0-9[:space:]]{3,}.[0-9[:space:]]{3,}\]\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
598 LOG_DETAILS="testing if \"$format\" print format works"
599 line=$($cmd_prefix $format)
600 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
603 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
604 LOG_DETAILS="testing if \"$format\" print format works"
605 line=$($cmd_prefix $format)
606 [[ $(echo "$line" | tr '\n' '@' | sed -re "$REGEX") == "1" ]] && ok || fail
609 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
610 LOG_DETAILS="testing if \"$format\" print format works"
611 line=$($cmd_prefix $format)
612 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
615 if [ "$type" = "pipe" ]; then
616 REGEX="s/^\{\"priority\":\"(verbose|debug|info|warning|error|fatal|silent)\",\"pid\":[1-9][0-9]*,\"tid\":[1-9][0-9]*,\"recv_real\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$regex_timezone\",\"recv_mono\":[1-9][0-9]+\.[0-9]{9},\"sent_real\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$regex_timezone\",\"sent_mono\":[1-9][0-9]+\.[0-9]{9},\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
618 REGEX="s/^\{\"priority\":\"(verbose|debug|info|warning|error|fatal|silent)\",\"pid\":[1-9][0-9]*,\"tid\":[1-9][0-9]*,\"sent_real\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$regex_timezone\",\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
620 LOG_DETAILS="testing if \"$format\" print format works"
621 line=$($cmd_prefix $format)
622 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
625 LOG_DETAILS="testing if \"$format\" print format works"
627 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
628 line=$($cmd_prefix $format)
629 [ "$line" = "rawformatTEST" ] && ok || fail
631 if [ "$quick" -ne 1 ]; then
632 LOG_DETAILS="testing if dlogsend -d works"
634 dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
638 # In 10 seconds, 3 cycles 7 logs each should have happened.
639 [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
642 if [ "$type" = "pipe" ]; then
643 for format in raw brief long; do
644 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
645 LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
646 log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
649 dlogsend -b main $log
650 line=$($cmd_prefix $format | tr '\n' '@')
657 REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
660 REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
664 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
669 LOG_DETAILS="testing if KMSG works in the default format"
670 # We check if the command returns at least 100 logs. This seems to be a safe value.
671 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
672 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
674 if [ "$quick" -ne 1 ]; then
675 LOG_DETAILS="testing if KMSG works in the raw format"
676 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
677 sleep 1 # To make sure dlog has already parsed the log
678 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
680 LOG_DETAILS="testing if pid filtering works"
681 dlogsend -b main -t DLOG_TESTSUITE pidTEST &
683 line=$(dlogutil -v raw -d --pid $!)
684 [ "$line" = "pidTEST" ] && ok || fail
686 LOG_DETAILS="testing if tid filtering works"
687 dlogsend -b main -t DLOG_TESTSUITE tidTEST &
689 line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
690 [ "$line" = "tidTEST" ] && ok || fail
692 mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
693 LOG_DETAILS="testing if secure logging works (1/2)"
694 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
695 echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
697 dlogsend -zb main -t DLOG_TESTSUITE secure &
699 dlogsend -b main -t DLOG_TESTSUITE insecure &
701 line=$(dlogutil -v raw -d | head -n1)
702 [ "$line" = "secure" ] && ok || fail
703 LOG_DETAILS="testing if secure logging works (2/2)"
704 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
705 echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
707 dlogsend -zb main -t DLOG_TESTSUITE secure &
709 dlogsend -b main -t DLOG_TESTSUITE insecure &
711 line=$(dlogutil -v raw -d | head -n1)
712 [ "$line" = "insecure" ] && ok || fail
713 mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
716 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
717 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
718 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
720 LOG_DETAILS="testing if tag filtering works (1/8)"
721 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
722 LOG_DETAILS="testing if tag filtering works (2/8)"
723 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
724 LOG_DETAILS="testing if tag filtering works (3/8)"
725 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
726 LOG_DETAILS="testing if tag filtering works (4/8)"
727 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
728 LOG_DETAILS="testing if tag filtering works (5/8)"
729 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
730 LOG_DETAILS="testing if tag filtering works (6/8)"
731 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
732 LOG_DETAILS="testing if tag filtering works (7/8)"
733 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
734 LOG_DETAILS="testing if tag filtering works (8/8)"
735 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
737 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
738 LOG_DETAILS="testing if limiter and runtime filtering works"
741 [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
743 LOG_DETAILS="testing proper SMACK label for dynamic config file location"
744 ORIG_FILTERS_DIR=$(cat "$ORIGINAL_CONFIG_PATH" | grep dynamic_config_path | awk -F "=" '{print $2}')
745 [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
747 if [ "$quick" -ne 1 ]; then
748 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
749 # PID-based limits and see what happens.
750 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
752 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
753 dlogctl --pid $DLOGSEND_PID -s deny
754 kill -s CONT $DLOGSEND_PID
756 LOG_DETAILS="testing if PID limiting works (1/14)"
757 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
758 LOG_DETAILS="testing if PID limiting works (2/14)"
759 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
760 LOG_DETAILS="testing if PID limiting works (3/14)"
761 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
762 LOG_DETAILS="testing if PID limiting works (4/14)"
763 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
765 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
767 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
768 dlogctl --pid $DLOGSEND_PID -s 25
769 kill -s CONT $DLOGSEND_PID
771 LOG_DETAILS="testing if PID limiting works (5/14)"
772 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
773 LOG_DETAILS="testing if PID limiting works (6/14)"
774 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
775 LOG_DETAILS="testing if PID limiting works (7/14)"
776 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
777 LOG_DETAILS="testing if PID limiting works (8/14)"
778 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
780 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
782 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
783 dlogctl --pid $DLOGSEND_PID -s allow
784 kill -s CONT $DLOGSEND_PID
786 LOG_DETAILS="testing if PID limiting works (9/14)"
787 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
788 LOG_DETAILS="testing if PID limiting works (10/14)"
789 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 0 ] && ok || fail
790 LOG_DETAILS="testing if PID limiting works (11/14)"
791 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
792 LOG_DETAILS="testing if PID limiting works (12/14)"
793 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
795 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
796 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
798 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
799 dlogctl --pid $DLOGSEND_PID -s 2
800 kill -s CONT $DLOGSEND_PID
802 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
803 LOG_DETAILS="testing if PID limiting works (13/14)"
804 # In each 5 second period, 5 logs are sent, so there are 3 periods.
805 # 2 logs from each period are allowed.
806 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
807 LOG_DETAILS="testing if PID limiting works (14/14)"
808 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 3 ] && ok || fail
811 # The following QoS tests make perfect sense for the pipe backend, since we kill the already
812 # existing dlog_logger and then start the new one, configured with QoS. However, on the logger
813 # backend this is impossible, as dlog_test uses the global service started by systemd then.
814 # This means that we have to just launch the second instance and hope nothing breaks.
815 # This can be improved by not using the global service in the logger backend, and by having it
816 # completely disabled in the config file -- this way, the tests would be completely uniform.
817 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
818 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
819 if [ "$type" = "pipe" ]; then
820 kill $LOGGER > /dev/null
827 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
828 LOG_DETAILS="testing if QoS works (1/4)"
829 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
830 kill $LOGGER > /dev/null
832 if [ "$type" = "logger" ]; then
839 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
840 LOG_DETAILS="testing if QoS works (2/4)"
841 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 180 ] && ok || fail
842 kill $LOGGER > /dev/null
844 if [ "$type" = "logger" ]; then
851 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
853 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
855 LOG_DETAILS="testing if QoS works (3/4)"
856 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 180 ] && ok || fail
857 LOG_DETAILS="testing if QoS works (4/4)"
858 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
860 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
861 kill $LOGGER > /dev/null
863 if [ "$type" = "pipe" ]; then
864 dlog_logger -b 99 -t 0 &
873 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
874 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
876 if [ "$quick" -ne 1 ]; then
877 if [ "$type" = "pipe" ]; then
878 dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
880 LOG_DETAILS="testing if logger daemon persistent logging works"
881 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/main)" -eq 100 ] && ok || fail
883 dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
885 RESULT=$(du -c "$TESTDIR"/main* | tail -n 1 | cut -f 1)
886 LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
887 [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
888 LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
889 [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
894 LOG_DETAILS="testing if the library works with multithreaded app"
895 dlogutil -f $TESTDIR/dlog_mt_test &
897 test_libdlog && ok || fail
906 # Spawn logging scripts and wait for them to finish.
907 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
908 # wait for all the jobs of the subshell, which means all the logging scripts.
909 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
910 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
911 # group, sent about at the same time.
913 for i in $(seq 1 $SPAWN_CNT); do
914 delay=$(shuf -i 1-5 -n 1)
915 ( sleep $delay && dlogsend -b main -t DLOG_TESTSUITE $delay ) &
920 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
922 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
924 if [ "$type" = "pipe" ]; then
925 for P in recv sent; do
926 for Q in mono real; do
927 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
928 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
931 for P in recv sent; do
932 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
933 dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
934 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
935 dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
939 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
941 for format in $sort_formats; do
942 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
944 # collect data and analyze timestamps
945 dlogutil -b main -d -v $format | (
949 while read -r line; do
950 # filter out empty lines in "long" format
951 [ -z "$line" ] && continue
952 # filter out non-timestamp lines in "long" format
953 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
957 ts=$(extract_timestamp "$format" "$line")
959 if [ "$ts" -ge "$prev_ts" ]; then
962 printf "%s\n%s\n\n" "$prev_line" "$line" || break
966 ) | grep -q ^ && fail || ok
971 # show results, clean up and return an exit code
973 echo "$OKS / $TOTAL tests passed"
974 echo "$FAILS / $TOTAL tests failed"
976 if [ "$quick" -eq 1 ]; then
977 echo "WARNING: quick mode!"
978 echo "About 20% slowest running tests are disabled. Some functionality might be untested."
979 echo "Rerun without '--quick' to run all tests."
983 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
984 # This is because it is used to pass the exit code. If another command
985 # is executed after it, its exit code will be propagated instead.