test_forwarding.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.conch.ssh.forwarding}.
  5. """
  6. from __future__ import division, absolute_import
  7. from twisted.conch.ssh import forwarding
  8. from twisted.internet.address import IPv6Address
  9. from twisted.trial import unittest
  10. from twisted.internet.test.test_endpoints import deterministicResolvingReactor
  11. from twisted.test.proto_helpers import MemoryReactorClock, StringTransport
  12. class TestSSHConnectForwardingChannel(unittest.TestCase):
  13. """
  14. Unit and integration tests for L{SSHConnectForwardingChannel}.
  15. """
  16. def makeTCPConnection(self, reactor):
  17. """
  18. Fake that connection was established for first connectTCP request made
  19. on C{reactor}.
  20. @param reactor: Reactor on which to fake the connection.
  21. @type reactor: A reactor.
  22. """
  23. factory = reactor.tcpClients[0][2]
  24. connector = reactor.connectors[0]
  25. protocol = factory.buildProtocol(None)
  26. transport = StringTransport(peerAddress=connector.getDestination())
  27. protocol.makeConnection(transport)
  28. def test_channelOpenHostnameRequests(self):
  29. """
  30. When a hostname is sent as part of forwarding requests, it
  31. is resolved using HostnameEndpoint's resolver.
  32. """
  33. sut = forwarding.SSHConnectForwardingChannel(
  34. hostport=('fwd.example.org', 1234))
  35. # Patch channel and resolver to not touch the network.
  36. memoryReactor = MemoryReactorClock()
  37. sut._reactor = deterministicResolvingReactor(memoryReactor, ['::1'])
  38. sut.channelOpen(None)
  39. self.makeTCPConnection(memoryReactor)
  40. self.successResultOf(sut._channelOpenDeferred)
  41. # Channel is connected using a forwarding client to the resolved
  42. # address of the requested host.
  43. self.assertIsInstance(sut.client, forwarding.SSHForwardingClient)
  44. self.assertEqual(
  45. IPv6Address('TCP', '::1', 1234), sut.client.transport.getPeer())