Imported Upstream version 12.1.0
[contrib/python-twisted.git] / twisted / words / test / test_jabbersaslmechanisms.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for L{twisted.words.protocols.jabber.sasl_mechanisms}.
6 """
7
8 from twisted.trial import unittest
9
10 from twisted.words.protocols.jabber import sasl_mechanisms
11
12 class PlainTest(unittest.TestCase):
13     def test_getInitialResponse(self):
14         """
15         Test the initial response.
16         """
17         m = sasl_mechanisms.Plain(None, 'test', 'secret')
18         self.assertEqual(m.getInitialResponse(), '\x00test\x00secret')
19
20
21
22 class AnonymousTest(unittest.TestCase):
23     """
24     Tests for L{twisted.words.protocols.jabber.sasl_mechanisms.Anonymous}.
25     """
26     def test_getInitialResponse(self):
27         """
28         Test the initial response to be empty.
29         """
30         m = sasl_mechanisms.Anonymous()
31         self.assertEqual(m.getInitialResponse(), None)
32
33
34
35 class DigestMD5Test(unittest.TestCase):
36     def setUp(self):
37         self.mechanism = sasl_mechanisms.DigestMD5('xmpp', 'example.org', None,
38                                                    'test', 'secret')
39
40
41     def test_getInitialResponse(self):
42         """
43         Test that no initial response is generated.
44         """
45         self.assertIdentical(self.mechanism.getInitialResponse(), None)
46
47     def test_getResponse(self):
48         """
49         Partially test challenge response.
50
51         Does not actually test the response-value, yet.
52         """
53
54         challenge = 'realm="localhost",nonce="1234",qop="auth",charset=utf-8,algorithm=md5-sess'
55         directives = self.mechanism._parse(self.mechanism.getResponse(challenge))
56         self.assertEqual(directives['username'], 'test')
57         self.assertEqual(directives['nonce'], '1234')
58         self.assertEqual(directives['nc'], '00000001')
59         self.assertEqual(directives['qop'], ['auth'])
60         self.assertEqual(directives['charset'], 'utf-8')
61         self.assertEqual(directives['digest-uri'], 'xmpp/example.org')
62         self.assertEqual(directives['realm'], 'localhost')
63
64     def test_getResponseNoRealm(self):
65         """
66         Test that we accept challenges without realm.
67
68         The realm should default to the host part of the JID.
69         """
70
71         challenge = 'nonce="1234",qop="auth",charset=utf-8,algorithm=md5-sess'
72         directives = self.mechanism._parse(self.mechanism.getResponse(challenge))
73         self.assertEqual(directives['realm'], 'example.org')
74
75     def test__parse(self):
76         """
77         Test challenge decoding.
78
79         Specifically, check for multiple values for the C{qop} and C{cipher}
80         directives.
81         """
82         challenge = 'nonce="1234",qop="auth,auth-conf",charset=utf-8,' \
83                     'algorithm=md5-sess,cipher="des,3des"'
84         directives = self.mechanism._parse(challenge)
85         self.assertEqual('1234', directives['nonce'])
86         self.assertEqual('utf-8', directives['charset'])
87         self.assertIn('auth', directives['qop'])
88         self.assertIn('auth-conf', directives['qop'])
89         self.assertIn('des', directives['cipher'])
90         self.assertIn('3des', directives['cipher'])