]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - tools/regression/usr.sbin/etcupdate/tzsetup.sh
MFC 281887:
[FreeBSD/stable/8.git] / tools / regression / usr.sbin / etcupdate / tzsetup.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 WORKDIR=work
33
34 usage()
35 {
36         echo "Usage: tzsetup.sh [-s script] [-w workdir]"
37         exit 1
38 }
39
40 # Allow the user to specify an alternate work directory or script.
41 COMMAND=etcupdate
42 while getopts "s:w:" option; do
43         case $option in
44                 s)
45                         COMMAND="sh $OPTARG"
46                         ;;
47                 w)
48                         WORKDIR=$OPTARG
49                         ;;
50                 *)
51                         echo
52                         usage
53                         ;;
54         esac
55 done
56 shift $((OPTIND - 1))
57 if [ $# -ne 0 ]; then
58         usage
59 fi
60
61 CONFLICTS=$WORKDIR/conflicts
62 OLD=$WORKDIR/old
63 NEW=$WORKDIR/current
64 TEST=$WORKDIR/test
65
66 build_trees()
67 {
68
69         # Build the base tree, but not /etc/localtime itself
70         local i j k
71
72         rm -rf $OLD $NEW $TEST $CONFLICTS
73         mkdir -p $OLD $NEW $TEST
74         mkdir -p $TEST/etc
75         mkdir -p $TEST/var/db
76         mkdir -p $TEST/usr/share/zoneinfo
77
78         # Create a dummy timezone file
79         echo "foo" > $TEST/usr/share/zoneinfo/foo
80
81 }
82
83 # $1 - relative path to file that should be missing from TEST
84 missing()
85 {
86         if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
87                 echo "File $1 should be missing"
88         fi
89 }
90
91 # $1 - relative path to file that should be a symlink in TEST
92 # $2 - optional value of the link
93 link()
94 {
95         local val
96
97         if ! [ -L $TEST/$1 ]; then
98                 echo "File $1 should be a link"
99         elif [ $# -gt 1 ]; then
100                 val=`readlink $TEST/$1`
101                 if [ "$val" != "$2" ]; then
102                         echo "Link $1 should link to \"$2\""
103                 fi
104         fi
105 }
106
107 # $1 - relative path to regular file that should be present in TEST
108 # $2 - optional string that should match file contents
109 # $3 - optional MD5 of the flie contents, overrides $2 if present
110 file()
111 {
112         local contents sum
113
114         if ! [ -f $TEST/$1 ]; then
115                 echo "File $1 should be a regular file"
116         elif [ $# -eq 2 ]; then
117                 contents=`cat $TEST/$1`
118                 if [ "$contents" != "$2" ]; then
119                         echo "File $1 has wrong contents"
120                 fi
121         elif [ $# -eq 3 ]; then
122                 sum=`md5 -q $TEST/$1`
123                 if [ "$sum" != "$3" ]; then
124                         echo "File $1 has wrong contents"
125                 fi
126         fi
127 }
128
129 if [ `id -u` -ne 0 ]; then
130         echo "must be root"
131 fi
132
133 if [ -r /etc/etcupdate.conf ]; then
134         echo "WARNING: /etc/etcupdate.conf settings may break some tests."
135 fi
136
137 # First, test for /etc/localtime not existing
138
139 build_trees
140
141 $COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
142
143 cat > $WORKDIR/correct.out <<EOF
144 EOF
145
146 echo "Differences for no /etc/localtime with -n:"
147 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out
148
149 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
150
151 echo "Differences for no /etc/localtime:"
152 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out
153
154 missing /etc/localtime
155 missing /var/db/zoneinfo
156
157 # Second, test for /etc/localtime being a symlink
158
159 build_trees
160 ln -s /dev/null $TEST/etc/localtime
161
162 $COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
163
164 cat > $WORKDIR/correct.out <<EOF
165 EOF
166
167 echo "Differences for symlinked /etc/localtime with -n:"
168 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out
169
170 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
171
172 echo "Differences for symlinked /etc/localtime:"
173 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out
174
175 link /etc/localtime "/dev/null"
176 missing /var/db/zoneinfo
177
178 # Third, test for /etc/localtime as a file and a missing /var/db/zoneinfo
179
180 build_trees
181 echo "bar" > $TEST/etc/localtime
182
183 $COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
184
185 cat > $WORKDIR/correct.out <<EOF
186 Warnings:
187   Needs update: /etc/localtime (required manual update via tzsetup(1))
188 EOF
189
190 echo "Differences for missing /var/db/zoneinfo with -n:"
191 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out
192
193 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
194
195 echo "Differences for missing /var/db/zoneinfo:"
196 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out
197
198 file /etc/localtime "bar"
199 missing /var/db/zoneinfo
200
201 # Finally, test the case where it should update /etc/localtime
202
203 build_trees
204 echo "bar" > $TEST/etc/localtime
205 echo "foo" > $TEST/var/db/zoneinfo
206
207 $COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
208
209 cat > $WORKDIR/correct.out <<EOF
210 EOF
211
212 echo "Differences for real update with -n:"
213 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out
214
215 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
216
217 echo "Differences for real update:"
218 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out
219
220 file /etc/localtime "foo"
221 file /var/db/zoneinfo "foo"