Skip to content

Commit 449010a

Browse files
author
BiffoBear
committed
Refactored parse_dhcp_response to raise ValueError instead of returning 0, 0 when a problem occurs.
1 parent 2161e7f commit 449010a

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ def send_dhcp_message(
259259
# pylint: disable=too-many-branches, too-many-statements
260260
def parse_dhcp_response(
261261
self,
262-
) -> Union[Tuple[int, bytearray], Tuple[int, int]]:
262+
) -> Tuple[int, bytearray]:
263263
"""Parse DHCP response from DHCP server.
264264
265-
:return Union[Tuple[int, bytearray], Tuple[int, int]]: DHCP packet type.
265+
:return Tuple[int, bytearray]: DHCP packet type and ID.
266266
"""
267267
# store packet in buffer
268268
_BUFF = self._sock.recv()
@@ -271,23 +271,20 @@ def parse_dhcp_response(
271271

272272
# -- Parse Packet, FIXED -- #
273273
# Validate OP
274-
assert (
275-
_BUFF[0] == DHCP_BOOT_REPLY
276-
), "Malformed Packet - \
277-
DHCP message OP is not expected BOOT Reply."
274+
if _BUFF[0] != DHCP_BOOT_REPLY:
275+
raise ValueError("DHCP message OP is not expected BOOTP Reply.")
278276

279277
xid = _BUFF[4:8]
280278
if bytes(xid) != self._initial_xid:
281-
print("f")
282-
return 0, 0
279+
raise ValueError("DHCP response ID mismatch.")
283280

284281
self.local_ip = tuple(_BUFF[16:20])
285282
# Check that there is a server ID.
286283
if _BUFF[28:34] == b"\x00\x00\x00\x00\x00\x00":
287-
return 0, 0
284+
raise ValueError("No DHCP server ID in the response.")
288285

289286
if _BUFF[236:240] != MAGIC_COOKIE:
290-
return 0, 0
287+
raise ValueError("No DHCP Magic Cookie in the response.")
291288

292289
# -- Parse Packet, VARIABLE -- #
293290
ptr = 240

tests/test_dhcp.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,21 @@ def test_parsing_failures(self, wiznet, wrench):
409409
# Transaction ID mismatch.
410410
dhcp_client._transaction_id = 0x42424242
411411
dhcp_client._initial_xid = dhcp_client._transaction_id.to_bytes(4, "little")
412-
assert dhcp_client.parse_dhcp_response() == (0, 0)
412+
with pytest.raises(ValueError):
413+
dhcp_client.parse_dhcp_response()
413414
# Bad OP code.
414415
self.BAD_DATA[0] = 0
415416
dhcp_client._transaction_id = 0x7FFFFFFF
416417
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)
418+
with pytest.raises(ValueError):
419+
dhcp_client.parse_dhcp_response()
420420
self.BAD_DATA[0] = 2 # Reset to good value
421421
# No server ID.
422422
self.BAD_DATA[28:34] = (0, 0, 0, 0, 0, 0)
423-
assert dhcp_client.parse_dhcp_response() == (0, 0)
423+
with pytest.raises(ValueError):
424+
dhcp_client.parse_dhcp_response()
424425
self.BAD_DATA[28:34] = (1, 1, 1, 1, 1, 1) # Reset to good value
425426
# Bad Magic Cookie.
426427
self.BAD_DATA[236] = 0
427-
assert dhcp_client.parse_dhcp_response() == (0, 0)
428+
with pytest.raises(ValueError):
429+
dhcp_client.parse_dhcp_response()

0 commit comments

Comments
 (0)