diff options
author | Philipp Hörist <philipp@hoerist.com> | 2019-06-30 10:49:19 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2019-06-30 22:55:09 +0300 |
commit | c7597a9ed659a106b4a436d77e25a8f5f3b8b257 (patch) | |
tree | e4dae611899dd5f07cd557909e3dfed0846f0a24 /test | |
parent | 54d8fea2057e5b301ef4ed167f0c55090ebf0fff (diff) |
Refactor validating JIDs
- Validate while parsing into JID
- Add dedicated methods for the different JID parts
- Follow RTC7622 exactly
- Add unit tests
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/test_jid_parsing.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/unit/test_jid_parsing.py b/test/unit/test_jid_parsing.py new file mode 100644 index 0000000..56ecf88 --- /dev/null +++ b/test/unit/test_jid_parsing.py @@ -0,0 +1,60 @@ +import unittest + +from nbxmpp.protocol import LocalpartByteLimit +from nbxmpp.protocol import LocalpartNotAllowedChar +from nbxmpp.protocol import ResourcepartByteLimit +from nbxmpp.protocol import ResourcepartNotAllowedChar +from nbxmpp.protocol import DomainpartByteLimit +from nbxmpp.protocol import DomainpartNotAllowedChar +from nbxmpp.protocol import JID + +class JIDParsing(unittest.TestCase): + + def test_valid_jids(self): + tests = [ + 'juliet@example.com', + 'juliet@example.com/foo', + 'juliet@example.com/foo bar', + 'juliet@example.com/foo@bar', + 'foo\\20bar@example.com', + 'fussball@example.com', + 'fu\U000000DFball@example.com', + '\U000003C0@example.com', + '\U000003A3@example.com/foo', + '\U000003C3@example.com/foo', + '\U000003C2@example.com/foo', + 'king@example.com/\U0000265A', + 'example.com', + 'example.com/foobar', + 'a.example.com/b@example.net', + ] + + for jid in tests: + JID(jid) + + def test_invalid_jids(self): + tests = [ + ('"juliet"@example.com', LocalpartNotAllowedChar), + ('foo bar@example.com', LocalpartNotAllowedChar), + ('henry\U00002163@example.com', LocalpartNotAllowedChar), + ('@example.com', LocalpartByteLimit), + ('user@example.com/', ResourcepartByteLimit), + ('user@example.com/\U00000001', ResourcepartNotAllowedChar), + ('\U0000265A@example.com', LocalpartNotAllowedChar), + ('user@host@example.com', DomainpartNotAllowedChar), + ('juliet@', DomainpartByteLimit), + ('/foobar', DomainpartByteLimit), + ] + + for jid, exception in tests: + with self.assertRaises(exception): + JID(jid) + + def test_ip_literals(self): + tests = [ + ('juliet@[2002:4559:1FE2::4559:1FE2]/res'), + ('juliet@123.123.123.123/res'), + ] + + for jid in tests: + JID(jid) |