File: flashproxy-client-test.py

package info (click to toggle)
flashproxy 1.7-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 936 kB
  • ctags: 876
  • sloc: python: 3,708; sh: 823; makefile: 246; lisp: 15
file content (401 lines) | stat: -rwxr-xr-x 15,075 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import base64
import cStringIO
import httplib
import socket
import subprocess
import sys
import unittest
try:
    from hashlib import sha1
except ImportError:
    # Python 2.4 uses this name.
    from sha import sha as sha1

# Special tricks to load a module whose filename contains a dash and doesn't end
# in ".py".
import imp
dont_write_bytecode = sys.dont_write_bytecode
sys.dont_write_bytecode = True
fp_client = imp.load_source("fp_client", "flashproxy-client")
parse_socks_request = fp_client.parse_socks_request
handle_websocket_request = fp_client.handle_websocket_request
WebSocketDecoder = fp_client.WebSocketDecoder
WebSocketEncoder = fp_client.WebSocketEncoder
sys.dont_write_bytecode = dont_write_bytecode
del dont_write_bytecode
del fp_client

LOCAL_ADDRESS = ("127.0.0.1", 40000)
REMOTE_ADDRESS = ("127.0.0.1", 40001)

class TestSocks(unittest.TestCase):
    def test_parse_socks_request_empty(self):
        self.assertRaises(ValueError, parse_socks_request, "")
    def test_parse_socks_request_short(self):
        self.assertRaises(ValueError, parse_socks_request, "\x04\x01\x99\x99\x01\x02\x03\x04")
    def test_parse_socks_request_ip_userid_missing(self):
        dest, port = parse_socks_request("\x04\x01\x99\x99\x01\x02\x03\x04\x00")
        dest, port = parse_socks_request("\x04\x01\x99\x99\x01\x02\x03\x04\x00userid")
        self.assertEqual((dest, port), ("1.2.3.4", 0x9999))
    def test_parse_socks_request_ip(self):
        dest, port = parse_socks_request("\x04\x01\x99\x99\x01\x02\x03\x04userid\x00")
        self.assertEqual((dest, port), ("1.2.3.4", 0x9999))
    def test_parse_socks_request_hostname_missing(self):
        self.assertRaises(ValueError, parse_socks_request, "\x04\x01\x99\x99\x00\x00\x00\x01userid\x00")
        self.assertRaises(ValueError, parse_socks_request, "\x04\x01\x99\x99\x00\x00\x00\x01userid\x00abc")
    def test_parse_socks_request_hostname(self):
        dest, port = parse_socks_request("\x04\x01\x99\x99\x00\x00\x00\x01userid\x00abc\x00")

class DummySocket(object):
    def __init__(self, read_fd, write_fd):
        self.read_fd = read_fd
        self.write_fd = write_fd
        self.readp = 0

    def read(self, *args, **kwargs):
        self.read_fd.seek(self.readp, 0)
        data = self.read_fd.read(*args, **kwargs)
        self.readp = self.read_fd.tell()
        return data

    def readline(self, *args, **kwargs):
        self.read_fd.seek(self.readp, 0)
        data = self.read_fd.readline(*args, **kwargs)
        self.readp = self.read_fd.tell()
        return data

    def recv(self, size, *args, **kwargs):
        return self.read(size)

    def write(self, data):
        self.write_fd.seek(0, 2)
        self.write_fd.write(data)

    def send(self, data, *args, **kwargs):
        return self.write(data)

    def sendall(self, data, *args, **kwargs):
        return self.write(data)

    def makefile(self, *args, **kwargs):
        return self

def dummy_socketpair():
    f1 = cStringIO.StringIO()
    f2 = cStringIO.StringIO()
    return (DummySocket(f1, f2), DummySocket(f2, f1))

class HTTPRequest(object):
    def __init__(self):
        self.method = "GET"
        self.path = "/"
        self.headers = {}

def transact_http(req):
    l, r = dummy_socketpair()
    r.send("%s %s HTTP/1.0\r\n" % (req.method, req.path))
    for k, v in req.headers.items():
        r.send("%s: %s\r\n" % (k, v))
    r.send("\r\n")
    protocols = handle_websocket_request(l)

    resp = httplib.HTTPResponse(r)
    resp.begin()
    return resp, protocols

class TestHandleWebSocketRequest(unittest.TestCase):
    DEFAULT_KEY = "0123456789ABCDEF"
    DEFAULT_KEY_BASE64 = base64.b64encode(DEFAULT_KEY)
    MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

    @staticmethod
    def default_req():
        req = HTTPRequest()
        req.method = "GET"
        req.path = "/"
        req.headers["Upgrade"] = "websocket"
        req.headers["Connection"] = "Upgrade"
        req.headers["Sec-WebSocket-Key"] = TestHandleWebSocketRequest.DEFAULT_KEY_BASE64
        req.headers["Sec-WebSocket-Version"] = "13"

        return req

    def assert_ok(self, req):
        resp, protocols = transact_http(req)
        self.assertEqual(resp.status, 101)
        self.assertEqual(resp.getheader("Upgrade").lower(), "websocket")
        self.assertEqual(resp.getheader("Connection").lower(), "upgrade")
        self.assertEqual(resp.getheader("Sec-WebSocket-Accept"), base64.b64encode(sha1(self.DEFAULT_KEY_BASE64 + self.MAGIC_GUID).digest()))
        self.assertEqual(protocols, [])

    def assert_not_ok(self, req):
        resp, protocols = transact_http(req)
        self.assertEqual(resp.status // 100, 4)
        self.assertEqual(protocols, None)

    def test_default(self):
        req = self.default_req()
        self.assert_ok(req)

    def test_missing_upgrade(self):
        req = self.default_req()
        del req.headers["Upgrade"]
        self.assert_not_ok(req)

    def test_missing_connection(self):
        req = self.default_req()
        del req.headers["Connection"]
        self.assert_not_ok(req)

    def test_case_insensitivity(self):
        """Test that the values of the Upgrade and Connection headers are
        case-insensitive."""
        req = self.default_req()
        req.headers["Upgrade"] = req.headers["Upgrade"].lower()
        self.assert_ok(req)
        req.headers["Upgrade"] = req.headers["Upgrade"].upper()
        self.assert_ok(req)
        req.headers["Connection"] = req.headers["Connection"].lower()
        self.assert_ok(req)
        req.headers["Connection"] = req.headers["Connection"].upper()
        self.assert_ok(req)

    def test_bogus_key(self):
        req = self.default_req()
        req.headers["Sec-WebSocket-Key"] = base64.b64encode(self.DEFAULT_KEY[:-1])
        self.assert_not_ok(req)

        req.headers["Sec-WebSocket-Key"] = "///"
        self.assert_not_ok(req)

    def test_versions(self):
        req = self.default_req()
        req.headers["Sec-WebSocket-Version"] = "13"
        self.assert_ok(req)
        req.headers["Sec-WebSocket-Version"] = "8"
        self.assert_ok(req)

        req.headers["Sec-WebSocket-Version"] = "7"
        self.assert_not_ok(req)
        req.headers["Sec-WebSocket-Version"] = "9"
        self.assert_not_ok(req)

        del req.headers["Sec-WebSocket-Version"]
        self.assert_not_ok(req)

    def test_protocols(self):
        req = self.default_req()
        req.headers["Sec-WebSocket-Protocol"] = "base64"
        resp, protocols = transact_http(req)
        self.assertEqual(resp.status, 101)
        self.assertEqual(protocols, ["base64"])
        self.assertEqual(resp.getheader("Sec-WebSocket-Protocol"), "base64")

        req = self.default_req()
        req.headers["Sec-WebSocket-Protocol"] = "cat"
        resp, protocols = transact_http(req)
        self.assertEqual(resp.status, 101)
        self.assertEqual(protocols, ["cat"])
        self.assertEqual(resp.getheader("Sec-WebSocket-Protocol"), None)

        req = self.default_req()
        req.headers["Sec-WebSocket-Protocol"] = "cat, base64"
        resp, protocols = transact_http(req)
        self.assertEqual(resp.status, 101)
        self.assertEqual(protocols, ["cat", "base64"])
        self.assertEqual(resp.getheader("Sec-WebSocket-Protocol"), "base64")

def read_frames(dec):
    frames = []
    while True:
        frame = dec.read_frame()
        if frame is None:
            break
        frames.append((frame.fin, frame.opcode, frame.payload))
    return frames

def read_messages(dec):
    messages = []
    while True:
        message = dec.read_message()
        if message is None:
            break
        messages.append((message.opcode, message.payload))
    return messages

class TestWebSocketDecoder(unittest.TestCase):
    def test_rfc(self):
        """Test samples from RFC 6455 section 5.7."""
        TESTS = [
            ("\x81\x05\x48\x65\x6c\x6c\x6f", False,
                [(True, 1, "Hello")],
                [(1, u"Hello")]),
            ("\x81\x85\x37\xfa\x21\x3d\x7f\x9f\x4d\x51\x58", True,
                [(True, 1, "Hello")],
                [(1, u"Hello")]),
            ("\x01\x03\x48\x65\x6c\x80\x02\x6c\x6f", False,
                [(False, 1, "Hel"), (True, 0, "lo")],
                [(1, u"Hello")]),
            ("\x89\x05\x48\x65\x6c\x6c\x6f", False,
                [(True, 9, "Hello")],
                [(9, u"Hello")]),
            ("\x8a\x85\x37\xfa\x21\x3d\x7f\x9f\x4d\x51\x58", True,
                [(True, 10, "Hello")],
                [(10, u"Hello")]),
            ("\x82\x7e\x01\x00" + "\x00" * 256, False,
                [(True, 2, "\x00" * 256)],
                [(2, "\x00" * 256)]),
            ("\x82\x7f\x00\x00\x00\x00\x00\x01\x00\x00" + "\x00" * 65536, False,
                [(True, 2, "\x00" * 65536)],
                [(2, "\x00" * 65536)]),
            ("\x82\x7f\x00\x00\x00\x00\x00\x01\x00\x03" + "ABCD" * 16384 + "XYZ", False,
                [(True, 2, "ABCD" * 16384 + "XYZ")],
                [(2, "ABCD" * 16384 + "XYZ")]),
        ]
        for data, use_mask, expected_frames, expected_messages in TESTS:
            dec = WebSocketDecoder(use_mask = use_mask)
            dec.feed(data)
            actual_frames = read_frames(dec)
            self.assertEqual(actual_frames, expected_frames)

            dec = WebSocketDecoder(use_mask = use_mask)
            dec.feed(data)
            actual_messages = read_messages(dec)
            self.assertEqual(actual_messages, expected_messages)

            dec = WebSocketDecoder(use_mask = not use_mask)
            dec.feed(data)
            self.assertRaises(WebSocketDecoder.MaskingError, dec.read_frame)

    def test_empty_feed(self):
        """Test that the decoder can handle a zero-byte feed."""
        dec = WebSocketDecoder()
        self.assertEqual(dec.read_frame(), None)
        dec.feed("")
        self.assertEqual(dec.read_frame(), None)
        dec.feed("\x81\x05H")
        self.assertEqual(dec.read_frame(), None)
        dec.feed("ello")
        self.assertEqual(read_frames(dec), [(True, 1, u"Hello")])

    def test_empty_frame(self):
        """Test that a frame may contain a zero-byte payload."""
        dec = WebSocketDecoder()
        dec.feed("\x81\x00")
        self.assertEqual(read_frames(dec), [(True, 1, u"")])
        dec.feed("\x82\x00")
        self.assertEqual(read_frames(dec), [(True, 2, "")])

    def test_empty_message(self):
        """Test that a message may have a zero-byte payload."""
        dec = WebSocketDecoder()
        dec.feed("\x01\x00\x00\x00\x80\x00")
        self.assertEqual(read_messages(dec), [(1, u"")])
        dec.feed("\x02\x00\x00\x00\x80\x00")
        self.assertEqual(read_messages(dec), [(2, "")])

    def test_interleaved_control(self):
        """Test that control messages interleaved with fragmented messages are
        returned."""
        dec = WebSocketDecoder()
        dec.feed("\x89\x04PING\x01\x03Hel\x8a\x04PONG\x80\x02lo\x89\x04PING")
        self.assertEqual(read_messages(dec), [(9, "PING"), (10, "PONG"), (1, u"Hello"), (9, "PING")])

    def test_fragmented_control(self):
        """Test that illegal fragmented control messages cause an error."""
        dec = WebSocketDecoder()
        dec.feed("\x09\x04PING")
        self.assertRaises(ValueError, dec.read_message)

    def test_zero_opcode(self):
        """Test that it is an error for the first frame in a message to have an
        opcode of 0."""
        dec = WebSocketDecoder()
        dec.feed("\x80\x05Hello")
        self.assertRaises(ValueError, dec.read_message)
        dec = WebSocketDecoder()
        dec.feed("\x00\x05Hello")
        self.assertRaises(ValueError, dec.read_message)

    def test_nonzero_opcode(self):
        """Test that every frame after the first must have a zero opcode."""
        dec = WebSocketDecoder()
        dec.feed("\x01\x01H\x01\x02el\x80\x02lo")
        self.assertRaises(ValueError, dec.read_message)
        dec = WebSocketDecoder()
        dec.feed("\x01\x01H\x00\x02el\x01\x02lo")
        self.assertRaises(ValueError, dec.read_message)

    def test_utf8(self):
        """Test that text frames (opcode 1) are decoded from UTF-8."""
        text = u"Hello World or Καλημέρα κόσμε or こんにちは 世界 or \U0001f639"
        utf8_text = text.encode("utf-8")
        dec = WebSocketDecoder()
        dec.feed("\x81" + chr(len(utf8_text)) + utf8_text)
        self.assertEqual(read_messages(dec), [(1, text)])

    def test_wrong_utf8(self):
        """Test that failed UTF-8 decoding causes an error."""
        TESTS = [
            "\xc0\x41", # Non-shortest form.
            "\xc2", # Unfinished sequence.
        ]
        for test in TESTS:
            dec = WebSocketDecoder()
            dec.feed("\x81" + chr(len(test)) + test)
            self.assertRaises(ValueError, dec.read_message)

    def test_overly_large_payload(self):
        """Test that large payloads are rejected."""
        dec = WebSocketDecoder()
        dec.feed("\x82\x7f\x00\x00\x00\x00\x01\x00\x00\x00")
        self.assertRaises(ValueError, dec.read_frame)

class TestWebSocketEncoder(unittest.TestCase):
    def test_length(self):
        """Test that payload lengths are encoded using the smallest number of
        bytes."""
        TESTS = [(0, 0), (125, 0), (126, 2), (65535, 2), (65536, 8)]
        for length, encoded_length in TESTS:
            enc = WebSocketEncoder(use_mask = False)
            eframe = enc.encode_frame(2, "\x00" * length)
            self.assertEqual(len(eframe), 1 + 1 + encoded_length + length)
            enc = WebSocketEncoder(use_mask = True)
            eframe = enc.encode_frame(2, "\x00" * length)
            self.assertEqual(len(eframe), 1 + 1 + encoded_length + 4 + length)

    def test_roundtrip(self):
        TESTS = [
            (1, u"Hello world"),
            (1, u"Hello \N{WHITE SMILING FACE}"),
        ]
        for opcode, payload in TESTS:
            for use_mask in (False, True):
                enc = WebSocketEncoder(use_mask = use_mask)
                enc_message = enc.encode_message(opcode, payload)
                dec = WebSocketDecoder(use_mask = use_mask)
                dec.feed(enc_message)
                self.assertEqual(read_messages(dec), [(opcode, payload)])

def format_address(addr):
    return "%s:%d" % addr

class TestConnectionLimit(unittest.TestCase):
    def setUp(self):
        self.p = subprocess.Popen(["./flashproxy-client", format_address(LOCAL_ADDRESS), format_address(REMOTE_ADDRESS)])

    def tearDown(self):
        self.p.terminate()

#     def test_remote_limit(self):
#         """Test that the client transport plugin limits the number of remote
#         connections that it will accept."""
#         for i in range(5):
#             s = socket.create_connection(REMOTE_ADDRESS, 2)
#         self.assertRaises(socket.error, socket.create_connection, REMOTE_ADDRESS)

if __name__ == "__main__":
    unittest.main()