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="dlogutil -t 1 -u 0 -v"
556 regex_prio="[VDIWEFS]{1}"
557 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
558 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
559 regex_timezone="[\+-]{1}[0-9]{4}"
561 for PARAM in always auto none never; do
562 for PRIO in info error; do
563 for OUTPUT in pipe tty; do
564 if [ "$PARAM" = "always" ]; then
566 elif [ "$PARAM" = "never" ]; then
568 elif [ "$OUTPUT" = "tty" ]; then
574 if [ "$PRIO" = "info" ]; then
575 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
576 elif [ "$COLOR_EXPECTED" -eq 0 ]; then
577 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
579 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m \([[:print:]]*\)@~\[0m$/1/g"
582 # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
583 COMMAND="$cmd_prefix $format"
585 if [ "$PARAM" = "none" ]; then
586 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
588 COMMAND="$COMMAND --color=$PARAM"
589 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
592 if [ "$PRIO" = "info" ]; then
593 COMMAND="$COMMAND '*:=I'"
595 COMMAND="$COMMAND '*:=E'"
598 if [ "$OUTPUT" = "tty" ]; then
599 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
600 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
603 # $(eval) is needed since `$COMMAND` may contain `"`
604 line="$(eval "$COMMAND" | tr '\033\n' '~@')"
605 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
609 # TODO: It would be cool to also test warning messages (which have a different color),
610 # but this would require deeper changes to this script, which is already janky enough.
612 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
613 LOG_DETAILS="testing if \"$format\" print format works"
614 line=$($cmd_prefix $format)
615 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
618 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
619 LOG_DETAILS="testing if \"$format\" print format works"
620 line=$($cmd_prefix $format)
621 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
624 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
625 LOG_DETAILS="testing if \"$format\" print format works"
626 line=$($cmd_prefix $format)
627 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
630 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
631 LOG_DETAILS="testing if \"$format\" print format works"
632 line=$($cmd_prefix $format)
633 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
636 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
637 LOG_DETAILS="testing if \"$format\" print format works"
638 line=$($cmd_prefix $format)
639 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
642 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
643 LOG_DETAILS="testing if \"$format\" print format works"
644 line=$($cmd_prefix $format)
645 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
647 format="recv_realtime"
648 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
649 LOG_DETAILS="testing if \"$format\" print format works"
650 line=$($cmd_prefix $format)
651 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
654 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"
655 LOG_DETAILS="testing if \"$format\" print format works"
656 line=$($cmd_prefix $format)
657 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
660 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
661 LOG_DETAILS="testing if \"$format\" print format works"
662 line=$($cmd_prefix $format)
663 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
666 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
667 LOG_DETAILS="testing if \"$format\" print format works"
668 line=$($cmd_prefix $format)
669 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
672 if [ "$type" = "pipe" ]; then
673 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"
675 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"
677 LOG_DETAILS="testing if \"$format\" print format works"
678 line=$($cmd_prefix $format)
679 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
682 LOG_DETAILS="testing if \"$format\" print format works"
684 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
685 line=$($cmd_prefix $format)
686 [ "$line" = "rawformatTEST" ] && ok || fail
688 if [ "$quick" -ne 1 ]; then
689 LOG_DETAILS="testing if dlogsend -d works"
691 dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
695 # In 10 seconds, 3 cycles 7 logs each should have happened.
696 [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
699 if [ "$type" = "pipe" ]; then
700 for format in raw brief long; do
701 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
702 LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
703 log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
706 dlogsend -b main "$log"
707 line=$($cmd_prefix $format | tr '\n' '@')
714 REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
717 REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
721 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
726 LOG_DETAILS="testing if KMSG works in the default format"
727 # We check if the command returns at least 100 logs. This seems to be a safe value.
728 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
729 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
731 if [ "$quick" -ne 1 ]; then
732 LOG_DETAILS="testing if KMSG works in the raw format"
733 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
734 sleep 1 # To make sure dlog has already parsed the log
735 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
737 LOG_DETAILS="testing if pid filtering works"
738 dlogsend -b main -t DLOG_TESTSUITE pidTEST &
740 line=$(dlogutil -v raw -d --pid $!)
741 [ "$line" = "pidTEST" ] && ok || fail
743 LOG_DETAILS="testing if tid filtering works"
744 dlogsend -b main -t DLOG_TESTSUITE tidTEST &
746 line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
747 [ "$line" = "tidTEST" ] && ok || fail
749 mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
750 LOG_DETAILS="testing if secure logging works (1/2)"
751 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
752 echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
754 dlogsend -zb main -t DLOG_TESTSUITE secure &
756 dlogsend -b main -t DLOG_TESTSUITE insecure &
758 line=$(dlogutil -v raw -d | head -n1)
759 [ "$line" = "secure" ] && ok || fail
760 LOG_DETAILS="testing if secure logging works (2/2)"
761 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
762 echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
764 dlogsend -zb main -t DLOG_TESTSUITE secure &
766 dlogsend -b main -t DLOG_TESTSUITE insecure &
768 line=$(dlogutil -v raw -d | head -n1)
769 [ "$line" = "insecure" ] && ok || fail
770 mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
773 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
774 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
775 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
777 LOG_DETAILS="testing if tag filtering works (1/8)"
778 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
779 LOG_DETAILS="testing if tag filtering works (2/8)"
780 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
781 LOG_DETAILS="testing if tag filtering works (3/8)"
782 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
783 LOG_DETAILS="testing if tag filtering works (4/8)"
784 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
785 LOG_DETAILS="testing if tag filtering works (5/8)"
786 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
787 LOG_DETAILS="testing if tag filtering works (6/8)"
788 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
789 LOG_DETAILS="testing if tag filtering works (7/8)"
790 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
791 LOG_DETAILS="testing if tag filtering works (8/8)"
792 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
794 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
795 LOG_DETAILS="testing if limiter and runtime filtering works"
798 [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
801 LOG_DETAILS="testing proper SMACK label for dynamic config file location"
802 ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
803 # Running ls on an arbitrary directory and trying to make sense of results is problematic.
804 # However, this is not a problem in our usecase, as we only care about the first field, the security context.
805 # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
806 # shellcheck disable=SC2012
807 [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
809 if [ "$quick" -ne 1 ]; then
810 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
811 # PID-based limits and see what happens.
812 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
814 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
815 dlogctl --pid $DLOGSEND_PID -s deny
816 kill -s CONT $DLOGSEND_PID
818 LOG_DETAILS="testing if PID limiting works (1/14)"
819 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
820 LOG_DETAILS="testing if PID limiting works (2/14)"
821 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
822 LOG_DETAILS="testing if PID limiting works (3/14)"
823 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
824 LOG_DETAILS="testing if PID limiting works (4/14)"
825 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
827 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
829 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
830 dlogctl --pid $DLOGSEND_PID -s 25
831 kill -s CONT $DLOGSEND_PID
833 LOG_DETAILS="testing if PID limiting works (5/14)"
834 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
835 LOG_DETAILS="testing if PID limiting works (6/14)"
836 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
837 LOG_DETAILS="testing if PID limiting works (7/14)"
838 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
839 LOG_DETAILS="testing if PID limiting works (8/14)"
840 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
842 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
844 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
845 dlogctl --pid $DLOGSEND_PID -s allow
846 kill -s CONT $DLOGSEND_PID
848 LOG_DETAILS="testing if PID limiting works (9/14)"
849 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
850 LOG_DETAILS="testing if PID limiting works (10/14)"
851 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 0 ] && ok || fail
852 LOG_DETAILS="testing if PID limiting works (11/14)"
853 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
854 LOG_DETAILS="testing if PID limiting works (12/14)"
855 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
857 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
858 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
860 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
861 dlogctl --pid $DLOGSEND_PID -s 2
862 kill -s CONT $DLOGSEND_PID
864 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
865 LOG_DETAILS="testing if PID limiting works (13/14)"
866 # In each 5 second period, 5 logs are sent, so there are 3 periods.
867 # 2 logs from each period are allowed.
868 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
869 LOG_DETAILS="testing if PID limiting works (14/14)"
870 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 3 ] && ok || fail
872 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
873 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
874 kill $LOGGER > /dev/null
876 if [ "$type" = "logger" ]; then
883 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
884 LOG_DETAILS="testing if QoS works (1/4)"
885 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
886 kill $LOGGER > /dev/null
888 if [ "$type" = "logger" ]; then
895 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
896 LOG_DETAILS="testing if QoS works (2/4)"
897 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
898 kill $LOGGER > /dev/null
900 if [ "$type" = "logger" ]; then
907 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
909 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
911 LOG_DETAILS="testing if QoS works (3/4)"
912 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
913 LOG_DETAILS="testing if QoS works (4/4)"
914 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
916 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
917 kill $LOGGER > /dev/null
925 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
928 LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
929 [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
931 LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
932 [ "$(dlogutil -db main -v tag)" = "I/FOO : Hi" ] && ok || fail
935 # No need to check output in this case, as nothing is written to stdout anyway
936 dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
938 LOG_DETAILS="testing if basic stderr redirection works correctly"
939 [ "$(dlogutil -db main -v tag)" = "E/BAR : Hi" ] && ok || fail
942 LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
943 [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
945 LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
946 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
949 dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
951 LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
952 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
953 LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
954 [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
957 dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
959 LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
960 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
961 LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
962 [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
965 echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
967 LOG_DETAILS="testing if redirector passes stdin correctly"
968 [ "$(dlogutil -db main -v tag)" = "I/BAZ : Testing" ] && ok || fail
971 dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
979 LOG_DETAILS="testing if multiline logs are handled correctly"
980 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
983 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
985 LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
986 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
989 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
991 LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
992 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
995 rm -f "$TESTDIR"/input
996 rm -f "$TESTDIR"/output
997 base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
998 dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
1000 dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
1001 LOG_DETAILS="testing if long lines are handled correctly"
1002 # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
1003 [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
1006 if [ "$type" = "pipe" ]; then
1007 # TODO: Actually implement and enable this in the logger backend
1008 dlogctl -b main --disable-stdout
1009 LOG_DETAILS="testing if redirection can be disabled (1/3)"
1010 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1012 LOG_DETAILS="testing if redirection can be disabled (2/3)"
1013 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
1014 LOG_DETAILS="testing if redirection can be disabled (3/3)"
1015 [ "$(dlogctl --get | grep main)" = "* main: ENABLED (stdout: DISABLED)" ] && ok || fail
1017 dlogctl -b main --enable-stdout
1018 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1019 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1021 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1022 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1023 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1024 [ "$(dlogctl --get | grep main)" = "* main: ENABLED (stdout: ENABLED)" ] && ok || fail
1028 dlogctl -b radio --disable-stdout
1029 dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1031 LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1032 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1035 LOG_DETAILS="testing if redirection check works (1/5)"
1036 test_libredirect 0 0 && ok || fail
1037 LOG_DETAILS="testing if redirection check works (2/5)"
1038 dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1039 LOG_DETAILS="testing if redirection check works (3/5)"
1040 dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1041 LOG_DETAILS="testing if redirection check works (4/5)"
1042 dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1043 LOG_DETAILS="testing if redirection check works (5/5)"
1044 dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1046 LOG_DETAILS="testing if multiple redirections work (1/4)"
1047 test_libredirect_multi && ok || fail
1048 LOG_DETAILS="testing if multiple redirections work (2/4)"
1049 [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1 : Message 1" ] && ok || fail
1050 LOG_DETAILS="testing if multiple redirections work (3/4)"
1051 [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2 : Message 2" ] && ok || fail
1052 LOG_DETAILS="testing if multiple redirections work (4/4)"
1053 [ "$(dlogutil -db system -v tag)" = "I/TAG3 : Message 3" ] && ok || fail
1054 dlogutil -cb main -b system
1058 echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1059 echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1061 dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1062 LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1063 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1064 LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1065 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1067 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1068 LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1069 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1070 LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1071 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1073 echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1075 dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1076 LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1077 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1078 LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1079 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1081 dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1082 LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1083 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1084 LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1085 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1086 LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1087 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1089 echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1091 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1092 LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1093 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1094 LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1095 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1096 LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1097 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1099 rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1101 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1102 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1104 if [ "$(date +%Y)" -le 2000 ]; then
1105 # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1106 date -s "@2121212121" > /dev/null
1108 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1109 LOG_DETAILS="testing if the timestamps are correct"
1110 test_verifytime && ok || fail
1112 if [ "$quick" -ne 1 ]; then
1113 PREAUTODETECT_CONFIG_PATH=$DLOG_CONFIG_PATH
1114 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.autodetect"
1115 kill $LOGGER > /dev/null
1117 if [ "$type" = "logger" ]; then
1124 LOG_DETAILS="testing if backend autodetection works properly (1/2)"
1125 dlogsend -b main -t DLOG_AUTODETECT "xd" && ok || fail
1128 DLOG_CONFIG_PATH=$PREAUTODETECT_CONFIG_PATH
1129 LOG_DETAILS="testing if backend autodetection works properly (2/2)"
1130 [ "$(dlogutil -db main -v raw)" = "xd" ] && ok || fail
1132 kill $LOGGER > /dev/null
1139 dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
1141 LOG_DETAILS="testing if logger daemon persistent logging works (1/3)"
1142 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1143 LOG_DETAILS="testing if logger daemon persistent logging works (2/3)"
1144 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test1)" -eq 100 ] && ok || fail
1145 LOG_DETAILS="testing if logger daemon persistent logging works (3/3)"
1146 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test2)" -eq 0 ] && ok || fail
1148 dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
1150 RESULT=$(du -c "$TESTDIR"/test0* | tail -n 1 | cut -f 1)
1151 LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
1152 [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
1153 LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
1154 [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
1157 kill $LOGGER > /dev/null
1159 rm "$TESTDIR"/test0*
1160 dlog_logger -t 5 -b 100000 &
1164 dlogsend -b main -t DLOG_LOGGER_TEST2 -c 100 "hi"
1166 LOG_DETAILS="testing if logger daemon buffering works (1/5)"
1167 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1170 LOG_DETAILS="testing if logger daemon buffering works (2/5)"
1171 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1173 dlogsend -b main -t DLOG_LOGGER_TEST3 -c 100 "hi"
1175 LOG_DETAILS="testing if logger daemon buffering works (3/5)"
1176 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1179 kill $LOGGER > /dev/null
1181 dlog_logger -t 10000 -b 100 &
1184 LOG_DETAILS="testing if logger daemon buffering works (4/5)"
1185 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1187 dlogsend -b main -t DLOG_LOGGER_TEST4 -c 100 "hi"
1189 LOG_DETAILS="testing if logger daemon buffering works (5/5)"
1190 [ "$(grep -c DLOG_LOGGER_TEST4 "$TESTDIR"/test0)" -gt 90 ] && ok || fail
1193 kill $LOGGER > /dev/null
1199 LOG_DETAILS="testing if the library works with multithreaded app"
1200 dlogutil -f $TESTDIR/dlog_mt_test &
1202 test_libdlog && ok || fail
1211 # Spawn logging scripts and wait for them to finish.
1212 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1213 # wait for all the jobs of the subshell, which means all the logging scripts.
1214 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1215 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1216 # group, sent about at the same time.
1218 for i in $(seq 1 $SPAWN_CNT); do
1219 delay=$(shuf -i 1-5 -n 1)
1220 ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1225 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1227 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1229 if [ "$type" = "pipe" ]; then
1230 for P in recv sent; do
1231 for Q in mono real; do
1232 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1233 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1236 for P in recv sent; do
1237 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1238 dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1239 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1240 dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
1244 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1246 for format in $sort_formats; do
1247 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1249 # collect data and analyze timestamps
1250 dlogutil -b main -d -v "$format" | (
1254 while read -r line; do
1255 # filter out empty lines in "long" format
1256 [ -z "$line" ] && continue
1257 # filter out non-timestamp lines in "long" format
1258 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1262 ts=$(extract_timestamp "$format" "$line")
1264 if [ "$ts" -ge "$prev_ts" ]; then
1267 printf "%s\n%s\n\n" "$prev_line" "$line" || break
1271 ) | grep -q ^ && fail || ok
1276 # show results, clean up and return an exit code
1278 echo "$OKS / $TOTAL tests passed"
1279 echo "$FAILS / $TOTAL tests failed"
1281 if [ "$quick" -eq 1 ]; then
1282 echo "WARNING: quick mode!"
1283 echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1284 echo "Rerun without '--quick' to run all tests."
1285 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1286 echo "WARNING: legacy logger module!"
1287 echo "Some functionality is untested."
1288 echo "Rerun on a device with the out-of-tree logger module to run all tests."
1292 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1293 # This is because it is used to pass the exit code. If another command
1294 # is executed after it, its exit code will be propagated instead.