Add a unit test for ptrs_list map
[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|zero-copy"
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 if [ "$type" != "zero-copy" ]; then # These tests are inherently dynamic.
362         dlogutil -c
363         LOG_DETAILS="testing if blocking dotnet and regular apps is separate (1/4)"
364         dlogctl -b apps --enable
365         dlogctl -b dotnet_api --disable
366         dlogsend -b dotnet_api "TEST"
367         [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
368         LOG_DETAILS="testing if blocking dotnet and regular apps is separate (2/4)"
369         dlogsend -b apps "TEST"
370         [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
371         LOG_DETAILS="testing if blocking dotnet and regular apps is separate (3/4)"
372         dlogctl -b apps --disable
373         dlogctl -b dotnet_api --enable
374         dlogsend -b dotnet_api "TEST"
375         [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
376         LOG_DETAILS="testing if blocking dotnet and regular apps is separate (4/4)"
377         dlogsend -b apps "TEST"
378         [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
379
380
381         dlogutil -c
382         LOG_DETAILS="testing if blocking native and regular apps is separate (1/4)"
383         dlogctl -b apps --enable
384         dlogctl -b native_api --disable
385         dlogsend -b native_api "TEST"
386         [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
387         LOG_DETAILS="testing if blocking native and regular apps is separate (2/4)"
388         dlogsend -b apps "TEST"
389         [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
390         LOG_DETAILS="testing if blocking native and regular apps is separate (3/4)"
391         dlogctl -b apps --disable
392         dlogctl -b native_api --enable
393         dlogsend -b native_api "TEST"
394         [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
395         LOG_DETAILS="testing if blocking native and regular apps is separate (4/4)"
396         dlogsend -b apps "TEST"
397         [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
398 fi
399
400 # put 100 log entries in the "main" buffer
401 dlogutil -c
402 test_libdlog 100
403 sleep 1
404
405
406 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
407 dlogutil -d > /dev/null && ok || fail
408
409 LOG_DETAILS="testing if -t argument is parsed properly"
410 dlogutil -t dummy > /dev/null && fail || ok
411
412 LOG_DETAILS="testing if -u argument is parsed properly"
413 dlogutil -du dummy > /dev/null && fail || ok
414
415 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
416 [ "$(dlogutil -b main -t  20 | wc -l)" -eq  20 ] && ok || fail # less logs than currently in buffer
417
418 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
419 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
420
421 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
422 [ "$(dlogutil -b main -d     | wc -l)" -eq 100 ] && ok || fail # exactly
423
424 LOG_DETAILS="testing if reading from  dummy buffer returns an error as expected"
425 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
426
427 if [ "$type" != "zero-copy" ]; then # No buffer split in zero-copy
428         LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
429         [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
430 fi
431
432 LOG_DETAILS="testing if dlogutil -c empties all buffers"
433 dlogutil -cb main && ok || fail
434
435 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
436 test_libdlog 10
437 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
438
439 LOG_DETAILS="testing if filters work (1/3)"
440 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
441
442 LOG_DETAILS="testing if filters work (2/3)"
443 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
444
445 LOG_DETAILS="testing if filters work (3/3)"
446 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
447
448 LOG_DETAILS="testing if exact filters work (1/3)"
449 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
450
451 LOG_DETAILS="testing if exact filters work (2/3)"
452 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
453
454 LOG_DETAILS="testing if exact filters work (3/3)"
455 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
456
457 LOG_DETAILS="testing if adding \"silent\" filter works"
458 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
459
460 if [ "$type" != "zero-copy" ]; then # No buffer size access in zero-copy
461         LOG_DETAILS="testing if reading buffer size returns proper exit code"
462         dlogutil -gb main > /dev/null && ok || fail
463 fi
464
465 LOG_DETAILS="testing if writing all entries to single file works (-f)"
466 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
467
468 if [ "$quick" -ne 1 ]; then
469         LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
470         dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
471         UTIL_PID=$!
472         sleep 1
473         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
474         UTIL_PID=-1
475         [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
476
477         LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
478         dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
479         UTIL_PID=$!
480         sleep 1
481         dlogsend -b main "Hi!"
482         sleep 1
483         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
484         UTIL_PID=-1
485         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
486
487         LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
488         dlogutil --monitor -b main -f $TESTDIR/dlog_monitor1_file &
489         UTIL_PID=$!
490         sleep 1
491         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
492         UTIL_PID=-1
493         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
494
495         LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
496         dlogutil --monitor -b main -f $TESTDIR/dlog_monitor2_file &
497         UTIL_PID=$!
498         sleep 1
499         dlogsend -b main "Hi!"
500         sleep 1
501         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
502         UTIL_PID=-1
503         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
504 fi
505
506 dlogutil -cb main
507 test_libdlog 100000 &
508 LIBDLOGUTIL_CORRECT_PID=$!
509 wait $LIBDLOGUTIL_CORRECT_PID
510
511 # NB: rotation only really makes sense in the continuous mode
512 # from a user PoV, but the logic doesn't care about dumping
513 # so we do it to simplify things
514 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
515
516 LOG_DETAILS="testing if single file is properly created"
517 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
518
519 LOG_DETAILS="testing if rotating file is properly created (1/4)"
520 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
521
522 LOG_DETAILS="testing if rotating file is properly created (2/4)"
523 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
524
525 LOG_DETAILS="testing if rotating file is properly created (3/4)"
526 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
527
528 LOG_DETAILS="testing if rotating file is properly created (4/4)"
529 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
530
531 LOG_DETAILS="testing the size of log files"
532 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
533
534 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
535 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
536 if [ "$quick" -ne 1 ]; then
537         LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
538         test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
539 fi
540 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
541 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
542 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
543 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
544 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
545 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
546 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
547 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
548 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
549 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
550 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
551 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
552 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
553 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
554 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
555 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
556 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
557 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
558 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
559 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
560 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
561 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
562 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
563 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
564 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
565 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
566 if [ "$type" != "zero-copy" ]; then # These rely on things not available in zero-copy
567         LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
568         test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
569         LOG_DETAILS="testing if libdlogutil aliasing works"
570         if [ "$type" = "pipe" ]; then
571                 test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
572         else
573                 DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
574         fi
575 fi
576 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
577 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
578
579 cmd_prefix_no_v="dlogutil -t 1 -u 0"
580 cmd_prefix="$cmd_prefix_no_v -v"
581
582 format="process"
583 regex_prio="[VDIWEFS]{1}"
584 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
585 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
586 regex_timezone="[\+-]{1}[0-9]{4}"
587
588 for PARAM in always auto none never; do
589         for PRIO in info error; do
590                 for OUTPUT in pipe tty; do
591                         if [ "$PARAM" = "always" ]; then
592                                 COLOR_EXPECTED=1
593                         elif [ "$PARAM" = "never" ]; then
594                                 COLOR_EXPECTED=0
595                         elif [ "$OUTPUT" = "tty" ]; then
596                                 COLOR_EXPECTED=1
597                         else
598                                 COLOR_EXPECTED=0
599                         fi
600
601                         if [ "$PRIO" = "info" ]; then
602                                 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
603                         elif [ "$COLOR_EXPECTED" -eq 0 ]; then
604                                 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
605                         else
606                                 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m  \([[:print:]]*\)@~\[0m$/1/g"
607                         fi
608
609                         # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
610                         COMMAND="$cmd_prefix $format"
611
612                         if [ "$PARAM" = "none" ]; then
613                                 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
614                         else
615                                 COMMAND="$COMMAND --color=$PARAM"
616                                 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
617                         fi
618
619                         if [ "$PRIO" = "info" ]; then
620                                 COMMAND="$COMMAND '*:=I'"
621                         else
622                                 COMMAND="$COMMAND '*:=E'"
623                         fi
624
625                         if [ "$OUTPUT" = "tty" ]; then
626                                 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
627                                 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
628                         fi
629
630                         # $(eval) is needed since `$COMMAND` may contain `"`
631                         line="$(eval "$COMMAND" | tr '\033\n' '~@')"
632                         [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
633                 done
634         done
635 done
636 # TODO: It would be cool to also test warning messages (which have a different color),
637 # but this would require deeper changes to this script, which is already janky enough.
638
639 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
640 LOG_DETAILS="testing if \"$format\" print format works"
641 line=$($cmd_prefix $format)
642 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
643
644 format="tag"
645 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
646 LOG_DETAILS="testing if \"$format\" print format works"
647 line=$($cmd_prefix $format)
648 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
649
650 format="thread"
651 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
652 LOG_DETAILS="testing if \"$format\" print format works"
653 line=$($cmd_prefix $format)
654 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
655
656 format="time"
657 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
658 LOG_DETAILS="testing if \"$format\" print format works"
659 line=$($cmd_prefix $format)
660 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
661
662 format="threadtime"
663 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
664 LOG_DETAILS="testing if \"$format\" print format works"
665 line=$($cmd_prefix $format)
666 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
667
668 format="kerneltime"
669 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
670 LOG_DETAILS="testing if \"$format\" print format works"
671 line=$($cmd_prefix $format)
672 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
673
674 format="recv_realtime"
675 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
676 LOG_DETAILS="testing if \"$format\" print format works"
677 line=$($cmd_prefix $format)
678 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
679
680 format="rwtime"
681 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"
682 LOG_DETAILS="testing if \"$format\" print format works"
683 line=$($cmd_prefix $format)
684 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
685
686 format="long"
687 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
688 LOG_DETAILS="testing if \"$format\" print format works"
689 line=$($cmd_prefix $format)
690 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
691
692 format="brief"
693 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
694 LOG_DETAILS="testing if \"$format\" print format works"
695 line=$($cmd_prefix $format)
696 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
697
698 format="json"
699 if [ "$type" = "pipe" ]; then
700         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"
701 elif [ "$type" = "logger" ]; then
702         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"
703 else # zero-copy
704         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"
705 fi
706 LOG_DETAILS="testing if \"$format\" print format works"
707 line=$($cmd_prefix $format)
708 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
709
710 SOURCE="$DLOG_CONFIG_PATH"
711 DLOG_CONFIG_PATH="$TESTDIR/defaultprintformat.conf"
712 cp "$SOURCE" "$DLOG_CONFIG_PATH"
713
714 # TODO: It would be good to also test the daemon side of things.
715 LOG_DETAILS="testing default log format selection (none -> brief)"
716 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
717 line=$($cmd_prefix_no_v)
718 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
719
720 echo "util_default_format=rwtime" >> $DLOG_CONFIG_PATH
721 LOG_DETAILS="testing default log format selection (rwtime)"
722 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"
723 line=$($cmd_prefix_no_v)
724 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
725
726 DLOG_CONFIG_PATH="$SOURCE"
727
728 format="raw"
729 LOG_DETAILS="testing if \"$format\" print format works"
730 dlogutil -c
731 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
732 line=$($cmd_prefix $format)
733 [ "$line" = "rawformatTEST" ] && ok || fail
734
735 if [ "$quick" -ne 1 ]; then
736         LOG_DETAILS="testing if dlogsend -d works"
737         dlogutil -c
738         dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
739         DLOGSEND_PID=$!
740         sleep 10
741         kill $DLOGSEND_PID
742         # In 10 seconds, 3 cycles 7 logs each should have happened.
743         [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
744 fi
745
746 if [ "$type" = "pipe" ]; then
747         for format in raw brief long; do
748                 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
749                         LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
750                         log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
751
752                         dlogutil -c
753                         dlogsend -b main "$log"
754                         line=$($cmd_prefix $format | tr '\n' '@')
755
756                         case $format in
757                                 raw)
758                                         REGEX="s/$log@/1/g"
759                                         ;;
760                                 brief)
761                                         REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
762                                         ;;
763                                 long)
764                                         REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
765                                         ;;
766                         esac
767
768                         [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
769                 done
770         done
771 fi
772
773 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
774         LOG_DETAILS="testing if KMSG works in the default format"
775         # We check if the command returns at least 100 logs. This seems to be a safe value.
776         # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
777         [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
778 fi
779
780 if [ "$quick" -ne 1 ]; then
781         if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
782                 LOG_DETAILS="testing if KMSG works in the raw format"
783                 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
784                 sleep 1 # To make sure dlog has already parsed the log
785                 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
786         fi
787
788         LOG_DETAILS="testing if pid filtering works"
789         dlogsend -b main -t DLOG_TESTSUITE pidTEST &
790         sleep 1
791         line=$(dlogutil -v raw -d --pid $!)
792         [ "$line" = "pidTEST" ] && ok || fail
793
794         LOG_DETAILS="testing if tid filtering works"
795         dlogsend -b main -t DLOG_TESTSUITE tidTEST &
796         sleep 1
797         line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
798         [ "$line" = "tidTEST" ] && ok || fail
799
800         mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
801         LOG_DETAILS="testing if secure logging works (1/2)"
802         grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
803         echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
804         dlogutil -c
805         dlogsend -zb main -t DLOG_TESTSUITE secure &
806         sleep 1
807         dlogsend -b main -t DLOG_TESTSUITE insecure &
808         sleep 1
809         line=$(dlogutil -v raw -d | head -n1)
810         [ "$line" = "secure" ] && ok || fail
811         LOG_DETAILS="testing if secure logging works (2/2)"
812         grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
813         echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
814         dlogutil -c
815         dlogsend -zb main -t DLOG_TESTSUITE secure &
816         sleep 1
817         dlogsend -b main -t DLOG_TESTSUITE insecure &
818         sleep 1
819         line=$(dlogutil -v raw -d | head -n1)
820         [ "$line" = "insecure" ] && ok || fail
821         mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
822 fi
823
824 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
825 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
826 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
827
828 LOG_DETAILS="testing if tag filtering works (1/8)"
829 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
830 LOG_DETAILS="testing if tag filtering works (2/8)"
831 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
832 LOG_DETAILS="testing if tag filtering works (3/8)"
833 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
834 LOG_DETAILS="testing if tag filtering works (4/8)"
835 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
836 LOG_DETAILS="testing if tag filtering works (5/8)"
837 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
838 LOG_DETAILS="testing if tag filtering works (6/8)"
839 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
840 LOG_DETAILS="testing if tag filtering works (7/8)"
841 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
842 LOG_DETAILS="testing if tag filtering works (8/8)"
843 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
844
845 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
846         LOG_DETAILS="testing if limiter and runtime filtering works"
847         dlogutil -c -b radio
848         test_filters
849         [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
850         dlogutil -c -b radio
851
852         LOG_DETAILS="testing proper SMACK label for dynamic config file location"
853         ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
854         # Running ls on an arbitrary directory and trying to make sense of results is problematic.
855         # However, this is not a problem in our usecase, as we only care about the first field, the security context.
856         # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
857         # shellcheck disable=SC2012
858         [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
859
860         if [ "$quick" -ne 1 ]; then
861                 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
862                 # PID-based limits and see what happens.
863                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
864                 DLOGSEND_PID=$!
865                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
866                 dlogctl --pid $DLOGSEND_PID -s deny
867                 kill -s CONT $DLOGSEND_PID
868                 wait $DLOGSEND_PID
869                 LOG_DETAILS="testing if PID limiting works (1/14)"
870                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
871                 LOG_DETAILS="testing if PID limiting works (2/14)"
872                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 1 ] && ok || fail
873                 LOG_DETAILS="testing if PID limiting works (3/14)"
874                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
875                 LOG_DETAILS="testing if PID limiting works (4/14)"
876                 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
877
878                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
879                 DLOGSEND_PID=$!
880                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
881                 dlogctl --pid $DLOGSEND_PID -s 25
882                 kill -s CONT $DLOGSEND_PID
883                 wait $DLOGSEND_PID
884                 LOG_DETAILS="testing if PID limiting works (5/14)"
885                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
886                 LOG_DETAILS="testing if PID limiting works (6/14)"
887                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 1 ] && ok || fail
888                 LOG_DETAILS="testing if PID limiting works (7/14)"
889                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
890                 LOG_DETAILS="testing if PID limiting works (8/14)"
891                 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
892
893                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
894                 DLOGSEND_PID=$!
895                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
896                 dlogctl --pid $DLOGSEND_PID -s allow
897                 kill -s CONT $DLOGSEND_PID
898                 wait $DLOGSEND_PID
899                 LOG_DETAILS="testing if PID limiting works (9/14)"
900                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
901                 LOG_DETAILS="testing if PID limiting works (10/14)"
902                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 0 ] && ok || fail
903                 LOG_DETAILS="testing if PID limiting works (11/14)"
904                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
905                 LOG_DETAILS="testing if PID limiting works (12/14)"
906                 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
907
908                 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
909                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
910                 DLOGSEND_PID=$!
911                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
912                 dlogctl --pid $DLOGSEND_PID -s 2
913                 kill -s CONT $DLOGSEND_PID
914                 wait $DLOGSEND_PID
915                 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
916                 LOG_DETAILS="testing if PID limiting works (13/14)"
917                 # In each 5 second period, 5 logs are sent, so there are 3 periods.
918                 # 2 logs from each period are allowed.
919                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
920                 LOG_DETAILS="testing if PID limiting works (14/14)"
921                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 3 ] && ok || fail
922
923                 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
924                 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
925                 kill $LOGGER > /dev/null
926                 sleep 1
927                 if [ "$type" = "logger" ]; then
928                         dlogutil -cb main
929                 fi
930                 dlog_logger -t 0 &
931                 LOGGER=$!
932                 sleep 1
933
934                 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
935                 LOG_DETAILS="testing if QoS works (1/4)"
936                 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
937                 kill $LOGGER > /dev/null
938                 sleep 1
939                 if [ "$type" = "logger" ]; then
940                         dlogutil -cb main
941                 fi
942                 dlog_logger -t 0 &
943                 LOGGER=$!
944                 sleep 1
945
946                 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
947                 LOG_DETAILS="testing if QoS works (2/4)"
948                 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
949                 kill $LOGGER > /dev/null
950                 sleep 1
951                 if [ "$type" = "logger" ]; then
952                         dlogutil -cb main
953                 fi
954                 dlog_logger -t 0 &
955                 LOGGER=$!
956                 sleep 1
957
958                 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
959                 DLOGSEND_PID=$!
960                 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
961                 wait $DLOGSEND_PID
962                 LOG_DETAILS="testing if QoS works (3/4)"
963                 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
964                 LOG_DETAILS="testing if QoS works (4/4)"
965                 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
966
967                 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
968                 kill $LOGGER > /dev/null
969                 sleep 1
970                 dlog_logger -t 0 &
971                 LOGGER=$!
972                 sleep 1
973
974                 # limiter test (the feature allows to enable/disable particular log type)
975                 dlogsend -c 11 -d 1 -t QWE blabla &
976
977                 sleep 0.5
978                 dlogctl -t QWE -s deny
979                 LOG_DETAILS="testing if log limiter works (deny) (1/3)"
980                 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
981                 sleep 6
982                 LOG_DETAILS="testing if log limiter works (deny) (2/3)"
983                 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
984
985                 dlogctl -t QWE -s allow
986                 sleep 4
987                 LOG_DETAILS="testing if log limiter works (allow) (3/3)"
988                 [ "$(dlogutil -d QWE | wc -l)" -eq 5 ] && ok || fail
989
990                 dlogutil -c
991         fi
992 fi
993
994 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
995         dlogutil -cb main
996
997         LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
998         [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
999         sleep 1
1000         LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
1001         [ "$(dlogutil -db main -v tag)" = "I/FOO     : Hi" ] && ok || fail
1002         dlogutil -cb main
1003
1004         # No need to check output in this case, as nothing is written to stdout anyway
1005         dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
1006         sleep 1
1007         LOG_DETAILS="testing if basic stderr redirection works correctly"
1008         [ "$(dlogutil -db main -v tag)" = "E/BAR     : Hi" ] && ok || fail
1009         dlogutil -cb main
1010
1011         LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
1012         [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
1013         sleep 1
1014         LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
1015         [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1016         dlogutil -cb main
1017
1018         if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1019                 dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
1020                 sleep 1
1021                 LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
1022                 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1023                 LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
1024                 [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
1025                 dlogutil -cb radio
1026
1027                 dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
1028                 sleep 1
1029                 LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
1030                 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
1031                 LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
1032                 [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
1033                 dlogutil -cb radio
1034         fi
1035
1036         echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
1037         sleep 1
1038         LOG_DETAILS="testing if redirector passes stdin correctly"
1039         [ "$(dlogutil -db main -v tag)" = "I/BAZ     : Testing" ] && ok || fail
1040         dlogutil -cb main
1041
1042         dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
1043 line 1
1044 line 2
1045 line 3
1046 line 4
1047 line 5
1048 EOF
1049         sleep 1
1050         LOG_DETAILS="testing if multiline logs are handled correctly"
1051         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1052         dlogutil -cb main
1053
1054         dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
1055         sleep 1
1056         LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
1057         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1058         dlogutil -cb main
1059
1060         dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
1061         sleep 1
1062         LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
1063         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
1064         dlogutil -cb main
1065
1066         rm -f "$TESTDIR"/input
1067         rm -f "$TESTDIR"/output
1068         base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
1069         dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
1070         sleep 1
1071         dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
1072         LOG_DETAILS="testing if long lines are handled correctly"
1073         # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
1074         [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
1075         dlogutil -cb main
1076
1077         if [ "$type" = "pipe" ]; then
1078                 # TODO: Actually implement and enable this in the logger backend
1079                 dlogctl -b main --disable-stdout
1080                 LOG_DETAILS="testing if redirection can be disabled (1/3)"
1081                 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1082                 sleep 1
1083                 LOG_DETAILS="testing if redirection can be disabled (2/3)"
1084                 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
1085                 LOG_DETAILS="testing if redirection can be disabled (3/3)"
1086                 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1087
1088                 dlogctl -b main --enable-stdout
1089                 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1090                 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1091                 sleep 1
1092                 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1093                 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1094                 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1095                 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1096                 dlogutil -cb main
1097         fi
1098
1099         if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1100                 dlogctl -b radio --disable-stdout
1101                 dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1102                 sleep 1
1103                 LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1104                 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1105                 dlogutil -cb main
1106         fi
1107
1108         LOG_DETAILS="testing if redirection check works (1/5)"
1109         test_libredirect 0 0 && ok || fail
1110         LOG_DETAILS="testing if redirection check works (2/5)"
1111         dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1112         LOG_DETAILS="testing if redirection check works (3/5)"
1113         dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1114         LOG_DETAILS="testing if redirection check works (4/5)"
1115         dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1116         LOG_DETAILS="testing if redirection check works (5/5)"
1117         dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1118
1119         if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1120                 LOG_DETAILS="testing if multiple redirections work (1/4)"
1121                 test_libredirect_multi && ok || fail
1122                 LOG_DETAILS="testing if multiple redirections work (2/4)"
1123                 [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1    : Message 1" ] && ok || fail
1124                 LOG_DETAILS="testing if multiple redirections work (3/4)"
1125                 [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2    : Message 2" ] && ok || fail
1126                 LOG_DETAILS="testing if multiple redirections work (4/4)"
1127                 [ "$(dlogutil -db system -v tag)" = "I/TAG3    : Message 3" ] && ok || fail
1128                 dlogutil -cb main -b system
1129         fi
1130 fi
1131
1132 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
1133         dlogutil -c
1134         echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1135         echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1136
1137         dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1138         LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1139         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1140         LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1141         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1142
1143         dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1144         LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1145         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1146         LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1147         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1148
1149         echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1150
1151         dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1152         LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1153         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1154         LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1155         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1156
1157         dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1158         LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1159         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1160         LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1161         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1162         LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1163         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1164
1165         echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1166
1167         dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1168         LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1169         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1170         LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1171         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1172         LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1173         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1174
1175         rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1176 fi
1177
1178 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1179 sleep 1 # make sure the earlier logs are handled so that they don't end up handled after the clear
1180 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1181
1182 if [ "$(date +%Y)" -le 2000 ]; then
1183         # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1184         date -s "@2121212121" > /dev/null
1185 fi
1186 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1187 LOG_DETAILS="testing if the timestamps are correct"
1188 test_verifytime && ok || fail
1189
1190 DLOG_PREV_PATH=$DLOG_CONFIG_PATH
1191
1192 LOG_DETAILS="testing if config has control over redirecting to stdout (1/3)"
1193 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.1"
1194
1195 test_config_redirect 1 1 1 1 && ok || fail
1196
1197 LOG_DETAILS="testing if config has control over redirecting to stdout (2/3)"
1198 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.2"
1199 test_config_redirect 0 0 1 1 && ok || fail
1200
1201 LOG_DETAILS="testing if config has control over redirecting to stdout (3/3)"
1202 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.stdout_enable.3"
1203 test_config_redirect 1 1 0 0 && ok || fail
1204
1205 DLOG_CONFIG_PATH=$DLOG_PREV_PATH
1206
1207 if [ "$quick" -ne 1 ]; then
1208         if [ "$type" != "zero-copy" ]; then # Backend autodection and logger daemon stuff don't really work on zero-copy
1209                 PREAUTODETECT_CONFIG_PATH=$DLOG_CONFIG_PATH
1210                 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.autodetect"
1211                 kill $LOGGER > /dev/null
1212                 sleep 1
1213                 if [ "$type" = "logger" ]; then
1214                         dlogutil -cb main
1215                 fi
1216                 dlog_logger -t 0 &
1217                 LOGGER=$!
1218                 sleep 1
1219
1220                 LOG_DETAILS="testing if backend autodetection works properly (1/2)"
1221                 dlogsend -b main -t DLOG_AUTODETECT "xd" && ok || fail
1222                 sleep 1
1223
1224                 DLOG_CONFIG_PATH=$PREAUTODETECT_CONFIG_PATH
1225                 LOG_DETAILS="testing if backend autodetection works properly (2/2)"
1226                 [ "$(dlogutil -db main -v raw)" = "xd" ] && ok || fail
1227
1228                 kill $LOGGER > /dev/null
1229                 sleep 1
1230                 rm "$TESTDIR"/test*
1231                 dlog_logger -t 0 &
1232                 LOGGER=$!
1233                 sleep 1
1234
1235                 dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
1236                 sleep 1
1237                 LOG_DETAILS="testing if logger daemon persistent logging works (1/3)"
1238                 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1239                 LOG_DETAILS="testing if logger daemon persistent logging works (2/3)"
1240                 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test1)" -eq 100 ] && ok || fail
1241                 LOG_DETAILS="testing if logger daemon persistent logging works (3/3)"
1242                 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/test2)" -eq 0 ] && ok || fail
1243
1244                 dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
1245                 sleep 1
1246                 RESULT=$(du -c "$TESTDIR"/test0* | tail -n 1 | cut -f 1)
1247                 LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
1248                 [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
1249                 LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
1250                 [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
1251
1252                 dlogutil -cb main
1253                 kill $LOGGER > /dev/null
1254                 sleep 1
1255                 rm "$TESTDIR"/test0*
1256                 dlog_logger -t 5 -b 100000 &
1257                 LOGGER=$!
1258                 sleep 1
1259
1260                 dlogsend -b main -t DLOG_LOGGER_TEST2 -c 100 "hi"
1261                 sleep 1
1262                 LOG_DETAILS="testing if logger daemon buffering works (1/5)"
1263                 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1264
1265                 sleep 6
1266                 LOG_DETAILS="testing if logger daemon buffering works (2/5)"
1267                 [ "$(grep -c DLOG_LOGGER_TEST2 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1268
1269                 dlogsend -b main -t DLOG_LOGGER_TEST3 -c 100 "hi"
1270                 sleep 1
1271                 LOG_DETAILS="testing if logger daemon buffering works (3/5)"
1272                 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 0 ] && ok || fail
1273
1274                 sleep 1
1275                 kill $LOGGER > /dev/null
1276                 LOGGER=-1
1277                 sleep 1
1278                 LOG_DETAILS="testing if logger daemon buffering works (4/5)"
1279                 [ "$(grep -c DLOG_LOGGER_TEST3 "$TESTDIR"/test0)" -eq 100 ] && ok || fail
1280
1281                 dlogutil -cb main
1282
1283                 dlog_logger -t 10000 -b 100 &
1284                 LOGGER=$!
1285                 sleep 1
1286
1287                 dlogsend -b main -t DLOG_LOGGER_TEST4 -c 100 "hi"
1288                 sleep 1
1289                 LOG_DETAILS="testing if logger daemon buffering works (5/5)"
1290                 [ "$(grep -c DLOG_LOGGER_TEST4 "$TESTDIR"/test0)" -gt 90 ] && ok || fail
1291
1292                 dlogutil -cb main
1293                 kill $LOGGER > /dev/null
1294                 sleep 1
1295                 dlog_logger -t 0 &
1296                 LOGGER=$!
1297                 sleep 1
1298         fi
1299
1300         LOG_DETAILS="testing if the library works with multithreaded app"
1301         dlogutil -f $TESTDIR/dlog_mt_test &
1302         MT_TEST=$!
1303         test_libdlog && ok || fail
1304
1305         sleep 1
1306         kill $MT_TEST
1307         MT_TEST=-1
1308
1309         dlogutil -c
1310         SPAWN_CNT=1000
1311
1312         # Spawn logging scripts and wait for them to finish.
1313         # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1314         # wait for all the jobs of the subshell, which means all the logging scripts.
1315         # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1316         # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1317         # group, sent about at the same time.
1318         (
1319                 for i in $(seq 1 $SPAWN_CNT); do
1320                         delay=$(shuf -i 1-5 -n 1)
1321                         ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1322                 done
1323                 wait
1324         )
1325
1326         LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1327
1328         test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1329
1330         if [ "$type" = "pipe" ]; then
1331                 for P in recv sent; do
1332                         for Q in mono real; do
1333                                 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1334                                 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1335                         done
1336                 done
1337                 for P in recv sent; do
1338                         LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1339                         dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1340                         LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1341                         dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
1342                 done
1343         fi
1344
1345         sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1346
1347         for format in $sort_formats; do
1348                 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1349
1350                 # collect data and analyze timestamps
1351                 dlogutil -b main -d -v "$format" | (
1352                         prev_ts=0
1353                         prev_line=""
1354
1355                         while read -r line; do
1356                                 # filter out empty lines in "long" format
1357                                 [ -z "$line" ] && continue
1358                                 # filter out non-timestamp lines in "long" format
1359                                 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1360                                         continue
1361                                 fi
1362
1363                                 ts=$(extract_timestamp "$format" "$line")
1364
1365                                 if [ "$ts" -ge "$prev_ts" ]; then
1366                                         prev_ts=$ts
1367                                 else
1368                                         printf "%s\n%s\n\n" "$prev_line" "$line" || break
1369                                 fi
1370                                 prev_line=$line
1371                         done
1372                 ) | grep -q ^ && fail || ok
1373
1374         done
1375 fi
1376
1377 if [ "$type" = "pipe" ]; then
1378         kill $LOGGER > /dev/null
1379         sleep 1
1380
1381         PRERESIZE_CONFIG_PATH="$DLOG_CONFIG_PATH"
1382         DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.compress"
1383
1384         RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
1385         mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
1386
1387         dlog_logger -t 0 &
1388         LOGGER=$!
1389         sleep 1
1390
1391         dlogsend -b main -c 9999 resizetestblalbl &
1392         sleep 4
1393         LOG_DETAILS="testing if compression-resize works (1/3)"
1394         [ "$(dlogutil -d -m xyz123 | wc -l)" -ge 1 ] && ok || fail
1395
1396         dlogctl --compression-name xyz123 --compression-resize 1
1397         sleep 5
1398         LOG_DETAILS="testing if compression-resize works (2/3)"
1399         [ "$(dlogutil -d -m xyz123 | wc -l)" -eq 0 ] && ok || fail
1400
1401         dlogctl --compression-name xyz123 --compression-resize 200000
1402         dlogsend -b main -c 9999 resizetestblalbl &
1403         sleep 4
1404         LOG_DETAILS="testing if compression-resize works (3/3)"
1405         [ "$(dlogutil -d -m xyz123 | wc -l)" -ge 1 ] && ok || fail
1406
1407         dlogutil -c
1408         kill $LOGGER > /dev/null
1409         DLOG_CONFIG_PATH=$PRERESIZE_CONFIG_PATH
1410         rm -rf $RUNTIME_FILTERS_DIR
1411 fi
1412
1413 # show results, clean up and return an exit code
1414
1415 echo "$OKS / $TOTAL tests passed"
1416 echo "$FAILS / $TOTAL tests failed"
1417
1418 if [ "$quick" -eq 1 ]; then
1419         echo "WARNING: quick mode!"
1420         echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1421         echo "Rerun without '--quick' to run all tests."
1422 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1423         echo "WARNING: legacy logger module!"
1424         echo "Some functionality is untested."
1425         echo "Rerun on a device with the out-of-tree logger module to run all tests."
1426 fi
1427
1428 [ "$FAILS" -eq 0 ]
1429 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1430 # This is because it is used to pass the exit code. If another command
1431 # is executed after it, its exit code will be propagated instead.