You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_loopback.py 14KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test case for L{twisted.protocols.loopback}.
  5. """
  6. from __future__ import division, absolute_import
  7. from zope.interface import implementer
  8. from twisted.python.compat import intToBytes
  9. from twisted.trial import unittest
  10. from twisted.protocols import basic, loopback
  11. from twisted.internet import defer
  12. from twisted.internet.protocol import Protocol
  13. from twisted.internet.defer import Deferred
  14. from twisted.internet.interfaces import IAddress, IPushProducer, IPullProducer
  15. from twisted.internet import reactor, interfaces
  16. class SimpleProtocol(basic.LineReceiver):
  17. def __init__(self):
  18. self.conn = defer.Deferred()
  19. self.lines = []
  20. self.connLost = []
  21. def connectionMade(self):
  22. self.conn.callback(None)
  23. def lineReceived(self, line):
  24. self.lines.append(line)
  25. def connectionLost(self, reason):
  26. self.connLost.append(reason)
  27. class DoomProtocol(SimpleProtocol):
  28. i = 0
  29. def lineReceived(self, line):
  30. self.i += 1
  31. if self.i < 4:
  32. # by this point we should have connection closed,
  33. # but just in case we didn't we won't ever send 'Hello 4'
  34. self.sendLine(b"Hello " + intToBytes(self.i))
  35. SimpleProtocol.lineReceived(self, line)
  36. if self.lines[-1] == b"Hello 3":
  37. self.transport.loseConnection()
  38. class LoopbackTestCaseMixin:
  39. def testRegularFunction(self):
  40. s = SimpleProtocol()
  41. c = SimpleProtocol()
  42. def sendALine(result):
  43. s.sendLine(b"THIS IS LINE ONE!")
  44. s.transport.loseConnection()
  45. s.conn.addCallback(sendALine)
  46. def check(ignored):
  47. self.assertEqual(c.lines, [b"THIS IS LINE ONE!"])
  48. self.assertEqual(len(s.connLost), 1)
  49. self.assertEqual(len(c.connLost), 1)
  50. d = defer.maybeDeferred(self.loopbackFunc, s, c)
  51. d.addCallback(check)
  52. return d
  53. def testSneakyHiddenDoom(self):
  54. s = DoomProtocol()
  55. c = DoomProtocol()
  56. def sendALine(result):
  57. s.sendLine(b"DOOM LINE")
  58. s.conn.addCallback(sendALine)
  59. def check(ignored):
  60. self.assertEqual(s.lines, [b'Hello 1', b'Hello 2', b'Hello 3'])
  61. self.assertEqual(
  62. c.lines, [b'DOOM LINE', b'Hello 1', b'Hello 2', b'Hello 3'])
  63. self.assertEqual(len(s.connLost), 1)
  64. self.assertEqual(len(c.connLost), 1)
  65. d = defer.maybeDeferred(self.loopbackFunc, s, c)
  66. d.addCallback(check)
  67. return d
  68. class LoopbackAsyncTests(LoopbackTestCaseMixin, unittest.TestCase):
  69. loopbackFunc = staticmethod(loopback.loopbackAsync)
  70. def test_makeConnection(self):
  71. """
  72. Test that the client and server protocol both have makeConnection
  73. invoked on them by loopbackAsync.
  74. """
  75. class TestProtocol(Protocol):
  76. transport = None
  77. def makeConnection(self, transport):
  78. self.transport = transport
  79. server = TestProtocol()
  80. client = TestProtocol()
  81. loopback.loopbackAsync(server, client)
  82. self.assertIsNotNone(client.transport)
  83. self.assertIsNotNone(server.transport)
  84. def _hostpeertest(self, get, testServer):
  85. """
  86. Test one of the permutations of client/server host/peer.
  87. """
  88. class TestProtocol(Protocol):
  89. def makeConnection(self, transport):
  90. Protocol.makeConnection(self, transport)
  91. self.onConnection.callback(transport)
  92. if testServer:
  93. server = TestProtocol()
  94. d = server.onConnection = Deferred()
  95. client = Protocol()
  96. else:
  97. server = Protocol()
  98. client = TestProtocol()
  99. d = client.onConnection = Deferred()
  100. loopback.loopbackAsync(server, client)
  101. def connected(transport):
  102. host = getattr(transport, get)()
  103. self.assertTrue(IAddress.providedBy(host))
  104. return d.addCallback(connected)
  105. def test_serverHost(self):
  106. """
  107. Test that the server gets a transport with a properly functioning
  108. implementation of L{ITransport.getHost}.
  109. """
  110. return self._hostpeertest("getHost", True)
  111. def test_serverPeer(self):
  112. """
  113. Like C{test_serverHost} but for L{ITransport.getPeer}
  114. """
  115. return self._hostpeertest("getPeer", True)
  116. def test_clientHost(self, get="getHost"):
  117. """
  118. Test that the client gets a transport with a properly functioning
  119. implementation of L{ITransport.getHost}.
  120. """
  121. return self._hostpeertest("getHost", False)
  122. def test_clientPeer(self):
  123. """
  124. Like C{test_clientHost} but for L{ITransport.getPeer}.
  125. """
  126. return self._hostpeertest("getPeer", False)
  127. def _greetingtest(self, write, testServer):
  128. """
  129. Test one of the permutations of write/writeSequence client/server.
  130. @param write: The name of the method to test, C{"write"} or
  131. C{"writeSequence"}.
  132. """
  133. class GreeteeProtocol(Protocol):
  134. bytes = b""
  135. def dataReceived(self, bytes):
  136. self.bytes += bytes
  137. if self.bytes == b"bytes":
  138. self.received.callback(None)
  139. class GreeterProtocol(Protocol):
  140. def connectionMade(self):
  141. if write == "write":
  142. self.transport.write(b"bytes")
  143. else:
  144. self.transport.writeSequence([b"byt", b"es"])
  145. if testServer:
  146. server = GreeterProtocol()
  147. client = GreeteeProtocol()
  148. d = client.received = Deferred()
  149. else:
  150. server = GreeteeProtocol()
  151. d = server.received = Deferred()
  152. client = GreeterProtocol()
  153. loopback.loopbackAsync(server, client)
  154. return d
  155. def test_clientGreeting(self):
  156. """
  157. Test that on a connection where the client speaks first, the server
  158. receives the bytes sent by the client.
  159. """
  160. return self._greetingtest("write", False)
  161. def test_clientGreetingSequence(self):
  162. """
  163. Like C{test_clientGreeting}, but use C{writeSequence} instead of
  164. C{write} to issue the greeting.
  165. """
  166. return self._greetingtest("writeSequence", False)
  167. def test_serverGreeting(self, write="write"):
  168. """
  169. Test that on a connection where the server speaks first, the client
  170. receives the bytes sent by the server.
  171. """
  172. return self._greetingtest("write", True)
  173. def test_serverGreetingSequence(self):
  174. """
  175. Like C{test_serverGreeting}, but use C{writeSequence} instead of
  176. C{write} to issue the greeting.
  177. """
  178. return self._greetingtest("writeSequence", True)
  179. def _producertest(self, producerClass):
  180. toProduce = list(map(intToBytes, range(0, 10)))
  181. class ProducingProtocol(Protocol):
  182. def connectionMade(self):
  183. self.producer = producerClass(list(toProduce))
  184. self.producer.start(self.transport)
  185. class ReceivingProtocol(Protocol):
  186. bytes = b""
  187. def dataReceived(self, data):
  188. self.bytes += data
  189. if self.bytes == b''.join(toProduce):
  190. self.received.callback((client, server))
  191. server = ProducingProtocol()
  192. client = ReceivingProtocol()
  193. client.received = Deferred()
  194. loopback.loopbackAsync(server, client)
  195. return client.received
  196. def test_pushProducer(self):
  197. """
  198. Test a push producer registered against a loopback transport.
  199. """
  200. @implementer(IPushProducer)
  201. class PushProducer(object):
  202. resumed = False
  203. def __init__(self, toProduce):
  204. self.toProduce = toProduce
  205. def resumeProducing(self):
  206. self.resumed = True
  207. def start(self, consumer):
  208. self.consumer = consumer
  209. consumer.registerProducer(self, True)
  210. self._produceAndSchedule()
  211. def _produceAndSchedule(self):
  212. if self.toProduce:
  213. self.consumer.write(self.toProduce.pop(0))
  214. reactor.callLater(0, self._produceAndSchedule)
  215. else:
  216. self.consumer.unregisterProducer()
  217. d = self._producertest(PushProducer)
  218. def finished(results):
  219. (client, server) = results
  220. self.assertFalse(
  221. server.producer.resumed,
  222. "Streaming producer should not have been resumed.")
  223. d.addCallback(finished)
  224. return d
  225. def test_pullProducer(self):
  226. """
  227. Test a pull producer registered against a loopback transport.
  228. """
  229. @implementer(IPullProducer)
  230. class PullProducer(object):
  231. def __init__(self, toProduce):
  232. self.toProduce = toProduce
  233. def start(self, consumer):
  234. self.consumer = consumer
  235. self.consumer.registerProducer(self, False)
  236. def resumeProducing(self):
  237. self.consumer.write(self.toProduce.pop(0))
  238. if not self.toProduce:
  239. self.consumer.unregisterProducer()
  240. return self._producertest(PullProducer)
  241. def test_writeNotReentrant(self):
  242. """
  243. L{loopback.loopbackAsync} does not call a protocol's C{dataReceived}
  244. method while that protocol's transport's C{write} method is higher up
  245. on the stack.
  246. """
  247. class Server(Protocol):
  248. def dataReceived(self, bytes):
  249. self.transport.write(b"bytes")
  250. class Client(Protocol):
  251. ready = False
  252. def connectionMade(self):
  253. reactor.callLater(0, self.go)
  254. def go(self):
  255. self.transport.write(b"foo")
  256. self.ready = True
  257. def dataReceived(self, bytes):
  258. self.wasReady = self.ready
  259. self.transport.loseConnection()
  260. server = Server()
  261. client = Client()
  262. d = loopback.loopbackAsync(client, server)
  263. def cbFinished(ignored):
  264. self.assertTrue(client.wasReady)
  265. d.addCallback(cbFinished)
  266. return d
  267. def test_pumpPolicy(self):
  268. """
  269. The callable passed as the value for the C{pumpPolicy} parameter to
  270. L{loopbackAsync} is called with a L{_LoopbackQueue} of pending bytes
  271. and a protocol to which they should be delivered.
  272. """
  273. pumpCalls = []
  274. def dummyPolicy(queue, target):
  275. bytes = []
  276. while queue:
  277. bytes.append(queue.get())
  278. pumpCalls.append((target, bytes))
  279. client = Protocol()
  280. server = Protocol()
  281. finished = loopback.loopbackAsync(server, client, dummyPolicy)
  282. self.assertEqual(pumpCalls, [])
  283. client.transport.write(b"foo")
  284. client.transport.write(b"bar")
  285. server.transport.write(b"baz")
  286. server.transport.write(b"quux")
  287. server.transport.loseConnection()
  288. def cbComplete(ignored):
  289. self.assertEqual(
  290. pumpCalls,
  291. # The order here is somewhat arbitrary. The implementation
  292. # happens to always deliver data to the client first.
  293. [(client, [b"baz", b"quux", None]),
  294. (server, [b"foo", b"bar"])])
  295. finished.addCallback(cbComplete)
  296. return finished
  297. def test_identityPumpPolicy(self):
  298. """
  299. L{identityPumpPolicy} is a pump policy which calls the target's
  300. C{dataReceived} method one for each string in the queue passed to it.
  301. """
  302. bytes = []
  303. client = Protocol()
  304. client.dataReceived = bytes.append
  305. queue = loopback._LoopbackQueue()
  306. queue.put(b"foo")
  307. queue.put(b"bar")
  308. queue.put(None)
  309. loopback.identityPumpPolicy(queue, client)
  310. self.assertEqual(bytes, [b"foo", b"bar"])
  311. def test_collapsingPumpPolicy(self):
  312. """
  313. L{collapsingPumpPolicy} is a pump policy which calls the target's
  314. C{dataReceived} only once with all of the strings in the queue passed
  315. to it joined together.
  316. """
  317. bytes = []
  318. client = Protocol()
  319. client.dataReceived = bytes.append
  320. queue = loopback._LoopbackQueue()
  321. queue.put(b"foo")
  322. queue.put(b"bar")
  323. queue.put(None)
  324. loopback.collapsingPumpPolicy(queue, client)
  325. self.assertEqual(bytes, [b"foobar"])
  326. class LoopbackTCPTests(LoopbackTestCaseMixin, unittest.TestCase):
  327. loopbackFunc = staticmethod(loopback.loopbackTCP)
  328. class LoopbackUNIXTests(LoopbackTestCaseMixin, unittest.TestCase):
  329. loopbackFunc = staticmethod(loopback.loopbackUNIX)
  330. if interfaces.IReactorUNIX(reactor, None) is None:
  331. skip = "Current reactor does not support UNIX sockets"
  332. class LoopbackRelayTest(unittest.TestCase):
  333. """
  334. Test for L{twisted.protocols.loopback.LoopbackRelay}
  335. """
  336. class Receiver(Protocol):
  337. """
  338. Simple Receiver class used for testing LoopbackRelay
  339. """
  340. data = b''
  341. def dataReceived(self, data):
  342. "Accumulate received data for verification"
  343. self.data += data
  344. def test_write(self):
  345. "Test to verify that the write function works as expected"
  346. receiver = self.Receiver()
  347. relay = loopback.LoopbackRelay(receiver)
  348. relay.write(b'abc')
  349. relay.write(b'def')
  350. self.assertEqual(receiver.data, b'')
  351. relay.clearBuffer()
  352. self.assertEqual(receiver.data, b'abcdef')
  353. def test_writeSequence(self):
  354. "Test to verify that the writeSequence function works as expected"
  355. receiver = self.Receiver()
  356. relay = loopback.LoopbackRelay(receiver)
  357. relay.writeSequence(
  358. [b'The ', b'quick ', b'brown ', b'fox '])
  359. relay.writeSequence(
  360. [b'jumps ', b'over ', b'the lazy dog'])
  361. self.assertEqual(receiver.data, b'')
  362. relay.clearBuffer()
  363. self.assertEqual(
  364. receiver.data, b'The quick brown fox jumps over the lazy dog')