Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.6KB

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