Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added beginnings of black box tests

  • Loading branch information...
commit fb2af3a5bc5f1352404bd378b90a998384c24851 1 parent 47e0534
Euan Goddard authored
5 .gitignore
... ... @@ -1 +1,4 @@
1   -.project
  1 +.project
  2 +.pydevproject
  3 +.settings/
  4 +*.pyc
16 tests/black-box/README
... ... @@ -0,0 +1,16 @@
  1 +The tests in this section are "black box" tests or, put another way, tests
  2 +which test the system as a whole without concerning the code behind the module.
  3 +
  4 +To run the tests, you will need to have node installed (obviously!) and have a
  5 +version of python as the SMTP client uses the built in one in python's standard
  6 +library. The tests have all been verified with python 2.5.
  7 +
  8 +The tests require the testing server to be running. In the current directory:
  9 +
  10 +$ node test-server.js
  11 +
  12 +Then you can run the tests that you want:
  13 +
  14 +$ python <name of test>.py.
  15 +
  16 +For unit tests, see the "unit tests" section.
116 tests/black-box/sequential.py
... ... @@ -0,0 +1,116 @@
  1 +#!/usr/bin/python
  2 +# -*- coding: utf-8 -*-
  3 +
  4 +from smtplib import SMTP
  5 +from telnetlib import Telnet
  6 +import unittest
  7 +
  8 +class SequentialTests(unittest.TestCase):
  9 +
  10 + addrs = [
  11 + 'bob@example.com', 'sheila@example.com', 'kurt@example.com',
  12 + 'wendy@example.com', 'tim@example.com'
  13 + ]
  14 +
  15 + def testFiveSequentialMessages(self):
  16 + """5 sequential messages can be sent in the same connection."""
  17 +
  18 + server = SMTP('localhost', 1025)
  19 +
  20 + for from_addr, to_addr in zip(self.addrs, reversed(self.addrs)):
  21 +
  22 + response = server.sendmail(
  23 + from_addr, [to_addr],
  24 + 'This is a test message\nSecond line.\nFinal line here.'
  25 + )
  26 +
  27 + self.assertEqual(response, {})
  28 +
  29 + server.quit()
  30 +
  31 + def testFiveSequentialConnections(self):
  32 + """5 sequential connections can be used to send messages."""
  33 +
  34 + for from_addr, to_addr in zip(self.addrs, reversed(self.addrs)):
  35 + server = SMTP('localhost', 1025)
  36 +
  37 + response = server.sendmail(
  38 + from_addr, [to_addr],
  39 + 'This is a test message\nSecond line.\nFinal line here.'
  40 + )
  41 +
  42 + self.assertEqual(response, {})
  43 +
  44 + server.quit()
  45 +
  46 + def testUnicode(self):
  47 + """Unicode characters are correctly received"""
  48 +
  49 + server = SMTP('localhost', 1025)
  50 +
  51 + response = server.sendmail(
  52 + 'ryu@example.jp', 'akira@example.jp',
  53 + u'こんにちは彰、どのようにして、今日ですか?リュ'.encode('utf8')
  54 + )
  55 +
  56 + self.assertEqual(response, {})
  57 +
  58 + def testWelcomeMessage(self):
  59 + """On connecting the server sends a 220 response with a welcome message."""
  60 + client = Telnet('localhost', 1025)
  61 + self.assertEqual(client.read_some(), '220 test node.js SMTP server\r\n')
  62 + client.close()
  63 +
  64 + def testIllegalHelo(self):
  65 + """HELO takes a single argument."""
  66 +
  67 + client = Telnet('localhost', 1025)
  68 + client.read_some()
  69 + client.write('HELO')
  70 + self.assertEqual(client.read_some(), '501 Syntax: HELO hostname\r\n')
  71 + client.close()
  72 +
  73 + def testLegalHelo(self):
  74 + """The server responds to a valid HELO command."""
  75 +
  76 + client = Telnet('localhost', 1025)
  77 + client.read_some()
  78 + client.write('HELO localhost')
  79 + self.assertEqual(client.read_some(), '250 test Hello 127.0.0.1\r\n')
  80 + client.close()
  81 +
  82 + def testIllegalNoop(self):
  83 + """The NOOP command fails if any argument is passed."""
  84 +
  85 + client = Telnet('localhost', 1025)
  86 + client.read_some()
  87 + client.write('NOOP something else here')
  88 + self.assertEqual(client.read_some(), '501 Syntax: NOOP\r\n')
  89 + client.close()
  90 +
  91 + def testLegalNoop(self):
  92 + """The NOOP command takes no arguments."""
  93 +
  94 + client = Telnet('localhost', 1025)
  95 + client.read_some()
  96 + client.write('NOOP')
  97 + self.assertEqual(client.read_some(), '250 Ok\r\n')
  98 + client.close()
  99 +
  100 + def testQuit(self):
  101 + """The QUIT command doesn't care about arguments - the connection is
  102 + closed regardless."""
  103 +
  104 + client = Telnet('localhost', 1025)
  105 + client.read_some()
  106 + client.write('QUIT')
  107 + self.assertEqual(client.read_some(), '221 test closing connection\r\n')
  108 +
  109 + client = Telnet('localhost', 1025)
  110 + client.read_some()
  111 + client.write('QUIT See you later')
  112 + self.assertEqual(client.read_some(), '221 test closing connection\r\n')
  113 +
  114 +
  115 +if __name__ == "__main__":
  116 + unittest.main()
17 tests/black-box/test-server.js
... ... @@ -0,0 +1,17 @@
  1 +/**
  2 + * Test server for black box tests.
  3 + * @author Euan Goddard
  4 + */
  5 +
  6 +var smtpevent = require('../../smtpevent.js'),
  7 +
  8 + server = new smtpevent.SMTPServer('test');
  9 +
  10 +server.listen(1025, "127.0.0.1");
  11 +server.on('incoming-mail', function (peer, from, to, message) {
  12 + console.log('Received test message from: '+ peer);
  13 + console.log('Message from: '+ from + ' to: '+ to);
  14 + console.log('---------------------');
  15 + console.log(message);
  16 + console.log('---------------------');
  17 +});

0 comments on commit fb2af3a

Please sign in to comment.
Something went wrong with that request. Please try again.