-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_netcat.py
34 lines (25 loc) · 1.05 KB
/
test_netcat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from StringIO import StringIO
import mock
from twisted.test import proto_helpers
from twisted.trial import unittest
from netcat import ReceiveFactory, Receiver
class ReceiverTest(unittest.TestCase):
def setUp(self):
self.buff = StringIO()
self.msg = StringIO()
def dataWriter(data):
self.buff.write(data)
def transferFinished(msg):
self.msg.write(msg)
self.factory = ReceiveFactory(dataWriter, transferFinished)
self.proto = self.factory.buildProtocol(('localhost', 0,))
self.transport = proto_helpers.StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
def test_successful_transfer(self):
self.proto.dataReceived('SomeData\r\n')
self.assertEqual(self.buff.getvalue(), 'SomeData\r\n')
def test_connectionLost(self):
self.proto.dataReceived('SomeData\r\n')
self.transport.loseConnection()
self.assertTrue(self.msg.getvalue(), 'Transfer finished successfully!')