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