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 source dlog_test_header
10 #relevant pids default vals
18 [ "$UTIL_PID" -ne -1 ] && kill "$UTIL_PID" > /dev/null 2>&1
19 [ "$MT_TEST" -ne -1 ] && kill "$MT_TEST" > /dev/null 2>&1
20 [ "$LOGGER" -ne -1 ] && kill "$LOGGER" > /dev/null 2>&1
21 # Shellcheck doesn't like this because the rm call is bad if $TESTDIR is empty.
22 # However, such cases would be caught by the conditional.
23 # shellcheck disable=SC2115
24 [ -d "$TESTDIR" ] && rm -rf "$TESTDIR"/*
25 [ -d "$RUNTIME_FILTERS_DIR" ] && rm -rf "$RUNTIME_FILTERS_DIR"
30 USAGE_MESSAGE="usage: $0 [--verbose] [--quick] pipe|logger"
32 OPTS=$(getopt --shell sh --options "" --long verbose,quick --quiet -- "$@")
33 if [ "$?" -eq 1 ]; then
54 echo "This should never happen :)"
60 if [ "$1" = "pipe" ]; then
62 elif [ "$1" = "logger" ]; then
64 elif [ "$1" = "zero-copy" ]; then
66 # We still accept the legacy {pipe,logger}_quick syntax because there's no reason not to.
67 elif [ "$1" = "pipe_quick" ]; then
70 elif [ "$1" = "logger_quick" ]; then
78 # supress stderr messages from subcommands
79 if [ "$verbose" -eq 0 ]; then
84 capsh --print | grep Current | grep cap_syslog > /dev/null || { echo "*** ERROR: cap_syslog missing"; NEEDS_TO_QUIT=1; }
85 mount | grep ' / ' | grep rw > /dev/null || { echo "*** ERROR: root not mounted read-write"; NEEDS_TO_QUIT=1; }
86 [ "$NEEDS_TO_QUIT" -eq 0 ] || exit 1
88 if [ "$type" = "pipe" ] || [ -f /lib/modules/"$(uname -r)"/extra/logger.ko ]; then
94 if [ -z "$DLOG_CONFIG_PATH" ]; then
95 ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
97 ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
100 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
101 PATH=$PATH:@libexecdir@/libdlog/
103 if [ "$type" != "zero-copy" ]; then # These tests are inherently dynamic.
104 #create dir for runtime filters
105 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
106 mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
108 PREEARLYQUIT_CONFIG_PATH="$DLOG_CONFIG_PATH"
109 DLOG_CONFIG_PATH="$TESTDIR/earlyquit.conf"
110 if [ "$quick" -ne 1 ]; then
111 for PERSISTENT in 1 0; do
113 for SYSLOG in 1 0; do
115 # TODO: Does syslog even work? Disabling for now.
116 [ "$SYSLOG" -eq 1 ] && continue
118 if [ "$QOS" -eq 1 ]; then
119 SOURCE="@datadir@/dlog-$type.conf.qos"
121 SOURCE="@datadir@/dlog-$type.conf.test"
124 if [ "$PERSISTENT" -eq 1 ]; then
125 cp "$SOURCE" "$DLOG_CONFIG_PATH"
127 sed 's/^dlog_logger_conf.*$/#\0/' <"$SOURCE" >"$DLOG_CONFIG_PATH"
130 echo "handle_kmsg=$KMSG" >> "$DLOG_CONFIG_PATH"
131 echo "syslog_force=$SYSLOG" >> "$DLOG_CONFIG_PATH"
137 kill $LOGGER > /dev/null
141 if [ "$PERSISTENT" -eq 0 ] && [ "$KMSG" -eq 0 ] && [ "$SYSLOG" -eq 0 ] && [ "$QOS" -eq 0 ] && [ "$type" = "logger" ]; then
147 LOG_DETAILS="testing if daemon stops when not needed (persistent=$PERSISTENT, kmsg=$KMSG, syslog=$SYSLOG, qos=$QOS)"
148 [ "$STOPPED" -eq "$EXPECTED" ] && ok || fail
149 done; done; done; done
151 DLOG_CONFIG_PATH="$PREEARLYQUIT_CONFIG_PATH"
158 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
161 dlogutil -c -b radio -b system -b main
162 LOG_DETAILS="dlogctl --disable (no args)"
164 [ "$(dlogctl -g | grep regular | grep -c ENABLED)" -eq 0 ] && ok || fail
165 LOG_DETAILS="testing if filters were applied"
166 dlogsend -b system -t TEST test
167 dlogsend -b main -t TEST test
168 dlogsend -b radio -t TEST test
169 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
171 LOG_DETAILS="dlogctl --enable (no args)"
173 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
174 LOG_DETAILS="testing if filters were applied"
175 dlogutil -c -b radio -b system -b main
176 dlogsend -b system -t TEST test
177 dlogsend -b main -t TEST test
178 dlogsend -b radio -t TEST test
179 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
181 LOG_DETAILS="dlogctl --disable (1 arg)"
182 dlogctl --disable -b system
183 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c system)" -eq 1 ] &&
184 [ "$(dlogctl -g | grep regular | grep -c DISABLED )" -eq 1 ] && ok || fail
185 LOG_DETAILS="testing if filters were applied"
186 dlogutil -c -b radio -b system -b main
187 dlogsend -b system -t TEST test
188 dlogsend -b main -t TEST test
189 dlogsend -b radio -t TEST test
190 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
192 LOG_DETAILS="dlogctl --disable (multiple args)"
193 dlogctl --disable -b main -b radio
194 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -cv system)" -eq 2 ] &&
195 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c radio)" -eq 1 ] &&
196 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c main)" -eq 1 ] && ok || fail
197 LOG_DETAILS="testing if filters were applied"
198 dlogutil -c -b radio -b system -b main
199 dlogsend -b system -t TEST test
200 dlogsend -b main -t TEST test
201 dlogsend -b radio -t TEST test
202 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
204 LOG_DETAILS="dlogctl --enable (multiple args)"
205 dlogctl --enable -b radio -b system
206 [ "$(dlogctl -g | grep regular | grep -c DISABLED )" -eq 1 ] &&
207 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c main)" -eq 1 ] &&
208 [ "$(dlogctl -g | grep regular | grep ENABLED | grep -c system)" -eq 1 ] &&
209 [ "$(dlogctl -g | grep regular | grep ENABLED | grep -c radio)" -eq 1 ] && ok || fail
210 LOG_DETAILS="testing if filters were applied"
211 dlogutil -c -b radio -b system -b main
212 dlogsend -b system -t TEST test
213 dlogsend -b main -t TEST test
214 dlogsend -b radio -t TEST test
215 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
217 LOG_DETAILS="dlogctl --enable (1 arg)"
218 dlogctl --enable -b main
219 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
220 LOG_DETAILS="testing if filters were applied"
221 dlogutil -c -b radio -b system -b main
222 dlogsend -b system -t TEST test
223 dlogsend -b main -t TEST test
224 dlogsend -b radio -t TEST test
225 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
227 LOG_DETAILS="dlogctl -s allow"
228 dlogutil -c -b radio -b system -b main
231 dlogctl -t TEST_TAG -s allow
232 dlogsend -b main -t TEST_TAG test
233 dlogsend -b main -t TEST test
234 [ "$(dlogutil -d -b main | wc -l)" -eq 1 ] && ok || fail
236 LOG_DETAILS="dlogctl -s [N]"
237 dlogutil -c -b radio -b system -b main
240 dlogctl -t TEST_TAG -s 3
241 dlogsend -b main -c 10 -t TEST_TAG test
242 dlogsend -b main -c 10 -t TEST test
243 [ "$(dlogutil -d -b main | wc -l)" -eq 4 ] && ok || fail
245 LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
246 dlogctl -s invalid > /dev/null && fail || ok
248 LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
249 dlogctl -p X -g > /dev/null && fail || ok
251 LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
252 dlogctl -b invalid --disable > /dev/null && fail || ok
254 LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
255 dlogctl -s allow -g -t xyz -p E > /dev/null && fail || ok
257 LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
258 dlogctl -s allow -c > /dev/null && fail || ok
260 LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
261 dlogctl -c -g -t xyz -p E > /dev/null && fail || ok
264 LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
265 dlogctl -s -10 > /dev/null && fail || ok
267 LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
268 dlogctl -s 999999 > /dev/null && fail || ok
271 LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
272 dlogctl -s 100 && ok || fail
274 LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
275 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 1 ] && ok || fail
278 LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
279 [ "$(dlogctl -g | grep -F '*:*' | grep -c 100)" -eq 1 ] && ok || fail
282 LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
283 dlogctl -c -t '*' -p '*' && ok || fail
285 LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
286 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 0 ] && ok || fail
289 LOG_DETAILS="testing if the whole dynamic config directory is respected"
291 cp $RUNTIME_FILTERS_DIR/05-logctl.conf $RUNTIME_FILTERS_DIR/10-other.conf
293 dlogutil -c -b radio -b system -b main
294 dlogsend -b system -t TEST test
295 dlogsend -b main -t TEST test
296 dlogsend -b radio -t TEST test
297 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
298 rm $RUNTIME_FILTERS_DIR/10-other.conf
302 SEED=$(head -c 6 /dev/urandom | base64)
303 dlogsend -b main -k "$SEED"
304 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
305 while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
306 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
307 LOG_DETAILS="testing if critical logging works"
308 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
310 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
311 dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
314 kill -s PIPE $DLOGSEND_PID
316 LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
317 [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
320 # put 100 log entries in the "main" buffer
326 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
327 dlogutil -d > /dev/null && ok || fail
329 LOG_DETAILS="testing if -t argument is parsed properly"
330 dlogutil -t dummy > /dev/null && fail || ok
332 LOG_DETAILS="testing if -u argument is parsed properly"
333 dlogutil -du dummy > /dev/null && fail || ok
335 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
336 [ "$(dlogutil -b main -t 20 | wc -l)" -eq 20 ] && ok || fail # less logs than currently in buffer
338 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
339 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
341 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
342 [ "$(dlogutil -b main -d | wc -l)" -eq 100 ] && ok || fail # exactly
344 LOG_DETAILS="testing if reading from dummy buffer returns an error as expected"
345 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
347 if [ "$type" != "zero-copy" ]; then # No buffer split in zero-copy
348 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
349 [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
352 LOG_DETAILS="testing if dlogutil -c empties all buffers"
353 dlogutil -cb main && ok || fail
355 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
357 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
359 LOG_DETAILS="testing if filters work (1/3)"
360 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
362 LOG_DETAILS="testing if filters work (2/3)"
363 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
365 LOG_DETAILS="testing if filters work (3/3)"
366 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
368 LOG_DETAILS="testing if exact filters work (1/3)"
369 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
371 LOG_DETAILS="testing if exact filters work (2/3)"
372 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
374 LOG_DETAILS="testing if exact filters work (3/3)"
375 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
377 LOG_DETAILS="testing if adding \"silent\" filter works"
378 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
380 if [ "$type" != "zero-copy" ]; then # No buffer size access in zero-copy
381 LOG_DETAILS="testing if reading buffer size returns proper exit code"
382 dlogutil -gb main > /dev/null && ok || fail
385 LOG_DETAILS="testing if writing all entries to single file works (-f)"
386 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
388 if [ "$quick" -ne 1 ]; then
389 LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
390 dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
393 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
395 [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
397 LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
398 dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
401 dlogsend -b main "Hi!"
403 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
405 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
407 LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
408 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor1_file &
411 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
413 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
415 LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
416 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor2_file &
419 dlogsend -b main "Hi!"
421 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
423 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
427 test_libdlog 100000 &
428 LIBDLOGUTIL_CORRECT_PID=$!
429 wait $LIBDLOGUTIL_CORRECT_PID
431 # NB: rotation only really makes sense in the continuous mode
432 # from a user PoV, but the logic doesn't care about dumping
433 # so we do it to simplify things
434 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
436 LOG_DETAILS="testing if single file is properly created"
437 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
439 LOG_DETAILS="testing if rotating file is properly created (1/4)"
440 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
442 LOG_DETAILS="testing if rotating file is properly created (2/4)"
443 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
445 LOG_DETAILS="testing if rotating file is properly created (3/4)"
446 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
448 LOG_DETAILS="testing if rotating file is properly created (4/4)"
449 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
451 LOG_DETAILS="testing the size of log files"
452 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
454 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
455 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
456 if [ "$quick" -ne 1 ]; then
457 LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
458 test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
460 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
461 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
462 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
463 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
464 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
465 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
466 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
467 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
468 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
469 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
470 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
471 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
472 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
473 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
474 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
475 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
476 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
477 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
478 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
479 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
480 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
481 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
482 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
483 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
484 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
485 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
486 if [ "$type" != "zero-copy" ]; then # These rely on things not available in zero-copy
487 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
488 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
489 LOG_DETAILS="testing if libdlogutil aliasing works"
490 if [ "$type" = "pipe" ]; then
491 test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
493 DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
496 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
497 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
499 cmd_prefix_no_v="dlogutil -t 1 -u 0"
500 cmd_prefix="$cmd_prefix_no_v -v"
503 regex_prio="[VDIWEFS]{1}"
504 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
505 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
506 regex_timezone="[\+-]{1}[0-9]{4}"
508 for PARAM in always auto none never; do
509 for PRIO in info error; do
510 for OUTPUT in pipe tty; do
511 if [ "$PARAM" = "always" ]; then
513 elif [ "$PARAM" = "never" ]; then
515 elif [ "$OUTPUT" = "tty" ]; then
521 if [ "$PRIO" = "info" ]; then
522 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
523 elif [ "$COLOR_EXPECTED" -eq 0 ]; then
524 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
526 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m \([[:print:]]*\)@~\[0m$/1/g"
529 # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
530 COMMAND="$cmd_prefix $format"
532 if [ "$PARAM" = "none" ]; then
533 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
535 COMMAND="$COMMAND --color=$PARAM"
536 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
539 if [ "$PRIO" = "info" ]; then
540 COMMAND="$COMMAND '*:=I'"
542 COMMAND="$COMMAND '*:=E'"
545 if [ "$OUTPUT" = "tty" ]; then
546 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
547 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
550 # $(eval) is needed since `$COMMAND` may contain `"`
551 line="$(eval "$COMMAND" | tr '\033\n' '~@')"
552 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
556 # TODO: It would be cool to also test warning messages (which have a different color),
557 # but this would require deeper changes to this script, which is already janky enough.
559 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
560 LOG_DETAILS="testing if \"$format\" print format works"
561 line=$($cmd_prefix $format)
562 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
565 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
566 LOG_DETAILS="testing if \"$format\" print format works"
567 line=$($cmd_prefix $format)
568 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
571 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
572 LOG_DETAILS="testing if \"$format\" print format works"
573 line=$($cmd_prefix $format)
574 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
577 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
578 LOG_DETAILS="testing if \"$format\" print format works"
579 line=$($cmd_prefix $format)
580 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
583 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
584 LOG_DETAILS="testing if \"$format\" print format works"
585 line=$($cmd_prefix $format)
586 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
589 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
590 LOG_DETAILS="testing if \"$format\" print format works"
591 line=$($cmd_prefix $format)
592 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
594 format="recv_realtime"
595 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
596 LOG_DETAILS="testing if \"$format\" print format works"
597 line=$($cmd_prefix $format)
598 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
601 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"
602 LOG_DETAILS="testing if \"$format\" print format works"
603 line=$($cmd_prefix $format)
604 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
607 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
608 LOG_DETAILS="testing if \"$format\" print format works"
609 line=$($cmd_prefix $format)
610 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
613 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[: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 if [ "$type" = "pipe" ]; then
620 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"
621 elif [ "$type" = "logger" ]; then
622 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"
624 REGEX="s/^\{\"priority\":\"(verbose|debug|info|warning|error|fatal|silent)\",\"pid\":[1-9][0-9]*,\"tid\":[1-9][0-9]*,\"sent_mono\":[1-9][0-9]+\.[0-9]{9},\"tag\":\"[[:print:]]*\",\"msg\":\"[[: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
630 SOURCE="$DLOG_CONFIG_PATH"
631 DLOG_CONFIG_PATH="$TESTDIR/defaultprintformat.conf"
632 cp "$SOURCE" "$DLOG_CONFIG_PATH"
634 # TODO: It would be good to also test the daemon side of things.
635 LOG_DETAILS="testing default log format selection (none -> brief)"
636 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
637 line=$($cmd_prefix_no_v)
638 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
640 echo "util_default_format=rwtime" >> $DLOG_CONFIG_PATH
641 LOG_DETAILS="testing default log format selection (rwtime)"
642 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"
643 line=$($cmd_prefix_no_v)
644 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
646 DLOG_CONFIG_PATH="$SOURCE"
649 LOG_DETAILS="testing if \"$format\" print format works"
651 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
652 line=$($cmd_prefix $format)
653 [ "$line" = "rawformatTEST" ] && ok || fail
655 if [ "$quick" -ne 1 ]; then
656 LOG_DETAILS="testing if dlogsend -d works"
658 dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
662 # In 10 seconds, 3 cycles 7 logs each should have happened.
663 [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
666 if [ "$type" = "pipe" ]; then
667 for format in raw brief long; do
668 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
669 LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
670 log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
673 dlogsend -b main "$log"
674 line=$($cmd_prefix $format | tr '\n' '@')
681 REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
684 REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
688 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
693 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
694 LOG_DETAILS="testing if KMSG works in the default format"
695 # We check if the command returns at least 100 logs. This seems to be a safe value.
696 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
697 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
700 if [ "$quick" -ne 1 ]; then
701 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
702 LOG_DETAILS="testing if KMSG works in the raw format"
703 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
704 sleep 1 # To make sure dlog has already parsed the log
705 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
708 LOG_DETAILS="testing if pid filtering works"
709 dlogsend -b main -t DLOG_TESTSUITE pidTEST &
711 line=$(dlogutil -v raw -d --pid $!)
712 [ "$line" = "pidTEST" ] && ok || fail
714 LOG_DETAILS="testing if tid filtering works"
715 dlogsend -b main -t DLOG_TESTSUITE tidTEST &
717 line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
718 [ "$line" = "tidTEST" ] && ok || fail
720 mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
721 LOG_DETAILS="testing if secure logging works (1/2)"
722 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
723 echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
725 dlogsend -zb main -t DLOG_TESTSUITE secure &
727 dlogsend -b main -t DLOG_TESTSUITE insecure &
729 line=$(dlogutil -v raw -d | head -n1)
730 [ "$line" = "secure" ] && ok || fail
731 LOG_DETAILS="testing if secure logging works (2/2)"
732 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
733 echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
735 dlogsend -zb main -t DLOG_TESTSUITE secure &
737 dlogsend -b main -t DLOG_TESTSUITE insecure &
739 line=$(dlogutil -v raw -d | head -n1)
740 [ "$line" = "insecure" ] && ok || fail
741 mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
744 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
745 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
746 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
748 LOG_DETAILS="testing if tag filtering works (1/8)"
749 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
750 LOG_DETAILS="testing if tag filtering works (2/8)"
751 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
752 LOG_DETAILS="testing if tag filtering works (3/8)"
753 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
754 LOG_DETAILS="testing if tag filtering works (4/8)"
755 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
756 LOG_DETAILS="testing if tag filtering works (5/8)"
757 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
758 LOG_DETAILS="testing if tag filtering works (6/8)"
759 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
760 LOG_DETAILS="testing if tag filtering works (7/8)"
761 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
762 LOG_DETAILS="testing if tag filtering works (8/8)"
763 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
765 if [ "$TEST_DYNAMIC_FILTERS" = "true" ] && [ "$type" != "zero-copy" ]; then # No dynamic things in zero-copy
766 LOG_DETAILS="testing if limiter and runtime filtering works"
769 [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
772 LOG_DETAILS="testing proper SMACK label for dynamic config file location"
773 ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
774 # Running ls on an arbitrary directory and trying to make sense of results is problematic.
775 # However, this is not a problem in our usecase, as we only care about the first field, the security context.
776 # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
777 # shellcheck disable=SC2012
778 [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
780 if [ "$quick" -ne 1 ]; then
781 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
782 # PID-based limits and see what happens.
783 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
785 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
786 dlogctl --pid $DLOGSEND_PID -s deny
787 kill -s CONT $DLOGSEND_PID
789 LOG_DETAILS="testing if PID limiting works (1/14)"
790 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
791 LOG_DETAILS="testing if PID limiting works (2/14)"
792 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
793 LOG_DETAILS="testing if PID limiting works (3/14)"
794 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
795 LOG_DETAILS="testing if PID limiting works (4/14)"
796 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
798 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
800 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
801 dlogctl --pid $DLOGSEND_PID -s 25
802 kill -s CONT $DLOGSEND_PID
804 LOG_DETAILS="testing if PID limiting works (5/14)"
805 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
806 LOG_DETAILS="testing if PID limiting works (6/14)"
807 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
808 LOG_DETAILS="testing if PID limiting works (7/14)"
809 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
810 LOG_DETAILS="testing if PID limiting works (8/14)"
811 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
813 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
815 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
816 dlogctl --pid $DLOGSEND_PID -s allow
817 kill -s CONT $DLOGSEND_PID
819 LOG_DETAILS="testing if PID limiting works (9/14)"
820 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
821 LOG_DETAILS="testing if PID limiting works (10/14)"
822 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 0 ] && ok || fail
823 LOG_DETAILS="testing if PID limiting works (11/14)"
824 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
825 LOG_DETAILS="testing if PID limiting works (12/14)"
826 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
828 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
829 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
831 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
832 dlogctl --pid $DLOGSEND_PID -s 2
833 kill -s CONT $DLOGSEND_PID
835 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
836 LOG_DETAILS="testing if PID limiting works (13/14)"
837 # In each 5 second period, 5 logs are sent, so there are 3 periods.
838 # 2 logs from each period are allowed.
839 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
840 LOG_DETAILS="testing if PID limiting works (14/14)"
841 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 3 ] && ok || fail
843 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
844 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
845 kill $LOGGER > /dev/null
847 if [ "$type" = "logger" ]; then
854 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
855 LOG_DETAILS="testing if QoS works (1/4)"
856 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
857 kill $LOGGER > /dev/null
859 if [ "$type" = "logger" ]; then
866 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
867 LOG_DETAILS="testing if QoS works (2/4)"
868 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
869 kill $LOGGER > /dev/null
871 if [ "$type" = "logger" ]; then
878 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
880 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
882 LOG_DETAILS="testing if QoS works (3/4)"
883 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
884 LOG_DETAILS="testing if QoS works (4/4)"
885 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
887 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
888 kill $LOGGER > /dev/null
894 # limiter test (the feature allows to enable/disable particular log type)
895 dlogsend -c 11 -d 1 -t QWE blabla &
898 dlogctl -t QWE -s deny
899 LOG_DETAILS="testing if log limiter works (deny) (1/3)"
900 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
902 LOG_DETAILS="testing if log limiter works (deny) (2/3)"
903 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
905 dlogctl -t QWE -s allow
907 LOG_DETAILS="testing if log limiter works (allow) (3/3)"
908 [ "$(dlogutil -d QWE | wc -l)" -eq 5 ] && ok || fail
914 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
917 LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
918 [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
920 LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
921 [ "$(dlogutil -db main -v tag)" = "I/FOO : Hi" ] && ok || fail
924 # No need to check output in this case, as nothing is written to stdout anyway
925 dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
927 LOG_DETAILS="testing if basic stderr redirection works correctly"
928 [ "$(dlogutil -db main -v tag)" = "E/BAR : Hi" ] && ok || fail
931 LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
932 [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
934 LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
935 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
938 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
939 dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
941 LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
942 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
943 LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
944 [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
947 dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
949 LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
950 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
951 LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
952 [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
956 echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
958 LOG_DETAILS="testing if redirector passes stdin correctly"
959 [ "$(dlogutil -db main -v tag)" = "I/BAZ : Testing" ] && ok || fail
962 dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
970 LOG_DETAILS="testing if multiline logs are handled correctly"
971 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
974 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
976 LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
977 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
980 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
982 LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
983 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
986 rm -f "$TESTDIR"/input
987 rm -f "$TESTDIR"/output
988 base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
989 dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
991 dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
992 LOG_DETAILS="testing if long lines are handled correctly"
993 # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
994 [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
997 if [ "$type" = "pipe" ]; then
998 # TODO: Actually implement and enable this in the logger backend
999 dlogctl -b main --disable-stdout
1000 LOG_DETAILS="testing if redirection can be disabled (1/3)"
1001 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1003 LOG_DETAILS="testing if redirection can be disabled (2/3)"
1004 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
1005 LOG_DETAILS="testing if redirection can be disabled (3/3)"
1006 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1008 dlogctl -b main --enable-stdout
1009 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1010 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1012 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1013 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1014 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1015 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1019 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1020 dlogctl -b radio --disable-stdout
1021 dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1023 LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1024 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1028 LOG_DETAILS="testing if redirection check works (1/5)"
1029 test_libredirect 0 0 && ok || fail
1030 LOG_DETAILS="testing if redirection check works (2/5)"
1031 dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1032 LOG_DETAILS="testing if redirection check works (3/5)"
1033 dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1034 LOG_DETAILS="testing if redirection check works (4/5)"
1035 dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1036 LOG_DETAILS="testing if redirection check works (5/5)"
1037 dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1039 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1040 LOG_DETAILS="testing if multiple redirections work (1/4)"
1041 test_libredirect_multi && ok || fail
1042 LOG_DETAILS="testing if multiple redirections work (2/4)"
1043 [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1 : Message 1" ] && ok || fail
1044 LOG_DETAILS="testing if multiple redirections work (3/4)"
1045 [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2 : Message 2" ] && ok || fail
1046 LOG_DETAILS="testing if multiple redirections work (4/4)"
1047 [ "$(dlogutil -db system -v tag)" = "I/TAG3 : Message 3" ] && ok || fail
1048 dlogutil -cb main -b system
1052 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
1054 echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1055 echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1057 dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1058 LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1059 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1060 LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1061 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1063 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1064 LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1065 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1066 LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1067 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1069 echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1071 dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1072 LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1073 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1074 LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1075 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1077 dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1078 LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1079 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1080 LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1081 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1082 LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1083 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1085 echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1087 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1088 LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1089 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1090 LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1091 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1092 LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1093 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1095 rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1098 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1099 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1101 if [ "$(date +%Y)" -le 2000 ]; then
1102 # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1103 date -s "@2121212121" > /dev/null
1105 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1106 LOG_DETAILS="testing if the timestamps are correct"
1107 test_verifytime && ok || fail
1109 if [ "$quick" -ne 1 ]; then
1110 if [ "$type" != "zero-copy" ]; then # Backend autodection and logger daemon stuff don't really work on zero-copy
1111 PREAUTODETECT_CONFIG_PATH=$DLOG_CONFIG_PATH
1112 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.autodetect"
1113 kill $LOGGER > /dev/null
1115 if [ "$type" = "logger" ]; then
1122 LOG_DETAILS="testing if backend autodetection works properly (1/2)"
1123 dlogsend -b main -t DLOG_AUTODETECT "xd" && ok || fail
1126 DLOG_CONFIG_PATH=$PREAUTODETECT_CONFIG_PATH
1127 LOG_DETAILS="testing if backend autodetection works properly (2/2)"
1128 [ "$(dlogutil -db main -v raw)" = "xd" ] && ok || fail
1130 kill $LOGGER > /dev/null
1137 dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
1139 LOG_DETAILS="testing if logger daemon persistent logging works (1/3)"
1140 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1141 LOG_DETAILS="testing if logger daemon persistent logging works (2/3)"
1142 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test1)" -eq 100 ] && ok || fail
1143 LOG_DETAILS="testing if logger daemon persistent logging works (3/3)"
1144 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test2)" -eq 0 ] && ok || fail
1146 dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
1148 RESULT=$(du -c "$TESTDIR"/test0* | tail -n 1 | cut -f 1)
1149 LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
1150 [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
1151 LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
1152 [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
1155 kill $LOGGER > /dev/null
1157 rm "$TESTDIR"/test0*
1158 dlog_logger -t 5 -b 100000 &
1162 dlogsend -b main -t DLOG_LOGGER_TEST2 -c 100 "hi"
1164 LOG_DETAILS="testing if logger daemon buffering works (1/5)"
1165 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1168 LOG_DETAILS="testing if logger daemon buffering works (2/5)"
1169 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1171 dlogsend -b main -t DLOG_LOGGER_TEST3 -c 100 "hi"
1173 LOG_DETAILS="testing if logger daemon buffering works (3/5)"
1174 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1177 kill $LOGGER > /dev/null
1179 dlog_logger -t 10000 -b 100 &
1182 LOG_DETAILS="testing if logger daemon buffering works (4/5)"
1183 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1185 dlogsend -b main -t DLOG_LOGGER_TEST4 -c 100 "hi"
1187 LOG_DETAILS="testing if logger daemon buffering works (5/5)"
1188 [ "$(grep -c DLOG_LOGGER_TEST4 "$TESTDIR"/test0)" -gt 90 ] && ok || fail
1191 kill $LOGGER > /dev/null
1198 LOG_DETAILS="testing if the library works with multithreaded app"
1199 dlogutil -f $TESTDIR/dlog_mt_test &
1201 test_libdlog && ok || fail
1210 # Spawn logging scripts and wait for them to finish.
1211 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1212 # wait for all the jobs of the subshell, which means all the logging scripts.
1213 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1214 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1215 # group, sent about at the same time.
1217 for i in $(seq 1 $SPAWN_CNT); do
1218 delay=$(shuf -i 1-5 -n 1)
1219 ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1224 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1226 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1228 if [ "$type" = "pipe" ]; then
1229 for P in recv sent; do
1230 for Q in mono real; do
1231 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1232 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1235 for P in recv sent; do
1236 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1237 dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1238 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1239 dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
1243 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1245 for format in $sort_formats; do
1246 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1248 # collect data and analyze timestamps
1249 dlogutil -b main -d -v "$format" | (
1253 while read -r line; do
1254 # filter out empty lines in "long" format
1255 [ -z "$line" ] && continue
1256 # filter out non-timestamp lines in "long" format
1257 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1261 ts=$(extract_timestamp "$format" "$line")
1263 if [ "$ts" -ge "$prev_ts" ]; then
1266 printf "%s\n%s\n\n" "$prev_line" "$line" || break
1270 ) | grep -q ^ && fail || ok
1275 # show results, clean up and return an exit code
1277 echo "$OKS / $TOTAL tests passed"
1278 echo "$FAILS / $TOTAL tests failed"
1280 if [ "$quick" -eq 1 ]; then
1281 echo "WARNING: quick mode!"
1282 echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1283 echo "Rerun without '--quick' to run all tests."
1284 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1285 echo "WARNING: legacy logger module!"
1286 echo "Some functionality is untested."
1287 echo "Rerun on a device with the out-of-tree logger module to run all tests."
1291 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1292 # This is because it is used to pass the exit code. If another command
1293 # is executed after it, its exit code will be propagated instead.