mail_tests.sh revision 153a17356a162543945669241441821d337f0e73
1################################################################################
2##                                                                            ##
3## Copyright (c) International Business Machines  Corp., 2001                 ##
4##                                                                            ##
5## This program is free software;  you can redistribute it and#or modify      ##
6## it under the terms of the GNU General Public License as published by       ##
7## the Free Software Foundation; either version 2 of the License, or          ##
8## (at your option) any later version.                                        ##
9##                                                                            ##
10## This program is distributed in the hope that it will be useful, but        ##
11## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
12## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
13## for more details.                                                          ##
14##                                                                            ##
15## You should have received a copy of the GNU General Public License          ##
16## along with this program;  if not, write to the Free Software               ##
17## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    ##
18##                                                                            ##
19################################################################################
20#
21# File :        mail_tests.sh
22#
23# Description:  Tests basic functions of mail system. The aim of the test is to
24#               make sure that certain basic functionality of mail is expected
25#               to work as per man page. There are 4 - 5 operations that are
26#               done on a regular basis wrt mail. ie. 
27#               mail send to an user@domain - received by that user@domain
28#               mail is send to nosuchuser@domain - mail delivery failure
29#               mail is send to user@nosuchdomain - mail delivery failure
30#               mail to user1@domain and cc user2@domain - mail rec by both
31#               mail to user1@domain and bcc user2@domain - mail rec by both
32#
33# Author:       Manoj Iyer, manjo@mail.utexas.edu
34#
35# History:      Jan 07 2003 - Created - Manoj Iyer.
36#               Jan 09 2003 - Added Test #2 #3 #4 and #5.
37#                Jan 10 2002 - Fixed various bugs I had introduced in the test.
38#                           - Added SETUP and CLEANUP sections 
39#
40#! /bin/sh
41
42
43export TST_TOTAL=5
44
45if [ -z "$LTPTMP" -a -z "$TMPBASE" ]
46then
47    LTPTMP=/tmp
48else
49    LTPTMP=$TMPBASE
50fi
51
52if [ -z "$LTPBIN" -a -z "$LTPROOT" ]
53then
54    LTPBIN=./
55else
56    LTPBIN=$LTPROOT/testcases/bin
57fi
58
59RC=0
60export TCID=SETUP
61export TST_COUNT=1
62
63# check if the user mail_test exists on this system.
64# if not add that user mail_test, will removed before exiting test.
65RC=$(awk '/^mail_test/ {print 1}' /etc/passwd)
66if [ -z $RC ]
67then
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 || 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
76    fi
77fi
78$LTPBIN/tst_resm TINFO "INIT: Removing all mails for mail_test and root"
79echo "d*" | mail -u mail_test &>/dev/null
80echo "d*" | mail -u root &>/dev/null
81
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.
84
85
86TFAILCNT=0
87RC=0
88RC1=0
89RC2=0
90
91# Test #1
92# Test that mail user@domain will send a mail to that user at that domain.
93
94export TCID=mail01
95export TST_COUNT=1
96
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."
99
100
101cat > $LTPTMP/tst_mail.in <<EOF
102This is a test email.
103EOF
104
105mail -s "Test" root@localhost < $LTPTMP/tst_mail.in \
106    &>$LTPTMP/tst_mail.out || RC=$?
107if [ $RC -ne 0 ]
108then
109    $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
110        "Test #1: mail command failed. Reason: "
111    TFAILCNT=$(( $TFAILCNT+1 ))
112else
113    # check if root received a new email with Test as subject
114    # but wait for the mail to arrive.
115    
116    sleep 10s
117    echo "d" | mail -u root &>$LTPTMP/tst_mail.res
118    mailsub=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
119    if [ $mailsub -ne 0 ]
120    then
121        $LTPBIN/tst_resm TPASS \
122            "Test #1: Mail was send to root & was received"
123    else
124        $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
125            "Test #1: Mail send to root, but was not received"
126        TFAILCNT=$(( $TFAILCNT+1 ))
127    fi
128fi
129
130
131# Test #2
132# Test that mail user@bad-domain will result in a warning from the mailer
133# daemon that the domain does not exist. 
134
135export TCID=mail02
136export TST_COUNT=2
137RC=0
138RC1=0
139RC2=0
140
141$LTPBIN/tst_resm TINFO \
142    "Test #2: mail user@bad-domain will result in failure"
143$LTPBIN/tst_resm TINFO "Test #2: to deliver the mail. Mailer daemon should"
144$LTPBIN/tst_resm TINFO "Test #2: report this failure."
145
146cat > $LTPTMP/tst_mail.in <<EOF
147This is a test email.
148EOF
149
150# Don't use underscores in domain names (they're illegal)...
151mail -s "Test" root@thisdomaindoesnotexist < $LTPTMP/tst_mail.in \
152     &>$LTPTMP/tst_mail.out
153if [ $? -ne 0 ]
154then
155    $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
156        "Test #2: mail command failed. Reason: "
157    TFAILCNT=$(( $TFAILCNT+1 ))
158else
159    # check if Mailer-Deamon reported any delivery failure.    
160    # but wait for the mail to arrive first, sleep 5s.
161    sleep 5s
162    echo "d" | mail -u root &>$LTPTMP/tst_mail.res
163    RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3, "Mailer-Daemon")}' \
164	$LTPTMP/tst_mail.res)
165    RC2=$(awk '/^>N/ {print match($9 $10 $11, "Maildeliveryfailed:")}' \
166        $LTPTMP/tst_mail.res)
167#####
168# Some mailers (e.g., Red Hat's sendmail) print different messages 
169#####
170    [ -z "$RC2" -o "X$RC2" = "X0" ] && \
171	RC2=$(awk '/^>N/ {print match($9 $10, "Returnedmail:")}' \
172		$LTPTMP/tst_mail.res)
173    if [ -z "$RC1" -a -z "$RC2" ]
174    then
175        $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
176        "Test #2: No new mail for root. Reason:"
177        TFAILCNT=$(( $TFAILCNT+1 ))
178    else
179        if [ $RC1 -ne 0 -a $RC2 -ne 0 ]
180        then
181            $LTPBIN/tst_resm TPASS \
182                "Test #2: Mailer-Deamon reported delivery failure"
183        else
184            $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
185            "Test #2: Mailer-Deamon failed to report delivery failure. Reason:"
186            TFAILCNT=$(( $TFAILCNT+1 ))
187        fi
188    fi
189fi
190    
191
192# Test #3
193# Test that mail non_existant_user@localhost will result in delivery failure.
194# Mailer-Deamon will report this failure.
195
196export TCID=mail03
197export TST_COUNT=3
198RC=0
199RC1=0
200RC2=0
201
202$LTPBIN/tst_resm TINFO \
203    "Test #3: mail non_existant_user@localhost will fail"
204$LTPBIN/tst_resm TINFO "Test #3: to deliver the mail. Mailer daemon should"
205$LTPBIN/tst_resm TINFO "Test #3: report this failure."
206
207cat > $LTPTMP/tst_mail.in <<EOF
208This is a test email.
209EOF
210
211mail -s "Test" non_existant_userr@localhost < $LTPTMP/tst_mail.in \
212     &>$LTPTMP/tst_mail.out
213if [ $? -ne 0 ]
214then
215    $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
216        "Test #3: mail command failed. Reason: "
217    TFAILCNT=$(( $TFAILCNT+1 ))
218else
219    # check if Mailer-Deamon reported any delivery failure.    
220    # but wait for the mail to arrive first, sleep 5s.
221    sleep 5s
222    echo "d" | mail -u root &>$LTPTMP/tst_mail.res
223    RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3, "Mailer-Daemon")}' \
224	$LTPTMP/tst_mail.res)
225    RC2=$(awk '/^>N/ {print match($9 $10 $11, "Maildeliveryfailed:")}' \
226        $LTPTMP/tst_mail.res)
227#####
228# Some mailers (e.g., Red Hat's sendmail) print different messages 
229#####
230    [ -z "$RC2" -o "X$RC2" = "X0" ] && \
231	RC2=$(awk '/^>N/ {print match($9 $10, "Returnedmail:")}' \
232		$LTPTMP/tst_mail.res)
233    if [ -z "$RC1" -a -z "$RC2" ]
234    then
235        $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
236        "Test #3: No new mail for root. Reason:"
237        TFAILCNT=$(( $TFAILCNT+1 ))
238    else
239        if [ $RC1 -ne 0 -a $RC2 -ne 0 ]
240        then
241            $LTPBIN/tst_resm TPASS \
242                "Test #3: Mailer-Deamon reported delivery failure"
243        else
244            $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
245            "Test #3: Mailer-Deamon failed to report delivery failure. Reason:"
246            TFAILCNT=$(( $TFAILCNT+1 ))
247        fi
248    fi
249fi
250
251# Test #4 
252# Test that mail -c user@domain option will carbon copy that user.
253
254export TCID=mail04
255export TST_COUNT=4
256RC=0
257
258$LTPBIN/tst_resm TINFO "Test #4: Test that mail -c user@domain will"
259$LTPBIN/tst_resm TINFO "Test #4: carbon copy user@domain"
260
261# send mail to root and carbon copy mail_test 
262mail -s "Test" root@localhost -c mail_test@localhost < \
263    $LTPTMP/tst_mail.in &>$LTPTMP/tst_mail.out || RC=$?
264if [ $RC -ne 0 ]
265then
266     $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
267        "Test #4: mail command failed. Reason:"
268    TFAILCNT=$(( $TFAILCNT+1 ))
269else
270    # Check if mail_test received the mail and 
271    # also if root received the main copy of the email.
272    sleep 5s
273    echo "d" | mail -u root &>$LTPTMP/tst_mail.res
274    RC1=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
275    echo "d" | mail -u mail_test &>$LTPTMP/tst_mail.res
276    RC2=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
277    if [ $RC1 -ne 0 -a $RC2 -ne 0 ]
278    then
279        $LTPBIN/tst_resm TPASS \
280            "Test #4: Mail was carbon copied to user mail_test"
281    else
282        $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
283            "Test #4: mail failed to carbon copy user mail_test. Reason:"
284        TFAILCNT=$(( $TFAILCNT+1 ))
285    fi
286fi
287
288
289# Test #5 
290# Test that mail -b user@domain option will blind carbon copy that user.
291
292export TCID=mail05
293export TST_COUNT=5
294RC=0
295
296$LTPBIN/tst_resm TINFO "Test #5: Test that mail -b user@domain will"
297$LTPBIN/tst_resm TINFO "Test #5: blind carbon copy user@domain"
298
299# send mail to root and carbon copy mail_test 
300mail -s "Test" root@localhost -c mail_test@localhost < \
301    $LTPTMP/tst_mail.in &>$LTPTMP/tst_mail.out || RC=$?
302if [ $RC -ne 0 ]
303then
304     $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.out \
305        "Test #5: mail command failed. Reason:"
306    TFAILCNT=$(( $TFAILCNT+1 ))
307else
308    # Check if mail_test received the mail and 
309    # also if root received the main copy of the email.
310    sleep 5s
311    echo "d" | mail -u root &>$LTPTMP/tst_mail.res
312    RC1=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
313    echo "d" | mail -u mail_test &>$LTPTMP/tst_mail.res
314    RC2=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
315    if [ $RC1 -ne 0 -a $RC2 -ne 0 ]
316    then
317        $LTPBIN/tst_resm TPASS \
318            "Test #5: Mail was carbon copied to user mail_test"
319    else
320        $LTPBIN/tst_res TFAIL $LTPTMP/tst_mail.res \
321            "Test #5: mail failed to carbon copy user mail_testi. Reason:"
322        TFAILCNT=$(( $TFAILCNT+1 ))
323    fi
324fi
325    
326
327#CLEANUP & EXIT
328# remove all the temporary files created by this test.
329export TCID=CLEANUP
330export TST_COUNT=1
331
332$LTPBIN/tst_resm TINFO "Test CLEAN: Removing temporary files from $LTPTMP"
333rm -fr $LTPTMP/tst_mail* 
334
335$LTPBIN/tst_resm TINFO "Test CLEAN: Removing temporary user mail_test"
336userdel -r mail_test &>/dev/null
337
338exit $TFAILCNT
339