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