@@ -278,6 +278,7 @@ def test_send_dhcp_message(
278
278
279
279
class TestParseDhcpMessage :
280
280
281
+ # Basic case, no extra fields, one each of router and DNS.
281
282
GOOD_DATA_01 = bytearray (
282
283
b"\x02 \x00 \x00 \x00 \xff \xff \xff \x7f \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xc0 "
283
284
b"\xa8 \x05 \x16 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x01 \x03 \x05 \x07 \t \x0b \x00 "
@@ -298,6 +299,7 @@ class TestParseDhcpMessage:
298
299
b"\xff \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
299
300
b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
300
301
)
302
+ # Complex case, extra field, 2 each router and DNS.
301
303
GOOD_DATA_02 = bytearray (
302
304
b"\x02 \x00 \x00 \x00 \x9a xV4\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x12 $@\n \x00 \x00 "
303
305
b"\x00 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
@@ -382,3 +384,44 @@ def test_parse_good_data(
382
384
assert dhcp_client ._lease_time == lease
383
385
assert dhcp_client ._t1 == t1
384
386
assert dhcp_client ._t2 == t2
387
+
388
+ BAD_DATA = bytearray (
389
+ b"\x02 \x00 \x00 \x00 \xff \xff \xff \x7f \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x12 $@\n \x00 \x00 "
390
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
391
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
392
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
393
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
394
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
395
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
396
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
397
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
398
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
399
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
400
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
401
+ b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 c\x82 Sc"
402
+ )
403
+
404
+ def test_parsing_failures (self , wiznet , wrench ):
405
+ # Test for bad OP code, ID mismatch, no server ID, bad Magic Cookie
406
+ dhcp_client = wiz_dhcp .DHCP (wiznet , (1 , 2 , 3 , 4 , 5 , 6 ))
407
+ dhcp_client ._sock = wrench .socket (type = wrench .SOCK_DGRAM )
408
+ dhcp_client ._sock .recv .return_value = self .BAD_DATA
409
+ # Transaction ID mismatch.
410
+ dhcp_client ._transaction_id = 0x42424242
411
+ dhcp_client ._initial_xid = dhcp_client ._transaction_id .to_bytes (4 , "little" )
412
+ assert dhcp_client .parse_dhcp_response () == (0 , 0 )
413
+ # Bad OP code.
414
+ self .BAD_DATA [0 ] = 0
415
+ dhcp_client ._transaction_id = 0x7FFFFFFF
416
+ dhcp_client ._initial_xid = dhcp_client ._transaction_id .to_bytes (4 , "little" )
417
+ with pytest .raises (AssertionError ):
418
+ # pylint: disable=expression-not-assigned
419
+ dhcp_client .parse_dhcp_response () == (0 , 0 )
420
+ self .BAD_DATA [0 ] = 2 # Reset to good value
421
+ # No server ID.
422
+ self .BAD_DATA [28 :34 ] = (0 , 0 , 0 , 0 , 0 , 0 )
423
+ assert dhcp_client .parse_dhcp_response () == (0 , 0 )
424
+ self .BAD_DATA [28 :34 ] = (1 , 1 , 1 , 1 , 1 , 1 ) # Reset to good value
425
+ # Bad Magic Cookie.
426
+ self .BAD_DATA [236 ] = 0
427
+ assert dhcp_client .parse_dhcp_response () == (0 , 0 )
0 commit comments