Add util_default_format configuration option
[platform/core/system/dlog.git] / tests / dlog_test.in
1 #!/bin/sh
2
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
7
8 # stuff for tracking test case counts
9 FAILS=0
10 OKS=0
11 TOTAL=0
12 LOG_DETAILS=
13
14 #relevant pids default vals
15 UTIL_PID=-1
16 DLOGSEND_PID=-1
17 MT_TEST=-1
18 LOGGER=-1
19
20 TEST_DYNAMIC_FILTERS="@DYNAMIC_FILTERS@"
21 TESTDIR=/var/lib/dlog-tests
22
23 extract_timestamp() {
24         ts=0
25         case "$1" in
26                 "threadtime")
27                         time=$(echo "$2" | awk -F '[ +]' '{print $2}')
28                         ts=$(date +%s%N -d "$time")
29                 ;;
30                 "long")
31                         time=$(echo "$2" | awk -F '[ +.]' '{print $3}')
32                         ms=$(echo "$2" | awk -F '[ +.]' '{print $4}')
33                         sec=$(date +%s%N -d "$time")
34                         ns=$((10#$ms * 1000000))
35                         ts=$((sec + ns))
36                 ;;
37                 "rwtime")
38                         time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
39                         ts=$(date +%s%N -d "$time")
40                 ;;
41                 "recv_realtime")
42                         time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
43                         ts=$(date +%s%N -d "$time")
44                 ;;
45                 "time")
46                         time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
47                         ts=$(date +%s%N -d "$time")
48                 ;;
49                 "kerneltime")
50                         ts=$(echo "$2" | awk -F '[ +]' '{print $1}' | sed -e 's/\.//g')
51                 ;;
52         esac
53         echo "$ts"
54 }
55
56 cleanup() {
57         [ "$UTIL_PID" -ne -1 ] && kill "$UTIL_PID" > /dev/null 2>&1
58         [ "$MT_TEST"  -ne -1 ] && kill "$MT_TEST"  > /dev/null 2>&1
59         [ "$LOGGER"   -ne -1 ] && kill "$LOGGER"   > /dev/null 2>&1
60         # Shellcheck doesn't like this because the rm call is bad if $TESTDIR is empty.
61         # However, such cases would be caught by the conditional.
62         # shellcheck disable=SC2115
63         [ -d "$TESTDIR" ] && rm -rf "$TESTDIR"/*
64         [ -d "$RUNTIME_FILTERS_DIR" ] && rm -rf "$RUNTIME_FILTERS_DIR"
65 }
66
67 trap cleanup 0
68
69 check_daemon() {
70         ret=1
71         if [ "$LOGGER" -ne -1 ] && [ -z "$(ps -o pid= -p "$LOGGER")" ]; then
72                 ret=0
73         fi
74         return "$ret"
75 }
76
77 fail() {
78         check_daemon && daemon_status="[logger daemon not running]"
79
80         FAILS=$((FAILS + 1))
81         TOTAL=$((TOTAL + 1))
82         printf "[%02d] FAILED: %s %s\n" "$TOTAL" "$LOG_DETAILS" "$daemon_status"
83         LOG_DETAILS=
84 }
85
86 ok() {
87         check_daemon && daemon_status="[logger daemon not running]"
88
89         OKS=$((OKS + 1))
90         TOTAL=$((TOTAL + 1))
91         printf "[%02d] PASSED: %s %s\n" "$TOTAL" "$LOG_DETAILS" "$daemon_status"
92         LOG_DETAILS=
93 }
94
95 USAGE_MESSAGE="usage: $0 [--verbose] [--quick] pipe|logger"
96
97 OPTS=$(getopt --shell sh --options "" --long verbose,quick --quiet -- "$@")
98 if [ "$?" -eq 1 ]; then
99         echo "$USAGE_MESSAGE"
100         exit 1
101 fi
102 eval set -- "$OPTS"
103
104 verbose=0
105 quick=0
106 while true; do
107         case "$1" in
108                 --verbose)
109                         verbose=1
110                         ;;
111                 --quick)
112                         quick=1
113                         ;;
114                 --)
115                         shift
116                         break
117                         ;;
118                 *)
119                         echo "This should never happen :)"
120                         exit 1
121                         ;;
122         esac
123         shift
124 done
125 if [ "$1" = "pipe" ]; then
126         type="pipe"
127 elif [ "$1" = "logger" ]; then
128         type="logger"
129 # We still accept the legacy {pipe,logger}_quick syntax because there's no reason not to.
130 elif [ "$1" = "pipe_quick" ]; then
131         type="pipe"
132         quick=1
133 elif [ "$1" = "logger_quick" ]; then
134         type="logger"
135         quick=1
136 else
137         echo "$USAGE_MESSAGE"
138         exit 1
139 fi
140
141 # supress stderr messages from subcommands
142 if [ "$verbose" -eq 0 ]; then
143         exec 2> /dev/null
144 fi
145
146 NEEDS_TO_QUIT=0
147 capsh --print | grep Current | grep cap_syslog > /dev/null || { echo "*** ERROR: cap_syslog missing"; NEEDS_TO_QUIT=1; }
148 mount | grep ' / ' | grep rw > /dev/null || { echo "*** ERROR: root not mounted read-write"; NEEDS_TO_QUIT=1; }
149 [ "$NEEDS_TO_QUIT" -eq 0 ] || exit 1
150
151 if [ "$type" = "pipe" ] || [ -f /lib/modules/"$(uname -r)"/extra/logger.ko ]; then
152         LEGACY_LOGGER=0
153 else
154         LEGACY_LOGGER=1
155 fi
156
157 if [ -z "$DLOG_CONFIG_PATH" ]; then
158         ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
159 else
160         ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
161 fi
162
163 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
164 PATH=$PATH:@libexecdir@/libdlog/
165
166 #create dir for runtime filters
167 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
168 mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
169
170 PREEARLYQUIT_CONFIG_PATH="$DLOG_CONFIG_PATH"
171 DLOG_CONFIG_PATH="$TESTDIR/earlyquit.conf"
172 if [ "$quick" -ne 1 ]; then
173         for PERSISTENT in 1 0; do
174         for KMSG in 1 0; do
175         for SYSLOG in 1 0; do
176         for QOS in 1 0; do
177                 # TODO: Does syslog even work? Disabling for now.
178                 [ "$SYSLOG" -eq 1 ] && continue
179
180                 if [ "$QOS" -eq 1 ]; then
181                         SOURCE="@datadir@/dlog-$type.conf.qos"
182                 else
183                         SOURCE="@datadir@/dlog-$type.conf.test"
184                 fi
185
186                 if [ "$PERSISTENT" -eq 1 ]; then
187                         cp "$SOURCE" "$DLOG_CONFIG_PATH"
188                 else
189                         sed 's/^dlog_logger_conf.*$/#\0/' <"$SOURCE" >"$DLOG_CONFIG_PATH"
190                 fi
191
192                 echo "handle_kmsg=$KMSG" >> "$DLOG_CONFIG_PATH"
193                 echo "syslog_force=$SYSLOG" >> "$DLOG_CONFIG_PATH"
194
195                 dlog_logger -t 0 &
196                 LOGGER=$!
197                 sleep 1
198
199                 kill $LOGGER > /dev/null
200                 STOPPED=$?
201                 LOGGER=-1
202
203                 if [ "$PERSISTENT" -eq 0 ] && [ "$KMSG" -eq 0 ] && [ "$SYSLOG" -eq 0 ] && [ "$QOS" -eq 0 ] && [ "$type" = "logger" ]; then
204                         EXPECTED=1
205                 else
206                         EXPECTED=0
207                 fi
208
209                 LOG_DETAILS="testing if daemon stops when not needed (persistent=$PERSISTENT, kmsg=$KMSG, syslog=$SYSLOG, qos=$QOS)"
210                 [ "$STOPPED" -eq "$EXPECTED" ] && ok || fail
211         done; done; done; done
212 fi
213 DLOG_CONFIG_PATH="$PREEARLYQUIT_CONFIG_PATH"
214
215 # Start the daemon
216 dlog_logger -t 0 &
217 LOGGER=$!
218 sleep 1
219
220 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
221         dlogctl -c
222         dlogctl --enable
223         dlogutil -c -b radio -b system -b main
224         LOG_DETAILS="dlogctl --disable (no args)"
225         dlogctl --disable
226         [ "$(dlogctl -g | grep -c '^\* [a-z]*: ENABLED')" -eq 0 ] && ok || fail
227         LOG_DETAILS="testing if filters were applied"
228         dlogsend -b system -t TEST test
229         dlogsend -b main -t TEST test
230         dlogsend -b radio -t TEST test
231         [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
232
233         LOG_DETAILS="dlogctl --enable (no args)"
234         dlogctl --enable
235         [ "$(dlogctl -g | grep -c '^\* [a-z]*: DISABLED')" -eq 0 ] && ok || fail
236         LOG_DETAILS="testing if filters were applied"
237         dlogutil -c -b radio -b system -b main
238         dlogsend -b system -t TEST test
239         dlogsend -b main -t TEST test
240         dlogsend -b radio -t TEST test
241         [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
242
243         LOG_DETAILS="dlogctl --disable (1 arg)"
244         dlogctl --disable -b system
245         [ "$(dlogctl -g | grep    '^\* [a-z]*: DISABLED' | grep -c system)" -eq 1 ] &&
246         [ "$(dlogctl -g | grep -c '^\* [a-z]*: DISABLED'                 )" -eq 1 ] && ok || fail
247         LOG_DETAILS="testing if filters were applied"
248         dlogutil -c -b radio -b system -b main
249         dlogsend -b system -t TEST test
250         dlogsend -b main -t TEST test
251         dlogsend -b radio -t TEST test
252         [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
253
254         LOG_DETAILS="dlogctl --disable (multiple args)"
255         dlogctl --disable -b main -b radio
256         [ "$(dlogctl -g | grep '^\* [a-z]*: DISABLED' | grep -cv system)" -eq 2 ] &&
257         [ "$(dlogctl -g | grep '^\* [a-z]*: DISABLED' | grep -c   radio)" -eq 1 ] &&
258         [ "$(dlogctl -g | grep '^\* [a-z]*: DISABLED' | grep -c    main)" -eq 1 ] && ok || fail
259         LOG_DETAILS="testing if filters were applied"
260         dlogutil -c -b radio -b system -b main
261         dlogsend -b system -t TEST test
262         dlogsend -b main -t TEST test
263         dlogsend -b radio -t TEST test
264         [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
265
266         LOG_DETAILS="dlogctl --enable (multiple args)"
267         dlogctl --enable -b radio -b system
268         [ "$(dlogctl -g | grep -c '^\* [a-z]*: DISABLED'                 )" -eq 1 ] &&
269         [ "$(dlogctl -g | grep    '^\* [a-z]*: DISABLED' | grep -c   main)" -eq 1 ] &&
270         [ "$(dlogctl -g | grep    '^\* [a-z]*: ENABLED'  | grep -c system)" -eq 1 ] &&
271         [ "$(dlogctl -g | grep    '^\* [a-z]*: ENABLED'  | grep -c  radio)" -eq 1 ] && ok || fail
272         LOG_DETAILS="testing if filters were applied"
273         dlogutil -c -b radio -b system -b main
274         dlogsend -b system -t TEST test
275         dlogsend -b main -t TEST test
276         dlogsend -b radio -t TEST test
277         [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
278
279         LOG_DETAILS="dlogctl --enable (1 arg)"
280         dlogctl --enable -b main
281         [ "$(dlogctl -g | grep -c '^\* [a-z]*: DISABLED')" -eq 0 ] && ok || fail
282         LOG_DETAILS="testing if filters were applied"
283         dlogutil -c -b radio -b system -b main
284         dlogsend -b system -t TEST test
285         dlogsend -b main -t TEST test
286         dlogsend -b radio -t TEST test
287         [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
288
289         LOG_DETAILS="dlogctl -s allow"
290         dlogutil -c -b radio -b system -b main
291         dlogctl -c
292         dlogctl -s deny
293         dlogctl -t TEST_TAG -s allow
294         dlogsend -b main -t TEST_TAG test
295         dlogsend -b main -t TEST test
296         [ "$(dlogutil -d -b main | wc -l)" -eq 1 ] && ok || fail
297
298         LOG_DETAILS="dlogctl -s [N]"
299         dlogutil -c -b radio -b system -b main
300         dlogctl -c
301         dlogctl -s deny
302         dlogctl -t TEST_TAG -s 3
303         dlogsend -b main -c 10 -t TEST_TAG test
304         dlogsend -b main -c 10 -t TEST test
305         [ "$(dlogutil -d -b main | wc -l)" -eq 4 ] && ok || fail
306
307         LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
308         dlogctl -s invalid > /dev/null && fail || ok
309
310         LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
311         dlogctl -p X -g > /dev/null && fail || ok
312
313         LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
314         dlogctl -b invalid --disable > /dev/null && fail || ok
315
316         LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
317         dlogctl -s allow -g -t xyz -p E > /dev/null && fail || ok
318
319         LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
320         dlogctl -s allow -c > /dev/null && fail || ok
321
322         LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
323         dlogctl -c -g -t xyz -p E > /dev/null && fail || ok
324
325         # -s out of range
326         LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
327         dlogctl -s -10 > /dev/null && fail || ok
328
329         LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
330         dlogctl -s 999999 > /dev/null && fail || ok
331
332         # -s correctness
333         LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
334         dlogctl -s 100 && ok || fail
335
336         LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
337         [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 1 ] && ok || fail
338
339         # -g correctness
340         LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
341         [ "$(dlogctl -g | grep -F '*:*' | grep -c 100)" -eq 1 ] && ok || fail
342
343         # -c correctness
344         LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
345         dlogctl -c -t '*' -p '*' && ok || fail
346
347         LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
348         [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 0 ] && ok || fail
349         dlogctl -c
350
351         LOG_DETAILS="testing if the whole dynamic config directory is respected"
352         dlogctl -s deny
353         cp $RUNTIME_FILTERS_DIR/05-logctl.conf $RUNTIME_FILTERS_DIR/10-other.conf
354         dlogctl -c
355         dlogutil -c -b radio -b system -b main
356         dlogsend -b system -t TEST test
357         dlogsend -b main -t TEST test
358         dlogsend -b radio -t TEST test
359         [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
360         rm $RUNTIME_FILTERS_DIR/10-other.conf
361 fi
362
363 SEED=$(head -c 6 /dev/urandom | base64)
364 dlogsend -b main -k "$SEED"
365 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
366 while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
367 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
368 LOG_DETAILS="testing if critical logging works"
369 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
370
371 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
372         dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
373         DLOGSEND_PID=$!
374         sleep 1
375         kill -s PIPE $DLOGSEND_PID
376         wait $DLOGSEND_PID
377         LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
378         [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
379 fi
380
381 # put 100 log entries in the "main" buffer
382 dlogutil -c
383 test_libdlog 100
384 sleep 1
385
386
387 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
388 dlogutil -d > /dev/null && ok || fail
389
390 LOG_DETAILS="testing if -t argument is parsed properly"
391 dlogutil -t dummy > /dev/null && fail || ok
392
393 LOG_DETAILS="testing if -u argument is parsed properly"
394 dlogutil -du dummy > /dev/null && fail || ok
395
396 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
397 [ "$(dlogutil -b main -t  20 | wc -l)" -eq  20 ] && ok || fail # less logs than currently in buffer
398
399 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
400 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
401
402 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
403 [ "$(dlogutil -b main -d     | wc -l)" -eq 100 ] && ok || fail # exactly
404
405 LOG_DETAILS="testing if reading from  dummy buffer returns an error as expected"
406 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
407
408 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
409 [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
410
411 LOG_DETAILS="testing if dlogutil -c empties all buffers"
412 dlogutil -cb main && ok || fail
413
414 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
415 test_libdlog 10
416 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
417
418 LOG_DETAILS="testing if filters work (1/3)"
419 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
420
421 LOG_DETAILS="testing if filters work (2/3)"
422 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
423
424 LOG_DETAILS="testing if filters work (3/3)"
425 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
426
427 LOG_DETAILS="testing if exact filters work (1/3)"
428 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
429
430 LOG_DETAILS="testing if exact filters work (2/3)"
431 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
432
433 LOG_DETAILS="testing if exact filters work (3/3)"
434 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
435
436 LOG_DETAILS="testing if adding \"silent\" filter works"
437 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
438
439 LOG_DETAILS="testing if reading buffer size returns proper exit code"
440 dlogutil -gb main > /dev/null && ok || fail
441
442 LOG_DETAILS="testing if writing all entries to single file works (-f)"
443 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
444
445 if [ "$quick" -ne 1 ]; then
446         LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
447         dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
448         UTIL_PID=$!
449         sleep 1
450         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
451         UTIL_PID=-1
452         [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
453
454         LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
455         dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
456         UTIL_PID=$!
457         sleep 1
458         dlogsend -b main "Hi!"
459         sleep 1
460         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
461         UTIL_PID=-1
462         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
463
464         LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
465         dlogutil --monitor -b main -f $TESTDIR/dlog_monitor1_file &
466         UTIL_PID=$!
467         sleep 1
468         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
469         UTIL_PID=-1
470         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
471
472         LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
473         dlogutil --monitor -b main -f $TESTDIR/dlog_monitor2_file &
474         UTIL_PID=$!
475         sleep 1
476         dlogsend -b main "Hi!"
477         sleep 1
478         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
479         UTIL_PID=-1
480         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
481 fi
482
483 test_libdlog 100000 &
484 LIBDLOGUTIL_CORRECT_PID=$!
485 wait $LIBDLOGUTIL_CORRECT_PID
486
487 # NB: rotation only really makes sense in the continuous mode
488 # from a user PoV, but the logic doesn't care about dumping
489 # so we do it to simplify things
490 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
491
492 LOG_DETAILS="testing if single file is properly created"
493 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
494
495 LOG_DETAILS="testing if rotating file is properly created (1/4)"
496 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
497
498 LOG_DETAILS="testing if rotating file is properly created (2/4)"
499 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
500
501 LOG_DETAILS="testing if rotating file is properly created (3/4)"
502 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
503
504 LOG_DETAILS="testing if rotating file is properly created (4/4)"
505 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
506
507 LOG_DETAILS="testing the size of log files"
508 if [ "$(du "$TESTDIR"/dlog_rotating_file.3 | sed "s#$TESTDIR/dlog_rotating_file.3##g")" -eq 16 ]; then ok; else fail; fi # the actual size is one sector more (so 12 -> 16) because the limit is checked after reaching it, not before
509
510 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
511 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
512 if [ "$quick" -ne 1 ]; then
513         LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
514         test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
515 fi
516 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
517 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
518 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
519 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
520 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
521 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
522 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
523 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
524 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
525 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
526 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
527 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
528 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
529 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
530 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
531 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
532 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
533 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
534 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
535 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
536 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
537 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
538 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
539 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
540 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
541 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
542 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
543 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
544 LOG_DETAILS="testing if libdlogutil aliasing works"
545 if [ "$type" = "pipe" ]; then
546         test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
547 else
548         DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
549 fi
550 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
551 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
552
553 cmd_prefix_no_v="dlogutil -t 1 -u 0"
554 cmd_prefix="$cmd_prefix_no_v -v"
555
556 format="process"
557 regex_prio="[VDIWEFS]{1}"
558 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
559 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
560 regex_timezone="[\+-]{1}[0-9]{4}"
561
562 for PARAM in always auto none never; do
563         for PRIO in info error; do
564                 for OUTPUT in pipe tty; do
565                         if [ "$PARAM" = "always" ]; then
566                                 COLOR_EXPECTED=1
567                         elif [ "$PARAM" = "never" ]; then
568                                 COLOR_EXPECTED=0
569                         elif [ "$OUTPUT" = "tty" ]; then
570                                 COLOR_EXPECTED=1
571                         else
572                                 COLOR_EXPECTED=0
573                         fi
574
575                         if [ "$PRIO" = "info" ]; then
576                                 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
577                         elif [ "$COLOR_EXPECTED" -eq 0 ]; then
578                                 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
579                         else
580                                 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m  \([[:print:]]*\)@~\[0m$/1/g"
581                         fi
582
583                         # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
584                         COMMAND="$cmd_prefix $format"
585
586                         if [ "$PARAM" = "none" ]; then
587                                 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
588                         else
589                                 COMMAND="$COMMAND --color=$PARAM"
590                                 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
591                         fi
592
593                         if [ "$PRIO" = "info" ]; then
594                                 COMMAND="$COMMAND '*:=I'"
595                         else
596                                 COMMAND="$COMMAND '*:=E'"
597                         fi
598
599                         if [ "$OUTPUT" = "tty" ]; then
600                                 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
601                                 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
602                         fi
603
604                         # $(eval) is needed since `$COMMAND` may contain `"`
605                         line="$(eval "$COMMAND" | tr '\033\n' '~@')"
606                         [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
607                 done
608         done
609 done
610 # TODO: It would be cool to also test warning messages (which have a different color),
611 # but this would require deeper changes to this script, which is already janky enough.
612
613 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
614 LOG_DETAILS="testing if \"$format\" print format works"
615 line=$($cmd_prefix $format)
616 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
617
618 format="tag"
619 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
620 LOG_DETAILS="testing if \"$format\" print format works"
621 line=$($cmd_prefix $format)
622 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
623
624 format="thread"
625 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
626 LOG_DETAILS="testing if \"$format\" print format works"
627 line=$($cmd_prefix $format)
628 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
629
630 format="time"
631 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
632 LOG_DETAILS="testing if \"$format\" print format works"
633 line=$($cmd_prefix $format)
634 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
635
636 format="threadtime"
637 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[: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
641
642 format="kerneltime"
643 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\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
647
648 format="recv_realtime"
649 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($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
653
654 format="rwtime"
655 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"
656 LOG_DETAILS="testing if \"$format\" print format works"
657 line=$($cmd_prefix $format)
658 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
659
660 format="long"
661 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
662 LOG_DETAILS="testing if \"$format\" print format works"
663 line=$($cmd_prefix $format)
664 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
665
666 format="brief"
667 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\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
671
672 format="json"
673 if [ "$type" = "pipe" ]; then
674         REGEX="s/^\{\"priority\":\"(verbose|debug|info|warning|error|fatal|silent)\",\"pid\":[1-9][0-9]*,\"tid\":[1-9][0-9]*,\"recv_real\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$regex_timezone\",\"recv_mono\":[1-9][0-9]+\.[0-9]{9},\"sent_real\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$regex_timezone\",\"sent_mono\":[1-9][0-9]+\.[0-9]{9},\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
675 else
676         REGEX="s/^\{\"priority\":\"(verbose|debug|info|warning|error|fatal|silent)\",\"pid\":[1-9][0-9]*,\"tid\":[1-9][0-9]*,\"sent_real\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$regex_timezone\",\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
677 fi
678 LOG_DETAILS="testing if \"$format\" print format works"
679 line=$($cmd_prefix $format)
680 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
681
682 SOURCE="$DLOG_CONFIG_PATH"
683 DLOG_CONFIG_PATH="$TESTDIR/defaultprintformat.conf"
684 cp "$SOURCE" "$DLOG_CONFIG_PATH"
685
686 # TODO: It would be good to also test the daemon side of things.
687 LOG_DETAILS="testing default log format selection (none -> brief)"
688 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
689 line=$($cmd_prefix_no_v)
690 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
691
692 echo "util_default_format=rwtime" >> $DLOG_CONFIG_PATH
693 LOG_DETAILS="testing default log format selection (rwtime)"
694 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"
695 line=$($cmd_prefix_no_v)
696 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
697
698 DLOG_CONFIG_PATH="$SOURCE"
699
700 format="raw"
701 LOG_DETAILS="testing if \"$format\" print format works"
702 dlogutil -c
703 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
704 line=$($cmd_prefix $format)
705 [ "$line" = "rawformatTEST" ] && ok || fail
706
707 if [ "$quick" -ne 1 ]; then
708         LOG_DETAILS="testing if dlogsend -d works"
709         dlogutil -c
710         dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
711         DLOGSEND_PID=$!
712         sleep 10
713         kill $DLOGSEND_PID
714         # In 10 seconds, 3 cycles 7 logs each should have happened.
715         [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
716 fi
717
718 if [ "$type" = "pipe" ]; then
719         for format in raw brief long; do
720                 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
721                         LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
722                         log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
723
724                         dlogutil -c
725                         dlogsend -b main "$log"
726                         line=$($cmd_prefix $format | tr '\n' '@')
727
728                         case $format in
729                                 raw)
730                                         REGEX="s/$log@/1/g"
731                                         ;;
732                                 brief)
733                                         REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
734                                         ;;
735                                 long)
736                                         REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
737                                         ;;
738                         esac
739
740                         [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
741                 done
742         done
743 fi
744
745 LOG_DETAILS="testing if KMSG works in the default format"
746 # We check if the command returns at least 100 logs. This seems to be a safe value.
747 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
748 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
749
750 if [ "$quick" -ne 1 ]; then
751         LOG_DETAILS="testing if KMSG works in the raw format"
752         last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
753         sleep 1 # To make sure dlog has already parsed the log
754         dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
755
756         LOG_DETAILS="testing if pid filtering works"
757         dlogsend -b main -t DLOG_TESTSUITE pidTEST &
758         sleep 1
759         line=$(dlogutil -v raw -d --pid $!)
760         [ "$line" = "pidTEST" ] && ok || fail
761
762         LOG_DETAILS="testing if tid filtering works"
763         dlogsend -b main -t DLOG_TESTSUITE tidTEST &
764         sleep 1
765         line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
766         [ "$line" = "tidTEST" ] && ok || fail
767
768         mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
769         LOG_DETAILS="testing if secure logging works (1/2)"
770         grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
771         echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
772         dlogutil -c
773         dlogsend -zb main -t DLOG_TESTSUITE secure &
774         sleep 1
775         dlogsend -b main -t DLOG_TESTSUITE insecure &
776         sleep 1
777         line=$(dlogutil -v raw -d | head -n1)
778         [ "$line" = "secure" ] && ok || fail
779         LOG_DETAILS="testing if secure logging works (2/2)"
780         grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
781         echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
782         dlogutil -c
783         dlogsend -zb main -t DLOG_TESTSUITE secure &
784         sleep 1
785         dlogsend -b main -t DLOG_TESTSUITE insecure &
786         sleep 1
787         line=$(dlogutil -v raw -d | head -n1)
788         [ "$line" = "insecure" ] && ok || fail
789         mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
790 fi
791
792 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
793 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
794 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
795
796 LOG_DETAILS="testing if tag filtering works (1/8)"
797 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
798 LOG_DETAILS="testing if tag filtering works (2/8)"
799 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
800 LOG_DETAILS="testing if tag filtering works (3/8)"
801 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
802 LOG_DETAILS="testing if tag filtering works (4/8)"
803 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
804 LOG_DETAILS="testing if tag filtering works (5/8)"
805 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
806 LOG_DETAILS="testing if tag filtering works (6/8)"
807 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
808 LOG_DETAILS="testing if tag filtering works (7/8)"
809 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
810 LOG_DETAILS="testing if tag filtering works (8/8)"
811 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
812
813 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
814         LOG_DETAILS="testing if limiter and runtime filtering works"
815         dlogutil -c -b radio
816         test_filters
817         [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
818         dlogutil -c -b radio
819
820         LOG_DETAILS="testing proper SMACK label for dynamic config file location"
821         ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
822         # Running ls on an arbitrary directory and trying to make sense of results is problematic.
823         # However, this is not a problem in our usecase, as we only care about the first field, the security context.
824         # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
825         # shellcheck disable=SC2012
826         [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
827
828         if [ "$quick" -ne 1 ]; then
829                 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
830                 # PID-based limits and see what happens.
831                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
832                 DLOGSEND_PID=$!
833                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
834                 dlogctl --pid $DLOGSEND_PID -s deny
835                 kill -s CONT $DLOGSEND_PID
836                 wait $DLOGSEND_PID
837                 LOG_DETAILS="testing if PID limiting works (1/14)"
838                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
839                 LOG_DETAILS="testing if PID limiting works (2/14)"
840                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 1 ] && ok || fail
841                 LOG_DETAILS="testing if PID limiting works (3/14)"
842                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
843                 LOG_DETAILS="testing if PID limiting works (4/14)"
844                 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
845
846                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
847                 DLOGSEND_PID=$!
848                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
849                 dlogctl --pid $DLOGSEND_PID -s 25
850                 kill -s CONT $DLOGSEND_PID
851                 wait $DLOGSEND_PID
852                 LOG_DETAILS="testing if PID limiting works (5/14)"
853                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
854                 LOG_DETAILS="testing if PID limiting works (6/14)"
855                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 1 ] && ok || fail
856                 LOG_DETAILS="testing if PID limiting works (7/14)"
857                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
858                 LOG_DETAILS="testing if PID limiting works (8/14)"
859                 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
860
861                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
862                 DLOGSEND_PID=$!
863                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
864                 dlogctl --pid $DLOGSEND_PID -s allow
865                 kill -s CONT $DLOGSEND_PID
866                 wait $DLOGSEND_PID
867                 LOG_DETAILS="testing if PID limiting works (9/14)"
868                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
869                 LOG_DETAILS="testing if PID limiting works (10/14)"
870                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 0 ] && ok || fail
871                 LOG_DETAILS="testing if PID limiting works (11/14)"
872                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
873                 LOG_DETAILS="testing if PID limiting works (12/14)"
874                 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
875
876                 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
877                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
878                 DLOGSEND_PID=$!
879                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
880                 dlogctl --pid $DLOGSEND_PID -s 2
881                 kill -s CONT $DLOGSEND_PID
882                 wait $DLOGSEND_PID
883                 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
884                 LOG_DETAILS="testing if PID limiting works (13/14)"
885                 # In each 5 second period, 5 logs are sent, so there are 3 periods.
886                 # 2 logs from each period are allowed.
887                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
888                 LOG_DETAILS="testing if PID limiting works (14/14)"
889                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 3 ] && ok || fail
890
891                 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
892                 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
893                 kill $LOGGER > /dev/null
894                 sleep 1
895                 if [ "$type" = "logger" ]; then
896                         dlogutil -cb main
897                 fi
898                 dlog_logger -t 0 &
899                 LOGGER=$!
900                 sleep 1
901
902                 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
903                 LOG_DETAILS="testing if QoS works (1/4)"
904                 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
905                 kill $LOGGER > /dev/null
906                 sleep 1
907                 if [ "$type" = "logger" ]; then
908                         dlogutil -cb main
909                 fi
910                 dlog_logger -t 0 &
911                 LOGGER=$!
912                 sleep 1
913
914                 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
915                 LOG_DETAILS="testing if QoS works (2/4)"
916                 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
917                 kill $LOGGER > /dev/null
918                 sleep 1
919                 if [ "$type" = "logger" ]; then
920                         dlogutil -cb main
921                 fi
922                 dlog_logger -t 0 &
923                 LOGGER=$!
924                 sleep 1
925
926                 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
927                 DLOGSEND_PID=$!
928                 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
929                 wait $DLOGSEND_PID
930                 LOG_DETAILS="testing if QoS works (3/4)"
931                 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
932                 LOG_DETAILS="testing if QoS works (4/4)"
933                 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
934
935                 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
936                 kill $LOGGER > /dev/null
937                 sleep 1
938                 dlog_logger -t 0 &
939                 LOGGER=$!
940                 sleep 1
941         fi
942 fi
943
944 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
945         dlogutil -cb main
946
947         LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
948         [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
949         sleep 1
950         LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
951         [ "$(dlogutil -db main -v tag)" = "I/FOO     : Hi" ] && ok || fail
952         dlogutil -cb main
953
954         # No need to check output in this case, as nothing is written to stdout anyway
955         dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
956         sleep 1
957         LOG_DETAILS="testing if basic stderr redirection works correctly"
958         [ "$(dlogutil -db main -v tag)" = "E/BAR     : Hi" ] && ok || fail
959         dlogutil -cb main
960
961         LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
962         [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
963         sleep 1
964         LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
965         [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
966         dlogutil -cb main
967
968         dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
969         sleep 1
970         LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
971         [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
972         LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
973         [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
974         dlogutil -cb radio
975
976         dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
977         sleep 1
978         LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
979         [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
980         LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
981         [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
982         dlogutil -cb radio
983
984         echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
985         sleep 1
986         LOG_DETAILS="testing if redirector passes stdin correctly"
987         [ "$(dlogutil -db main -v tag)" = "I/BAZ     : Testing" ] && ok || fail
988         dlogutil -cb main
989
990         dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
991 line 1
992 line 2
993 line 3
994 line 4
995 line 5
996 EOF
997         sleep 1
998         LOG_DETAILS="testing if multiline logs are handled correctly"
999         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1000         dlogutil -cb main
1001
1002         dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
1003         sleep 1
1004         LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
1005         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1006         dlogutil -cb main
1007
1008         dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
1009         sleep 1
1010         LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
1011         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1012         dlogutil -cb main
1013
1014         rm -f "$TESTDIR"/input
1015         rm -f "$TESTDIR"/output
1016         base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
1017         dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
1018         sleep 1
1019         dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
1020         LOG_DETAILS="testing if long lines are handled correctly"
1021         # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
1022         [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
1023         dlogutil -cb main
1024
1025         if [ "$type" = "pipe" ]; then
1026                 # TODO: Actually implement and enable this in the logger backend
1027                 dlogctl -b main --disable-stdout
1028                 LOG_DETAILS="testing if redirection can be disabled (1/3)"
1029                 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1030                 sleep 1
1031                 LOG_DETAILS="testing if redirection can be disabled (2/3)"
1032                 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
1033                 LOG_DETAILS="testing if redirection can be disabled (3/3)"
1034                 [ "$(dlogctl --get | grep main)" = "* main: ENABLED (stdout: DISABLED)" ] && ok || fail
1035
1036                 dlogctl -b main --enable-stdout
1037                 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1038                 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1039                 sleep 1
1040                 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1041                 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1042                 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1043                 [ "$(dlogctl --get | grep main)" = "* main: ENABLED (stdout: ENABLED)" ] && ok || fail
1044                 dlogutil -cb main
1045         fi
1046
1047         dlogctl -b radio --disable-stdout
1048         dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1049         sleep 1
1050         LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1051         [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1052         dlogutil -cb main
1053
1054         LOG_DETAILS="testing if redirection check works (1/5)"
1055         test_libredirect 0 0 && ok || fail
1056         LOG_DETAILS="testing if redirection check works (2/5)"
1057         dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1058         LOG_DETAILS="testing if redirection check works (3/5)"
1059         dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1060         LOG_DETAILS="testing if redirection check works (4/5)"
1061         dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1062         LOG_DETAILS="testing if redirection check works (5/5)"
1063         dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1064
1065         LOG_DETAILS="testing if multiple redirections work (1/4)"
1066         test_libredirect_multi && ok || fail
1067         LOG_DETAILS="testing if multiple redirections work (2/4)"
1068         [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1    : Message 1" ] && ok || fail
1069         LOG_DETAILS="testing if multiple redirections work (3/4)"
1070         [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2    : Message 2" ] && ok || fail
1071         LOG_DETAILS="testing if multiple redirections work (4/4)"
1072         [ "$(dlogutil -db system -v tag)" = "I/TAG3    : Message 3" ] && ok || fail
1073         dlogutil -cb main -b system
1074 fi
1075
1076 dlogutil -c
1077 echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1078 echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1079
1080 dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1081 LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1082 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1083 LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1084 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1085
1086 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1087 LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1088 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1089 LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1090 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1091
1092 echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1093
1094 dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1095 LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1096 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1097 LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1098 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1099
1100 dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1101 LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1102 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1103 LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1104 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1105 LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1106 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1107
1108 echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1109
1110 dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1111 LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1112 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1113 LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1114 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1115 LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1116 [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1117
1118 rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1119
1120 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1121 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1122
1123 if [ "$(date +%Y)" -le 2000 ]; then
1124         # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1125         date -s "@2121212121" > /dev/null
1126 fi
1127 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1128 LOG_DETAILS="testing if the timestamps are correct"
1129 test_verifytime && ok || fail
1130
1131 if [ "$quick" -ne 1 ]; then
1132         PREAUTODETECT_CONFIG_PATH=$DLOG_CONFIG_PATH
1133         DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.autodetect"
1134         kill $LOGGER > /dev/null
1135         sleep 1
1136         if [ "$type" = "logger" ]; then
1137                 dlogutil -cb main
1138         fi
1139         dlog_logger -t 0 &
1140         LOGGER=$!
1141         sleep 1
1142
1143         LOG_DETAILS="testing if backend autodetection works properly (1/2)"
1144         dlogsend -b main -t DLOG_AUTODETECT "xd" && ok || fail
1145         sleep 1
1146
1147         DLOG_CONFIG_PATH=$PREAUTODETECT_CONFIG_PATH
1148         LOG_DETAILS="testing if backend autodetection works properly (2/2)"
1149         [ "$(dlogutil -db main -v raw)" = "xd" ] && ok || fail
1150
1151         kill $LOGGER > /dev/null
1152         sleep 1
1153         rm "$TESTDIR"/test*
1154         dlog_logger -t 0 &
1155         LOGGER=$!
1156         sleep 1
1157
1158         dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
1159         sleep 1
1160         LOG_DETAILS="testing if logger daemon persistent logging works (1/3)"
1161         [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1162         LOG_DETAILS="testing if logger daemon persistent logging works (2/3)"
1163         [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test1)" -eq 100 ] && ok || fail
1164         LOG_DETAILS="testing if logger daemon persistent logging works (3/3)"
1165         [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test2)" -eq 0 ] && ok || fail
1166
1167         dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
1168         sleep 1
1169         RESULT=$(du -c "$TESTDIR"/test0* | tail -n 1 | cut -f 1)
1170         LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
1171         [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
1172         LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
1173         [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
1174
1175         dlogutil -cb main
1176         kill $LOGGER > /dev/null
1177         sleep 1
1178         rm "$TESTDIR"/test0*
1179         dlog_logger -t 5 -b 100000 &
1180         LOGGER=$!
1181         sleep 1
1182
1183         dlogsend -b main -t DLOG_LOGGER_TEST2 -c 100 "hi"
1184         sleep 1
1185         LOG_DETAILS="testing if logger daemon buffering works (1/5)"
1186         [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1187
1188         sleep 6
1189         LOG_DETAILS="testing if logger daemon buffering works (2/5)"
1190         [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1191
1192         dlogsend -b main -t DLOG_LOGGER_TEST3 -c 100 "hi"
1193         sleep 1
1194         LOG_DETAILS="testing if logger daemon buffering works (3/5)"
1195         [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1196
1197         dlogutil -cb main
1198         kill $LOGGER > /dev/null
1199         sleep 1
1200         dlog_logger -t 10000 -b 100 &
1201         LOGGER=$!
1202         sleep 1
1203         LOG_DETAILS="testing if logger daemon buffering works (4/5)"
1204         [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1205
1206         dlogsend -b main -t DLOG_LOGGER_TEST4 -c 100 "hi"
1207         sleep 1
1208         LOG_DETAILS="testing if logger daemon buffering works (5/5)"
1209         [ "$(grep -c DLOG_LOGGER_TEST4 "$TESTDIR"/test0)" -gt 90 ] && ok || fail
1210
1211         dlogutil -cb main
1212         kill $LOGGER > /dev/null
1213         sleep 1
1214         dlog_logger -t 0 &
1215         LOGGER=$!
1216         sleep 1
1217
1218         LOG_DETAILS="testing if the library works with multithreaded app"
1219         dlogutil -f $TESTDIR/dlog_mt_test &
1220         MT_TEST=$!
1221         test_libdlog && ok || fail
1222
1223         sleep 1
1224         kill $MT_TEST
1225         MT_TEST=-1
1226
1227         dlogutil -c
1228         SPAWN_CNT=1000
1229
1230         # Spawn logging scripts and wait for them to finish.
1231         # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1232         # wait for all the jobs of the subshell, which means all the logging scripts.
1233         # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1234         # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1235         # group, sent about at the same time.
1236         (
1237                 for i in $(seq 1 $SPAWN_CNT); do
1238                         delay=$(shuf -i 1-5 -n 1)
1239                         ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1240                 done
1241                 wait
1242         )
1243
1244         LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1245
1246         test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1247
1248         if [ "$type" = "pipe" ]; then
1249                 for P in recv sent; do
1250                         for Q in mono real; do
1251                                 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1252                                 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1253                         done
1254                 done
1255                 for P in recv sent; do
1256                         LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1257                         dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1258                         LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1259                         dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
1260                 done
1261         fi
1262
1263         sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1264
1265         for format in $sort_formats; do
1266                 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1267
1268                 # collect data and analyze timestamps
1269                 dlogutil -b main -d -v "$format" | (
1270                         prev_ts=0
1271                         prev_line=""
1272
1273                         while read -r line; do
1274                                 # filter out empty lines in "long" format
1275                                 [ -z "$line" ] && continue
1276                                 # filter out non-timestamp lines in "long" format
1277                                 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1278                                         continue
1279                                 fi
1280
1281                                 ts=$(extract_timestamp "$format" "$line")
1282
1283                                 if [ "$ts" -ge "$prev_ts" ]; then
1284                                         prev_ts=$ts
1285                                 else
1286                                         printf "%s\n%s\n\n" "$prev_line" "$line" || break
1287                                 fi
1288                                 prev_line=$line
1289                         done
1290                 ) | grep -q ^ && fail || ok
1291
1292         done
1293 fi
1294
1295 # show results, clean up and return an exit code
1296
1297 echo "$OKS / $TOTAL tests passed"
1298 echo "$FAILS / $TOTAL tests failed"
1299
1300 if [ "$quick" -eq 1 ]; then
1301         echo "WARNING: quick mode!"
1302         echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1303         echo "Rerun without '--quick' to run all tests."
1304 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1305         echo "WARNING: legacy logger module!"
1306         echo "Some functionality is untested."
1307         echo "Rerun on a device with the out-of-tree logger module to run all tests."
1308 fi
1309
1310 [ "$FAILS" -eq 0 ]
1311 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1312 # This is because it is used to pass the exit code. If another command
1313 # is executed after it, its exit code will be propagated instead.