@@ -185,14 +185,93 @@ def test_fragmented_control_frame_is_invalid(self):
185185 f = Frame ()
186186 self .assertRaises (ProtocolException , f .parser .send , b'0x9' )
187187
188- def test_frame_sized_below_127 (self ):
189- bytes = Frame (opcode = OPCODE_TEXT , body = b'*' * 65536 , fin = 1 ).build ()
188+ def test_fragmented_control_frame_is_too_large (self ):
189+ bytes = Frame (opcode = OPCODE_PING , body = b'*' * 65536 , fin = 1 ).build ()
190+ f = Frame ()
191+ self .assertRaises (FrameTooLargeException , f .parser .send , bytes )
192+
193+ def test_frame_sized_127 (self ):
194+ body = b'*' * 65536
195+ bytes = Frame (opcode = OPCODE_TEXT , body = body , fin = 1 ).build ()
190196
191197 f = Frame ()
198+ # determine how the size is stored
192199 f .parser .send (bytes [:3 ])
193200 self .assertTrue (f .masking_key is None )
201+ # that's a large frame indeed
194202 self .assertEqual (f .payload_length , 127 )
195203
204+ # this will compute the actual application data size
205+ # it will also read the first byte of data
206+ # indeed the length is found from byte 3 to 10
207+ f .parser .send (bytes [3 :11 ])
208+ self .assertEqual (f .payload_length , 65536 )
209+
210+ # parse the rest of our data
211+ f .parser .send (bytes [11 :])
212+ self .assertEqual (f .body , body )
213+
214+
215+ # The same but this time we provide enough
216+ # bytes so that the application's data length
217+ # can be computed from the first generator's send call
218+ f = Frame ()
219+ f .parser .send (bytes [:10 ])
220+ self .assertTrue (f .masking_key is None )
221+ self .assertEqual (f .payload_length , 65536 )
222+
223+ # parse the rest of our data
224+ f .parser .send (bytes [10 :])
225+ self .assertEqual (f .body , body )
226+
227+
228+ # The same with masking given out gradually
229+ mask = os .urandom (4 )
230+ bytes = Frame (opcode = OPCODE_TEXT , body = body , fin = 1 , masking_key = mask ).build ()
231+ f = Frame ()
232+ f .parser .send (bytes [:10 ])
233+ self .assertTrue (f .masking_key is None )
234+ self .assertEqual (f .payload_length , 65536 )
235+
236+ # parse the mask gradually
237+ f .parser .send (bytes [10 :12 ])
238+ f .parser .send (bytes [12 :])
239+ self .assertEqual (f .unmask (f .body ), body )
240+
241+ def test_frame_sized_126 (self ):
242+ body = b'*' * 256
243+ bytes = Frame (opcode = OPCODE_TEXT , body = body , fin = 1 ).build ()
244+
245+ f = Frame ()
246+ # determine how the size is stored
247+ f .parser .send (bytes [:3 ])
248+ self .assertTrue (f .masking_key is None )
249+ # that's a large frame indeed
250+ self .assertEqual (f .payload_length , 126 )
251+
252+ # this will compute the actual application data size
253+ # it will also read the first byte of data
254+ # indeed the length is found from byte 3 to 10
255+ f .parser .send (bytes [3 :11 ])
256+ self .assertEqual (f .payload_length , 256 )
257+
258+ # parse the rest of our data
259+ f .parser .send (bytes [11 :])
260+ self .assertEqual (f .body , body )
261+
262+
263+ # The same but this time we provide enough
264+ # bytes so that the application's data length
265+ # can be computed from the first generator's send call
266+ f = Frame ()
267+ f .parser .send (bytes [:10 ])
268+ self .assertTrue (f .masking_key is None )
269+ self .assertEqual (f .payload_length , 256 )
270+
271+ # parse the rest of our data
272+ f .parser .send (bytes [10 :])
273+ self .assertEqual (f .body , body )
274+
196275if __name__ == '__main__' :
197276 suite = unittest .TestSuite ()
198277 loader = unittest .TestLoader ()
0 commit comments