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