@@ -60,7 +60,7 @@ def test_constants(self):
60
60
assert wiz_dhcp .DHCP_HLENETHERNET == const (0x06 )
61
61
assert wiz_dhcp .DHCP_HOPS == const (0x00 )
62
62
63
- assert wiz_dhcp .MAGIC_COOKIE == const ( 0x63825363 )
63
+ assert wiz_dhcp .MAGIC_COOKIE == b"c \x82 Sc"
64
64
assert wiz_dhcp .MAX_DHCP_OPT == const (0x10 )
65
65
66
66
# Default DHCP Server port
@@ -274,3 +274,111 @@ def test_send_dhcp_message(
274
274
dhcp_client .send_dhcp_message (state , time_elapsed , renew = renew )
275
275
dhcp_client ._sock .send .assert_called_once_with (result )
276
276
assert len (wiz_dhcp ._BUFF ) == 318
277
+
278
+
279
+ class TestParseDhcpMessage :
280
+
281
+ GOOD_DATA_01 = bytearray (
282
+ b"\x02 \x00 \x00 \x00 \xff \xff \xff \x7f \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xc0 "
283
+ b"\xa8 \x05 \x16 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x01 \x03 \x05 \x07 \t \x0b \x00 "
284
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
285
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
286
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
287
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
288
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
289
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
290
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
291
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
292
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
293
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
294
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
295
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 c\x82 Sc5\x01 "
296
+ b"\x02 \x01 \x04 \xc0 \xa8 \x06 \x02 6\x04 \xea o\xde {3\x04 \x00 \x01 \x01 \x00 \x03 "
297
+ b'\x04 yy\x04 \x05 \x06 \x04 \x05 \x06 \x07 \x08 :\x04 \x00 ""\x00 ;\x04 \x00 33\x00 '
298
+ b"\xff \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
299
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
300
+ )
301
+ GOOD_DATA_02 = bytearray (
302
+ b"\x02 \x00 \x00 \x00 \x9a xV4\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x12 $@\n \x00 \x00 "
303
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
304
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
305
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
306
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
307
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
308
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
309
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
310
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
311
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
312
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
313
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
314
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 c\x82 Sc5"
315
+ b"\x01 \x05 <\x05 \x01 \x02 \x03 \x04 \x05 \x01 \x04 \n \x0b \x07 \xde 6\x04 zN\x91 \x03 \x03 "
316
+ b"\x08 \n \x0b \x0e \x0f \xff \x00 \xff \x00 \x06 \x08 \x13 \x11 \x0b \x07 ****3\x04 \x00 \x00 "
317
+ b"=;:\x04 \x00 \x0e \x17 @;\x04 \x02 \x92 ]\xde \xff \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
318
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
319
+ )
320
+
321
+ @pytest .mark .parametrize (
322
+ "xid, local_ip, msg_type, subnet, dhcp_ip, gate_ip, dns_ip, lease, t1, t2, response" ,
323
+ (
324
+ (
325
+ 0x7FFFFFFF ,
326
+ (192 , 168 , 5 , 22 ),
327
+ 2 ,
328
+ (192 , 168 , 6 , 2 ),
329
+ (234 , 111 , 222 , 123 ),
330
+ (121 , 121 , 4 , 5 ),
331
+ (5 , 6 , 7 , 8 ),
332
+ 65792 ,
333
+ 2236928 ,
334
+ 3355392 ,
335
+ GOOD_DATA_01 ,
336
+ ),
337
+ (
338
+ 0x3456789A ,
339
+ (18 , 36 , 64 , 10 ),
340
+ 5 ,
341
+ (10 , 11 , 7 , 222 ),
342
+ (122 , 78 , 145 , 3 ),
343
+ (10 , 11 , 14 , 15 ),
344
+ (19 , 17 , 11 , 7 ),
345
+ 15675 ,
346
+ 923456 ,
347
+ 43146718 ,
348
+ GOOD_DATA_02 ,
349
+ ),
350
+ ),
351
+ )
352
+ # pylint: disable=too-many-locals
353
+ def test_parse_good_data (
354
+ self ,
355
+ wiznet ,
356
+ wrench ,
357
+ xid ,
358
+ local_ip ,
359
+ msg_type ,
360
+ subnet ,
361
+ dhcp_ip ,
362
+ gate_ip ,
363
+ dns_ip ,
364
+ lease ,
365
+ t1 ,
366
+ t2 ,
367
+ response ,
368
+ ):
369
+ dhcp_client = wiz_dhcp .DHCP (wiznet , (1 , 2 , 3 , 4 , 5 , 6 ))
370
+ dhcp_client ._sock = wrench .socket (type = wrench .SOCK_DGRAM )
371
+ dhcp_client ._transaction_id = xid
372
+ dhcp_client ._initial_xid = dhcp_client ._transaction_id .to_bytes (4 , "little" )
373
+ dhcp_client ._sock .recv .return_value = response
374
+ response_type , response_id = dhcp_client .parse_dhcp_response ()
375
+ assert response_type == msg_type
376
+ assert response_id == bytearray (xid .to_bytes (4 , "little" ))
377
+ assert dhcp_client .local_ip == local_ip
378
+ assert dhcp_client .subnet_mask == subnet
379
+ assert dhcp_client .dhcp_server_ip == dhcp_ip
380
+ assert dhcp_client .gateway_ip == gate_ip
381
+ assert dhcp_client .dns_server_ip == dns_ip
382
+ assert dhcp_client ._lease_time == lease
383
+ assert dhcp_client ._t1 == t1
384
+ assert dhcp_client ._t2 == t2
0 commit comments