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