Merge "Document confusing daemon structures" into tizen
[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 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
377 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
378
379 LOG_DETAILS="testing if libdlogutil works correctly in continuous mode"
380 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type &
381 TEST_LIBDLOGUTIL_PID=$!
382 sleep 1
383 kill $TEST_LIBDLOGUTIL_PID && ok || fail
384 TEST_LIBDLOGUTIL_PID=-1
385
386 LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
387 test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
388
389 cmd_prefix="dlogutil -d -u 0 -v"
390 cmd_postfix="head -n 1 "
391
392 format="process"
393 regex_prio="[VDIWEFS]{1}"
394 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
395 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
396 regex_timezone="[\+-]{1}[0-9]{4}"
397
398 for PARAM in always auto none never; do
399         for PRIO in info error; do
400                 for OUTPUT in pipe tty; do
401                         if [[ "$PARAM" == "always" ]]; then
402                                 COLOR_EXPECTED=1
403                         elif [[ "$PARAM" == "never" ]]; then
404                                 COLOR_EXPECTED=0
405                         elif [[ "$OUTPUT" == "tty" ]]; then
406                                 COLOR_EXPECTED=1
407                         else
408                                 COLOR_EXPECTED=0
409                         fi
410
411                         if [[ "$PRIO" == "info" ]]; then
412                                 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
413                         elif [[ "$COLOR_EXPECTED" -eq 0 ]]; then
414                                 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
415                         else
416                                 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m  \([[:print:]]*\)@~\[0m$/1/g"
417                         fi
418
419                         # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
420                         COMMAND="$cmd_prefix $format -t 1"
421
422                         if [[ "$PARAM" == "none" ]]; then
423                                 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
424                         else
425                                 COMMAND="$COMMAND --color=$PARAM"
426                                 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
427                         fi
428
429                         if [[ "$PRIO" == "info" ]]; then
430                                 COMMAND="$COMMAND '*:=I'"
431                         else
432                                 COMMAND="$COMMAND '*:=E'"
433                         fi
434
435                         if [[ "$OUTPUT" == "tty" ]]; then
436                                 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
437                                 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
438                         fi
439
440                         # `eval` is needed since `$COMMAND` may contain `"`
441                         line=`eval $COMMAND | tr '\033\n' '~@'`
442                         [[ `echo "$line" | sed -re "$REGEX"` == "1" ]] && ok || fail
443                 done
444         done
445 done
446 # TODO: It would be cool to also test warning messages (which have a different color),
447 # but this would require deeper changes to this script, which is already janky enough.
448
449 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)$/1/g"
450 LOG_DETAILS="testing if \"$format\" print format works"
451 line=`$cmd_prefix $format | $cmd_postfix`
452 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
453
454 format="tag"
455 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
456 LOG_DETAILS="testing if \"$format\" print format works"
457 line=`$cmd_prefix $format | $cmd_postfix`
458 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
459
460 format="thread"
461 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
462 LOG_DETAILS="testing if \"$format\" print format works"
463 line=`$cmd_prefix $format | $cmd_postfix`
464 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
465
466 format="time"
467 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
468 LOG_DETAILS="testing if \"$format\" print format works"
469 line=`$cmd_prefix $format | $cmd_postfix`
470 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
471
472 format="threadtime"
473 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
474 LOG_DETAILS="testing if \"$format\" print format works"
475 line=`$cmd_prefix $format | $cmd_postfix`
476 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
477
478 format="kerneltime"
479 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
480 LOG_DETAILS="testing if \"$format\" print format works"
481 line=`$cmd_prefix $format | $cmd_postfix`
482 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
483
484 format="recv_realtime"
485 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
486 LOG_DETAILS="testing if \"$format\" print format works"
487 line=`$cmd_prefix $format | $cmd_postfix`
488 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
489
490 format="rwtime"
491 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"
492 LOG_DETAILS="testing if \"$format\" print format works"
493 line=`$cmd_prefix $format | $cmd_postfix`
494 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
495
496 format="long"
497 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]\s{1}[[:print:]]*$/1/g"
498 LOG_DETAILS="testing if \"$format\" print format works"
499 line=`$cmd_prefix $format | $cmd_postfix`
500 [[ `echo "$line" | tr '\n' ' ' | sed -re $REGEX` == "1" ]] && ok || fail
501
502 format="brief"
503 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
504 LOG_DETAILS="testing if \"$format\" print format works"
505 line=`$cmd_prefix $format | $cmd_postfix`
506 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
507
508 format="json"
509 if [[ "$type" == "pipe" ]]; then
510         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"
511 else
512         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"
513 fi
514 LOG_DETAILS="testing if \"$format\" print format works"
515 line=`$cmd_prefix $format | $cmd_postfix`
516 [[ `echo "$line" | sed -re $REGEX` == "1" ]] && ok || fail
517
518 format="raw"
519 LOG_DETAILS="testing if \"$format\" print format works"
520 dlogutil -c
521 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
522 line=`$cmd_prefix $format | $cmd_postfix`
523 [[ "$line" == "rawformatTEST" ]] && ok || fail
524
525 LOG_DETAILS="testing if KMSG works in the default format"
526 # We check if the command returns at least 1000 logs. This seems to be a safe value. In my test,
527 # my target reaches it in about 2 seconds after the boot. Feel free to change it to a reasonable
528 # yet lower value if the test happens to fail otherwise, but it would be extremely surprising.
529 [[ "$(dlogutil -db kmsg | wc -l)" -gt 1000 ]] && ok || fail
530
531 LOG_DETAILS="testing if KMSG works in the raw format"
532 last_dmesg=`dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g"`
533 sleep 1 # To make sure dlog has already parsed the log
534 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
535
536 LOG_DETAILS="testing if pid filtering works"
537 dlogsend -b main -t DLOG_TESTSUITE pidTEST &
538 sleep 1
539 line=`dlogutil -v raw -d --pid $!`
540 [[ "$line" == "pidTEST" ]] && ok || fail
541
542 LOG_DETAILS="testing if tid filtering works"
543 dlogsend -b main -t DLOG_TESTSUITE tidTEST &
544 sleep 1
545 line=`dlogutil -v raw -d --tid $!` #dlogsend is a single threaded app so tid is the same as pid
546 [[ "$line" == "tidTEST" ]] && ok || fail
547
548 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
549 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
550 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
551
552 LOG_DETAILS="testing if tag filtering works (1/8)"
553 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l` -eq 1 ]] && ok || fail
554 LOG_DETAILS="testing if tag filtering works (2/8)"
555 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l` -eq 1 ]] && ok || fail
556 LOG_DETAILS="testing if tag filtering works (3/8)"
557 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l` -eq 0 ]] && ok || fail
558 LOG_DETAILS="testing if tag filtering works (4/8)"
559 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l` -eq 3 ]] && ok || fail
560 LOG_DETAILS="testing if tag filtering works (5/8)"
561 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l` -eq 3 ]] && ok || fail
562 LOG_DETAILS="testing if tag filtering works (6/8)"
563 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l` -eq 1 ]] && ok || fail
564 LOG_DETAILS="testing if tag filtering works (7/8)"
565 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l` -eq 0 ]] && ok || fail
566 LOG_DETAILS="testing if tag filtering works (8/8)"
567 [[ `dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l` -eq 0 ]] && ok || fail
568
569 if [ "$TEST_DYNAMIC_FILTERS" == "true" ]; then
570         LOG_DETAILS="testing if limiter and runtime filtering works"
571         dlogutil -c -b radio
572         test_filters
573         [ $(dlogutil -d -b radio | wc -l) -eq 12 ] && ok || fail
574
575         LOG_DETAILS="testing proper SMACK label for dynamic config file location"
576         ORIG_FILTERS_DIR=$(cat $ORIGINAL_CONFIG_PATH | grep dynamic_config_path | awk -F "=" '{print $2}')
577         [ $(ls -dZ $ORIG_FILTERS_DIR | awk -F " " '{print $1}') == "System::Shared" ] && ok || fail
578 fi
579
580 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
581 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
582
583 LOG_DETAILS="testing if the library works with multithreaded app"
584 dlogutil -f $TESTDIR/dlog_mt_test &
585 MT_TEST=$!
586 test_libdlog && ok || fail
587
588 sleep 1
589 kill $MT_TEST
590 MT_TEST=-1
591
592 dlogutil -c
593 SPAWN_CNT=1000
594
595 # Spawn logging scripts and wait for them to finish.
596 # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
597 # wait for all the jobs of the subshell, which means all the logging scripts.
598 # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
599 # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
600 # group, sent about at the same time.
601 (
602         for i in `seq 1 $SPAWN_CNT`; do
603                 delay=$(( (RANDOM) % 5 ))
604                 ( sleep $delay && dlogsend -b main -t DLOG_TESTSUITE $delay ) &
605         done
606         wait
607 )
608
609 LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
610
611 test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
612
613 sort_formats="kerneltime time recv_realtime rwtime threadtime long"
614
615 for format in $sort_formats; do
616         LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
617         prev_ts=0
618         unsorted=""
619         prev_line=""
620
621         # collect data and analyze timestamps
622         lines=`dlogutil -b main -d -v $format`
623         while read line; do
624                 # filter out empty lines in "long" format
625                 [ -z "$line" ] && continue
626                 # filter out non-timestamp lines in "long" format
627                 if [ $format == "long" ] && [ "`echo "$line" | sed -re 's/^\[[[:print:]]*\]$/line_ok/g'`" != "line_ok" ]; then
628                         continue
629                 fi
630
631                 [ "$line" -eq "$line" ] 2>/dev/null && continue
632
633                 ts=$(extract_timestamp "$format" "$line")
634
635                 if [ "$ts" -ge "$prev_ts" ]; then
636                         prev_ts=$ts
637                 else
638                         printf -v unsorted '%s\n\n%s\n%s' "$unsorted" "$prev_line" "$line"
639                         break
640                 fi
641                 prev_line=$line
642         done <<< "$lines"
643
644         [ -z "$unsorted" ] && ok || fail
645 done
646
647 sleep 3
648
649 if [ $type == "pipe" ]; then
650
651         # spawn a new daemon with limited FDs for the FD checks
652         kill $LOGGER > /dev/null
653         sleep 1
654         ULIMIT_CURRENT=`ulimit -n`
655         ulimit -n 30
656         dlog_logger -b 99 -t 600 &> /dev/null &
657         LOGGER=$!
658         ulimit -n $ULIMIT_CURRENT
659
660         LOG_DETAILS="checking if the daemon closes util connections without logs stored"
661         run_utils
662         sleep 3
663         [ `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
664
665         LOG_DETAILS="checking if the daemon closes libdlog connections"
666         for i in {1..30}; do
667                 dlogsend -c $i -b main -p e -t FOO BAR BAZ &
668         done
669         dlogsend -c 99999 -b main -p e -t FOO BAR BAZ &
670         sleep 0.5
671         kill $!
672         sleep 1.5
673         [ `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
674
675         LOG_DETAILS="checking if the daemon closes util connections with logs stored"
676         run_utils
677         sleep 0.5
678         killall dlogutil &> /dev/null
679         sleep 1.5
680         [ `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
681 fi
682
683 # show results, clean up and return an exit code
684
685 echo "$OKS / $TOTAL tests passed"
686 echo "$FAILS / $TOTAL tests failed"
687
688 [[ "$FAILS" -eq 0 ]]
689 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
690 # This is because it is used to pass the exit code. If another command
691 # is executed after it, its exit code will be propagated instead.