File: test_frame.py

package info (click to toggle)
python-ws4py 0.4.2%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 688 kB
  • sloc: python: 4,500; makefile: 140; javascript: 96
file content (307 lines) | stat: -rw-r--r-- 10,816 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
import os
import unittest
import types
import random
from struct import pack, unpack

from ws4py.framing import Frame, \
     OPCODE_CONTINUATION, OPCODE_TEXT, \
     OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG
from ws4py.exc import FrameTooLargeException, ProtocolException
from ws4py.compat import *

def map_on_bytes(f, bytes):
    for index, byte in enumerate(bytes):
        f(bytes[index:index+1])

class WSFrameBuilderTest(unittest.TestCase):
    def test_7_bit_length(self):
        f = Frame(opcode=OPCODE_TEXT,
                  body=b'', fin=1)
        self.assertEqual(len(f.build()), 2)

        f = Frame(opcode=OPCODE_TEXT,
                  body=b'*' * 125, fin=1)
        self.assertEqual(len(f.build()), 127)

        mask = os.urandom(4)
        f = Frame(opcode=OPCODE_TEXT,
                  body=b'', masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 6)

        f = Frame(opcode=OPCODE_TEXT,
                  body=b'*' * 125, masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 131)

    def test_16_bit_length(self):
        f = Frame(opcode=OPCODE_TEXT,
                  body=b'*' * 126, fin=1)
        self.assertEqual(len(f.build()), 130)

        f = Frame(opcode=OPCODE_TEXT,
                  body=b'*' * 65535, fin=1)
        self.assertEqual(len(f.build()), 65539)

        mask = os.urandom(4)
        f = Frame(opcode=OPCODE_TEXT,
                  body=b'*' * 126, masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 134)

        f = Frame(opcode=OPCODE_TEXT,
                  body=b'*' * 65535, masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 65543)

    def test_63_bit_length(self):
        f = Frame(opcode=OPCODE_TEXT,
                  body=b'*' * 65536, fin=1)
        self.assertEqual(len(f.build()), 65546)

        mask = os.urandom(4)
        f = Frame(opcode=OPCODE_TEXT,
                  body=b'*' * 65536, masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 65550)

    def test_non_zero_nor_one_fin(self):
        f = Frame(opcode=OPCODE_TEXT,
                  body=b'', fin=2)
        self.assertRaises(ValueError, f.build)

    def test_opcodes(self):
        for opcode in [OPCODE_CONTINUATION, OPCODE_TEXT,
                       OPCODE_BINARY, OPCODE_CLOSE,
                       OPCODE_PING, OPCODE_PONG]:
            f = Frame(opcode=opcode, body=b'', fin=1)
            byte = ord(f.build()[0])
            self.assertTrue(byte & opcode == opcode)

        f = Frame(opcode=0x3, body=b'', fin=1)
        self.assertRaises(ValueError, f.build)

    def test_masking(self):
        if py3k: mask = b"7\xfa!="
        else: mask = "7\xfa!="
        f = Frame(opcode=OPCODE_TEXT,
                  body=b'Hello',
                  masking_key=mask, fin=1)

        if py3k: spec_says = b'\x81\x857\xfa!=\x7f\x9fMQX'
        else: spec_says = '\x81\x857\xfa!=\x7f\x9fMQX'
        self.assertEqual(f.build(), spec_says)

    def test_frame_too_large(self):
        f = Frame(opcode=OPCODE_TEXT, body=b'', fin=1)
        # fake huge length
        f.payload_length = 1 << 63
        self.assertRaises(FrameTooLargeException, f.build)

    def test_passing_encoded_string(self):
        # once encoded the u'\xe9' character will be of length 2
        f = Frame(opcode=OPCODE_TEXT, body=u'\xe9trange'.encode('utf-8'), fin=1)
        self.assertEqual(len(f.build()), 10)

    def test_passing_unencoded_string_raises_type_error(self):
        self.assertRaises(TypeError, Frame, opcode=OPCODE_TEXT, body=u'\xe9', fin=1)

class WSFrameParserTest(unittest.TestCase):
    def test_frame_parser_is_a_generator(self):
        f = Frame()
        self.assertEqual(type(f.parser), types.GeneratorType)
        f.parser.close()
        self.assertRaises(StopIteration, next, f.parser)

    def test_frame_header_parsing(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1).build()

        f = Frame()
        self.assertEqual(f.parser.send(bytes[0:1]), 1)
        self.assertEqual(f.fin, 1)
        self.assertEqual(f.rsv1, 0)
        self.assertEqual(f.rsv2, 0)
        self.assertEqual(f.rsv3, 0)
        self.assertEqual(f.parser.send(bytes[1:2]), 5)
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 5)
        f.parser.close()

    def test_frame_payload_parsing(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1).build()

        f = Frame()
        self.assertEqual(f.parser.send(bytes[0:1]), 1)
        self.assertEqual(f.parser.send(bytes[1:2]), 5)
        f.parser.send(bytes[2:])
        self.assertEqual(f.body, b'hello')

        f = Frame()
        f.parser.send(bytes)
        self.assertRaises(StopIteration, next, f.parser)
        self.assertEqual(f.body, b'hello')

    def test_incremental_parsing_small_7_bit_length(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1).build()

        f = Frame()
        map_on_bytes(f.parser.send, bytes)
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 5)

    def test_incremental_parsing_16_bit_length(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'*' * 126, fin=1).build()

        f = Frame()
        map_on_bytes(f.parser.send, bytes)
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 126)

    def test_incremental_parsing_63_bit_length(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'*' * 65536, fin=1).build()

        f = Frame()
        map_on_bytes(f.parser.send, bytes)
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 65536)

    def test_rsv1_bits_set(self):
        f = Frame()
        self.assertRaises(ProtocolException, f.parser.send, b'\x40')

    def test_rsv2_bits_set(self):
        f = Frame()
        self.assertRaises(ProtocolException, f.parser.send, b'\x20')

    def test_rsv3_bits_set(self):
        f = Frame()
        self.assertRaises(ProtocolException, f.parser.send, b'\x10')

    def test_invalid_opcode(self):
        for opcode in range(3, 9):
            f = Frame()
            self.assertRaises(ProtocolException, f.parser.send, chr(opcode))

        f = Frame()
        self.assertRaises(ProtocolException, f.parser.send, chr(10))

    def test_fragmented_control_frame_is_invalid(self):
        f = Frame()
        self.assertRaises(ProtocolException, f.parser.send, b'0x9')

    def test_fragmented_control_frame_is_too_large(self):
        bytes = Frame(opcode=OPCODE_PING, body=b'*'*65536, fin=1).build()
        f = Frame()
        self.assertRaises(FrameTooLargeException, f.parser.send, bytes)

    def test_frame_sized_127(self):
        body = b'*'*65536
        bytes = Frame(opcode=OPCODE_TEXT, body=body, fin=1).build()

        f = Frame()
        # determine how the size is stored
        f.parser.send(bytes[:3])
        self.assertTrue(f.masking_key is None)
        # that's a large frame indeed
        self.assertEqual(f.payload_length, 127)

        # this will compute the actual application data size
        # it will also read the first byte of data
        # indeed the length is found from byte 3 to 10
        f.parser.send(bytes[3:11])
        self.assertEqual(f.payload_length, 65536)
        
        # parse the rest of our data
        f.parser.send(bytes[11:])
        self.assertEqual(f.body, body)

        
        # The same but this time we provide enough
        # bytes so that the application's data length
        # can be computed from the first generator's send call
        f = Frame()
        f.parser.send(bytes[:10])
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 65536)
        
        # parse the rest of our data
        f.parser.send(bytes[10:])
        self.assertEqual(f.body, body)
        
        
        # The same with masking given out gradually
        mask = os.urandom(4)
        bytes = Frame(opcode=OPCODE_TEXT, body=body, fin=1, masking_key=mask).build()
        f = Frame()
        f.parser.send(bytes[:10])
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 65536)
        
        # parse the mask gradually
        f.parser.send(bytes[10:12])
        f.parser.send(bytes[12:])
        self.assertEqual(f.unmask(f.body), body)
        
    def test_frame_too_large_with_7_bit_length(self):
        header = pack('!B', ((1 << 7)
                             | (0 << 6)
                             | (0 << 5)
                             | (0 << 4)
                             | OPCODE_TEXT))
        header += pack('!B', 127) + pack('!Q', 1 << 63)
        b = bytes(header + b'')
        f = Frame()
        self.assertRaises(FrameTooLargeException, f.parser.send, b)

    def test_not_sensitive_to_overflow(self):
        header = pack('!B', ((1 << 7)
                             | (0 << 6)
                             | (0 << 5)
                             | (0 << 4)
                             | OPCODE_TEXT))
        header += pack('!B', 126) + pack('!H', 256)
        b = bytes(header + b'*' * 512)
        f = Frame()
        f.parser.send(b)
        # even though we tried to inject 512 bytes, we
        # still only read 256
        self.assertEqual(len(f.body), 256)
        
    def test_frame_sized_126(self):
        body = b'*'*256
        bytes = Frame(opcode=OPCODE_TEXT, body=body, fin=1).build()

        f = Frame()
        # determine how the size is stored
        f.parser.send(bytes[:3])
        self.assertTrue(f.masking_key is None)
        # that's a large frame indeed
        self.assertEqual(f.payload_length, 126)

        # this will compute the actual application data size
        # it will also read the first byte of data
        # indeed the length is found from byte 3 to 10
        f.parser.send(bytes[3:11])
        self.assertEqual(f.payload_length, 256)
        
        # parse the rest of our data
        f.parser.send(bytes[11:])
        self.assertEqual(f.body, body)
        
        
        # The same but this time we provide enough
        # bytes so that the application's data length
        # can be computed from the first generator's send call
        f = Frame()
        f.parser.send(bytes[:10])
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 256)
        
        # parse the rest of our data
        f.parser.send(bytes[10:])
        self.assertEqual(f.body, body)

if __name__ == '__main__':
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    for testcase in (WSFrameBuilderTest, WSFrameParserTest,):
        tests = loader.loadTestsFromTestCase(testcase)
        suite.addTests(tests)
    unittest.TextTestRunner(verbosity=2).run(suite)