Fix QoS blocking too many logs
[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 # stuff for tracking test case counts
9 FAILS=0
10 OKS=0
11 TOTAL=0
12 LOG_DETAILS=
13
14 #relevant pids default vals
15 UTIL_PID=-1
16 DLOGSEND_PID=-1
17 MT_TEST=-1
18 LOGGER=-1
19
20 TEST_DYNAMIC_FILTERS="@DYNAMIC_FILTERS@"
21 TESTDIR=/var/lib/dlog-tests
22
23 extract_timestamp() {
24         ts=0
25         case "$1" in
26                 "threadtime")
27                         time=$(echo "$2" | awk -F '[ +]' '{print $2}')
28                         ts=$(date +%s%N -d "$time")
29                 ;;
30                 "long")
31                         time=$(echo "$2" | awk -F '[ +.]' '{print $3}')
32                         ms=$(echo "$2" | awk -F '[ +.]' '{print $4}')
33                         sec=$(date +%s%N -d "$time")
34                         ns=$((10#$ms * 1000000))
35                         ts=$((sec + ns))
36                 ;;
37                 "rwtime")
38                         time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
39                         ts=$(date +%s%N -d "$time")
40                 ;;
41                 "recv_realtime")
42                         time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
43                         ts=$(date +%s%N -d "$time")
44                 ;;
45                 "time")
46                         time=$(echo "$2" | awk -F '[ +.]' '{print $2}')
47                         ts=$(date +%s%N -d "$time")
48                 ;;
49                 "kerneltime")
50                         ts=$(echo "$2" | awk -F '[ +]' '{print $1}' | sed -e 's/\.//g')
51                 ;;
52         esac
53         echo "$ts"
54 }
55
56 cleanup() {
57         [ "$UTIL_PID" -ne -1 ] && kill "$UTIL_PID" > /dev/null 2>&1
58         [ "$MT_TEST"  -ne -1 ] && kill "$MT_TEST"  > /dev/null 2>&1
59         [ "$LOGGER"   -ne -1 ] && kill "$LOGGER"   > /dev/null 2>&1
60         # Shellcheck doesn't like this because the rm call is bad if $TESTDIR is empty.
61         # However, such cases would be caught by the conditional.
62         # shellcheck disable=SC2115
63         [ -d "$TESTDIR" ] && rm -rf "$TESTDIR"/*
64         [ -d "$RUNTIME_FILTERS_DIR" ] && rm -rf "$RUNTIME_FILTERS_DIR"
65 }
66
67 trap cleanup 0
68
69 check_daemon() {
70         ret=1
71         if [ "$LOGGER" -ne -1 ] && [ -z "$(ps -o pid= -p "$LOGGER")" ]; then
72                 ret=0
73         fi
74         return "$ret"
75 }
76
77 fail() {
78         check_daemon && daemon_status="[logger daemon not running]"
79
80         FAILS=$((FAILS + 1))
81         TOTAL=$((TOTAL + 1))
82         printf "[%02d] FAILED: %s %s\n" "$TOTAL" "$LOG_DETAILS" "$daemon_status"
83         LOG_DETAILS=
84 }
85
86 ok() {
87         check_daemon && daemon_status="[logger daemon not running]"
88
89         OKS=$((OKS + 1))
90         TOTAL=$((TOTAL + 1))
91         printf "[%02d] PASSED: %s %s\n" "$TOTAL" "$LOG_DETAILS" "$daemon_status"
92         LOG_DETAILS=
93 }
94
95 USAGE_MESSAGE="usage: $0 [--verbose] [--quick] pipe|logger"
96
97 OPTS=$(getopt --shell sh --options "" --long verbose,quick --quiet -- "$@")
98 if [ "$?" -eq 1 ]; then
99         echo "$USAGE_MESSAGE"
100         exit 1
101 fi
102 eval set -- "$OPTS"
103
104 verbose=0
105 quick=0
106 while true; do
107         case "$1" in
108                 --verbose)
109                         verbose=1
110                         ;;
111                 --quick)
112                         quick=1
113                         ;;
114                 --)
115                         shift
116                         break
117                         ;;
118                 *)
119                         echo "This should never happen :)"
120                         exit 1
121                         ;;
122         esac
123         shift
124 done
125 if [ "$1" = "pipe" ]; then
126         type="pipe"
127 elif [ "$1" = "logger" ]; then
128         type="logger"
129 # We still accept the legacy {pipe,logger}_quick syntax because there's no reason not to.
130 elif [ "$1" = "pipe_quick" ]; then
131         type="pipe"
132         quick=1
133 elif [ "$1" = "logger_quick" ]; then
134         type="logger"
135         quick=1
136 else
137         echo "$USAGE_MESSAGE"
138         exit 1
139 fi
140
141 # supress stderr messages from subcommands
142 if [ "$verbose" -eq 0 ]; then
143         exec 2> /dev/null
144 fi
145
146 NEEDS_TO_QUIT=0
147 capsh --print | grep Current | grep cap_syslog > /dev/null || { echo "*** ERROR: cap_syslog missing"; NEEDS_TO_QUIT=1; }
148 mount | grep ' / ' | grep rw > /dev/null || { echo "*** ERROR: root not mounted read-write"; NEEDS_TO_QUIT=1; }
149 [ "$NEEDS_TO_QUIT" -eq 0 ] || exit 1
150
151 if [ -z "$DLOG_CONFIG_PATH" ]; then
152         ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
153 else
154         ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
155 fi
156
157 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
158 PATH=$PATH:@libexecdir@/libdlog/
159
160 #create dir for runtime filters
161 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
162 mkdir -p "$RUNTIME_FILTERS_DIR"
163
164 # Start the daemon
165 if [ "$type" = "pipe" ]; then
166         dlog_logger &
167         LOGGER=$!
168         sleep 1
169 fi
170
171 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
172         dlogctl -c
173         dlogctl --enable
174         dlogutil -c -b radio -b system -b main
175         LOG_DETAILS="dlogctl --disable (no args)"
176         dlogctl --disable
177         [ "$(dlogctl -g | grep -c ENABLED)" -eq 0 ] && ok || fail
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 -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    DISABLED | grep -c system)" -eq 1 ] &&
197         [ "$(dlogctl -g | 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 DISABLED | grep -cv system)" -eq 2 ] &&
208         [ "$(dlogctl -g | grep DISABLED | grep -c   radio)" -eq 1 ] &&
209         [ "$(dlogctl -g | 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 -c DISABLED                 )" -eq 1 ] &&
220         [ "$(dlogctl -g | grep    DISABLED | grep -c   main)" -eq 1 ] &&
221         [ "$(dlogctl -g | grep    ENABLED  | grep -c system)" -eq 1 ] &&
222         [ "$(dlogctl -g | 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 -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
314 SEED=$(head -c 6 /dev/urandom | base64)
315 dlogsend -b main -k "$SEED"
316 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
317 while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
318 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
319 LOG_DETAILS="testing if critical logging works"
320 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
321
322 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
323         dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
324         DLOGSEND_PID=$!
325         sleep 1
326         kill -s PIPE $DLOGSEND_PID
327         wait $DLOGSEND_PID
328         LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
329         [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
330 fi
331
332 # put 100 log entries in the "main" buffer
333 dlogutil -c
334 test_libdlog 100
335 sleep 1
336
337
338 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
339 dlogutil -d > /dev/null && ok || fail
340
341 LOG_DETAILS="testing if -t argument is parsed properly"
342 dlogutil -t dummy > /dev/null && fail || ok
343
344 LOG_DETAILS="testing if -u argument is parsed properly"
345 dlogutil -du dummy > /dev/null && fail || ok
346
347 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
348 [ "$(dlogutil -b main -t  20 | wc -l)" -eq  20 ] && ok || fail # less logs than currently in buffer
349
350 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
351 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
352
353 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
354 [ "$(dlogutil -b main -d     | wc -l)" -eq 100 ] && ok || fail # exactly
355
356 LOG_DETAILS="testing if reading from  dummy buffer returns an error as expected"
357 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
358
359 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
360 [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
361
362 LOG_DETAILS="testing if dlogutil -c empties all buffers"
363 dlogutil -cb main && ok || fail
364
365 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
366 test_libdlog 10
367 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
368
369 LOG_DETAILS="testing if filters work (1/3)"
370 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
371
372 LOG_DETAILS="testing if filters work (2/3)"
373 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
374
375 LOG_DETAILS="testing if filters work (3/3)"
376 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
377
378 LOG_DETAILS="testing if exact filters work (1/3)"
379 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
380
381 LOG_DETAILS="testing if exact filters work (2/3)"
382 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
383
384 LOG_DETAILS="testing if exact filters work (3/3)"
385 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
386
387 LOG_DETAILS="testing if adding \"silent\" filter works"
388 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
389
390 LOG_DETAILS="testing if reading buffer size returns proper exit code"
391 dlogutil -gb main > /dev/null && ok || fail
392
393 LOG_DETAILS="testing if writing all entries to single file works (-f)"
394 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
395
396 if [ "$quick" -ne 1 ]; then
397         LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
398         dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
399         UTIL_PID=$!
400         sleep 1
401         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
402         UTIL_PID=-1
403         [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
404
405         LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
406         dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
407         UTIL_PID=$!
408         sleep 1
409         dlogsend -b main "Hi!"
410         sleep 1
411         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
412         UTIL_PID=-1
413         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
414
415         LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
416         dlogutil -mb main -f $TESTDIR/dlog_monitor1_file &
417         UTIL_PID=$!
418         sleep 1
419         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
420         UTIL_PID=-1
421         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
422
423         LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
424         dlogutil -mb main -f $TESTDIR/dlog_monitor2_file &
425         UTIL_PID=$!
426         sleep 1
427         dlogsend -b main "Hi!"
428         sleep 1
429         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
430         UTIL_PID=-1
431         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
432 fi
433
434 test_libdlog 100000 &
435 LIBDLOGUTIL_CORRECT_PID=$!
436 wait $LIBDLOGUTIL_CORRECT_PID
437
438 # NB: rotation only really makes sense in the continuous mode
439 # from a user PoV, but the logic doesn't care about dumping
440 # so we do it to simplify things
441 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
442
443 LOG_DETAILS="testing if single file is properly created"
444 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
445
446 LOG_DETAILS="testing if rotating file is properly created (1/4)"
447 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
448
449 LOG_DETAILS="testing if rotating file is properly created (2/4)"
450 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
451
452 LOG_DETAILS="testing if rotating file is properly created (3/4)"
453 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
454
455 LOG_DETAILS="testing if rotating file is properly created (4/4)"
456 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
457
458 LOG_DETAILS="testing the size of log files"
459 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
460
461 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
462 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
463 if [ "$quick" -ne 1 ]; then
464         LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
465         test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
466 fi
467 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
468 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
469 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
470 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
471 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
472 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
473 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
474 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
475 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
476 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
477 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
478 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
479 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
480 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
481 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
482 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
483 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
484 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
485 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
486 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
487 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
488 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
489 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
490 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
491 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
492 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
493 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
494 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
495 LOG_DETAILS="testing if libdlogutil aliasing works"
496 if [ "$type" = "pipe" ]; then
497         test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
498 else
499         DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
500 fi
501 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
502 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
503
504 cmd_prefix="dlogutil -t 1 -u 0 -v"
505
506 format="process"
507 regex_prio="[VDIWEFS]{1}"
508 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
509 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
510 regex_timezone="[\+-]{1}[0-9]{4}"
511
512 for PARAM in always auto none never; do
513         for PRIO in info error; do
514                 for OUTPUT in pipe tty; do
515                         if [ "$PARAM" = "always" ]; then
516                                 COLOR_EXPECTED=1
517                         elif [ "$PARAM" = "never" ]; then
518                                 COLOR_EXPECTED=0
519                         elif [ "$OUTPUT" = "tty" ]; then
520                                 COLOR_EXPECTED=1
521                         else
522                                 COLOR_EXPECTED=0
523                         fi
524
525                         if [ "$PRIO" = "info" ]; then
526                                 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
527                         elif [ "$COLOR_EXPECTED" -eq 0 ]; then
528                                 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
529                         else
530                                 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m  \([[:print:]]*\)@~\[0m$/1/g"
531                         fi
532
533                         # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
534                         COMMAND="$cmd_prefix $format"
535
536                         if [ "$PARAM" = "none" ]; then
537                                 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
538                         else
539                                 COMMAND="$COMMAND --color=$PARAM"
540                                 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
541                         fi
542
543                         if [ "$PRIO" = "info" ]; then
544                                 COMMAND="$COMMAND '*:=I'"
545                         else
546                                 COMMAND="$COMMAND '*:=E'"
547                         fi
548
549                         if [ "$OUTPUT" = "tty" ]; then
550                                 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
551                                 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
552                         fi
553
554                         # $(eval) is needed since `$COMMAND` may contain `"`
555                         line="$(eval "$COMMAND" | tr '\033\n' '~@')"
556                         [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
557                 done
558         done
559 done
560 # TODO: It would be cool to also test warning messages (which have a different color),
561 # but this would require deeper changes to this script, which is already janky enough.
562
563 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
564 LOG_DETAILS="testing if \"$format\" print format works"
565 line=$($cmd_prefix $format)
566 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
567
568 format="tag"
569 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
570 LOG_DETAILS="testing if \"$format\" print format works"
571 line=$($cmd_prefix $format)
572 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
573
574 format="thread"
575 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
576 LOG_DETAILS="testing if \"$format\" print format works"
577 line=$($cmd_prefix $format)
578 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
579
580 format="time"
581 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
582 LOG_DETAILS="testing if \"$format\" print format works"
583 line=$($cmd_prefix $format)
584 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
585
586 format="threadtime"
587 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
588 LOG_DETAILS="testing if \"$format\" print format works"
589 line=$($cmd_prefix $format)
590 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
591
592 format="kerneltime"
593 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
594 LOG_DETAILS="testing if \"$format\" print format works"
595 line=$($cmd_prefix $format)
596 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
597
598 format="recv_realtime"
599 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
600 LOG_DETAILS="testing if \"$format\" print format works"
601 line=$($cmd_prefix $format)
602 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
603
604 format="rwtime"
605 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"
606 LOG_DETAILS="testing if \"$format\" print format works"
607 line=$($cmd_prefix $format)
608 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
609
610 format="long"
611 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
612 LOG_DETAILS="testing if \"$format\" print format works"
613 line=$($cmd_prefix $format)
614 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
615
616 format="brief"
617 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
618 LOG_DETAILS="testing if \"$format\" print format works"
619 line=$($cmd_prefix $format)
620 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
621
622 format="json"
623 if [ "$type" = "pipe" ]; then
624         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"
625 else
626         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"
627 fi
628 LOG_DETAILS="testing if \"$format\" print format works"
629 line=$($cmd_prefix $format)
630 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
631
632 format="raw"
633 LOG_DETAILS="testing if \"$format\" print format works"
634 dlogutil -c
635 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
636 line=$($cmd_prefix $format)
637 [ "$line" = "rawformatTEST" ] && ok || fail
638
639 if [ "$quick" -ne 1 ]; then
640         LOG_DETAILS="testing if dlogsend -d works"
641         dlogutil -c
642         dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
643         DLOGSEND_PID=$!
644         sleep 10
645         kill $DLOGSEND_PID
646         # In 10 seconds, 3 cycles 7 logs each should have happened.
647         [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
648 fi
649
650 if [ "$type" = "pipe" ]; then
651         for format in raw brief long; do
652                 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
653                         LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
654                         log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
655
656                         dlogutil -c
657                         dlogsend -b main "$log"
658                         line=$($cmd_prefix $format | tr '\n' '@')
659
660                         case $format in
661                                 raw)
662                                         REGEX="s/$log@/1/g"
663                                         ;;
664                                 brief)
665                                         REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
666                                         ;;
667                                 long)
668                                         REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
669                                         ;;
670                         esac
671
672                         [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
673                 done
674         done
675 fi
676
677 LOG_DETAILS="testing if KMSG works in the default format"
678 # We check if the command returns at least 100 logs. This seems to be a safe value.
679 # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
680 [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
681
682 if [ "$quick" -ne 1 ]; then
683         LOG_DETAILS="testing if KMSG works in the raw format"
684         last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
685         sleep 1 # To make sure dlog has already parsed the log
686         dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
687
688         LOG_DETAILS="testing if pid filtering works"
689         dlogsend -b main -t DLOG_TESTSUITE pidTEST &
690         sleep 1
691         line=$(dlogutil -v raw -d --pid $!)
692         [ "$line" = "pidTEST" ] && ok || fail
693
694         LOG_DETAILS="testing if tid filtering works"
695         dlogsend -b main -t DLOG_TESTSUITE tidTEST &
696         sleep 1
697         line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
698         [ "$line" = "tidTEST" ] && ok || fail
699
700         mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
701         LOG_DETAILS="testing if secure logging works (1/2)"
702         grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
703         echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
704         dlogutil -c
705         dlogsend -zb main -t DLOG_TESTSUITE secure &
706         sleep 1
707         dlogsend -b main -t DLOG_TESTSUITE insecure &
708         sleep 1
709         line=$(dlogutil -v raw -d | head -n1)
710         [ "$line" = "secure" ] && ok || fail
711         LOG_DETAILS="testing if secure logging works (2/2)"
712         grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
713         echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
714         dlogutil -c
715         dlogsend -zb main -t DLOG_TESTSUITE secure &
716         sleep 1
717         dlogsend -b main -t DLOG_TESTSUITE insecure &
718         sleep 1
719         line=$(dlogutil -v raw -d | head -n1)
720         [ "$line" = "insecure" ] && ok || fail
721         mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH
722 fi
723
724 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
725 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
726 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
727
728 LOG_DETAILS="testing if tag filtering works (1/8)"
729 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
730 LOG_DETAILS="testing if tag filtering works (2/8)"
731 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
732 LOG_DETAILS="testing if tag filtering works (3/8)"
733 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
734 LOG_DETAILS="testing if tag filtering works (4/8)"
735 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
736 LOG_DETAILS="testing if tag filtering works (5/8)"
737 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
738 LOG_DETAILS="testing if tag filtering works (6/8)"
739 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
740 LOG_DETAILS="testing if tag filtering works (7/8)"
741 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
742 LOG_DETAILS="testing if tag filtering works (8/8)"
743 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
744
745 if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
746         LOG_DETAILS="testing if limiter and runtime filtering works"
747         dlogutil -c -b radio
748         test_filters
749         [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
750
751         LOG_DETAILS="testing proper SMACK label for dynamic config file location"
752         ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
753         # Running ls on an arbitrary directory and trying to make sense of results is problematic.
754         # However, this is not a problem in our usecase, as we only care about the first field, the security context.
755         # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
756         # shellcheck disable=SC2012
757         [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
758
759         if [ "$quick" -ne 1 ]; then
760                 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
761                 # PID-based limits and see what happens.
762                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
763                 DLOGSEND_PID=$!
764                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
765                 dlogctl --pid $DLOGSEND_PID -s deny
766                 kill -s CONT $DLOGSEND_PID
767                 wait $DLOGSEND_PID
768                 LOG_DETAILS="testing if PID limiting works (1/14)"
769                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
770                 LOG_DETAILS="testing if PID limiting works (2/14)"
771                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 1 ] && ok || fail
772                 LOG_DETAILS="testing if PID limiting works (3/14)"
773                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
774                 LOG_DETAILS="testing if PID limiting works (4/14)"
775                 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
776
777                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
778                 DLOGSEND_PID=$!
779                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
780                 dlogctl --pid $DLOGSEND_PID -s 25
781                 kill -s CONT $DLOGSEND_PID
782                 wait $DLOGSEND_PID
783                 LOG_DETAILS="testing if PID limiting works (5/14)"
784                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
785                 LOG_DETAILS="testing if PID limiting works (6/14)"
786                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 1 ] && ok || fail
787                 LOG_DETAILS="testing if PID limiting works (7/14)"
788                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
789                 LOG_DETAILS="testing if PID limiting works (8/14)"
790                 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
791
792                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
793                 DLOGSEND_PID=$!
794                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
795                 dlogctl --pid $DLOGSEND_PID -s allow
796                 kill -s CONT $DLOGSEND_PID
797                 wait $DLOGSEND_PID
798                 LOG_DETAILS="testing if PID limiting works (9/14)"
799                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
800                 LOG_DETAILS="testing if PID limiting works (10/14)"
801                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 0 ] && ok || fail
802                 LOG_DETAILS="testing if PID limiting works (11/14)"
803                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
804                 LOG_DETAILS="testing if PID limiting works (12/14)"
805                 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
806
807                 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
808                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
809                 DLOGSEND_PID=$!
810                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
811                 dlogctl --pid $DLOGSEND_PID -s 2
812                 kill -s CONT $DLOGSEND_PID
813                 wait $DLOGSEND_PID
814                 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
815                 LOG_DETAILS="testing if PID limiting works (13/14)"
816                 # In each 5 second period, 5 logs are sent, so there are 3 periods.
817                 # 2 logs from each period are allowed.
818                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
819                 LOG_DETAILS="testing if PID limiting works (14/14)"
820                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 3 ] && ok || fail
821
822                 # TODO: HACK ALERT!
823                 # The following QoS tests make perfect sense for the pipe backend, since we kill the already
824                 # existing dlog_logger and then start the new one, configured with QoS. However, on the logger
825                 # backend this is impossible, as dlog_test uses the global service started by systemd then.
826                 # This means that we have to just launch the second instance and hope nothing breaks.
827                 # This can be improved by not using the global service in the logger backend, and by having it
828                 # completely disabled in the config file -- this way, the tests would be completely uniform.
829                 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
830                 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
831                 if [ "$type" = "pipe" ]; then
832                         kill $LOGGER > /dev/null
833                         sleep 1
834                 fi
835                 dlog_logger &
836                 LOGGER=$!
837                 sleep 1
838
839                 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
840                 LOG_DETAILS="testing if QoS works (1/4)"
841                 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
842                 kill $LOGGER > /dev/null
843                 sleep 1
844                 if [ "$type" = "logger" ]; then
845                         dlogutil -cb main
846                 fi
847                 dlog_logger &
848                 LOGGER=$!
849                 sleep 1
850
851                 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
852                 LOG_DETAILS="testing if QoS works (2/4)"
853                 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
854                 kill $LOGGER > /dev/null
855                 sleep 1
856                 if [ "$type" = "logger" ]; then
857                         dlogutil -cb main
858                 fi
859                 dlog_logger &
860                 LOGGER=$!
861                 sleep 1
862
863                 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
864                 DLOGSEND_PID=$!
865                 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
866                 wait $DLOGSEND_PID
867                 LOG_DETAILS="testing if QoS works (3/4)"
868                 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
869                 LOG_DETAILS="testing if QoS works (4/4)"
870                 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
871
872                 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
873                 kill $LOGGER > /dev/null
874                 sleep 1
875                 if [ "$type" = "pipe" ]; then
876                         dlog_logger -b 99 -t 0 &
877                         LOGGER=$!
878                         sleep 1
879                 else
880                         LOGGER=-1
881                 fi
882         fi
883 fi
884
885 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
886 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
887
888 if [ "$quick" -ne 1 ]; then
889         if [ "$type" = "pipe" ]; then
890                 dlogsend -b main -t DLOG_LOGGER_TEST0 -c 100 "hi"
891                 sleep 1
892                 LOG_DETAILS="testing if logger daemon persistent logging works"
893                 [ "$(grep -c DLOG_LOGGER_TEST0 "$TESTDIR"/main)" -eq 100 ] && ok || fail
894
895                 dlogsend -b main -t DLOG_LOGGER_TEST1 -c 500000 "hi"
896                 sleep 1
897                 RESULT=$(du -c "$TESTDIR"/main* | tail -n 1 | cut -f 1)
898                 LOG_DETAILS="testing if logger daemon log rotating works (1/2)"
899                 [ "$RESULT" -gt 4096 ] && ok || fail # Min 4 MB
900                 LOG_DETAILS="testing if logger daemon log rotating works (2/2)"
901                 [ "$RESULT" -lt 6144 ] && ok || fail # Max 6 MB
902
903                 dlogutil -cb main
904         fi
905
906         LOG_DETAILS="testing if the library works with multithreaded app"
907         dlogutil -f $TESTDIR/dlog_mt_test &
908         MT_TEST=$!
909         test_libdlog && ok || fail
910
911         sleep 1
912         kill $MT_TEST
913         MT_TEST=-1
914
915         dlogutil -c
916         SPAWN_CNT=1000
917
918         # Spawn logging scripts and wait for them to finish.
919         # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
920         # wait for all the jobs of the subshell, which means all the logging scripts.
921         # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
922         # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
923         # group, sent about at the same time.
924         (
925                 for i in $(seq 1 $SPAWN_CNT); do
926                         delay=$(shuf -i 1-5 -n 1)
927                         ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
928                 done
929                 wait
930         )
931
932         LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
933
934         test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
935
936         if [ "$type" = "pipe" ]; then
937                 for P in recv sent; do
938                         for Q in mono real; do
939                                 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
940                                 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
941                         done
942                 done
943                 for P in recv sent; do
944                         LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
945                         dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
946                         LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
947                         dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\".*\$/\\1/" | sort -c && ok || fail
948                 done
949         fi
950
951         sort_formats="kerneltime time recv_realtime rwtime threadtime long"
952
953         for format in $sort_formats; do
954                 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
955
956                 # collect data and analyze timestamps
957                 dlogutil -b main -d -v "$format" | (
958                         prev_ts=0
959                         prev_line=""
960
961                         while read -r line; do
962                                 # filter out empty lines in "long" format
963                                 [ -z "$line" ] && continue
964                                 # filter out non-timestamp lines in "long" format
965                                 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
966                                         continue
967                                 fi
968
969                                 ts=$(extract_timestamp "$format" "$line")
970
971                                 if [ "$ts" -ge "$prev_ts" ]; then
972                                         prev_ts=$ts
973                                 else
974                                         printf "%s\n%s\n\n" "$prev_line" "$line" || break
975                                 fi
976                                 prev_line=$line
977                         done
978                 ) | grep -q ^ && fail || ok
979
980         done
981 fi
982
983 # show results, clean up and return an exit code
984
985 echo "$OKS / $TOTAL tests passed"
986 echo "$FAILS / $TOTAL tests failed"
987
988 if [ "$quick" -eq 1 ]; then
989         echo "WARNING: quick mode!"
990         echo "About 20% slowest running tests are disabled. Some functionality might be untested."
991         echo "Rerun without '--quick' to run all tests."
992 fi
993
994 [ "$FAILS" -eq 0 ]
995 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
996 # This is because it is used to pass the exit code. If another command
997 # is executed after it, its exit code will be propagated instead.