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 -F -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=" -a ${dir}"
220		name_postfix="${ext} archive dir"
221	else
222		newsyslog_args=""
223		name_postfix="${ext}"
224	fi
225
226	tmpdir_create
227
228	begin "create file ${name_postfix}" -newdir
229	run_newsyslog -C
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=" -a ${dir}"
298		name_postfix="${ext} archive dir"
299	else
300		newsyslog_args=""
301		name_postfix="${ext}"
302	fi
303
304	tmpdir_create
305
306	begin "create file ${name_postfix}" -newdir
307	run_newsyslog -C
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="-t DEFAULT -a ${dir}"
367		name_postfix="${ext} archive dir"
368	else
369		newsyslog_args="-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_rfc5424() {
421	local dir ext name_postfix newsyslog_args
422
423	ext="$1"
424	dir="$2"
425
426	if [ -n "$dir" ]; then
427		newsyslog_args=" -a ${dir}"
428		name_postfix="${ext} archive dir"
429	else
430		newsyslog_args=""
431		name_postfix="${ext}"
432	fi
433
434	tmpdir_create
435
436	begin "RFC-5424 - create file ${name_postfix}" -newdir
437	run_newsyslog -C
438	ckfe $LOGFNAME
439	cknt ${dir}${LOGFNAME}.0${ext}
440	ckfe $LOGFNAME5424
441	cknt ${dir}${LOGFNAME5424}.0${ext}
442	ckrfc3164 ${LOGFNAME}
443	ckrfc5424 ${LOGFNAME5424}
444	end
445
446	begin "RFC-5424 - rotate normal 1 ${name_postfix}"
447	run_newsyslog $newsyslog_args
448	ckfe ${LOGFNAME}
449	ckfe ${dir}${LOGFNAME}.0${ext}
450	ckfe $LOGFNAME5424
451	ckfe ${dir}${LOGFNAME5424}.0${ext}
452	ckrfc3164 ${LOGFNAME}
453	ckrfc3164 ${dir}${LOGFNAME}.0${ext}
454	ckrfc5424 ${LOGFNAME5424}
455	ckrfc5424 ${dir}${LOGFNAME5424}.0${ext}
456	end
457
458	tmpdir_clean
459}
460
461tests_p_flag_rotate() {
462	local ext
463
464	ext="$1"
465
466	tmpdir_create
467
468	begin "create file"
469	run_newsyslog -C
470	ckfe $LOGFNAME
471	cknt ${LOGFNAME}.0
472	cknt ${LOGFNAME}.0${ext}
473	end
474
475	begin "rotate p flag 1 ${ext}"
476	run_newsyslog
477	ckfe $LOGFNAME
478	ckfe ${LOGFNAME}.0
479	cknt ${LOGFNAME}.0${ext}
480	run_newsyslog
481	ckfe $LOGFNAME
482	ckfe ${LOGFNAME}.0
483	cknt ${LOGFNAME}.0${ext}
484	ckfe ${LOGFNAME}.1${ext}
485	run_newsyslog
486	ckfe $LOGFNAME
487	ckfe ${LOGFNAME}.0
488	cknt ${LOGFNAME}.0${ext}
489	ckfe ${LOGFNAME}.1${ext}
490	ckfe ${LOGFNAME}.2${ext}
491	end
492
493	tmpdir_clean
494}
495
496tests_normal_rotate_recompress() {
497	local ext
498
499	ext=".gz"
500
501	tmpdir_create
502
503	begin "create file recompress"
504	run_newsyslog -C
505	ckfe $LOGFNAME
506	cknt ${LOGFNAME}.0${ext}
507	end
508
509	begin "rotate normal 1"
510	run_newsyslog
511	ckfe $LOGFNAME
512	ckfe ${LOGFNAME}.0${ext}
513	cknt ${LOGFNAME}.1${ext}
514	end
515
516	begin "rotate recompress 1"
517	gunzip ${LOGFNAME}.0${ext}
518	ckfe ${LOGFNAME}.0
519	cknt ${LOGFNAME}.0${ext}
520	run_newsyslog
521	ckfe $LOGFNAME
522	ckfe ${LOGFNAME}.0${ext}
523	ckfe ${LOGFNAME}.1${ext}
524	end
525}
526
527echo 1..185
528mkdir -p ${TMPDIR}
529cd ${TMPDIR}
530
531LOGFNAME=foo.log
532LOGFPATH=${TMPDIR}/log/${LOGFNAME}
533
534# Log file for RFC-5424 testing
535LOGFNAME5424=foo5424.log
536LOGFPATH5424=${TMPDIR}/log/${LOGFNAME5424}
537
538# Normal, no archive dir, keep X files
539echo "$LOGFPATH	640  0	   *	@T00  NC" > newsyslog.conf
540tests_normal_rotate_keepn 0
541
542echo "$LOGFPATH	640  1	   *	@T00  NC" > newsyslog.conf
543tests_normal_rotate_keepn 1
544
545echo "$LOGFPATH	640  2	   *	@T00  NC" > newsyslog.conf
546tests_normal_rotate_keepn 2
547
548echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
549tests_normal_rotate_keepn 3
550
551# Normal, no archive dir, keep X files, gz
552echo "$LOGFPATH	640  0	   *	@T00  NCZ" > newsyslog.conf
553tests_normal_rotate_keepn 0 ".gz"
554
555echo "$LOGFPATH	640  1	   *	@T00  NCZ" > newsyslog.conf
556tests_normal_rotate_keepn 1 ".gz"
557
558echo "$LOGFPATH	640  2	   *	@T00  NCZ" > newsyslog.conf
559tests_normal_rotate_keepn 2 ".gz"
560
561echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
562tests_normal_rotate_keepn 3 ".gz"
563
564# Normal, no archive dir
565echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
566tests_normal_rotate
567
568echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
569tests_normal_rotate ".gz"
570
571echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
572tests_normal_rotate ".bz2"
573
574echo "$LOGFPATH	640  3	   *	@T00  NCX" > newsyslog.conf
575tests_normal_rotate ".xz"
576
577echo "$LOGFPATH	640  3	   *	@T00  NCY" > newsyslog.conf
578tests_normal_rotate ".zst"
579
580# Normal, archive dir
581echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
582tests_normal_rotate "" "${TMPDIR}/alog/"
583
584echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
585tests_normal_rotate ".gz" "${TMPDIR}/alog/"
586
587echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
588tests_normal_rotate ".bz2" "${TMPDIR}/alog/"
589
590echo "$LOGFPATH	640  3	   *	@T00  NCX" > newsyslog.conf
591tests_normal_rotate ".xz" "${TMPDIR}/alog/"
592
593echo "$LOGFPATH	640  3	   *	@T00  NCY" > newsyslog.conf
594tests_normal_rotate ".zst" "${TMPDIR}/alog/"
595
596# Time based, no archive dir
597echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
598tests_time_rotate
599
600echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
601tests_time_rotate "gz" ""
602
603echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
604tests_time_rotate "bz2" ""
605
606echo "$LOGFPATH	640  3	   *	@T00  NCX" > newsyslog.conf
607tests_time_rotate "xz" ""
608
609echo "$LOGFPATH	640  3	   *	@T00  NCY" > newsyslog.conf
610tests_time_rotate "zst" ""
611
612# Time based, archive dir
613echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
614tests_time_rotate "" "${TMPDIR}/alog/"
615
616echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
617tests_time_rotate "gz" "${TMPDIR}/alog/"
618
619echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
620tests_time_rotate "bz2" "${TMPDIR}/alog/"
621
622echo "$LOGFPATH	640  3	   *	@T00  NCX" > newsyslog.conf
623tests_time_rotate "xz" "${TMPDIR}/alog/"
624
625echo "$LOGFPATH	640  3	   *	@T00  NCY" > newsyslog.conf
626tests_time_rotate "zst" "${TMPDIR}/alog/"
627
628# RFC-5424; Normal, no archive dir
629echo "$LOGFPATH5424	640  3	   *	@T00  NCT" > newsyslog.conf
630echo "$LOGFPATH	640  3	   *	@T00  NC" >> newsyslog.conf
631tests_rfc5424
632
633echo "$LOGFPATH 640  3     *    @T00  NCpZ" > newsyslog.conf
634tests_p_flag_rotate ".gz"
635
636echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
637tests_normal_rotate_recompress
638
639rm -rf "${TMPDIR}"
640