test_jabberjid.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.words.protocols.jabber.jid}.
  5. """
  6. from twisted.python.compat import unicode
  7. from twisted.trial import unittest
  8. from twisted.words.protocols.jabber import jid
  9. class JIDParsingTests(unittest.TestCase):
  10. def test_parse(self):
  11. """
  12. Test different forms of JIDs.
  13. """
  14. # Basic forms
  15. self.assertEqual(jid.parse("user@host/resource"),
  16. ("user", "host", "resource"))
  17. self.assertEqual(jid.parse("user@host"),
  18. ("user", "host", None))
  19. self.assertEqual(jid.parse("host"),
  20. (None, "host", None))
  21. self.assertEqual(jid.parse("host/resource"),
  22. (None, "host", "resource"))
  23. # More interesting forms
  24. self.assertEqual(jid.parse("foo/bar@baz"),
  25. (None, "foo", "bar@baz"))
  26. self.assertEqual(jid.parse("boo@foo/bar@baz"),
  27. ("boo", "foo", "bar@baz"))
  28. self.assertEqual(jid.parse("boo@foo/bar/baz"),
  29. ("boo", "foo", "bar/baz"))
  30. self.assertEqual(jid.parse("boo/foo@bar@baz"),
  31. (None, "boo", "foo@bar@baz"))
  32. self.assertEqual(jid.parse("boo/foo/bar"),
  33. (None, "boo", "foo/bar"))
  34. self.assertEqual(jid.parse("boo//foo"),
  35. (None, "boo", "/foo"))
  36. def test_noHost(self):
  37. """
  38. Test for failure on no host part.
  39. """
  40. self.assertRaises(jid.InvalidFormat, jid.parse, "user@")
  41. def test_doubleAt(self):
  42. """
  43. Test for failure on double @ signs.
  44. This should fail because @ is not a valid character for the host
  45. part of the JID.
  46. """
  47. self.assertRaises(jid.InvalidFormat, jid.parse, "user@@host")
  48. def test_multipleAt(self):
  49. """
  50. Test for failure on two @ signs.
  51. This should fail because @ is not a valid character for the host
  52. part of the JID.
  53. """
  54. self.assertRaises(jid.InvalidFormat, jid.parse, "user@host@host")
  55. # Basic tests for case mapping. These are fallback tests for the
  56. # prepping done in twisted.words.protocols.jabber.xmpp_stringprep
  57. def test_prepCaseMapUser(self):
  58. """
  59. Test case mapping of the user part of the JID.
  60. """
  61. self.assertEqual(jid.prep("UsEr", "host", "resource"),
  62. ("user", "host", "resource"))
  63. def test_prepCaseMapHost(self):
  64. """
  65. Test case mapping of the host part of the JID.
  66. """
  67. self.assertEqual(jid.prep("user", "hoST", "resource"),
  68. ("user", "host", "resource"))
  69. def test_prepNoCaseMapResource(self):
  70. """
  71. Test no case mapping of the resourcce part of the JID.
  72. """
  73. self.assertEqual(jid.prep("user", "hoST", "resource"),
  74. ("user", "host", "resource"))
  75. self.assertNotEqual(jid.prep("user", "host", "Resource"),
  76. ("user", "host", "resource"))
  77. class JIDTests(unittest.TestCase):
  78. def test_noneArguments(self):
  79. """
  80. Test that using no arguments raises an exception.
  81. """
  82. self.assertRaises(RuntimeError, jid.JID)
  83. def test_attributes(self):
  84. """
  85. Test that the attributes correspond with the JID parts.
  86. """
  87. j = jid.JID("user@host/resource")
  88. self.assertEqual(j.user, "user")
  89. self.assertEqual(j.host, "host")
  90. self.assertEqual(j.resource, "resource")
  91. def test_userhost(self):
  92. """
  93. Test the extraction of the bare JID.
  94. """
  95. j = jid.JID("user@host/resource")
  96. self.assertEqual("user@host", j.userhost())
  97. def test_userhostOnlyHost(self):
  98. """
  99. Test the extraction of the bare JID of the full form host/resource.
  100. """
  101. j = jid.JID("host/resource")
  102. self.assertEqual("host", j.userhost())
  103. def test_userhostJID(self):
  104. """
  105. Test getting a JID object of the bare JID.
  106. """
  107. j1 = jid.JID("user@host/resource")
  108. j2 = jid.internJID("user@host")
  109. self.assertIdentical(j2, j1.userhostJID())
  110. def test_userhostJIDNoResource(self):
  111. """
  112. Test getting a JID object of the bare JID when there was no resource.
  113. """
  114. j = jid.JID("user@host")
  115. self.assertIdentical(j, j.userhostJID())
  116. def test_fullHost(self):
  117. """
  118. Test giving a string representation of the JID with only a host part.
  119. """
  120. j = jid.JID(tuple=(None, 'host', None))
  121. self.assertEqual('host', j.full())
  122. def test_fullHostResource(self):
  123. """
  124. Test giving a string representation of the JID with host, resource.
  125. """
  126. j = jid.JID(tuple=(None, 'host', 'resource'))
  127. self.assertEqual('host/resource', j.full())
  128. def test_fullUserHost(self):
  129. """
  130. Test giving a string representation of the JID with user, host.
  131. """
  132. j = jid.JID(tuple=('user', 'host', None))
  133. self.assertEqual('user@host', j.full())
  134. def test_fullAll(self):
  135. """
  136. Test giving a string representation of the JID.
  137. """
  138. j = jid.JID(tuple=('user', 'host', 'resource'))
  139. self.assertEqual('user@host/resource', j.full())
  140. def test_equality(self):
  141. """
  142. Test JID equality.
  143. """
  144. j1 = jid.JID("user@host/resource")
  145. j2 = jid.JID("user@host/resource")
  146. self.assertNotIdentical(j1, j2)
  147. self.assertEqual(j1, j2)
  148. def test_equalityWithNonJIDs(self):
  149. """
  150. Test JID equality.
  151. """
  152. j = jid.JID("user@host/resource")
  153. self.assertFalse(j == 'user@host/resource')
  154. def test_inequality(self):
  155. """
  156. Test JID inequality.
  157. """
  158. j1 = jid.JID("user1@host/resource")
  159. j2 = jid.JID("user2@host/resource")
  160. self.assertNotEqual(j1, j2)
  161. def test_inequalityWithNonJIDs(self):
  162. """
  163. Test JID equality.
  164. """
  165. j = jid.JID("user@host/resource")
  166. self.assertNotEqual(j, 'user@host/resource')
  167. def test_hashable(self):
  168. """
  169. Test JID hashability.
  170. """
  171. j1 = jid.JID("user@host/resource")
  172. j2 = jid.JID("user@host/resource")
  173. self.assertEqual(hash(j1), hash(j2))
  174. def test_unicode(self):
  175. """
  176. Test unicode representation of JIDs.
  177. """
  178. j = jid.JID(tuple=('user', 'host', 'resource'))
  179. self.assertEqual(u"user@host/resource", unicode(j))
  180. def test_repr(self):
  181. """
  182. Test representation of JID objects.
  183. """
  184. j = jid.JID(tuple=('user', 'host', 'resource'))
  185. self.assertEqual("JID(%s)" % repr(u'user@host/resource'), repr(j))
  186. class InternJIDTests(unittest.TestCase):
  187. def test_identity(self):
  188. """
  189. Test that two interned JIDs yield the same object.
  190. """
  191. j1 = jid.internJID("user@host")
  192. j2 = jid.internJID("user@host")
  193. self.assertIdentical(j1, j2)