| 1 |
# Copyright (c) Ralph Meijer. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
""" |
| 5 |
Tests for L{tx_xmpp.server}. |
| 6 |
""" |
| 7 |
|
| 8 |
from twisted.internet import defer |
| 9 |
from twisted.python import failure |
| 10 |
|
| 11 |
try: |
| 12 |
from twisted.internet.testing import StringTransport |
| 13 |
except ImportError: |
| 14 |
from twisted.test.proto_helpers import StringTransport |
| 15 |
|
| 16 |
from twisted.trial import unittest |
| 17 |
from twisted.words.protocols.jabber import error, jid, xmlstream |
| 18 |
from twisted.words.xish import domish |
| 19 |
|
| 20 |
from .. import component, server |
| 21 |
|
| 22 |
NS_STREAMS = "http://etherx.jabber.org/streams" |
| 23 |
NS_DIALBACK = "jabber:server:dialback" |
| 24 |
|
| 25 |
|
| 26 |
class GenerateKeyTest(unittest.TestCase): |
| 27 |
""" |
| 28 |
Tests for L{server.generateKey}. |
| 29 |
""" |
| 30 |
|
| 31 |
def testBasic(self): |
| 32 |
originating = "example.org" |
| 33 |
receiving = "xmpp.example.com" |
| 34 |
sid = "D60000229F" |
| 35 |
secret = "s3cr3tf0rd14lb4ck" |
| 36 |
|
| 37 |
key = server.generateKey(secret, receiving, originating, sid) |
| 38 |
|
| 39 |
self.assertEqual( |
| 40 |
key, "37c69b1cf07a3f67c04a5ef5902fa5114f2c76fe4a2686482ba5b89323075643" |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
class XMPPServerListenAuthenticatorTest(unittest.TestCase): |
| 45 |
""" |
| 46 |
Tests for L{server.XMPPServerListenAuthenticator}. |
| 47 |
""" |
| 48 |
|
| 49 |
secret = "s3cr3tf0rd14lb4ck" |
| 50 |
originating = "example.org" |
| 51 |
receiving = "xmpp.example.com" |
| 52 |
sid = "D60000229F" |
| 53 |
key = "37c69b1cf07a3f67c04a5ef5902fa5114f2c76fe4a2686482ba5b89323075643" |
| 54 |
|
| 55 |
def setUp(self): |
| 56 |
self.output = [] |
| 57 |
|
| 58 |
class MyService: |
| 59 |
pass |
| 60 |
|
| 61 |
self.service = MyService() |
| 62 |
self.service.defaultDomain = self.receiving |
| 63 |
self.service.domains = [self.receiving, "pubsub." + self.receiving] |
| 64 |
self.service.secret = self.secret |
| 65 |
|
| 66 |
self.authenticator = server.XMPPServerListenAuthenticator(self.service) |
| 67 |
self.xmlstream = xmlstream.XmlStream(self.authenticator) |
| 68 |
self.xmlstream.send = self.output.append |
| 69 |
self.xmlstream.transport = StringTransport() |
| 70 |
|
| 71 |
def test_attributes(self): |
| 72 |
""" |
| 73 |
Test attributes of authenticator and stream objects. |
| 74 |
""" |
| 75 |
self.assertEqual(self.service, self.authenticator.service) |
| 76 |
self.assertEqual(self.xmlstream.initiating, False) |
| 77 |
|
| 78 |
def test_streamStartedVersion0(self): |
| 79 |
""" |
| 80 |
The authenticator supports pre-XMPP 1.0 streams. |
| 81 |
""" |
| 82 |
self.xmlstream.connectionMade() |
| 83 |
self.xmlstream.dataReceived( |
| 84 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 85 |
"xmlns:db='jabber:server:dialback' " |
| 86 |
"xmlns='jabber:server' " |
| 87 |
"to='xmpp.example.com'>" |
| 88 |
) |
| 89 |
self.assertEqual((0, 0), self.xmlstream.version) |
| 90 |
|
| 91 |
def test_streamStartedVersion1(self): |
| 92 |
""" |
| 93 |
The authenticator supports XMPP 1.0 streams. |
| 94 |
""" |
| 95 |
self.xmlstream.connectionMade() |
| 96 |
self.xmlstream.dataReceived( |
| 97 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 98 |
"xmlns:db='jabber:server:dialback' " |
| 99 |
"xmlns='jabber:server' " |
| 100 |
"to='xmpp.example.com' " |
| 101 |
"version='1.0'>" |
| 102 |
) |
| 103 |
self.assertEqual((1, 0), self.xmlstream.version) |
| 104 |
|
| 105 |
def test_streamStartedSID(self): |
| 106 |
""" |
| 107 |
The response stream will have a stream ID. |
| 108 |
""" |
| 109 |
self.xmlstream.connectionMade() |
| 110 |
self.assertIdentical(None, self.xmlstream.sid) |
| 111 |
|
| 112 |
self.xmlstream.dataReceived( |
| 113 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 114 |
"xmlns:db='jabber:server:dialback' " |
| 115 |
"xmlns='jabber:server' " |
| 116 |
"to='xmpp.example.com' " |
| 117 |
"version='1.0'>" |
| 118 |
) |
| 119 |
self.assertNotIdentical(None, self.xmlstream.sid) |
| 120 |
|
| 121 |
def test_streamStartedSentResponseHeader(self): |
| 122 |
""" |
| 123 |
A stream header is sent in response to the incoming stream header. |
| 124 |
""" |
| 125 |
self.xmlstream.connectionMade() |
| 126 |
self.assertFalse(self.xmlstream._headerSent) |
| 127 |
|
| 128 |
self.xmlstream.dataReceived( |
| 129 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 130 |
"xmlns:db='jabber:server:dialback' " |
| 131 |
"xmlns='jabber:server' " |
| 132 |
"to='xmpp.example.com'>" |
| 133 |
) |
| 134 |
self.assertTrue(self.xmlstream._headerSent) |
| 135 |
|
| 136 |
def test_streamStartedNotSentFeatures(self): |
| 137 |
""" |
| 138 |
No features are sent in response to an XMPP < 1.0 stream header. |
| 139 |
""" |
| 140 |
self.xmlstream.connectionMade() |
| 141 |
self.xmlstream.dataReceived( |
| 142 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 143 |
"xmlns:db='jabber:server:dialback' " |
| 144 |
"xmlns='jabber:server' " |
| 145 |
"to='xmpp.example.com'>" |
| 146 |
) |
| 147 |
self.assertEqual(1, len(self.output)) |
| 148 |
|
| 149 |
def test_streamStartedSentFeatures(self): |
| 150 |
""" |
| 151 |
Features are sent in response to an XMPP >= 1.0 stream header. |
| 152 |
""" |
| 153 |
self.xmlstream.connectionMade() |
| 154 |
self.xmlstream.dataReceived( |
| 155 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 156 |
"xmlns:db='jabber:server:dialback' " |
| 157 |
"xmlns='jabber:server' " |
| 158 |
"to='xmpp.example.com' " |
| 159 |
"version='1.0'>" |
| 160 |
) |
| 161 |
self.assertEqual(2, len(self.output)) |
| 162 |
features = self.output[-1] |
| 163 |
self.assertEqual(NS_STREAMS, features.uri) |
| 164 |
self.assertEqual("features", features.name) |
| 165 |
|
| 166 |
def test_streamRootElement(self): |
| 167 |
""" |
| 168 |
Test stream error on wrong stream namespace. |
| 169 |
""" |
| 170 |
self.xmlstream.connectionMade() |
| 171 |
self.xmlstream.dataReceived( |
| 172 |
"<stream:stream xmlns:stream='badns' " |
| 173 |
"xmlns:db='jabber:server:dialback' " |
| 174 |
"xmlns='jabber:server' " |
| 175 |
"to='xmpp.example.com'>" |
| 176 |
) |
| 177 |
|
| 178 |
self.assertEqual(3, len(self.output)) |
| 179 |
exc = error.exceptionFromStreamError(self.output[1]) |
| 180 |
self.assertEqual("invalid-namespace", exc.condition) |
| 181 |
|
| 182 |
def test_streamDefaultNamespace(self): |
| 183 |
""" |
| 184 |
Test stream error on missing dialback namespace. |
| 185 |
""" |
| 186 |
self.xmlstream.connectionMade() |
| 187 |
self.xmlstream.dataReceived( |
| 188 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 189 |
"xmlns:db='jabber:server:dialback' " |
| 190 |
"xmlns='badns' " |
| 191 |
"to='xmpp.example.com'>" |
| 192 |
) |
| 193 |
|
| 194 |
self.assertEqual(3, len(self.output)) |
| 195 |
exc = error.exceptionFromStreamError(self.output[1]) |
| 196 |
self.assertEqual("invalid-namespace", exc.condition) |
| 197 |
|
| 198 |
def test_streamNoDialbackNamespace(self): |
| 199 |
""" |
| 200 |
Test stream error on missing dialback namespace. |
| 201 |
""" |
| 202 |
self.xmlstream.connectionMade() |
| 203 |
self.xmlstream.dataReceived( |
| 204 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 205 |
"xmlns='jabber:server' " |
| 206 |
"to='xmpp.example.com'>" |
| 207 |
) |
| 208 |
|
| 209 |
self.assertEqual(3, len(self.output)) |
| 210 |
exc = error.exceptionFromStreamError(self.output[1]) |
| 211 |
self.assertEqual("invalid-namespace", exc.condition) |
| 212 |
|
| 213 |
def test_streamBadDialbackNamespace(self): |
| 214 |
""" |
| 215 |
Test stream error on missing dialback namespace. |
| 216 |
""" |
| 217 |
self.xmlstream.connectionMade() |
| 218 |
self.xmlstream.dataReceived( |
| 219 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 220 |
"xmlns:db='badns' " |
| 221 |
"xmlns='jabber:server' " |
| 222 |
"to='xmpp.example.com'>" |
| 223 |
) |
| 224 |
|
| 225 |
self.assertEqual(3, len(self.output)) |
| 226 |
exc = error.exceptionFromStreamError(self.output[1]) |
| 227 |
self.assertEqual("invalid-namespace", exc.condition) |
| 228 |
|
| 229 |
def test_streamToUnknownHost(self): |
| 230 |
""" |
| 231 |
Test stream error on stream's to attribute having unknown host. |
| 232 |
""" |
| 233 |
self.xmlstream.connectionMade() |
| 234 |
self.xmlstream.dataReceived( |
| 235 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 236 |
"xmlns:db='jabber:server:dialback' " |
| 237 |
"xmlns='jabber:server' " |
| 238 |
"to='badhost'>" |
| 239 |
) |
| 240 |
|
| 241 |
self.assertEqual(3, len(self.output)) |
| 242 |
exc = error.exceptionFromStreamError(self.output[1]) |
| 243 |
self.assertEqual("host-unknown", exc.condition) |
| 244 |
|
| 245 |
def test_streamToOtherLocalHost(self): |
| 246 |
""" |
| 247 |
The authenticator supports XMPP 1.0 streams. |
| 248 |
""" |
| 249 |
self.xmlstream.connectionMade() |
| 250 |
self.xmlstream.dataReceived( |
| 251 |
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' " |
| 252 |
"xmlns:db='jabber:server:dialback' " |
| 253 |
"xmlns='jabber:server' " |
| 254 |
"to='pubsub.xmpp.example.com' " |
| 255 |
"version='1.0'>" |
| 256 |
) |
| 257 |
|
| 258 |
self.assertEqual(2, len(self.output)) |
| 259 |
self.assertEqual(jid.JID("pubsub.xmpp.example.com"), self.xmlstream.thisEntity) |
| 260 |
|
| 261 |
def test_onResult(self): |
| 262 |
def cb(result): |
| 263 |
self.assertEqual(1, len(self.output)) |
| 264 |
reply = self.output[0] |
| 265 |
self.assertEqual(self.originating, reply["to"]) |
| 266 |
self.assertEqual(self.receiving, reply["from"]) |
| 267 |
self.assertEqual("valid", reply["type"]) |
| 268 |
|
| 269 |
def validateConnection(thisHost, otherHost, sid, key): |
| 270 |
self.assertEqual(thisHost, self.receiving) |
| 271 |
self.assertEqual(otherHost, self.originating) |
| 272 |
self.assertEqual(sid, self.sid) |
| 273 |
self.assertEqual(key, self.key) |
| 274 |
return defer.succeed(None) |
| 275 |
|
| 276 |
self.xmlstream.sid = self.sid |
| 277 |
self.service.validateConnection = validateConnection |
| 278 |
|
| 279 |
result = domish.Element((NS_DIALBACK, "result")) |
| 280 |
result["to"] = self.receiving |
| 281 |
result["from"] = self.originating |
| 282 |
result.addContent(self.key) |
| 283 |
|
| 284 |
d = self.authenticator.onResult(result) |
| 285 |
d.addCallback(cb) |
| 286 |
return d |
| 287 |
|
| 288 |
def test_onResultFailure(self): |
| 289 |
class TestError(Exception): |
| 290 |
pass |
| 291 |
|
| 292 |
def cb(result): |
| 293 |
reply = self.output[0] |
| 294 |
self.assertEqual("invalid", reply["type"]) |
| 295 |
self.assertEqual(1, len(self.flushLoggedErrors(TestError))) |
| 296 |
|
| 297 |
def validateConnection(thisHost, otherHost, sid, key): |
| 298 |
return defer.fail(TestError()) |
| 299 |
|
| 300 |
self.xmlstream.sid = self.sid |
| 301 |
self.service.validateConnection = validateConnection |
| 302 |
|
| 303 |
result = domish.Element((NS_DIALBACK, "result")) |
| 304 |
result["to"] = self.receiving |
| 305 |
result["from"] = self.originating |
| 306 |
result.addContent(self.key) |
| 307 |
|
| 308 |
d = self.authenticator.onResult(result) |
| 309 |
d.addCallback(cb) |
| 310 |
return d |
| 311 |
|
| 312 |
|
| 313 |
class FakeService: |
| 314 |
domains = set(["example.org", "pubsub.example.org"]) |
| 315 |
defaultDomain = "example.org" |
| 316 |
secret = "mysecret" |
| 317 |
|
| 318 |
def __init__(self): |
| 319 |
self.dispatched = [] |
| 320 |
|
| 321 |
def dispatch(self, xs, element): |
| 322 |
self.dispatched.append(element) |
| 323 |
|
| 324 |
|
| 325 |
class XMPPS2SServerFactoryTest(unittest.TestCase): |
| 326 |
""" |
| 327 |
Tests for L{component.XMPPS2SServerFactory}. |
| 328 |
""" |
| 329 |
|
| 330 |
def setUp(self): |
| 331 |
self.service = FakeService() |
| 332 |
self.factory = server.XMPPS2SServerFactory(self.service) |
| 333 |
self.xmlstream = self.factory.buildProtocol(None) |
| 334 |
self.transport = StringTransport() |
| 335 |
self.xmlstream.thisEntity = jid.JID("example.org") |
| 336 |
self.xmlstream.otherEntity = jid.JID("example.com") |
| 337 |
|
| 338 |
def test_makeConnection(self): |
| 339 |
""" |
| 340 |
A new connection increases the stream serial count. No logs by default. |
| 341 |
""" |
| 342 |
self.xmlstream.makeConnection(self.transport) |
| 343 |
self.assertEqual(0, self.xmlstream.serial) |
| 344 |
self.assertEqual(1, self.factory.serial) |
| 345 |
self.assertIdentical(None, self.xmlstream.rawDataInFn) |
| 346 |
self.assertIdentical(None, self.xmlstream.rawDataOutFn) |
| 347 |
|
| 348 |
def test_makeConnectionLogTraffic(self): |
| 349 |
""" |
| 350 |
Setting logTraffic should set up raw data loggers. |
| 351 |
""" |
| 352 |
self.factory.logTraffic = True |
| 353 |
self.xmlstream.makeConnection(self.transport) |
| 354 |
self.assertNotIdentical(None, self.xmlstream.rawDataInFn) |
| 355 |
self.assertNotIdentical(None, self.xmlstream.rawDataOutFn) |
| 356 |
|
| 357 |
def test_onError(self): |
| 358 |
""" |
| 359 |
An observer for stream errors should trigger onError to log it. |
| 360 |
""" |
| 361 |
self.xmlstream.makeConnection(self.transport) |
| 362 |
|
| 363 |
class TestError(Exception): |
| 364 |
pass |
| 365 |
|
| 366 |
reason = failure.Failure(TestError()) |
| 367 |
self.xmlstream.dispatch(reason, xmlstream.STREAM_ERROR_EVENT) |
| 368 |
self.assertEqual(1, len(self.flushLoggedErrors(TestError))) |
| 369 |
|
| 370 |
def test_connectionInitialized(self): |
| 371 |
""" """ |
| 372 |
self.xmlstream.makeConnection(self.transport) |
| 373 |
self.xmlstream.dispatch(self.xmlstream, xmlstream.STREAM_AUTHD_EVENT) |
| 374 |
|
| 375 |
def test_connectionLost(self): |
| 376 |
""" """ |
| 377 |
self.xmlstream.makeConnection(self.transport) |
| 378 |
self.xmlstream.dispatch(self.xmlstream, xmlstream.STREAM_AUTHD_EVENT) |
| 379 |
self.xmlstream.dispatch(None, xmlstream.STREAM_END_EVENT) |
| 380 |
|
| 381 |
def test_Element(self): |
| 382 |
self.xmlstream.makeConnection(self.transport) |
| 383 |
self.xmlstream.dispatch(self.xmlstream, xmlstream.STREAM_AUTHD_EVENT) |
| 384 |
|
| 385 |
stanza = domish.Element((None, "presence")) |
| 386 |
self.xmlstream.dispatch(stanza) |
| 387 |
self.assertEqual(1, len(self.service.dispatched)) |
| 388 |
self.assertIdentical(stanza, self.service.dispatched[-1]) |
| 389 |
|
| 390 |
def test_ElementNotAuthenticated(self): |
| 391 |
self.xmlstream.makeConnection(self.transport) |
| 392 |
|
| 393 |
stanza = domish.Element((None, "presence")) |
| 394 |
self.xmlstream.dispatch(stanza) |
| 395 |
self.assertEqual(0, len(self.service.dispatched)) |
| 396 |
|
| 397 |
|
| 398 |
class ServerServiceTest(unittest.TestCase): |
| 399 |
|
| 400 |
def setUp(self): |
| 401 |
self.output = [] |
| 402 |
|
| 403 |
self.xmlstream = xmlstream.XmlStream(xmlstream.Authenticator()) |
| 404 |
self.xmlstream.thisEntity = jid.JID("example.org") |
| 405 |
self.xmlstream.otherEntity = jid.JID("example.com") |
| 406 |
self.xmlstream.send = self.output.append |
| 407 |
|
| 408 |
self.router = component.Router() |
| 409 |
self.service = server.ServerService( |
| 410 |
self.router, secret="mysecret", domain="example.org" |
| 411 |
) |
| 412 |
self.service.xmlstream = self.xmlstream |
| 413 |
|
| 414 |
def test_defaultDomainInDomains(self): |
| 415 |
""" |
| 416 |
The default domain is part of the domains considered local. |
| 417 |
""" |
| 418 |
self.assertIn(self.service.defaultDomain, self.service.domains) |
| 419 |
|
| 420 |
def test_dispatch(self): |
| 421 |
stanza = domish.Element((None, "presence")) |
| 422 |
stanza["to"] = "[email protected]" |
| 423 |
stanza["from"] = "[email protected]" |
| 424 |
self.service.dispatch(self.xmlstream, stanza) |
| 425 |
|
| 426 |
self.assertEqual(1, len(self.output)) |
| 427 |
self.assertIdentical(stanza, self.output[-1]) |
| 428 |
|
| 429 |
def test_dispatchNoTo(self): |
| 430 |
errors = [] |
| 431 |
self.xmlstream.sendStreamError = errors.append |
| 432 |
|
| 433 |
stanza = domish.Element((None, "presence")) |
| 434 |
stanza["from"] = "[email protected]" |
| 435 |
self.service.dispatch(self.xmlstream, stanza) |
| 436 |
|
| 437 |
self.assertEqual(1, len(errors)) |
| 438 |
|
| 439 |
def test_generatedSecret(self): |
| 440 |
self.router = component.Router() |
| 441 |
self.service = server.ServerService(self.router, domain="example.org") |
| 442 |
self.assertEqual(32, len(self.service.secret)) |