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