Add exit code propagation to the integration tests
[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 MT_TEST=-1
12 LOGGER=-1
13
14 TEST_DYNAMIC_FILTERS="@DYNAMIC_FILTERS@"
15 TESTDIR=/var/lib/dlog-tests
16
17 extract_timestamp() {
18         local ts=0
19         case "$1" in
20                 "threadtime")
21                         time=`echo $2 | awk -F '[ +]' '{print $2}'`
22                         ts=`date +%s%N -d $time`
23                 ;;
24                 "long")
25                         time=`echo $2 | awk -F '[ +.]' '{print $3}'`
26                         ms=`echo $2 | awk -F '[ +.]' '{print $4}'`
27                         sec=`date +%s%N -d $time`
28                         ns=$((10#$ms * 1000000))
29                         ts=$(($sec + $ns))
30                 ;;
31                 "rwtime")
32                         time=`echo $2 | awk -F '[ +.]' '{print $2}'`
33                         ts=`date +%s%N -d $time`
34                 ;;
35                 "recv_realtime")
36                         time=`echo $2 | awk -F '[ +.]' '{print $2}'`
37                         ts=`date +%s%N -d $time`
38                 ;;
39                 "time")
40                         time=`echo $2 | awk -F '[ +.]' '{print $2}'`
41                         ts=`date +%s%N -d $time`
42                 ;;
43                 "kerneltime")
44                         ts=`echo $2 | awk -F '[ +]' '{print $1}' | sed -e 's/\.//g'`
45                 ;;
46         esac
47         echo "$ts"
48 }
49
50 cleanup() {
51         [ $UTIL_PID -ne -1 ] && kill $UTIL_PID > /dev/null 2>&1
52         [ $MT_TEST  -ne -1 ] && kill $MT_TEST  > /dev/null 2>&1
53         [ $LOGGER   -ne -1 ] && kill $LOGGER   > /dev/null 2>&1
54         [ -d $TESTDIR ] && rm -rf $TESTDIR/*
55         [ -d $RUNTIME_FILTERS_DIR ] && rm -rf $RUNTIME_FILTERS_DIR
56 }
57
58 trap cleanup 0
59
60 check_daemon() {
61         local ret=1
62         if [ $LOGGER -ne -1 ] && [ -z "$(ps -o pid= -p $LOGGER)" ]; then
63                 ret=0
64         fi
65         return $ret
66 }
67
68 fail() {
69         check_daemon
70         [ $? -eq 0 ] && 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
80         [ $? -eq 0 ] && daemon_status="[logger daemon not running]"
81
82         OKS=$(($OKS + 1))
83         TOTAL=$(($TOTAL + 1))
84         printf "[%02d] PASSED: %s %s\n" $TOTAL "$LOG_DETAILS" "$daemon_status"
85         LOG_DETAILS=
86 }
87
88 # supress stderr messages from subcommands
89 exec 2> /dev/null
90
91 if [ $# -ne 1 ]; then
92         echo "usage: $0 pipe|logger"
93         exit 1
94 fi
95
96 if [ "$1" == "pipe" ]; then
97         type="pipe"
98 elif [ "$1" == "logger" ]; then
99         type="logger"
100 else
101         echo "usage: $0 pipe|logger"
102         exit 1
103 fi
104
105 if [ -z "$DLOG_CONFIG_PATH" ]; then
106         ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
107 else
108         ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
109 fi
110
111 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
112 PATH=$PATH:@libexecdir@/libdlog/
113
114 #create dir for runtime filters
115 RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
116 mkdir -p "$RUNTIME_FILTERS_DIR"
117
118 # Start the daemon
119 if [ $type == "pipe" ]; then
120         dlog_logger -b 99 -t 0 &
121         LOGGER=$!
122         sleep 1
123 fi
124
125 if [ "$TEST_DYNAMIC_FILTERS" == "true" ]; then
126         dlogctl -c
127         dlogctl --enable
128         dlogutil -c -b radio -b system -b main
129         LOG_DETAILS="dlogctl --disable (no args)"
130         dlogctl --disable
131         [ `dlogctl -g | grep ENABLED | wc -l` -eq 0 ] && ok || fail
132         LOG_DETAILS="testing if filters were applied"
133         dlogsend -b system -t TEST test
134         dlogsend -b main -t TEST test
135         dlogsend -b radio -t TEST test
136         [ `dlogutil -d -b radio -b system -b main | wc -l` -eq 0 ] && ok || fail
137
138         LOG_DETAILS="dlogctl --enable (no args)"
139         dlogctl --enable
140         [ `dlogctl -g | grep DISABLED | wc -l` -eq 0 ] && ok || fail
141         LOG_DETAILS="testing if filters were applied"
142         dlogutil -c -b radio -b system -b main
143         dlogsend -b system -t TEST test
144         dlogsend -b main -t TEST test
145         dlogsend -b radio -t TEST test
146         [ `dlogutil -d -b radio -b system -b main | wc -l` -eq 3 ] && ok || fail
147
148         LOG_DETAILS="dlogctl --disable (1 arg)"
149         dlogctl --disable -b system
150         [ `dlogctl -g | grep DISABLED | grep system | wc -l` -eq 1 ] &&
151         [ `dlogctl -g | grep DISABLED               | wc -l` -eq 1 ] && ok || fail
152         LOG_DETAILS="testing if filters were applied"
153         dlogutil -c -b radio -b system -b main
154         dlogsend -b system -t TEST test
155         dlogsend -b main -t TEST test
156         dlogsend -b radio -t TEST test
157         [ `dlogutil -d -b radio -b system -b main | wc -l` -eq 2 ] && ok || fail
158
159         LOG_DETAILS="dlogctl --disable (multiple args)"
160         dlogctl --disable -b main -b radio
161         [ `dlogctl -g | grep DISABLED | grep -v system | wc -l` -eq 2 ] &&
162         [ `dlogctl -g | grep DISABLED | grep     radio | wc -l` -eq 1 ] &&
163         [ `dlogctl -g | grep DISABLED | grep      main | wc -l` -eq 1 ] && ok || fail
164         LOG_DETAILS="testing if filters were applied"
165         dlogutil -c -b radio -b system -b main
166         dlogsend -b system -t TEST test
167         dlogsend -b main -t TEST test
168         dlogsend -b radio -t TEST test
169         [ `dlogutil -d -b radio -b system -b main | wc -l` -eq 0 ] && ok || fail
170
171         LOG_DETAILS="dlogctl --enable (multiple args)"
172         dlogctl --enable -b radio -b system
173         [ `dlogctl -g | grep DISABLED               | wc -l` -eq 1 ] &&
174         [ `dlogctl -g | grep DISABLED | grep   main | wc -l` -eq 1 ] &&
175         [ `dlogctl -g | grep  ENABLED | grep system | wc -l` -eq 1 ] &&
176         [ `dlogctl -g | grep  ENABLED | grep  radio | wc -l` -eq 1 ] && ok || fail
177         LOG_DETAILS="testing if filters were applied"
178         dlogutil -c -b radio -b system -b main
179         dlogsend -b system -t TEST test
180         dlogsend -b main -t TEST test
181         dlogsend -b radio -t TEST test
182         [ `dlogutil -d -b radio -b system -b main | wc -l` -eq 2 ] && ok || fail
183
184         LOG_DETAILS="dlogctl --enable (1 arg)"
185         dlogctl --enable -b main
186         [ `dlogctl -g | grep DISABLED | wc -l` -eq 0 ] && ok || fail
187         LOG_DETAILS="testing if filters were applied"
188         dlogutil -c -b radio -b system -b main
189         dlogsend -b system -t TEST test
190         dlogsend -b main -t TEST test
191         dlogsend -b radio -t TEST test
192         [ `dlogutil -d -b radio -b system -b main | wc -l` -eq 3 ] && ok || fail
193
194         LOG_DETAILS="dlogctl -s allow"
195         dlogutil -c -b radio -b system -b main
196         dlogctl -c
197         dlogctl -s deny
198         dlogctl -t TEST_TAG -s allow
199         dlogsend -b main -t TEST_TAG test
200         dlogsend -b main -t TEST test
201         [ `dlogutil -d -b main | wc -l` -eq 1 ] && ok || fail
202
203         LOG_DETAILS="dlogctl -s [N]"
204         dlogutil -c -b radio -b system -b main
205         dlogctl -c
206         dlogctl -s deny
207         dlogctl -t TEST_TAG -s 3
208         dlogsend -b main -c 10 -t TEST_TAG test
209         dlogsend -b main -c 10 -t TEST test
210         [ `dlogutil -d -b main | wc -l` -eq 4 ] && ok || fail
211
212         LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
213         dlogctl -s invalid &> /dev/null && fail || ok
214
215         LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
216         dlogctl -p X -g &> /dev/null && fail || ok
217
218         LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
219         dlogctl -b invalid --disable &> /dev/null && fail || ok
220
221         LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
222         dlogctl -s allow -g -t xyz -p E &> /dev/null && fail || ok
223
224         LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
225         dlogctl -s allow -c &> /dev/null && fail || ok
226
227         LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
228         dlogctl -c -g -t xyz -p E &> /dev/null && fail || ok
229
230         # -s out of range
231         LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
232         dlogctl -s -10 &> /dev/null && fail || ok
233
234         LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
235         dlogctl -s 999999 &> /dev/null && fail || ok
236
237         # -s correctness
238         LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
239         dlogctl -s 100 && ok || fail
240
241         LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
242         [ `cat $RUNTIME_FILTERS_DIR/FILTERS | grep -E 'limiter\|\*\|\*=100' | wc -l` -eq 1 ] && ok || fail
243
244         # -g correctness
245         LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
246         [ `dlogctl -g | grep '*:*' | grep 100 | wc -l` -eq 1 ] && ok || fail
247
248         # -c correctness
249         LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
250         dlogctl -c -t '*' -p '*' && ok || fail
251
252         LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
253         [ `cat $RUNTIME_FILTERS_DIR/FILTERS | grep -E 'limiter\|\*\|\*=100' | wc -l` -eq 0 ] && ok || fail
254         dlogctl -c
255 fi
256
257 # put 100 log entries in the "main" buffer
258 dlogutil -c
259 test_libdlog 100
260 sleep 1
261
262
263 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
264 dlogutil -d &> /dev/null && ok || fail
265
266 LOG_DETAILS="testing if -t argument is parsed properly"
267 dlogutil -t dummy &> /dev/null && fail || ok
268
269 LOG_DETAILS="testing if -u argument is parsed properly"
270 dlogutil -du dummy &> /dev/null && fail || ok
271
272 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
273 [ $(dlogutil -b main -t  20 | wc -l) -eq  20 ] && ok || fail # less logs than currently in buffer
274
275 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
276 [ $(dlogutil -b main -t 200 | wc -l) -eq 100 ] && ok || fail # more
277
278 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
279 [ $(dlogutil -b main -d     | wc -l) -eq 100 ] && ok || fail # exactly
280
281 LOG_DETAILS="testing if reading from  dummy buffer returns an error as expected"
282 dlogutil -b nonexistent_buffer &> /dev/null && fail || ok
283
284 LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
285 [ $(dlogutil -d -b system | wc -l) -eq 0 ] && ok || fail
286
287 LOG_DETAILS="testing if dlogutil -c empties all buffers"
288 dlogutil -cb main && ok || fail
289
290 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
291 test_libdlog 10
292 [ $(dlogutil -b main -d | wc -l) -eq 10 ] && ok || fail # should be 10, not 110
293
294 LOG_DETAILS="testing if filters work (1/3)"
295 [ $(dlogutil -b main -d *:E | wc -l) -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
296
297 LOG_DETAILS="testing if filters work (2/3)"
298 [ $(dlogutil -b main -d *:W | wc -l) -eq 5 ] && ok || fail
299
300 LOG_DETAILS="testing if filters work (3/3)"
301 [ $(dlogutil -b main -d *:I | wc -l) -eq 10 ] && ok || fail
302
303 LOG_DETAILS="testing if exact filters work (1/3)"
304 [ $(dlogutil -b main -d *:=E | wc -l) -eq 5 ] && ok || fail
305
306 LOG_DETAILS="testing if exact filters work (2/3)"
307 [ $(dlogutil -b main -d *:=W | wc -l) -eq 0 ] && ok || fail
308
309 LOG_DETAILS="testing if exact filters work (3/3)"
310 [ $(dlogutil -b main -d *:=I | wc -l) -eq 5 ] && ok || fail
311
312 LOG_DETAILS="testing if adding \"silent\" filter works"
313 [ $(dlogutil -b main -s -d | wc -l) -eq 0 ] && ok || fail
314
315 LOG_DETAILS="testing if reading buffer size returns proper exit code"
316 dlogutil -gb main &> /dev/null && ok || fail
317
318 LOG_DETAILS="testing if writing all entries to single file works (-f)"
319 dlogutil -f $TESTDIR/dlog_test_file -d &> /dev/null && ok || fail
320
321 LOG_DETAILS="testing if writing entries to rotating files works (-r/-n)"
322 dlogutil -f $TESTDIR/dlog_rotating_file -r 12 -n 3 & # 3 files at 12 KB each
323 UTIL_PID=$!
324
325 test_libdlog 100000 &
326 LIBDLOGUTIL_CORRECT_PID=$!
327 wait $LIBDLOGUTIL_CORRECT_PID
328
329 LOG_DETAILS="testing if single file is properly created"
330 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
331
332 LOG_DETAILS="testing if rotating file is properly created (1/4)"
333 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
334
335 LOG_DETAILS="testing if rotating file is properly created (2/4)"
336 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
337
338 LOG_DETAILS="testing if rotating file is properly created (3/4)"
339 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
340
341 LOG_DETAILS="testing if rotating file is properly created (4/4)"
342 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
343
344 LOG_DETAILS="testing the size of log files"
345 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
346
347 kill $UTIL_PID
348 UTIL_PID=-1
349
350 LOG_DETAILS="testing if libdlogutil works correctly in a simple scenario"
351 test_libdlogutil normal $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
352 LOG_DETAILS="testing if libdlogutil works correctly without any additional settings"
353 test_libdlogutil defaults $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
354 LOG_DETAILS="testing if libdlogutil works correctly if the callback takes some time"
355 test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
356 LOG_DETAILS="testing if libdlogutil works correctly with filtering on priority (1/2)"
357 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
358 LOG_DETAILS="testing if libdlogutil works correctly with filtering on priority (2/2)"
359 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
360 LOG_DETAILS="testing if libdlogutil works correctly with log count limit"
361 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
362 LOG_DETAILS="testing if libdlogutil works correctly with PID filtering"
363 test_libdlogutil pid $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
364 LOG_DETAILS="testing if libdlogutil works correctly with TID filtering"
365 test_libdlogutil tid $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
366 LOG_DETAILS="testing if libdlogutil works correctly with tag filtering (1/2)"
367 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
368 LOG_DETAILS="testing if libdlogutil works correctly with tag filtering (2/2)"
369 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
370 LOG_DETAILS="testing if libdlogutil works correctly with prefix filtering (1/2)"
371 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
372 LOG_DETAILS="testing if libdlogutil works correctly with prefix filtering (2/2)"
373 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
374 LOG_DETAILS="testing if libdlogutil propagates the callback return value correctly"
375 test_libdlogutil return $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
376
377 LOG_DETAILS="testing if libdlogutil works correctly in continuous mode"
378 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type &
379 TEST_LIBDLOGUTIL_PID=$!
380 sleep 1
381 kill $TEST_LIBDLOGUTIL_PID && ok || fail
382 TEST_LIBDLOGUTIL_PID=-1
383
384 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
385 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
386
387 cmd_prefix="dlogutil -d -u 0 -v"
388 cmd_postfix="head -n 1 "
389
390 format="process"
391 regex_prio="[VDIWEFS]{1}"
392 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
393 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
394 regex_timezone="[\+-]{1}[0-9]{4}"
395
396 for PARAM in always auto none never; do
397         for PRIO in info error; do
398                 for OUTPUT in pipe tty; do
399                         if [[ "$PARAM" == "always" ]]; then
400                                 COLOR_EXPECTED=1
401                         elif [[ "$PARAM" == "never" ]]; then
402                                 COLOR_EXPECTED=0
403                         elif [[ "$OUTPUT" == "tty" ]]; then
404                                 COLOR_EXPECTED=1
405                         else
406                                 COLOR_EXPECTED=0
407                         fi
408
409                         if [[ "$PRIO" == "info" ]]; then
410                                 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
411                         elif [[ "$COLOR_EXPECTED" -eq 0 ]]; then
412                                 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
413                         else
414                                 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m  \([[:print:]]*\)@~\[0m$/1/g"
415                         fi
416
417                         # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
418                         COMMAND="$cmd_prefix $format -t 1"
419
420                         if [[ "$PARAM" == "none" ]]; then
421                                 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
422                         else
423                                 COMMAND="$COMMAND --color=$PARAM"
424                                 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
425                         fi
426
427                         if [[ "$PRIO" == "info" ]]; then
428                                 COMMAND="$COMMAND '*:=I'"
429                         else
430                                 COMMAND="$COMMAND '*:=E'"
431                         fi
432
433                         if [[ "$OUTPUT" == "tty" ]]; then
434                                 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
435                                 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
436                         fi
437
438                         # `eval` is needed since `$COMMAND` may contain `"`
439                         line=`eval $COMMAND | tr '\033\n' '~@'`
440                         [[ `echo "$line" | sed -re "$REGEX"` == "1" ]] && ok || fail
441                 done
442         done
443 done
444 # TODO: It would be cool to also test warning messages (which have a different color),
445 # but this would require deeper changes to this script, which is already janky enough.
446
447 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
448 LOG_DETAILS="testing if \"$format\" print format works"
449 line=`$cmd_prefix $format | $cmd_postfix`
450 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
451
452 format="tag"
453 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
454 LOG_DETAILS="testing if \"$format\" print format works"
455 line=`$cmd_prefix $format | $cmd_postfix`
456 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
457
458 format="thread"
459 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
460 LOG_DETAILS="testing if \"$format\" print format works"
461 line=`$cmd_prefix $format | $cmd_postfix`
462 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
463
464 format="time"
465 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
466 LOG_DETAILS="testing if \"$format\" print format works"
467 line=`$cmd_prefix $format | $cmd_postfix`
468 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
469
470 format="threadtime"
471 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
472 LOG_DETAILS="testing if \"$format\" print format works"
473 line=`$cmd_prefix $format | $cmd_postfix`
474 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
475
476 format="kerneltime"
477 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
478 LOG_DETAILS="testing if \"$format\" print format works"
479 line=`$cmd_prefix $format | $cmd_postfix`
480 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
481
482 format="recv_realtime"
483 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
484 LOG_DETAILS="testing if \"$format\" print format works"
485 line=`$cmd_prefix $format | $cmd_postfix`
486 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
487
488 format="rwtime"
489 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"
490 LOG_DETAILS="testing if \"$format\" print format works"
491 line=`$cmd_prefix $format | $cmd_postfix`
492 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
493
494 format="long"
495 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]\s{1}[[:print:]]*$/1/g"
496 LOG_DETAILS="testing if \"$format\" print format works"
497 line=`$cmd_prefix $format | $cmd_postfix`
498 [[ `echo "$line" | tr '\n' ' ' | sed -re $REGEX` == "1" ]] && ok || fail
499
500 format="brief"
501 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
502 LOG_DETAILS="testing if \"$format\" print format works"
503 line=`$cmd_prefix $format | $cmd_postfix`
504 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
505
506 format="json"
507 if [[ "$type" == "pipe" ]]; then
508         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"
509 else
510         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"
511 fi
512 LOG_DETAILS="testing if \"$format\" print format works"
513 line=`$cmd_prefix $format | $cmd_postfix`
514 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
515
516 format="raw"
517 LOG_DETAILS="testing if \"$format\" print format works"
518 dlogutil -c
519 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
520 line=`$cmd_prefix $format | $cmd_postfix`
521 [[ "$line" == "rawformatTEST" ]] && ok || fail
522
523 LOG_DETAILS="testing if KMSG works in the default format"
524 # We check if the command returns at least 1000 logs. This seems to be a safe value. In my test,
525 # my target reaches it in about 2 seconds after the boot. Feel free to change it to a reasonable
526 # yet lower value if the test happens to fail otherwise, but it would be extremely surprising.
527 [[ "$(dlogutil -db kmsg | wc -l)" -gt 1000 ]] && ok || fail
528
529 LOG_DETAILS="testing if KMSG works in the raw format"
530 last_dmesg=`dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g"`
531 sleep 1 # To make sure dlog has already parsed the log
532 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
533
534 LOG_DETAILS="testing if pid filtering works"
535 dlogsend -b main -t DLOG_TESTSUITE pidTEST &
536 sleep 1
537 line=`dlogutil -v raw -d --pid $!`
538 [[ "$line" == "pidTEST" ]] && ok || fail
539
540 LOG_DETAILS="testing if tid filtering works"
541 dlogsend -b main -t DLOG_TESTSUITE tidTEST &
542 sleep 1
543 line=`dlogutil -v raw -d --tid $!` #dlogsend is a single threaded app so tid is the same as pid
544 [[ "$line" == "tidTEST" ]] && ok || fail
545
546 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
547 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
548 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
549
550 LOG_DETAILS="testing if tag filtering works (1/8)"
551 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l` -eq 1 ]] && ok || fail
552 LOG_DETAILS="testing if tag filtering works (2/8)"
553 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l` -eq 1 ]] && ok || fail
554 LOG_DETAILS="testing if tag filtering works (3/8)"
555 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l` -eq 0 ]] && ok || fail
556 LOG_DETAILS="testing if tag filtering works (4/8)"
557 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l` -eq 3 ]] && ok || fail
558 LOG_DETAILS="testing if tag filtering works (5/8)"
559 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l` -eq 3 ]] && ok || fail
560 LOG_DETAILS="testing if tag filtering works (6/8)"
561 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l` -eq 1 ]] && ok || fail
562 LOG_DETAILS="testing if tag filtering works (7/8)"
563 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l` -eq 0 ]] && ok || fail
564 LOG_DETAILS="testing if tag filtering works (8/8)"
565 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l` -eq 0 ]] && ok || fail
566
567 if [ "$TEST_DYNAMIC_FILTERS" == "true" ]; then
568         LOG_DETAILS="testing if limiter and runtime filtering works"
569         dlogutil -c -b radio
570         test_filters
571         [ $(dlogutil -d -b radio | wc -l) -eq 12 ] && ok || fail
572
573         LOG_DETAILS="testing proper SMACK label for dynamic config file location"
574         ORIG_FILTERS_DIR=$(cat $ORIGINAL_CONFIG_PATH | grep dynamic_config_path | awk -F "=" '{print $2}')
575         [ $(ls -dZ $ORIG_FILTERS_DIR | awk -F " " '{print $1}') == "System::Shared" ] && ok || fail
576 fi
577
578 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
579 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
580
581 LOG_DETAILS="testing if the library works with multithreaded app"
582 dlogutil -f $TESTDIR/dlog_mt_test &
583 MT_TEST=$!
584 test_libdlog && ok || fail
585
586 sleep 1
587 kill $MT_TEST
588 MT_TEST=-1
589
590 dlogutil -c
591 SPAWN_CNT=1000
592
593 # Spawn logging scripts and wait for them to finish.
594 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
595 # wait for all the jobs of the subshell, which means all the logging scripts.
596 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
597 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
598 # group, sent about at the same time.
599 (
600         for i in `seq 1 $SPAWN_CNT`; do
601                 delay=$(( (RANDOM) % 5 ))
602                 ( sleep $delay && dlogsend -b main -t DLOG_TESTSUITE $delay ) &
603         done
604         wait
605 )
606
607 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
608
609 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
610
611 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
612
613 for format in $sort_formats; do
614         LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
615         prev_ts=0
616         unsorted=""
617         prev_line=""
618
619         # collect data and analyze timestamps
620         lines=`dlogutil -b main -d -v $format`
621         while read line; do
622                 # filter out empty lines in "long" format
623                 [ -z "$line" ] && continue
624                 # filter out non-timestamp lines in "long" format
625                 if [ $format == "long" ] && [ "`echo "$line" | sed -re 's/^\[[[:print:]]*\]$/line_ok/g'`" != "line_ok" ]; then
626                         continue
627                 fi
628
629                 [ "$line" -eq "$line" ] 2>/dev/null && continue
630
631                 ts=$(extract_timestamp "$format" "$line")
632
633                 if [ "$ts" -ge "$prev_ts" ]; then
634                         prev_ts=$ts
635                 else
636                         printf -v unsorted '%s\n\n%s\n%s' "$unsorted" "$prev_line" "$line"
637                         break
638                 fi
639                 prev_line=$line
640         done <<< "$lines"
641
642         [ -z "$unsorted" ] && ok || fail
643 done
644
645 sleep 3
646
647 if [ $type == "pipe" ]; then
648
649         # spawn a new daemon with limited FDs for the FD checks
650         kill $LOGGER > /dev/null
651         sleep 1
652         ULIMIT_CURRENT=`ulimit -n`
653         ulimit -n 30
654         dlog_logger -b 99 -t 600 &> /dev/null &
655         LOGGER=$!
656         ulimit -n $ULIMIT_CURRENT
657
658         LOG_DETAILS="checking if the daemon closes util connections without logs stored"
659         run_utils
660         sleep 3
661         [ `ls -la /proc/$LOGGER/fd | grep fifo | wc -l` -eq 0 -a `ls -la /proc/$LOGGER/fd | grep pipe | wc -l` -eq 0 ] && ok || fail
662
663         LOG_DETAILS="checking if the daemon closes libdlog connections"
664         for i in {1..30}; do
665                 dlogsend -c $i -b main -p e -t FOO BAR BAZ &
666         done
667         dlogsend -c 99999 -b main -p e -t FOO BAR BAZ &
668         sleep 0.5
669         kill $!
670         sleep 1.5
671         [ `ls -la /proc/$LOGGER/fd | grep fifo | wc -l` -eq 0 -a `ls -la /proc/$LOGGER/fd | grep pipe | wc -l` -eq 0 ] && ok || fail
672
673         LOG_DETAILS="checking if the daemon closes util connections with logs stored"
674         run_utils
675         sleep 0.5
676         killall dlogutil &> /dev/null
677         sleep 1.5
678         [ `ls -la /proc/$LOGGER/fd | grep fifo | wc -l` -eq 0 -a `ls -la /proc/$LOGGER/fd | grep pipe | wc -l` -eq 0 ] && ok || fail
679 fi
680
681 # show results, clean up and return an exit code
682
683 echo "$OKS / $TOTAL tests passed"
684 echo "$FAILS / $TOTAL tests failed"
685
686 [[ "$FAILS" -eq 0 ]]
687 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
688 # This is because it is used to pass the exit code. If another command
689 # is executed after it, its exit code will be propagated instead.