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