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, limiter_apply_to_all_buffers=0)"
165 # without 'limiter_apply_to_all_buffers=1' this
166 # only applies to the three platform buffers:
167 # main, radio, and system
168 PREPLOG_CONFIG_PATH="$DLOG_CONFIG_PATH"
169 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.plog"
170 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 3 ] && ok || fail
171 DLOG_CONFIG_PATH="$PREPLOG_CONFIG_PATH"
173 LOG_DETAILS="dlogctl --disable (no args, limiter_apply_to_all_buffers=1)"
175 [ "$(dlogctl -g | grep regular | grep -c ENABLED)" -eq 0 ] && ok || fail
177 LOG_DETAILS="testing if filters were applied"
178 dlogsend -b system -t TEST test
179 dlogsend -b main -t TEST test
180 dlogsend -b radio -t TEST test
181 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
183 LOG_DETAILS="dlogctl --enable (no args)"
185 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
186 LOG_DETAILS="testing if filters were applied"
187 dlogutil -c -b radio -b system -b main
188 dlogsend -b system -t TEST test
189 dlogsend -b main -t TEST test
190 dlogsend -b radio -t TEST test
191 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
193 LOG_DETAILS="dlogctl --disable (1 arg)"
194 dlogctl --disable -b system
195 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c system)" -eq 1 ] &&
196 [ "$(dlogctl -g | grep regular | grep -c DISABLED )" -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 2 ] && ok || fail
204 LOG_DETAILS="dlogctl --disable (multiple args)"
205 dlogctl --disable -b main -b radio
206 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -cv system)" -eq 2 ] &&
207 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c radio)" -eq 1 ] &&
208 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c main)" -eq 1 ] && ok || fail
209 LOG_DETAILS="testing if filters were applied"
210 dlogutil -c -b radio -b system -b main
211 dlogsend -b system -t TEST test
212 dlogsend -b main -t TEST test
213 dlogsend -b radio -t TEST test
214 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
216 LOG_DETAILS="dlogctl --enable (multiple args)"
217 dlogctl --enable -b radio -b system
218 [ "$(dlogctl -g | grep regular | grep -c DISABLED )" -eq 1 ] &&
219 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c main)" -eq 1 ] &&
220 [ "$(dlogctl -g | grep regular | grep ENABLED | grep -c system)" -eq 1 ] &&
221 [ "$(dlogctl -g | grep regular | grep ENABLED | grep -c radio)" -eq 1 ] && ok || fail
222 LOG_DETAILS="testing if filters were applied"
223 dlogutil -c -b radio -b system -b main
224 dlogsend -b system -t TEST test
225 dlogsend -b main -t TEST test
226 dlogsend -b radio -t TEST test
227 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
229 LOG_DETAILS="dlogctl --enable (1 arg)"
230 dlogctl --enable -b main
231 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
232 LOG_DETAILS="testing if filters were applied"
233 dlogutil -c -b radio -b system -b main
234 dlogsend -b system -t TEST test
235 dlogsend -b main -t TEST test
236 dlogsend -b radio -t TEST test
237 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
239 LOG_DETAILS="dlogctl -s allow"
240 dlogutil -c -b radio -b system -b main
243 dlogctl -t TEST_TAG -s allow
244 dlogsend -b main -t TEST_TAG test
245 dlogsend -b main -t TEST test
246 [ "$(dlogutil -d -b main | wc -l)" -eq 1 ] && ok || fail
248 LOG_DETAILS="dlogctl -s [N]"
249 dlogutil -c -b radio -b system -b main
252 dlogctl -t TEST_TAG -s 3
253 dlogsend -b main -c 10 -t TEST_TAG test
254 dlogsend -b main -c 10 -t TEST test
255 [ "$(dlogutil -d -b main | wc -l)" -eq 4 ] && ok || fail
257 LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
258 dlogctl -s invalid > /dev/null && fail || ok
260 LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
261 dlogctl -p X -g > /dev/null && fail || ok
263 LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
264 dlogctl -b invalid --disable > /dev/null && fail || ok
266 LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
267 dlogctl -s allow -g -t xyz -p E > /dev/null && fail || ok
269 LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
270 dlogctl -s allow -c > /dev/null && fail || ok
272 LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
273 dlogctl -c -g -t xyz -p E > /dev/null && fail || ok
276 LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
277 dlogctl -s -10 > /dev/null && fail || ok
279 LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
280 dlogctl -s 999999 > /dev/null && fail || ok
283 LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
284 dlogctl -s 100 && ok || fail
286 LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
287 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 1 ] && ok || fail
290 LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
291 [ "$(dlogctl -g | grep -F '*:*' | grep -c 100)" -eq 1 ] && ok || fail
294 LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
295 dlogctl -c -t '*' -p '*' && ok || fail
297 LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
298 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 0 ] && ok || fail
301 LOG_DETAILS="testing if the whole dynamic config directory is respected"
303 cp $RUNTIME_FILTERS_DIR/05-logctl.conf $RUNTIME_FILTERS_DIR/10-other.conf
305 dlogutil -c -b radio -b system -b main
306 dlogsend -b system -t TEST test
307 dlogsend -b main -t TEST test
308 dlogsend -b radio -t TEST test
309 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
310 rm $RUNTIME_FILTERS_DIR/10-other.conf
314 SEED=$(head -c 6 /dev/urandom | base64)
315 dlogsend -b main -k "$SEED"
316 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
317 while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
318 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
319 LOG_DETAILS="testing if critical logging works"
320 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
322 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
323 dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
326 kill -s PIPE $DLOGSEND_PID
328 LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
329 [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
334 LOG_DETAILS="testing if dlogutil rejects dotnet"
335 dlogutil -db dotnet_api && fail || ok
337 LOG_DETAILS="testing if dlogutil rejects native"
338 dlogutil -db native_api && fail || ok
340 LOG_DETAILS="testing if dotnet redirects to regular apps (1/2)"
341 dlogsend -b dotnet_api "TEST"
343 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
345 LOG_DETAILS="testing if dotnet redirects to regular apps (2/2)"
346 dlogsend -c 6 -b dotnet_api "TEST"
348 [ "$(dlogutil -d -b apps | wc -l)" -eq 7 ] && ok || fail
350 LOG_DETAILS="testing if native redirects to regular apps (1/2)"
351 dlogsend -b native_api "TEST"
353 [ "$(dlogutil -d -b apps | wc -l)" -eq 8 ] && ok || fail
355 LOG_DETAILS="testing if native redirects to regular apps (2/2)"
356 dlogsend -c 6 -b native_api "TEST"
358 [ "$(dlogutil -d -b apps | wc -l)" -eq 14 ] && ok || fail
361 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (1/4)"
362 dlogctl -b apps --enable
363 dlogctl -b dotnet_api --disable
364 dlogsend -b dotnet_api "TEST"
365 [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
366 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (2/4)"
367 dlogsend -b apps "TEST"
368 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
369 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (3/4)"
370 dlogctl -b apps --disable
371 dlogctl -b dotnet_api --enable
372 dlogsend -b dotnet_api "TEST"
373 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
374 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (4/4)"
375 dlogsend -b apps "TEST"
376 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
380 LOG_DETAILS="testing if blocking native and regular apps is separate (1/4)"
381 dlogctl -b apps --enable
382 dlogctl -b native_api --disable
383 dlogsend -b native_api "TEST"
384 [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
385 LOG_DETAILS="testing if blocking native and regular apps is separate (2/4)"
386 dlogsend -b apps "TEST"
387 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
388 LOG_DETAILS="testing if blocking native and regular apps is separate (3/4)"
389 dlogctl -b apps --disable
390 dlogctl -b native_api --enable
391 dlogsend -b native_api "TEST"
392 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
393 LOG_DETAILS="testing if blocking native and regular apps is separate (4/4)"
394 dlogsend -b apps "TEST"
395 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
397 # put 100 log entries in the "main" buffer
403 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
404 dlogutil -d > /dev/null && ok || fail
406 LOG_DETAILS="testing if -t argument is parsed properly"
407 dlogutil -t dummy > /dev/null && fail || ok
409 LOG_DETAILS="testing if -u argument is parsed properly"
410 dlogutil -du dummy > /dev/null && fail || ok
412 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
413 [ "$(dlogutil -b main -t 20 | wc -l)" -eq 20 ] && ok || fail # less logs than currently in buffer
415 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
416 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
418 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
419 [ "$(dlogutil -b main -d | wc -l)" -eq 100 ] && ok || fail # exactly
421 LOG_DETAILS="testing if reading from dummy buffer returns an error as expected"
422 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
424 if [ "$type" != "zero-copy" ]; then # No buffer split in zero-copy
425 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
426 [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
429 LOG_DETAILS="testing if dlogutil -c empties all buffers"
430 dlogutil -cb main && ok || fail
432 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
434 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
436 LOG_DETAILS="testing if filters work (1/3)"
437 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
439 LOG_DETAILS="testing if filters work (2/3)"
440 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
442 LOG_DETAILS="testing if filters work (3/3)"
443 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
445 LOG_DETAILS="testing if exact filters work (1/3)"
446 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
448 LOG_DETAILS="testing if exact filters work (2/3)"
449 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
451 LOG_DETAILS="testing if exact filters work (3/3)"
452 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
454 LOG_DETAILS="testing if adding \"silent\" filter works"
455 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
457 if [ "$type" != "zero-copy" ]; then # No buffer size access in zero-copy
458 LOG_DETAILS="testing if reading buffer size returns proper exit code"
459 dlogutil -gb main > /dev/null && ok || fail
462 LOG_DETAILS="testing if writing all entries to single file works (-f)"
463 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
465 if [ "$quick" -ne 1 ]; then
466 LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
467 dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
470 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
472 [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
474 LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
475 dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
478 dlogsend -b main "Hi!"
480 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
482 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
484 LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
485 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor1_file &
488 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
490 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
492 LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
493 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor2_file &
496 dlogsend -b main "Hi!"
498 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
500 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
504 test_libdlog 100000 &
505 LIBDLOGUTIL_CORRECT_PID=$!
506 wait $LIBDLOGUTIL_CORRECT_PID
508 # NB: rotation only really makes sense in the continuous mode
509 # from a user PoV, but the logic doesn't care about dumping
510 # so we do it to simplify things
511 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
513 LOG_DETAILS="testing if single file is properly created"
514 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
516 LOG_DETAILS="testing if rotating file is properly created (1/4)"
517 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
519 LOG_DETAILS="testing if rotating file is properly created (2/4)"
520 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
522 LOG_DETAILS="testing if rotating file is properly created (3/4)"
523 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
525 LOG_DETAILS="testing if rotating file is properly created (4/4)"
526 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
528 LOG_DETAILS="testing the size of log files"
529 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
531 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
532 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
533 if [ "$quick" -ne 1 ]; then
534 LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
535 test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
537 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
538 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
539 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
540 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
541 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
542 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
543 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
544 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
545 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
546 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
547 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
548 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
549 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
550 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
551 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
552 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
553 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
554 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
555 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
556 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
557 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
558 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
559 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
560 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
561 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
562 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
563 if [ "$type" != "zero-copy" ]; then # These rely on things not available in zero-copy
564 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
565 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
566 LOG_DETAILS="testing if libdlogutil aliasing works"
567 if [ "$type" = "pipe" ]; then
568 test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
570 DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
573 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
574 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
576 cmd_prefix_no_v="dlogutil -t 1 -u 0"
577 cmd_prefix="$cmd_prefix_no_v -v"
580 regex_prio="[VDIWEFS]{1}"
581 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
582 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
583 regex_timezone="[\+-]{1}[0-9]{4}"
585 for PARAM in always auto none never; do
586 for PRIO in info error; do
587 for OUTPUT in pipe tty; do
588 if [ "$PARAM" = "always" ]; then
590 elif [ "$PARAM" = "never" ]; then
592 elif [ "$OUTPUT" = "tty" ]; then
598 if [ "$PRIO" = "info" ]; then
599 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
600 elif [ "$COLOR_EXPECTED" -eq 0 ]; then
601 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
603 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m \([[:print:]]*\)@~\[0m$/1/g"
606 # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
607 COMMAND="$cmd_prefix $format"
609 if [ "$PARAM" = "none" ]; then
610 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
612 COMMAND="$COMMAND --color=$PARAM"
613 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
616 if [ "$PRIO" = "info" ]; then
617 COMMAND="$COMMAND '*:=I'"
619 COMMAND="$COMMAND '*:=E'"
622 if [ "$OUTPUT" = "tty" ]; then
623 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
624 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
627 # $(eval) is needed since `$COMMAND` may contain `"`
628 line="$(eval "$COMMAND" | tr '\033\n' '~@')"
629 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
633 # TODO: It would be cool to also test warning messages (which have a different color),
634 # but this would require deeper changes to this script, which is already janky enough.
636 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
637 LOG_DETAILS="testing if \"$format\" print format works"
638 line=$($cmd_prefix $format)
639 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
642 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
643 LOG_DETAILS="testing if \"$format\" print format works"
644 line=$($cmd_prefix $format)
645 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
648 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
649 LOG_DETAILS="testing if \"$format\" print format works"
650 line=$($cmd_prefix $format)
651 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
654 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
655 LOG_DETAILS="testing if \"$format\" print format works"
656 line=$($cmd_prefix $format)
657 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
660 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
661 LOG_DETAILS="testing if \"$format\" print format works"
662 line=$($cmd_prefix $format)
663 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
666 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
667 LOG_DETAILS="testing if \"$format\" print format works"
668 line=$($cmd_prefix $format)
669 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
671 format="recv_realtime"
672 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
673 LOG_DETAILS="testing if \"$format\" print format works"
674 line=$($cmd_prefix $format)
675 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
678 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"
679 LOG_DETAILS="testing if \"$format\" print format works"
680 line=$($cmd_prefix $format)
681 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
684 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
685 LOG_DETAILS="testing if \"$format\" print format works"
686 line=$($cmd_prefix $format)
687 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
690 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
691 LOG_DETAILS="testing if \"$format\" print format works"
692 line=$($cmd_prefix $format)
693 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
696 if [ "$type" = "pipe" ]; then
697 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"
698 elif [ "$type" = "logger" ]; then
699 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"
701 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"
703 LOG_DETAILS="testing if \"$format\" print format works"
704 line=$($cmd_prefix $format)
705 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
707 SOURCE="$DLOG_CONFIG_PATH"
708 DLOG_CONFIG_PATH="$TESTDIR/defaultprintformat.conf"
709 cp "$SOURCE" "$DLOG_CONFIG_PATH"
711 # TODO: It would be good to also test the daemon side of things.
712 LOG_DETAILS="testing default log format selection (none -> brief)"
713 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
714 line=$($cmd_prefix_no_v)
715 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
717 echo "util_default_format=rwtime" >> $DLOG_CONFIG_PATH
718 LOG_DETAILS="testing default log format selection (rwtime)"
719 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"
720 line=$($cmd_prefix_no_v)
721 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
723 DLOG_CONFIG_PATH="$SOURCE"
726 LOG_DETAILS="testing if \"$format\" print format works"
728 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
729 line=$($cmd_prefix $format)
730 [ "$line" = "rawformatTEST" ] && ok || fail
732 if [ "$quick" -ne 1 ]; then
733 LOG_DETAILS="testing if dlogsend -d works"
735 dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
739 # In 10 seconds, 3 cycles 7 logs each should have happened.
740 [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
743 if [ "$type" = "pipe" ]; then
744 for format in raw brief long; do
745 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
746 LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
747 log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
750 dlogsend -b main "$log"
751 line=$($cmd_prefix $format | tr '\n' '@')
758 REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
761 REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
765 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
770 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
771 LOG_DETAILS="testing if KMSG works in the default format"
772 # We check if the command returns at least 100 logs. This seems to be a safe value.
773 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
774 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
777 if [ "$quick" -ne 1 ]; then
778 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
779 LOG_DETAILS="testing if KMSG works in the raw format"
780 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
781 sleep 1 # To make sure dlog has already parsed the log
782 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
785 LOG_DETAILS="testing if pid filtering works"
786 dlogsend -b main -t DLOG_TESTSUITE pidTEST &
788 line=$(dlogutil -v raw -d --pid $!)
789 [ "$line" = "pidTEST" ] && ok || fail
791 LOG_DETAILS="testing if tid filtering works"
792 dlogsend -b main -t DLOG_TESTSUITE tidTEST &
794 line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
795 [ "$line" = "tidTEST" ] && ok || fail
797 mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
798 LOG_DETAILS="testing if secure logging works (1/2)"
799 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
800 echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
802 dlogsend -zb main -t DLOG_TESTSUITE secure &
804 dlogsend -b main -t DLOG_TESTSUITE insecure &
806 line=$(dlogutil -v raw -d | head -n1)
807 [ "$line" = "secure" ] && ok || fail
808 LOG_DETAILS="testing if secure logging works (2/2)"
809 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
810 echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
812 dlogsend -zb main -t DLOG_TESTSUITE secure &
814 dlogsend -b main -t DLOG_TESTSUITE insecure &
816 line=$(dlogutil -v raw -d | head -n1)
817 [ "$line" = "insecure" ] && ok || fail
818 mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
821 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
822 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
823 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
825 LOG_DETAILS="testing if tag filtering works (1/8)"
826 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
827 LOG_DETAILS="testing if tag filtering works (2/8)"
828 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
829 LOG_DETAILS="testing if tag filtering works (3/8)"
830 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
831 LOG_DETAILS="testing if tag filtering works (4/8)"
832 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
833 LOG_DETAILS="testing if tag filtering works (5/8)"
834 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
835 LOG_DETAILS="testing if tag filtering works (6/8)"
836 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
837 LOG_DETAILS="testing if tag filtering works (7/8)"
838 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
839 LOG_DETAILS="testing if tag filtering works (8/8)"
840 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
842 if [ "$TEST_DYNAMIC_FILTERS" = "true" ] && [ "$type" != "zero-copy" ]; then # No dynamic things in zero-copy
843 LOG_DETAILS="testing if limiter and runtime filtering works"
846 [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
849 LOG_DETAILS="testing proper SMACK label for dynamic config file location"
850 ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
851 # Running ls on an arbitrary directory and trying to make sense of results is problematic.
852 # However, this is not a problem in our usecase, as we only care about the first field, the security context.
853 # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
854 # shellcheck disable=SC2012
855 [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
857 if [ "$quick" -ne 1 ]; then
858 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
859 # PID-based limits and see what happens.
860 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
862 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
863 dlogctl --pid $DLOGSEND_PID -s deny
864 kill -s CONT $DLOGSEND_PID
866 LOG_DETAILS="testing if PID limiting works (1/14)"
867 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
868 LOG_DETAILS="testing if PID limiting works (2/14)"
869 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
870 LOG_DETAILS="testing if PID limiting works (3/14)"
871 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
872 LOG_DETAILS="testing if PID limiting works (4/14)"
873 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
875 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
877 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
878 dlogctl --pid $DLOGSEND_PID -s 25
879 kill -s CONT $DLOGSEND_PID
881 LOG_DETAILS="testing if PID limiting works (5/14)"
882 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
883 LOG_DETAILS="testing if PID limiting works (6/14)"
884 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
885 LOG_DETAILS="testing if PID limiting works (7/14)"
886 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
887 LOG_DETAILS="testing if PID limiting works (8/14)"
888 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
890 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
892 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
893 dlogctl --pid $DLOGSEND_PID -s allow
894 kill -s CONT $DLOGSEND_PID
896 LOG_DETAILS="testing if PID limiting works (9/14)"
897 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
898 LOG_DETAILS="testing if PID limiting works (10/14)"
899 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 0 ] && ok || fail
900 LOG_DETAILS="testing if PID limiting works (11/14)"
901 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
902 LOG_DETAILS="testing if PID limiting works (12/14)"
903 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
905 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
906 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
908 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
909 dlogctl --pid $DLOGSEND_PID -s 2
910 kill -s CONT $DLOGSEND_PID
912 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
913 LOG_DETAILS="testing if PID limiting works (13/14)"
914 # In each 5 second period, 5 logs are sent, so there are 3 periods.
915 # 2 logs from each period are allowed.
916 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
917 LOG_DETAILS="testing if PID limiting works (14/14)"
918 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 3 ] && ok || fail
920 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
921 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
922 kill $LOGGER > /dev/null
924 if [ "$type" = "logger" ]; then
931 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
932 LOG_DETAILS="testing if QoS works (1/4)"
933 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
934 kill $LOGGER > /dev/null
936 if [ "$type" = "logger" ]; then
943 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
944 LOG_DETAILS="testing if QoS works (2/4)"
945 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
946 kill $LOGGER > /dev/null
948 if [ "$type" = "logger" ]; then
955 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
957 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
959 LOG_DETAILS="testing if QoS works (3/4)"
960 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
961 LOG_DETAILS="testing if QoS works (4/4)"
962 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
964 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
965 kill $LOGGER > /dev/null
971 # limiter test (the feature allows to enable/disable particular log type)
972 dlogsend -c 11 -d 1 -t QWE blabla &
975 dlogctl -t QWE -s deny
976 LOG_DETAILS="testing if log limiter works (deny) (1/3)"
977 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
979 LOG_DETAILS="testing if log limiter works (deny) (2/3)"
980 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
982 dlogctl -t QWE -s allow
984 LOG_DETAILS="testing if log limiter works (allow) (3/3)"
985 [ "$(dlogutil -d QWE | wc -l)" -eq 5 ] && ok || fail
991 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
994 LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
995 [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
997 LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
998 [ "$(dlogutil -db main -v tag)" = "I/FOO : Hi" ] && ok || fail
1001 # No need to check output in this case, as nothing is written to stdout anyway
1002 dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
1004 LOG_DETAILS="testing if basic stderr redirection works correctly"
1005 [ "$(dlogutil -db main -v tag)" = "E/BAR : Hi" ] && ok || fail
1008 LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
1009 [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
1011 LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
1012 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1015 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1016 dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
1018 LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
1019 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1020 LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
1021 [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
1024 dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
1026 LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
1027 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1028 LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
1029 [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
1033 echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
1035 LOG_DETAILS="testing if redirector passes stdin correctly"
1036 [ "$(dlogutil -db main -v tag)" = "I/BAZ : Testing" ] && ok || fail
1039 dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
1047 LOG_DETAILS="testing if multiline logs are handled correctly"
1048 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1051 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
1053 LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
1054 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1057 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
1059 LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
1060 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1063 rm -f "$TESTDIR"/input
1064 rm -f "$TESTDIR"/output
1065 base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
1066 dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
1068 dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
1069 LOG_DETAILS="testing if long lines are handled correctly"
1070 # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
1071 [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
1074 if [ "$type" = "pipe" ]; then
1075 # TODO: Actually implement and enable this in the logger backend
1076 dlogctl -b main --disable-stdout
1077 LOG_DETAILS="testing if redirection can be disabled (1/3)"
1078 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1080 LOG_DETAILS="testing if redirection can be disabled (2/3)"
1081 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
1082 LOG_DETAILS="testing if redirection can be disabled (3/3)"
1083 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1085 dlogctl -b main --enable-stdout
1086 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1087 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1089 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1090 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1091 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1092 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1096 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1097 dlogctl -b radio --disable-stdout
1098 dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1100 LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1101 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1105 LOG_DETAILS="testing if redirection check works (1/5)"
1106 test_libredirect 0 0 && ok || fail
1107 LOG_DETAILS="testing if redirection check works (2/5)"
1108 dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1109 LOG_DETAILS="testing if redirection check works (3/5)"
1110 dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1111 LOG_DETAILS="testing if redirection check works (4/5)"
1112 dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1113 LOG_DETAILS="testing if redirection check works (5/5)"
1114 dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1116 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1117 LOG_DETAILS="testing if multiple redirections work (1/4)"
1118 test_libredirect_multi && ok || fail
1119 LOG_DETAILS="testing if multiple redirections work (2/4)"
1120 [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1 : Message 1" ] && ok || fail
1121 LOG_DETAILS="testing if multiple redirections work (3/4)"
1122 [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2 : Message 2" ] && ok || fail
1123 LOG_DETAILS="testing if multiple redirections work (4/4)"
1124 [ "$(dlogutil -db system -v tag)" = "I/TAG3 : Message 3" ] && ok || fail
1125 dlogutil -cb main -b system
1129 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
1131 echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1132 echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1134 dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1135 LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1136 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1137 LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1138 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1140 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1141 LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1142 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1143 LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1144 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1146 echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1148 dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1149 LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1150 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1151 LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1152 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1154 dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1155 LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1156 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1157 LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1158 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1159 LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1160 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1162 echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1164 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1165 LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1166 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1167 LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1168 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1169 LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1170 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1172 rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1175 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1176 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1178 if [ "$(date +%Y)" -le 2000 ]; then
1179 # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1180 date -s "@2121212121" > /dev/null
1182 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1183 LOG_DETAILS="testing if the timestamps are correct"
1184 test_verifytime && ok || fail
1186 DLOG_PREV_PATH=$DLOG_CONFIG_PATH
1188 LOG_DETAILS="testing if config has control over redirecting to stdout (1/3)"
1189 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.1"
1191 test_config_redirect 1 1 1 1 && ok || fail
1193 LOG_DETAILS="testing if config has control over redirecting to stdout (2/3)"
1194 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.2"
1195 test_config_redirect 0 0 1 1 && ok || fail
1197 LOG_DETAILS="testing if config has control over redirecting to stdout (3/3)"
1198 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.3"
1199 test_config_redirect 1 1 0 0 && ok || fail
1201 DLOG_CONFIG_PATH=$DLOG_PREV_PATH
1203 if [ "$quick" -ne 1 ]; then
1204 if [ "$type" != "zero-copy" ]; then # Backend autodection and logger daemon stuff don't really work on zero-copy
1205 PREAUTODETECT_CONFIG_PATH=$DLOG_CONFIG_PATH
1206 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.autodetect"
1207 kill $LOGGER > /dev/null
1209 if [ "$type" = "logger" ]; then
1216 LOG_DETAILS="testing if backend autodetection works properly (1/2)"
1217 dlogsend -b main -t DLOG_AUTODETECT "xd" && ok || fail
1220 DLOG_CONFIG_PATH=$PREAUTODETECT_CONFIG_PATH
1221 LOG_DETAILS="testing if backend autodetection works properly (2/2)"
1222 [ "$(dlogutil -db main -v raw)" = "xd" ] && ok || fail
1224 kill $LOGGER > /dev/null
1231 dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
1233 LOG_DETAILS="testing if logger daemon persistent logging works (1/3)"
1234 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1235 LOG_DETAILS="testing if logger daemon persistent logging works (2/3)"
1236 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test1)" -eq 100 ] && ok || fail
1237 LOG_DETAILS="testing if logger daemon persistent logging works (3/3)"
1238 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test2)" -eq 0 ] && ok || fail
1240 dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
1242 RESULT=$(du -c "$TESTDIR"/test0* | tail -n 1 | cut -f 1)
1243 LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
1244 [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
1245 LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
1246 [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
1249 kill $LOGGER > /dev/null
1251 rm "$TESTDIR"/test0*
1252 dlog_logger -t 5 -b 100000 &
1256 dlogsend -b main -t DLOG_LOGGER_TEST2 -c 100 "hi"
1258 LOG_DETAILS="testing if logger daemon buffering works (1/5)"
1259 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1262 LOG_DETAILS="testing if logger daemon buffering works (2/5)"
1263 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1265 dlogsend -b main -t DLOG_LOGGER_TEST3 -c 100 "hi"
1267 LOG_DETAILS="testing if logger daemon buffering works (3/5)"
1268 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1271 kill $LOGGER > /dev/null
1274 LOG_DETAILS="testing if logger daemon buffering works (4/5)"
1275 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1279 dlog_logger -t 10000 -b 100 &
1283 dlogsend -b main -t DLOG_LOGGER_TEST4 -c 100 "hi"
1285 LOG_DETAILS="testing if logger daemon buffering works (5/5)"
1286 [ "$(grep -c DLOG_LOGGER_TEST4 "$TESTDIR"/test0)" -gt 90 ] && ok || fail
1289 kill $LOGGER > /dev/null
1296 LOG_DETAILS="testing if the library works with multithreaded app"
1297 dlogutil -f $TESTDIR/dlog_mt_test &
1299 test_libdlog && ok || fail
1308 # Spawn logging scripts and wait for them to finish.
1309 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1310 # wait for all the jobs of the subshell, which means all the logging scripts.
1311 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1312 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1313 # group, sent about at the same time.
1315 for i in $(seq 1 $SPAWN_CNT); do
1316 delay=$(shuf -i 1-5 -n 1)
1317 ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1322 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1324 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1326 if [ "$type" = "pipe" ]; then
1327 for P in recv sent; do
1328 for Q in mono real; do
1329 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1330 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1333 for P in recv sent; do
1334 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1335 dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1336 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1337 dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
1341 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1343 for format in $sort_formats; do
1344 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1346 # collect data and analyze timestamps
1347 dlogutil -b main -d -v "$format" | (
1351 while read -r line; do
1352 # filter out empty lines in "long" format
1353 [ -z "$line" ] && continue
1354 # filter out non-timestamp lines in "long" format
1355 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1359 ts=$(extract_timestamp "$format" "$line")
1361 if [ "$ts" -ge "$prev_ts" ]; then
1364 printf "%s\n%s\n\n" "$prev_line" "$line" || break
1368 ) | grep -q ^ && fail || ok
1373 # show results, clean up and return an exit code
1375 echo "$OKS / $TOTAL tests passed"
1376 echo "$FAILS / $TOTAL tests failed"
1378 if [ "$quick" -eq 1 ]; then
1379 echo "WARNING: quick mode!"
1380 echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1381 echo "Rerun without '--quick' to run all tests."
1382 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1383 echo "WARNING: legacy logger module!"
1384 echo "Some functionality is untested."
1385 echo "Rerun on a device with the out-of-tree logger module to run all tests."
1389 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1390 # This is because it is used to pass the exit code. If another command
1391 # is executed after it, its exit code will be propagated instead.