Merge branch 'dfsg_clean'
[ltp-debian.git] / testcases / commands / mail / mail_tests.sh
blob1b27c064dced4f034c75950343183c7c42410504
1 #!/bin/sh
2 ################################################################################
3 ## ##
4 ## Copyright (c) International Business Machines Corp., 2001 ##
5 ## ##
6 ## This program is free software; you can redistribute it and#or modify ##
7 ## it under the terms of the GNU General Public License as published by ##
8 ## the Free Software Foundation; either version 2 of the License, or ##
9 ## (at your option) any later version. ##
10 ## ##
11 ## This program is distributed in the hope that it will be useful, but ##
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
13 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ##
14 ## for more details. ##
15 ## ##
16 ## You should have received a copy of the GNU General Public License ##
17 ## along with this program; if not, write to the Free Software ##
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ##
19 ## ##
20 ################################################################################
22 # File : mail_tests.sh
24 # Description: Tests basic functions of mail system. The aim of the test is to
25 # make sure that certain basic functionality of mail is expected
26 # to work as per man page. There are 4 - 5 operations that are
27 # done on a regular basis wrt mail. ie.
28 # mail send to an user@domain - received by that user@domain
29 # mail is send to nosuchuser@domain - mail delivery failure
30 # mail is send to user@nosuchdomain - mail delivery failure
31 # mail to user1@domain and cc user2@domain - mail rec by both
32 # mail to user1@domain and bcc user2@domain - mail rec by both
34 # Author: Manoj Iyer, manjo@mail.utexas.edu
36 # History: Jan 07 2003 - Created - Manoj Iyer.
37 # Jan 09 2003 - Added Test #2 #3 #4 and #5.
38 # Jan 10 2002 - Fixed various bugs I had introduced in the test.
39 # - Added SETUP and CLEANUP sections
42 export TST_TOTAL=5
44 if [ -z "$LTPTMP" -a -z "$TMPBASE" ]
45 then
46 LTPTMP=/tmp
47 else
48 LTPTMP=$TMPBASE
51 LTPBIN=$LTPTOOLS
53 RC=0
54 export TCID=SETUP
55 export TST_COUNT=1
57 MAIL_NOT_INSTALLED=
58 type mail > /dev/null 2>&1
59 if [ $? != 0 ]; then
60 MAIL_NOT_INSTALLED=1
63 # check if the user mail_test exists on this system.
64 # if not add that user mail_test, will removed before exiting test.
65 RC=$(awk '/^mail_test/ {print 1}' /etc/passwd)
66 if [ -z $RC ]
67 then
68 RC=0
69 $LTPBIN/tst_resm TINFO "INIT: Adding temporary user mail_test"
70 useradd -m -s /bin/bash mail_test > $LTPTMP/tst_mail.out 2>&1 || RC=$?
71 if [ $RC -ne 0 ]
72 then
73 $LTPBIN/tst_brk TBROK $LTPTMP/tst_mail.out NULL \
74 "Test INIT: Failed adding user mail_test. Reason:"
75 exit 1
78 $LTPBIN/tst_resm TINFO "INIT: Removing all mails for mail_test and root"
79 echo "d*" | mail -u mail_test > /dev/null 2>&1
80 echo "d*" | mail -u root > /dev/null 2>&1
82 # Set return code RC variable to 0, it will be set with a non-zero return code
83 # in case of error. Set TFAILCNT to 0, increment if there occures a failure.
86 TFAILCNT=0
87 RC=0
88 RC1=0
89 RC2=0
91 # Test #1
92 # Test that mail user@domain will send a mail to that user at that domain.
94 export TCID=mail01
95 export TST_COUNT=1
97 $LTPBIN/tst_resm TINFO "Test #1: mail root@localhost will send mail to root"
98 $LTPBIN/tst_resm TINFO "Test #1: user on local machine."
101 if [ -z "$MAIL_NOT_INSTALLED" ]; then
102 cat > $LTPTMP/tst_mail.in <<EOF
103 This is a test email.
106 mail -s "Test" root@localhost < $LTPTMP/tst_mail.in \
107 > $LTPTMP/tst_mail.out 2>&1 || RC=$?
108 if [ $RC -ne 0 ]
109 then
110 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
111 "Test #1: mail command failed. Reason: "
112 TFAILCNT=$(( $TFAILCNT+1 ))
113 else
114 # check if root received a new email with Test as subject
115 # but wait for the mail to arrive.
117 sleep 10s
118 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
119 mailsub=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
120 if [ -n "$mailsub" ] && [ "$mailsub" -ne 0 ]
121 then
122 $LTPBIN/tst_resm TPASS \
123 "Test #1: Mail was send to root & was received"
124 else
125 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
126 "Test #1: Mail send to root, but was not received"
127 TFAILCNT=$(( $TFAILCNT+1 ))
130 else
131 $LTPBIN/tst_resm TCONF "mail command not installed"
135 # Test #2
136 # Test that mail user@bad-domain will result in a warning from the mailer
137 # daemon that the domain does not exist.
139 export TCID=mail02
140 export TST_COUNT=2
141 RC=0
142 RC1=0
143 RC2=0
145 $LTPBIN/tst_resm TINFO \
146 "Test #2: mail user@bad-domain will result in failure"
147 $LTPBIN/tst_resm TINFO "Test #2: to deliver the mail. Mailer daemon should"
148 $LTPBIN/tst_resm TINFO "Test #2: report this failure."
150 if [ -z $MAIL_NOT_INSTALLED ]; then
151 cat > $LTPTMP/tst_mail.in <<EOF
152 This is a test email.
155 # Don't use underscores in domain names (they're illegal)...
156 mail -s "Test" root@thisdomaindoesnotexist < $LTPTMP/tst_mail.in \
157 > $LTPTMP/tst_mail.out 2>&1
158 if [ $? -ne 0 ]
159 then
160 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
161 "Test #2: mail command failed. Reason: "
162 TFAILCNT=$(( $TFAILCNT+1 ))
163 else
164 # check if Mailer-Deamon reported any delivery failure.
165 # but wait for the mail to arrive first, sleep 5s.
166 sleep 5s
167 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
168 RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3, "Mailer-Daemon")}' \
169 $LTPTMP/tst_mail.res)
170 ##################################################################
171 # In this testcase, mail will get "Returnedmail:", while mailx will
172 # get "UndeliveredMailReturned:".
173 # Either of mail and mailx may be linked to another.
174 # For example,
175 # /bin/mail -> /bin/mailx
176 # or
177 # /bin/mailx -> /bin/mail
178 ##################################################################
179 RC2=$(awk '/^>N/ {print match($9 $10, "Returnedmail:")}' \
180 $LTPTMP/tst_mail.res)
181 RC3=$(awk '/^>N/ {print match($9 $10, "UndeliveredMail")}' \
182 $LTPTMP/tst_mail.res)
184 if [ -z "$RC1" -a -z "$RC2" -a -z "$RC3" ]
185 then
186 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
187 "Test #2: No new mail for root. Reason:"
188 TFAILCNT=$(( $TFAILCNT+1 ))
189 else
190 if [ $RC1 -ne 0 -a $RC2 -ne 0 ] || [ $RC1 -ne 0 -a $RC3 -ne 0 ]
191 then
192 $LTPBIN/tst_resm TPASS \
193 "Test #2: Mailer-Deamon reported delivery failure"
194 else
195 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
196 "Test #2: Mailer-Deamon failed to report delivery failure. Reason:"
197 TFAILCNT=$(( $TFAILCNT+1 ))
200 else
201 $LTPBIN/tst_resm TCONF "mail command not installed"
204 # Test #3
205 # Test that mail non_existant_user@localhost will result in delivery failure.
206 # Mailer-Deamon will report this failure.
208 export TCID=mail03
209 export TST_COUNT=3
210 RC=0
211 RC1=0
212 RC2=0
214 $LTPBIN/tst_resm TINFO \
215 "Test #3: mail non_existant_user@localhost will fail"
216 $LTPBIN/tst_resm TINFO "Test #3: to deliver the mail. Mailer daemon should"
217 $LTPBIN/tst_resm TINFO "Test #3: report this failure."
219 if [ -z $MAIL_NOT_INSTALLED ]; then
220 cat > $LTPTMP/tst_mail.in <<EOF
221 This is a test email.
224 mail -s "Test" non_existant_userr@localhost < $LTPTMP/tst_mail.in \
225 > $LTPTMP/tst_mail.out 2>&1
226 if [ $? -ne 0 ]
227 then
228 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
229 "Test #3: mail command failed. Reason: "
230 TFAILCNT=$(( $TFAILCNT+1 ))
231 else
232 # check if Mailer-Deamon reported any delivery failure.
233 # but wait for the mail to arrive first, sleep 5s.
234 sleep 5s
235 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
236 RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3, "Mailer-Daemon")}' \
237 $LTPTMP/tst_mail.res)
238 ##################################################################
239 # In this testcase, mail will get "Returnedmail:", while mailx will
240 # get "UndeliveredMailReturned:".
241 # Either of mail and mailx may be linked to another.
242 # For example,
243 # /bin/mail -> /bin/mailx
244 # or
245 # /bin/mailx -> /bin/mail
246 ##################################################################
247 RC2=$(awk '/^>N/ {print match($9 $10, "Returnedmail:")}' \
248 $LTPTMP/tst_mail.res)
249 RC3=$(awk '/^>N/ {print match($9 $10, "UndeliveredMail")}' \
250 $LTPTMP/tst_mail.res)
252 if [ -z "$RC1" -a -z "$RC2" -a -z "$RC3" ]
253 then
254 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
255 "Test #2: No new mail for root. Reason:"
256 TFAILCNT=$(( $TFAILCNT+1 ))
257 else
258 if [ $RC1 -ne 0 -a $RC2 -ne 0 ] || [ $RC1 -ne 0 -a $RC3 -ne 0 ]
259 then
260 $LTPBIN/tst_resm TPASS \
261 "Test #3: Mailer-Deamon reported delivery failure"
262 else
263 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
264 "Test #3: Mailer-Deamon failed to report delivery failure. Reason:"
265 TFAILCNT=$(( $TFAILCNT+1 ))
269 else
270 $LTPBIN/tst_resm TCONF "mail command not installed"
273 # Test #4
274 # Test that mail -c user@domain option will carbon copy that user.
276 export TCID=mail04
277 export TST_COUNT=4
278 RC=0
280 $LTPBIN/tst_resm TINFO "Test #4: Test that mail -c user@domain will"
281 $LTPBIN/tst_resm TINFO "Test #4: carbon copy user@domain"
282 if [ -z $MAIL_NOT_INSTALLED ]; then
284 # send mail to root and carbon copy mail_test
285 mail -s "Test" root@localhost -c mail_test@localhost < \
286 $LTPTMP/tst_mail.in > $LTPTMP/tst_mail.out 2>&1 || RC=$?
287 if [ $RC -ne 0 ]
288 then
289 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
290 "Test #4: mail command failed. Reason:"
291 TFAILCNT=$(( $TFAILCNT+1 ))
292 else
293 # Check if mail_test received the mail and
294 # also if root received the main copy of the email.
295 sleep 5s
296 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
297 RC1=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
298 echo "d" | mail -u mail_test > $LTPTMP/tst_mail.res 2>&1
299 RC2=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
300 if [ -n "$RC1" -a -n "$RC2" ] && ["$RC1" -ne 0 -a "$RC2" -ne 0 ]
301 then
302 $LTPBIN/tst_resm TPASS \
303 "Test #4: Mail was carbon copied to user mail_test"
304 else
305 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
306 "Test #4: mail failed to carbon copy user mail_test. Reason:"
307 TFAILCNT=$(( $TFAILCNT+1 ))
310 else
311 $LTPBIN/tst_resm TCONF "mail command not installed"
314 # Test #5
315 # Test that mail -b user@domain option will blind carbon copy that user.
317 export TCID=mail05
318 export TST_COUNT=5
319 RC=0
321 $LTPBIN/tst_resm TINFO "Test #5: Test that mail -b user@domain will"
322 $LTPBIN/tst_resm TINFO "Test #5: blind carbon copy user@domain"
323 if [ -z $MAIL_NOT_INSTALLED ]; then
325 # send mail to root and carbon copy mail_test
326 mail -s "Test" root@localhost -c mail_test@localhost < \
327 $LTPTMP/tst_mail.in > $LTPTMP/tst_mail.out 2>&1 || RC=$?
328 if [ $RC -ne 0 ]
329 then
330 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
331 "Test #5: mail command failed. Reason:"
332 TFAILCNT=$(( $TFAILCNT+1 ))
333 else
334 # Check if mail_test received the mail and
335 # also if root received the main copy of the email.
336 sleep 5s
337 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
338 RC1=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
339 echo "d" | mail -u mail_test > $LTPTMP/tst_mail.res 2>&1
340 RC2=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
341 if [ -n "$RC1" -a -n "$RC2" ] && [ "$RC1" -ne 0 -a "$RC2" -ne 0 ]
342 then
343 $LTPBIN/tst_resm TPASS \
344 "Test #5: Mail was carbon copied to user mail_test"
345 else
346 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
347 "Test #5: mail failed to carbon copy user mail_testi. Reason:"
348 TFAILCNT=$(( $TFAILCNT+1 ))
351 else
352 $LTPBIN/tst_resm TCONF "mail command not installed"
355 #CLEANUP & EXIT
356 # remove all the temporary files created by this test.
357 export TCID=CLEANUP
358 export TST_COUNT=1
360 $LTPBIN/tst_resm TINFO "Test CLEAN: Removing temporary files from $LTPTMP"
361 rm -fr $LTPTMP/tst_mail*
363 $LTPBIN/tst_resm TINFO "Test CLEAN: Removing temporary user mail_test"
364 userdel -r mail_test > /dev/null 2>&1
366 exit $TFAILCNT