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