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