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