OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2007 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 """ | |
5 Tests for L{twisted.words.protocols.jabber.component} | |
6 """ | |
7 | |
8 import sha | |
9 from twisted.trial import unittest | |
10 | |
11 from twisted.words.protocols.jabber import component | |
12 from twisted.words.protocols import jabber | |
13 from twisted.words.protocols.jabber import xmlstream | |
14 | |
15 class DummyTransport: | |
16 def __init__(self, list): | |
17 self.list = list | |
18 | |
19 def write(self, bytes): | |
20 self.list.append(bytes) | |
21 | |
22 class ComponentInitiatingInitializerTest(unittest.TestCase): | |
23 def setUp(self): | |
24 self.output = [] | |
25 | |
26 self.authenticator = xmlstream.Authenticator() | |
27 self.authenticator.password = 'secret' | |
28 self.xmlstream = xmlstream.XmlStream(self.authenticator) | |
29 self.xmlstream.namespace = 'test:component' | |
30 self.xmlstream.send = self.output.append | |
31 self.xmlstream.connectionMade() | |
32 self.xmlstream.dataReceived( | |
33 "<stream:stream xmlns='test:component' " | |
34 "xmlns:stream='http://etherx.jabber.org/streams' " | |
35 "from='example.com' id='12345' version='1.0'>") | |
36 self.xmlstream.sid = '12345' | |
37 self.init = component.ComponentInitiatingInitializer(self.xmlstream) | |
38 | |
39 def testHandshake(self): | |
40 """ | |
41 Test basic operations of component handshake. | |
42 """ | |
43 | |
44 d = self.init.initialize() | |
45 | |
46 # the initializer should have sent the handshake request | |
47 | |
48 handshake = self.output[-1] | |
49 self.assertEquals('handshake', handshake.name) | |
50 self.assertEquals('test:component', handshake.uri) | |
51 self.assertEquals(sha.new("%s%s" % ('12345', 'secret')).hexdigest(), | |
52 unicode(handshake)) | |
53 | |
54 # successful authentication | |
55 | |
56 handshake.children = [] | |
57 self.xmlstream.dataReceived(handshake.toXml()) | |
58 | |
59 return d | |
60 | |
61 class ComponentAuthTest(unittest.TestCase): | |
62 def authPassed(self, stream): | |
63 self.authComplete = True | |
64 | |
65 def testAuth(self): | |
66 self.authComplete = False | |
67 outlist = [] | |
68 | |
69 ca = component.ConnectComponentAuthenticator("cjid", "secret") | |
70 xs = xmlstream.XmlStream(ca) | |
71 xs.transport = DummyTransport(outlist) | |
72 | |
73 xs.addObserver(xmlstream.STREAM_AUTHD_EVENT, | |
74 self.authPassed) | |
75 | |
76 # Go... | |
77 xs.connectionMade() | |
78 xs.dataReceived("<stream:stream xmlns='jabber:component:accept' xmlns:st
ream='http://etherx.jabber.org/streams' from='cjid' id='12345'>") | |
79 | |
80 # Calculate what we expect the handshake value to be | |
81 hv = sha.new("%s%s" % ("12345", "secret")).hexdigest() | |
82 | |
83 self.assertEquals(outlist[1], "<handshake>%s</handshake>" % (hv)) | |
84 | |
85 xs.dataReceived("<handshake/>") | |
86 | |
87 self.assertEquals(self.authComplete, True) | |
88 | |
89 | |
90 class JabberServiceHarness(jabber.component.Service): | |
91 def __init__(self): | |
92 self.componentConnectedFlag = False | |
93 self.componentDisconnectedFlag = False | |
94 self.transportConnectedFlag = False | |
95 | |
96 def componentConnected(self, xmlstream): | |
97 self.componentConnectedFlag = True | |
98 | |
99 def componentDisconnected(self): | |
100 self.componentDisconnectedFlag = True | |
101 | |
102 def transportConnected(self, xmlstream): | |
103 self.transportConnectedFlag = True | |
104 | |
105 | |
106 class TestJabberServiceManager(unittest.TestCase): | |
107 def testSM(self): | |
108 # Setup service manager and test harnes | |
109 sm = jabber.component.ServiceManager("foo", "password") | |
110 svc = JabberServiceHarness() | |
111 svc.setServiceParent(sm) | |
112 | |
113 # Create a write list | |
114 wlist = [] | |
115 | |
116 # Setup a XmlStream | |
117 xs = sm.getFactory().buildProtocol(None) | |
118 xs.transport = self | |
119 xs.transport.write = wlist.append | |
120 | |
121 # Indicate that it's connected | |
122 xs.connectionMade() | |
123 | |
124 # Ensure the test service harness got notified | |
125 self.assertEquals(True, svc.transportConnectedFlag) | |
126 | |
127 # Jump ahead and pretend like the stream got auth'd | |
128 xs.dispatch(xs, xmlstream.STREAM_AUTHD_EVENT) | |
129 | |
130 # Ensure the test service harness got notified | |
131 self.assertEquals(True, svc.componentConnectedFlag) | |
132 | |
133 # Pretend to drop the connection | |
134 xs.connectionLost(None) | |
135 | |
136 # Ensure the test service harness got notified | |
137 self.assertEquals(True, svc.componentDisconnectedFlag) | |
OLD | NEW |