test_manhole_tap.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.conch.manhole_tap}.
  5. """
  6. try:
  7. import cryptography
  8. except ImportError:
  9. cryptography = None
  10. try:
  11. import pyasn1
  12. except ImportError:
  13. pyasn1 = None
  14. if cryptography and pyasn1:
  15. from twisted.conch import manhole_tap, manhole_ssh
  16. from twisted.application.internet import StreamServerEndpointService
  17. from twisted.application.service import MultiService
  18. from twisted.cred import error
  19. from twisted.cred.credentials import UsernamePassword
  20. from twisted.conch import telnet
  21. from twisted.python import usage
  22. from twisted.trial.unittest import TestCase
  23. class MakeServiceTests(TestCase):
  24. """
  25. Tests for L{manhole_tap.makeService}.
  26. """
  27. if not cryptography:
  28. skip = "can't run without cryptography"
  29. if not pyasn1:
  30. skip = "Cannot run without PyASN1"
  31. usernamePassword = (b'iamuser', b'thisispassword')
  32. def setUp(self):
  33. """
  34. Create a passwd-like file with a user.
  35. """
  36. self.filename = self.mktemp()
  37. with open(self.filename, 'wb') as f:
  38. f.write(b':'.join(self.usernamePassword))
  39. self.options = manhole_tap.Options()
  40. def test_requiresPort(self):
  41. """
  42. L{manhole_tap.makeService} requires either 'telnetPort' or 'sshPort' to
  43. be given.
  44. """
  45. with self.assertRaises(usage.UsageError) as e:
  46. manhole_tap.Options().parseOptions([])
  47. self.assertEqual(e.exception.args[0], ("At least one of --telnetPort "
  48. "and --sshPort must be specified"))
  49. def test_telnetPort(self):
  50. """
  51. L{manhole_tap.makeService} will make a telnet service on the port
  52. defined by C{--telnetPort}. It will not make a SSH service.
  53. """
  54. self.options.parseOptions(["--telnetPort", "tcp:222"])
  55. service = manhole_tap.makeService(self.options)
  56. self.assertIsInstance(service, MultiService)
  57. self.assertEqual(len(service.services), 1)
  58. self.assertIsInstance(service.services[0], StreamServerEndpointService)
  59. self.assertIsInstance(service.services[0].factory.protocol,
  60. manhole_tap.makeTelnetProtocol)
  61. self.assertEqual(service.services[0].endpoint._port, 222)
  62. def test_sshPort(self):
  63. """
  64. L{manhole_tap.makeService} will make a SSH service on the port
  65. defined by C{--sshPort}. It will not make a telnet service.
  66. """
  67. # Why the sshKeyDir and sshKeySize params? To prevent it stomping over
  68. # (or using!) the user's private key, we just make a super small one
  69. # which will never be used in a temp directory.
  70. self.options.parseOptions(["--sshKeyDir", self.mktemp(),
  71. "--sshKeySize", "512",
  72. "--sshPort", "tcp:223"])
  73. service = manhole_tap.makeService(self.options)
  74. self.assertIsInstance(service, MultiService)
  75. self.assertEqual(len(service.services), 1)
  76. self.assertIsInstance(service.services[0], StreamServerEndpointService)
  77. self.assertIsInstance(service.services[0].factory,
  78. manhole_ssh.ConchFactory)
  79. self.assertEqual(service.services[0].endpoint._port, 223)
  80. def test_passwd(self):
  81. """
  82. The C{--passwd} command-line option will load a passwd-like file.
  83. """
  84. self.options.parseOptions(['--telnetPort', 'tcp:22',
  85. '--passwd', self.filename])
  86. service = manhole_tap.makeService(self.options)
  87. portal = service.services[0].factory.protocol.portal
  88. self.assertEqual(len(portal.checkers.keys()), 2)
  89. # Ensure it's the passwd file we wanted by trying to authenticate
  90. self.assertTrue(self.successResultOf(
  91. portal.login(UsernamePassword(*self.usernamePassword),
  92. None, telnet.ITelnetProtocol)))
  93. self.assertIsInstance(self.failureResultOf(
  94. portal.login(UsernamePassword(b"wrong", b"user"),
  95. None, telnet.ITelnetProtocol)).value,
  96. error.UnauthorizedLogin)