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