]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/newsyslog/tests/legacy_test.sh
MFC r318960,r319545,r319546,r319548,r321261:
[FreeBSD/stable/10.git] / usr.sbin / newsyslog / tests / legacy_test.sh
1 #!/bin/sh
2
3 # $FreeBSD$
4
5 # A regular expression matching the format of an RFC-5424 log line header,
6 # including the timestamp up through the seconds indicator; it does not include
7 # the (optional) timezone offset.
8 RFC5424_FMT='^<[0-9][0-9]*>1 [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}'
9
10 # A regular expression matching the format of an RFC-3164 (traditional syslog)
11 # log line header, including the timestamp.
12 RFC3164_FMT='^[A-Z][a-z]{2} [ 0-9][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2}'
13
14 COUNT=0
15 TMPDIR=$(pwd)/work
16 if [ $? -ne 0 ]; then
17         echo "$0: Can't create temp dir, exiting..."
18         exit 1
19 fi
20
21 # Begin an individual test
22 begin()
23 {
24         COUNT=`expr $COUNT + 1`
25         OK=1
26         NAME="$1"
27 }
28
29 # End an individual test
30 end()
31 {
32         if [ $OK = 1 ]
33         then
34                 printf 'ok '
35         else
36                 printf 'not ok '
37         fi
38         echo "$COUNT - $NAME"
39 }
40
41 # Make a file that can later be verified
42 mkf()
43 {
44         CN=`basename $1`
45         echo "$CN-$CN" >$1
46 }
47
48 # Verify that the file specified is correct
49 ckf()
50 {
51         if [ -f $2 ] && echo "$1-$1" | diff - $2 >/dev/null
52         then
53                 ok
54         else
55                 notok
56         fi
57 }
58
59 # Check that a file exists
60 ckfe()
61 {
62         if [ -f $1 ]
63         then
64                 ok
65         else
66                 notok
67         fi
68 }
69
70 # Verify that the specified file does not exist
71 # (is not there)
72 cknt()
73 {
74         if [ -r $1 ]
75         then
76                 notok
77         else
78                 ok
79         fi
80 }
81
82 # Check if a file is there, depending of if it's supposed to or not -
83 # basically how many log files we are supposed to keep vs. how many we
84 # actually keep.
85 ckntfe()
86 {
87         curcnt=$1
88         keepcnt=$2
89         f=$3
90
91         if [ $curcnt -le $keepcnt ]
92         then
93                 #echo Assuming file there
94                 ckfe $f
95         else
96                 #echo Assuming file NOT there
97                 cknt $f
98         fi
99 }
100
101 # Verify that the specified file has RFC-5424 rotation messages.
102 ckrfc5424()
103 {
104         local lc=$(wc -l $1 | cut -w -f2)
105         local rc=$(grep -cE "${RFC5424_FMT}" $1)
106         if [ "$lc" -eq 0 -o "$rc" -eq 0 -o "$lc" -ne "$rc" ]
107         then
108                 notok
109         else
110                 ok
111         fi
112 }
113
114
115 # Verify that the specified file has RFC-3164 rotation messages.
116 ckrfc3164()
117 {
118         local lc=$(wc -l $1 | cut -w -f2)
119         local rc=$(grep -cE "${RFC3164_FMT}" $1)
120         if [ "$lc" -eq 0 -o "$rc" -eq 0 -o "$lc" -ne "$rc" ]
121         then
122                 notok
123         else
124                 ok
125         fi
126 }
127
128
129 # A part of a test succeeds
130 ok()
131 {
132         :
133 }
134
135 # A part of a test fails
136 notok()
137 {
138         OK=0
139 }
140
141 # Verify that the exit code passed is for unsuccessful termination
142 ckfail()
143 {
144         if [ $1 -gt 0 ]
145         then
146                 ok
147         else
148                 notok
149         fi
150 }
151
152 # Verify that the exit code passed is for successful termination
153 ckok()
154 {
155         if [ $1 -eq 0 ]
156         then
157                 ok
158         else
159                 notok
160         fi
161 }
162
163 # Check that there are X files which match expr
164 chkfcnt()
165 {
166         cnt=$1; shift
167         if [ $cnt -eq `echo "$@" | wc -w` ]
168         then
169                 ok
170         else
171                 notok
172         fi
173 }
174
175 # Check that two strings are alike
176 ckstr()
177 {
178         if [ "$1" = "$2" ]
179         then
180                 ok
181         else
182                 notok
183         fi
184 }
185
186 tmpdir_create()
187 {
188         mkdir -p ${TMPDIR}/log ${TMPDIR}/alog
189         cd ${TMPDIR}/log
190 }
191
192 tmpdir_clean()
193 {
194         cd ${TMPDIR}
195         rm -rf "${TMPDIR}/log" "${TMPDIR}/alog" newsyslog.conf
196 }
197
198 run_newsyslog()
199 {
200
201         newsyslog -f ../newsyslog.conf -F -r "$@"
202 }
203
204 tests_normal_rotate() {
205         ext="$1"
206         dir="$2"
207
208         if [ -n "$dir" ]; then
209                 newsyslog_args=" -a ${dir}"
210                 name_postfix="${ext} archive dir"
211         else
212                 newsyslog_args=""
213                 name_postfix="${ext}"
214         fi
215
216         tmpdir_create
217
218         begin "create file ${name_postfix}" -newdir
219         run_newsyslog -C
220         ckfe $LOGFNAME
221         cknt ${dir}${LOGFNAME}.0${ext}
222         end
223
224         begin "rotate normal 1 ${name_postfix}"
225         run_newsyslog $newsyslog_args
226         ckfe ${LOGFNAME}
227         ckfe ${dir}${LOGFNAME}.0${ext}
228         cknt ${dir}${LOGFNAME}.1${ext}
229         end
230
231         begin "rotate normal 2 ${name_postfix}"
232         run_newsyslog $newsyslog_args
233         ckfe ${LOGFNAME}
234         ckfe ${dir}${LOGFNAME}.0${ext}
235         ckfe ${dir}${LOGFNAME}.1${ext}
236         cknt ${dir}${LOGFNAME}.2${ext}
237         end
238
239         begin "rotate normal 3 ${name_postfix}"
240         run_newsyslog $newsyslog_args
241         ckfe ${LOGFNAME}
242         ckfe ${dir}${LOGFNAME}.0${ext}
243         ckfe ${dir}${LOGFNAME}.1${ext}
244         ckfe ${dir}${LOGFNAME}.2${ext}
245         cknt ${dir}${LOGFNAME}.3${ext}
246         end
247
248         begin "rotate normal 4 ${name_postfix}"
249         run_newsyslog $newsyslog_args
250         ckfe ${LOGFNAME}
251         ckfe ${dir}${LOGFNAME}.0${ext}
252         ckfe ${dir}${LOGFNAME}.1${ext}
253         ckfe ${dir}${LOGFNAME}.2${ext}
254         cknt ${dir}${LOGFNAME}.4${ext}
255         end
256
257         begin "rotate normal 5 ${name_postfix}"
258         run_newsyslog $newsyslog_args
259         ckfe ${LOGFNAME}
260         ckfe ${dir}${LOGFNAME}.0${ext}
261         ckfe ${dir}${LOGFNAME}.1${ext}
262         ckfe ${dir}${LOGFNAME}.2${ext}
263         cknt ${dir}${LOGFNAME}.4${ext}
264         end
265
266         # Wait a bit so we can see if the noaction test rotates files
267         sleep 1.1
268
269         begin "noaction ${name_postfix}"
270         ofiles=`ls -Tl ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`
271         run_newsyslog ${newsyslog_args} -n >/dev/null
272         ckfe ${LOGFNAME}
273         ckstr "$ofiles" "`ls -lT ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`"
274         end
275
276         tmpdir_clean
277 }
278
279 tests_normal_rotate_keepn() {
280         cnt="$1"
281         ext="$2"
282         dir="$3"
283
284         if [ -n "$dir" ]; then
285                 newsyslog_args=" -a ${dir}"
286                 name_postfix="${ext} archive dir"
287         else
288                 newsyslog_args=""
289                 name_postfix="${ext}"
290         fi
291
292         tmpdir_create
293
294         begin "create file ${name_postfix}" -newdir
295         run_newsyslog -C
296         ckfe $LOGFNAME
297         cknt ${dir}${LOGFNAME}.0${ext}
298         end
299
300         begin "rotate normal 1 cnt=$cnt ${name_postfix}"
301         run_newsyslog $newsyslog_args
302         ckfe ${LOGFNAME}
303         ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
304         cknt ${dir}${LOGFNAME}.1${ext}
305         end
306
307         begin "rotate normal 2 cnt=$cnt ${name_postfix}"
308         run_newsyslog $newsyslog_args
309         ckfe ${LOGFNAME}
310         ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
311         ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext}
312         cknt ${dir}${LOGFNAME}.2${ext}
313         end
314
315         begin "rotate normal 3 cnt=$cnt ${name_postfix}"
316         run_newsyslog $newsyslog_args
317         ckfe ${LOGFNAME}
318         ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
319         ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext}
320         ckntfe 3 $cnt ${dir}${LOGFNAME}.2${ext}
321         cknt ${dir}${LOGFNAME}.3${ext}
322         end
323
324         begin "rotate normal 3 cnt=$cnt ${name_postfix}"
325         run_newsyslog $newsyslog_args
326         ckfe ${LOGFNAME}
327         ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
328         ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext}
329         ckntfe 3 $cnt ${dir}${LOGFNAME}.2${ext}
330         ckntfe 4 $cnt ${dir}${LOGFNAME}.3${ext}
331         cknt ${dir}${LOGFNAME}.4${ext}
332         end
333
334         # Wait a bit so we can see if the noaction test rotates files
335         sleep 1.1
336
337         begin "noaction ${name_postfix}"
338         osum=`md5 ${dir}${LOGFNAME} | tr -d '\n'`
339         run_newsyslog ${newsyslog_args} -n >/dev/null
340         ckfe ${LOGFNAME}
341         ckstr "$osum" "`md5 ${dir}${LOGFNAME} | tr -d '\n'`"
342         end
343
344         tmpdir_clean
345 }
346
347 tests_time_rotate() {
348         ext="$1"
349         dir="$2"
350
351         if [ -n "$dir" ]; then
352                 newsyslog_args="-t DEFAULT -a ${dir}"
353                 name_postfix="${ext} archive dir"
354         else
355                 newsyslog_args="-t DEFAULT"
356                 name_postfix="${ext}"
357         fi
358
359         tmpdir_create
360
361         begin "create file ${name_postfix}" -newdir
362         run_newsyslog -C ${newsyslog_args}
363         ckfe ${LOGFNAME}
364         end
365
366         begin "rotate time 1 ${name_postfix}"
367         run_newsyslog ${newsyslog_args}
368         ckfe ${LOGFNAME}
369         chkfcnt 1 ${dir}${LOGFNAME}.*${ext}
370         end
371
372         sleep 1.1
373
374         begin "rotate time 2 ${name_postfix}"
375         run_newsyslog ${newsyslog_args}
376         ckfe ${LOGFNAME}
377         chkfcnt 2 ${dir}${LOGFNAME}.*${ext}
378         end
379
380         sleep 1.1
381
382         begin "rotate time 3 ${name_postfix}"
383         run_newsyslog ${newsyslog_args}
384         ckfe ${LOGFNAME}
385         chkfcnt 3 ${dir}${LOGFNAME}.*${ext}
386         end
387
388         sleep 1.1
389
390         begin "rotate time 4 ${name_postfix}"
391         run_newsyslog ${newsyslog_args}
392         ckfe ${LOGFNAME}
393         chkfcnt 3 ${dir}${LOGFNAME}.*${ext}
394         end
395
396         begin "noaction ${name_postfix}"
397         ofiles=`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`
398         run_newsyslog ${newsyslog_args} -n >/dev/null
399         ckfe ${LOGFNAME}
400         ckstr "$ofiles" "`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`"
401         end
402
403         tmpdir_clean
404 }
405
406 tests_rfc5424() {
407         ext="$1"
408         dir="$2"
409
410         if [ -n "$dir" ]; then
411                 newsyslog_args=" -a ${dir}"
412                 name_postfix="${ext} archive dir"
413         else
414                 newsyslog_args=""
415                 name_postfix="${ext}"
416         fi
417
418         tmpdir_create
419
420         begin "RFC-5424 - create file ${name_postfix}" -newdir
421         run_newsyslog -C
422         ckfe $LOGFNAME
423         cknt ${dir}${LOGFNAME}.0${ext}
424         ckfe $LOGFNAME5424
425         cknt ${dir}${LOGFNAME5424}.0${ext}
426         ckrfc3164 ${LOGFNAME}
427         ckrfc5424 ${LOGFNAME5424}
428         end
429
430         begin "RFC-5424 - rotate normal 1 ${name_postfix}"
431         run_newsyslog $newsyslog_args
432         ckfe ${LOGFNAME}
433         ckfe ${dir}${LOGFNAME}.0${ext}
434         ckfe $LOGFNAME5424
435         ckfe ${dir}${LOGFNAME5424}.0${ext}
436         ckrfc3164 ${LOGFNAME}
437         ckrfc3164 ${dir}${LOGFNAME}.0${ext}
438         ckrfc5424 ${LOGFNAME5424}
439         ckrfc5424 ${dir}${LOGFNAME5424}.0${ext}
440         end
441
442         tmpdir_clean
443 }
444
445 echo 1..128
446 mkdir -p ${TMPDIR}
447 cd ${TMPDIR}
448
449 LOGFNAME=foo.log
450 LOGFPATH=${TMPDIR}/log/${LOGFNAME}
451
452 # Log file for RFC-5424 testing
453 LOGFNAME5424=foo5424.log
454 LOGFPATH5424=${TMPDIR}/log/${LOGFNAME5424}
455
456 # Normal, no archive dir, keep X files
457 echo "$LOGFPATH 640  0     *    @T00  NC" > newsyslog.conf
458 tests_normal_rotate_keepn 0
459
460 echo "$LOGFPATH 640  1     *    @T00  NC" > newsyslog.conf
461 tests_normal_rotate_keepn 1
462
463 echo "$LOGFPATH 640  2     *    @T00  NC" > newsyslog.conf
464 tests_normal_rotate_keepn 2
465
466 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
467 tests_normal_rotate_keepn 3
468
469 # Normal, no archive dir, keep X files, gz
470 echo "$LOGFPATH 640  0     *    @T00  NCZ" > newsyslog.conf
471 tests_normal_rotate_keepn 0 ".gz"
472
473 echo "$LOGFPATH 640  1     *    @T00  NCZ" > newsyslog.conf
474 tests_normal_rotate_keepn 1 ".gz"
475
476 echo "$LOGFPATH 640  2     *    @T00  NCZ" > newsyslog.conf
477 tests_normal_rotate_keepn 2 ".gz"
478
479 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
480 tests_normal_rotate_keepn 3 ".gz"
481
482 # Normal, no archive dir
483 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
484 tests_normal_rotate
485
486 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
487 tests_normal_rotate ".gz"
488
489 echo "$LOGFPATH 640  3     *    @T00  NCJ" > newsyslog.conf
490 tests_normal_rotate ".bz2"
491
492 # Normal, archive dir
493 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
494 tests_normal_rotate "" "${TMPDIR}/alog/"
495
496 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
497 tests_normal_rotate ".gz" "${TMPDIR}/alog/"
498
499 echo "$LOGFPATH 640  3     *    @T00  NCJ" > newsyslog.conf
500 tests_normal_rotate ".bz2" "${TMPDIR}/alog/"
501
502 # Time based, no archive dir
503 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
504 tests_time_rotate
505
506 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
507 tests_time_rotate "gz" ""
508
509 echo "$LOGFPATH 640  3     *    @T00  NCJ" > newsyslog.conf
510 tests_time_rotate "bz2" ""
511
512 # Time based, archive dir
513 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
514 tests_time_rotate "" "${TMPDIR}/alog/"
515
516 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
517 tests_time_rotate "gz" "${TMPDIR}/alog/"
518
519 echo "$LOGFPATH 640  3     *    @T00  NCJ" > newsyslog.conf
520 tests_time_rotate "bz2" "${TMPDIR}/alog/"
521
522 # RFC-5424; Normal, no archive dir
523 echo "$LOGFPATH5424     640  3     *    @T00  NCT" > newsyslog.conf
524 echo "$LOGFPATH 640  3     *    @T00  NC" >> newsyslog.conf
525 tests_rfc5424
526
527 rm -rf "${TMPDIR}"