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
15 TEST_DYNAMIC_FILTERS=true
19 [ "$UTIL_PID" -ne -1 ] && kill "$UTIL_PID" > /dev/null 2>&1
20 [ "$MT_TEST" -ne -1 ] && kill "$MT_TEST" > /dev/null 2>&1
21 [ "$LOGGER" -ne -1 ] && kill "$LOGGER" > /dev/null 2>&1
22 # Shellcheck doesn't like this because the rm call is bad if $TESTDIR is empty.
23 # However, such cases would be caught by the conditional.
24 # shellcheck disable=SC2115
25 [ -d "$TESTDIR" ] && rm -rf "$TESTDIR"/*
26 [ -d "$RUNTIME_FILTERS_DIR" ] && rm -rf "$RUNTIME_FILTERS_DIR"
31 USAGE_MESSAGE="usage: $0 [--verbose] [--quick] pipe|logger"
33 OPTS=$(getopt --shell sh --options "" --long verbose,quick --quiet -- "$@")
34 if [ "$?" -eq 1 ]; then
55 echo "This should never happen :)"
61 if [ "$1" = "pipe" ]; then
63 elif [ "$1" = "logger" ]; then
65 elif [ "$1" = "zero-copy" ]; then
67 # We still accept the legacy {pipe,logger}_quick syntax because there's no reason not to.
68 elif [ "$1" = "pipe_quick" ]; then
71 elif [ "$1" = "logger_quick" ]; then
79 # supress stderr messages from subcommands
80 if [ "$verbose" -eq 0 ]; then
85 capsh --print | grep Current | grep cap_syslog > /dev/null || { echo "*** ERROR: cap_syslog missing"; NEEDS_TO_QUIT=1; }
86 mount | grep ' / ' | grep rw > /dev/null || { echo "*** ERROR: root not mounted read-write"; NEEDS_TO_QUIT=1; }
87 [ "$NEEDS_TO_QUIT" -eq 0 ] || exit 1
89 if [ "$type" = "pipe" ] || [ -f /lib/modules/"$(uname -r)"/extra/logger.ko ]; then
95 if [ -z "$DLOG_CONFIG_PATH" ]; then
96 ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
98 ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
101 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
102 PATH=$PATH:@libexecdir@/libdlog/
104 if [ "$type" != "zero-copy" ]; then # These tests are inherently dynamic.
105 #create dir for runtime filters
106 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
107 mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
109 PREEARLYQUIT_CONFIG_PATH="$DLOG_CONFIG_PATH"
110 DLOG_CONFIG_PATH="$TESTDIR/earlyquit.conf"
111 if [ "$quick" -ne 1 ]; then
112 for PERSISTENT in 1 0; do
114 for SYSLOG in 1 0; do
116 # TODO: Does syslog even work? Disabling for now.
117 [ "$SYSLOG" -eq 1 ] && continue
119 if [ "$QOS" -eq 1 ]; then
120 SOURCE="@datadir@/dlog-$type.conf.qos"
122 SOURCE="@datadir@/dlog-$type.conf.test"
125 if [ "$PERSISTENT" -eq 1 ]; then
126 cp "$SOURCE" "$DLOG_CONFIG_PATH"
128 sed 's/^dlog_logger_conf.*$/#\0/' <"$SOURCE" >"$DLOG_CONFIG_PATH"
131 echo "handle_kmsg=$KMSG" >> "$DLOG_CONFIG_PATH"
132 echo "syslog_force=$SYSLOG" >> "$DLOG_CONFIG_PATH"
138 kill $LOGGER > /dev/null
142 if [ "$PERSISTENT" -eq 0 ] && [ "$KMSG" -eq 0 ] && [ "$SYSLOG" -eq 0 ] && [ "$QOS" -eq 0 ] && [ "$type" = "logger" ]; then
148 LOG_DETAILS="testing if daemon stops when not needed (persistent=$PERSISTENT, kmsg=$KMSG, syslog=$SYSLOG, qos=$QOS)"
149 [ "$STOPPED" -eq "$EXPECTED" ] && ok || fail
150 done; done; done; done
152 DLOG_CONFIG_PATH="$PREEARLYQUIT_CONFIG_PATH"
159 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
162 dlogutil -c -b radio -b system -b main
163 LOG_DETAILS="dlogctl --disable (no args, limiter_apply_to_all_buffers=0)"
166 # without 'limiter_apply_to_all_buffers=1' this
167 # only applies to the three platform buffers:
168 # main, radio, and system
169 PREPLOG_CONFIG_PATH="$DLOG_CONFIG_PATH"
170 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.plog"
171 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 3 ] && ok || fail
172 DLOG_CONFIG_PATH="$PREPLOG_CONFIG_PATH"
174 LOG_DETAILS="dlogctl --disable (no args, limiter_apply_to_all_buffers=1)"
176 [ "$(dlogctl -g | grep regular | grep -c ENABLED)" -eq 0 ] && ok || fail
178 LOG_DETAILS="testing if filters were applied"
179 dlogsend -b system -t TEST test
180 dlogsend -b main -t TEST test
181 dlogsend -b radio -t TEST test
182 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
184 LOG_DETAILS="dlogctl --enable (no args)"
186 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
187 LOG_DETAILS="testing if filters were applied"
188 dlogutil -c -b radio -b system -b main
189 dlogsend -b system -t TEST test
190 dlogsend -b main -t TEST test
191 dlogsend -b radio -t TEST test
192 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
194 LOG_DETAILS="dlogctl --disable (1 arg)"
195 dlogctl --disable -b system
196 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c system)" -eq 1 ] &&
197 [ "$(dlogctl -g | grep regular | grep -c DISABLED )" -eq 1 ] && ok || fail
198 LOG_DETAILS="testing if filters were applied"
199 dlogutil -c -b radio -b system -b main
200 dlogsend -b system -t TEST test
201 dlogsend -b main -t TEST test
202 dlogsend -b radio -t TEST test
203 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
205 LOG_DETAILS="dlogctl --disable (multiple args)"
206 dlogctl --disable -b main -b radio
207 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -cv system)" -eq 2 ] &&
208 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c radio)" -eq 1 ] &&
209 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c main)" -eq 1 ] && ok || fail
210 LOG_DETAILS="testing if filters were applied"
211 dlogutil -c -b radio -b system -b main
212 dlogsend -b system -t TEST test
213 dlogsend -b main -t TEST test
214 dlogsend -b radio -t TEST test
215 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
217 LOG_DETAILS="dlogctl --enable (multiple args)"
218 dlogctl --enable -b radio -b system
219 [ "$(dlogctl -g | grep regular | grep -c DISABLED )" -eq 1 ] &&
220 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c main)" -eq 1 ] &&
221 [ "$(dlogctl -g | grep regular | grep ENABLED | grep -c system)" -eq 1 ] &&
222 [ "$(dlogctl -g | grep regular | grep ENABLED | grep -c radio)" -eq 1 ] && ok || fail
223 LOG_DETAILS="testing if filters were applied"
224 dlogutil -c -b radio -b system -b main
225 dlogsend -b system -t TEST test
226 dlogsend -b main -t TEST test
227 dlogsend -b radio -t TEST test
228 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
230 LOG_DETAILS="dlogctl --enable (1 arg)"
231 dlogctl --enable -b main
232 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
233 LOG_DETAILS="testing if filters were applied"
234 dlogutil -c -b radio -b system -b main
235 dlogsend -b system -t TEST test
236 dlogsend -b main -t TEST test
237 dlogsend -b radio -t TEST test
238 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
240 LOG_DETAILS="dlogctl -s allow"
241 dlogutil -c -b radio -b system -b main
244 dlogctl -t TEST_TAG -s allow
245 dlogsend -b main -t TEST_TAG test
246 dlogsend -b main -t TEST test
247 [ "$(dlogutil -d -b main | wc -l)" -eq 1 ] && ok || fail
249 LOG_DETAILS="dlogctl -s [N]"
250 dlogutil -c -b radio -b system -b main
253 dlogctl -t TEST_TAG -s 3
254 dlogsend -b main -c 10 -t TEST_TAG test
255 dlogsend -b main -c 10 -t TEST test
256 [ "$(dlogutil -d -b main | wc -l)" -eq 4 ] && ok || fail
258 LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
259 dlogctl -s invalid > /dev/null && fail || ok
261 LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
262 dlogctl -p X -g > /dev/null && fail || ok
264 LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
265 dlogctl -b invalid --disable > /dev/null && fail || ok
267 LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
268 dlogctl -s allow -g -t xyz -p E > /dev/null && fail || ok
270 LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
271 dlogctl -s allow -c > /dev/null && fail || ok
273 LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
274 dlogctl -c -g -t xyz -p E > /dev/null && fail || ok
277 LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
278 dlogctl -s -10 > /dev/null && fail || ok
280 LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
281 dlogctl -s 999999 > /dev/null && fail || ok
284 LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
285 dlogctl -s 100 && ok || fail
287 LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
288 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 1 ] && ok || fail
291 LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
292 [ "$(dlogctl -g | grep -F '*:*' | grep -c 100)" -eq 1 ] && ok || fail
295 LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
296 dlogctl -c -t '*' -p '*' && ok || fail
298 LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
299 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 0 ] && ok || fail
302 LOG_DETAILS="testing if the whole dynamic config directory is respected"
304 cp $RUNTIME_FILTERS_DIR/05-logctl.conf $RUNTIME_FILTERS_DIR/10-other.conf
306 dlogutil -c -b radio -b system -b main
307 dlogsend -b system -t TEST test
308 dlogsend -b main -t TEST test
309 dlogsend -b radio -t TEST test
310 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
311 rm $RUNTIME_FILTERS_DIR/10-other.conf
315 SEED=$(head -c 6 /dev/urandom | base64)
316 dlogsend -b main -k "$SEED"
317 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
318 while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
319 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
320 LOG_DETAILS="testing if critical logging works"
321 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
323 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
324 dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
327 kill -s PIPE $DLOGSEND_PID
329 LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
330 [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
335 LOG_DETAILS="testing if dlogutil rejects dotnet"
336 dlogutil -db dotnet_api && fail || ok
338 LOG_DETAILS="testing if dlogutil rejects native"
339 dlogutil -db native_api && fail || ok
341 LOG_DETAILS="testing if dotnet redirects to regular apps (1/2)"
342 dlogsend -b dotnet_api "TEST"
344 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
346 LOG_DETAILS="testing if dotnet redirects to regular apps (2/2)"
347 dlogsend -c 6 -b dotnet_api "TEST"
349 [ "$(dlogutil -d -b apps | wc -l)" -eq 7 ] && ok || fail
351 LOG_DETAILS="testing if native redirects to regular apps (1/2)"
352 dlogsend -b native_api "TEST"
354 [ "$(dlogutil -d -b apps | wc -l)" -eq 8 ] && ok || fail
356 LOG_DETAILS="testing if native redirects to regular apps (2/2)"
357 dlogsend -c 6 -b native_api "TEST"
359 [ "$(dlogutil -d -b apps | wc -l)" -eq 14 ] && ok || fail
362 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (1/4)"
363 dlogctl -b apps --enable
364 dlogctl -b dotnet_api --disable
365 dlogsend -b dotnet_api "TEST"
366 [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
367 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (2/4)"
368 dlogsend -b apps "TEST"
369 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
370 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (3/4)"
371 dlogctl -b apps --disable
372 dlogctl -b dotnet_api --enable
373 dlogsend -b dotnet_api "TEST"
374 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
375 LOG_DETAILS="testing if blocking dotnet and regular apps is separate (4/4)"
376 dlogsend -b apps "TEST"
377 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
381 LOG_DETAILS="testing if blocking native and regular apps is separate (1/4)"
382 dlogctl -b apps --enable
383 dlogctl -b native_api --disable
384 dlogsend -b native_api "TEST"
385 [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
386 LOG_DETAILS="testing if blocking native and regular apps is separate (2/4)"
387 dlogsend -b apps "TEST"
388 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
389 LOG_DETAILS="testing if blocking native and regular apps is separate (3/4)"
390 dlogctl -b apps --disable
391 dlogctl -b native_api --enable
392 dlogsend -b native_api "TEST"
393 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
394 LOG_DETAILS="testing if blocking native and regular apps is separate (4/4)"
395 dlogsend -b apps "TEST"
396 [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
398 # put 100 log entries in the "main" buffer
404 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
405 dlogutil -d > /dev/null && ok || fail
407 LOG_DETAILS="testing if -t argument is parsed properly"
408 dlogutil -t dummy > /dev/null && fail || ok
410 LOG_DETAILS="testing if -u argument is parsed properly"
411 dlogutil -du dummy > /dev/null && fail || ok
413 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
414 [ "$(dlogutil -b main -t 20 | wc -l)" -eq 20 ] && ok || fail # less logs than currently in buffer
416 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
417 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
419 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
420 [ "$(dlogutil -b main -d | wc -l)" -eq 100 ] && ok || fail # exactly
422 LOG_DETAILS="testing if reading from dummy buffer returns an error as expected"
423 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
425 if [ "$type" != "zero-copy" ]; then # No buffer split in zero-copy
426 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
427 [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
430 LOG_DETAILS="testing if dlogutil -c empties all buffers"
431 dlogutil -cb main && ok || fail
433 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
435 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
437 LOG_DETAILS="testing if filters work (1/3)"
438 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
440 LOG_DETAILS="testing if filters work (2/3)"
441 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
443 LOG_DETAILS="testing if filters work (3/3)"
444 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
446 LOG_DETAILS="testing if exact filters work (1/3)"
447 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
449 LOG_DETAILS="testing if exact filters work (2/3)"
450 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
452 LOG_DETAILS="testing if exact filters work (3/3)"
453 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
455 LOG_DETAILS="testing if adding \"silent\" filter works"
456 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
458 if [ "$type" != "zero-copy" ]; then # No buffer size access in zero-copy
459 LOG_DETAILS="testing if reading buffer size returns proper exit code"
460 dlogutil -gb main > /dev/null && ok || fail
463 LOG_DETAILS="testing if writing all entries to single file works (-f)"
464 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
466 if [ "$quick" -ne 1 ]; then
467 LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
468 dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
471 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
473 [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
475 LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
476 dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
479 dlogsend -b main "Hi!"
481 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
483 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
485 LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
486 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor1_file &
489 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
491 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
493 LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
494 dlogutil --monitor -b main -f $TESTDIR/dlog_monitor2_file &
497 dlogsend -b main "Hi!"
499 kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
501 [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
505 test_libdlog 100000 &
506 LIBDLOGUTIL_CORRECT_PID=$!
507 wait $LIBDLOGUTIL_CORRECT_PID
509 # NB: rotation only really makes sense in the continuous mode
510 # from a user PoV, but the logic doesn't care about dumping
511 # so we do it to simplify things
512 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
514 LOG_DETAILS="testing if single file is properly created"
515 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
517 LOG_DETAILS="testing if rotating file is properly created (1/4)"
518 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
520 LOG_DETAILS="testing if rotating file is properly created (2/4)"
521 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
523 LOG_DETAILS="testing if rotating file is properly created (3/4)"
524 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
526 LOG_DETAILS="testing if rotating file is properly created (4/4)"
527 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
529 LOG_DETAILS="testing the size of log files"
530 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
532 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
533 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
534 if [ "$quick" -ne 1 ]; then
535 LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
536 test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
538 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
539 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
540 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
541 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
542 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
543 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
544 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
545 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
546 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
547 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
548 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
549 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
550 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
551 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
552 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
553 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
554 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
555 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
556 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
557 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
558 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
559 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
560 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
561 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
562 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
563 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
564 if [ "$type" != "zero-copy" ]; then # These rely on things not available in zero-copy
565 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
566 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
567 LOG_DETAILS="testing if libdlogutil aliasing works"
568 if [ "$type" = "pipe" ]; then
569 test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
571 DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
574 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
575 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
577 cmd_prefix_no_v="dlogutil -t 1 -u 0"
578 cmd_prefix="$cmd_prefix_no_v -v"
581 regex_prio="[VDIWEFS]{1}"
582 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
583 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
584 regex_timezone="[\+-]{1}[0-9]{4}"
586 for PARAM in always auto none never; do
587 for PRIO in info error; do
588 for OUTPUT in pipe tty; do
589 if [ "$PARAM" = "always" ]; then
591 elif [ "$PARAM" = "never" ]; then
593 elif [ "$OUTPUT" = "tty" ]; then
599 if [ "$PRIO" = "info" ]; then
600 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
601 elif [ "$COLOR_EXPECTED" -eq 0 ]; then
602 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
604 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m \([[:print:]]*\)@~\[0m$/1/g"
607 # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
608 COMMAND="$cmd_prefix $format"
610 if [ "$PARAM" = "none" ]; then
611 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
613 COMMAND="$COMMAND --color=$PARAM"
614 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
617 if [ "$PRIO" = "info" ]; then
618 COMMAND="$COMMAND '*:=I'"
620 COMMAND="$COMMAND '*:=E'"
623 if [ "$OUTPUT" = "tty" ]; then
624 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
625 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
628 # $(eval) is needed since `$COMMAND` may contain `"`
629 line="$(eval "$COMMAND" | tr '\033\n' '~@')"
630 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
634 # TODO: It would be cool to also test warning messages (which have a different color),
635 # but this would require deeper changes to this script, which is already janky enough.
637 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
638 LOG_DETAILS="testing if \"$format\" print format works"
639 line=$($cmd_prefix $format)
640 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
643 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
644 LOG_DETAILS="testing if \"$format\" print format works"
645 line=$($cmd_prefix $format)
646 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
649 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
650 LOG_DETAILS="testing if \"$format\" print format works"
651 line=$($cmd_prefix $format)
652 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
655 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
656 LOG_DETAILS="testing if \"$format\" print format works"
657 line=$($cmd_prefix $format)
658 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
661 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
662 LOG_DETAILS="testing if \"$format\" print format works"
663 line=$($cmd_prefix $format)
664 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
667 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
668 LOG_DETAILS="testing if \"$format\" print format works"
669 line=$($cmd_prefix $format)
670 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
672 format="recv_realtime"
673 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
674 LOG_DETAILS="testing if \"$format\" print format works"
675 line=$($cmd_prefix $format)
676 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
679 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"
680 LOG_DETAILS="testing if \"$format\" print format works"
681 line=$($cmd_prefix $format)
682 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
685 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
686 LOG_DETAILS="testing if \"$format\" print format works"
687 line=$($cmd_prefix $format)
688 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
691 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
692 LOG_DETAILS="testing if \"$format\" print format works"
693 line=$($cmd_prefix $format)
694 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
697 if [ "$type" = "pipe" ]; then
698 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"
699 elif [ "$type" = "logger" ]; then
700 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"
702 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"
704 LOG_DETAILS="testing if \"$format\" print format works"
705 line=$($cmd_prefix $format)
706 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
708 SOURCE="$DLOG_CONFIG_PATH"
709 DLOG_CONFIG_PATH="$TESTDIR/defaultprintformat.conf"
710 cp "$SOURCE" "$DLOG_CONFIG_PATH"
712 # TODO: It would be good to also test the daemon side of things.
713 LOG_DETAILS="testing default log format selection (none -> brief)"
714 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
715 line=$($cmd_prefix_no_v)
716 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
718 echo "util_default_format=rwtime" >> $DLOG_CONFIG_PATH
719 LOG_DETAILS="testing default log format selection (rwtime)"
720 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"
721 line=$($cmd_prefix_no_v)
722 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
724 DLOG_CONFIG_PATH="$SOURCE"
727 LOG_DETAILS="testing if \"$format\" print format works"
729 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
730 line=$($cmd_prefix $format)
731 [ "$line" = "rawformatTEST" ] && ok || fail
733 if [ "$quick" -ne 1 ]; then
734 LOG_DETAILS="testing if dlogsend -d works"
736 dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
740 # In 10 seconds, 3 cycles 7 logs each should have happened.
741 [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
744 if [ "$type" = "pipe" ]; then
745 for format in raw brief long; do
746 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
747 LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
748 log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
751 dlogsend -b main "$log"
752 line=$($cmd_prefix $format | tr '\n' '@')
759 REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
762 REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
766 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
771 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
772 LOG_DETAILS="testing if KMSG works in the default format"
773 # We check if the command returns at least 100 logs. This seems to be a safe value.
774 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
775 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
778 if [ "$quick" -ne 1 ]; then
779 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
780 LOG_DETAILS="testing if KMSG works in the raw format"
781 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
782 sleep 1 # To make sure dlog has already parsed the log
783 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
786 LOG_DETAILS="testing if pid filtering works"
787 dlogsend -b main -t DLOG_TESTSUITE pidTEST &
789 line=$(dlogutil -v raw -d --pid $!)
790 [ "$line" = "pidTEST" ] && ok || fail
792 LOG_DETAILS="testing if tid filtering works"
793 dlogsend -b main -t DLOG_TESTSUITE tidTEST &
795 line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
796 [ "$line" = "tidTEST" ] && ok || fail
798 mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
799 LOG_DETAILS="testing if secure logging works (1/2)"
800 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
801 echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
803 dlogsend -zb main -t DLOG_TESTSUITE secure &
805 dlogsend -b main -t DLOG_TESTSUITE insecure &
807 line=$(dlogutil -v raw -d | head -n1)
808 [ "$line" = "secure" ] && ok || fail
809 LOG_DETAILS="testing if secure logging works (2/2)"
810 grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
811 echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
813 dlogsend -zb main -t DLOG_TESTSUITE secure &
815 dlogsend -b main -t DLOG_TESTSUITE insecure &
817 line=$(dlogutil -v raw -d | head -n1)
818 [ "$line" = "insecure" ] && ok || fail
819 mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
822 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
823 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
824 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
826 LOG_DETAILS="testing if tag filtering works (1/8)"
827 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
828 LOG_DETAILS="testing if tag filtering works (2/8)"
829 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
830 LOG_DETAILS="testing if tag filtering works (3/8)"
831 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
832 LOG_DETAILS="testing if tag filtering works (4/8)"
833 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
834 LOG_DETAILS="testing if tag filtering works (5/8)"
835 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
836 LOG_DETAILS="testing if tag filtering works (6/8)"
837 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
838 LOG_DETAILS="testing if tag filtering works (7/8)"
839 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
840 LOG_DETAILS="testing if tag filtering works (8/8)"
841 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
843 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
844 LOG_DETAILS="testing if limiter and runtime filtering works"
847 [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
850 LOG_DETAILS="testing proper SMACK label for dynamic config file location"
851 ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
852 # Running ls on an arbitrary directory and trying to make sense of results is problematic.
853 # However, this is not a problem in our usecase, as we only care about the first field, the security context.
854 # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
855 # shellcheck disable=SC2012
856 [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
858 if [ "$quick" -ne 1 ]; then
859 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
860 # PID-based limits and see what happens.
861 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
863 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
864 dlogctl --pid $DLOGSEND_PID -s deny
865 kill -s CONT $DLOGSEND_PID
867 LOG_DETAILS="testing if PID limiting works (1/14)"
868 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
869 LOG_DETAILS="testing if PID limiting works (2/14)"
870 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
871 LOG_DETAILS="testing if PID limiting works (3/14)"
872 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
873 LOG_DETAILS="testing if PID limiting works (4/14)"
874 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
876 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
878 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
879 dlogctl --pid $DLOGSEND_PID -s 25
880 kill -s CONT $DLOGSEND_PID
882 LOG_DETAILS="testing if PID limiting works (5/14)"
883 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
884 LOG_DETAILS="testing if PID limiting works (6/14)"
885 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 1 ] && ok || fail
886 LOG_DETAILS="testing if PID limiting works (7/14)"
887 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
888 LOG_DETAILS="testing if PID limiting works (8/14)"
889 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
891 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
893 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
894 dlogctl --pid $DLOGSEND_PID -s allow
895 kill -s CONT $DLOGSEND_PID
897 LOG_DETAILS="testing if PID limiting works (9/14)"
898 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
899 LOG_DETAILS="testing if PID limiting works (10/14)"
900 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 0 ] && ok || fail
901 LOG_DETAILS="testing if PID limiting works (11/14)"
902 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
903 LOG_DETAILS="testing if PID limiting works (12/14)"
904 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
906 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
907 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
909 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
910 dlogctl --pid $DLOGSEND_PID -s 2
911 kill -s CONT $DLOGSEND_PID
913 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
914 LOG_DETAILS="testing if PID limiting works (13/14)"
915 # In each 5 second period, 5 logs are sent, so there are 3 periods.
916 # 2 logs from each period are allowed.
917 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
918 LOG_DETAILS="testing if PID limiting works (14/14)"
919 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c blocked)" -eq 3 ] && ok || fail
921 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
922 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
923 kill $LOGGER > /dev/null
925 if [ "$type" = "logger" ]; then
932 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
933 LOG_DETAILS="testing if QoS works (1/4)"
934 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
935 kill $LOGGER > /dev/null
937 if [ "$type" = "logger" ]; then
944 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
945 LOG_DETAILS="testing if QoS works (2/4)"
946 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
947 kill $LOGGER > /dev/null
949 if [ "$type" = "logger" ]; then
956 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
958 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
960 LOG_DETAILS="testing if QoS works (3/4)"
961 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
962 LOG_DETAILS="testing if QoS works (4/4)"
963 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
965 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
966 kill $LOGGER > /dev/null
972 # limiter test (the feature allows to enable/disable particular log type)
973 dlogsend -c 11 -d 1 -t QWE blabla &
976 dlogctl -t QWE -s deny
977 LOG_DETAILS="testing if log limiter works (deny) (1/3)"
978 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
980 LOG_DETAILS="testing if log limiter works (deny) (2/3)"
981 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
983 dlogctl -t QWE -s allow
985 LOG_DETAILS="testing if log limiter works (allow) (3/3)"
986 [ "$(dlogutil -d QWE | wc -l)" -eq 5 ] && ok || fail
992 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
995 LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
996 [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
998 LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
999 [ "$(dlogutil -db main -v tag)" = "I/FOO : Hi" ] && ok || fail
1002 # No need to check output in this case, as nothing is written to stdout anyway
1003 dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
1005 LOG_DETAILS="testing if basic stderr redirection works correctly"
1006 [ "$(dlogutil -db main -v tag)" = "E/BAR : Hi" ] && ok || fail
1009 LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
1010 [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
1012 LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
1013 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1016 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1017 dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
1019 LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
1020 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1021 LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
1022 [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
1025 dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
1027 LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
1028 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1029 LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
1030 [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
1034 echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
1036 LOG_DETAILS="testing if redirector passes stdin correctly"
1037 [ "$(dlogutil -db main -v tag)" = "I/BAZ : Testing" ] && ok || fail
1040 dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
1048 LOG_DETAILS="testing if multiline logs are handled correctly"
1049 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1052 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
1054 LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
1055 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1058 dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
1060 LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
1061 [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1064 rm -f "$TESTDIR"/input
1065 rm -f "$TESTDIR"/output
1066 base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
1067 dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
1069 dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
1070 LOG_DETAILS="testing if long lines are handled correctly"
1071 # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
1072 [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
1075 if [ "$type" = "pipe" ]; then
1076 # TODO: Actually implement and enable this in the logger backend
1077 dlogctl -b main --disable-stdout
1078 LOG_DETAILS="testing if redirection can be disabled (1/3)"
1079 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1081 LOG_DETAILS="testing if redirection can be disabled (2/3)"
1082 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
1083 LOG_DETAILS="testing if redirection can be disabled (3/3)"
1084 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1086 dlogctl -b main --enable-stdout
1087 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1088 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1090 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1091 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1092 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1093 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1097 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1098 dlogctl -b radio --disable-stdout
1099 dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1101 LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1102 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1106 LOG_DETAILS="testing if redirection check works (1/5)"
1107 test_libredirect 0 0 && ok || fail
1108 LOG_DETAILS="testing if redirection check works (2/5)"
1109 dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1110 LOG_DETAILS="testing if redirection check works (3/5)"
1111 dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1112 LOG_DETAILS="testing if redirection check works (4/5)"
1113 dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1114 LOG_DETAILS="testing if redirection check works (5/5)"
1115 dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1117 if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1118 LOG_DETAILS="testing if multiple redirections work (1/4)"
1119 test_libredirect_multi && ok || fail
1120 LOG_DETAILS="testing if multiple redirections work (2/4)"
1121 [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1 : Message 1" ] && ok || fail
1122 LOG_DETAILS="testing if multiple redirections work (3/4)"
1123 [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2 : Message 2" ] && ok || fail
1124 LOG_DETAILS="testing if multiple redirections work (4/4)"
1125 [ "$(dlogutil -db system -v tag)" = "I/TAG3 : Message 3" ] && ok || fail
1126 dlogutil -cb main -b system
1130 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
1132 echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1133 echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1135 dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1136 LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1137 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1138 LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1139 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1141 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1142 LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1143 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1144 LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1145 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1147 echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1149 dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1150 LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1151 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1152 LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1153 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1155 dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1156 LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1157 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1158 LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1159 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1160 LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1161 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1163 echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1165 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1166 LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1167 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1168 LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1169 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1170 LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1171 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1173 rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1176 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1177 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1179 if [ "$(date +%Y)" -le 2000 ]; then
1180 # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1181 date -s "@2121212121" > /dev/null
1183 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1184 LOG_DETAILS="testing if the timestamps are correct"
1185 test_verifytime && ok || fail
1187 DLOG_PREV_PATH=$DLOG_CONFIG_PATH
1189 LOG_DETAILS="testing if config has control over redirecting to stdout (1/3)"
1190 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.1"
1192 test_config_redirect 1 1 1 1 && ok || fail
1194 LOG_DETAILS="testing if config has control over redirecting to stdout (2/3)"
1195 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.2"
1196 test_config_redirect 0 0 1 1 && ok || fail
1198 LOG_DETAILS="testing if config has control over redirecting to stdout (3/3)"
1199 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.3"
1200 test_config_redirect 1 1 0 0 && ok || fail
1202 DLOG_CONFIG_PATH=$DLOG_PREV_PATH
1204 if [ "$quick" -ne 1 ]; then
1205 if [ "$type" != "zero-copy" ]; then # Backend autodection and logger daemon stuff don't really work on zero-copy
1206 PREAUTODETECT_CONFIG_PATH=$DLOG_CONFIG_PATH
1207 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.autodetect"
1208 kill $LOGGER > /dev/null
1210 if [ "$type" = "logger" ]; then
1217 LOG_DETAILS="testing if backend autodetection works properly (1/2)"
1218 dlogsend -b main -t DLOG_AUTODETECT "xd" && ok || fail
1221 DLOG_CONFIG_PATH=$PREAUTODETECT_CONFIG_PATH
1222 LOG_DETAILS="testing if backend autodetection works properly (2/2)"
1223 [ "$(dlogutil -db main -v raw)" = "xd" ] && ok || fail
1225 kill $LOGGER > /dev/null
1232 dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
1234 LOG_DETAILS="testing if logger daemon persistent logging works (1/3)"
1235 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1236 LOG_DETAILS="testing if logger daemon persistent logging works (2/3)"
1237 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test1)" -eq 100 ] && ok || fail
1238 LOG_DETAILS="testing if logger daemon persistent logging works (3/3)"
1239 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test2)" -eq 0 ] && ok || fail
1241 dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
1243 RESULT=$(du -c "$TESTDIR"/test0* | tail -n 1 | cut -f 1)
1244 LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
1245 [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
1246 LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
1247 [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
1250 kill $LOGGER > /dev/null
1252 rm "$TESTDIR"/test0*
1253 dlog_logger -t 5 -b 100000 &
1257 dlogsend -b main -t DLOG_LOGGER_TEST2 -c 100 "hi"
1259 LOG_DETAILS="testing if logger daemon buffering works (1/5)"
1260 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1263 LOG_DETAILS="testing if logger daemon buffering works (2/5)"
1264 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1266 dlogsend -b main -t DLOG_LOGGER_TEST3 -c 100 "hi"
1268 LOG_DETAILS="testing if logger daemon buffering works (3/5)"
1269 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1272 kill $LOGGER > /dev/null
1275 LOG_DETAILS="testing if logger daemon buffering works (4/5)"
1276 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1280 dlog_logger -t 10000 -b 100 &
1284 dlogsend -b main -t DLOG_LOGGER_TEST4 -c 100 "hi"
1286 LOG_DETAILS="testing if logger daemon buffering works (5/5)"
1287 [ "$(grep -c DLOG_LOGGER_TEST4 "$TESTDIR"/test0)" -gt 90 ] && ok || fail
1290 kill $LOGGER > /dev/null
1297 LOG_DETAILS="testing if the library works with multithreaded app"
1298 dlogutil -f $TESTDIR/dlog_mt_test &
1300 test_libdlog && ok || fail
1309 # Spawn logging scripts and wait for them to finish.
1310 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1311 # wait for all the jobs of the subshell, which means all the logging scripts.
1312 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1313 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1314 # group, sent about at the same time.
1316 for i in $(seq 1 $SPAWN_CNT); do
1317 delay=$(shuf -i 1-5 -n 1)
1318 ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1323 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1325 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1327 if [ "$type" = "pipe" ]; then
1328 for P in recv sent; do
1329 for Q in mono real; do
1330 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1331 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1334 for P in recv sent; do
1335 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1336 dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1337 LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1338 dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
1342 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1344 for format in $sort_formats; do
1345 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1347 # collect data and analyze timestamps
1348 dlogutil -b main -d -v "$format" | (
1352 while read -r line; do
1353 # filter out empty lines in "long" format
1354 [ -z "$line" ] && continue
1355 # filter out non-timestamp lines in "long" format
1356 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1360 ts=$(extract_timestamp "$format" "$line")
1362 if [ "$ts" -ge "$prev_ts" ]; then
1365 printf "%s\n%s\n\n" "$prev_line" "$line" || break
1369 ) | grep -q ^ && fail || ok
1374 if [ "$type" = "pipe" ]; then
1375 kill $LOGGER > /dev/null
1378 PRERESIZE_CONFIG_PATH="$DLOG_CONFIG_PATH"
1379 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.compress"
1381 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
1382 mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
1388 dlogsend -b main -c 9999 resizetestblalbl &
1390 LOG_DETAILS="testing if compression-resize works (1/3)"
1391 [ "$(dlogutil -d -m xyz123 | wc -l)" -ge 1 ] && ok || fail
1393 dlogctl --compression-name xyz123 --compression-resize 1
1395 LOG_DETAILS="testing if compression-resize works (2/3)"
1396 [ "$(dlogutil -d -m xyz123 | wc -l)" -eq 0 ] && ok || fail
1398 dlogctl --compression-name xyz123 --compression-resize 200000
1399 dlogsend -b main -c 9999 resizetestblalbl &
1401 LOG_DETAILS="testing if compression-resize works (3/3)"
1402 [ "$(dlogutil -d -m xyz123 | wc -l)" -ge 1 ] && ok || fail
1405 kill $LOGGER > /dev/null
1406 DLOG_CONFIG_PATH=$PRERESIZE_CONFIG_PATH
1407 rm -rf $RUNTIME_FILTERS_DIR
1410 # show results, clean up and return an exit code
1412 echo "$OKS / $TOTAL tests passed"
1413 echo "$FAILS / $TOTAL tests failed"
1415 if [ "$quick" -eq 1 ]; then
1416 echo "WARNING: quick mode!"
1417 echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1418 echo "Rerun without '--quick' to run all tests."
1419 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1420 echo "WARNING: legacy logger module!"
1421 echo "Some functionality is untested."
1422 echo "Rerun on a device with the out-of-tree logger module to run all tests."
1426 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1427 # This is because it is used to pass the exit code. If another command
1428 # is executed after it, its exit code will be propagated instead.