/usr/share/pyshared/wokkel/test/test_component.py is in python-wokkel 0.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | # Copyright (c) Ralph Meijer.
# See LICENSE for details.
"""
Tests for L{wokkel.component}.
"""
from zope.interface.verify import verifyObject
from twisted.internet.base import BaseConnector
from twisted.internet.error import ConnectionRefusedError
from twisted.internet.task import Clock
from twisted.python import failure
from twisted.trial import unittest
from twisted.words.protocols.jabber import xmlstream
from twisted.words.protocols.jabber.ijabber import IXMPPHandlerCollection
from twisted.words.protocols.jabber.jid import JID
from twisted.words.protocols.jabber.xmlstream import XMPPHandler
from twisted.words.xish import domish
from wokkel import component
from wokkel.generic import XmlPipe
class FakeConnector(BaseConnector):
"""
Fake connector that counts connection attempts.
"""
connects = 0
def connect(self):
self.connects += 1
BaseConnector.connect(self)
def _makeTransport(self):
return None
class TestableComponent(component.Component):
"""
Testable component.
This component provides the created factory with a L{Clock}
instead of the regular reactor and uses L{FakeConnector} for testing
connects and reconnects.
"""
def __init__(self, *args, **kwargs):
component.Component.__init__(self, *args, **kwargs)
self.factory.clock = Clock()
def _getConnection(self):
c = FakeConnector(self.factory, None, None)
c.connect()
return c
class ComponentTest(unittest.TestCase):
"""
Tests for L{component.Component}.
"""
def test_startServiceReconnectAfterFailure(self):
"""
When the first connection attempt fails, retry.
"""
comp = TestableComponent('example.org', 5347,
'test.example.org', 'secret')
# Starting the service initiates a connection attempt.
comp.startService()
connector = comp._connection
self.assertEqual(1, connector.connects)
# Fail the connection.
connector.connectionFailed(ConnectionRefusedError())
# After a back-off delay, a new connection is attempted.
comp.factory.clock.advance(5)
self.assertEqual(2, connector.connects)
def test_stopServiceNoReconnect(self):
"""
When the service is stopped, no reconnect is attempted.
"""
comp = TestableComponent('example.org', 5347,
'test.example.org', 'secret')
# Starting the service initiates a connection attempt.
comp.startService()
connector = comp._connection
# Fail the connection.
connector.connectionFailed(ConnectionRefusedError())
# If the service is stopped before the back-off delay expires,
# no new connection is attempted.
comp.factory.clock.advance(1)
comp.stopService()
comp.factory.clock.advance(4)
self.assertEqual(1, connector.connects)
class InternalComponentTest(unittest.TestCase):
"""
Tests for L{component.InternalComponent}.
"""
def setUp(self):
self.router = component.Router()
self.component = component.InternalComponent(self.router, 'component')
def test_interface(self):
"""
L{component.InternalComponent} implements
L{IXMPPHandlerCollection}.
"""
verifyObject(IXMPPHandlerCollection, self.component)
def test_startServiceRunning(self):
"""
Starting the service makes it running.
"""
self.assertFalse(self.component.running)
self.component.startService()
self.assertTrue(self.component.running)
def test_startServiceAddRoute(self):
"""
Starting the service creates a new route.
"""
self.component.startService()
self.assertIn('component', self.router.routes)
def test_startServiceNoDomain(self):
self.component = component.InternalComponent(self.router)
self.component.startService()
def test_startServiceAddMultipleRoutes(self):
"""
Starting the service creates a new route.
"""
self.component.domains.add('component2')
self.component.startService()
self.assertIn('component', self.router.routes)
self.assertIn('component2', self.router.routes)
def test_startServiceHandlerDispatch(self):
"""
Starting the service hooks up handlers.
"""
events = []
class TestHandler(XMPPHandler):
def connectionInitialized(self):
fn = lambda obj: events.append(obj)
self.xmlstream.addObserver('//event/test', fn)
TestHandler().setHandlerParent(self.component)
self.component.startService()
self.assertEquals([], events)
self.component.xmlstream.dispatch(None, '//event/test')
self.assertEquals([None], events)
def test_stopServiceNotRunning(self):
"""
Stopping the service makes it not running.
"""
self.component.startService()
self.component.stopService()
self.assertFalse(self.component.running)
def test_stopServiceRemoveRoute(self):
"""
Stopping the service removes routes.
"""
self.component.startService()
self.component.stopService()
self.assertNotIn('component', self.router.routes)
def test_stopServiceNoDomain(self):
self.component = component.InternalComponent(self.router)
self.component.startService()
self.component.stopService()
def test_startServiceRemoveMultipleRoutes(self):
"""
Starting the service creates a new route.
"""
self.component.domains.add('component2')
self.component.startService()
self.component.stopService()
self.assertNotIn('component', self.router.routes)
self.assertNotIn('component2', self.router.routes)
def test_stopServiceHandlerDispatch(self):
"""
Stopping the service disconnects handlers.
"""
events = []
class TestHandler(XMPPHandler):
def connectionLost(self, reason):
events.append(reason)
TestHandler().setHandlerParent(self.component)
self.component.startService()
self.component.stopService()
self.assertEquals(1, len(events))
def test_addHandler(self):
"""
Adding a handler connects it to the stream.
"""
events = []
class TestHandler(XMPPHandler):
def connectionInitialized(self):
fn = lambda obj: events.append(obj)
self.xmlstream.addObserver('//event/test', fn)
self.component.startService()
self.component.xmlstream.dispatch(None, '//event/test')
self.assertEquals([], events)
TestHandler().setHandlerParent(self.component)
self.component.xmlstream.dispatch(None, '//event/test')
self.assertEquals([None], events)
def test_send(self):
"""
A message sent from the component ends up at the router.
"""
events = []
fn = lambda obj: events.append(obj)
message = domish.Element((None, 'message'))
self.router.route = fn
self.component.startService()
self.component.send(message)
self.assertEquals([message], events)
class RouterTest(unittest.TestCase):
"""
Tests for L{component.Router}.
"""
def test_addRoute(self):
"""
Test route registration and routing on incoming stanzas.
"""
router = component.Router()
routed = []
router.route = lambda element: routed.append(element)
pipe = XmlPipe()
router.addRoute('example.org', pipe.sink)
self.assertEquals(1, len(router.routes))
self.assertEquals(pipe.sink, router.routes['example.org'])
element = domish.Element(('testns', 'test'))
pipe.source.send(element)
self.assertEquals([element], routed)
def test_route(self):
"""
Test routing of a message.
"""
component1 = XmlPipe()
component2 = XmlPipe()
router = component.Router()
router.addRoute('component1.example.org', component1.sink)
router.addRoute('component2.example.org', component2.sink)
outgoing = []
component2.source.addObserver('/*',
lambda element: outgoing.append(element))
stanza = domish.Element((None, 'presence'))
stanza['from'] = 'component1.example.org'
stanza['to'] = 'component2.example.org'
component1.source.send(stanza)
self.assertEquals([stanza], outgoing)
def test_routeDefault(self):
"""
Test routing of a message using the default route.
The default route is the one with C{None} as its key in the
routing table. It is taken when there is no more specific route
in the routing table that matches the stanza's destination.
"""
component1 = XmlPipe()
s2s = XmlPipe()
router = component.Router()
router.addRoute('component1.example.org', component1.sink)
router.addRoute(None, s2s.sink)
outgoing = []
s2s.source.addObserver('/*', lambda element: outgoing.append(element))
stanza = domish.Element((None, 'presence'))
stanza['from'] = 'component1.example.org'
stanza['to'] = 'example.com'
component1.source.send(stanza)
self.assertEquals([stanza], outgoing)
class ListenComponentAuthenticatorTest(unittest.TestCase):
"""
Tests for L{component.ListenComponentAuthenticator}.
"""
def setUp(self):
self.output = []
authenticator = component.ListenComponentAuthenticator('secret')
self.xmlstream = xmlstream.XmlStream(authenticator)
self.xmlstream.send = self.output.append
def loseConnection(self):
"""
Stub loseConnection because we are a transport.
"""
self.xmlstream.connectionLost("no reason")
def test_streamStarted(self):
"""
The received stream header should set several attributes.
"""
observers = []
def addOnetimeObserver(event, observerfn):
observers.append((event, observerfn))
xs = self.xmlstream
xs.addOnetimeObserver = addOnetimeObserver
xs.makeConnection(self)
self.assertIdentical(None, xs.sid)
self.assertFalse(xs._headerSent)
xs.dataReceived("<stream:stream xmlns='jabber:component:accept' "
"xmlns:stream='http://etherx.jabber.org/streams' "
"to='component.example.org'>")
self.assertEqual((0, 0), xs.version)
self.assertNotIdentical(None, xs.sid)
self.assertTrue(xs._headerSent)
self.assertEquals(('/*', xs.authenticator.onElement), observers[-1])
def test_streamStartedWrongNamespace(self):
"""
The received stream header should have a correct namespace.
"""
streamErrors = []
xs = self.xmlstream
xs.sendStreamError = streamErrors.append
xs.makeConnection(self)
xs.dataReceived("<stream:stream xmlns='jabber:client' "
"xmlns:stream='http://etherx.jabber.org/streams' "
"to='component.example.org'>")
self.assertEquals(1, len(streamErrors))
self.assertEquals('invalid-namespace', streamErrors[-1].condition)
def test_streamStartedNoTo(self):
"""
The received stream header should have a 'to' attribute.
"""
streamErrors = []
xs = self.xmlstream
xs.sendStreamError = streamErrors.append
xs.makeConnection(self)
xs.dataReceived("<stream:stream xmlns='jabber:component:accept' "
"xmlns:stream='http://etherx.jabber.org/streams'>")
self.assertEquals(1, len(streamErrors))
self.assertEquals('improper-addressing', streamErrors[-1].condition)
def test_onElement(self):
"""
We expect a handshake element with a hash.
"""
handshakes = []
xs = self.xmlstream
xs.authenticator.onHandshake = handshakes.append
handshake = domish.Element(('jabber:component:accept', 'handshake'))
handshake.addContent('1234')
xs.authenticator.onElement(handshake)
self.assertEqual('1234', handshakes[-1])
def test_onElementNotHandshake(self):
"""
Reject elements that are not handshakes
"""
handshakes = []
streamErrors = []
xs = self.xmlstream
xs.authenticator.onHandshake = handshakes.append
xs.sendStreamError = streamErrors.append
element = domish.Element(('jabber:component:accept', 'message'))
xs.authenticator.onElement(element)
self.assertFalse(handshakes)
self.assertEquals('not-authorized', streamErrors[-1].condition)
def test_onHandshake(self):
"""
Receiving a handshake matching the secret authenticates the stream.
"""
authd = []
def authenticated(xs):
authd.append(xs)
xs = self.xmlstream
xs.addOnetimeObserver(xmlstream.STREAM_AUTHD_EVENT, authenticated)
xs.sid = u'1234'
theHash = '32532c0f7dbf1253c095b18b18e36d38d94c1256'
xs.authenticator.onHandshake(theHash)
self.assertEqual('<handshake/>', self.output[-1])
self.assertEquals(1, len(authd))
def test_onHandshakeWrongHash(self):
"""
Receiving a bad handshake should yield a stream error.
"""
streamErrors = []
authd = []
def authenticated(xs):
authd.append(xs)
xs = self.xmlstream
xs.addOnetimeObserver(xmlstream.STREAM_AUTHD_EVENT, authenticated)
xs.sendStreamError = streamErrors.append
xs.sid = u'1234'
theHash = '1234'
xs.authenticator.onHandshake(theHash)
self.assertEquals('not-authorized', streamErrors[-1].condition)
self.assertEquals(0, len(authd))
class XMPPComponentServerFactoryTest(unittest.TestCase):
"""
Tests for L{component.XMPPComponentServerFactory}.
"""
def setUp(self):
self.router = component.Router()
self.factory = component.XMPPComponentServerFactory(self.router,
'secret')
self.xmlstream = self.factory.buildProtocol(None)
self.xmlstream.thisEntity = JID('component.example.org')
def test_makeConnection(self):
"""
A new connection increases the stream serial count. No logs by default.
"""
self.xmlstream.dispatch(self.xmlstream,
xmlstream.STREAM_CONNECTED_EVENT)
self.assertEqual(0, self.xmlstream.serial)
self.assertEqual(1, self.factory.serial)
self.assertIdentical(None, self.xmlstream.rawDataInFn)
self.assertIdentical(None, self.xmlstream.rawDataOutFn)
def test_makeConnectionLogTraffic(self):
"""
Setting logTraffic should set up raw data loggers.
"""
self.factory.logTraffic = True
self.xmlstream.dispatch(self.xmlstream,
xmlstream.STREAM_CONNECTED_EVENT)
self.assertNotIdentical(None, self.xmlstream.rawDataInFn)
self.assertNotIdentical(None, self.xmlstream.rawDataOutFn)
def test_onError(self):
"""
An observer for stream errors should trigger onError to log it.
"""
self.xmlstream.dispatch(self.xmlstream,
xmlstream.STREAM_CONNECTED_EVENT)
class TestError(Exception):
pass
reason = failure.Failure(TestError())
self.xmlstream.dispatch(reason, xmlstream.STREAM_ERROR_EVENT)
self.assertEqual(1, len(self.flushLoggedErrors(TestError)))
def test_connectionInitialized(self):
"""
Make sure a new stream is added to the routing table.
"""
self.xmlstream.dispatch(self.xmlstream, xmlstream.STREAM_AUTHD_EVENT)
self.assertIn('component.example.org', self.router.routes)
self.assertIdentical(self.xmlstream,
self.router.routes['component.example.org'])
def test_connectionLost(self):
"""
Make sure a stream is removed from the routing table on disconnect.
"""
self.xmlstream.dispatch(self.xmlstream, xmlstream.STREAM_AUTHD_EVENT)
self.xmlstream.dispatch(None, xmlstream.STREAM_END_EVENT)
self.assertNotIn('component.example.org', self.router.routes)
|