]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/etcupdate/tests/conflicts_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 / conflicts_test.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2010 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 to run for the 'resolve' command.
31
32 FAILED=no
33 WORKDIR=work
34
35 usage()
36 {
37         echo "Usage: conflicts.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 # These tests deal with conflicts to a single file.  For each test, we
68 # generate a conflict in /etc/login.conf.  Each resolve option is tested
69 # to ensure it DTRT.
70 build_login_conflict()
71 {
72
73         rm -rf $OLD $NEW $TEST $CONFLICTS
74         mkdir -p $OLD/etc $NEW/etc $TEST/etc
75         
76         # Generate a conflict in /etc/login.conf.
77         cat > $OLD/etc/login.conf <<EOF
78 default:\\
79         :passwd_format=md5:
80 EOF
81         cat > $NEW/etc/login.conf <<EOF
82 default:\\
83         :passwd_format=md5:\\
84         :copyright=/etc/COPYRIGHT
85 EOF
86         cat > $TEST/etc/login.conf <<EOF
87 default:\\
88         :passwd_format=md5:\\
89         :welcome=/etc/motd:
90 EOF
91
92         $COMMAND -r -d $WORKDIR -D $TEST >/dev/null
93 }
94
95 # This is used to verify special handling for /etc/mail/aliases and
96 # the newaliases warning.
97 build_aliases_conflict()
98 {
99
100         rm -rf $OLD $NEW $TEST $CONFLICTS
101         mkdir -p $OLD/etc/mail $NEW/etc/mail $TEST/etc/mail
102
103         # Generate a conflict in /etc/mail/aliases
104         cat > $OLD/etc/mail/aliases <<EOF
105 # root: me@my.domain
106
107 # Basic system aliases -- these MUST be present
108 MAILER-DAEMON: postmaster
109 postmaster: root
110 EOF
111         cat > $NEW/etc/mail/aliases <<EOF
112 # root: me@my.domain
113
114 # Basic system aliases -- these MUST be present
115 MAILER-DAEMON: postmaster
116 postmaster: root
117
118 # General redirections for pseudo accounts
119 _dhcp:  root
120 _pflogd: root
121 EOF
122         cat > $TEST/etc/mail/aliases <<EOF
123 root: someone@example.com
124
125 # Basic system aliases -- these MUST be present
126 MAILER-DAEMON: postmaster
127 postmaster: foo
128 EOF
129
130         $COMMAND -r -d $WORKDIR -D $TEST >/dev/null
131 }
132
133 # $1 - relative path to file that should be missing from TEST
134 missing()
135 {
136         if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
137                 echo "File $1 should be missing"
138                 FAILED=yes
139         fi
140 }
141
142 # $1 - relative path to file that should be present in TEST
143 present()
144 {
145         if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
146                 echo "File $1 should be present"
147                 FAILED=yes
148         fi
149 }
150
151 # $1 - relative path to regular file that should be present in TEST
152 # $2 - optional string that should match file contents
153 # $3 - optional MD5 of the flie contents, overrides $2 if present
154 file()
155 {
156         local contents sum
157
158         if ! [ -f $TEST/$1 ]; then
159                 echo "File $1 should be a regular file"
160                 FAILED=yes
161         elif [ $# -eq 2 ]; then
162                 contents=`cat $TEST/$1`
163                 if [ "$contents" != "$2" ]; then
164                         echo "File $1 has wrong contents"
165                         FAILED=yes
166                 fi
167         elif [ $# -eq 3 ]; then
168                 sum=`md5 -q $TEST/$1`
169                 if [ "$sum" != "$3" ]; then
170                         echo "File $1 has wrong contents"
171                         FAILED=yes
172                 fi
173         fi
174 }
175
176 # $1 - relative path to a regular file that should have a conflict
177 # $2 - optional MD5 of the conflict file contents
178 conflict()
179 {
180         local sum
181
182         if ! [ -f $CONFLICTS/$1 ]; then
183                 echo "File $1 missing conflict"
184                 FAILED=yes
185         elif [ $# -gt 1 ]; then
186                 sum=`md5 -q $CONFLICTS/$1`
187                 if [ "$sum" != "$2" ]; then
188                         echo "Conflict $1 has wrong contents"
189                         FAILED=yes
190                 fi
191         fi
192 }
193
194 # $1 - relative path to a regular file that should no longer have a conflict
195 resolved()
196 {
197         if [ -f $CONFLICTS/$1 ]; then
198                 echo "Conflict $1 should be resolved"
199                 FAILED=yes
200         fi
201 }
202
203 if [ `id -u` -ne 0 ]; then
204         echo "must be root"
205         exit 0
206 fi
207
208 if [ -r /etc/etcupdate.conf ]; then
209         echo "WARNING: /etc/etcupdate.conf settings may break some tests."
210 fi
211
212 # Test each of the following resolve options: 'p', 'mf', 'tf', 'r'.
213
214 build_login_conflict
215
216 # Verify that 'p' doesn't do anything.
217 echo "Checking 'p':"
218 echo 'p' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
219
220 file /etc/login.conf "" 95de92ea3f1bb1bf4f612a8b5908cddd
221 missing /etc/login.conf.db
222 conflict /etc/login.conf
223
224 # Verify that 'mf' removes the conflict, but does nothing else.
225 echo "Checking 'mf':"
226 echo 'mf' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
227
228 file /etc/login.conf "" 95de92ea3f1bb1bf4f612a8b5908cddd
229 missing /etc/login.conf.db
230 resolved /etc/login.conf
231
232 build_login_conflict
233
234 # Verify that 'tf' installs the new version of the file.
235 echo "Checking 'tf':"
236 echo 'tf' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
237
238 file /etc/login.conf "" 7774a0f9a3a372c7c109c32fd31c4b6b
239 file /etc/login.conf.db
240 resolved /etc/login.conf
241
242 build_login_conflict
243
244 # Verify that 'r' installs the resolved version of the file.  To
245 # simulate this, manually edit the merged file so that it doesn't
246 # contain conflict markers.
247 echo "Checking 'r':"
248 cat > $CONFLICTS/etc/login.conf <<EOF
249 default:\\
250         :passwd_format=md5:\\
251         :copyright=/etc/COPYRIGHT\\
252         :welcome=/etc/motd:
253 EOF
254
255 echo 'r' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
256
257 file /etc/login.conf "" 966e25984b9b63da8eaac8479dcb0d4d
258 file /etc/login.conf.db
259 resolved /etc/login.conf
260
261 build_aliases_conflict
262
263 # Verify that 'p' and 'mf' do not generate the newaliases warning.
264 echo "Checking newalias warning for 'p'":
265 echo 'p' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
266 if [ $? -eq 0 ]; then
267         echo "+ Extra warning"
268         FAILED=yes
269 fi
270 echo "Checking newalias warning for 'mf'":
271 echo 'mf' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
272 if [ $? -eq 0 ]; then
273         echo "+ Extra warning"
274         FAILED=yes
275 fi
276
277 # Verify that 'tf' and 'r' do generate the newaliases warning.
278 build_aliases_conflict
279 echo "Checking newalias warning for 'tf'":
280 echo 'tf' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
281 if [ $? -ne 0 ]; then
282         echo "- Missing warning"
283         FAILED=yes
284 fi
285
286 build_aliases_conflict
287 cp $TEST/etc/mail/aliases $CONFLICTS/etc/mail/aliases
288 echo 'r' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
289 if [ $? -ne 0 ]; then
290         echo "- Missing warning"
291         FAILED=yes
292 fi
293
294 [ "${FAILED}" = no ]