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_ident.py 6.9KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for twisted.protocols.ident module.
  5. """
  6. import struct
  7. from twisted.protocols import ident
  8. from twisted.python import failure
  9. from twisted.internet import error
  10. from twisted.internet import defer
  11. from twisted.python.compat import NativeStringIO
  12. from twisted.trial import unittest
  13. from twisted.test.proto_helpers import StringTransport
  14. try:
  15. import builtins
  16. except ImportError:
  17. import __builtin__ as builtins
  18. class ClassParserTests(unittest.TestCase):
  19. """
  20. Test parsing of ident responses.
  21. """
  22. def setUp(self):
  23. """
  24. Create an ident client used in tests.
  25. """
  26. self.client = ident.IdentClient()
  27. def test_indentError(self):
  28. """
  29. 'UNKNOWN-ERROR' error should map to the L{ident.IdentError} exception.
  30. """
  31. d = defer.Deferred()
  32. self.client.queries.append((d, 123, 456))
  33. self.client.lineReceived('123, 456 : ERROR : UNKNOWN-ERROR')
  34. return self.assertFailure(d, ident.IdentError)
  35. def test_noUSerError(self):
  36. """
  37. 'NO-USER' error should map to the L{ident.NoUser} exception.
  38. """
  39. d = defer.Deferred()
  40. self.client.queries.append((d, 234, 456))
  41. self.client.lineReceived('234, 456 : ERROR : NO-USER')
  42. return self.assertFailure(d, ident.NoUser)
  43. def test_invalidPortError(self):
  44. """
  45. 'INVALID-PORT' error should map to the L{ident.InvalidPort} exception.
  46. """
  47. d = defer.Deferred()
  48. self.client.queries.append((d, 345, 567))
  49. self.client.lineReceived('345, 567 : ERROR : INVALID-PORT')
  50. return self.assertFailure(d, ident.InvalidPort)
  51. def test_hiddenUserError(self):
  52. """
  53. 'HIDDEN-USER' error should map to the L{ident.HiddenUser} exception.
  54. """
  55. d = defer.Deferred()
  56. self.client.queries.append((d, 567, 789))
  57. self.client.lineReceived('567, 789 : ERROR : HIDDEN-USER')
  58. return self.assertFailure(d, ident.HiddenUser)
  59. def test_lostConnection(self):
  60. """
  61. A pending query which failed because of a ConnectionLost should
  62. receive an L{ident.IdentError}.
  63. """
  64. d = defer.Deferred()
  65. self.client.queries.append((d, 765, 432))
  66. self.client.connectionLost(failure.Failure(error.ConnectionLost()))
  67. return self.assertFailure(d, ident.IdentError)
  68. class TestIdentServer(ident.IdentServer):
  69. def lookup(self, serverAddress, clientAddress):
  70. return self.resultValue
  71. class TestErrorIdentServer(ident.IdentServer):
  72. def lookup(self, serverAddress, clientAddress):
  73. raise self.exceptionType()
  74. class NewException(RuntimeError):
  75. pass
  76. class ServerParserTests(unittest.TestCase):
  77. def testErrors(self):
  78. p = TestErrorIdentServer()
  79. p.makeConnection(StringTransport())
  80. L = []
  81. p.sendLine = L.append
  82. p.exceptionType = ident.IdentError
  83. p.lineReceived('123, 345')
  84. self.assertEqual(L[0], '123, 345 : ERROR : UNKNOWN-ERROR')
  85. p.exceptionType = ident.NoUser
  86. p.lineReceived('432, 210')
  87. self.assertEqual(L[1], '432, 210 : ERROR : NO-USER')
  88. p.exceptionType = ident.InvalidPort
  89. p.lineReceived('987, 654')
  90. self.assertEqual(L[2], '987, 654 : ERROR : INVALID-PORT')
  91. p.exceptionType = ident.HiddenUser
  92. p.lineReceived('756, 827')
  93. self.assertEqual(L[3], '756, 827 : ERROR : HIDDEN-USER')
  94. p.exceptionType = NewException
  95. p.lineReceived('987, 789')
  96. self.assertEqual(L[4], '987, 789 : ERROR : UNKNOWN-ERROR')
  97. errs = self.flushLoggedErrors(NewException)
  98. self.assertEqual(len(errs), 1)
  99. for port in -1, 0, 65536, 65537:
  100. del L[:]
  101. p.lineReceived('%d, 5' % (port,))
  102. p.lineReceived('5, %d' % (port,))
  103. self.assertEqual(
  104. L, ['%d, 5 : ERROR : INVALID-PORT' % (port,),
  105. '5, %d : ERROR : INVALID-PORT' % (port,)])
  106. def testSuccess(self):
  107. p = TestIdentServer()
  108. p.makeConnection(StringTransport())
  109. L = []
  110. p.sendLine = L.append
  111. p.resultValue = ('SYS', 'USER')
  112. p.lineReceived('123, 456')
  113. self.assertEqual(L[0], '123, 456 : USERID : SYS : USER')
  114. if struct.pack('=L', 1)[0:1] == b'\x01':
  115. _addr1 = '0100007F'
  116. _addr2 = '04030201'
  117. else:
  118. _addr1 = '7F000001'
  119. _addr2 = '01020304'
  120. class ProcMixinTests(unittest.TestCase):
  121. line = ('4: %s:0019 %s:02FA 0A 00000000:00000000 '
  122. '00:00000000 00000000 0 0 10927 1 f72a5b80 '
  123. '3000 0 0 2 -1') % (_addr1, _addr2)
  124. sampleFile = (' sl local_address rem_address st tx_queue rx_queue tr '
  125. 'tm->when retrnsmt uid timeout inode\n ' + line)
  126. def testDottedQuadFromHexString(self):
  127. p = ident.ProcServerMixin()
  128. self.assertEqual(p.dottedQuadFromHexString(_addr1), '127.0.0.1')
  129. def testUnpackAddress(self):
  130. p = ident.ProcServerMixin()
  131. self.assertEqual(p.unpackAddress(_addr1 + ':0277'),
  132. ('127.0.0.1', 631))
  133. def testLineParser(self):
  134. p = ident.ProcServerMixin()
  135. self.assertEqual(
  136. p.parseLine(self.line),
  137. (('127.0.0.1', 25), ('1.2.3.4', 762), 0))
  138. def testExistingAddress(self):
  139. username = []
  140. p = ident.ProcServerMixin()
  141. p.entries = lambda: iter([self.line])
  142. p.getUsername = lambda uid: (username.append(uid), 'root')[1]
  143. self.assertEqual(
  144. p.lookup(('127.0.0.1', 25), ('1.2.3.4', 762)),
  145. (p.SYSTEM_NAME, 'root'))
  146. self.assertEqual(username, [0])
  147. def testNonExistingAddress(self):
  148. p = ident.ProcServerMixin()
  149. p.entries = lambda: iter([self.line])
  150. self.assertRaises(ident.NoUser, p.lookup, ('127.0.0.1', 26),
  151. ('1.2.3.4', 762))
  152. self.assertRaises(ident.NoUser, p.lookup, ('127.0.0.1', 25),
  153. ('1.2.3.5', 762))
  154. self.assertRaises(ident.NoUser, p.lookup, ('127.0.0.1', 25),
  155. ('1.2.3.4', 763))
  156. def testLookupProcNetTcp(self):
  157. """
  158. L{ident.ProcServerMixin.lookup} uses the Linux TCP process table.
  159. """
  160. open_calls = []
  161. def mocked_open(*args, **kwargs):
  162. """
  163. Mock for the open call to prevent actually opening /proc/net/tcp.
  164. """
  165. open_calls.append((args, kwargs))
  166. return NativeStringIO(self.sampleFile)
  167. self.patch(builtins, 'open', mocked_open)
  168. p = ident.ProcServerMixin()
  169. self.assertRaises(ident.NoUser, p.lookup, ('127.0.0.1', 26),
  170. ('1.2.3.4', 762))
  171. self.assertEqual([(('/proc/net/tcp',), {})], open_calls)