tests: extract stdout redirection
[platform/core/system/dlog.git] / tests / dlog_test.in
1 #!/bin/sh
2
3 # We use the something && ok || fail pattern quite a lot; it is not correct in general,
4 # which makes shellcheck complain about it, but in our case it's ok to use it since
5 # the ok function will never fail.
6 # shellcheck disable=SC2015
7
8 source dlog_test_header
9
10 trap cleanup 0
11
12 USAGE_MESSAGE="usage: $0 [--verbose] [--quick] pipe|logger|zero-copy"
13
14 OPTS=$(getopt --shell sh --options "" --long verbose,quick --quiet -- "$@")
15 if [ "$?" -eq 1 ]; then
16         echo "$USAGE_MESSAGE"
17         exit 1
18 fi
19 eval set -- "$OPTS"
20
21 verbose=0
22 quick=0
23 while true; do
24         case "$1" in
25                 --verbose)
26                         verbose=1
27                         ;;
28                 --quick)
29                         quick=1
30                         ;;
31                 --)
32                         shift
33                         break
34                         ;;
35                 *)
36                         echo "This should never happen :)"
37                         exit 1
38                         ;;
39         esac
40         shift
41 done
42 if [ "$1" = "pipe" ]; then
43         type="pipe"
44 elif [ "$1" = "logger" ]; then
45         type="logger"
46 elif [ "$1" = "zero-copy" ]; then
47         type="zero-copy"
48 # We still accept the legacy {pipe,logger}_quick syntax because there's no reason not to.
49 elif [ "$1" = "pipe_quick" ]; then
50         type="pipe"
51         quick=1
52 elif [ "$1" = "logger_quick" ]; then
53         type="logger"
54         quick=1
55 else
56         echo "$USAGE_MESSAGE"
57         exit 1
58 fi
59
60 # supress stderr messages from subcommands
61 if [ "$verbose" -eq 0 ]; then
62         exec 2> /dev/null
63 fi
64
65 NEEDS_TO_QUIT=0
66 capsh --print | grep Current | grep cap_syslog > /dev/null || { echo "*** ERROR: cap_syslog missing"; NEEDS_TO_QUIT=1; }
67 mount | grep ' / ' | grep rw > /dev/null || { echo "*** ERROR: root not mounted read-write"; NEEDS_TO_QUIT=1; }
68 [ "$NEEDS_TO_QUIT" -eq 0 ] || exit 1
69
70 if [ "$type" = "pipe" ] || [ -f /lib/modules/"$(uname -r)"/extra/logger.ko ]; then
71         LEGACY_LOGGER=0
72 else
73         LEGACY_LOGGER=1
74 fi
75
76 if [ -z "$DLOG_CONFIG_PATH" ]; then
77         ORIGINAL_CONFIG_PATH="/etc/dlog.conf"
78 else
79         ORIGINAL_CONFIG_PATH="$DLOG_CONFIG_PATH"
80 fi
81
82 export DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.test"
83 PATH=$PATH:@libexecdir@/libdlog/
84
85 if [ "$type" != "zero-copy" ]; then # These tests are inherently dynamic.
86         #create dir for runtime filters
87         RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
88         mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
89
90         if [ "$quick" -ne 1 ]; then
91                 dlog_test_daemon_early_quit $type
92         fi
93
94         # Start the daemon
95         dlog_logger -t 0 &
96         LOGGER=$!
97         sleep 1
98
99         if [ "$TEST_DYNAMIC_FILTERS" = "true" ]; then
100                 dlogctl -c
101                 dlogctl --enable
102                 dlogutil -c -b radio -b system -b main
103                 LOG_DETAILS="dlogctl --disable (no args, limiter_apply_to_all_buffers=0)"
104                 dlogctl --disable
105
106                 # without 'limiter_apply_to_all_buffers=1' this
107                 # only applies to the three platform buffers:
108                 # main, radio, and system
109                 PREPLOG_CONFIG_PATH="$DLOG_CONFIG_PATH"
110                 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.plog"
111                 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 3 ] && ok || fail
112                 DLOG_CONFIG_PATH="$PREPLOG_CONFIG_PATH"
113
114                 LOG_DETAILS="dlogctl --disable (no args, limiter_apply_to_all_buffers=1)"
115                 dlogctl --disable
116                 [ "$(dlogctl -g | grep regular | grep -c ENABLED)" -eq 0 ] && ok || fail
117
118                 LOG_DETAILS="testing if filters were applied"
119                 dlogsend -b system -t TEST test
120                 dlogsend -b main -t TEST test
121                 dlogsend -b radio -t TEST test
122                 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
123
124                 LOG_DETAILS="dlogctl --enable (no args)"
125                 dlogctl --enable
126                 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
127                 LOG_DETAILS="testing if filters were applied"
128                 dlogutil -c -b radio -b system -b main
129                 dlogsend -b system -t TEST test
130                 dlogsend -b main -t TEST test
131                 dlogsend -b radio -t TEST test
132                 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
133
134                 LOG_DETAILS="dlogctl --disable (1 arg)"
135                 dlogctl --disable -b system
136                 [ "$(dlogctl -g | grep regular | grep    DISABLED | grep -c system)" -eq 1 ] &&
137                 [ "$(dlogctl -g | grep regular | grep -c DISABLED                 )" -eq 1 ] && ok || fail
138                 LOG_DETAILS="testing if filters were applied"
139                 dlogutil -c -b radio -b system -b main
140                 dlogsend -b system -t TEST test
141                 dlogsend -b main -t TEST test
142                 dlogsend -b radio -t TEST test
143                 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
144
145                 LOG_DETAILS="dlogctl --disable (multiple args)"
146                 dlogctl --disable -b main -b radio
147                 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -cv system)" -eq 2 ] &&
148                 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c   radio)" -eq 1 ] &&
149                 [ "$(dlogctl -g | grep regular | grep DISABLED | grep -c    main)" -eq 1 ] && ok || fail
150                 LOG_DETAILS="testing if filters were applied"
151                 dlogutil -c -b radio -b system -b main
152                 dlogsend -b system -t TEST test
153                 dlogsend -b main -t TEST test
154                 dlogsend -b radio -t TEST test
155                 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
156
157                 LOG_DETAILS="dlogctl --enable (multiple args)"
158                 dlogctl --enable -b radio -b system
159                 [ "$(dlogctl -g | grep regular | grep -c DISABLED                 )" -eq 1 ] &&
160                 [ "$(dlogctl -g | grep regular | grep    DISABLED | grep -c   main)" -eq 1 ] &&
161                 [ "$(dlogctl -g | grep regular | grep     ENABLED | grep -c system)" -eq 1 ] &&
162                 [ "$(dlogctl -g | grep regular | grep     ENABLED | grep -c  radio)" -eq 1 ] && ok || fail
163                 LOG_DETAILS="testing if filters were applied"
164                 dlogutil -c -b radio -b system -b main
165                 dlogsend -b system -t TEST test
166                 dlogsend -b main -t TEST test
167                 dlogsend -b radio -t TEST test
168                 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 2 ] && ok || fail
169
170                 LOG_DETAILS="dlogctl --enable (1 arg)"
171                 dlogctl --enable -b main
172                 [ "$(dlogctl -g | grep regular | grep -c DISABLED)" -eq 0 ] && ok || fail
173                 LOG_DETAILS="testing if filters were applied"
174                 dlogutil -c -b radio -b system -b main
175                 dlogsend -b system -t TEST test
176                 dlogsend -b main -t TEST test
177                 dlogsend -b radio -t TEST test
178                 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 3 ] && ok || fail
179
180                 LOG_DETAILS="dlogctl -s allow"
181                 dlogutil -c -b radio -b system -b main
182                 dlogctl -c
183                 dlogctl -s deny
184                 dlogctl -t TEST_TAG -s allow
185                 dlogsend -b main -t TEST_TAG test
186                 dlogsend -b main -t TEST test
187                 [ "$(dlogutil -d -b main | wc -l)" -eq 1 ] && ok || fail
188
189                 LOG_DETAILS="dlogctl -s [N]"
190                 dlogutil -c -b radio -b system -b main
191                 dlogctl -c
192                 dlogctl -s deny
193                 dlogctl -t TEST_TAG -s 3
194                 dlogsend -b main -c 10 -t TEST_TAG test
195                 dlogsend -b main -c 10 -t TEST test
196                 [ "$(dlogutil -d -b main | wc -l)" -eq 4 ] && ok || fail
197
198                 LOG_DETAILS="testing invalid parameters for dlogctl (1/13)"
199                 dlogctl -s invalid > /dev/null && fail || ok
200
201                 LOG_DETAILS="testing invalid parameters for dlogctl (2/13)"
202                 dlogctl -p X -g > /dev/null && fail || ok
203
204                 LOG_DETAILS="testing invalid parameters for dlogctl (3/13)"
205                 dlogctl -b invalid --disable > /dev/null && fail || ok
206
207                 LOG_DETAILS="testing invalid parameters for dlogctl (4/13)"
208                 dlogctl -s allow -g -t xyz -p E > /dev/null && fail || ok
209
210                 LOG_DETAILS="testing invalid parameters for dlogctl (5/13)"
211                 dlogctl -s allow -c > /dev/null && fail || ok
212
213                 LOG_DETAILS="testing invalid parameters for dlogctl (6/13)"
214                 dlogctl -c -g -t xyz -p E > /dev/null && fail || ok
215
216                 # -s out of range
217                 LOG_DETAILS="testing invalid parameters for dlogctl (7/13)"
218                 dlogctl -s -10 > /dev/null && fail || ok
219
220                 LOG_DETAILS="testing invalid parameters for dlogctl (8/13)"
221                 dlogctl -s 999999 > /dev/null && fail || ok
222
223                 # -s correctness
224                 LOG_DETAILS="testing invalid parameters for dlogctl (9/13)"
225                 dlogctl -s 100 && ok || fail
226
227                 LOG_DETAILS="testing invalid parameters for dlogctl (10/13)"
228                 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 1 ] && ok || fail
229
230                 # -g correctness
231                 LOG_DETAILS="testing invalid parameters for dlogctl (11/13)"
232                 [ "$(dlogctl -g | grep -F '*:*' | grep -c 100)" -eq 1 ] && ok || fail
233
234                 # -c correctness
235                 LOG_DETAILS="testing invalid parameters for dlogctl (12/13)"
236                 dlogctl -c -t '*' -p '*' && ok || fail
237
238                 LOG_DETAILS="testing invalid parameters for dlogctl (13/13)"
239                 [ "$(grep -cE 'limiter\|\*\|\*=100' "$RUNTIME_FILTERS_DIR"/05-logctl.conf)" -eq 0 ] && ok || fail
240                 dlogctl -c
241
242                 LOG_DETAILS="testing if the whole dynamic config directory is respected"
243                 dlogctl -s deny
244                 cp $RUNTIME_FILTERS_DIR/05-logctl.conf $RUNTIME_FILTERS_DIR/10-other.conf
245                 dlogctl -c
246                 dlogutil -c -b radio -b system -b main
247                 dlogsend -b system -t TEST test
248                 dlogsend -b main -t TEST test
249                 dlogsend -b radio -t TEST test
250                 [ "$(dlogutil -d -b radio -b system -b main | wc -l)" -eq 0 ] && ok || fail
251                 rm $RUNTIME_FILTERS_DIR/10-other.conf
252         fi
253 fi
254
255 SEED=$(head -c 6 /dev/urandom | base64)
256 dlogsend -b main -k "$SEED"
257 # -k is async, unlike normal mode; therefore we have to wait until the logging process actually finishes.
258 while pgrep "dlogsend" > /dev/null || pgrep "dlog-log-critical" > /dev/null; do :; done
259 CRIT_PATH="@DLOG_CRITICAL_LOGFILE_PATH@"
260 LOG_DETAILS="testing if critical logging works"
261 cat "$CRIT_PATH".a "$CRIT_PATH".b | grep -F "$SEED" > /dev/null && ok || fail
262
263 if [ "$type" = "pipe" ] && [ "$quick" -ne 1 ]; then
264         dlogsend -c 2 -d 2 -t DLOGSEND_SIGPIPE_TEST test &
265         DLOGSEND_PID=$!
266         sleep 1
267         kill -s PIPE $DLOGSEND_PID
268         wait $DLOGSEND_PID
269         LOG_DETAILS="testing if dlogsend logs even after SIGPIPE"
270         [ "$(dlogutil -d DLOGSEND_SIGPIPE_TEST | wc -l)" -eq 2 ] && ok || fail
271 fi
272
273 dlogutil -c
274
275 LOG_DETAILS="testing if dlogutil rejects dotnet"
276 dlogutil -db dotnet_api && fail || ok
277
278 LOG_DETAILS="testing if dlogutil rejects native"
279 dlogutil -db native_api && fail || ok
280
281 LOG_DETAILS="testing if dotnet redirects to regular apps (1/2)"
282 dlogsend -b dotnet_api "TEST"
283 sleep 0.25
284 [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
285
286 LOG_DETAILS="testing if dotnet redirects to regular apps (2/2)"
287 dlogsend -c 6 -b dotnet_api "TEST"
288 sleep 0.25
289 [ "$(dlogutil -d -b apps | wc -l)" -eq 7 ] && ok || fail
290
291 LOG_DETAILS="testing if native redirects to regular apps (1/2)"
292 dlogsend -b native_api "TEST"
293 sleep 0.25
294 [ "$(dlogutil -d -b apps | wc -l)" -eq 8 ] && ok || fail
295
296 LOG_DETAILS="testing if native redirects to regular apps (2/2)"
297 dlogsend -c 6 -b native_api "TEST"
298 sleep 0.25
299 [ "$(dlogutil -d -b apps | wc -l)" -eq 14 ] && ok || fail
300
301 if [ "$type" != "zero-copy" ]; then # These tests are inherently dynamic.
302         dlogutil -c
303         LOG_DETAILS="testing if blocking dotnet and regular apps is separate (1/4)"
304         dlogctl -b apps --enable
305         dlogctl -b dotnet_api --disable
306         dlogsend -b dotnet_api "TEST"
307         [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
308         LOG_DETAILS="testing if blocking dotnet and regular apps is separate (2/4)"
309         dlogsend -b apps "TEST"
310         [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
311         LOG_DETAILS="testing if blocking dotnet and regular apps is separate (3/4)"
312         dlogctl -b apps --disable
313         dlogctl -b dotnet_api --enable
314         dlogsend -b dotnet_api "TEST"
315         [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
316         LOG_DETAILS="testing if blocking dotnet and regular apps is separate (4/4)"
317         dlogsend -b apps "TEST"
318         [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
319
320
321         dlogutil -c
322         LOG_DETAILS="testing if blocking native and regular apps is separate (1/4)"
323         dlogctl -b apps --enable
324         dlogctl -b native_api --disable
325         dlogsend -b native_api "TEST"
326         [ "$(dlogutil -d -b apps | wc -l)" -eq 0 ] && ok || fail
327         LOG_DETAILS="testing if blocking native and regular apps is separate (2/4)"
328         dlogsend -b apps "TEST"
329         [ "$(dlogutil -d -b apps | wc -l)" -eq 1 ] && ok || fail
330         LOG_DETAILS="testing if blocking native and regular apps is separate (3/4)"
331         dlogctl -b apps --disable
332         dlogctl -b native_api --enable
333         dlogsend -b native_api "TEST"
334         [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
335         LOG_DETAILS="testing if blocking native and regular apps is separate (4/4)"
336         dlogsend -b apps "TEST"
337         [ "$(dlogutil -d -b apps | wc -l)" -eq 2 ] && ok || fail
338 fi
339
340 # put 100 log entries in the "main" buffer
341 dlogutil -c
342 test_libdlog 100
343 sleep 1
344
345
346 LOG_DETAILS="testing if dlogutil -d exits with success after printing logs"
347 dlogutil -d > /dev/null && ok || fail
348
349 LOG_DETAILS="testing if -t argument is parsed properly"
350 dlogutil -t dummy > /dev/null && fail || ok
351
352 LOG_DETAILS="testing if -u argument is parsed properly"
353 dlogutil -du dummy > /dev/null && fail || ok
354
355 LOG_DETAILS="testing if limiting printed log entries to less than exists in the buffer returns proper value"
356 [ "$(dlogutil -b main -t  20 | wc -l)" -eq  20 ] && ok || fail # less logs than currently in buffer
357
358 LOG_DETAILS="testing if limiting printed log entries to more than exists in the buffer returns proper value"
359 [ "$(dlogutil -b main -t 200 | wc -l)" -eq 100 ] && ok || fail # more
360
361 LOG_DETAILS="testing if dlogutil returns exact amount of entries as there is in the buffer"
362 [ "$(dlogutil -b main -d     | wc -l)" -eq 100 ] && ok || fail # exactly
363
364 LOG_DETAILS="testing if reading from  dummy buffer returns an error as expected"
365 dlogutil -b nonexistent_buffer > /dev/null && fail || ok
366
367 if [ "$type" != "zero-copy" ]; then # No buffer split in zero-copy
368         LOG_DETAILS="testing if reading from \"system\" buffer returns zero entries (logs are in the \"main\" buffer)"
369         [ "$(dlogutil -d -b system | wc -l)" -eq 0 ] && ok || fail
370 fi
371
372 LOG_DETAILS="testing if dlogutil -c empties all buffers"
373 dlogutil -cb main && ok || fail
374
375 LOG_DETAILS="testing if writing entries to empty buffer and reading them returns proper amount of entries"
376 test_libdlog 10
377 [ "$(dlogutil -b main -d | wc -l)" -eq 10 ] && ok || fail # should be 10, not 110
378
379 LOG_DETAILS="testing if filters work (1/3)"
380 [ "$(dlogutil -b main -d '*:E' | wc -l)" -eq 5 ] && ok || fail # half of current logs (test_libdlog alternates between error and info log levels)
381
382 LOG_DETAILS="testing if filters work (2/3)"
383 [ "$(dlogutil -b main -d '*:W' | wc -l)" -eq 5 ] && ok || fail
384
385 LOG_DETAILS="testing if filters work (3/3)"
386 [ "$(dlogutil -b main -d '*:I' | wc -l)" -eq 10 ] && ok || fail
387
388 LOG_DETAILS="testing if exact filters work (1/3)"
389 [ "$(dlogutil -b main -d '*:=E' | wc -l)" -eq 5 ] && ok || fail
390
391 LOG_DETAILS="testing if exact filters work (2/3)"
392 [ "$(dlogutil -b main -d '*:=W' | wc -l)" -eq 0 ] && ok || fail
393
394 LOG_DETAILS="testing if exact filters work (3/3)"
395 [ "$(dlogutil -b main -d '*:=I' | wc -l)" -eq 5 ] && ok || fail
396
397 LOG_DETAILS="testing if adding \"silent\" filter works"
398 [ "$(dlogutil -b main -s -d | wc -l)" -eq 0 ] && ok || fail
399
400 if [ "$type" != "zero-copy" ]; then # No buffer size access in zero-copy
401         LOG_DETAILS="testing if reading buffer size returns proper exit code"
402         dlogutil -gb main > /dev/null && ok || fail
403 fi
404
405 LOG_DETAILS="testing if writing all entries to single file works (-f)"
406 dlogutil -f $TESTDIR/dlog_test_file -d > /dev/null && ok || fail
407
408 if [ "$quick" -ne 1 ]; then
409         LOG_DETAILS="testing if the continuous mode works as expected (1/2)"
410         dlogutil -b main -f $TESTDIR/dlog_continuous1_file &
411         UTIL_PID=$!
412         sleep 1
413         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
414         UTIL_PID=-1
415         [ "$WAS_ALIVE" -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous1_file)" -eq 10 ] && ok || fail
416
417         LOG_DETAILS="testing if the continuous mode works as expected (2/2)"
418         dlogutil -b main -f $TESTDIR/dlog_continuous2_file &
419         UTIL_PID=$!
420         sleep 1
421         dlogsend -b main "Hi!"
422         sleep 1
423         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
424         UTIL_PID=-1
425         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_continuous2_file)" -eq 11 ] && ok || fail
426
427         LOG_DETAILS="testing if the monitor mode works as expected (1/2)"
428         dlogutil --monitor -b main -f $TESTDIR/dlog_monitor1_file &
429         UTIL_PID=$!
430         sleep 1
431         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
432         UTIL_PID=-1
433         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor1_file)" -eq 0 ] && ok || fail
434
435         LOG_DETAILS="testing if the monitor mode works as expected (2/2)"
436         dlogutil --monitor -b main -f $TESTDIR/dlog_monitor2_file &
437         UTIL_PID=$!
438         sleep 1
439         dlogsend -b main "Hi!"
440         sleep 1
441         kill $UTIL_PID && WAS_ALIVE=1 || WAS_ALIVE=0
442         UTIL_PID=-1
443         [ $WAS_ALIVE -eq 1 ] && [ "$(wc -l < "$TESTDIR"/dlog_monitor2_file)" -eq 1 ] && ok || fail
444 fi
445
446 dlogutil -cb main
447 test_libdlog 100000 &
448 LIBDLOGUTIL_CORRECT_PID=$!
449 wait $LIBDLOGUTIL_CORRECT_PID
450
451 # NB: rotation only really makes sense in the continuous mode
452 # from a user PoV, but the logic doesn't care about dumping
453 # so we do it to simplify things
454 dlogutil -df $TESTDIR/dlog_rotating_file -r 12 -n 3 # 3 files at 12 KB each
455
456 LOG_DETAILS="testing if single file is properly created"
457 if [ -e $TESTDIR/dlog_test_file ]; then ok; else fail; fi
458
459 LOG_DETAILS="testing if rotating file is properly created (1/4)"
460 if [ -e $TESTDIR/dlog_rotating_file.1 ]; then ok; else fail; fi
461
462 LOG_DETAILS="testing if rotating file is properly created (2/4)"
463 if [ -e $TESTDIR/dlog_rotating_file.2 ]; then ok; else fail; fi
464
465 LOG_DETAILS="testing if rotating file is properly created (3/4)"
466 if [ -e $TESTDIR/dlog_rotating_file.3 ]; then ok; else fail; fi
467
468 LOG_DETAILS="testing if rotating file is properly created (4/4)"
469 if [ -e $TESTDIR/dlog_rotating_file.4 ]; then fail; else ok; fi
470
471 LOG_DETAILS="testing the size of log files"
472 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
473
474 LOG_DETAILS="testing if libdlogutil works correctly in the dump mode"
475 test_libdlogutil dump $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
476 if [ "$quick" -ne 1 ]; then
477         LOG_DETAILS="testing if libdlogutil works correctly if processing the logs takes some time"
478         test_libdlogutil timer $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
479 fi
480 LOG_DETAILS="testing if libdlogutil works correctly in the continuous mode"
481 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
482 LOG_DETAILS="testing if libdlogutil skips all the logs in the monitor mode correctly"
483 test_libdlogutil monitor $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
484 LOG_DETAILS="testing if libdlogutil priority filter works correctly"
485 test_libdlogutil priority $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
486 LOG_DETAILS="testing if libdlogutil exact priority filter works correctly"
487 test_libdlogutil priority_exact $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
488 LOG_DETAILS="testing if libdlogutil limits the log amount correctly"
489 test_libdlogutil limit $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
490 LOG_DETAILS="testing if libdlogutil PID filter works correctly (1/2)"
491 test_libdlogutil pid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
492 LOG_DETAILS="testing if libdlogutil PID filter works correctly (2/2)"
493 test_libdlogutil pid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
494 LOG_DETAILS="testing if libdlogutil TID filter works correctly (1/2)"
495 test_libdlogutil tid_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
496 LOG_DETAILS="testing if libdlogutil TID filter works correctly (2/2)"
497 test_libdlogutil tid_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
498 LOG_DETAILS="testing if libdlogutil tag filter works correctly (1/2)"
499 test_libdlogutil tag_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
500 LOG_DETAILS="testing if libdlogutil tag filter works correctly (2/2)"
501 test_libdlogutil tag_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
502 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (1/2)"
503 test_libdlogutil prefix_correct $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
504 LOG_DETAILS="testing if libdlogutil prefix filter works correctly (2/2)"
505 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
506 if [ "$type" != "zero-copy" ]; then # These rely on things not available in zero-copy
507         LOG_DETAILS="testing if libdlogutil returns the correct buffer traits"
508         test_libdlogutil traits $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
509         LOG_DETAILS="testing if libdlogutil aliasing works"
510         if [ "$type" = "pipe" ]; then
511                 test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
512         else
513                 DLOG_CONFIG_PATH="@datadir@/dlog-logger.conf.alias" test_libdlogutil alias $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
514         fi
515 fi
516 LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
517 test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
518
519 cmd_prefix_no_v="dlogutil -t 1 -u 0"
520 cmd_prefix="$cmd_prefix_no_v -v"
521
522 format="process"
523 regex_prio="[VDIWEFS]{1}"
524 regex_pidtid="P[0-9[:space:]]{5,},\s{1}T[0-9[:space:]]{5,}"
525 regex_time="[0-9]{2}-[0-9]{2}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}"
526 regex_timezone="[\+-]{1}[0-9]{4}"
527
528 for PARAM in always auto none never; do
529         for PRIO in info error; do
530                 for OUTPUT in pipe tty; do
531                         if [ "$PARAM" = "always" ]; then
532                                 COLOR_EXPECTED=1
533                         elif [ "$PARAM" = "never" ]; then
534                                 COLOR_EXPECTED=0
535                         elif [ "$OUTPUT" = "tty" ]; then
536                                 COLOR_EXPECTED=1
537                         else
538                                 COLOR_EXPECTED=0
539                         fi
540
541                         if [ "$PRIO" = "info" ]; then
542                                 REGEX="s/^I\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
543                         elif [ "$COLOR_EXPECTED" -eq 0 ]; then
544                                 REGEX="s/^E\([0-9[:space:]]{5,}\)[[:print:]]*\([[:print:]]*\)@$/1/g"
545                         else
546                                 REGEX="s/^~\[31;1mE\([0-9[:space:]]{5,}\) ~\[0m[[:print:]]*~\[31;1m  \([[:print:]]*\)@~\[0m$/1/g"
547                         fi
548
549                         # -t 1 instead of | head -n 1 because the `script` command can't cope with SIGPIPE.
550                         COMMAND="$cmd_prefix $format"
551
552                         if [ "$PARAM" = "none" ]; then
553                                 LOG_DETAILS="testing if color output is correct (implicit --color=auto/$PRIO priority/$OUTPUT output)"
554                         else
555                                 COMMAND="$COMMAND --color=$PARAM"
556                                 LOG_DETAILS="testing if color output is correct (--color=$PARAM/$PRIO priority/$OUTPUT output)"
557                         fi
558
559                         if [ "$PRIO" = "info" ]; then
560                                 COMMAND="$COMMAND '*:=I'"
561                         else
562                                 COMMAND="$COMMAND '*:=E'"
563                         fi
564
565                         if [ "$OUTPUT" = "tty" ]; then
566                                 # This emulates a TTY. The sed call is because `script` likes to mess up the output for some reason.
567                                 COMMAND="script -qc\"$COMMAND\" /dev/null | sed 's/\r$//'"
568                         fi
569
570                         # $(eval) is needed since `$COMMAND` may contain `"`
571                         line="$(eval "$COMMAND" | tr '\033\n' '~@')"
572                         [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
573                 done
574         done
575 done
576 # TODO: It would be cool to also test warning messages (which have a different color),
577 # but this would require deeper changes to this script, which is already janky enough.
578
579 REGEX="s/^$regex_prio\([0-9[:space:]]{5,}\)[[:print:]]*\([[: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="tag"
585 REGEX="s/^$regex_prio\/[[:print:]]{9,}:\s{1}[[:print:]]*$/1/g"
586 LOG_DETAILS="testing if \"$format\" print format works"
587 line=$($cmd_prefix $format)
588 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
589
590 format="thread"
591 REGEX="s/^$regex_prio\($regex_pidtid\)\s{1}[[:print:]]*$/1/g"
592 LOG_DETAILS="testing if \"$format\" print format works"
593 line=$($cmd_prefix $format)
594 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
595
596 format="time"
597 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
598 LOG_DETAILS="testing if \"$format\" print format works"
599 line=$($cmd_prefix $format)
600 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
601
602 format="threadtime"
603 REGEX="s/^$regex_time.[0-9]{3}$regex_timezone\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
604 LOG_DETAILS="testing if \"$format\" print format works"
605 line=$($cmd_prefix $format)
606 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
607
608 format="kerneltime"
609 REGEX="s/^[0-9[:space:]]{1,}.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
610 LOG_DETAILS="testing if \"$format\" print format works"
611 line=$($cmd_prefix $format)
612 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
613
614 format="recv_realtime"
615 REGEX="s/^$regex_time.[0-9]{3}\s{1}$regex_prio\/[[:print:]]{8,}\($regex_pidtid\):\s{1}[[:print:]]*$/1/g"
616 LOG_DETAILS="testing if \"$format\" print format works"
617 line=$($cmd_prefix $format)
618 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
619
620 format="rwtime"
621 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"
622 LOG_DETAILS="testing if \"$format\" print format works"
623 line=$($cmd_prefix $format)
624 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
625
626 format="long"
627 REGEX="s/^\[\s{1}$regex_time.[0-9]{3,}\s{1}$regex_prio\/[[:print:]]{8,}\s{1}$regex_pidtid\]@[[:print:]]+@$/1/g"
628 LOG_DETAILS="testing if \"$format\" print format works"
629 line=$($cmd_prefix $format)
630 [ "$(echo "$line" | tr '\n' '@' | sed -re "$REGEX")" = "1" ] && ok || fail
631
632 format="brief"
633 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
634 LOG_DETAILS="testing if \"$format\" print format works"
635 line=$($cmd_prefix $format)
636 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
637
638 format="json"
639 if [ "$type" = "pipe" ]; then
640         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_real_ns\":[1-9][0-9]*,\"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_real_ns\":[1-9][0-9]*,\"sent_mono\":[1-9][0-9]+\.[0-9]{9},\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
641 elif [ "$type" = "logger" ]; then
642         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\",\"sent_real_ns\":[1-9][0-9]*,\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
643 else # zero-copy
644         REGEX="s/^\{\"priority\":\"(verbose|debug|info|warning|error|fatal|silent)\",\"pid\":[1-9][0-9]*,\"tid\":[1-9][0-9]*,\"sent_mono\":[1-9][0-9]+\.[0-9]{9},\"tag\":\"[[:print:]]*\",\"msg\":\"[[:print:]]*\"\}$/1/g"
645 fi
646 LOG_DETAILS="testing if \"$format\" print format works"
647 line=$($cmd_prefix $format)
648 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
649
650 SOURCE="$DLOG_CONFIG_PATH"
651 DLOG_CONFIG_PATH="$TESTDIR/defaultprintformat.conf"
652 cp "$SOURCE" "$DLOG_CONFIG_PATH"
653
654 # TODO: It would be good to also test the daemon side of things.
655 LOG_DETAILS="testing default log format selection (none -> brief)"
656 REGEX="s/^$regex_prio\/[[:print:]]{8,}\([0-9[:space:]]{5,}\):\s{1}[[:print:]]*$/1/g"
657 line=$($cmd_prefix_no_v)
658 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
659
660 echo "util_default_format=rwtime" >> $DLOG_CONFIG_PATH
661 LOG_DETAILS="testing default log format selection (rwtime)"
662 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"
663 line=$($cmd_prefix_no_v)
664 [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
665
666 DLOG_CONFIG_PATH="$SOURCE"
667
668 format="raw"
669 LOG_DETAILS="testing if \"$format\" print format works"
670 dlogutil -c
671 dlogsend -b main -t DLOG_TESTSUITE rawformatTEST
672 line=$($cmd_prefix $format)
673 [ "$line" = "rawformatTEST" ] && ok || fail
674
675 if [ "$quick" -ne 1 ]; then
676         LOG_DETAILS="testing if dlogsend -d works"
677         dlogutil -c
678         dlogsend -b main -c 9999 -d 4 -f 7 -t DLOG_TESTSUITE "hi" &
679         DLOGSEND_PID=$!
680         sleep 10
681         kill $DLOGSEND_PID
682         # In 10 seconds, 3 cycles 7 logs each should have happened.
683         [ "$(dlogutil -d DLOG_TESTSUITE | wc -l)" -eq 21 ] && ok || fail
684 fi
685
686 if [ "$type" = "pipe" ]; then
687         for format in raw brief long; do
688                 for i in 1 2 3 4 5 6 7 8 10 15 20 25 513 514 515 516 517 518 519 520; do
689                         LOG_DETAILS="testing if padding works correctly (format $format/count $i)"
690                         log=$(printf '%0.s&' $(seq 1 $i)) # Print the '&' character $i times
691
692                         dlogutil -c
693                         dlogsend -b main "$log"
694                         line=$($cmd_prefix $format | tr '\n' '@')
695
696                         case $format in
697                                 raw)
698                                         REGEX="s/$log@/1/g"
699                                         ;;
700                                 brief)
701                                         REGEX="s/I\/DLOG_SEND\([0-9[:space:]]{5,}\):\s{1}$log@$/1/g"
702                                         ;;
703                                 long)
704                                         REGEX="s/\[\s{1}$regex_time.[0-9]{3,}\s{1}I\/DLOG_SEND\s{1}$regex_pidtid\]@$log@@/1/g"
705                                         ;;
706                         esac
707
708                         [ "$(echo "$line" | sed -re "$REGEX")" = "1" ] && ok || fail
709                 done
710         done
711 fi
712
713 if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
714         LOG_DETAILS="testing if KMSG works in the default format"
715         # We check if the command returns at least 100 logs. This seems to be a safe value.
716         # Feel free to change it to a reasonable yet lower value if the test happens to fail otherwise.
717         [ "$(dlogutil -db kmsg | wc -l)" -gt 100 ] && ok || fail
718 fi
719
720 if [ "$quick" -ne 1 ]; then
721         if [ "$type" != "zero-copy" ]; then # No KMSG in zero-copy
722                 LOG_DETAILS="testing if KMSG works in the raw format"
723                 last_dmesg=$(dmesg | tail -n1 | sed -re "s/^\[[ 0-9]{5,}\.[0-9]{6}\] (.*)$/\1/g")
724                 sleep 1 # To make sure dlog has already parsed the log
725                 dlogutil -db kmsg -v raw | grep -Fm1 "$last_dmesg" >/dev/null && ok || fail
726         fi
727
728         dlog_test_tid_filtering_secure_logging $type
729 fi
730
731 dlog_logger -t 0 &
732 LOGGER=$!
733 sleep 1
734
735 dlogsend -b main -t DLOG_TESTSUITE_TAG0 -pI tagTEST0
736 dlogsend -b main -t DLOG_TESTSUITE_TAG1 -pI tagTEST1
737 dlogsend -b main -t DLOG_TESTSUITE_TAG2 -pF tagTEST2
738
739 LOG_DETAILS="testing if tag filtering works (1/8)"
740 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG0' | wc -l)" -eq 1 ] && ok || fail
741 LOG_DETAILS="testing if tag filtering works (2/8)"
742 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG1' | wc -l)" -eq 1 ] && ok || fail
743 LOG_DETAILS="testing if tag filtering works (3/8)"
744 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG' | wc -l)" -eq 0 ] && ok || fail
745 LOG_DETAILS="testing if tag filtering works (4/8)"
746 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*' | wc -l)" -eq 3 ] && ok || fail
747 LOG_DETAILS="testing if tag filtering works (5/8)"
748 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:I' | wc -l)" -eq 3 ] && ok || fail
749 LOG_DETAILS="testing if tag filtering works (6/8)"
750 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_TAG*:W' | wc -l)" -eq 1 ] && ok || fail
751 LOG_DETAILS="testing if tag filtering works (7/8)"
752 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG' | wc -l)" -eq 0 ] && ok || fail
753 LOG_DETAILS="testing if tag filtering works (8/8)"
754 [ "$(dlogutil -dv raw 'DLOG_TESTSUITE_SAMSUNG*' | wc -l)" -eq 0 ] && ok || fail
755
756 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
757         LOG_DETAILS="testing if limiter and runtime filtering works"
758         dlogutil -c -b radio
759         test_filters
760         [ "$(dlogutil -d -b radio | wc -l)" -eq 12 ] && ok || fail
761         dlogutil -c -b radio
762
763         LOG_DETAILS="testing proper SMACK label for dynamic config file location"
764         ORIG_FILTERS_DIR=$(grep dynamic_config_path "$ORIGINAL_CONFIG_PATH" | awk -F "=" '{print $2}')
765         # Running ls on an arbitrary directory and trying to make sense of results is problematic.
766         # However, this is not a problem in our usecase, as we only care about the first field, the security context.
767         # We don't even care about the filename, which isn't arbitrary anyway (-d on fixed path).
768         # shellcheck disable=SC2012
769         [ "$(ls -dZ "$ORIG_FILTERS_DIR" | awk -F ' ' '{print $1}')" = "System::Shared" ] && ok || fail
770
771         if [ "$quick" -ne 1 ]; then
772                 # This creates a process which will wait for SIGCONT and then spam logs. We know its PID, so we can add
773                 # PID-based limits and see what happens.
774                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
775                 DLOGSEND_PID=$!
776                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
777                 dlogctl --pid $DLOGSEND_PID -s deny
778                 kill -s CONT $DLOGSEND_PID
779                 wait $DLOGSEND_PID
780                 LOG_DETAILS="testing if PID limiting works (1/14)"
781                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 0 ] && ok || fail
782                 LOG_DETAILS="testing if PID limiting works (2/14)"
783                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 1 ] && ok || fail
784                 LOG_DETAILS="testing if PID limiting works (3/14)"
785                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = "Denied" ] && ok || fail
786                 LOG_DETAILS="testing if PID limiting works (4/14)"
787                 dlogctl -g | grep -q "Denied for $DLOGSEND_PID" && ok || fail
788
789                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
790                 DLOGSEND_PID=$!
791                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
792                 dlogctl --pid $DLOGSEND_PID -s 25
793                 kill -s CONT $DLOGSEND_PID
794                 wait $DLOGSEND_PID
795                 LOG_DETAILS="testing if PID limiting works (5/14)"
796                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 25 ] && ok || fail
797                 LOG_DETAILS="testing if PID limiting works (6/14)"
798                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 1 ] && ok || fail
799                 LOG_DETAILS="testing if PID limiting works (7/14)"
800                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = '25 logs/min' ] && ok || fail
801                 LOG_DETAILS="testing if PID limiting works (8/14)"
802                 dlogctl -g | grep -q "25 logs/min for $DLOGSEND_PID" && ok || fail
803
804                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 100 "Pid test"' &
805                 DLOGSEND_PID=$!
806                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
807                 dlogctl --pid $DLOGSEND_PID -s allow
808                 kill -s CONT $DLOGSEND_PID
809                 wait $DLOGSEND_PID
810                 LOG_DETAILS="testing if PID limiting works (9/14)"
811                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 100 ] && ok || fail
812                 LOG_DETAILS="testing if PID limiting works (10/14)"
813                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 0 ] && ok || fail
814                 LOG_DETAILS="testing if PID limiting works (11/14)"
815                 [ "$(dlogctl --pid $DLOGSEND_PID -g)" = 'Unlimited' ] && ok || fail
816                 LOG_DETAILS="testing if PID limiting works (12/14)"
817                 dlogctl -g | grep -q "Unlimited for $DLOGSEND_PID" && ok || fail
818
819                 echo "qos_refresh_rate_s=5" > "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
820                 sh -c 'kill -s STOP $$; exec dlogsend -b main -c 15 -d 1 "Pid test"' &
821                 DLOGSEND_PID=$!
822                 while [ "$(cut -d ' ' -f 3 < /proc/$DLOGSEND_PID/stat)" != "T" ]; do :; done
823                 dlogctl --pid $DLOGSEND_PID -s 2
824                 kill -s CONT $DLOGSEND_PID
825                 wait $DLOGSEND_PID
826                 rm "$RUNTIME_FILTERS_DIR/69-refresh-rate.conf"
827                 LOG_DETAILS="testing if PID limiting works (13/14)"
828                 # In each 5 second period, 5 logs are sent, so there are 3 periods.
829                 # 2 logs from each period are allowed.
830                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -cv blocked)" -eq 6 ] && ok || fail
831                 LOG_DETAILS="testing if PID limiting works (14/14)"
832                 [ "$(dlogutil --pid $DLOGSEND_PID -d | grep -c  blocked)" -eq 3 ] && ok || fail
833
834                 PREQOS_CONFIG_PATH=$DLOG_CONFIG_PATH
835                 DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.qos"
836                 kill $LOGGER > /dev/null
837                 sleep 1
838                 if [ "$type" = "logger" ]; then
839                         dlogutil -cb main
840                 fi
841                 dlog_logger -t 0 &
842                 LOGGER=$!
843                 sleep 1
844
845                 dlogsend -b main -t DLOG_QOS_TEST0 -c 30 -d 1 -f 2 "A normal app that just wants to log"
846                 LOG_DETAILS="testing if QoS works (1/4)"
847                 [ "$(dlogutil -d DLOG_QOS_TEST0 | grep -cv blocked)" -eq 30 ] && ok || fail
848                 kill $LOGGER > /dev/null
849                 sleep 1
850                 if [ "$type" = "logger" ]; then
851                         dlogutil -cb main
852                 fi
853                 dlog_logger -t 0 &
854                 LOGGER=$!
855                 sleep 1
856
857                 dlogsend -b main -t DLOG_QOS_TEST1 -c 300 -d 1 -f 20 "Some log spam"
858                 LOG_DETAILS="testing if QoS works (2/4)"
859                 [ "$(dlogutil -d DLOG_QOS_TEST1 | grep -cv blocked)" -le 210 ] && ok || fail
860                 kill $LOGGER > /dev/null
861                 sleep 1
862                 if [ "$type" = "logger" ]; then
863                         dlogutil -cb main
864                 fi
865                 dlog_logger -t 0 &
866                 LOGGER=$!
867                 sleep 1
868
869                 dlogsend -b main -t DLOG_QOS_TEST2 -c 30 -d 1 -f 2 "A normal app that just wants to log" &
870                 DLOGSEND_PID=$!
871                 dlogsend -b main -t DLOG_QOS_TEST3 -c 270 -d 1 -f 18 "Some log spam"
872                 wait $DLOGSEND_PID
873                 LOG_DETAILS="testing if QoS works (3/4)"
874                 [ "$(dlogutil -d DLOG_QOS_TEST2 DLOG_QOS_TEST3 | grep -cv blocked)" -le 210 ] && ok || fail
875                 LOG_DETAILS="testing if QoS works (4/4)"
876                 [ "$(dlogutil -d DLOG_QOS_TEST2 | grep -cv blocked)" -gt 24 ] && ok || fail
877
878                 DLOG_CONFIG_PATH=$PREQOS_CONFIG_PATH
879                 kill $LOGGER > /dev/null
880                 sleep 1
881                 dlog_logger -t 0 &
882                 LOGGER=$!
883                 sleep 1
884
885                 # limiter test (the feature allows to enable/disable particular log type)
886                 dlogsend -c 11 -d 1 -t QWE blabla &
887
888                 sleep 0.5
889                 dlogctl -t QWE -s deny
890                 LOG_DETAILS="testing if log limiter works (deny) (1/3)"
891                 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
892                 sleep 6
893                 LOG_DETAILS="testing if log limiter works (deny) (2/3)"
894                 [ "$(dlogutil -d QWE | wc -l)" -eq 1 ] && ok || fail
895
896                 dlogctl -t QWE -s allow
897                 sleep 4
898                 LOG_DETAILS="testing if log limiter works (allow) (3/3)"
899                 [ "$(dlogutil -d QWE | wc -l)" -eq 5 ] && ok || fail
900
901                 dlogutil -c
902         fi
903 fi
904
905 if [ "$LEGACY_LOGGER" -ne 1 ] && [ "$quick" -ne 1 ]; then
906         dlogutil -cb main
907
908         LOG_DETAILS="testing if basic stdout redirection works correctly (1/2)"
909         [ "$(dlog_redirect_stdout --outtag FOO -- /usr/bin/echo Hi)" = "" ] && ok || fail
910         sleep 1
911         LOG_DETAILS="testing if basic stdout redirection works correctly (2/2)"
912         [ "$(dlogutil -db main -v tag)" = "I/FOO     : Hi" ] && ok || fail
913         dlogutil -cb main
914
915         # No need to check output in this case, as nothing is written to stdout anyway
916         dlog_redirect_stdout --errtag BAR -- /usr/bin/sh -c "echo Hi >&2"
917         sleep 1
918         LOG_DETAILS="testing if basic stderr redirection works correctly"
919         [ "$(dlogutil -db main -v tag)" = "E/BAR     : Hi" ] && ok || fail
920         dlogutil -cb main
921
922         LOG_DETAILS="testing if redirector doesn't redirect anything by default (1/2)"
923         [ "$(dlog_redirect_stdout -- /usr/bin/echo Hi)" = "Hi" ] && ok || fail
924         sleep 1
925         LOG_DETAILS="testing if redirector doesn't redirect anything by default (2/2)"
926         [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
927         dlogutil -cb main
928
929         if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
930                 dlog_redirect_stdout --outbuffer radio --outtag AN_INTERESTING_TAG --outprio W -- /usr/bin/echo Hi
931                 sleep 1
932                 LOG_DETAILS="testing if redirector settings work correctly on stdout (1/2)"
933                 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
934                 LOG_DETAILS="testing if redirector settings work correctly on stdout (2/2)"
935                 [ "$(dlogutil -db radio -v tag)" = "W/AN_INTERESTING_TAG: Hi" ] && ok || fail
936                 dlogutil -cb radio
937
938                 dlog_redirect_stdout --errbuffer radio --errtag ANOTHER_INTERESTING_TAG --errprio I -- /usr/bin/sh -c "echo Hi >&2"
939                 sleep 1
940                 LOG_DETAILS="testing if redirector settings work correctly on stderr (1/2)"
941                 [ "$(dlogutil -db main -v tag)" = "" ] && ok || fail
942                 LOG_DETAILS="testing if redirector settings work correctly on stderr (2/2)"
943                 [ "$(dlogutil -db radio -v tag)" = "I/ANOTHER_INTERESTING_TAG: Hi" ] && ok || fail
944                 dlogutil -cb radio
945         fi
946
947         echo "Testing" | dlog_redirect_stdout --outtag BAZ -- /usr/bin/cat
948         sleep 1
949         LOG_DETAILS="testing if redirector passes stdin correctly"
950         [ "$(dlogutil -db main -v tag)" = "I/BAZ     : Testing" ] && ok || fail
951         dlogutil -cb main
952
953         dlog_redirect_stdout --outtag QUUX -- /usr/bin/cat << EOF
954 line 1
955 line 2
956 line 3
957 line 4
958 line 5
959 EOF
960         sleep 1
961         LOG_DETAILS="testing if multiline logs are handled correctly"
962         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
963         dlogutil -cb main
964
965         dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\0line 3\0line 4\0line 5\""
966         sleep 1
967         LOG_DETAILS="testing if logs with the '\\0' character are handled correctly"
968         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
969         dlogutil -cb main
970
971         dlog_redirect_stdout --outtag QUUX -- /usr/bin/sh -c "printf \"line 1\0line 2\nline 3\0line 4\nline 5\""
972         sleep 1
973         LOG_DETAILS="testing if logs with mixed delimiters are handled correctly"
974         [ "$(dlogutil -db main | wc -l)" -eq 5 ] && ok || fail
975         dlogutil -cb main
976
977         rm -f "$TESTDIR"/input
978         rm -f "$TESTDIR"/output
979         base64 -w 0 < /dev/urandom 2> /dev/null | head -c 250000 > "$TESTDIR"/input
980         dlog_redirect_stdout --outtag XYZZY -- /usr/bin/cat "$TESTDIR"/input
981         sleep 1
982         dlogutil -db main -v raw | tr -d '\n' > "$TESTDIR"/output
983         LOG_DETAILS="testing if long lines are handled correctly"
984         # Usually we would just diff or cmp the files. Unfortunately, as of writing, Tizen doesn't have such luxuries.
985         [ "$(sha256sum < "$TESTDIR"/input)" = "$(sha256sum < "$TESTDIR"/output)" ] && ok || fail
986         dlogutil -cb main
987
988         if [ "$type" = "pipe" ]; then
989                 # TODO: Actually implement and enable this in the logger backend
990                 dlogctl -b main --disable-stdout
991                 LOG_DETAILS="testing if redirection can be disabled (1/3)"
992                 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
993                 sleep 1
994                 LOG_DETAILS="testing if redirection can be disabled (2/3)"
995                 [ "$(dlogutil -db main -v raw)" = "" ] && ok || fail
996                 LOG_DETAILS="testing if redirection can be disabled (3/3)"
997                 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
998
999                 dlogctl -b main --enable-stdout
1000                 LOG_DETAILS="testing if redirection can be reenabled (1/3)"
1001                 [ "$(dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test)" = "" ] && ok || fail
1002                 sleep 1
1003                 LOG_DETAILS="testing if redirection can be reenabled (2/3)"
1004                 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1005                 LOG_DETAILS="testing if redirection can be reenabled (3/3)"
1006                 [ "$(dlogctl --get | grep 'main (regular)')" = "* main (regular): ENABLED" ] && ok || fail
1007                 dlogutil -cb main
1008         fi
1009
1010         if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1011                 dlogctl -b radio --disable-stdout
1012                 dlog_redirect_stdout --outtag THETAG -- /usr/bin/echo test
1013                 sleep 1
1014                 LOG_DETAILS="testing if dlogctl --disable-stdout respects buffer choice"
1015                 [ "$(dlogutil -db main -v raw)" = "test" ] && ok || fail
1016                 dlogutil -cb main
1017         fi
1018
1019         LOG_DETAILS="testing if redirection check works (1/5)"
1020         test_libredirect 0 0 && ok || fail
1021         LOG_DETAILS="testing if redirection check works (2/5)"
1022         dlog_redirect_stdout -- /usr/libexec/libdlog/test_libredirect 0 0 && ok || fail
1023         LOG_DETAILS="testing if redirection check works (3/5)"
1024         dlog_redirect_stdout --outtag X -- /usr/libexec/libdlog/test_libredirect 1 0 && ok || fail
1025         LOG_DETAILS="testing if redirection check works (4/5)"
1026         dlog_redirect_stdout --errtag X -- /usr/libexec/libdlog/test_libredirect 0 1 && ok || fail
1027         LOG_DETAILS="testing if redirection check works (5/5)"
1028         dlog_redirect_stdout --outtag X --errtag Y -- /usr/libexec/libdlog/test_libredirect 1 1 && ok || fail
1029
1030         if [ "$type" != "zero-copy" ]; then # No buffers in zero-copy
1031                 LOG_DETAILS="testing if multiple redirections work (1/4)"
1032                 test_libredirect_multi && ok || fail
1033                 LOG_DETAILS="testing if multiple redirections work (2/4)"
1034                 [ "$(dlogutil -db main -v tag | grep "Message 1")" = "W/TAG1    : Message 1" ] && ok || fail
1035                 LOG_DETAILS="testing if multiple redirections work (3/4)"
1036                 [ "$(dlogutil -db main -v tag | grep "Message 2")" = "E/TAG2    : Message 2" ] && ok || fail
1037                 LOG_DETAILS="testing if multiple redirections work (4/4)"
1038                 [ "$(dlogutil -db system -v tag)" = "I/TAG3    : Message 3" ] && ok || fail
1039                 dlogutil -cb main -b system
1040         fi
1041 fi
1042
1043 if [ "$type" != "zero-copy" ]; then # No dynamic filters in zero-copy
1044         dlogutil -c
1045         echo "deduplicate_method=only_identical_neighbours" > "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1046         echo "deduplicate_interval_ms=10" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1047
1048         dlogsend -p Info -b main -c 9 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1049         LOG_DETAILS="testing if deduplication with warnings works properly (1/12)"
1050         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 1 ] && ok || fail
1051         LOG_DETAILS="testing if deduplication with warnings works properly (2/12)"
1052         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 0 ] && ok || fail
1053
1054         dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, default warn_quantity"
1055         LOG_DETAILS="testing if deduplication with warnings works properly (3/12)"
1056         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity')" -eq 3 ] && ok || fail
1057         LOG_DETAILS="testing if deduplication with warnings works properly (4/12)"
1058         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, default warn_quantity LOG DUPLICATED')" -eq 1 ] && ok || fail
1059
1060         echo "deduplicate_warn_quantity=8" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1061
1062         dlogsend -p Info -b main -c 24 -t DLOG_DEDUP_TEST Message
1063         LOG_DETAILS="testing if deduplication with warnings works properly (5/12)"
1064         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 4 ] && ok || fail
1065         LOG_DETAILS="testing if deduplication with warnings works properly (6/12)"
1066         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Message LOG DUPLICATED')" -eq 3 ] && ok || fail
1067
1068         dlogsend -p Info -b main -c 8 -t DLOG_DEDUP_TEST longerMessage
1069         LOG_DETAILS="testing if deduplication with warnings works properly (7/12)"
1070         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c longerMessage)" -eq 2 ] && ok || fail
1071         LOG_DETAILS="testing if deduplication with warnings works properly (8/12)"
1072         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c Message)" -eq 6 ] && ok || fail
1073         LOG_DETAILS="testing if deduplication with warnings works properly (9/12)"
1074         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'longerMessage LOG DUPLICATED')" -eq 1 ] && ok || fail
1075
1076         echo "deduplicate_warn_quantity=-1" >> "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1077
1078         dlogsend -p Info -b main -c 12 -t DLOG_DEDUP_TEST "Basic deduplication, no warning"
1079         LOG_DETAILS="testing if deduplication with warnings works properly (10/12)"
1080         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning')" -eq 1 ] && ok || fail
1081         LOG_DETAILS="testing if deduplication with warnings works properly (11/12)"
1082         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication')" -eq 4 ] && ok || fail
1083         LOG_DETAILS="testing if deduplication with warnings works properly (12/12)"
1084         [ "$(dlogutil -d DLOG_DEDUP_TEST | grep -c 'Basic deduplication, no warning LOG DUPLICATED')" -eq 0 ] && ok || fail
1085
1086         rm "$RUNTIME_FILTERS_DIR/30-deduplicate.conf"
1087 fi
1088
1089 LOG_DETAILS="testing if libdlogutil clears the buffer correctly"
1090 sleep 1 # make sure the earlier logs are handled so that they don't end up handled after the clear
1091 test_libdlogutil clear $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1092
1093 if [ "$(date +%Y)" -le 2000 ]; then
1094         # This implies that the date isn't set. Let's set it to some date to better test timestamps.
1095         date -s "@2121212121" > /dev/null
1096 fi
1097 dlogsend -b main -t DLOG_TS_TEST "The future is now, old man"
1098 LOG_DETAILS="testing if the timestamps are correct"
1099 test_verifytime && ok || fail
1100
1101 DLOG_PREV_PATH=$DLOG_CONFIG_PATH
1102
1103 dlog_test_stdout_redirect $type
1104
1105 DLOG_CONFIG_PATH=$DLOG_PREV_PATH
1106
1107 if [ "$quick" -ne 1 ]; then
1108         if [ "$type" != "zero-copy" ]; then # Backend autodection and logger daemon stuff don't really work on zero-copy
1109                 kill $LOGGER > /dev/null
1110                 sleep 1
1111
1112                 dlog_test_backend_autodetection $type
1113                 dlog_test_logger_buffering_logging $type
1114         fi
1115
1116         dlog_logger -t 0 &
1117         LOGGER=$!
1118         sleep 1
1119
1120         LOG_DETAILS="testing if the library works with multithreaded app"
1121         dlogutil -f $TESTDIR/dlog_mt_test &
1122         MT_TEST=$!
1123         test_libdlog && ok || fail
1124
1125         sleep 1
1126         kill $MT_TEST
1127         MT_TEST=-1
1128
1129         dlogutil -c
1130         SPAWN_CNT=1000
1131
1132         # Spawn logging scripts and wait for them to finish.
1133         # We launch all the $SPAWN_CNT scripts in a subshell, and then use the wait command in order to
1134         # wait for all the jobs of the subshell, which means all the logging scripts.
1135         # Each of the logging scripts is a new subsubshell which waits a random amount of seconds and then logs
1136         # a single message. This gets us 5 groups of logs separated by a second; there are about 200 logs in each
1137         # group, sent about at the same time.
1138         (
1139                 for i in $(seq 1 $SPAWN_CNT); do
1140                         delay=$(shuf -i 1-5 -n 1)
1141                         ( sleep "$delay" && dlogsend -b main -t DLOG_TESTSUITE "$delay" ) &
1142                 done
1143                 wait
1144         )
1145
1146         LOG_DETAILS="testing if libdlogutil sorts by the timestamp correctly"
1147
1148         test_libdlogutil sorting $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
1149
1150         if [ "$type" = "pipe" ]; then
1151                 for P in recv sent; do
1152                         for Q in mono real; do
1153                                 LOG_DETAILS="testing if dlogutil --sort-by works correctly (${P}_${Q})"
1154                                 [ "$(dlogutil -db main --sort-by "${P}_${Q}" | wc -l)" -eq "$SPAWN_CNT" ] && ok || fail
1155                         done
1156                 done
1157                 for P in recv sent; do
1158                         LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_mono)"
1159                         dlogutil -db main --sort-by "${P}_mono" -v json | sed -Ee "s/^.*\"${P}_mono\":([^,}]*).*\$/\\1/" | sort -c && ok || fail
1160                         LOG_DETAILS="testing if dlogutil --sort-by sorts correctly (${P}_real)"
1161                         dlogutil -db main --sort-by "${P}_real" -v json | sed -Ee "s/^.*\"${P}_real\":\"([^\"]*)\",\"${P}_real_ns\":([^,}]*).*\$/\\1 \2/" | sort -c -k1,1 -k2,2g && ok || fail
1162                 done
1163         fi
1164
1165         sort_formats="kerneltime time recv_realtime rwtime threadtime long"
1166
1167         for format in $sort_formats; do
1168                 LOG_DETAILS="testing if sorting by timestamp from random sources on heavy load works ($format format)"
1169
1170                 # collect data and analyze timestamps
1171                 dlogutil -b main -d -v "$format" | (
1172                         prev_ts=0
1173                         prev_line=""
1174
1175                         while read -r line; do
1176                                 # filter out empty lines in "long" format
1177                                 [ -z "$line" ] && continue
1178                                 # filter out non-timestamp lines in "long" format
1179                                 if [ "$format" = "long" ] && [ "$(echo "$line" | sed -re 's/^\[[:print:]*\]$/line_ok/g')" != "line_ok" ]; then
1180                                         continue
1181                                 fi
1182
1183                                 ts=$(extract_timestamp "$format" "$line")
1184
1185                                 if [ "$ts" -ge "$prev_ts" ]; then
1186                                         prev_ts=$ts
1187                                 else
1188                                         printf "%s\n%s\n\n" "$prev_line" "$line" || break
1189                                 fi
1190                                 prev_line=$line
1191                         done
1192                 ) | grep -q ^ && fail || ok
1193
1194         done
1195 fi
1196
1197 if [ "$type" = "pipe" ]; then
1198         kill $LOGGER > /dev/null
1199         sleep 1
1200
1201         PRERESIZE_CONFIG_PATH="$DLOG_CONFIG_PATH"
1202         DLOG_CONFIG_PATH="@datadir@/dlog-$type.conf.compress"
1203
1204         RUNTIME_FILTERS_DIR="/tmp/dlog-filters/"
1205         mkdir -m 777 -p "$RUNTIME_FILTERS_DIR"
1206
1207         dlog_logger -t 0 &
1208         LOGGER=$!
1209         sleep 1
1210
1211         dlogsend -b main -c 9999 resizetestblalbl &
1212         sleep 4
1213         LOG_DETAILS="testing if compression-resize works (1/3)"
1214         [ "$(dlogutil -d -m xyz123 | wc -l)" -ge 1 ] && ok || fail
1215
1216         dlogctl --compression-name xyz123 --compression-resize 1
1217         sleep 5
1218         LOG_DETAILS="testing if compression-resize works (2/3)"
1219         [ "$(dlogutil -d -m xyz123 | wc -l)" -eq 0 ] && ok || fail
1220
1221         dlogctl --compression-name xyz123 --compression-resize 200000
1222         dlogsend -b main -c 9999 resizetestblalbl &
1223         sleep 4
1224         LOG_DETAILS="testing if compression-resize works (3/3)"
1225         [ "$(dlogutil -d -m xyz123 | wc -l)" -ge 1 ] && ok || fail
1226
1227         dlogutil -c
1228         kill $LOGGER > /dev/null
1229         DLOG_CONFIG_PATH=$PRERESIZE_CONFIG_PATH
1230         rm -rf $RUNTIME_FILTERS_DIR
1231 fi
1232
1233 # show results, clean up and return an exit code
1234
1235 echo "$OKS / $TOTAL tests passed"
1236 echo "$FAILS / $TOTAL tests failed"
1237
1238 if [ "$quick" -eq 1 ]; then
1239         echo "WARNING: quick mode!"
1240         echo "About 20% slowest running tests are disabled. Some functionality might be untested."
1241         echo "Rerun without '--quick' to run all tests."
1242 elif [ "$LEGACY_LOGGER" -eq 1 ]; then
1243         echo "WARNING: legacy logger module!"
1244         echo "Some functionality is untested."
1245         echo "Rerun on a device with the out-of-tree logger module to run all tests."
1246 fi
1247
1248 [ "$FAILS" -eq 0 ]
1249 # THE LINE ABOVE MUST STAY THE LAST COMMAND IN THE FILE!
1250 # This is because it is used to pass the exit code. If another command
1251 # is executed after it, its exit code will be propagated instead.