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