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 TODO="rotate time 2-4 fail today; bug 212160" 384 385 begin "rotate time 2 ${name_postfix}" 386 run_newsyslog ${newsyslog_args} 387 ckfe ${LOGFNAME} 388 chkfcnt 2 ${dir}${LOGFNAME}.*${ext} 389 end 390 391 sleep 1.1 392 393 begin "rotate time 3 ${name_postfix}" 394 run_newsyslog ${newsyslog_args} 395 ckfe ${LOGFNAME} 396 chkfcnt 3 ${dir}${LOGFNAME}.*${ext} 397 end 398 399 sleep 1.1 400 401 begin "rotate time 4 ${name_postfix}" 402 run_newsyslog ${newsyslog_args} 403 ckfe ${LOGFNAME} 404 chkfcnt 3 ${dir}${LOGFNAME}.*${ext} 405 end 406 unset TODO 407 408 begin "noaction ${name_postfix}" 409 ofiles=`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'` 410 run_newsyslog ${newsyslog_args} -n >/dev/null 411 ckfe ${LOGFNAME} 412 ckstr "$ofiles" "`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`" 413 end 414 415 tmpdir_clean 416} 417 418tests_rfc5424() { 419 ext="$1" 420 dir="$2" 421 422 if [ -n "$dir" ]; then 423 newsyslog_args=" -a ${dir}" 424 name_postfix="${ext} archive dir" 425 else 426 newsyslog_args="" 427 name_postfix="${ext}" 428 fi 429 430 tmpdir_create 431 432 begin "RFC-5424 - create file ${name_postfix}" -newdir 433 run_newsyslog -C 434 ckfe $LOGFNAME 435 cknt ${dir}${LOGFNAME}.0${ext} 436 ckfe $LOGFNAME5424 437 cknt ${dir}${LOGFNAME5424}.0${ext} 438 ckrfc3164 ${LOGFNAME} 439 ckrfc5424 ${LOGFNAME5424} 440 end 441 442 begin "RFC-5424 - rotate normal 1 ${name_postfix}" 443 run_newsyslog $newsyslog_args 444 ckfe ${LOGFNAME} 445 ckfe ${dir}${LOGFNAME}.0${ext} 446 ckfe $LOGFNAME5424 447 ckfe ${dir}${LOGFNAME5424}.0${ext} 448 ckrfc3164 ${LOGFNAME} 449 ckrfc3164 ${dir}${LOGFNAME}.0${ext} 450 ckrfc5424 ${LOGFNAME5424} 451 ckrfc5424 ${dir}${LOGFNAME5424}.0${ext} 452 end 453 454 tmpdir_clean 455} 456 457echo 1..126 458mkdir -p ${TMPDIR} 459cd ${TMPDIR} 460 461LOGFNAME=foo.log 462LOGFPATH=${TMPDIR}/log/${LOGFNAME} 463 464# Log file for RFC-5424 testing 465LOGFNAME5424=foo5424.log 466LOGFPATH5424=${TMPDIR}/log/${LOGFNAME5424} 467 468# Normal, no archive dir, keep X files 469echo "$LOGFPATH 640 0 * @T00 NC" > newsyslog.conf 470tests_normal_rotate_keepn 0 471 472echo "$LOGFPATH 640 1 * @T00 NC" > newsyslog.conf 473tests_normal_rotate_keepn 1 474 475echo "$LOGFPATH 640 2 * @T00 NC" > newsyslog.conf 476tests_normal_rotate_keepn 2 477 478echo "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 479tests_normal_rotate_keepn 3 480 481# Normal, no archive dir, keep X files, gz 482echo "$LOGFPATH 640 0 * @T00 NCZ" > newsyslog.conf 483tests_normal_rotate_keepn 0 ".gz" 484 485echo "$LOGFPATH 640 1 * @T00 NCZ" > newsyslog.conf 486tests_normal_rotate_keepn 1 ".gz" 487 488echo "$LOGFPATH 640 2 * @T00 NCZ" > newsyslog.conf 489tests_normal_rotate_keepn 2 ".gz" 490 491echo "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 492tests_normal_rotate_keepn 3 ".gz" 493 494# Normal, no archive dir 495echo "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 496tests_normal_rotate 497 498echo "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 499tests_normal_rotate ".gz" 500 501echo "$LOGFPATH 640 3 * @T00 NCJ" > newsyslog.conf 502tests_normal_rotate ".bz2" 503 504# Normal, archive dir 505echo "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 506tests_normal_rotate "" "${TMPDIR}/alog/" 507 508echo "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 509tests_normal_rotate ".gz" "${TMPDIR}/alog/" 510 511echo "$LOGFPATH 640 3 * @T00 NCJ" > newsyslog.conf 512tests_normal_rotate ".bz2" "${TMPDIR}/alog/" 513 514# Time based, no archive dir 515echo "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 516tests_time_rotate 517 518echo "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 519tests_time_rotate "gz" "" 520 521echo "$LOGFPATH 640 3 * @T00 NCJ" > newsyslog.conf 522tests_time_rotate "bz2" "" 523 524# Time based, archive dir 525echo "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 526tests_time_rotate "" "${TMPDIR}/alog/" 527 528echo "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 529tests_time_rotate "gz" "${TMPDIR}/alog/" 530 531echo "$LOGFPATH 640 3 * @T00 NCJ" > newsyslog.conf 532tests_time_rotate "bz2" "${TMPDIR}/alog/" 533 534# RFC-5424; Normal, no archive dir 535echo "$LOGFPATH5424 640 3 * @T00 NCT" > newsyslog.conf 536echo "$LOGFPATH 640 3 * @T00 NC" >> newsyslog.conf 537tests_rfc5424 538 539rm -rf "${TMPDIR}" 540