/usr/lib/python2.7/dist-packages/autobahn/websocket/test/test_websocket.py is in python-autobahn 0.14.1+dfsg1-2.
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 | ###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Tavendo GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
from __future__ import absolute_import, print_function
import os
import struct
if os.environ.get('USE_TWISTED', False):
from twisted.trial import unittest
from twisted.internet.address import IPv4Address
from twisted.internet.task import Clock
from six import PY3
from autobahn.twisted.websocket import WebSocketServerProtocol
from autobahn.twisted.websocket import WebSocketServerFactory
from autobahn.twisted.websocket import WebSocketClientProtocol
from autobahn.twisted.websocket import WebSocketClientFactory
from mock import MagicMock, patch
from txaio.testutil import replace_loop
from base64 import b64decode
@patch('base64.b64encode')
def create_client_frame(b64patch, **kwargs):
"""
Kind-of hack-y; maybe better to re-factor the Protocol to have a
frame-encoder method-call? Anyway, makes a throwaway protocol
encode a frame for us, collects the .sendData call and returns
the data that would have gone out. Accepts all the kwargs that
WebSocketClientProtocol.sendFrame() accepts.
"""
# only real way to inject a "known" secret-key for the headers
# to line up... :/
b64patch.return_value = b'QIatSt9QkZPyS4QQfdufO8TgkL0='
factory = WebSocketClientFactory(protocols=['wamp.2.json'])
factory.protocol = WebSocketClientProtocol
factory.doStart()
proto = factory.buildProtocol(IPv4Address('TCP', '127.0.0.9', 65534))
proto.transport = MagicMock()
proto.connectionMade()
proto.data = mock_handshake_server
proto.processHandshake()
data = []
def collect(d, *args):
data.append(d)
proto.sendData = collect
proto.sendFrame(**kwargs)
return b''.join(data)
# beware the evils of line-endings...
mock_handshake_client = b'GET / HTTP/1.1\r\nUser-Agent: AutobahnPython/0.10.2\r\nHost: localhost:80\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nSec-WebSocket-Key: 6Jid6RgXpH0RVegaNSs/4g==\r\nSec-WebSocket-Protocol: wamp.2.json\r\nSec-WebSocket-Version: 13\r\n\r\n'
mock_handshake_server = b'HTTP/1.1 101 Switching Protocols\r\nServer: AutobahnPython/0.10.2\r\nX-Powered-By: AutobahnPython/0.10.2\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nSec-WebSocket-Protocol: wamp.2.json\r\nSec-WebSocket-Accept: QIatSt9QkZPyS4QQfdufO8TgkL0=\r\n\r\n\x81~\x02\x19[1,"crossbar",{"roles":{"subscriber":{"features":{"publisher_identification":true,"pattern_based_subscription":true,"subscription_revocation":true}},"publisher":{"features":{"publisher_identification":true,"publisher_exclusion":true,"subscriber_blackwhite_listing":true}},"caller":{"features":{"caller_identification":true,"progressive_call_results":true}},"callee":{"features":{"progressive_call_results":true,"pattern_based_registration":true,"registration_revocation":true,"shared_registration":true,"caller_identification":true}}}}]\x18'
class TestClient(unittest.TestCase):
def setUp(self):
self.factory = WebSocketClientFactory(protocols=['wamp.2.json'])
self.factory.protocol = WebSocketClientProtocol
self.factory.doStart()
self.proto = self.factory.buildProtocol(IPv4Address('TCP', '127.0.0.1', 65534))
self.transport = MagicMock()
self.proto.transport = self.transport
self.proto.connectionMade()
def tearDown(self):
if self.proto.openHandshakeTimeoutCall:
self.proto.openHandshakeTimeoutCall.cancel()
self.factory.doStop()
# not really necessary, but ...
del self.factory
del self.proto
def test_missing_reason_raw(self):
# we want to hit the "STATE_OPEN" case, so pretend we're there
self.proto.echoCloseCodeReason = True
self.proto.state = self.proto.STATE_OPEN
self.proto.websocket_version = 1
self.proto.sendCloseFrame = MagicMock()
self.proto.onCloseFrame(1000, None)
def test_unclean_timeout_client(self):
"""
make a delayed call to drop the connection (client-side)
"""
if False:
self.proto.factory._log = print
# get to STATE_OPEN
self.proto.websocket_key = b64decode('6Jid6RgXpH0RVegaNSs/4g==')
self.proto.data = mock_handshake_server
self.proto.processHandshake()
self.assertEqual(self.proto.state, WebSocketServerProtocol.STATE_OPEN)
self.assertTrue(self.proto.serverConnectionDropTimeout > 0)
with replace_loop(Clock()) as reactor:
# now 'do the test' and transition to CLOSING
self.proto.sendCloseFrame()
self.proto.onCloseFrame(1000, b"raw reason")
# check we scheduled a call
self.assertEqual(len(reactor.calls), 1)
self.assertEqual(reactor.calls[0].func, self.proto.onServerConnectionDropTimeout)
self.assertEqual(reactor.calls[0].getTime(), self.proto.serverConnectionDropTimeout)
# now, advance the clock past the call (and thereby
# execute it)
reactor.advance(self.proto.closeHandshakeTimeout + 1)
# we should have called abortConnection
self.assertEqual("call.abortConnection()", str(self.proto.transport.method_calls[-1]))
self.assertTrue(self.proto.transport.abortConnection.called)
# ...too "internal" for an assert?
self.assertEqual(self.proto.state, WebSocketServerProtocol.STATE_CLOSED)
class TestPing(unittest.TestCase):
def setUp(self):
self.factory = WebSocketServerFactory(protocols=['wamp.2.json'])
self.factory.protocol = WebSocketServerProtocol
self.factory.doStart()
self.proto = self.factory.buildProtocol(IPv4Address('TCP', '127.0.0.1', 65534))
self.transport = MagicMock()
self.proto.transport = self.transport
self.proto.connectionMade()
def tearDown(self):
self.factory.doStop()
# not really necessary, but ...
del self.factory
del self.proto
def test_unclean_timeout(self):
"""
make a delayed call to drop the connection
"""
# first we have to drive the protocol to STATE_CLOSING
# ... which we achieve by sendCloseFrame after we're in
# STATE_OPEN
# XXX double-check this is the correct code-path to get here
# "normally"?
# get to STATE_OPEN
self.proto.data = mock_handshake_client
self.proto.processHandshake()
self.assertTrue(self.proto.state == WebSocketServerProtocol.STATE_OPEN)
with replace_loop(Clock()) as reactor:
# now 'do the test' and transition to CLOSING
self.proto.sendCloseFrame()
# check we scheduled a call
self.assertEqual(len(reactor.calls), 1)
# now, advance the clock past the call (and thereby
# execute it)
reactor.advance(self.proto.closeHandshakeTimeout + 1)
# we should have called abortConnection
self.assertEqual("call.abortConnection()", str(self.proto.transport.method_calls[-1]))
self.assertTrue(self.proto.transport.abortConnection.called)
# ...too "internal" for an assert?
self.assertEqual(self.proto.state, WebSocketServerProtocol.STATE_CLOSED)
def test_auto_pingpong_timeout(self):
"""
autoping and autoping-timeout timing
"""
# options are evaluated in succeedHandshake, called below
self.proto.autoPingInterval = 5
self.proto.autoPingTimeout = 2
with replace_loop(Clock()) as reactor:
# get to STATE_OPEN
self.proto.data = mock_handshake_client
self.proto.processHandshake()
self.assertTrue(self.proto.state == WebSocketServerProtocol.STATE_OPEN)
# we should have scheduled an autoPing
self.assertEqual(1, len(reactor.calls))
# advance past first auto-ping timeout
reactor.advance(5)
# first element from args tuple from transport.write()
# call is our data
self.assertTrue(self.transport.write.called)
data = self.transport.write.call_args[0][0]
if PY3:
_data = bytes([data[0]])
else:
_data = data[0]
# the opcode is the lower 7 bits of the first byte.
(opcode,) = struct.unpack("B", _data)
opcode = opcode & (~0x80)
# ... and should be "9" for ping
self.assertEqual(9, opcode)
# Because we have autoPingTimeout there should be
# another delayed-called created now
self.assertEqual(1, len(reactor.calls))
self.assertNotEqual(self.proto.state, self.proto.STATE_CLOSED)
# ...which we'll now cause to trigger, aborting the connection
reactor.advance(3)
self.assertEqual(self.proto.state, self.proto.STATE_CLOSED)
def test_auto_ping_got_pong(self):
"""
auto-ping with correct reply cancels timeout
"""
# options are evaluated in succeedHandshake, called below
self.proto.autoPingInterval = 5
self.proto.autoPingTimeout = 2
with replace_loop(Clock()) as reactor:
# get to STATE_OPEN
self.proto.data = mock_handshake_client
self.proto.processHandshake()
self.assertTrue(self.proto.state == WebSocketServerProtocol.STATE_OPEN)
# we should have scheduled an autoPing
self.assertEqual(1, len(reactor.calls))
# advance past first auto-ping timeout
reactor.advance(5)
# should have an auto-ping timeout scheduled, and we
# save it for later (to check it got cancelled)
self.assertEqual(1, len(reactor.calls))
timeout_call = reactor.calls[0]
# elsewhere we check that we actually send an opcode-9
# message; now we just blindly inject our own reply
# with a PONG frame
frame = create_client_frame(opcode=10, payload=self.proto.autoPingPending)
self.proto.data = frame
# really needed twice; does header first, then rest
self.proto.processData()
self.proto.processData()
# which should have cancelled the call
self.assertTrue(timeout_call.cancelled)
|