1############################################################################### 2# 3# The MIT License (MIT) 4# 5# Copyright (c) Crossbar.io Technologies GmbH 6# 7# Permission is hereby granted, free of charge, to any person obtaining a copy 8# of this software and associated documentation files (the "Software"), to deal 9# in the Software without restriction, including without limitation the rights 10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11# copies of the Software, and to permit persons to whom the Software is 12# furnished to do so, subject to the following conditions: 13# 14# The above copyright notice and this permission notice shall be included in 15# all copies or substantial portions of the Software. 16# 17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23# THE SOFTWARE. 24# 25############################################################################### 26 27from __future__ import absolute_import, print_function 28 29import os 30import unittest 31from txaio.testutil import replace_loop 32 33if os.environ.get('USE_TWISTED', False): 34 from mock import patch 35 from zope.interface import implementer 36 from twisted.internet.interfaces import IReactorTime 37 38 @implementer(IReactorTime) 39 class FakeReactor(object): 40 ''' 41 This just fakes out enough reactor methods so .run() can work. 42 ''' 43 stop_called = False 44 45 def __init__(self, to_raise): 46 self.stop_called = False 47 self.to_raise = to_raise 48 self.delayed = [] 49 50 def run(self, *args, **kw): 51 raise self.to_raise 52 53 def stop(self): 54 self.stop_called = True 55 56 def callLater(self, delay, func, *args, **kwargs): 57 self.delayed.append((delay, func, args, kwargs)) 58 59 def connectTCP(self, *args, **kw): 60 raise RuntimeError("ConnectTCP shouldn't get called") 61 62 class TestWampTwistedRunner(unittest.TestCase): 63 # XXX should figure out *why* but the test_protocol timeout 64 # tests fail if we *don't* patch out this txaio stuff. So, 65 # presumably it's messing up some global state that both tests 66 # implicitly depend on ... 67 68 @patch('txaio.use_twisted') 69 @patch('txaio.start_logging') 70 @patch('txaio.config') 71 def test_connect_error(self, *args): 72 ''' 73 Ensure the runner doesn't swallow errors and that it exits the 74 reactor properly if there is one. 75 ''' 76 try: 77 from autobahn.twisted.wamp import ApplicationRunner 78 from twisted.internet.error import ConnectionRefusedError 79 # the 'reactor' member doesn't exist until we import it 80 from twisted.internet import reactor # noqa: F401 81 except ImportError: 82 raise unittest.SkipTest('No twisted') 83 84 runner = ApplicationRunner(u'ws://localhost:1', u'realm') 85 exception = ConnectionRefusedError("It's a trap!") 86 87 with patch('twisted.internet.reactor', FakeReactor(exception)) as mockreactor: 88 self.assertRaises( 89 ConnectionRefusedError, 90 # pass a no-op session-creation method 91 runner.run, lambda _: None, start_reactor=True 92 ) 93 self.assertTrue(mockreactor.stop_called) 94else: 95 # Asyncio tests. 96 try: 97 import asyncio 98 from unittest.mock import patch, Mock 99 except ImportError: 100 # Trollius >= 0.3 was renamed to asyncio 101 # noinspection PyUnresolvedReferences 102 import trollius as asyncio 103 from mock import patch, Mock 104 from autobahn.asyncio.wamp import ApplicationRunner 105 106 class TestApplicationRunner(unittest.TestCase): 107 ''' 108 Test the autobahn.asyncio.wamp.ApplicationRunner class. 109 ''' 110 def _assertRaisesRegex(self, exception, error, *args, **kw): 111 try: 112 self.assertRaisesRegex 113 except AttributeError: 114 f = self.assertRaisesRegexp 115 else: 116 f = self.assertRaisesRegex 117 f(exception, error, *args, **kw) 118 119 def test_explicit_SSLContext(self): 120 ''' 121 Ensure that loop.create_connection is called with the exact SSL 122 context object that is passed (as ssl) to the __init__ method of 123 ApplicationRunner. 124 ''' 125 with replace_loop(Mock()) as loop: 126 with patch.object(asyncio, 'get_event_loop', return_value=loop): 127 loop.run_until_complete = Mock(return_value=(Mock(), Mock())) 128 ssl = {} 129 runner = ApplicationRunner(u'ws://127.0.0.1:8080/ws', u'realm', 130 ssl=ssl) 131 runner.run('_unused_') 132 self.assertIs(ssl, loop.create_connection.call_args[1]['ssl']) 133 134 def test_omitted_SSLContext_insecure(self): 135 ''' 136 Ensure that loop.create_connection is called with ssl=False 137 if no ssl argument is passed to the __init__ method of 138 ApplicationRunner and the websocket URL starts with "ws:". 139 ''' 140 with replace_loop(Mock()) as loop: 141 with patch.object(asyncio, 'get_event_loop', return_value=loop): 142 loop.run_until_complete = Mock(return_value=(Mock(), Mock())) 143 runner = ApplicationRunner(u'ws://127.0.0.1:8080/ws', u'realm') 144 runner.run('_unused_') 145 self.assertIs(False, loop.create_connection.call_args[1]['ssl']) 146 147 def test_omitted_SSLContext_secure(self): 148 ''' 149 Ensure that loop.create_connection is called with ssl=True 150 if no ssl argument is passed to the __init__ method of 151 ApplicationRunner and the websocket URL starts with "wss:". 152 ''' 153 with replace_loop(Mock()) as loop: 154 with patch.object(asyncio, 'get_event_loop', return_value=loop): 155 loop.run_until_complete = Mock(return_value=(Mock(), Mock())) 156 runner = ApplicationRunner(u'wss://127.0.0.1:8080/wss', u'realm') 157 runner.run(self.fail) 158 self.assertIs(True, loop.create_connection.call_args[1]['ssl']) 159 160 def test_conflict_SSL_True_with_ws_url(self): 161 ''' 162 ApplicationRunner must raise an exception if given an ssl value of True 163 but only a "ws:" URL. 164 ''' 165 with replace_loop(Mock()) as loop: 166 loop.run_until_complete = Mock(return_value=(Mock(), Mock())) 167 runner = ApplicationRunner(u'ws://127.0.0.1:8080/wss', u'realm', 168 ssl=True) 169 error = (r'^ssl argument value passed to ApplicationRunner ' 170 r'conflicts with the "ws:" prefix of the url ' 171 r'argument\. Did you mean to use "wss:"\?$') 172 self._assertRaisesRegex(Exception, error, runner.run, '_unused_') 173 174 def test_conflict_SSLContext_with_ws_url(self): 175 ''' 176 ApplicationRunner must raise an exception if given an ssl value that is 177 an instance of SSLContext, but only a "ws:" URL. 178 ''' 179 import ssl 180 try: 181 # Try to create an SSLContext, to be as rigorous as we can be 182 # by avoiding making assumptions about the ApplicationRunner 183 # implementation. If we happen to be on a Python that has no 184 # SSLContext, we pass ssl=True, which will simply cause this 185 # test to degenerate to the behavior of 186 # test_conflict_SSL_True_with_ws_url (above). In fact, at the 187 # moment (2015-05-10), none of this matters because the 188 # ApplicationRunner implementation does not check to require 189 # that its ssl argument is either a bool or an SSLContext. But 190 # that may change, so we should be careful. 191 ssl.create_default_context 192 except AttributeError: 193 context = True 194 else: 195 context = ssl.create_default_context() 196 197 with replace_loop(Mock()) as loop: 198 loop.run_until_complete = Mock(return_value=(Mock(), Mock())) 199 runner = ApplicationRunner(u'ws://127.0.0.1:8080/wss', u'realm', 200 ssl=context) 201 error = (r'^ssl argument value passed to ApplicationRunner ' 202 r'conflicts with the "ws:" prefix of the url ' 203 r'argument\. Did you mean to use "wss:"\?$') 204 self._assertRaisesRegex(Exception, error, runner.run, '_unused_') 205