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