xref: /freebsd/usr.sbin/newsyslog/tests/legacy_test.sh (revision bca9d05fdb058aa709621661c2feccae8940d94b)
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	mkdir -p ${TMPDIR}/log ${TMPDIR}/alog
198	cd ${TMPDIR}/log
199}
200
201tmpdir_clean()
202{
203	cd ${TMPDIR}
204	rm -rf "${TMPDIR}/log" "${TMPDIR}/alog" newsyslog.conf
205}
206
207run_newsyslog()
208{
209
210	newsyslog -f ../newsyslog.conf -F -r "$@"
211}
212
213tests_normal_rotate() {
214	ext="$1"
215	dir="$2"
216
217	if [ -n "$dir" ]; then
218		newsyslog_args=" -a ${dir}"
219		name_postfix="${ext} archive dir"
220	else
221		newsyslog_args=""
222		name_postfix="${ext}"
223	fi
224
225	tmpdir_create
226
227	begin "create file ${name_postfix}" -newdir
228	run_newsyslog -C
229	ckfe $LOGFNAME
230	cknt ${dir}${LOGFNAME}.0${ext}
231	end
232
233	begin "rotate normal 1 ${name_postfix}"
234	run_newsyslog $newsyslog_args
235	ckfe ${LOGFNAME}
236	ckfe ${dir}${LOGFNAME}.0${ext}
237	cknt ${dir}${LOGFNAME}.1${ext}
238	end
239
240	begin "rotate normal 2 ${name_postfix}"
241	run_newsyslog $newsyslog_args
242	ckfe ${LOGFNAME}
243	ckfe ${dir}${LOGFNAME}.0${ext}
244	ckfe ${dir}${LOGFNAME}.1${ext}
245	cknt ${dir}${LOGFNAME}.2${ext}
246	end
247
248	begin "rotate normal 3 ${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}.3${ext}
255	end
256
257	begin "rotate normal 4 ${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	begin "rotate normal 5 ${name_postfix}"
267	run_newsyslog $newsyslog_args
268	ckfe ${LOGFNAME}
269	ckfe ${dir}${LOGFNAME}.0${ext}
270	ckfe ${dir}${LOGFNAME}.1${ext}
271	ckfe ${dir}${LOGFNAME}.2${ext}
272	cknt ${dir}${LOGFNAME}.4${ext}
273	end
274
275	# Wait a bit so we can see if the noaction test rotates files
276	sleep 1.1
277
278	begin "noaction ${name_postfix}"
279	ofiles=`ls -Tl ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`
280	run_newsyslog ${newsyslog_args} -n >/dev/null
281	ckfe ${LOGFNAME}
282	ckstr "$ofiles" "`ls -lT ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`"
283	end
284
285	tmpdir_clean
286}
287
288tests_normal_rotate_keepn() {
289	cnt="$1"
290	ext="$2"
291	dir="$3"
292
293	if [ -n "$dir" ]; then
294		newsyslog_args=" -a ${dir}"
295		name_postfix="${ext} archive dir"
296	else
297		newsyslog_args=""
298		name_postfix="${ext}"
299	fi
300
301	tmpdir_create
302
303	begin "create file ${name_postfix}" -newdir
304	run_newsyslog -C
305	ckfe $LOGFNAME
306	cknt ${dir}${LOGFNAME}.0${ext}
307	end
308
309	begin "rotate normal 1 cnt=$cnt ${name_postfix}"
310	run_newsyslog $newsyslog_args
311	ckfe ${LOGFNAME}
312	ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
313	cknt ${dir}${LOGFNAME}.1${ext}
314	end
315
316	begin "rotate normal 2 cnt=$cnt ${name_postfix}"
317	run_newsyslog $newsyslog_args
318	ckfe ${LOGFNAME}
319	ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
320	ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext}
321	cknt ${dir}${LOGFNAME}.2${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	cknt ${dir}${LOGFNAME}.3${ext}
331	end
332
333	begin "rotate normal 3 cnt=$cnt ${name_postfix}"
334	run_newsyslog $newsyslog_args
335	ckfe ${LOGFNAME}
336	ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
337	ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext}
338	ckntfe 3 $cnt ${dir}${LOGFNAME}.2${ext}
339	ckntfe 4 $cnt ${dir}${LOGFNAME}.3${ext}
340	cknt ${dir}${LOGFNAME}.4${ext}
341	end
342
343	# Wait a bit so we can see if the noaction test rotates files
344	sleep 1.1
345
346	begin "noaction ${name_postfix}"
347	osum=`md5 ${dir}${LOGFNAME} | tr -d '\n'`
348	run_newsyslog ${newsyslog_args} -n >/dev/null
349	ckfe ${LOGFNAME}
350	ckstr "$osum" "`md5 ${dir}${LOGFNAME} | tr -d '\n'`"
351	end
352
353	tmpdir_clean
354}
355
356tests_time_rotate() {
357	ext="$1"
358	dir="$2"
359
360	if [ -n "$dir" ]; then
361		newsyslog_args="-t DEFAULT -a ${dir}"
362		name_postfix="${ext} archive dir"
363	else
364		newsyslog_args="-t DEFAULT"
365		name_postfix="${ext}"
366	fi
367
368	tmpdir_create
369
370	begin "create file ${name_postfix}" -newdir
371	run_newsyslog -C ${newsyslog_args}
372	ckfe ${LOGFNAME}
373	end
374
375	begin "rotate time 1 ${name_postfix}"
376	run_newsyslog ${newsyslog_args}
377	ckfe ${LOGFNAME}
378	chkfcnt 1 ${dir}${LOGFNAME}.*${ext}
379	end
380
381	sleep 1.1
382
383	begin "rotate time 2 ${name_postfix}"
384	run_newsyslog ${newsyslog_args}
385	ckfe ${LOGFNAME}
386	chkfcnt 2 ${dir}${LOGFNAME}.*${ext}
387	end
388
389	sleep 1.1
390
391	begin "rotate time 3 ${name_postfix}"
392	run_newsyslog ${newsyslog_args}
393	ckfe ${LOGFNAME}
394	chkfcnt 3 ${dir}${LOGFNAME}.*${ext}
395	end
396
397	sleep 1.1
398
399	begin "rotate time 4 ${name_postfix}"
400	run_newsyslog ${newsyslog_args}
401	ckfe ${LOGFNAME}
402	chkfcnt 3 ${dir}${LOGFNAME}.*${ext}
403	end
404
405	begin "noaction ${name_postfix}"
406	ofiles=`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`
407	run_newsyslog ${newsyslog_args} -n >/dev/null
408	ckfe ${LOGFNAME}
409	ckstr "$ofiles" "`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`"
410	end
411
412	tmpdir_clean
413}
414
415tests_rfc5424() {
416	ext="$1"
417	dir="$2"
418
419	if [ -n "$dir" ]; then
420		newsyslog_args=" -a ${dir}"
421		name_postfix="${ext} archive dir"
422	else
423		newsyslog_args=""
424		name_postfix="${ext}"
425	fi
426
427	tmpdir_create
428
429	begin "RFC-5424 - create file ${name_postfix}" -newdir
430	run_newsyslog -C
431	ckfe $LOGFNAME
432	cknt ${dir}${LOGFNAME}.0${ext}
433	ckfe $LOGFNAME5424
434	cknt ${dir}${LOGFNAME5424}.0${ext}
435	ckrfc3164 ${LOGFNAME}
436	ckrfc5424 ${LOGFNAME5424}
437	end
438
439	begin "RFC-5424 - rotate normal 1 ${name_postfix}"
440	run_newsyslog $newsyslog_args
441	ckfe ${LOGFNAME}
442	ckfe ${dir}${LOGFNAME}.0${ext}
443	ckfe $LOGFNAME5424
444	ckfe ${dir}${LOGFNAME5424}.0${ext}
445	ckrfc3164 ${LOGFNAME}
446	ckrfc3164 ${dir}${LOGFNAME}.0${ext}
447	ckrfc5424 ${LOGFNAME5424}
448	ckrfc5424 ${dir}${LOGFNAME5424}.0${ext}
449	end
450
451	tmpdir_clean
452}
453
454echo 1..128
455mkdir -p ${TMPDIR}
456cd ${TMPDIR}
457
458LOGFNAME=foo.log
459LOGFPATH=${TMPDIR}/log/${LOGFNAME}
460
461# Log file for RFC-5424 testing
462LOGFNAME5424=foo5424.log
463LOGFPATH5424=${TMPDIR}/log/${LOGFNAME5424}
464
465# Normal, no archive dir, keep X files
466echo "$LOGFPATH	640  0	   *	@T00  NC" > newsyslog.conf
467tests_normal_rotate_keepn 0
468
469echo "$LOGFPATH	640  1	   *	@T00  NC" > newsyslog.conf
470tests_normal_rotate_keepn 1
471
472echo "$LOGFPATH	640  2	   *	@T00  NC" > newsyslog.conf
473tests_normal_rotate_keepn 2
474
475echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
476tests_normal_rotate_keepn 3
477
478# Normal, no archive dir, keep X files, gz
479echo "$LOGFPATH	640  0	   *	@T00  NCZ" > newsyslog.conf
480tests_normal_rotate_keepn 0 ".gz"
481
482echo "$LOGFPATH	640  1	   *	@T00  NCZ" > newsyslog.conf
483tests_normal_rotate_keepn 1 ".gz"
484
485echo "$LOGFPATH	640  2	   *	@T00  NCZ" > newsyslog.conf
486tests_normal_rotate_keepn 2 ".gz"
487
488echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
489tests_normal_rotate_keepn 3 ".gz"
490
491# Normal, no archive dir
492echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
493tests_normal_rotate
494
495echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
496tests_normal_rotate ".gz"
497
498echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
499tests_normal_rotate ".bz2"
500
501# Normal, archive dir
502echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
503tests_normal_rotate "" "${TMPDIR}/alog/"
504
505echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
506tests_normal_rotate ".gz" "${TMPDIR}/alog/"
507
508echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
509tests_normal_rotate ".bz2" "${TMPDIR}/alog/"
510
511# Time based, no archive dir
512echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
513tests_time_rotate
514
515echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
516tests_time_rotate "gz" ""
517
518echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
519tests_time_rotate "bz2" ""
520
521# Time based, archive dir
522echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
523tests_time_rotate "" "${TMPDIR}/alog/"
524
525echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
526tests_time_rotate "gz" "${TMPDIR}/alog/"
527
528echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
529tests_time_rotate "bz2" "${TMPDIR}/alog/"
530
531# RFC-5424; Normal, no archive dir
532echo "$LOGFPATH5424	640  3	   *	@T00  NCT" > newsyslog.conf
533echo "$LOGFPATH	640  3	   *	@T00  NC" >> newsyslog.conf
534tests_rfc5424
535
536rm -rf "${TMPDIR}"
537