1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Tests for L{twisted.words.protocols.jabber.sasl_mechanisms}.
8 from twisted.trial import unittest
10 from twisted.words.protocols.jabber import sasl_mechanisms
12 class PlainTest(unittest.TestCase):
13 def test_getInitialResponse(self):
15 Test the initial response.
17 m = sasl_mechanisms.Plain(None, 'test', 'secret')
18 self.assertEqual(m.getInitialResponse(), '\x00test\x00secret')
22 class AnonymousTest(unittest.TestCase):
24 Tests for L{twisted.words.protocols.jabber.sasl_mechanisms.Anonymous}.
26 def test_getInitialResponse(self):
28 Test the initial response to be empty.
30 m = sasl_mechanisms.Anonymous()
31 self.assertEqual(m.getInitialResponse(), None)
35 class DigestMD5Test(unittest.TestCase):
37 self.mechanism = sasl_mechanisms.DigestMD5('xmpp', 'example.org', None,
41 def test_getInitialResponse(self):
43 Test that no initial response is generated.
45 self.assertIdentical(self.mechanism.getInitialResponse(), None)
47 def test_getResponse(self):
49 Partially test challenge response.
51 Does not actually test the response-value, yet.
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')
64 def test_getResponseNoRealm(self):
66 Test that we accept challenges without realm.
68 The realm should default to the host part of the JID.
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')
75 def test__parse(self):
77 Test challenge decoding.
79 Specifically, check for multiple values for the C{qop} and C{cipher}
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'])