Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes
test.test_smtplib.DebuggingServerTests Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def testBasic
def testNOOP
def testRSET
def testNotImplemented
def testVRFY
def testSecondHELO
def testHELP
def testSend
def testSendBinary
def testSendNeedingDotQuote
def testSendNullSender
def testSendMessage
def testSendMessageWithAddresses
def testSendMessageWithSomeAddresses
def testSendMessageWithSpecifiedAddresses
def testSendMessageWithMultipleFrom
def testSendMessageResent
def testSendMessageMultipleResentRaises

Public Attributes

 real_getfqdn
 old_stdout
 output
 serv_evt
 client_evt
 old_DEBUGSTREAM
 serv
 port
 thread

Static Public Attributes

 maxDiff = None

Private Attributes

 _threads

Detailed Description

Definition at line 160 of file test_smtplib.py.


Member Function Documentation

Definition at line 164 of file test_smtplib.py.

00164 
00165     def setUp(self):
00166         self.real_getfqdn = socket.getfqdn
00167         socket.getfqdn = mock_socket.getfqdn
00168         # temporarily replace sys.stdout to capture DebuggingServer output
00169         self.old_stdout = sys.stdout
00170         self.output = io.StringIO()
00171         sys.stdout = self.output
00172 
00173         self._threads = support.threading_setup()
00174         self.serv_evt = threading.Event()
00175         self.client_evt = threading.Event()
00176         # Capture SMTPChannel debug output
00177         self.old_DEBUGSTREAM = smtpd.DEBUGSTREAM
00178         smtpd.DEBUGSTREAM = io.StringIO()
00179         # Pick a random unused port by passing 0 for the port number
00180         self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1))
00181         # Keep a note of what port was assigned
00182         self.port = self.serv.socket.getsockname()[1]
00183         serv_args = (self.serv, self.serv_evt, self.client_evt)
00184         self.thread = threading.Thread(target=debugging_server, args=serv_args)
00185         self.thread.start()
00186 
00187         # wait until server thread has assigned a port number
00188         self.serv_evt.wait()
00189         self.serv_evt.clear()

Here is the caller graph for this function:

Definition at line 190 of file test_smtplib.py.

00190 
00191     def tearDown(self):
00192         socket.getfqdn = self.real_getfqdn
00193         # indicate that the client is finished
00194         self.client_evt.set()
00195         # wait for the server thread to terminate
00196         self.serv_evt.wait()
00197         self.thread.join()
00198         support.threading_cleanup(*self._threads)
00199         # restore sys.stdout
00200         sys.stdout = self.old_stdout
00201         # restore DEBUGSTREAM
00202         smtpd.DEBUGSTREAM.close()
00203         smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM

Here is the caller graph for this function:

Definition at line 204 of file test_smtplib.py.

00204 
00205     def testBasic(self):
00206         # connect
00207         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00208         smtp.quit()

Definition at line 245 of file test_smtplib.py.

00245 
00246     def testHELP(self):
00247         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00248         self.assertEqual(smtp.help(), b'Error: command "HELP" not implemented')
00249         smtp.quit()

Here is the call graph for this function:

Definition at line 209 of file test_smtplib.py.

00209 
00210     def testNOOP(self):
00211         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00212         expected = (250, b'Ok')
00213         self.assertEqual(smtp.noop(), expected)
00214         smtp.quit()

Here is the call graph for this function:

Definition at line 221 of file test_smtplib.py.

00221 
00222     def testNotImplemented(self):
00223         # EHLO isn't implemented in DebuggingServer
00224         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00225         expected = (502, b'Error: command "EHLO" not implemented')
00226         self.assertEqual(smtp.ehlo(), expected)
00227         smtp.quit()

Here is the call graph for this function:

Definition at line 215 of file test_smtplib.py.

00215 
00216     def testRSET(self):
00217         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00218         expected = (250, b'Ok')
00219         self.assertEqual(smtp.rset(), expected)
00220         smtp.quit()

Here is the call graph for this function:

Definition at line 236 of file test_smtplib.py.

00236 
00237     def testSecondHELO(self):
00238         # check that a second HELO returns a message that it's a duplicate
00239         # (this behavior is specific to smtpd.SMTPChannel)
00240         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00241         smtp.helo()
00242         expected = (503, b'Duplicate HELO/EHLO')
00243         self.assertEqual(smtp.helo(), expected)
00244         smtp.quit()

Here is the call graph for this function:

Definition at line 250 of file test_smtplib.py.

00250 
00251     def testSend(self):
00252         # connect and send mail
00253         m = 'A test message'
00254         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00255         smtp.sendmail('John', 'Sally', m)
00256         # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
00257         # in asyncore.  This sleep might help, but should really be fixed
00258         # properly by using an Event variable.
00259         time.sleep(0.01)
00260         smtp.quit()
00261 
00262         self.client_evt.set()
00263         self.serv_evt.wait()
00264         self.output.flush()
00265         mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
00266         self.assertEqual(self.output.getvalue(), mexpect)

Here is the call graph for this function:

Definition at line 267 of file test_smtplib.py.

00267 
00268     def testSendBinary(self):
00269         m = b'A test message'
00270         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00271         smtp.sendmail('John', 'Sally', m)
00272         # XXX (see comment in testSend)
00273         time.sleep(0.01)
00274         smtp.quit()
00275 
00276         self.client_evt.set()
00277         self.serv_evt.wait()
00278         self.output.flush()
00279         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.decode('ascii'), MSG_END)
00280         self.assertEqual(self.output.getvalue(), mexpect)

Here is the call graph for this function:

Definition at line 313 of file test_smtplib.py.

00313 
00314     def testSendMessage(self):
00315         m = email.mime.text.MIMEText('A test message')
00316         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00317         smtp.send_message(m, from_addr='John', to_addrs='Sally')
00318         # XXX (see comment in testSend)
00319         time.sleep(0.01)
00320         smtp.quit()
00321 
00322         self.client_evt.set()
00323         self.serv_evt.wait()
00324         self.output.flush()
00325         # Add the X-Peer header that DebuggingServer adds
00326         m['X-Peer'] = socket.gethostbyname('localhost')
00327         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
00328         self.assertEqual(self.output.getvalue(), mexpect)

Here is the call graph for this function:

Definition at line 477 of file test_smtplib.py.

00477 
00478     def testSendMessageMultipleResentRaises(self):
00479         m = email.mime.text.MIMEText('A test message')
00480         m['From'] = 'foo@bar.com'
00481         m['To'] = 'John'
00482         m['CC'] = 'Sally, Fred'
00483         m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
00484         m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
00485         m['Resent-From'] = 'holy@grail.net'
00486         m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff'
00487         m['Resent-Bcc'] = 'doe@losthope.net'
00488         m['Resent-Date'] = 'Thu, 2 Jan 1970 17:42:00 +0000'
00489         m['Resent-To'] = 'holy@grail.net'
00490         m['Resent-From'] = 'Martha <my_mom@great.cooker.com>, Jeff'
00491         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00492         with self.assertRaises(ValueError):
00493             smtp.send_message(m)
00494         smtp.close()

Here is the call graph for this function:

Definition at line 443 of file test_smtplib.py.

00443 
00444     def testSendMessageResent(self):
00445         m = email.mime.text.MIMEText('A test message')
00446         m['From'] = 'foo@bar.com'
00447         m['To'] = 'John'
00448         m['CC'] = 'Sally, Fred'
00449         m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
00450         m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
00451         m['Resent-From'] = 'holy@grail.net'
00452         m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff'
00453         m['Resent-Bcc'] = 'doe@losthope.net'
00454         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00455         smtp.send_message(m)
00456         # XXX (see comment in testSend)
00457         time.sleep(0.01)
00458         smtp.quit()
00459 
00460         self.client_evt.set()
00461         self.serv_evt.wait()
00462         self.output.flush()
00463         # The Resent-Bcc headers are deleted before serialization.
00464         del m['Bcc']
00465         del m['Resent-Bcc']
00466         # Add the X-Peer header that DebuggingServer adds
00467         m['X-Peer'] = socket.gethostbyname('localhost')
00468         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
00469         self.assertEqual(self.output.getvalue(), mexpect)
00470         debugout = smtpd.DEBUGSTREAM.getvalue()
00471         sender = re.compile("^sender: holy@grail.net$", re.MULTILINE)
00472         self.assertRegex(debugout, sender)
00473         for addr in ('my_mom@great.cooker.com', 'Jeff', 'doe@losthope.net'):
00474             to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
00475                                  re.MULTILINE)
00476             self.assertRegex(debugout, to_addr)

Here is the call graph for this function:

Definition at line 329 of file test_smtplib.py.

00329 
00330     def testSendMessageWithAddresses(self):
00331         m = email.mime.text.MIMEText('A test message')
00332         m['From'] = 'foo@bar.com'
00333         m['To'] = 'John'
00334         m['CC'] = 'Sally, Fred'
00335         m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
00336         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00337         smtp.send_message(m)
00338         # XXX (see comment in testSend)
00339         time.sleep(0.01)
00340         smtp.quit()
00341         # make sure the Bcc header is still in the message.
00342         self.assertEqual(m['Bcc'], 'John Root <root@localhost>, "Dinsdale" '
00343                                     '<warped@silly.walks.com>')
00344 
00345         self.client_evt.set()
00346         self.serv_evt.wait()
00347         self.output.flush()
00348         # Add the X-Peer header that DebuggingServer adds
00349         m['X-Peer'] = socket.gethostbyname('localhost')
00350         # The Bcc header should not be transmitted.
00351         del m['Bcc']
00352         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
00353         self.assertEqual(self.output.getvalue(), mexpect)
00354         debugout = smtpd.DEBUGSTREAM.getvalue()
00355         sender = re.compile("^sender: foo@bar.com$", re.MULTILINE)
00356         self.assertRegex(debugout, sender)
00357         for addr in ('John', 'Sally', 'Fred', 'root@localhost',
00358                      'warped@silly.walks.com'):
00359             to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
00360                                  re.MULTILINE)
00361             self.assertRegex(debugout, to_addr)

Here is the call graph for this function:

Definition at line 416 of file test_smtplib.py.

00416 
00417     def testSendMessageWithMultipleFrom(self):
00418         # Sender overrides To
00419         m = email.mime.text.MIMEText('A test message')
00420         m['From'] = 'Bernard, Bianca'
00421         m['Sender'] = 'the_rescuers@Rescue-Aid-Society.com'
00422         m['To'] = 'John, Dinsdale'
00423         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00424         smtp.send_message(m)
00425         # XXX (see comment in testSend)
00426         time.sleep(0.01)
00427         smtp.quit()
00428 
00429         self.client_evt.set()
00430         self.serv_evt.wait()
00431         self.output.flush()
00432         # Add the X-Peer header that DebuggingServer adds
00433         m['X-Peer'] = socket.gethostbyname('localhost')
00434         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
00435         self.assertEqual(self.output.getvalue(), mexpect)
00436         debugout = smtpd.DEBUGSTREAM.getvalue()
00437         sender = re.compile("^sender: the_rescuers@Rescue-Aid-Society.com$", re.MULTILINE)
00438         self.assertRegex(debugout, sender)
00439         for addr in ('John', 'Dinsdale'):
00440             to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
00441                                  re.MULTILINE)
00442             self.assertRegex(debugout, to_addr)

Here is the call graph for this function:

Definition at line 362 of file test_smtplib.py.

00362 
00363     def testSendMessageWithSomeAddresses(self):
00364         # Make sure nothing breaks if not all of the three 'to' headers exist
00365         m = email.mime.text.MIMEText('A test message')
00366         m['From'] = 'foo@bar.com'
00367         m['To'] = 'John, Dinsdale'
00368         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00369         smtp.send_message(m)
00370         # XXX (see comment in testSend)
00371         time.sleep(0.01)
00372         smtp.quit()
00373 
00374         self.client_evt.set()
00375         self.serv_evt.wait()
00376         self.output.flush()
00377         # Add the X-Peer header that DebuggingServer adds
00378         m['X-Peer'] = socket.gethostbyname('localhost')
00379         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
00380         self.assertEqual(self.output.getvalue(), mexpect)
00381         debugout = smtpd.DEBUGSTREAM.getvalue()
00382         sender = re.compile("^sender: foo@bar.com$", re.MULTILINE)
00383         self.assertRegex(debugout, sender)
00384         for addr in ('John', 'Dinsdale'):
00385             to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
00386                                  re.MULTILINE)
00387             self.assertRegex(debugout, to_addr)

Here is the call graph for this function:

Definition at line 388 of file test_smtplib.py.

00388 
00389     def testSendMessageWithSpecifiedAddresses(self):
00390         # Make sure addresses specified in call override those in message.
00391         m = email.mime.text.MIMEText('A test message')
00392         m['From'] = 'foo@bar.com'
00393         m['To'] = 'John, Dinsdale'
00394         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00395         smtp.send_message(m, from_addr='joe@example.com', to_addrs='foo@example.net')
00396         # XXX (see comment in testSend)
00397         time.sleep(0.01)
00398         smtp.quit()
00399 
00400         self.client_evt.set()
00401         self.serv_evt.wait()
00402         self.output.flush()
00403         # Add the X-Peer header that DebuggingServer adds
00404         m['X-Peer'] = socket.gethostbyname('localhost')
00405         mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
00406         self.assertEqual(self.output.getvalue(), mexpect)
00407         debugout = smtpd.DEBUGSTREAM.getvalue()
00408         sender = re.compile("^sender: joe@example.com$", re.MULTILINE)
00409         self.assertRegex(debugout, sender)
00410         for addr in ('John', 'Dinsdale'):
00411             to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
00412                                  re.MULTILINE)
00413             self.assertNotRegex(debugout, to_addr)
00414         recip = re.compile(r"^recips: .*'foo@example.net'.*$", re.MULTILINE)
00415         self.assertRegex(debugout, recip)

Here is the call graph for this function:

Definition at line 281 of file test_smtplib.py.

00281 
00282     def testSendNeedingDotQuote(self):
00283         # Issue 12283
00284         m = '.A test\n.mes.sage.'
00285         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00286         smtp.sendmail('John', 'Sally', m)
00287         # XXX (see comment in testSend)
00288         time.sleep(0.01)
00289         smtp.quit()
00290 
00291         self.client_evt.set()
00292         self.serv_evt.wait()
00293         self.output.flush()
00294         mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
00295         self.assertEqual(self.output.getvalue(), mexpect)

Here is the call graph for this function:

Definition at line 296 of file test_smtplib.py.

00296 
00297     def testSendNullSender(self):
00298         m = 'A test message'
00299         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00300         smtp.sendmail('<>', 'Sally', m)
00301         # XXX (see comment in testSend)
00302         time.sleep(0.01)
00303         smtp.quit()
00304 
00305         self.client_evt.set()
00306         self.serv_evt.wait()
00307         self.output.flush()
00308         mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
00309         self.assertEqual(self.output.getvalue(), mexpect)
00310         debugout = smtpd.DEBUGSTREAM.getvalue()
00311         sender = re.compile("^sender: <>$", re.MULTILINE)
00312         self.assertRegex(debugout, sender)

Here is the call graph for this function:

Definition at line 228 of file test_smtplib.py.

00228 
00229     def testVRFY(self):
00230         # VRFY isn't implemented in DebuggingServer
00231         smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
00232         expected = (502, b'Error: command "VRFY" not implemented')
00233         self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
00234         self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
00235         smtp.quit()

Here is the call graph for this function:


Member Data Documentation

Definition at line 172 of file test_smtplib.py.

Definition at line 174 of file test_smtplib.py.

Definition at line 162 of file test_smtplib.py.

Definition at line 176 of file test_smtplib.py.

Definition at line 168 of file test_smtplib.py.

Definition at line 169 of file test_smtplib.py.

Definition at line 181 of file test_smtplib.py.

Definition at line 165 of file test_smtplib.py.

Definition at line 179 of file test_smtplib.py.

Definition at line 173 of file test_smtplib.py.

Definition at line 183 of file test_smtplib.py.


The documentation for this class was generated from the following file: