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