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