]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/etcupdate/tests/tzsetup_test.sh
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / etcupdate / tests / tzsetup_test.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2013 Hudson River Trading LLC
4 # Written by: John H. Baldwin <jhb@FreeBSD.org>
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29
30 # Various regression tests for the tzsetup handling in the 'update' command.
31
32 FAILED=no
33 WORKDIR=work
34
35 usage()
36 {
37         echo "Usage: tzsetup.sh [-s script] [-w workdir]"
38         exit 1
39 }
40
41 # Allow the user to specify an alternate work directory or script.
42 COMMAND=etcupdate
43 while getopts "s:w:" option; do
44         case $option in
45                 s)
46                         COMMAND="sh $OPTARG"
47                         ;;
48                 w)
49                         WORKDIR=$OPTARG
50                         ;;
51                 *)
52                         echo
53                         usage
54                         ;;
55         esac
56 done
57 shift $((OPTIND - 1))
58 if [ $# -ne 0 ]; then
59         usage
60 fi
61
62 CONFLICTS=$WORKDIR/conflicts
63 OLD=$WORKDIR/old
64 NEW=$WORKDIR/current
65 TEST=$WORKDIR/test
66
67 build_trees()
68 {
69
70         # Build the base tree, but not /etc/localtime itself
71         local i j k
72
73         rm -rf $OLD $NEW $TEST $CONFLICTS
74         mkdir -p $OLD $NEW $TEST
75         mkdir -p $TEST/etc
76         mkdir -p $TEST/var/db
77         mkdir -p $TEST/usr/share/zoneinfo
78
79         # Create a dummy timezone file
80         echo "foo" > $TEST/usr/share/zoneinfo/foo
81
82 }
83
84 # $1 - relative path to file that should be missing from TEST
85 missing()
86 {
87         if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
88                 echo "File $1 should be missing"
89                 FAILED=yes
90         fi
91 }
92
93 # $1 - relative path to file that should be a symlink in TEST
94 # $2 - optional value of the link
95 link()
96 {
97         local val
98
99         if ! [ -L $TEST/$1 ]; then
100                 echo "File $1 should be a link"
101                 FAILED=yes
102         elif [ $# -gt 1 ]; then
103                 val=`readlink $TEST/$1`
104                 if [ "$val" != "$2" ]; then
105                         echo "Link $1 should link to \"$2\""
106                         FAILED=yes
107                 fi
108         fi
109 }
110
111 # $1 - relative path to regular file that should be present in TEST
112 # $2 - optional string that should match file contents
113 # $3 - optional MD5 of the flie contents, overrides $2 if present
114 file()
115 {
116         local contents sum
117
118         if ! [ -f $TEST/$1 ]; then
119                 echo "File $1 should be a regular file"
120                 FAILED=yes
121         elif [ $# -eq 2 ]; then
122                 contents=`cat $TEST/$1`
123                 if [ "$contents" != "$2" ]; then
124                         echo "File $1 has wrong contents"
125                         FAILED=yes
126                 fi
127         elif [ $# -eq 3 ]; then
128                 sum=`md5 -q $TEST/$1`
129                 if [ "$sum" != "$3" ]; then
130                         echo "File $1 has wrong contents"
131                         FAILED=yes
132                 fi
133         fi
134 }
135
136 if [ `id -u` -ne 0 ]; then
137         echo "must be root"
138         exit 0
139 fi
140
141 if [ -r /etc/etcupdate.conf ]; then
142         echo "WARNING: /etc/etcupdate.conf settings may break some tests."
143 fi
144
145 # First, test for /etc/localtime not existing
146
147 build_trees
148
149 $COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
150
151 cat > $WORKDIR/correct.out <<EOF
152 EOF
153
154 echo "Differences for no /etc/localtime with -n:"
155 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
156     || FAILED=yes
157
158 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
159
160 echo "Differences for no /etc/localtime:"
161 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
162     || FAILED=yes
163
164 missing /etc/localtime
165 missing /var/db/zoneinfo
166
167 # Second, test for /etc/localtime being a symlink
168
169 build_trees
170 ln -s /dev/null $TEST/etc/localtime
171
172 $COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
173
174 cat > $WORKDIR/correct.out <<EOF
175 EOF
176
177 echo "Differences for symlinked /etc/localtime with -n:"
178 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
179     || FAILED=yes
180
181 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
182
183 echo "Differences for symlinked /etc/localtime:"
184 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
185     || FAILED=yes
186
187 link /etc/localtime "/dev/null"
188 missing /var/db/zoneinfo
189
190 # Third, test for /etc/localtime as a file and a missing /var/db/zoneinfo
191
192 build_trees
193 echo "bar" > $TEST/etc/localtime
194
195 $COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
196
197 cat > $WORKDIR/correct.out <<EOF
198 Warnings:
199   Needs update: /etc/localtime (required manual update via tzsetup(1))
200 EOF
201
202 echo "Differences for missing /var/db/zoneinfo with -n:"
203 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
204     || FAILED=yes
205
206 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
207
208 echo "Differences for missing /var/db/zoneinfo:"
209 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
210     || FAILED=yes
211
212 file /etc/localtime "bar"
213 missing /var/db/zoneinfo
214
215 # Finally, test the case where it should update /etc/localtime
216
217 build_trees
218 echo "bar" > $TEST/etc/localtime
219 echo "foo" > $TEST/var/db/zoneinfo
220
221 $COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
222
223 cat > $WORKDIR/correct.out <<EOF
224 EOF
225
226 echo "Differences for real update with -n:"
227 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
228     || FAILED=yes
229
230 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
231
232 echo "Differences for real update:"
233 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
234     || FAILED=yes
235
236 file /etc/localtime "foo"
237 file /var/db/zoneinfo "foo"
238
239 [ "${FAILED}" = no ]