Skip to content

Commit 2161e7f

Browse files
author
BiffoBear
committed
Added tested for parse_dhcp_response failures. Fixed the logic for checking server ID.
1 parent 6469eac commit 2161e7f

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,13 @@ def parse_dhcp_response(
277277
DHCP message OP is not expected BOOT Reply."
278278

279279
xid = _BUFF[4:8]
280-
if bytes(xid) < self._initial_xid:
280+
if bytes(xid) != self._initial_xid:
281281
print("f")
282282
return 0, 0
283283

284284
self.local_ip = tuple(_BUFF[16:20])
285-
if _BUFF[28:34] == 0:
285+
# Check that there is a server ID.
286+
if _BUFF[28:34] == b"\x00\x00\x00\x00\x00\x00":
286287
return 0, 0
287288

288289
if _BUFF[236:240] != MAGIC_COOKIE:

tests/test_dhcp.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def test_send_dhcp_message(
278278

279279
class TestParseDhcpMessage:
280280

281+
# Basic case, no extra fields, one each of router and DNS.
281282
GOOD_DATA_01 = bytearray(
282283
b"\x02\x00\x00\x00\xff\xff\xff\x7f\x00\x00\x00\x00\x00\x00\x00\x00\xc0"
283284
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:
298299
b"\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
299300
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
300301
)
302+
# Complex case, extra field, 2 each router and DNS.
301303
GOOD_DATA_02 = bytearray(
302304
b"\x02\x00\x00\x00\x9axV4\x00\x00\x00\x00\x00\x00\x00\x00\x12$@\n\x00\x00"
303305
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(
382384
assert dhcp_client._lease_time == lease
383385
assert dhcp_client._t1 == t1
384386
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\x00c\x82Sc"
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

Comments
 (0)