diff options
author | lovetox <philipp@hoerist.com> | 2020-08-27 13:11:56 +0300 |
---|---|---|
committer | lovetox <philipp@hoerist.com> | 2020-08-27 23:05:41 +0300 |
commit | 597c55ea72ca2ee9535082b174bd3eb33e0d0176 (patch) | |
tree | b5c8f2bf9d54d832f781bbe7855bd35499b69221 /test | |
parent | 3199c2f01a7bfcd133c802ba8245237481da940f (diff) |
Add JID Escaping (XEP-0106) support
Fixes #82
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/test_jid_parsing.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/test/unit/test_jid_parsing.py b/test/unit/test_jid_parsing.py index d1d9d00..991942a 100644 --- a/test/unit/test_jid_parsing.py +++ b/test/unit/test_jid_parsing.py @@ -68,3 +68,111 @@ class JIDParsing(unittest.TestCase): for jid in tests: self.assertTrue(JID.from_string(jid) == JID.from_string(jid)) + + def test_jid_escaping(self): + # (user input, escaped) + tests = [ + (r'space cadet@example.com', + r'space\20cadet@example.com'), + + (r'call me "ishmael"@example.com', + r'call\20me\20\22ishmael\22@example.com'), + + (r'at&t guy@example.com', + r'at\26t\20guy@example.com'), + + ('d\'artagnan@example.com', + r'd\27artagnan@example.com'), + + (r'/.fanboy@example.com', + r'\2f.fanboy@example.com'), + + (r'::foo::@example.com', + r'\3a\3afoo\3a\3a@example.com'), + + (r'<foo>@example.com', + r'\3cfoo\3e@example.com'), + + (r'user@host@example.com', + r'user\40host@example.com'), + + (r'c:\net@example.com', + r'c\3a\net@example.com'), + + (r'c:\\net@example.com', + r'c\3a\\net@example.com'), + + (r'c:\cool stuff@example.com', + r'c\3a\cool\20stuff@example.com'), + + (r'c:\5commas@example.com', + r'c\3a\5c5commas@example.com'), + + (r'call me\20@example.com', + r'call\20me\5c20@example.com'), + ] + + tests2 = [ + 'juliet@example.com', + 'juliet@example.com', + 'juliet@example.com', + 'juliet@example.com', + 'fussball@example.com', + 'fu\U000000DFball@example.com', + '\U000003C0@example.com', + '\U000003A3@example.com', + '\U000003C3@example.com', + '\U000003C2@example.com', + 'example.com', + ] + + test3 = [ + '\\20callme\\20@example.com', + '\\20callme@example.com', + 'callme\\20@example.com', + ] + + test4 = [ + ('call\\20me@example.com', 'call me@example.com',) + ] + + fail_tests = [ + r'c:\5commas@example.com/asd', + r'juliet@example.com/test' + ] + + for user_input, escaped in tests: + # Parse user input and escape it + jid = JID.from_user_input(user_input) + + self.assertTrue(jid.domain == 'example.com') + self.assertTrue(str(jid) == escaped) + self.assertTrue(jid.to_user_string() == user_input) + + # We must fail on invalid JIDs + with self.assertRaises(Exception): + JID.from_string(user_input) + + # Parse escaped JIDs + jid = JID.from_string(escaped) + self.assertTrue(str(jid) == escaped) + self.assertTrue(jid.domain == 'example.com') + + for jid in tests2: + # show that from_string() and from_user_input() produce the same + # result for valid bare JIDs + self.assertTrue(JID.from_string(jid) == JID.from_user_input(jid)) + + for jid in test3: + # JIDs starting or ending with \20 are not escaped + self.assertTrue(JID.from_string(jid).to_user_string() == jid) + + for user_input, user_string in test4: + # Test escaped keyword argument + self.assertTrue(JID.from_user_input(user_input, escaped=True) != JID.from_user_input(user_input)) + self.assertTrue(JID.from_user_input(user_input, escaped=True).to_user_string() == user_string) + + for user_input in fail_tests: + # from_user_input does only support bare jids + with self.assertRaises(Exception): + JID.from_user_input(user_input) |