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