test_jabbercomponent.py [plain text]
import sys, os, sha
from twisted.trial import unittest
from twisted.protocols.jabber.component import ConnectComponentAuthenticator
from twisted.protocols import jabber
from twisted.protocols import xmlstream
class ComponentAuthTest(unittest.TestCase):
def authPassed(self, stream):
self.authComplete = True
def testAuth(self):
self.authComplete = False
outlist = []
ca = ConnectComponentAuthenticator("cjid", "secret")
xs = xmlstream.XmlStream(ca)
xs.addObserver(xmlstream.STREAM_AUTHD_EVENT,
self.authPassed)
xs.send = outlist.append
xs.connectionMade()
xs.dataReceived("<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' from='cjid' id='12345'>")
hv = sha.new("%s%s" % ("12345", "secret")).hexdigest()
self.assertEquals(outlist[1].toXml(),
"<handshake>%s</handshake>" % (hv))
xs.dataReceived("<handshake/>")
self.assertEquals(self.authComplete, True)
class JabberServiceHarness(jabber.component.Service):
def __init__(self):
self.componentConnectedFlag = False
self.componentDisconnectedFlag = False
self.transportConnectedFlag = False
def componentConnected(self, xmlstream):
self.componentConnectedFlag = True
def componentDisconnected(self):
self.componentDisconnectedFlag = True
def transportConnected(self, xmlstream):
self.transportConnectedFlag = True
class TestJabberServiceManager(unittest.TestCase):
def testSM(self):
sm = jabber.component.ServiceManager("foo", "password")
svc = JabberServiceHarness()
svc.setServiceParent(sm)
wlist = []
xs = sm.getFactory().buildProtocol(None)
xs.transport = self
xs.transport.write = wlist.append
xs.connectionMade()
self.assertEquals(True, svc.transportConnectedFlag)
xs.dispatch(xs, xmlstream.STREAM_AUTHD_EVENT)
self.assertEquals(True, svc.componentConnectedFlag)
xs.connectionLost(None)
self.assertEquals(True, svc.componentDisconnectedFlag)