1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
# -*- encoding: utf8 -*-
import unittest
from slixmpp.test import SlixTest
from slixmpp import JID, InvalidJID
class TestJIDClass(SlixTest):
"""Verify that the JID class can parse and manipulate JIDs."""
def testJIDFromFull(self):
"""Test using JID of the form 'user@server/resource/with/slashes'."""
self.check_jid(JID('user@someserver/some/resource'),
'user',
'someserver',
'some/resource',
'user@someserver',
'user@someserver/some/resource',
'user@someserver/some/resource')
def testJIDchange(self):
"""Test changing JID of the form 'user@server/resource/with/slashes'"""
j = JID('user1@someserver1/some1/resource1')
j.user = 'user'
j.domain = 'someserver'
j.resource = 'some/resource'
self.check_jid(j,
'user',
'someserver',
'some/resource',
'user@someserver',
'user@someserver/some/resource',
'user@someserver/some/resource')
j.node = None
self.check_jid(j,
None,
'someserver',
'some/resource',
'someserver',
'someserver/some/resource',
'someserver/some/resource')
j.resource = None
self.check_jid(j,
None,
'someserver',
None,
'someserver',
'someserver',
'someserver')
def testJIDaliases(self):
"""Test changing JID using aliases for domain."""
j = JID('user@someserver/resource')
j.server = 'anotherserver'
self.check_jid(j, domain='anotherserver')
j.host = 'yetanother'
self.check_jid(j, domain='yetanother')
def testJIDSetFullWithUser(self):
"""Test setting the full JID with a user portion."""
j = JID('user@domain/resource')
j.full = 'otheruser@otherdomain/otherresource'
self.check_jid(j,
'otheruser',
'otherdomain',
'otherresource',
'otheruser@otherdomain',
'otheruser@otherdomain/otherresource',
'otheruser@otherdomain/otherresource')
def testJIDFullNoUserWithResource(self):
"""
Test setting the full JID without a user
portion and with a resource.
"""
j = JID('user@domain/resource')
j.full = 'otherdomain/otherresource'
self.check_jid(j,
'',
'otherdomain',
'otherresource',
'otherdomain',
'otherdomain/otherresource',
'otherdomain/otherresource')
def testJIDFullNoUserNoResource(self):
"""
Test setting the full JID without a user
portion and without a resource.
"""
j = JID('user@domain/resource')
j.full = 'otherdomain'
self.check_jid(j,
'',
'otherdomain',
'',
'otherdomain',
'otherdomain',
'otherdomain')
def testJIDBareUser(self):
"""Test setting the bare JID with a user."""
j = JID('user@domain/resource')
j.bare = 'otheruser@otherdomain'
self.check_jid(j,
'otheruser',
'otherdomain',
'resource',
'otheruser@otherdomain',
'otheruser@otherdomain/resource',
'otheruser@otherdomain/resource')
def testJIDBareNoUser(self):
"""Test setting the bare JID without a user."""
j = JID('user@domain/resource')
j.bare = 'otherdomain'
self.check_jid(j,
'',
'otherdomain',
'resource',
'otherdomain',
'otherdomain/resource',
'otherdomain/resource')
def testJIDNoResource(self):
"""Test using JID of the form 'user@domain'."""
self.check_jid(JID('user@someserver'),
'user',
'someserver',
'',
'user@someserver',
'user@someserver',
'user@someserver')
def testJIDNoUser(self):
"""Test JID of the form 'component.domain.tld'."""
self.check_jid(JID('component.someserver'),
'',
'component.someserver',
'',
'component.someserver',
'component.someserver',
'component.someserver')
def testJIDEquality(self):
"""Test that JIDs with the same content are equal."""
jid1 = JID('user@domain/resource')
jid2 = JID('user@domain/resource')
self.assertTrue(jid1 == jid2, "Same JIDs are not considered equal")
self.assertFalse(jid1 != jid2, "Same JIDs are considered not equal")
jid3 = JID()
jid4 = JID()
self.assertTrue(jid3 == jid4, "Same empty JIDs are not considered equal")
self.assertFalse(jid3 != jid4, "Same empty JIDs are considered not equal")
def testJIDInequality(self):
jid1 = JID('user@domain/resource')
jid2 = JID('otheruser@domain/resource')
self.assertFalse(jid1 == jid2, "Different JIDs are considered equal")
self.assertTrue(jid1 != jid2, "Different JIDs are considered equal")
jid3 = JID()
self.assertFalse(jid1 == jid3, "JID and empty JID are considered equal")
self.assertTrue(jid1 != jid3, "JID and empty JID are considered equal")
def testZeroLengthDomain(self):
jid1 = JID('')
jid2 = JID()
self.assertTrue(jid1 == jid2, "Empty JIDs are not considered equal")
self.assertTrue(jid1.domain == '', "Empty JID’s domain part not empty")
self.assertTrue(jid1.full == '', "Empty JID’s full part not empty")
self.assertRaises(InvalidJID, JID, 'user@')
self.assertRaises(InvalidJID, JID, '/resource')
self.assertRaises(InvalidJID, JID, 'user@/resource')
def testZeroLengthLocalPart(self):
self.assertRaises(InvalidJID, JID, '@test.com')
self.assertRaises(InvalidJID, JID, '@test.com/resource')
def testZeroLengthNodeDomain(self):
self.assertRaises(InvalidJID, JID, '@/test.com')
def testZeroLengthResource(self):
self.assertRaises(InvalidJID, JID, 'test.com/')
self.assertRaises(InvalidJID, JID, 'user@test.com/')
def test1023LengthLocalPart(self):
local = 'a' * 1023
jid = JID('%s@test.com' % local)
def test1023LengthResource(self):
resource = 'r' * 1023
jid = JID('test.com/%s' % resource)
def test1024LengthDomain(self):
domain = ('a.' * 509) + 'aa.com'
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s' % domain)
self.assertRaises(InvalidJID, JID, '%s/resource' % domain)
self.assertRaises(InvalidJID, JID, domain)
def test1024LengthLocalPart(self):
local = 'a' * 1024
self.assertRaises(InvalidJID, JID, '%s@test.com' % local)
self.assertRaises(InvalidJID, JID, '%s@test.com/resource' % local)
def test1024LengthResource(self):
resource = 'r' * 1024
self.assertRaises(InvalidJID, JID, 'test.com/%s' % resource)
self.assertRaises(InvalidJID, JID, 'user@test.com/%s' % resource)
def testTooLongDomainLabel(self):
domain = ('a' * 64) + '.com'
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
def testDomainEmptyLabel(self):
domain = 'aaa..bbb.com'
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
def testDomainIPv4(self):
domain = '127.0.0.1'
jid1 = JID('%s' % domain)
jid2 = JID('user@%s' % domain)
jid3 = JID('%s/resource' % domain)
jid4 = JID('user@%s/resource' % domain)
def testDomainIPv6(self):
domain = '[::1]'
jid1 = JID('%s' % domain)
jid2 = JID('user@%s' % domain)
jid3 = JID('%s/resource' % domain)
jid4 = JID('user@%s/resource' % domain)
def testDomainInvalidIPv6NoBrackets(self):
domain = '::1'
self.assertRaises(InvalidJID, JID, '%s' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s' % domain)
self.assertRaises(InvalidJID, JID, '%s/resource' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
def testDomainInvalidIPv6MissingBracket(self):
domain = '[::1'
self.assertRaises(InvalidJID, JID, '%s' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s' % domain)
self.assertRaises(InvalidJID, JID, '%s/resource' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
def testDomainInvalidIPv6WrongBracket(self):
domain = '[::]1]'
self.assertRaises(InvalidJID, JID, '%s' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s' % domain)
self.assertRaises(InvalidJID, JID, '%s/resource' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
def testDomainWithPort(self):
domain = 'example.com:5555'
self.assertRaises(InvalidJID, JID, '%s' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s' % domain)
self.assertRaises(InvalidJID, JID, '%s/resource' % domain)
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
def testDomainWithTrailingDot(self):
domain = 'example.com.'
jid = JID('user@%s/resource' % domain)
self.assertEqual(jid.domain, 'example.com')
def testDomainWithDashes(self):
domain = 'example.com-'
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
domain = '-example.com'
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
def testStartOrEndWithEscapedSpaces(self):
local = ' foo'
self.assertRaises(InvalidJID, JID, '%s@example.com' % local)
local = 'bar '
self.assertRaises(InvalidJID, JID, '%s@example.com' % local)
# Need more input for these cases. A JID starting with \20 *is* valid
# according to RFC 6122, but is not according to XEP-0106.
#self.assertRaises(InvalidJID, JID, '%s@example.com' % '\\20foo2')
#self.assertRaises(InvalidJID, JID, '%s@example.com' % 'bar2\\20')
def testJapaneseLocalpart(self):
jid = JID('あいうえお@example.com')
self.assertEqual(jid.node, 'あいうえお')
self.assertEqual(jid.domain, 'example.com')
def testHash(self):
jid = JID('あいうえお@example.com')
self.assertEqual(hash(jid.full), hash(jid))
jid = JID('toto@example.com/aaa')
self.assertEqual(hash(jid.full), hash(jid))
jid = JID('totoéà@example.com/aaa')
self.assertEqual(hash(jid.full), hash(jid))
def test_comparison_invalid(self):
jid = JID('toto@example.com')
self.assertFalse(jid == "@@@@")
self.assertFalse(jid in ["abc", "def", "@@@@"])
suite = unittest.TestLoader().loadTestsFromTestCase(TestJIDClass)
|