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
12 USAGE_MESSAGE="usage: $0 [--verbose] [--quick] pipe|logger|zero-copy"
14 OPTS=$(getopt --shell sh --options "" --long verbose,quick --quiet -- "$@")
15 if [ "$?" -eq 1 ]; then
36 echo "This should never happen :)"
42 if [ "$1" = "pipe" ]; then
44 elif [ "$1" = "logger" ]; then
46 elif [ "$1" = "zero-copy" ]; then
48 # We still accept the legacy {pipe,logger}_quick syntax because there's no reason not to.
49 elif [ "$1" = "pipe_quick" ]; then
52 elif [ "$1" = "logger_quick" ]; then
60 # supress stderr messages from subcommands
61 if [ "$verbose" -eq 0 ]; then
66 capsh --print | grep Current | grep cap_syslog > /dev/null || { echo "*** ERROR: cap_syslog missing"; NEEDS_TO_QUIT=1; }
67 mount | grep ' / ' | grep rw > /dev/null || { echo "*** ERROR: root not mounted read-write"; NEEDS_TO_QUIT=1; }
68 [ "$NEEDS_TO_QUIT" -eq 0 ] || exit 1
70 if [ "$type" = "pipe" ] || [ -f /lib/modules/"$(uname -r)"/extra/logger.ko ]; then
76 if [ -z "$DLOG_CONFIG_PATH" ]; then
77 ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
79 ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
82 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
83 PATH=$PATH:@libexecdir@/libdlog/
85 if [ "$type" != "zero-copy" ]; then # These tests are inherently dynamic.
86 #create dir for runtime filters
87 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
88 mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
90 if [ "$quick" -ne 1 ]; then
91 dlog_test_daemon_early_quit $type
99 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
102 dlogutil -c -b radio -b system -b main
103 LOG_DETAILS="dlogctl --disable (no args, limiter_apply_to_all_buffers=0)"
106 # without 'limiter_apply_to_all_buffers=1' this
107 # only applies to the three platform buffers:
108 # main, radio, and system
109 PREPLOG_CONFIG_PATH="$DLOG_CONFIG_PATH"
110 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.plog"
111 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 3 ] && ok || fail
112 DLOG_CONFIG_PATH="$PREPLOG_CONFIG_PATH"
114 LOG_DETAILS="dlogctl --disable (no args, limiter_apply_to_all_buffers=1)"
116 [ "$(dlogctl -g | grep regular | grep -c ENABLED)" -eq 0 ] && ok || fail
118 LOG_DETAILS="testing if filters were applied"
119 dlogsend -b system -t TEST test
120 dlogsend -b main -t TEST test
121 dlogsend -b radio -t TEST test
122 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
124 LOG_DETAILS="dlogctl --enable (no args)"
126 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
127 LOG_DETAILS="testing if filters were applied"
128 dlogutil -c -b radio -b system -b main
129 dlogsend -b system -t TEST test
130 dlogsend -b main -t TEST test
131 dlogsend -b radio -t TEST test
132 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
134 LOG_DETAILS="dlogctl --disable (1 arg)"
135 dlogctl --disable -b system
136 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c system)" -eq 1 ] &&
137 [ "$(dlogctl -g | grep regular | grep -c DISABLED )" -eq 1 ] && ok || fail
138 LOG_DETAILS="testing if filters were applied"
139 dlogutil -c -b radio -b system -b main
140 dlogsend -b system -t TEST test
141 dlogsend -b main -t TEST test
142 dlogsend -b radio -t TEST test
143 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
145 LOG_DETAILS="dlogctl --disable (multiple args)"
146 dlogctl --disable -b main -b radio
147 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -cv system)" -eq 2 ] &&
148 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c radio)" -eq 1 ] &&
149 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c main)" -eq 1 ] && ok || fail
150 LOG_DETAILS="testing if filters were applied"
151 dlogutil -c -b radio -b system -b main
152 dlogsend -b system -t TEST test
153 dlogsend -b main -t TEST test
154 dlogsend -b radio -t TEST test
155 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
157 LOG_DETAILS="dlogctl --enable (multiple args)"
158 dlogctl --enable -b radio -b system
159 [ "$(dlogctl -g | grep regular | grep -c DISABLED )" -eq 1 ] &&
160 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c main)" -eq 1 ] &&
161 [ "$(dlogctl -g | grep regular | grep ENABLED | grep -c system)" -eq 1 ] &&
162 [ "$(dlogctl -g | grep regular | grep ENABLED | grep -c radio)" -eq 1 ] && ok || fail
163 LOG_DETAILS="testing if filters were applied"
164 dlogutil -c -b radio -b system -b main
165 dlogsend -b system -t TEST test
166 dlogsend -b main -t TEST test
167 dlogsend -b radio -t TEST test
168 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
170 LOG_DETAILS="dlogctl --enable (1 arg)"
171 dlogctl --enable -b main
172 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
173 LOG_DETAILS="testing if filters were applied"
174 dlogutil -c -b radio -b system -b main
175 dlogsend -b system -t TEST test
176 dlogsend -b main -t TEST test
177 dlogsend -b radio -t TEST test
178 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
180 LOG_DETAILS="dlogctl -s allow"
181 dlogutil -c -b radio -b system -b main
184 dlogctl -t TEST_TAG -s allow
185 dlogsend -b main -t TEST_TAG test
186 dlogsend -b main -t TEST test
187 [ "$(dlogutil -d -b main | wc -l)" -eq 1 ] && ok || fail
189 LOG_DETAILS="dlogctl -s [N]"
190 dlogutil -c -b radio -b system -b main
193 dlogctl -t TEST_TAG -s 3
194 dlogsend -b main -c 10 -t TEST_TAG test
195 dlogsend -b main -c 10 -t TEST test
196 [ "$(dlogutil -d -b main | wc -l)" -eq 4 ] && ok || fail
198 LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
199 dlogctl -s invalid > /dev/null && fail || ok
201 LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
202 dlogctl -p X -g > /dev/null && fail || ok
204 LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
205 dlogctl -b invalid --disable > /dev/null && fail || ok
207 LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
208 dlogctl -s allow -g -t xyz -p E > /dev/null && fail || ok
210 LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
211 dlogctl -s allow -c > /dev/null && fail || ok
213 LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
214 dlogctl -c -g -t xyz -p E > /dev/null && fail || ok
217 LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
218 dlogctl -s -10 > /dev/null && fail || ok
220 LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
221 dlogctl -s 999999 > /dev/null && fail || ok
224 LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
225 dlogctl -s 100 && ok || fail
227 LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
228 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 1 ] && ok || fail
231 LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
232 [ "$(dlogctl -g | grep -F '*:*' | grep -c 100)" -eq 1 ] && ok || fail
235 LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
236 dlogctl -c -t '*' -p '*' && ok || fail
238 LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
239 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 0 ] && ok || fail
242 LOG_DETAILS="testing if the whole dynamic config directory is respected"
244 cp $RUNTIME_FILTERS_DIR/05-logctl.conf $RUNTIME_FILTERS_DIR/10-other.conf
246 dlogutil -c -b radio -b system -b main
247 dlogsend -b system -t TEST test
248 dlogsend -b main -t TEST test
249 dlogsend -b radio -t TEST test
250 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
251 rm $RUNTIME_FILTERS_DIR/10-other.conf
255 SEED=$(head -c 6 /dev/urandom | base64)
256 dlogsend -b main -k "$SEED"
257 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
258 while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
259 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
260 LOG_DETAILS="testing if critical logging works"
261 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
263 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
264 dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
267 kill -s PIPE $DLOGSEND_PID
269 LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
270 [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
275 LOG_DETAILS="testing if dlogutil rejects dotnet"
276 dlogutil -db dotnet_api && fail || ok
278 LOG_DETAILS="testing if dlogutil rejects native"
279 dlogutil -db native_api && fail || ok
281 LOG_DETAILS="testing if dotnet redirects to regular apps (1/2)"
282 dlogsend -b dotnet_api "TEST"
284 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
286 LOG_DETAILS="testing if dotnet redirects to regular apps (2/2)"
287 dlogsend -c 6 -b dotnet_api "TEST"
289 [ "$(dlogutil -d -b apps | wc -l)" -eq 7 ] && ok || fail
291 LOG_DETAILS="testing if native redirects to regular apps (1/2)"
292 dlogsend -b native_api "TEST"
294 [ "$(dlogutil -d -b apps | wc -l)" -eq 8 ] && ok || fail
296 LOG_DETAILS="testing if native redirects to regular apps (2/2)"
297 dlogsend -c 6 -b native_api "TEST"
299 [ "$(dlogutil -d -b apps | wc -l)" -eq 14 ] && ok || fail
301 if [ "$type" != "zero-copy" ]; then # These tests are inherently dynamic.
303 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (1/4)"
304 dlogctl -b apps --enable
305 dlogctl -b dotnet_api --disable
306 dlogsend -b dotnet_api "TEST"
307 [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
308 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (2/4)"
309 dlogsend -b apps "TEST"
310 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
311 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (3/4)"
312 dlogctl -b apps --disable
313 dlogctl -b dotnet_api --enable
314 dlogsend -b dotnet_api "TEST"
315 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
316 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (4/4)"
317 dlogsend -b apps "TEST"
318 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
322 LOG_DETAILS="testing if blocking native and regular apps is separate (1/4)"
323 dlogctl -b apps --enable
324 dlogctl -b native_api --disable
325 dlogsend -b native_api "TEST"
326 [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
327 LOG_DETAILS="testing if blocking native and regular apps is separate (2/4)"
328 dlogsend -b apps "TEST"
329 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
330 LOG_DETAILS="testing if blocking native and regular apps is separate (3/4)"
331 dlogctl -b apps --disable
332 dlogctl -b native_api --enable
333 dlogsend -b native_api "TEST"
334 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
335 LOG_DETAILS="testing if blocking native and regular apps is separate (4/4)"
336 dlogsend -b apps "TEST"
337 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
340 # put 100 log entries in the "main" buffer
346 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
347 dlogutil -d > /dev/null && ok || fail
349 LOG_DETAILS="testing if -t argument is parsed properly"
350 dlogutil -t dummy > /dev/null && fail || ok
352 LOG_DETAILS="testing if -u argument is parsed properly"
353 dlogutil -du dummy > /dev/null && fail || ok
355 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
356 [ "$(dlogutil -b main -t 20 | wc -l)" -eq 20 ] && ok || fail # less logs than currently in buffer
358 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
359 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
361 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
362 [ "$(dlogutil -b main -d | wc -l)" -eq 100 ] && ok || fail # exactly
364 LOG_DETAILS="testing if reading from dummy buffer returns an error as expected"
365 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
367 if [ "$type" != "zero-copy" ]; then # No buffer split in zero-copy
368 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
369 [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
372 LOG_DETAILS="testing if dlogutil -c empties all buffers"
373 dlogutil -cb main && ok || fail
375 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
377 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
379 LOG_DETAILS="testing if filters work (1/3)"
380 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
382 LOG_DETAILS="testing if filters work (2/3)"
383 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
385 LOG_DETAILS="testing if filters work (3/3)"
386 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
388 LOG_DETAILS="testing if exact filters work (1/3)"
389 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
391 LOG_DETAILS="testing if exact filters work (2/3)"
392 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
394 LOG_DETAILS="testing if exact filters work (3/3)"
395 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
397 LOG_DETAILS="testing if adding \"silent\" filter works"
398 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
400 if [ "$type" != "zero-copy" ]; then # No buffer size access in zero-copy
401 LOG_DETAILS="testing if reading buffer size returns proper exit code"
402 dlogutil -gb main > /dev/null && ok || fail
405 LOG_DETAILS="testing if writing all entries to single file works (-f)"
406 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
408 if [ "$quick" -ne 1 ]; then
409 LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
410 dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
413 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
415 [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
417 LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
418 dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
421 dlogsend -b main "Hi!"
423 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
425 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
427 LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
428 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor1_file &
431 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
433 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
435 LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
436 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor2_file &
439 dlogsend -b main "Hi!"
441 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
443 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
447 test_libdlog 100000 &
448 LIBDLOGUTIL_CORRECT_PID=$!
449 wait $LIBDLOGUTIL_CORRECT_PID
451 # NB: rotation only really makes sense in the continuous mode
452 # from a user PoV, but the logic doesn't care about dumping
453 # so we do it to simplify things
454 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
456 LOG_DETAILS="testing if single file is properly created"
457 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
459 LOG_DETAILS="testing if rotating file is properly created (1/4)"
460 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
462 LOG_DETAILS="testing if rotating file is properly created (2/4)"
463 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
465 LOG_DETAILS="testing if rotating file is properly created (3/4)"
466 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
468 LOG_DETAILS="testing if rotating file is properly created (4/4)"
469 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
471 LOG_DETAILS="testing the size of log files"
472 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
474 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
475 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
476 if [ "$quick" -ne 1 ]; then
477 LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
478 test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
480 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
481 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
482 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
483 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
484 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
485 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
486 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
487 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
488 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
489 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
490 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
491 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
492 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
493 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
494 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
495 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
496 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
497 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
498 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
499 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
500 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
501 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
502 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
503 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
504 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
505 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
506 if [ "$type" != "zero-copy" ]; then # These rely on things not available in zero-copy
507 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
508 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
509 LOG_DETAILS="testing if libdlogutil aliasing works"
510 if [ "$type" = "pipe" ]; then
511 test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
513 DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
516 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
517 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
519 cmd_prefix_no_v="dlogutil -t 1 -u 0"
520 cmd_prefix="$cmd_prefix_no_v -v"
523 regex_prio="[VDIWEFS]{1}"
524 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
525 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
526 regex_timezone="[\+-]{1}[0-9]{4}"
528 for PARAM in always auto none never; do
529 for PRIO in info error; do
530 for OUTPUT in pipe tty; do
531 if [ "$PARAM" = "always" ]; then
533 elif [ "$PARAM" = "never" ]; then
535 elif [ "$OUTPUT" = "tty" ]; then
541 if [ "$PRIO" = "info" ]; then
542 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
543 elif [ "$COLOR_EXPECTED" -eq 0 ]; then
544 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
546 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m \([[:print:]]*\)@~\[0m$/1/g"
549 # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
550 COMMAND="$cmd_prefix $format"
552 if [ "$PARAM" = "none" ]; then
553 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
555 COMMAND="$COMMAND --color=$PARAM"
556 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
559 if [ "$PRIO" = "info" ]; then
560 COMMAND="$COMMAND '*:=I'"
562 COMMAND="$COMMAND '*:=E'"
565 if [ "$OUTPUT" = "tty" ]; then
566 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
567 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
570 # $(eval) is needed since `$COMMAND` may contain `"`
571 line="$(eval "$COMMAND" | tr '\033\n' '~@')"
572 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
576 # TODO: It would be cool to also test warning messages (which have a different color),
577 # but this would require deeper changes to this script, which is already janky enough.
579 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
580 LOG_DETAILS="testing if \"$format\" print format works"
581 line=$($cmd_prefix $format)
582 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
585 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
586 LOG_DETAILS="testing if \"$format\" print format works"
587 line=$($cmd_prefix $format)
588 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
591 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
592 LOG_DETAILS="testing if \"$format\" print format works"
593 line=$($cmd_prefix $format)
594 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
597 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
598 LOG_DETAILS="testing if \"$format\" print format works"
599 line=$($cmd_prefix $format)
600 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
603 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
604 LOG_DETAILS="testing if \"$format\" print format works"
605 line=$($cmd_prefix $format)
606 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
609 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
610 LOG_DETAILS="testing if \"$format\" print format works"
611 line=$($cmd_prefix $format)
612 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
614 format="recv_realtime"
615 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
616 LOG_DETAILS="testing if \"$format\" print format works"
617 line=$($cmd_prefix $format)
618 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
621 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"
622 LOG_DETAILS="testing if \"$format\" print format works"
623 line=$($cmd_prefix $format)
624 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
627 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
628 LOG_DETAILS="testing if \"$format\" print format works"
629 line=$($cmd_prefix $format)
630 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
633 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
634 LOG_DETAILS="testing if \"$format\" print format works"
635 line=$($cmd_prefix $format)
636 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
639 if [ "$type" = "pipe" ]; then
640 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_real_ns\":[1-9][0-9]*,\"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_real_ns\":[1-9][0-9]*,\"sent_mono\":[1-9][0-9]+\.[0-9]{9},\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
641 elif [ "$type" = "logger" ]; then
642 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\",\"sent_real_ns\":[1-9][0-9]*,\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
644 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"
646 LOG_DETAILS="testing if \"$format\" print format works"
647 line=$($cmd_prefix $format)
648 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
650 SOURCE="$DLOG_CONFIG_PATH"
651 DLOG_CONFIG_PATH="$TESTDIR/defaultprintformat.conf"
652 cp "$SOURCE" "$DLOG_CONFIG_PATH"
654 # TODO: It would be good to also test the daemon side of things.
655 LOG_DETAILS="testing default log format selection (none -> brief)"
656 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
657 line=$($cmd_prefix_no_v)
658 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
660 echo "util_default_format=rwtime" >> $DLOG_CONFIG_PATH
661 LOG_DETAILS="testing default log format selection (rwtime)"
662 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"
663 line=$($cmd_prefix_no_v)
664 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
666 DLOG_CONFIG_PATH="$SOURCE"
669 LOG_DETAILS="testing if \"$format\" print format works"
671 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
672 line=$($cmd_prefix $format)
673 [ "$line" = "rawformatTEST" ] && ok || fail
675 if [ "$quick" -ne 1 ]; then
676 LOG_DETAILS="testing if dlogsend -d works"
678 dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
682 # In 10 seconds, 3 cycles 7 logs each should have happened.
683 [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
686 if [ "$type" = "pipe" ]; then
687 for format in raw brief long; do
688 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
689 LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
690 log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
693 dlogsend -b main "$log"
694 line=$($cmd_prefix $format | tr '\n' '@')
701 REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
704 REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
708 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
713 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
714 LOG_DETAILS="testing if KMSG works in the default format"
715 # We check if the command returns at least 100 logs. This seems to be a safe value.
716 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
717 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
720 if [ "$quick" -ne 1 ]; then
721 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
722 LOG_DETAILS="testing if KMSG works in the raw format"
723 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
724 sleep 1 # To make sure dlog has already parsed the log
725 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
728 dlog_test_tid_filtering_secure_logging $type
731 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
737 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
738 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
739 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
741 LOG_DETAILS="testing if tag filtering works (1/8)"
742 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
743 LOG_DETAILS="testing if tag filtering works (2/8)"
744 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
745 LOG_DETAILS="testing if tag filtering works (3/8)"
746 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
747 LOG_DETAILS="testing if tag filtering works (4/8)"
748 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
749 LOG_DETAILS="testing if tag filtering works (5/8)"
750 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
751 LOG_DETAILS="testing if tag filtering works (6/8)"
752 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
753 LOG_DETAILS="testing if tag filtering works (7/8)"
754 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
755 LOG_DETAILS="testing if tag filtering works (8/8)"
756 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
758 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
759 LOG_DETAILS="testing if limiter and runtime filtering works"
762 [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
765 LOG_DETAILS="testing proper SMACK label for dynamic config file location"
766 ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
767 # Running ls on an arbitrary directory and trying to make sense of results is problematic.
768 # However, this is not a problem in our usecase, as we only care about the first field, the security context.
769 # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
770 # shellcheck disable=SC2012
771 [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
773 if [ "$quick" -ne 1 ]; then
774 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
775 # PID-based limits and see what happens.
776 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
778 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
779 dlogctl --pid $DLOGSEND_PID -s deny
780 kill -s CONT $DLOGSEND_PID
782 LOG_DETAILS="testing if PID limiting works (1/14)"
783 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
784 LOG_DETAILS="testing if PID limiting works (2/14)"
785 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
786 LOG_DETAILS="testing if PID limiting works (3/14)"
787 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
788 LOG_DETAILS="testing if PID limiting works (4/14)"
789 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
791 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
793 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
794 dlogctl --pid $DLOGSEND_PID -s 25
795 kill -s CONT $DLOGSEND_PID
797 LOG_DETAILS="testing if PID limiting works (5/14)"
798 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
799 LOG_DETAILS="testing if PID limiting works (6/14)"
800 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
801 LOG_DETAILS="testing if PID limiting works (7/14)"
802 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
803 LOG_DETAILS="testing if PID limiting works (8/14)"
804 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
806 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
808 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
809 dlogctl --pid $DLOGSEND_PID -s allow
810 kill -s CONT $DLOGSEND_PID
812 LOG_DETAILS="testing if PID limiting works (9/14)"
813 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
814 LOG_DETAILS="testing if PID limiting works (10/14)"
815 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 0 ] && ok || fail
816 LOG_DETAILS="testing if PID limiting works (11/14)"
817 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
818 LOG_DETAILS="testing if PID limiting works (12/14)"
819 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
821 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
822 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
824 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
825 dlogctl --pid $DLOGSEND_PID -s 2
826 kill -s CONT $DLOGSEND_PID
828 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
829 LOG_DETAILS="testing if PID limiting works (13/14)"
830 # In each 5 second period, 5 logs are sent, so there are 3 periods.
831 # 2 logs from each period are allowed.
832 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
833 LOG_DETAILS="testing if PID limiting works (14/14)"
834 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 3 ] && ok || fail
836 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
837 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
838 kill $LOGGER > /dev/null
840 if [ "$type" = "logger" ]; then
847 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
848 LOG_DETAILS="testing if QoS works (1/4)"
849 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
850 kill $LOGGER > /dev/null
852 if [ "$type" = "logger" ]; then
859 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
860 LOG_DETAILS="testing if QoS works (2/4)"
861 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
862 kill $LOGGER > /dev/null
864 if [ "$type" = "logger" ]; then
871 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
873 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
875 LOG_DETAILS="testing if QoS works (3/4)"
876 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
877 LOG_DETAILS="testing if QoS works (4/4)"
878 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
880 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
881 kill $LOGGER > /dev/null
887 # limiter test (the feature allows to enable/disable particular log type)
888 dlogsend -c 11 -d 1 -t QWE blabla &
891 dlogctl -t QWE -s deny
892 LOG_DETAILS="testing if log limiter works (deny) (1/3)"
893 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
895 LOG_DETAILS="testing if log limiter works (deny) (2/3)"
896 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
898 dlogctl -t QWE -s allow
900 LOG_DETAILS="testing if log limiter works (allow) (3/3)"
901 [ "$(dlogutil -d QWE | wc -l)" -eq 5 ] && ok || fail
907 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
910 LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
911 [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
913 LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
914 [ "$(dlogutil -db main -v tag)" = "I/FOO : Hi" ] && ok || fail
917 # No need to check output in this case, as nothing is written to stdout anyway
918 dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
920 LOG_DETAILS="testing if basic stderr redirection works correctly"
921 [ "$(dlogutil -db main -v tag)" = "E/BAR : Hi" ] && ok || fail
924 LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
925 [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
927 LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
928 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
931 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
932 dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
934 LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
935 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
936 LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
937 [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
940 dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
942 LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
943 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
944 LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
945 [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
949 echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
951 LOG_DETAILS="testing if redirector passes stdin correctly"
952 [ "$(dlogutil -db main -v tag)" = "I/BAZ : Testing" ] && ok || fail
955 dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
963 LOG_DETAILS="testing if multiline logs are handled correctly"
964 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
967 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
969 LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
970 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
973 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
975 LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
976 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
979 rm -f "$TESTDIR"/input
980 rm -f "$TESTDIR"/output
981 base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
982 dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
984 dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
985 LOG_DETAILS="testing if long lines are handled correctly"
986 # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
987 [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
990 if [ "$type" = "pipe" ]; then
991 # TODO: Actually implement and enable this in the logger backend
992 dlogctl -b main --disable-stdout
993 LOG_DETAILS="testing if redirection can be disabled (1/3)"
994 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
996 LOG_DETAILS="testing if redirection can be disabled (2/3)"
997 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
998 LOG_DETAILS="testing if redirection can be disabled (3/3)"
999 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1001 dlogctl -b main --enable-stdout
1002 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1003 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1005 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1006 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1007 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1008 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1012 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1013 dlogctl -b radio --disable-stdout
1014 dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1016 LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1017 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1021 LOG_DETAILS="testing if redirection check works (1/5)"
1022 test_libredirect 0 0 && ok || fail
1023 LOG_DETAILS="testing if redirection check works (2/5)"
1024 dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1025 LOG_DETAILS="testing if redirection check works (3/5)"
1026 dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1027 LOG_DETAILS="testing if redirection check works (4/5)"
1028 dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1029 LOG_DETAILS="testing if redirection check works (5/5)"
1030 dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1032 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1033 LOG_DETAILS="testing if multiple redirections work (1/4)"
1034 test_libredirect_multi && ok || fail
1035 LOG_DETAILS="testing if multiple redirections work (2/4)"
1036 [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1 : Message 1" ] && ok || fail
1037 LOG_DETAILS="testing if multiple redirections work (3/4)"
1038 [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2 : Message 2" ] && ok || fail
1039 LOG_DETAILS="testing if multiple redirections work (4/4)"
1040 [ "$(dlogutil -db system -v tag)" = "I/TAG3 : Message 3" ] && ok || fail
1041 dlogutil -cb main -b system
1045 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
1047 echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1048 echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1050 dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1051 LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1052 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1053 LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1054 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1056 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1057 LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1058 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1059 LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1060 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1062 echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1064 dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1065 LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1066 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1067 LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1068 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1070 dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1071 LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1072 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1073 LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1074 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1075 LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1076 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1078 echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1080 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1081 LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1082 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1083 LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1084 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1085 LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1086 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1088 rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1091 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1092 sleep 1 # make sure the earlier logs are handled so that they don't end up handled after the clear
1093 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1095 if [ "$(date +%Y)" -le 2000 ]; then
1096 # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1097 date -s "@2121212121" > /dev/null
1099 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1100 LOG_DETAILS="testing if the timestamps are correct"
1101 test_verifytime && ok || fail
1103 DLOG_PREV_PATH=$DLOG_CONFIG_PATH
1105 dlog_test_stdout_redirect $type
1107 DLOG_CONFIG_PATH=$DLOG_PREV_PATH
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 kill $LOGGER > /dev/null
1114 dlog_test_backend_autodetection $type
1115 dlog_test_logger_buffering_logging $type
1122 LOG_DETAILS="testing if the library works with multithreaded app"
1123 dlogutil -f $TESTDIR/dlog_mt_test &
1125 test_libdlog && ok || fail
1134 # Spawn logging scripts and wait for them to finish.
1135 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1136 # wait for all the jobs of the subshell, which means all the logging scripts.
1137 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1138 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1139 # group, sent about at the same time.
1141 for i in $(seq 1 $SPAWN_CNT); do
1142 delay=$(shuf -i 1-5 -n 1)
1143 ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1148 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1150 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1152 if [ "$type" = "pipe" ]; then
1153 for P in recv sent; do
1154 for Q in mono real; do
1155 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1156 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1159 for P in recv sent; do
1160 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1161 dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1162 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1163 dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\",\"${P}_real_ns\":([^,}]*).*\$/\\1 \2/" | sort -c -k1,1 -k2,2g && ok || fail
1167 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1169 for format in $sort_formats; do
1170 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1172 # collect data and analyze timestamps
1173 dlogutil -b main -d -v "$format" | (
1177 while read -r line; do
1178 # filter out empty lines in "long" format
1179 [ -z "$line" ] && continue
1180 # filter out non-timestamp lines in "long" format
1181 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1185 ts=$(extract_timestamp "$format" "$line")
1187 if [ "$ts" -ge "$prev_ts" ]; then
1190 printf "%s\n%s\n\n" "$prev_line" "$line" || break
1194 ) | grep -q ^ && fail || ok
1199 if [ "$type" = "pipe" ]; then
1200 kill $LOGGER > /dev/null
1203 PRERESIZE_CONFIG_PATH="$DLOG_CONFIG_PATH"
1204 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.compress"
1206 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
1207 mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
1213 dlogsend -b main -c 9999 resizetestblalbl &
1215 LOG_DETAILS="testing if compression-resize works (1/3)"
1216 [ "$(dlogutil -d -m xyz123 | wc -l)" -ge 1 ] && ok || fail
1218 dlogctl --compression-name xyz123 --compression-resize 1
1220 LOG_DETAILS="testing if compression-resize works (2/3)"
1221 [ "$(dlogutil -d -m xyz123 | wc -l)" -eq 0 ] && ok || fail
1223 dlogctl --compression-name xyz123 --compression-resize 200000
1224 dlogsend -b main -c 9999 resizetestblalbl &
1226 LOG_DETAILS="testing if compression-resize works (3/3)"
1227 [ "$(dlogutil -d -m xyz123 | wc -l)" -ge 1 ] && ok || fail
1230 kill $LOGGER > /dev/null
1231 DLOG_CONFIG_PATH=$PRERESIZE_CONFIG_PATH
1232 rm -rf $RUNTIME_FILTERS_DIR
1235 # show results, clean up and return an exit code
1237 echo "$OKS / $TOTAL tests passed"
1238 echo "$FAILS / $TOTAL tests failed"
1240 if [ "$quick" -eq 1 ]; then
1241 echo "WARNING: quick mode!"
1242 echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1243 echo "Rerun without '--quick' to run all tests."
1244 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1245 echo "WARNING: legacy logger module!"
1246 echo "Some functionality is untested."
1247 echo "Rerun on a device with the out-of-tree logger module to run all tests."
1251 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1252 # This is because it is used to pass the exit code. If another command
1253 # is executed after it, its exit code will be propagated instead.