1
# Copyright (c) 2001-2005 Twisted Matrix Laboratories.
2
# See LICENSE for details.
6
from twisted.trial import unittest
8
from twisted.words.protocols.jabber.component import ConnectComponentAuthenticator, ComponentInitiatingInitializer
9
from twisted.words.protocols import jabber
10
from twisted.words.protocols.jabber import xmlstream
13
def __init__(self, list):
16
def write(self, bytes):
17
self.list.append(bytes)
19
class ComponentInitiatingInitializerTest(unittest.TestCase):
23
self.authenticator = xmlstream.Authenticator()
24
self.authenticator.password = 'secret'
25
self.xmlstream = xmlstream.XmlStream(self.authenticator)
26
self.xmlstream.namespace = 'test:component'
27
self.xmlstream.send = self.output.append
28
self.xmlstream.connectionMade()
29
self.xmlstream.dataReceived(
30
"<stream:stream xmlns='test:component' "
31
"xmlns:stream='http://etherx.jabber.org/streams' "
32
"from='example.com' id='12345' version='1.0'>")
33
self.init = ComponentInitiatingInitializer(self.xmlstream)
35
def testHandshake(self):
37
Test basic operations of component handshake.
40
d = self.init.initialize()
42
# the initializer should have sent the handshake request
44
handshake = self.output[0]
45
self.assertEquals('handshake', handshake.name)
46
self.assertEquals('test:component', handshake.uri)
47
self.assertEquals(sha.new("%s%s" % ('12345', 'secret')).hexdigest(),
50
# successful authentication
52
handshake.children = []
53
self.xmlstream.dataReceived(handshake.toXml())
57
class ComponentAuthTest(unittest.TestCase):
58
def authPassed(self, stream):
59
self.authComplete = True
62
self.authComplete = False
65
ca = ConnectComponentAuthenticator("cjid", "secret")
66
xs = xmlstream.XmlStream(ca)
67
xs.transport = DummyTransport(outlist)
69
xs.addObserver(xmlstream.STREAM_AUTHD_EVENT,
74
xs.dataReceived("<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' from='cjid' id='12345'>")
76
# Calculate what we expect the handshake value to be
77
hv = sha.new("%s%s" % ("12345", "secret")).hexdigest()
79
self.assertEquals(outlist[1], "<handshake>%s</handshake>" % (hv))
81
xs.dataReceived("<handshake/>")
83
self.assertEquals(self.authComplete, True)
86
class JabberServiceHarness(jabber.component.Service):
88
self.componentConnectedFlag = False
89
self.componentDisconnectedFlag = False
90
self.transportConnectedFlag = False
92
def componentConnected(self, xmlstream):
93
self.componentConnectedFlag = True
95
def componentDisconnected(self):
96
self.componentDisconnectedFlag = True
98
def transportConnected(self, xmlstream):
99
self.transportConnectedFlag = True
102
class TestJabberServiceManager(unittest.TestCase):
104
# Setup service manager and test harnes
105
sm = jabber.component.ServiceManager("foo", "password")
106
svc = JabberServiceHarness()
107
svc.setServiceParent(sm)
109
# Create a write list
113
xs = sm.getFactory().buildProtocol(None)
115
xs.transport.write = wlist.append
117
# Indicate that it's connected
120
# Ensure the test service harness got notified
121
self.assertEquals(True, svc.transportConnectedFlag)
123
# Jump ahead and pretend like the stream got auth'd
124
xs.dispatch(xs, xmlstream.STREAM_AUTHD_EVENT)
126
# Ensure the test service harness got notified
127
self.assertEquals(True, svc.componentConnectedFlag)
129
# Pretend to drop the connection
130
xs.connectionLost(None)
132
# Ensure the test service harness got notified
133
self.assertEquals(True, svc.componentDisconnectedFlag)