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