3 # We use the something && ok || fail pattern quite a lot; it is not correct in general,
4 # which makes shellcheck complain about it, but in our case it's ok to use it since
5 # the ok function will never fail.
6 # shellcheck disable=SC2015
8 # stuff for tracking test case counts
14 #relevant pids default vals
20 TEST_DYNAMIC_FILTERS="@DYNAMIC_FILTERS@"
21 TESTDIR=/var/lib/dlog-tests
27 time=$(echo "$2" | awk -F '[ +]' '{print $2}')
28 ts=$(date +%s%N -d "$time")
31 time=$(echo "$2" | awk -F '[ +.]' '{print $3}')
32 ms=$(echo "$2" | awk -F '[ +.]' '{print $4}')
33 sec=$(date +%s%N -d "$time")
34 ns=$((10#$ms * 1000000))
38 time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
39 ts=$(date +%s%N -d "$time")
42 time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
43 ts=$(date +%s%N -d "$time")
46 time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
47 ts=$(date +%s%N -d "$time")
50 ts=$(echo "$2" | awk -F '[ +]' '{print $1}' | sed -e 's/\.//g')
57 [ "$UTIL_PID" -ne -1 ] && kill "$UTIL_PID" > /dev/null 2>&1
58 [ "$MT_TEST" -ne -1 ] && kill "$MT_TEST" > /dev/null 2>&1
59 [ "$LOGGER" -ne -1 ] && kill "$LOGGER" > /dev/null 2>&1
60 # Shellcheck doesn't like this because the rm call is bad if $TESTDIR is empty.
61 # However, such cases would be caught by the conditional.
62 # shellcheck disable=SC2115
63 [ -d "$TESTDIR" ] && rm -rf "$TESTDIR"/*
64 [ -d "$RUNTIME_FILTERS_DIR" ] && rm -rf "$RUNTIME_FILTERS_DIR"
71 if [ "$LOGGER" -ne -1 ] && [ -z "$(ps -o pid= -p "$LOGGER")" ]; then
78 check_daemon && daemon_status="[logger daemon not running]"
82 printf "[%02d] FAILED: %s %s\n" "$TOTAL" "$LOG_DETAILS" "$daemon_status"
87 check_daemon && daemon_status="[logger daemon not running]"
91 printf "[%02d] PASSED: %s %s\n" "$TOTAL" "$LOG_DETAILS" "$daemon_status"
95 USAGE_MESSAGE="usage: $0 [--verbose] [--quick] pipe|logger"
97 OPTS=$(getopt --shell sh --options "" --long verbose,quick --quiet -- "$@")
98 if [ "$?" -eq 1 ]; then
119 echo "This should never happen :)"
125 if [ "$1" = "pipe" ]; then
127 elif [ "$1" = "logger" ]; then
129 # We still accept the legacy {pipe,logger}_quick syntax because there's no reason not to.
130 elif [ "$1" = "pipe_quick" ]; then
133 elif [ "$1" = "logger_quick" ]; then
137 echo "$USAGE_MESSAGE"
141 # supress stderr messages from subcommands
142 if [ "$verbose" -eq 0 ]; then
147 capsh --print | grep Current | grep cap_syslog > /dev/null || { echo "*** ERROR: cap_syslog missing"; NEEDS_TO_QUIT=1; }
148 mount | grep ' / ' | grep rw > /dev/null || { echo "*** ERROR: root not mounted read-write"; NEEDS_TO_QUIT=1; }
149 [ "$NEEDS_TO_QUIT" -eq 0 ] || exit 1
151 if [ "$type" = "pipe" ] || [ -f /lib/modules/"$(uname -r)"/extra/logger.ko ]; then
157 if [ -z "$DLOG_CONFIG_PATH" ]; then
158 ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
160 ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
163 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
164 PATH=$PATH:@libexecdir@/libdlog/
166 #create dir for runtime filters
167 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
168 mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
170 PREEARLYQUIT_CONFIG_PATH="$DLOG_CONFIG_PATH"
171 DLOG_CONFIG_PATH="$TESTDIR/earlyquit.conf"
172 if [ "$quick" -ne 1 ]; then
173 for PERSISTENT in 1 0; do
175 for SYSLOG in 1 0; do
177 # TODO: Does syslog even work? Disabling for now.
178 [ "$SYSLOG" -eq 1 ] && continue
180 if [ "$QOS" -eq 1 ]; then
181 SOURCE="@datadir@/dlog-$type.conf.qos"
183 SOURCE="@datadir@/dlog-$type.conf.test"
186 if [ "$PERSISTENT" -eq 1 ]; then
187 cp "$SOURCE" "$DLOG_CONFIG_PATH"
189 sed 's/^dlog_logger_conf.*$/#\0/' <"$SOURCE" >"$DLOG_CONFIG_PATH"
192 echo "handle_kmsg=$KMSG" >> "$DLOG_CONFIG_PATH"
193 echo "syslog_force=$SYSLOG" >> "$DLOG_CONFIG_PATH"
199 kill $LOGGER > /dev/null
203 if [ "$PERSISTENT" -eq 0 ] && [ "$KMSG" -eq 0 ] && [ "$SYSLOG" -eq 0 ] && [ "$QOS" -eq 0 ] && [ "$type" = "logger" ]; then
209 LOG_DETAILS="testing if daemon stops when not needed (persistent=$PERSISTENT, kmsg=$KMSG, syslog=$SYSLOG, qos=$QOS)"
210 [ "$STOPPED" -eq "$EXPECTED" ] && ok || fail
211 done; done; done; done
213 DLOG_CONFIG_PATH="$PREEARLYQUIT_CONFIG_PATH"
220 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
223 dlogutil -c -b radio -b system -b main
224 LOG_DETAILS="dlogctl --disable (no args)"
226 [ "$(dlogctl -g | grep -c '^\* [a-z]*: ENABLED')" -eq 0 ] && ok || fail
227 LOG_DETAILS="testing if filters were applied"
228 dlogsend -b system -t TEST test
229 dlogsend -b main -t TEST test
230 dlogsend -b radio -t TEST test
231 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
233 LOG_DETAILS="dlogctl --enable (no args)"
235 [ "$(dlogctl -g | grep -c '^\* [a-z]*: DISABLED')" -eq 0 ] && ok || fail
236 LOG_DETAILS="testing if filters were applied"
237 dlogutil -c -b radio -b system -b main
238 dlogsend -b system -t TEST test
239 dlogsend -b main -t TEST test
240 dlogsend -b radio -t TEST test
241 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
243 LOG_DETAILS="dlogctl --disable (1 arg)"
244 dlogctl --disable -b system
245 [ "$(dlogctl -g | grep '^\* [a-z]*: DISABLED' | grep -c system)" -eq 1 ] &&
246 [ "$(dlogctl -g | grep -c '^\* [a-z]*: DISABLED' )" -eq 1 ] && ok || fail
247 LOG_DETAILS="testing if filters were applied"
248 dlogutil -c -b radio -b system -b main
249 dlogsend -b system -t TEST test
250 dlogsend -b main -t TEST test
251 dlogsend -b radio -t TEST test
252 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
254 LOG_DETAILS="dlogctl --disable (multiple args)"
255 dlogctl --disable -b main -b radio
256 [ "$(dlogctl -g | grep '^\* [a-z]*: DISABLED' | grep -cv system)" -eq 2 ] &&
257 [ "$(dlogctl -g | grep '^\* [a-z]*: DISABLED' | grep -c radio)" -eq 1 ] &&
258 [ "$(dlogctl -g | grep '^\* [a-z]*: DISABLED' | grep -c main)" -eq 1 ] && ok || fail
259 LOG_DETAILS="testing if filters were applied"
260 dlogutil -c -b radio -b system -b main
261 dlogsend -b system -t TEST test
262 dlogsend -b main -t TEST test
263 dlogsend -b radio -t TEST test
264 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
266 LOG_DETAILS="dlogctl --enable (multiple args)"
267 dlogctl --enable -b radio -b system
268 [ "$(dlogctl -g | grep -c '^\* [a-z]*: DISABLED' )" -eq 1 ] &&
269 [ "$(dlogctl -g | grep '^\* [a-z]*: DISABLED' | grep -c main)" -eq 1 ] &&
270 [ "$(dlogctl -g | grep '^\* [a-z]*: ENABLED' | grep -c system)" -eq 1 ] &&
271 [ "$(dlogctl -g | grep '^\* [a-z]*: ENABLED' | grep -c radio)" -eq 1 ] && ok || fail
272 LOG_DETAILS="testing if filters were applied"
273 dlogutil -c -b radio -b system -b main
274 dlogsend -b system -t TEST test
275 dlogsend -b main -t TEST test
276 dlogsend -b radio -t TEST test
277 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
279 LOG_DETAILS="dlogctl --enable (1 arg)"
280 dlogctl --enable -b main
281 [ "$(dlogctl -g | grep -c '^\* [a-z]*: DISABLED')" -eq 0 ] && ok || fail
282 LOG_DETAILS="testing if filters were applied"
283 dlogutil -c -b radio -b system -b main
284 dlogsend -b system -t TEST test
285 dlogsend -b main -t TEST test
286 dlogsend -b radio -t TEST test
287 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
289 LOG_DETAILS="dlogctl -s allow"
290 dlogutil -c -b radio -b system -b main
293 dlogctl -t TEST_TAG -s allow
294 dlogsend -b main -t TEST_TAG test
295 dlogsend -b main -t TEST test
296 [ "$(dlogutil -d -b main | wc -l)" -eq 1 ] && ok || fail
298 LOG_DETAILS="dlogctl -s [N]"
299 dlogutil -c -b radio -b system -b main
302 dlogctl -t TEST_TAG -s 3
303 dlogsend -b main -c 10 -t TEST_TAG test
304 dlogsend -b main -c 10 -t TEST test
305 [ "$(dlogutil -d -b main | wc -l)" -eq 4 ] && ok || fail
307 LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
308 dlogctl -s invalid > /dev/null && fail || ok
310 LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
311 dlogctl -p X -g > /dev/null && fail || ok
313 LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
314 dlogctl -b invalid --disable > /dev/null && fail || ok
316 LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
317 dlogctl -s allow -g -t xyz -p E > /dev/null && fail || ok
319 LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
320 dlogctl -s allow -c > /dev/null && fail || ok
322 LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
323 dlogctl -c -g -t xyz -p E > /dev/null && fail || ok
326 LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
327 dlogctl -s -10 > /dev/null && fail || ok
329 LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
330 dlogctl -s 999999 > /dev/null && fail || ok
333 LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
334 dlogctl -s 100 && ok || fail
336 LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
337 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 1 ] && ok || fail
340 LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
341 [ "$(dlogctl -g | grep -F '*:*' | grep -c 100)" -eq 1 ] && ok || fail
344 LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
345 dlogctl -c -t '*' -p '*' && ok || fail
347 LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
348 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 0 ] && ok || fail
351 LOG_DETAILS="testing if the whole dynamic config directory is respected"
353 cp $RUNTIME_FILTERS_DIR/05-logctl.conf $RUNTIME_FILTERS_DIR/10-other.conf
355 dlogutil -c -b radio -b system -b main
356 dlogsend -b system -t TEST test
357 dlogsend -b main -t TEST test
358 dlogsend -b radio -t TEST test
359 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
360 rm $RUNTIME_FILTERS_DIR/10-other.conf
363 SEED=$(head -c 6 /dev/urandom | base64)
364 dlogsend -b main -k "$SEED"
365 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
366 while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
367 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
368 LOG_DETAILS="testing if critical logging works"
369 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
371 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
372 dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
375 kill -s PIPE $DLOGSEND_PID
377 LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
378 [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
381 # put 100 log entries in the "main" buffer
387 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
388 dlogutil -d > /dev/null && ok || fail
390 LOG_DETAILS="testing if -t argument is parsed properly"
391 dlogutil -t dummy > /dev/null && fail || ok
393 LOG_DETAILS="testing if -u argument is parsed properly"
394 dlogutil -du dummy > /dev/null && fail || ok
396 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
397 [ "$(dlogutil -b main -t 20 | wc -l)" -eq 20 ] && ok || fail # less logs than currently in buffer
399 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
400 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
402 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
403 [ "$(dlogutil -b main -d | wc -l)" -eq 100 ] && ok || fail # exactly
405 LOG_DETAILS="testing if reading from dummy buffer returns an error as expected"
406 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
408 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
409 [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
411 LOG_DETAILS="testing if dlogutil -c empties all buffers"
412 dlogutil -cb main && ok || fail
414 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
416 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
418 LOG_DETAILS="testing if filters work (1/3)"
419 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
421 LOG_DETAILS="testing if filters work (2/3)"
422 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
424 LOG_DETAILS="testing if filters work (3/3)"
425 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
427 LOG_DETAILS="testing if exact filters work (1/3)"
428 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
430 LOG_DETAILS="testing if exact filters work (2/3)"
431 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
433 LOG_DETAILS="testing if exact filters work (3/3)"
434 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
436 LOG_DETAILS="testing if adding \"silent\" filter works"
437 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
439 LOG_DETAILS="testing if reading buffer size returns proper exit code"
440 dlogutil -gb main > /dev/null && ok || fail
442 LOG_DETAILS="testing if writing all entries to single file works (-f)"
443 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
445 if [ "$quick" -ne 1 ]; then
446 LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
447 dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
450 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
452 [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
454 LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
455 dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
458 dlogsend -b main "Hi!"
460 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
462 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
464 LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
465 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor1_file &
468 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
470 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
472 LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
473 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor2_file &
476 dlogsend -b main "Hi!"
478 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
480 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
483 test_libdlog 100000 &
484 LIBDLOGUTIL_CORRECT_PID=$!
485 wait $LIBDLOGUTIL_CORRECT_PID
487 # NB: rotation only really makes sense in the continuous mode
488 # from a user PoV, but the logic doesn't care about dumping
489 # so we do it to simplify things
490 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
492 LOG_DETAILS="testing if single file is properly created"
493 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
495 LOG_DETAILS="testing if rotating file is properly created (1/4)"
496 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
498 LOG_DETAILS="testing if rotating file is properly created (2/4)"
499 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
501 LOG_DETAILS="testing if rotating file is properly created (3/4)"
502 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
504 LOG_DETAILS="testing if rotating file is properly created (4/4)"
505 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
507 LOG_DETAILS="testing the size of log files"
508 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
510 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
511 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
512 if [ "$quick" -ne 1 ]; then
513 LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
514 test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
516 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
517 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
518 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
519 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
520 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
521 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
522 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
523 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
524 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
525 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
526 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
527 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
528 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
529 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
530 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
531 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
532 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
533 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
534 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
535 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
536 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
537 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
538 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
539 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
540 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
541 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
542 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
543 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
544 LOG_DETAILS="testing if libdlogutil aliasing works"
545 if [ "$type" = "pipe" ]; then
546 test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
548 DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
550 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
551 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
553 cmd_prefix_no_v="dlogutil -t 1 -u 0"
554 cmd_prefix="$cmd_prefix_no_v -v"
557 regex_prio="[VDIWEFS]{1}"
558 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
559 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
560 regex_timezone="[\+-]{1}[0-9]{4}"
562 for PARAM in always auto none never; do
563 for PRIO in info error; do
564 for OUTPUT in pipe tty; do
565 if [ "$PARAM" = "always" ]; then
567 elif [ "$PARAM" = "never" ]; then
569 elif [ "$OUTPUT" = "tty" ]; then
575 if [ "$PRIO" = "info" ]; then
576 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
577 elif [ "$COLOR_EXPECTED" -eq 0 ]; then
578 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
580 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m \([[:print:]]*\)@~\[0m$/1/g"
583 # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
584 COMMAND="$cmd_prefix $format"
586 if [ "$PARAM" = "none" ]; then
587 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
589 COMMAND="$COMMAND --color=$PARAM"
590 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
593 if [ "$PRIO" = "info" ]; then
594 COMMAND="$COMMAND '*:=I'"
596 COMMAND="$COMMAND '*:=E'"
599 if [ "$OUTPUT" = "tty" ]; then
600 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
601 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
604 # $(eval) is needed since `$COMMAND` may contain `"`
605 line="$(eval "$COMMAND" | tr '\033\n' '~@')"
606 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
610 # TODO: It would be cool to also test warning messages (which have a different color),
611 # but this would require deeper changes to this script, which is already janky enough.
613 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
614 LOG_DETAILS="testing if \"$format\" print format works"
615 line=$($cmd_prefix $format)
616 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
619 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[: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 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
626 LOG_DETAILS="testing if \"$format\" print format works"
627 line=$($cmd_prefix $format)
628 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
631 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
632 LOG_DETAILS="testing if \"$format\" print format works"
633 line=$($cmd_prefix $format)
634 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
637 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
638 LOG_DETAILS="testing if \"$format\" print format works"
639 line=$($cmd_prefix $format)
640 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
643 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
644 LOG_DETAILS="testing if \"$format\" print format works"
645 line=$($cmd_prefix $format)
646 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
648 format="recv_realtime"
649 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
650 LOG_DETAILS="testing if \"$format\" print format works"
651 line=$($cmd_prefix $format)
652 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
655 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"
656 LOG_DETAILS="testing if \"$format\" print format works"
657 line=$($cmd_prefix $format)
658 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
661 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
662 LOG_DETAILS="testing if \"$format\" print format works"
663 line=$($cmd_prefix $format)
664 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
667 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
668 LOG_DETAILS="testing if \"$format\" print format works"
669 line=$($cmd_prefix $format)
670 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
673 if [ "$type" = "pipe" ]; then
674 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"
676 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"
678 LOG_DETAILS="testing if \"$format\" print format works"
679 line=$($cmd_prefix $format)
680 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
682 SOURCE="$DLOG_CONFIG_PATH"
683 DLOG_CONFIG_PATH="$TESTDIR/defaultprintformat.conf"
684 cp "$SOURCE" "$DLOG_CONFIG_PATH"
686 # TODO: It would be good to also test the daemon side of things.
687 LOG_DETAILS="testing default log format selection (none -> brief)"
688 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
689 line=$($cmd_prefix_no_v)
690 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
692 echo "util_default_format=rwtime" >> $DLOG_CONFIG_PATH
693 LOG_DETAILS="testing default log format selection (rwtime)"
694 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"
695 line=$($cmd_prefix_no_v)
696 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
698 DLOG_CONFIG_PATH="$SOURCE"
701 LOG_DETAILS="testing if \"$format\" print format works"
703 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
704 line=$($cmd_prefix $format)
705 [ "$line" = "rawformatTEST" ] && ok || fail
707 if [ "$quick" -ne 1 ]; then
708 LOG_DETAILS="testing if dlogsend -d works"
710 dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
714 # In 10 seconds, 3 cycles 7 logs each should have happened.
715 [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
718 if [ "$type" = "pipe" ]; then
719 for format in raw brief long; do
720 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
721 LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
722 log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
725 dlogsend -b main "$log"
726 line=$($cmd_prefix $format | tr '\n' '@')
733 REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
736 REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
740 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
745 LOG_DETAILS="testing if KMSG works in the default format"
746 # We check if the command returns at least 100 logs. This seems to be a safe value.
747 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
748 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
750 if [ "$quick" -ne 1 ]; then
751 LOG_DETAILS="testing if KMSG works in the raw format"
752 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
753 sleep 1 # To make sure dlog has already parsed the log
754 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
756 LOG_DETAILS="testing if pid filtering works"
757 dlogsend -b main -t DLOG_TESTSUITE pidTEST &
759 line=$(dlogutil -v raw -d --pid $!)
760 [ "$line" = "pidTEST" ] && ok || fail
762 LOG_DETAILS="testing if tid filtering works"
763 dlogsend -b main -t DLOG_TESTSUITE tidTEST &
765 line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
766 [ "$line" = "tidTEST" ] && ok || fail
768 mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
769 LOG_DETAILS="testing if secure logging works (1/2)"
770 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
771 echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
773 dlogsend -zb main -t DLOG_TESTSUITE secure &
775 dlogsend -b main -t DLOG_TESTSUITE insecure &
777 line=$(dlogutil -v raw -d | head -n1)
778 [ "$line" = "secure" ] && ok || fail
779 LOG_DETAILS="testing if secure logging works (2/2)"
780 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
781 echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
783 dlogsend -zb main -t DLOG_TESTSUITE secure &
785 dlogsend -b main -t DLOG_TESTSUITE insecure &
787 line=$(dlogutil -v raw -d | head -n1)
788 [ "$line" = "insecure" ] && ok || fail
789 mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
792 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
793 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
794 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
796 LOG_DETAILS="testing if tag filtering works (1/8)"
797 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
798 LOG_DETAILS="testing if tag filtering works (2/8)"
799 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
800 LOG_DETAILS="testing if tag filtering works (3/8)"
801 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
802 LOG_DETAILS="testing if tag filtering works (4/8)"
803 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
804 LOG_DETAILS="testing if tag filtering works (5/8)"
805 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
806 LOG_DETAILS="testing if tag filtering works (6/8)"
807 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
808 LOG_DETAILS="testing if tag filtering works (7/8)"
809 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
810 LOG_DETAILS="testing if tag filtering works (8/8)"
811 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
813 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
814 LOG_DETAILS="testing if limiter and runtime filtering works"
817 [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
820 LOG_DETAILS="testing proper SMACK label for dynamic config file location"
821 ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
822 # Running ls on an arbitrary directory and trying to make sense of results is problematic.
823 # However, this is not a problem in our usecase, as we only care about the first field, the security context.
824 # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
825 # shellcheck disable=SC2012
826 [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
828 if [ "$quick" -ne 1 ]; then
829 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
830 # PID-based limits and see what happens.
831 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
833 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
834 dlogctl --pid $DLOGSEND_PID -s deny
835 kill -s CONT $DLOGSEND_PID
837 LOG_DETAILS="testing if PID limiting works (1/14)"
838 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
839 LOG_DETAILS="testing if PID limiting works (2/14)"
840 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
841 LOG_DETAILS="testing if PID limiting works (3/14)"
842 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
843 LOG_DETAILS="testing if PID limiting works (4/14)"
844 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
846 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
848 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
849 dlogctl --pid $DLOGSEND_PID -s 25
850 kill -s CONT $DLOGSEND_PID
852 LOG_DETAILS="testing if PID limiting works (5/14)"
853 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
854 LOG_DETAILS="testing if PID limiting works (6/14)"
855 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
856 LOG_DETAILS="testing if PID limiting works (7/14)"
857 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
858 LOG_DETAILS="testing if PID limiting works (8/14)"
859 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
861 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
863 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
864 dlogctl --pid $DLOGSEND_PID -s allow
865 kill -s CONT $DLOGSEND_PID
867 LOG_DETAILS="testing if PID limiting works (9/14)"
868 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
869 LOG_DETAILS="testing if PID limiting works (10/14)"
870 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 0 ] && ok || fail
871 LOG_DETAILS="testing if PID limiting works (11/14)"
872 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
873 LOG_DETAILS="testing if PID limiting works (12/14)"
874 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
876 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
877 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
879 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
880 dlogctl --pid $DLOGSEND_PID -s 2
881 kill -s CONT $DLOGSEND_PID
883 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
884 LOG_DETAILS="testing if PID limiting works (13/14)"
885 # In each 5 second period, 5 logs are sent, so there are 3 periods.
886 # 2 logs from each period are allowed.
887 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
888 LOG_DETAILS="testing if PID limiting works (14/14)"
889 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 3 ] && ok || fail
891 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
892 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
893 kill $LOGGER > /dev/null
895 if [ "$type" = "logger" ]; then
902 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
903 LOG_DETAILS="testing if QoS works (1/4)"
904 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
905 kill $LOGGER > /dev/null
907 if [ "$type" = "logger" ]; then
914 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
915 LOG_DETAILS="testing if QoS works (2/4)"
916 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
917 kill $LOGGER > /dev/null
919 if [ "$type" = "logger" ]; then
926 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
928 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
930 LOG_DETAILS="testing if QoS works (3/4)"
931 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
932 LOG_DETAILS="testing if QoS works (4/4)"
933 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
935 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
936 kill $LOGGER > /dev/null
944 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
947 LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
948 [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
950 LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
951 [ "$(dlogutil -db main -v tag)" = "I/FOO : Hi" ] && ok || fail
954 # No need to check output in this case, as nothing is written to stdout anyway
955 dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
957 LOG_DETAILS="testing if basic stderr redirection works correctly"
958 [ "$(dlogutil -db main -v tag)" = "E/BAR : Hi" ] && ok || fail
961 LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
962 [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
964 LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
965 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
968 dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
970 LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
971 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
972 LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
973 [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
976 dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
978 LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
979 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
980 LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
981 [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
984 echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
986 LOG_DETAILS="testing if redirector passes stdin correctly"
987 [ "$(dlogutil -db main -v tag)" = "I/BAZ : Testing" ] && ok || fail
990 dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
998 LOG_DETAILS="testing if multiline logs are handled correctly"
999 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1002 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
1004 LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
1005 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1008 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
1010 LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
1011 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1014 rm -f "$TESTDIR"/input
1015 rm -f "$TESTDIR"/output
1016 base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
1017 dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
1019 dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
1020 LOG_DETAILS="testing if long lines are handled correctly"
1021 # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
1022 [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
1025 if [ "$type" = "pipe" ]; then
1026 # TODO: Actually implement and enable this in the logger backend
1027 dlogctl -b main --disable-stdout
1028 LOG_DETAILS="testing if redirection can be disabled (1/3)"
1029 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1031 LOG_DETAILS="testing if redirection can be disabled (2/3)"
1032 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
1033 LOG_DETAILS="testing if redirection can be disabled (3/3)"
1034 [ "$(dlogctl --get | grep main)" = "* main: ENABLED (stdout: DISABLED)" ] && ok || fail
1036 dlogctl -b main --enable-stdout
1037 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1038 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1040 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1041 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1042 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1043 [ "$(dlogctl --get | grep main)" = "* main: ENABLED (stdout: ENABLED)" ] && ok || fail
1047 dlogctl -b radio --disable-stdout
1048 dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1050 LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1051 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1054 LOG_DETAILS="testing if redirection check works (1/5)"
1055 test_libredirect 0 0 && ok || fail
1056 LOG_DETAILS="testing if redirection check works (2/5)"
1057 dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1058 LOG_DETAILS="testing if redirection check works (3/5)"
1059 dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1060 LOG_DETAILS="testing if redirection check works (4/5)"
1061 dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1062 LOG_DETAILS="testing if redirection check works (5/5)"
1063 dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1065 LOG_DETAILS="testing if multiple redirections work (1/4)"
1066 test_libredirect_multi && ok || fail
1067 LOG_DETAILS="testing if multiple redirections work (2/4)"
1068 [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1 : Message 1" ] && ok || fail
1069 LOG_DETAILS="testing if multiple redirections work (3/4)"
1070 [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2 : Message 2" ] && ok || fail
1071 LOG_DETAILS="testing if multiple redirections work (4/4)"
1072 [ "$(dlogutil -db system -v tag)" = "I/TAG3 : Message 3" ] && ok || fail
1073 dlogutil -cb main -b system
1077 echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1078 echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1080 dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1081 LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1082 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1083 LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1084 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1086 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1087 LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1088 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1089 LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1090 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1092 echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1094 dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1095 LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1096 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1097 LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1098 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1100 dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1101 LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1102 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1103 LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1104 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1105 LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1106 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1108 echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1110 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1111 LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1112 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1113 LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1114 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1115 LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1116 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1118 rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1120 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1121 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1123 if [ "$(date +%Y)" -le 2000 ]; then
1124 # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1125 date -s "@2121212121" > /dev/null
1127 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1128 LOG_DETAILS="testing if the timestamps are correct"
1129 test_verifytime && ok || fail
1131 if [ "$quick" -ne 1 ]; then
1132 PREAUTODETECT_CONFIG_PATH=$DLOG_CONFIG_PATH
1133 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.autodetect"
1134 kill $LOGGER > /dev/null
1136 if [ "$type" = "logger" ]; then
1143 LOG_DETAILS="testing if backend autodetection works properly (1/2)"
1144 dlogsend -b main -t DLOG_AUTODETECT "xd" && ok || fail
1147 DLOG_CONFIG_PATH=$PREAUTODETECT_CONFIG_PATH
1148 LOG_DETAILS="testing if backend autodetection works properly (2/2)"
1149 [ "$(dlogutil -db main -v raw)" = "xd" ] && ok || fail
1151 kill $LOGGER > /dev/null
1158 dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
1160 LOG_DETAILS="testing if logger daemon persistent logging works (1/3)"
1161 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1162 LOG_DETAILS="testing if logger daemon persistent logging works (2/3)"
1163 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test1)" -eq 100 ] && ok || fail
1164 LOG_DETAILS="testing if logger daemon persistent logging works (3/3)"
1165 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test2)" -eq 0 ] && ok || fail
1167 dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
1169 RESULT=$(du -c "$TESTDIR"/test0* | tail -n 1 | cut -f 1)
1170 LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
1171 [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
1172 LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
1173 [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
1176 kill $LOGGER > /dev/null
1178 rm "$TESTDIR"/test0*
1179 dlog_logger -t 5 -b 100000 &
1183 dlogsend -b main -t DLOG_LOGGER_TEST2 -c 100 "hi"
1185 LOG_DETAILS="testing if logger daemon buffering works (1/5)"
1186 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1189 LOG_DETAILS="testing if logger daemon buffering works (2/5)"
1190 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1192 dlogsend -b main -t DLOG_LOGGER_TEST3 -c 100 "hi"
1194 LOG_DETAILS="testing if logger daemon buffering works (3/5)"
1195 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1198 kill $LOGGER > /dev/null
1200 dlog_logger -t 10000 -b 100 &
1203 LOG_DETAILS="testing if logger daemon buffering works (4/5)"
1204 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1206 dlogsend -b main -t DLOG_LOGGER_TEST4 -c 100 "hi"
1208 LOG_DETAILS="testing if logger daemon buffering works (5/5)"
1209 [ "$(grep -c DLOG_LOGGER_TEST4 "$TESTDIR"/test0)" -gt 90 ] && ok || fail
1212 kill $LOGGER > /dev/null
1218 LOG_DETAILS="testing if the library works with multithreaded app"
1219 dlogutil -f $TESTDIR/dlog_mt_test &
1221 test_libdlog && ok || fail
1230 # Spawn logging scripts and wait for them to finish.
1231 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1232 # wait for all the jobs of the subshell, which means all the logging scripts.
1233 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1234 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1235 # group, sent about at the same time.
1237 for i in $(seq 1 $SPAWN_CNT); do
1238 delay=$(shuf -i 1-5 -n 1)
1239 ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1244 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1246 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1248 if [ "$type" = "pipe" ]; then
1249 for P in recv sent; do
1250 for Q in mono real; do
1251 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1252 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1255 for P in recv sent; do
1256 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1257 dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1258 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1259 dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
1263 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1265 for format in $sort_formats; do
1266 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1268 # collect data and analyze timestamps
1269 dlogutil -b main -d -v "$format" | (
1273 while read -r line; do
1274 # filter out empty lines in "long" format
1275 [ -z "$line" ] && continue
1276 # filter out non-timestamp lines in "long" format
1277 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1281 ts=$(extract_timestamp "$format" "$line")
1283 if [ "$ts" -ge "$prev_ts" ]; then
1286 printf "%s\n%s\n\n" "$prev_line" "$line" || break
1290 ) | grep -q ^ && fail || ok
1295 # show results, clean up and return an exit code
1297 echo "$OKS / $TOTAL tests passed"
1298 echo "$FAILS / $TOTAL tests failed"
1300 if [ "$quick" -eq 1 ]; then
1301 echo "WARNING: quick mode!"
1302 echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1303 echo "Rerun without '--quick' to run all tests."
1304 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1305 echo "WARNING: legacy logger module!"
1306 echo "Some functionality is untested."
1307 echo "Rerun on a device with the out-of-tree logger module to run all tests."
1311 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1312 # This is because it is used to pass the exit code. If another command
1313 # is executed after it, its exit code will be propagated instead.