2013-06-19 15:21:54 +00:00
|
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
|
from __future__ import unicode_literals
|
2013-07-26 11:02:26 +00:00
|
|
|
|
import unittest
|
2014-07-17 12:19:04 +00:00
|
|
|
|
from slixmpp.test import SlixTest
|
|
|
|
|
from slixmpp import JID, InvalidJID
|
|
|
|
|
from slixmpp.jid import nodeprep
|
2010-07-27 01:13:09 +00:00
|
|
|
|
|
2010-10-07 14:58:13 +00:00
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
|
class TestJIDClass(SlixTest):
|
2010-10-24 22:22:41 +00:00
|
|
|
|
|
|
|
|
|
"""Verify that the JID class can parse and manipulate JIDs."""
|
|
|
|
|
|
2010-10-24 23:06:54 +00:00
|
|
|
|
def testJIDFromFull(self):
|
2010-10-24 22:22:41 +00:00
|
|
|
|
"""Test using JID of the form 'user@server/resource/with/slashes'."""
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(JID('user@someserver/some/resource'),
|
2010-10-24 22:22:41 +00:00
|
|
|
|
'user',
|
|
|
|
|
'someserver',
|
|
|
|
|
'some/resource',
|
|
|
|
|
'user@someserver',
|
|
|
|
|
'user@someserver/some/resource',
|
|
|
|
|
'user@someserver/some/resource')
|
2010-07-27 01:13:09 +00:00
|
|
|
|
|
|
|
|
|
def testJIDchange(self):
|
2010-10-24 22:22:41 +00:00
|
|
|
|
"""Test changing JID of the form 'user@server/resource/with/slashes'"""
|
2010-07-27 01:13:09 +00:00
|
|
|
|
j = JID('user1@someserver1/some1/resource1')
|
|
|
|
|
j.user = 'user'
|
|
|
|
|
j.domain = 'someserver'
|
|
|
|
|
j.resource = 'some/resource'
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(j,
|
2010-10-24 22:22:41 +00:00
|
|
|
|
'user',
|
|
|
|
|
'someserver',
|
|
|
|
|
'some/resource',
|
|
|
|
|
'user@someserver',
|
|
|
|
|
'user@someserver/some/resource',
|
|
|
|
|
'user@someserver/some/resource')
|
|
|
|
|
|
2010-10-24 23:06:54 +00:00
|
|
|
|
def testJIDaliases(self):
|
|
|
|
|
"""Test changing JID using aliases for domain."""
|
|
|
|
|
j = JID('user@someserver/resource')
|
|
|
|
|
j.server = 'anotherserver'
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(j, domain='anotherserver')
|
2010-10-24 23:06:54 +00:00
|
|
|
|
j.host = 'yetanother'
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(j, domain='yetanother')
|
2010-10-24 23:06:54 +00:00
|
|
|
|
|
|
|
|
|
def testJIDSetFullWithUser(self):
|
|
|
|
|
"""Test setting the full JID with a user portion."""
|
|
|
|
|
j = JID('user@domain/resource')
|
|
|
|
|
j.full = 'otheruser@otherdomain/otherresource'
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(j,
|
2010-10-24 23:06:54 +00:00
|
|
|
|
'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'
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(j,
|
2010-10-24 23:06:54 +00:00
|
|
|
|
'',
|
|
|
|
|
'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'
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(j,
|
2010-10-24 23:06:54 +00:00
|
|
|
|
'',
|
|
|
|
|
'otherdomain',
|
|
|
|
|
'',
|
|
|
|
|
'otherdomain',
|
|
|
|
|
'otherdomain',
|
|
|
|
|
'otherdomain')
|
|
|
|
|
|
|
|
|
|
def testJIDBareUser(self):
|
|
|
|
|
"""Test setting the bare JID with a user."""
|
|
|
|
|
j = JID('user@domain/resource')
|
|
|
|
|
j.bare = 'otheruser@otherdomain'
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(j,
|
2010-10-24 23:06:54 +00:00
|
|
|
|
'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'
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(j,
|
2010-10-24 23:06:54 +00:00
|
|
|
|
'',
|
|
|
|
|
'otherdomain',
|
|
|
|
|
'resource',
|
|
|
|
|
'otherdomain',
|
|
|
|
|
'otherdomain/resource',
|
|
|
|
|
'otherdomain/resource')
|
|
|
|
|
|
|
|
|
|
def testJIDNoResource(self):
|
2010-10-24 22:22:41 +00:00
|
|
|
|
"""Test using JID of the form 'user@domain'."""
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(JID('user@someserver'),
|
2010-10-24 22:22:41 +00:00
|
|
|
|
'user',
|
|
|
|
|
'someserver',
|
|
|
|
|
'',
|
|
|
|
|
'user@someserver',
|
|
|
|
|
'user@someserver',
|
|
|
|
|
'user@someserver')
|
|
|
|
|
|
2010-10-24 23:06:54 +00:00
|
|
|
|
def testJIDNoUser(self):
|
2010-10-24 22:22:41 +00:00
|
|
|
|
"""Test JID of the form 'component.domain.tld'."""
|
2010-11-05 18:45:58 +00:00
|
|
|
|
self.check_jid(JID('component.someserver'),
|
2010-10-24 22:22:41 +00:00
|
|
|
|
'',
|
|
|
|
|
'component.someserver',
|
|
|
|
|
'',
|
|
|
|
|
'component.someserver',
|
|
|
|
|
'component.someserver',
|
|
|
|
|
'component.someserver')
|
2010-07-27 01:13:09 +00:00
|
|
|
|
|
2011-11-14 17:15:43 +00:00
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
def testJIDInequality(self):
|
|
|
|
|
jid1 = JID('user@domain/resource')
|
|
|
|
|
jid2 = JID('otheruser@domain/resource')
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertFalse(jid1 == jid2, "Different JIDs are considered equal")
|
|
|
|
|
self.assertTrue(jid1 != jid2, "Different JIDs are considered equal")
|
2011-11-14 17:15:43 +00:00
|
|
|
|
|
2012-07-24 04:45:24 +00:00
|
|
|
|
def testZeroLengthDomain(self):
|
2015-06-11 23:47:21 +00:00
|
|
|
|
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')
|
2012-07-24 04:45:24 +00:00
|
|
|
|
self.assertRaises(InvalidJID, JID, 'user@/resource')
|
|
|
|
|
|
|
|
|
|
def testZeroLengthLocalPart(self):
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertRaises(InvalidJID, JID, '@test.com')
|
|
|
|
|
self.assertRaises(InvalidJID, JID, '@test.com/resource')
|
|
|
|
|
|
|
|
|
|
def testZeroLengthNodeDomain(self):
|
2012-07-24 04:45:24 +00:00
|
|
|
|
self.assertRaises(InvalidJID, JID, '@/test.com')
|
|
|
|
|
|
|
|
|
|
def testZeroLengthResource(self):
|
|
|
|
|
self.assertRaises(InvalidJID, JID, 'test.com/')
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertRaises(InvalidJID, JID, 'user@test.com/')
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def test1023LengthDomain(self):
|
|
|
|
|
domain = ('a.' * 509) + 'a.com'
|
2015-06-11 23:47:21 +00:00
|
|
|
|
jid = JID('user@%s/resource' % domain)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def test1023LengthLocalPart(self):
|
|
|
|
|
local = 'a' * 1023
|
2015-06-11 23:47:21 +00:00
|
|
|
|
jid = JID('%s@test.com' % local)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def test1023LengthResource(self):
|
|
|
|
|
resource = 'r' * 1023
|
2015-06-11 23:47:21 +00:00
|
|
|
|
jid = JID('test.com/%s' % resource)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def test1024LengthDomain(self):
|
|
|
|
|
domain = ('a.' * 509) + 'aa.com'
|
|
|
|
|
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertRaises(InvalidJID, JID, 'user@%s' % domain)
|
|
|
|
|
self.assertRaises(InvalidJID, JID, '%s/resource' % domain)
|
|
|
|
|
self.assertRaises(InvalidJID, JID, domain)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def test1024LengthLocalPart(self):
|
|
|
|
|
local = 'a' * 1024
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertRaises(InvalidJID, JID, '%s@test.com' % local)
|
|
|
|
|
self.assertRaises(InvalidJID, JID, '%s@test.com/resource' % local)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def test1024LengthResource(self):
|
|
|
|
|
resource = 'r' * 1024
|
|
|
|
|
self.assertRaises(InvalidJID, JID, 'test.com/%s' % resource)
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertRaises(InvalidJID, JID, 'user@test.com/%s' % resource)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
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'
|
2015-06-11 23:47:21 +00:00
|
|
|
|
|
|
|
|
|
jid1 = JID('%s' % domain)
|
|
|
|
|
jid2 = JID('user@%s' % domain)
|
|
|
|
|
jid3 = JID('%s/resource' % domain)
|
|
|
|
|
jid4 = JID('user@%s/resource' % domain)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def testDomainIPv6(self):
|
|
|
|
|
domain = '[::1]'
|
2015-06-11 23:47:21 +00:00
|
|
|
|
|
|
|
|
|
jid1 = JID('%s' % domain)
|
|
|
|
|
jid2 = JID('user@%s' % domain)
|
|
|
|
|
jid3 = JID('%s/resource' % domain)
|
|
|
|
|
jid4 = JID('user@%s/resource' % domain)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def testDomainInvalidIPv6NoBrackets(self):
|
|
|
|
|
domain = '::1'
|
|
|
|
|
|
2015-06-11 23:47:21 +00:00
|
|
|
|
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)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def testDomainInvalidIPv6MissingBracket(self):
|
|
|
|
|
domain = '[::1'
|
|
|
|
|
|
2015-06-11 23:47:21 +00:00
|
|
|
|
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)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def testDomainWithPort(self):
|
|
|
|
|
domain = 'example.com:5555'
|
2015-06-11 23:47:21 +00:00
|
|
|
|
|
|
|
|
|
self.assertRaises(InvalidJID, JID, '%s' % domain)
|
|
|
|
|
self.assertRaises(InvalidJID, JID, 'user@%s' % domain)
|
|
|
|
|
self.assertRaises(InvalidJID, JID, '%s/resource' % domain)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
self.assertRaises(InvalidJID, JID, 'user@%s/resource' % domain)
|
|
|
|
|
|
|
|
|
|
def testDomainWithTrailingDot(self):
|
|
|
|
|
domain = 'example.com.'
|
2015-06-11 23:47:21 +00:00
|
|
|
|
jid = JID('user@%s/resource' % domain)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertEqual(jid.domain, 'example.com')
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
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 testACEDomain(self):
|
|
|
|
|
domain = 'xn--bcher-kva.ch'
|
2015-06-11 23:47:21 +00:00
|
|
|
|
jid = JID('user@%s/resource' % domain)
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertEqual(jid.domain.encode('utf-8'), b'b\xc3\xbccher.ch')
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
|
|
|
|
def testJIDUnescape(self):
|
2015-06-11 23:47:21 +00:00
|
|
|
|
jid = JID('here\\27s_a_wild_\\26_\\2fcr%zy\\2f_\\40ddress\\20for\\3a\\3cwv\\3e(\\22IMPS\\22)\\5c@example.com')
|
2012-07-24 04:45:24 +00:00
|
|
|
|
ujid = jid.unescape()
|
2015-06-11 23:47:21 +00:00
|
|
|
|
self.assertEqual(ujid.local, 'here\'s_a_wild_&_/cr%zy/_@ddress for:<wv>("imps")\\')
|
2012-07-24 04:45:24 +00:00
|
|
|
|
|
2015-06-11 23:47:21 +00:00
|
|
|
|
jid = JID('blah\\5cfoo\\5c20bar@example.com')
|
2012-07-24 04:45:24 +00:00
|
|
|
|
ujid = jid.unescape()
|
|
|
|
|
self.assertEqual(ujid.local, 'blah\\foo\\20bar')
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
2013-06-19 15:21:54 +00:00
|
|
|
|
def testNodePrepIdemptotent(self):
|
|
|
|
|
node = 'ᴹᴵᴷᴬᴱᴸ'
|
|
|
|
|
self.assertEqual(nodeprep(node), nodeprep(nodeprep(node)))
|
|
|
|
|
|
2010-10-07 14:58:13 +00:00
|
|
|
|
|
2010-07-27 01:13:09 +00:00
|
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestJIDClass)
|