»Core Development>Code coverage>Lib/test/test_smtplib.py

Python code coverage for Lib/test/test_smtplib.py

1n/aimport asyncore
2n/aimport base64
3n/aimport email.mime.text
4n/afrom email.message import EmailMessage
5n/afrom email.base64mime import body_encode as encode_base64
6n/aimport email.utils
7n/aimport hmac
8n/aimport socket
9n/aimport smtpd
10n/aimport smtplib
11n/aimport io
12n/aimport re
13n/aimport sys
14n/aimport time
15n/aimport select
16n/aimport errno
17n/aimport textwrap
19n/aimport unittest
20n/afrom test import support, mock_socket
23n/a import threading
24n/aexcept ImportError:
25n/a threading = None
27n/aHOST = support.HOST
29n/aif sys.platform == 'darwin':
30n/a # select.poll returns a select.POLLHUP at the end of the tests
31n/a # on darwin, so just ignore it
32n/a def handle_expt(self):
33n/a pass
34n/a smtpd.SMTPChannel.handle_expt = handle_expt
37n/adef server(evt, buf, serv):
38n/a serv.listen()
39n/a evt.set()
40n/a try:
41n/a conn, addr = serv.accept()
42n/a except socket.timeout:
43n/a pass
44n/a else:
45n/a n = 500
46n/a while buf and n > 0:
47n/a r, w, e = select.select([], [conn], [])
48n/a if w:
49n/a sent = conn.send(buf)
50n/a buf = buf[sent:]
52n/a n -= 1
54n/a conn.close()
55n/a finally:
56n/a serv.close()
57n/a evt.set()
59n/aclass GeneralTests(unittest.TestCase):
61n/a def setUp(self):
62n/a smtplib.socket = mock_socket
63n/a self.port = 25
65n/a def tearDown(self):
66n/a smtplib.socket = socket
68n/a # This method is no longer used but is retained for backward compatibility,
69n/a # so test to make sure it still works.
70n/a def testQuoteData(self):
71n/a teststr = "abc\n.jkl\rfoo\r\n..blue"
72n/a expected = "abc\r\n..jkl\r\nfoo\r\n...blue"
73n/a self.assertEqual(expected, smtplib.quotedata(teststr))
75n/a def testBasic1(self):
76n/a mock_socket.reply_with(b"220 Hola mundo")
77n/a # connects
78n/a smtp = smtplib.SMTP(HOST, self.port)
79n/a smtp.close()
81n/a def testSourceAddress(self):
82n/a mock_socket.reply_with(b"220 Hola mundo")
83n/a # connects
84n/a smtp = smtplib.SMTP(HOST, self.port,
85n/a source_address=('',19876))
86n/a self.assertEqual(smtp.source_address, ('', 19876))
87n/a smtp.close()
89n/a def testBasic2(self):
90n/a mock_socket.reply_with(b"220 Hola mundo")
91n/a # connects, include port in host name
92n/a smtp = smtplib.SMTP("%s:%s" % (HOST, self.port))
93n/a smtp.close()
95n/a def testLocalHostName(self):
96n/a mock_socket.reply_with(b"220 Hola mundo")
97n/a # check that supplied local_hostname is used
98n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost")
99n/a self.assertEqual(smtp.local_hostname, "testhost")
100n/a smtp.close()
102n/a def testTimeoutDefault(self):
103n/a mock_socket.reply_with(b"220 Hola mundo")
104n/a self.assertIsNone(mock_socket.getdefaulttimeout())
105n/a mock_socket.setdefaulttimeout(30)
106n/a self.assertEqual(mock_socket.getdefaulttimeout(), 30)
107n/a try:
108n/a smtp = smtplib.SMTP(HOST, self.port)
109n/a finally:
110n/a mock_socket.setdefaulttimeout(None)
111n/a self.assertEqual(smtp.sock.gettimeout(), 30)
112n/a smtp.close()
114n/a def testTimeoutNone(self):
115n/a mock_socket.reply_with(b"220 Hola mundo")
116n/a self.assertIsNone(socket.getdefaulttimeout())
117n/a socket.setdefaulttimeout(30)
118n/a try:
119n/a smtp = smtplib.SMTP(HOST, self.port, timeout=None)
120n/a finally:
121n/a socket.setdefaulttimeout(None)
122n/a self.assertIsNone(smtp.sock.gettimeout())
123n/a smtp.close()
125n/a def testTimeoutValue(self):
126n/a mock_socket.reply_with(b"220 Hola mundo")
127n/a smtp = smtplib.SMTP(HOST, self.port, timeout=30)
128n/a self.assertEqual(smtp.sock.gettimeout(), 30)
129n/a smtp.close()
131n/a def test_debuglevel(self):
132n/a mock_socket.reply_with(b"220 Hello world")
133n/a smtp = smtplib.SMTP()
134n/a smtp.set_debuglevel(1)
135n/a with support.captured_stderr() as stderr:
136n/a smtp.connect(HOST, self.port)
137n/a smtp.close()
138n/a expected = re.compile(r"^connect:", re.MULTILINE)
139n/a self.assertRegex(stderr.getvalue(), expected)
141n/a def test_debuglevel_2(self):
142n/a mock_socket.reply_with(b"220 Hello world")
143n/a smtp = smtplib.SMTP()
144n/a smtp.set_debuglevel(2)
145n/a with support.captured_stderr() as stderr:
146n/a smtp.connect(HOST, self.port)
147n/a smtp.close()
148n/a expected = re.compile(r"^\d{2}:\d{2}:\d{2}\.\d{6} connect: ",
149n/a re.MULTILINE)
150n/a self.assertRegex(stderr.getvalue(), expected)
153n/a# Test server thread using the specified SMTP server class
154n/adef debugging_server(serv, serv_evt, client_evt):
155n/a serv_evt.set()
157n/a try:
158n/a if hasattr(select, 'poll'):
159n/a poll_fun = asyncore.poll2
160n/a else:
161n/a poll_fun = asyncore.poll
163n/a n = 1000
164n/a while asyncore.socket_map and n > 0:
165n/a poll_fun(0.01, asyncore.socket_map)
167n/a # when the client conversation is finished, it will
168n/a # set client_evt, and it's then ok to kill the server
169n/a if client_evt.is_set():
170n/a serv.close()
171n/a break
173n/a n -= 1
175n/a except socket.timeout:
176n/a pass
177n/a finally:
178n/a if not client_evt.is_set():
179n/a # allow some time for the client to read the result
180n/a time.sleep(0.5)
181n/a serv.close()
182n/a asyncore.close_all()
183n/a serv_evt.set()
185n/aMSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
186n/aMSG_END = '------------ END MESSAGE ------------\n'
188n/a# NOTE: Some SMTP objects in the tests below are created with a non-default
189n/a# local_hostname argument to the constructor, since (on some systems) the FQDN
190n/a# lookup caused by the default local_hostname sometimes takes so long that the
191n/a# test server times out, causing the test to fail.
193n/a# Test behavior of smtpd.DebuggingServer
194n/a@unittest.skipUnless(threading, 'Threading required for this test.')
195n/aclass DebuggingServerTests(unittest.TestCase):
197n/a maxDiff = None
199n/a def setUp(self):
200n/a self.real_getfqdn = socket.getfqdn
201n/a socket.getfqdn = mock_socket.getfqdn
202n/a # temporarily replace sys.stdout to capture DebuggingServer output
203n/a self.old_stdout = sys.stdout
204n/a self.output = io.StringIO()
205n/a sys.stdout = self.output
207n/a self.serv_evt = threading.Event()
208n/a self.client_evt = threading.Event()
209n/a # Capture SMTPChannel debug output
210n/a self.old_DEBUGSTREAM = smtpd.DEBUGSTREAM
211n/a smtpd.DEBUGSTREAM = io.StringIO()
212n/a # Pick a random unused port by passing 0 for the port number
213n/a self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1),
214n/a decode_data=True)
215n/a # Keep a note of what port was assigned
216n/a self.port = self.serv.socket.getsockname()[1]
217n/a serv_args = (self.serv, self.serv_evt, self.client_evt)
218n/a self.thread = threading.Thread(target=debugging_server, args=serv_args)
219n/a self.thread.start()
221n/a # wait until server thread has assigned a port number
222n/a self.serv_evt.wait()
223n/a self.serv_evt.clear()
225n/a def tearDown(self):
226n/a socket.getfqdn = self.real_getfqdn
227n/a # indicate that the client is finished
228n/a self.client_evt.set()
229n/a # wait for the server thread to terminate
230n/a self.serv_evt.wait()
231n/a self.thread.join()
232n/a # restore sys.stdout
233n/a sys.stdout = self.old_stdout
234n/a # restore DEBUGSTREAM
235n/a smtpd.DEBUGSTREAM.close()
236n/a smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM
238n/a def testBasic(self):
239n/a # connect
240n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
241n/a smtp.quit()
243n/a def testSourceAddress(self):
244n/a # connect
245n/a port = support.find_unused_port()
246n/a try:
247n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
248n/a timeout=3, source_address=('', port))
249n/a self.assertEqual(smtp.source_address, ('', port))
250n/a self.assertEqual(smtp.local_hostname, 'localhost')
251n/a smtp.quit()
252n/a except OSError as e:
253n/a if e.errno == errno.EADDRINUSE:
254n/a self.skipTest("couldn't bind to port %d" % port)
255n/a raise
257n/a def testNOOP(self):
258n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
259n/a expected = (250, b'OK')
260n/a self.assertEqual(smtp.noop(), expected)
261n/a smtp.quit()
263n/a def testRSET(self):
264n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
265n/a expected = (250, b'OK')
266n/a self.assertEqual(smtp.rset(), expected)
267n/a smtp.quit()
269n/a def testELHO(self):
270n/a # EHLO isn't implemented in DebuggingServer
271n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
272n/a expected = (250, b'\nSIZE 33554432\nHELP')
273n/a self.assertEqual(smtp.ehlo(), expected)
274n/a smtp.quit()
276n/a def testEXPNNotImplemented(self):
277n/a # EXPN isn't implemented in DebuggingServer
278n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
279n/a expected = (502, b'EXPN not implemented')
280n/a smtp.putcmd('EXPN')
281n/a self.assertEqual(smtp.getreply(), expected)
282n/a smtp.quit()
284n/a def testVRFY(self):
285n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
286n/a expected = (252, b'Cannot VRFY user, but will accept message ' + \
287n/a b'and attempt delivery')
288n/a self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
289n/a self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
290n/a smtp.quit()
292n/a def testSecondHELO(self):
293n/a # check that a second HELO returns a message that it's a duplicate
294n/a # (this behavior is specific to smtpd.SMTPChannel)
295n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
296n/a smtp.helo()
297n/a expected = (503, b'Duplicate HELO/EHLO')
298n/a self.assertEqual(smtp.helo(), expected)
299n/a smtp.quit()
301n/a def testHELP(self):
302n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
303n/a self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \
305n/a smtp.quit()
307n/a def testSend(self):
308n/a # connect and send mail
309n/a m = 'A test message'
310n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
311n/a smtp.sendmail('John', 'Sally', m)
312n/a # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
313n/a # in asyncore. This sleep might help, but should really be fixed
314n/a # properly by using an Event variable.
315n/a time.sleep(0.01)
316n/a smtp.quit()
318n/a self.client_evt.set()
319n/a self.serv_evt.wait()
320n/a self.output.flush()
321n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
322n/a self.assertEqual(self.output.getvalue(), mexpect)
324n/a def testSendBinary(self):
325n/a m = b'A test message'
326n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
327n/a smtp.sendmail('John', 'Sally', m)
328n/a # XXX (see comment in testSend)
329n/a time.sleep(0.01)
330n/a smtp.quit()
332n/a self.client_evt.set()
333n/a self.serv_evt.wait()
334n/a self.output.flush()
335n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m.decode('ascii'), MSG_END)
336n/a self.assertEqual(self.output.getvalue(), mexpect)
338n/a def testSendNeedingDotQuote(self):
339n/a # Issue 12283
340n/a m = '.A test\n.mes.sage.'
341n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
342n/a smtp.sendmail('John', 'Sally', m)
343n/a # XXX (see comment in testSend)
344n/a time.sleep(0.01)
345n/a smtp.quit()
347n/a self.client_evt.set()
348n/a self.serv_evt.wait()
349n/a self.output.flush()
350n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
351n/a self.assertEqual(self.output.getvalue(), mexpect)
353n/a def testSendNullSender(self):
354n/a m = 'A test message'
355n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
356n/a smtp.sendmail('<>', 'Sally', m)
357n/a # XXX (see comment in testSend)
358n/a time.sleep(0.01)
359n/a smtp.quit()
361n/a self.client_evt.set()
362n/a self.serv_evt.wait()
363n/a self.output.flush()
364n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
365n/a self.assertEqual(self.output.getvalue(), mexpect)
366n/a debugout = smtpd.DEBUGSTREAM.getvalue()
367n/a sender = re.compile("^sender: <>$", re.MULTILINE)
368n/a self.assertRegex(debugout, sender)
370n/a def testSendMessage(self):
371n/a m = email.mime.text.MIMEText('A test message')
372n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
373n/a smtp.send_message(m, from_addr='John', to_addrs='Sally')
374n/a # XXX (see comment in testSend)
375n/a time.sleep(0.01)
376n/a smtp.quit()
378n/a self.client_evt.set()
379n/a self.serv_evt.wait()
380n/a self.output.flush()
381n/a # Add the X-Peer header that DebuggingServer adds
382n/a m['X-Peer'] = socket.gethostbyname('localhost')
383n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
384n/a self.assertEqual(self.output.getvalue(), mexpect)
386n/a def testSendMessageWithAddresses(self):
387n/a m = email.mime.text.MIMEText('A test message')
388n/a m['From'] = 'foo@bar.com'
389n/a m['To'] = 'John'
390n/a m['CC'] = 'Sally, Fred'
391n/a m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
392n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
393n/a smtp.send_message(m)
394n/a # XXX (see comment in testSend)
395n/a time.sleep(0.01)
396n/a smtp.quit()
397n/a # make sure the Bcc header is still in the message.
398n/a self.assertEqual(m['Bcc'], 'John Root <root@localhost>, "Dinsdale" '
399n/a '<warped@silly.walks.com>')
401n/a self.client_evt.set()
402n/a self.serv_evt.wait()
403n/a self.output.flush()
404n/a # Add the X-Peer header that DebuggingServer adds
405n/a m['X-Peer'] = socket.gethostbyname('localhost')
406n/a # The Bcc header should not be transmitted.
407n/a del m['Bcc']
408n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
409n/a self.assertEqual(self.output.getvalue(), mexpect)
410n/a debugout = smtpd.DEBUGSTREAM.getvalue()
411n/a sender = re.compile("^sender: foo@bar.com$", re.MULTILINE)
412n/a self.assertRegex(debugout, sender)
413n/a for addr in ('John', 'Sally', 'Fred', 'root@localhost',
414n/a 'warped@silly.walks.com'):
415n/a to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
416n/a re.MULTILINE)
417n/a self.assertRegex(debugout, to_addr)
419n/a def testSendMessageWithSomeAddresses(self):
420n/a # Make sure nothing breaks if not all of the three 'to' headers exist
421n/a m = email.mime.text.MIMEText('A test message')
422n/a m['From'] = 'foo@bar.com'
423n/a m['To'] = 'John, Dinsdale'
424n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
425n/a smtp.send_message(m)
426n/a # XXX (see comment in testSend)
427n/a time.sleep(0.01)
428n/a smtp.quit()
430n/a self.client_evt.set()
431n/a self.serv_evt.wait()
432n/a self.output.flush()
433n/a # Add the X-Peer header that DebuggingServer adds
434n/a m['X-Peer'] = socket.gethostbyname('localhost')
435n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
436n/a self.assertEqual(self.output.getvalue(), mexpect)
437n/a debugout = smtpd.DEBUGSTREAM.getvalue()
438n/a sender = re.compile("^sender: foo@bar.com$", re.MULTILINE)
439n/a self.assertRegex(debugout, sender)
440n/a for addr in ('John', 'Dinsdale'):
441n/a to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
442n/a re.MULTILINE)
443n/a self.assertRegex(debugout, to_addr)
445n/a def testSendMessageWithSpecifiedAddresses(self):
446n/a # Make sure addresses specified in call override those in message.
447n/a m = email.mime.text.MIMEText('A test message')
448n/a m['From'] = 'foo@bar.com'
449n/a m['To'] = 'John, Dinsdale'
450n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
451n/a smtp.send_message(m, from_addr='joe@example.com', to_addrs='foo@example.net')
452n/a # XXX (see comment in testSend)
453n/a time.sleep(0.01)
454n/a smtp.quit()
456n/a self.client_evt.set()
457n/a self.serv_evt.wait()
458n/a self.output.flush()
459n/a # Add the X-Peer header that DebuggingServer adds
460n/a m['X-Peer'] = socket.gethostbyname('localhost')
461n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
462n/a self.assertEqual(self.output.getvalue(), mexpect)
463n/a debugout = smtpd.DEBUGSTREAM.getvalue()
464n/a sender = re.compile("^sender: joe@example.com$", re.MULTILINE)
465n/a self.assertRegex(debugout, sender)
466n/a for addr in ('John', 'Dinsdale'):
467n/a to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
468n/a re.MULTILINE)
469n/a self.assertNotRegex(debugout, to_addr)
470n/a recip = re.compile(r"^recips: .*'foo@example.net'.*$", re.MULTILINE)
471n/a self.assertRegex(debugout, recip)
473n/a def testSendMessageWithMultipleFrom(self):
474n/a # Sender overrides To
475n/a m = email.mime.text.MIMEText('A test message')
476n/a m['From'] = 'Bernard, Bianca'
477n/a m['Sender'] = 'the_rescuers@Rescue-Aid-Society.com'
478n/a m['To'] = 'John, Dinsdale'
479n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
480n/a smtp.send_message(m)
481n/a # XXX (see comment in testSend)
482n/a time.sleep(0.01)
483n/a smtp.quit()
485n/a self.client_evt.set()
486n/a self.serv_evt.wait()
487n/a self.output.flush()
488n/a # Add the X-Peer header that DebuggingServer adds
489n/a m['X-Peer'] = socket.gethostbyname('localhost')
490n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
491n/a self.assertEqual(self.output.getvalue(), mexpect)
492n/a debugout = smtpd.DEBUGSTREAM.getvalue()
493n/a sender = re.compile("^sender: the_rescuers@Rescue-Aid-Society.com$", re.MULTILINE)
494n/a self.assertRegex(debugout, sender)
495n/a for addr in ('John', 'Dinsdale'):
496n/a to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
497n/a re.MULTILINE)
498n/a self.assertRegex(debugout, to_addr)
500n/a def testSendMessageResent(self):
501n/a m = email.mime.text.MIMEText('A test message')
502n/a m['From'] = 'foo@bar.com'
503n/a m['To'] = 'John'
504n/a m['CC'] = 'Sally, Fred'
505n/a m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
506n/a m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
507n/a m['Resent-From'] = 'holy@grail.net'
508n/a m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff'
509n/a m['Resent-Bcc'] = 'doe@losthope.net'
510n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
511n/a smtp.send_message(m)
512n/a # XXX (see comment in testSend)
513n/a time.sleep(0.01)
514n/a smtp.quit()
516n/a self.client_evt.set()
517n/a self.serv_evt.wait()
518n/a self.output.flush()
519n/a # The Resent-Bcc headers are deleted before serialization.
520n/a del m['Bcc']
521n/a del m['Resent-Bcc']
522n/a # Add the X-Peer header that DebuggingServer adds
523n/a m['X-Peer'] = socket.gethostbyname('localhost')
524n/a mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
525n/a self.assertEqual(self.output.getvalue(), mexpect)
526n/a debugout = smtpd.DEBUGSTREAM.getvalue()
527n/a sender = re.compile("^sender: holy@grail.net$", re.MULTILINE)
528n/a self.assertRegex(debugout, sender)
529n/a for addr in ('my_mom@great.cooker.com', 'Jeff', 'doe@losthope.net'):
530n/a to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
531n/a re.MULTILINE)
532n/a self.assertRegex(debugout, to_addr)
534n/a def testSendMessageMultipleResentRaises(self):
535n/a m = email.mime.text.MIMEText('A test message')
536n/a m['From'] = 'foo@bar.com'
537n/a m['To'] = 'John'
538n/a m['CC'] = 'Sally, Fred'
539n/a m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
540n/a m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
541n/a m['Resent-From'] = 'holy@grail.net'
542n/a m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff'
543n/a m['Resent-Bcc'] = 'doe@losthope.net'
544n/a m['Resent-Date'] = 'Thu, 2 Jan 1970 17:42:00 +0000'
545n/a m['Resent-To'] = 'holy@grail.net'
546n/a m['Resent-From'] = 'Martha <my_mom@great.cooker.com>, Jeff'
547n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
548n/a with self.assertRaises(ValueError):
549n/a smtp.send_message(m)
550n/a smtp.close()
552n/aclass NonConnectingTests(unittest.TestCase):
554n/a def testNotConnected(self):
555n/a # Test various operations on an unconnected SMTP object that
556n/a # should raise exceptions (at present the attempt in SMTP.send
557n/a # to reference the nonexistent 'sock' attribute of the SMTP object
558n/a # causes an AttributeError)
559n/a smtp = smtplib.SMTP()
560n/a self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
561n/a self.assertRaises(smtplib.SMTPServerDisconnected,
562n/a smtp.send, 'test msg')
564n/a def testNonnumericPort(self):
565n/a # check that non-numeric port raises OSError
566n/a self.assertRaises(OSError, smtplib.SMTP,
567n/a "localhost", "bogus")
568n/a self.assertRaises(OSError, smtplib.SMTP,
569n/a "localhost:bogus")
572n/a# test response of client to a non-successful HELO message
573n/a@unittest.skipUnless(threading, 'Threading required for this test.')
574n/aclass BadHELOServerTests(unittest.TestCase):
576n/a def setUp(self):
577n/a smtplib.socket = mock_socket
578n/a mock_socket.reply_with(b"199 no hello for you!")
579n/a self.old_stdout = sys.stdout
580n/a self.output = io.StringIO()
581n/a sys.stdout = self.output
582n/a self.port = 25
584n/a def tearDown(self):
585n/a smtplib.socket = socket
586n/a sys.stdout = self.old_stdout
588n/a def testFailingHELO(self):
589n/a self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
590n/a HOST, self.port, 'localhost', 3)
593n/a@unittest.skipUnless(threading, 'Threading required for this test.')
594n/aclass TooLongLineTests(unittest.TestCase):
595n/a respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n'
597n/a def setUp(self):
598n/a self.old_stdout = sys.stdout
599n/a self.output = io.StringIO()
600n/a sys.stdout = self.output
602n/a self.evt = threading.Event()
603n/a self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
604n/a self.sock.settimeout(15)
605n/a self.port = support.bind_port(self.sock)
606n/a servargs = (self.evt, self.respdata, self.sock)
607n/a threading.Thread(target=server, args=servargs).start()
608n/a self.evt.wait()
609n/a self.evt.clear()
611n/a def tearDown(self):
612n/a self.evt.wait()
613n/a sys.stdout = self.old_stdout
615n/a def testLineTooLong(self):
616n/a self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
617n/a HOST, self.port, 'localhost', 3)
620n/asim_users = {'Mr.A@somewhere.com':'John A',
621n/a 'Ms.B@xn--fo-fka.com':'Sally B',
622n/a 'Mrs.C@somewhereesle.com':'Ruth C',
623n/a }
625n/asim_auth = ('Mr.A@somewhere.com', 'somepassword')
626n/asim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
627n/a 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
628n/asim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
629n/a 'list-2':['Ms.B@xn--fo-fka.com',],
630n/a }
632n/a# Simulated SMTP channel & server
633n/aclass ResponseException(Exception): pass
634n/aclass SimSMTPChannel(smtpd.SMTPChannel):
636n/a quit_response = None
637n/a mail_response = None
638n/a rcpt_response = None
639n/a data_response = None
640n/a rcpt_count = 0
641n/a rset_count = 0
642n/a disconnect = 0
643n/a AUTH = 99 # Add protocol state to enable auth testing.
644n/a authenticated_user = None
646n/a def __init__(self, extra_features, *args, **kw):
647n/a self._extrafeatures = ''.join(
648n/a [ "250-{0}\r\n".format(x) for x in extra_features ])
649n/a super(SimSMTPChannel, self).__init__(*args, **kw)
651n/a # AUTH related stuff. It would be nice if support for this were in smtpd.
652n/a def found_terminator(self):
653n/a if self.smtp_state == self.AUTH:
654n/a line = self._emptystring.join(self.received_lines)
655n/a print('Data:', repr(line), file=smtpd.DEBUGSTREAM)
656n/a self.received_lines = []
657n/a try:
658n/a self.auth_object(line)
659n/a except ResponseException as e:
660n/a self.smtp_state = self.COMMAND
661n/a self.push('%s %s' % (e.smtp_code, e.smtp_error))
662n/a return
663n/a super().found_terminator()
666n/a def smtp_AUTH(self, arg):
667n/a if not self.seen_greeting:
668n/a self.push('503 Error: send EHLO first')
669n/a return
670n/a if not self.extended_smtp or 'AUTH' not in self._extrafeatures:
671n/a self.push('500 Error: command "AUTH" not recognized')
672n/a return
673n/a if self.authenticated_user is not None:
674n/a self.push(
675n/a '503 Bad sequence of commands: already authenticated')
676n/a return
677n/a args = arg.split()
678n/a if len(args) not in [1, 2]:
679n/a self.push('501 Syntax: AUTH <mechanism> [initial-response]')
680n/a return
681n/a auth_object_name = '_auth_%s' % args[0].lower().replace('-', '_')
682n/a try:
683n/a self.auth_object = getattr(self, auth_object_name)
684n/a except AttributeError:
685n/a self.push('504 Command parameter not implemented: unsupported '
686n/a ' authentication mechanism {!r}'.format(auth_object_name))
687n/a return
688n/a self.smtp_state = self.AUTH
689n/a self.auth_object(args[1] if len(args) == 2 else None)
691n/a def _authenticated(self, user, valid):
692n/a if valid:
693n/a self.authenticated_user = user
694n/a self.push('235 Authentication Succeeded')
695n/a else:
696n/a self.push('535 Authentication credentials invalid')
697n/a self.smtp_state = self.COMMAND
699n/a def _decode_base64(self, string):
700n/a return base64.decodebytes(string.encode('ascii')).decode('utf-8')
702n/a def _auth_plain(self, arg=None):
703n/a if arg is None:
704n/a self.push('334 ')
705n/a else:
706n/a logpass = self._decode_base64(arg)
707n/a try:
708n/a *_, user, password = logpass.split('\0')
709n/a except ValueError as e:
710n/a self.push('535 Splitting response {!r} into user and password'
711n/a ' failed: {}'.format(logpass, e))
712n/a return
713n/a self._authenticated(user, password == sim_auth[1])
715n/a def _auth_login(self, arg=None):
716n/a if arg is None:
717n/a # base64 encoded 'Username:'
718n/a self.push('334 VXNlcm5hbWU6')
719n/a elif not hasattr(self, '_auth_login_user'):
720n/a self._auth_login_user = self._decode_base64(arg)
721n/a # base64 encoded 'Password:'
722n/a self.push('334 UGFzc3dvcmQ6')
723n/a else:
724n/a password = self._decode_base64(arg)
725n/a self._authenticated(self._auth_login_user, password == sim_auth[1])
726n/a del self._auth_login_user
728n/a def _auth_cram_md5(self, arg=None):
729n/a if arg is None:
730n/a self.push('334 {}'.format(sim_cram_md5_challenge))
731n/a else:
732n/a logpass = self._decode_base64(arg)
733n/a try:
734n/a user, hashed_pass = logpass.split()
735n/a except ValueError as e:
736n/a self.push('535 Splitting response {!r} into user and password'
737n/a 'failed: {}'.format(logpass, e))
738n/a return False
739n/a valid_hashed_pass = hmac.HMAC(
740n/a sim_auth[1].encode('ascii'),
741n/a self._decode_base64(sim_cram_md5_challenge).encode('ascii'),
742n/a 'md5').hexdigest()
743n/a self._authenticated(user, hashed_pass == valid_hashed_pass)
744n/a # end AUTH related stuff.
746n/a def smtp_EHLO(self, arg):
747n/a resp = ('250-testhost\r\n'
748n/a '250-EXPN\r\n'
749n/a '250-SIZE 20000000\r\n'
750n/a '250-STARTTLS\r\n'
751n/a '250-DELIVERBY\r\n')
752n/a resp = resp + self._extrafeatures + '250 HELP'
753n/a self.push(resp)
754n/a self.seen_greeting = arg
755n/a self.extended_smtp = True
757n/a def smtp_VRFY(self, arg):
758n/a # For max compatibility smtplib should be sending the raw address.
759n/a if arg in sim_users:
760n/a self.push('250 %s %s' % (sim_users[arg], smtplib.quoteaddr(arg)))
761n/a else:
762n/a self.push('550 No such user: %s' % arg)
764n/a def smtp_EXPN(self, arg):
765n/a list_name = arg.lower()
766n/a if list_name in sim_lists:
767n/a user_list = sim_lists[list_name]
768n/a for n, user_email in enumerate(user_list):
769n/a quoted_addr = smtplib.quoteaddr(user_email)
770n/a if n < len(user_list) - 1:
771n/a self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
772n/a else:
773n/a self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
774n/a else:
775n/a self.push('550 No access for you!')
777n/a def smtp_QUIT(self, arg):
778n/a if self.quit_response is None:
779n/a super(SimSMTPChannel, self).smtp_QUIT(arg)
780n/a else:
781n/a self.push(self.quit_response)
782n/a self.close_when_done()
784n/a def smtp_MAIL(self, arg):
785n/a if self.mail_response is None:
786n/a super().smtp_MAIL(arg)
787n/a else:
788n/a self.push(self.mail_response)
789n/a if self.disconnect:
790n/a self.close_when_done()
792n/a def smtp_RCPT(self, arg):
793n/a if self.rcpt_response is None:
794n/a super().smtp_RCPT(arg)
795n/a return
796n/a self.rcpt_count += 1
797n/a self.push(self.rcpt_response[self.rcpt_count-1])
799n/a def smtp_RSET(self, arg):
800n/a self.rset_count += 1
801n/a super().smtp_RSET(arg)
803n/a def smtp_DATA(self, arg):
804n/a if self.data_response is None:
805n/a super().smtp_DATA(arg)
806n/a else:
807n/a self.push(self.data_response)
809n/a def handle_error(self):
810n/a raise
813n/aclass SimSMTPServer(smtpd.SMTPServer):
815n/a channel_class = SimSMTPChannel
817n/a def __init__(self, *args, **kw):
818n/a self._extra_features = []
819n/a smtpd.SMTPServer.__init__(self, *args, **kw)
821n/a def handle_accepted(self, conn, addr):
822n/a self._SMTPchannel = self.channel_class(
823n/a self._extra_features, self, conn, addr,
824n/a decode_data=self._decode_data)
826n/a def process_message(self, peer, mailfrom, rcpttos, data):
827n/a pass
829n/a def add_feature(self, feature):
830n/a self._extra_features.append(feature)
832n/a def handle_error(self):
833n/a raise
836n/a# Test various SMTP & ESMTP commands/behaviors that require a simulated server
837n/a# (i.e., something with more features than DebuggingServer)
838n/a@unittest.skipUnless(threading, 'Threading required for this test.')
839n/aclass SMTPSimTests(unittest.TestCase):
841n/a def setUp(self):
842n/a self.real_getfqdn = socket.getfqdn
843n/a socket.getfqdn = mock_socket.getfqdn
844n/a self.serv_evt = threading.Event()
845n/a self.client_evt = threading.Event()
846n/a # Pick a random unused port by passing 0 for the port number
847n/a self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1), decode_data=True)
848n/a # Keep a note of what port was assigned
849n/a self.port = self.serv.socket.getsockname()[1]
850n/a serv_args = (self.serv, self.serv_evt, self.client_evt)
851n/a self.thread = threading.Thread(target=debugging_server, args=serv_args)
852n/a self.thread.start()
854n/a # wait until server thread has assigned a port number
855n/a self.serv_evt.wait()
856n/a self.serv_evt.clear()
858n/a def tearDown(self):
859n/a socket.getfqdn = self.real_getfqdn
860n/a # indicate that the client is finished
861n/a self.client_evt.set()
862n/a # wait for the server thread to terminate
863n/a self.serv_evt.wait()
864n/a self.thread.join()
866n/a def testBasic(self):
867n/a # smoke test
868n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
869n/a smtp.quit()
871n/a def testEHLO(self):
872n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
874n/a # no features should be present before the EHLO
875n/a self.assertEqual(smtp.esmtp_features, {})
877n/a # features expected from the test server
878n/a expected_features = {'expn':'',
879n/a 'size': '20000000',
880n/a 'starttls': '',
881n/a 'deliverby': '',
882n/a 'help': '',
883n/a }
885n/a smtp.ehlo()
886n/a self.assertEqual(smtp.esmtp_features, expected_features)
887n/a for k in expected_features:
888n/a self.assertTrue(smtp.has_extn(k))
889n/a self.assertFalse(smtp.has_extn('unsupported-feature'))
890n/a smtp.quit()
892n/a def testVRFY(self):
893n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
895n/a for addr_spec, name in sim_users.items():
896n/a expected_known = (250, bytes('%s %s' %
897n/a (name, smtplib.quoteaddr(addr_spec)),
898n/a "ascii"))
899n/a self.assertEqual(smtp.vrfy(addr_spec), expected_known)
901n/a u = 'nobody@nowhere.com'
902n/a expected_unknown = (550, ('No such user: %s' % u).encode('ascii'))
903n/a self.assertEqual(smtp.vrfy(u), expected_unknown)
904n/a smtp.quit()
906n/a def testEXPN(self):
907n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
909n/a for listname, members in sim_lists.items():
910n/a users = []
911n/a for m in members:
912n/a users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
913n/a expected_known = (250, bytes('\n'.join(users), "ascii"))
914n/a self.assertEqual(smtp.expn(listname), expected_known)
916n/a u = 'PSU-Members-List'
917n/a expected_unknown = (550, b'No access for you!')
918n/a self.assertEqual(smtp.expn(u), expected_unknown)
919n/a smtp.quit()
921n/a def testAUTH_PLAIN(self):
922n/a self.serv.add_feature("AUTH PLAIN")
923n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
924n/a resp = smtp.login(sim_auth[0], sim_auth[1])
925n/a self.assertEqual(resp, (235, b'Authentication Succeeded'))
926n/a smtp.close()
928n/a def testAUTH_LOGIN(self):
929n/a self.serv.add_feature("AUTH LOGIN")
930n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
931n/a resp = smtp.login(sim_auth[0], sim_auth[1])
932n/a self.assertEqual(resp, (235, b'Authentication Succeeded'))
933n/a smtp.close()
935n/a def testAUTH_CRAM_MD5(self):
936n/a self.serv.add_feature("AUTH CRAM-MD5")
937n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
938n/a resp = smtp.login(sim_auth[0], sim_auth[1])
939n/a self.assertEqual(resp, (235, b'Authentication Succeeded'))
940n/a smtp.close()
942n/a def testAUTH_multiple(self):
943n/a # Test that multiple authentication methods are tried.
944n/a self.serv.add_feature("AUTH BOGUS PLAIN LOGIN CRAM-MD5")
945n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
946n/a resp = smtp.login(sim_auth[0], sim_auth[1])
947n/a self.assertEqual(resp, (235, b'Authentication Succeeded'))
948n/a smtp.close()
950n/a def test_auth_function(self):
951n/a supported = {'CRAM-MD5', 'PLAIN', 'LOGIN'}
952n/a for mechanism in supported:
953n/a self.serv.add_feature("AUTH {}".format(mechanism))
954n/a for mechanism in supported:
955n/a with self.subTest(mechanism=mechanism):
956n/a smtp = smtplib.SMTP(HOST, self.port,
957n/a local_hostname='localhost', timeout=15)
958n/a smtp.ehlo('foo')
959n/a smtp.user, smtp.password = sim_auth[0], sim_auth[1]
960n/a method = 'auth_' + mechanism.lower().replace('-', '_')
961n/a resp = smtp.auth(mechanism, getattr(smtp, method))
962n/a self.assertEqual(resp, (235, b'Authentication Succeeded'))
963n/a smtp.close()
965n/a def test_quit_resets_greeting(self):
966n/a smtp = smtplib.SMTP(HOST, self.port,
967n/a local_hostname='localhost',
968n/a timeout=15)
969n/a code, message = smtp.ehlo()
970n/a self.assertEqual(code, 250)
971n/a self.assertIn('size', smtp.esmtp_features)
972n/a smtp.quit()
973n/a self.assertNotIn('size', smtp.esmtp_features)
974n/a smtp.connect(HOST, self.port)
975n/a self.assertNotIn('size', smtp.esmtp_features)
976n/a smtp.ehlo_or_helo_if_needed()
977n/a self.assertIn('size', smtp.esmtp_features)
978n/a smtp.quit()
980n/a def test_with_statement(self):
981n/a with smtplib.SMTP(HOST, self.port) as smtp:
982n/a code, message = smtp.noop()
983n/a self.assertEqual(code, 250)
984n/a self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo')
985n/a with smtplib.SMTP(HOST, self.port) as smtp:
986n/a smtp.close()
987n/a self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo')
989n/a def test_with_statement_QUIT_failure(self):
990n/a with self.assertRaises(smtplib.SMTPResponseException) as error:
991n/a with smtplib.SMTP(HOST, self.port) as smtp:
992n/a smtp.noop()
993n/a self.serv._SMTPchannel.quit_response = '421 QUIT FAILED'
994n/a self.assertEqual(error.exception.smtp_code, 421)
995n/a self.assertEqual(error.exception.smtp_error, b'QUIT FAILED')
997n/a #TODO: add tests for correct AUTH method fallback now that the
998n/a #test infrastructure can support it.
1000n/a # Issue 17498: make sure _rset does not raise SMTPServerDisconnected exception
1001n/a def test__rest_from_mail_cmd(self):
1002n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
1003n/a smtp.noop()
1004n/a self.serv._SMTPchannel.mail_response = '451 Requested action aborted'
1005n/a self.serv._SMTPchannel.disconnect = True
1006n/a with self.assertRaises(smtplib.SMTPSenderRefused):
1007n/a smtp.sendmail('John', 'Sally', 'test message')
1008n/a self.assertIsNone(smtp.sock)
1010n/a # Issue 5713: make sure close, not rset, is called if we get a 421 error
1011n/a def test_421_from_mail_cmd(self):
1012n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
1013n/a smtp.noop()
1014n/a self.serv._SMTPchannel.mail_response = '421 closing connection'
1015n/a with self.assertRaises(smtplib.SMTPSenderRefused):
1016n/a smtp.sendmail('John', 'Sally', 'test message')
1017n/a self.assertIsNone(smtp.sock)
1018n/a self.assertEqual(self.serv._SMTPchannel.rset_count, 0)
1020n/a def test_421_from_rcpt_cmd(self):
1021n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
1022n/a smtp.noop()
1023n/a self.serv._SMTPchannel.rcpt_response = ['250 accepted', '421 closing']
1024n/a with self.assertRaises(smtplib.SMTPRecipientsRefused) as r:
1025n/a smtp.sendmail('John', ['Sally', 'Frank', 'George'], 'test message')
1026n/a self.assertIsNone(smtp.sock)
1027n/a self.assertEqual(self.serv._SMTPchannel.rset_count, 0)
1028n/a self.assertDictEqual(r.exception.args[0], {'Frank': (421, b'closing')})
1030n/a def test_421_from_data_cmd(self):
1031n/a class MySimSMTPChannel(SimSMTPChannel):
1032n/a def found_terminator(self):
1033n/a if self.smtp_state == self.DATA:
1034n/a self.push('421 closing')
1035n/a else:
1036n/a super().found_terminator()
1037n/a self.serv.channel_class = MySimSMTPChannel
1038n/a smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
1039n/a smtp.noop()
1040n/a with self.assertRaises(smtplib.SMTPDataError):
1041n/a smtp.sendmail('John@foo.org', ['Sally@foo.org'], 'test message')
1042n/a self.assertIsNone(smtp.sock)
1043n/a self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0)
1045n/a def test_smtputf8_NotSupportedError_if_no_server_support(self):
1046n/a smtp = smtplib.SMTP(
1047n/a HOST, self.port, local_hostname='localhost', timeout=3)
1048n/a self.addCleanup(smtp.close)
1049n/a smtp.ehlo()
1050n/a self.assertTrue(smtp.does_esmtp)
1051n/a self.assertFalse(smtp.has_extn('smtputf8'))
1052n/a self.assertRaises(
1053n/a smtplib.SMTPNotSupportedError,
1054n/a smtp.sendmail,
1055n/a 'John', 'Sally', '', mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
1056n/a self.assertRaises(
1057n/a smtplib.SMTPNotSupportedError,
1058n/a smtp.mail, 'John', options=['BODY=8BITMIME', 'SMTPUTF8'])
1060n/a def test_send_unicode_without_SMTPUTF8(self):
1061n/a smtp = smtplib.SMTP(
1062n/a HOST, self.port, local_hostname='localhost', timeout=3)
1063n/a self.addCleanup(smtp.close)
1064n/a self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '')
1065n/a self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice')
1068n/aclass SimSMTPUTF8Server(SimSMTPServer):
1070n/a def __init__(self, *args, **kw):
1071n/a # The base SMTP server turns these on automatically, but our test
1072n/a # server is set up to munge the EHLO response, so we need to provide
1073n/a # them as well. And yes, the call is to SMTPServer not SimSMTPServer.
1074n/a self._extra_features = ['SMTPUTF8', '8BITMIME']
1075n/a smtpd.SMTPServer.__init__(self, *args, **kw)
1077n/a def handle_accepted(self, conn, addr):
1078n/a self._SMTPchannel = self.channel_class(
1079n/a self._extra_features, self, conn, addr,
1080n/a decode_data=self._decode_data,
1081n/a enable_SMTPUTF8=self.enable_SMTPUTF8,
1082n/a )
1084n/a def process_message(self, peer, mailfrom, rcpttos, data, mail_options=None,
1085n/a rcpt_options=None):
1086n/a self.last_peer = peer
1087n/a self.last_mailfrom = mailfrom
1088n/a self.last_rcpttos = rcpttos
1089n/a self.last_message = data
1090n/a self.last_mail_options = mail_options
1091n/a self.last_rcpt_options = rcpt_options
1094n/a@unittest.skipUnless(threading, 'Threading required for this test.')
1095n/aclass SMTPUTF8SimTests(unittest.TestCase):
1097n/a maxDiff = None
1099n/a def setUp(self):
1100n/a self.real_getfqdn = socket.getfqdn
1101n/a socket.getfqdn = mock_socket.getfqdn
1102n/a self.serv_evt = threading.Event()
1103n/a self.client_evt = threading.Event()
1104n/a # Pick a random unused port by passing 0 for the port number
1105n/a self.serv = SimSMTPUTF8Server((HOST, 0), ('nowhere', -1),
1106n/a decode_data=False,
1107n/a enable_SMTPUTF8=True)
1108n/a # Keep a note of what port was assigned
1109n/a self.port = self.serv.socket.getsockname()[1]
1110n/a serv_args = (self.serv, self.serv_evt, self.client_evt)
1111n/a self.thread = threading.Thread(target=debugging_server, args=serv_args)
1112n/a self.thread.start()
1114n/a # wait until server thread has assigned a port number
1115n/a self.serv_evt.wait()
1116n/a self.serv_evt.clear()
1118n/a def tearDown(self):
1119n/a socket.getfqdn = self.real_getfqdn
1120n/a # indicate that the client is finished
1121n/a self.client_evt.set()
1122n/a # wait for the server thread to terminate
1123n/a self.serv_evt.wait()
1124n/a self.thread.join()
1126n/a def test_test_server_supports_extensions(self):
1127n/a smtp = smtplib.SMTP(
1128n/a HOST, self.port, local_hostname='localhost', timeout=3)
1129n/a self.addCleanup(smtp.close)
1130n/a smtp.ehlo()
1131n/a self.assertTrue(smtp.does_esmtp)
1132n/a self.assertTrue(smtp.has_extn('smtputf8'))
1134n/a def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
1135n/a m = '¡a test message containing unicode!'.encode('utf-8')
1136n/a smtp = smtplib.SMTP(
1137n/a HOST, self.port, local_hostname='localhost', timeout=3)
1138n/a self.addCleanup(smtp.close)
1139n/a smtp.sendmail('Jőhn', 'Sálly', m,
1140n/a mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
1141n/a self.assertEqual(self.serv.last_mailfrom, 'JÅ‘hn')
1142n/a self.assertEqual(self.serv.last_rcpttos, ['Sálly'])
1143n/a self.assertEqual(self.serv.last_message, m)
1144n/a self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1145n/a self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1146n/a self.assertEqual(self.serv.last_rcpt_options, [])
1148n/a def test_send_unicode_with_SMTPUTF8_via_low_level_API(self):
1149n/a m = '¡a test message containing unicode!'.encode('utf-8')
1150n/a smtp = smtplib.SMTP(
1151n/a HOST, self.port, local_hostname='localhost', timeout=3)
1152n/a self.addCleanup(smtp.close)
1153n/a smtp.ehlo()
1154n/a self.assertEqual(
1155n/a smtp.mail('JÅ‘', options=['BODY=8BITMIME', 'SMTPUTF8']),
1156n/a (250, b'OK'))
1157n/a self.assertEqual(smtp.rcpt('János'), (250, b'OK'))
1158n/a self.assertEqual(smtp.data(m), (250, b'OK'))
1159n/a self.assertEqual(self.serv.last_mailfrom, 'JÅ‘')
1160n/a self.assertEqual(self.serv.last_rcpttos, ['János'])
1161n/a self.assertEqual(self.serv.last_message, m)
1162n/a self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1163n/a self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1164n/a self.assertEqual(self.serv.last_rcpt_options, [])
1166n/a def test_send_message_uses_smtputf8_if_addrs_non_ascii(self):
1167n/a msg = EmailMessage()
1168n/a msg['From'] = "Páolo <főo@bar.com>"
1169n/a msg['To'] = 'Dinsdale'
1170n/a msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
1171n/a # XXX I don't know why I need two \n's here, but this is an existing
1172n/a # bug (if it is one) and not a problem with the new functionality.
1173n/a msg.set_content("oh là là, know what I mean, know what I mean?\n\n")
1174n/a # XXX smtpd converts received /r/n to /n, so we can't easily test that
1175n/a # we are successfully sending /r/n :(.
1176n/a expected = textwrap.dedent("""\
1177n/a From: Páolo <főo@bar.com>
1178n/a To: Dinsdale
1179n/a Subject: Nudge nudge, wink, wink \u1F609
1180n/a Content-Type: text/plain; charset="utf-8"
1181n/a Content-Transfer-Encoding: 8bit
1182n/a MIME-Version: 1.0
1184n/a oh là là, know what I mean, know what I mean?
1185n/a """)
1186n/a smtp = smtplib.SMTP(
1187n/a HOST, self.port, local_hostname='localhost', timeout=3)
1188n/a self.addCleanup(smtp.close)
1189n/a self.assertEqual(smtp.send_message(msg), {})
1190n/a self.assertEqual(self.serv.last_mailfrom, 'főo@bar.com')
1191n/a self.assertEqual(self.serv.last_rcpttos, ['Dinsdale'])
1192n/a self.assertEqual(self.serv.last_message.decode(), expected)
1193n/a self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1194n/a self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1195n/a self.assertEqual(self.serv.last_rcpt_options, [])
1197n/a def test_send_message_error_on_non_ascii_addrs_if_no_smtputf8(self):
1198n/a msg = EmailMessage()
1199n/a msg['From'] = "Páolo <főo@bar.com>"
1200n/a msg['To'] = 'Dinsdale'
1201n/a msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
1202n/a smtp = smtplib.SMTP(
1203n/a HOST, self.port, local_hostname='localhost', timeout=3)
1204n/a self.addCleanup(smtp.close)
1205n/a self.assertRaises(smtplib.SMTPNotSupportedError,
1206n/a smtp.send_message(msg))
1209n/aEXPECTED_RESPONSE = encode_base64(b'\0psu\0doesnotexist', eol='')
1211n/aclass SimSMTPAUTHInitialResponseChannel(SimSMTPChannel):
1212n/a def smtp_AUTH(self, arg):
1213n/a # RFC 4954's AUTH command allows for an optional initial-response.
1214n/a # Not all AUTH methods support this; some require a challenge. AUTH
1215n/a # PLAIN does those, so test that here. See issue #15014.
1216n/a args = arg.split()
1217n/a if args[0].lower() == 'plain':
1218n/a if len(args) == 2:
1219n/a # AUTH PLAIN <initial-response> with the response base 64
1220n/a # encoded. Hard code the expected response for the test.
1221n/a if args[1] == EXPECTED_RESPONSE:
1222n/a self.push('235 Ok')
1223n/a return
1224n/a self.push('571 Bad authentication')
1226n/aclass SimSMTPAUTHInitialResponseServer(SimSMTPServer):
1227n/a channel_class = SimSMTPAUTHInitialResponseChannel
1230n/a@unittest.skipUnless(threading, 'Threading required for this test.')
1231n/aclass SMTPAUTHInitialResponseSimTests(unittest.TestCase):
1232n/a def setUp(self):
1233n/a self.real_getfqdn = socket.getfqdn
1234n/a socket.getfqdn = mock_socket.getfqdn
1235n/a self.serv_evt = threading.Event()
1236n/a self.client_evt = threading.Event()
1237n/a # Pick a random unused port by passing 0 for the port number
1238n/a self.serv = SimSMTPAUTHInitialResponseServer(
1239n/a (HOST, 0), ('nowhere', -1), decode_data=True)
1240n/a # Keep a note of what port was assigned
1241n/a self.port = self.serv.socket.getsockname()[1]
1242n/a serv_args = (self.serv, self.serv_evt, self.client_evt)
1243n/a self.thread = threading.Thread(target=debugging_server, args=serv_args)
1244n/a self.thread.start()
1246n/a # wait until server thread has assigned a port number
1247n/a self.serv_evt.wait()
1248n/a self.serv_evt.clear()
1250n/a def tearDown(self):
1251n/a socket.getfqdn = self.real_getfqdn
1252n/a # indicate that the client is finished
1253n/a self.client_evt.set()
1254n/a # wait for the server thread to terminate
1255n/a self.serv_evt.wait()
1256n/a self.thread.join()
1258n/a def testAUTH_PLAIN_initial_response_login(self):
1259n/a self.serv.add_feature('AUTH PLAIN')
1260n/a smtp = smtplib.SMTP(HOST, self.port,
1261n/a local_hostname='localhost', timeout=15)
1262n/a smtp.login('psu', 'doesnotexist')
1263n/a smtp.close()
1265n/a def testAUTH_PLAIN_initial_response_auth(self):
1266n/a self.serv.add_feature('AUTH PLAIN')
1267n/a smtp = smtplib.SMTP(HOST, self.port,
1268n/a local_hostname='localhost', timeout=15)
1269n/a smtp.user = 'psu'
1270n/a smtp.password = 'doesnotexist'
1271n/a code, response = smtp.auth('plain', smtp.auth_plain)
1272n/a smtp.close()
1273n/a self.assertEqual(code, 235)
1277n/adef test_main(verbose=None):
1278n/a support.run_unittest(
1279n/a BadHELOServerTests,
1280n/a DebuggingServerTests,
1281n/a GeneralTests,
1282n/a NonConnectingTests,
1283n/a SMTPAUTHInitialResponseSimTests,
1284n/a SMTPSimTests,
1285n/a TooLongLineTests,
1286n/a )
1289n/aif __name__ == '__main__':
1290n/a test_main()