Home | History | Annotate | Line # | Download | only in logfileconfig
      1 #!/bin/sh
      2 
      3 # Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      4 #
      5 # SPDX-License-Identifier: MPL-2.0
      6 #
      7 # This Source Code Form is subject to the terms of the Mozilla Public
      8 # License, v. 2.0.  If a copy of the MPL was not distributed with this
      9 # file, you can obtain one at https://mozilla.org/MPL/2.0/.
     10 #
     11 # See the COPYRIGHT file distributed with this work for additional
     12 # information regarding copyright ownership.
     13 
     14 set -e
     15 
     16 . ../conf.sh
     17 
     18 # Test given condition.  If true, test again after a second.  Used for testing
     19 # filesystem-dependent conditions in order to prevent false negatives caused by
     20 # directory contents not being synchronized immediately after rename() returns.
     21 test_with_retry() {
     22   if test "$@"; then
     23     sleep 1
     24     if test "$@"; then
     25       return 0
     26     fi
     27   fi
     28   return 1
     29 }
     30 
     31 status=0
     32 n=0
     33 
     34 # First run with a known good config.
     35 n=$((n + 1))
     36 echo_i "testing log file validity (only plain files allowed) ($n)"
     37 ret=0
     38 cat /dev/null >ns1/named_log
     39 cp ns1/named.plainlog.conf ns1/named.conf
     40 nextpart ns1/named.run >/dev/null
     41 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
     42 wait_for_log 5 "reloading configuration succeeded" ns1/named.run || ret=1
     43 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
     44 status=$((status + ret))
     45 
     46 # Now try directory, expect failure
     47 n=$((n + 1))
     48 echo_i "testing directory as log file ($n)"
     49 ret=0
     50 nextpart ns1/named.run >/dev/null
     51 cp ns1/named.dir.conf ns1/named.conf
     52 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
     53 wait_for_log 5 "reloading configuration failed: invalid file" ns1/named.run || ret=1
     54 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
     55 status=$((status + ret))
     56 
     57 # Now try pipe file, expect failure
     58 n=$((n + 1))
     59 echo_i "testing pipe file as log file ($n)"
     60 ret=0
     61 nextpart ns1/named.run >/dev/null
     62 rm -f ns1/named_pipe
     63 if mkfifo ns1/named_pipe >/dev/null 2>&1; then
     64   cp ns1/named.pipe.conf ns1/named.conf
     65   rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
     66   wait_for_log 5 "reloading configuration failed: invalid file" ns1/named.run || ret=1
     67   if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
     68   status=$((status + ret))
     69 else
     70   echo_i "skipping pipe test (unable to create pipe)"
     71 fi
     72 
     73 # Now try symlink file to plain file, expect success
     74 n=$((n + 1))
     75 echo_i "testing symlink to plain file as log file ($n)"
     76 ret=0
     77 rm -f ns1/named_log ns1/named_sym
     78 touch ns1/named_log
     79 if ln -s $(pwd)/ns1/named_log $(pwd)/ns1/named_sym >/dev/null 2>&1; then
     80   nextpart ns1/named.run >/dev/null
     81   cp ns1/named.sym.conf ns1/named.conf
     82   rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
     83   wait_for_log 5 "reloading configuration succeeded" ns1/named.run || ret=1
     84   if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
     85   status=$((status + ret))
     86 else
     87   echo_i "skipping symlink test (unable to create symlink)"
     88 fi
     89 
     90 echo_i "repeat previous tests without named -g"
     91 cp ns1/named.plain.conf ns1/named.conf
     92 stop_server --use-rndc --port ${CONTROLPORT} ns1
     93 cp named1.args ns1/named.args
     94 start_server --noclean --restart --port ${PORT} ns1
     95 
     96 n=$((n + 1))
     97 echo_i "testing log file validity (only plain files allowed) ($n)"
     98 ret=0
     99 cat /dev/null >ns1/named_log
    100 cp ns1/named.plainlog.conf ns1/named.conf
    101 nextpart ns1/named.run >/dev/null
    102 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    103 wait_for_log 5 "reloading configuration succeeded" ns1/named.run || ret=1
    104 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    105 status=$((status + ret))
    106 
    107 # Now try directory, expect failure
    108 n=$((n + 1))
    109 echo_i "testing directory as log file ($n)"
    110 ret=0
    111 nextpart ns1/named.run >/dev/null
    112 cp ns1/named.dir.conf ns1/named.conf
    113 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    114 wait_for_log 5 "reloading configuration failed: invalid file" ns1/named.run || ret=1
    115 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    116 status=$((status + ret))
    117 
    118 # Now try pipe file, expect failure
    119 n=$((n + 1))
    120 echo_i "testing pipe file as log file ($n)"
    121 ret=0
    122 nextpart ns1/named.run >/dev/null
    123 rm -f ns1/named_pipe
    124 if mkfifo ns1/named_pipe >/dev/null 2>&1; then
    125   cp ns1/named.pipe.conf ns1/named.conf
    126   rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    127   wait_for_log 5 "reloading configuration failed: invalid file" ns1/named.run || ret=1
    128   if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    129   status=$((status + ret))
    130 else
    131   echo_i "skipping pipe test (unable to create pipe)"
    132 fi
    133 
    134 # Now try symlink file to plain file, expect success
    135 n=$((n + 1))
    136 echo_i "testing symlink to plain file as log file ($n)"
    137 ret=0
    138 rm -f ns1/named_log ns1/named_sym
    139 touch ns1/named_log
    140 if ln -s $(pwd)/ns1/named_log $(pwd)/ns1/named_sym >/dev/null 2>&1; then
    141   nextpart ns1/named.run >/dev/null
    142   cp ns1/named.sym.conf ns1/named.conf
    143   rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    144   wait_for_log 5 "reloading configuration succeeded" ns1/named.run || ret=1
    145   if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    146   status=$((status + ret))
    147 else
    148   echo_i "skipping symlink test (unable to create symlink)"
    149 fi
    150 
    151 echo_i "testing logging functionality"
    152 n=$((n + 1))
    153 ret=0
    154 echo_i "testing iso8601 timestamp ($n)"
    155 cp ns1/named.iso8601.conf ns1/named.conf
    156 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    157 grep '^....-..-..T..:..:..\.... ' ns1/named_iso8601 >/dev/null || ret=1
    158 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    159 status=$((status + ret))
    160 
    161 n=$((n + 1))
    162 echo_i "testing iso8601-utc timestamp ($n)"
    163 ret=0
    164 cp ns1/named.iso8601-utc.conf ns1/named.conf
    165 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    166 grep '^....-..-..T..:..:..\....Z' ns1/named_iso8601_utc >/dev/null || ret=1
    167 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    168 status=$((status + ret))
    169 
    170 n=$((n + 1))
    171 echo_i "testing explicit versions ($n)"
    172 ret=0
    173 cp ns1/named.vers.conf ns1/named.conf
    174 # a seconds since epoch version number
    175 touch ns1/named_vers.1480039317
    176 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    177 $DIG version.bind txt ch @10.53.0.1 -p ${PORT} >dig.out.test$n || ret=1
    178 grep "status: NOERROR" dig.out.test$n >/dev/null || ret=1
    179 # we are configured to retain five logfiles (a current file
    180 # and 4 backups). so files with version number 5 or higher
    181 # should be removed.
    182 test_with_retry -f ns1/named_vers.1480039317 && ret=1
    183 test_with_retry -f ns1/named_vers.5 && ret=1
    184 test_with_retry -f ns1/named_vers.4 || ret=1
    185 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    186 status=$((status + ret))
    187 
    188 n=$((n + 1))
    189 echo_i "testing timestamped versions ($n)"
    190 ret=0
    191 cp ns1/named.ts.conf ns1/named.conf
    192 # a seconds since epoch version number
    193 touch ns1/named_ts.1480039317
    194 # a timestamp version number
    195 touch ns1/named_ts.20150101120000120
    196 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    197 _found2() (
    198   $DIG version.bind txt ch @10.53.0.1 -p ${PORT} >dig.out.test$n || return 1
    199   grep "status: NOERROR" dig.out.test$n >/dev/null || return 1
    200 
    201   # we are configured to keep three versions, so the oldest
    202   # timestamped versions should be gone, and there should
    203   # be two or three backup ones.
    204   [ -f ns1/named_ts.1480039317 ] && return 1
    205   [ -f ns1/named_ts.20150101120000120 ] && return 1
    206   set -- ns1/named_ts.*
    207   [ "$#" -eq 2 -o "$#" -eq 3 ] || return 1
    208 )
    209 retry_quiet 5 _found2 || ret=1
    210 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    211 status=$((status + ret))
    212 
    213 n=$((n + 1))
    214 echo_i "testing incremented versions ($n)"
    215 ret=0
    216 cp ns1/named.inc.conf ns1/named.conf
    217 try=0
    218 while test $try -lt 12; do
    219   touch ns1/named_inc.$try
    220   try=$((try + 1))
    221 done
    222 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    223 _found2() (
    224   $DIG version.bind txt ch @10.53.0.1 -p ${PORT} >dig.out.test$n || return 1
    225   grep "status: NOERROR" dig.out.test$n >/dev/null || return 1
    226 
    227   try=1
    228   while test $try -lt 12; do
    229     [ -f ns1/named_inc.$try ] && return 1
    230     try=$((try + 1))
    231   done
    232   set -- ns1/named_inc.*
    233   [ "$#" -eq 1 ] || return 1
    234 )
    235 retry_quiet 5 _found2 || ret=1
    236 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    237 status=$((status + ret))
    238 
    239 n=$((n + 1))
    240 echo_i "testing absolute file path versions ($n)"
    241 ret=0
    242 cp ns1/named.abspath.conf ns1/named.conf
    243 try=0
    244 while test $try -lt 12; do
    245   touch $TMPDIR/example.log.$try
    246   try=$((try + 1))
    247 done
    248 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    249 _found2() (
    250   $DIG version.bind txt ch @10.53.0.1 -p ${PORT} >dig.out.test$n || return 1
    251   grep "status: NOERROR" dig.out.test$n >/dev/null || return 1
    252 
    253   try=1
    254   while test $try -lt 12; do
    255     [ -f $TMPDIR/example.log.$try ] && return 1
    256     try=$((try + 1))
    257   done
    258   set -- $TMPDIR/example.log.*
    259   [ "$#" -eq 1 ] || return 1
    260 )
    261 retry_quiet 5 _found2 || ret=1
    262 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    263 status=$((status + ret))
    264 
    265 n=$((n + 1))
    266 echo_i "testing unlimited versions ($n)"
    267 ret=0
    268 cp ns1/named.unlimited.conf ns1/named.conf
    269 # a seconds since epoch version number
    270 touch ns1/named_unlimited.1480039317
    271 rndc_reconfig ns1 10.53.0.1 >rndc.out.test$n
    272 $DIG version.bind txt ch @10.53.0.1 -p ${PORT} >dig.out.test$n || ret=1
    273 grep "status: NOERROR" dig.out.test$n >/dev/null || ret=1
    274 test_with_retry -f ns1/named_unlimited.1480039317 || ret=1
    275 test_with_retry -f ns1/named_unlimited.4 || ret=1
    276 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    277 status=$((status + ret))
    278 
    279 n=$((n + 1))
    280 echo_i "testing default logfile using named -L file ($n)"
    281 ret=0
    282 stop_server ns1
    283 cp named2.args ns1/named.args
    284 test -f ns1/named.pid && ret=1
    285 rm -f ns1/named_deflog
    286 cp ns1/named.plainlog.conf ns1/named.conf
    287 start_server --noclean --restart --port ${PORT} ns1
    288 [ -f "ns1/named_deflog" ] || ret=1
    289 if [ "$ret" -ne 0 ]; then echo_i "failed"; fi
    290 status=$((status + ret))
    291 
    292 echo_i "exit status: $status"
    293 [ $status -eq 0 ] || exit 1
    294