Skip to content

Commit caa5136

Browse files
author
BiffoBear
committed
Altered the _dhcp_state_machine to handle exceptions raised by send_dhcp_message. Added pytest to optional_requirements.txt.
1 parent 449010a commit caa5136

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ def _dhcp_state_machine(self) -> None:
421421
if self._sock.available():
422422
if self._debug:
423423
print("* DHCP: Parsing OFFER")
424-
msg_type, xid = self.parse_dhcp_response()
424+
try:
425+
msg_type, xid = self.parse_dhcp_response()
426+
except ValueError as error:
427+
if self._debug:
428+
print(error)
425429
if msg_type == DHCP_OFFER:
426430
# Check if transaction ID matches, otherwise it may be an offer
427431
# for another device
@@ -446,7 +450,11 @@ def _dhcp_state_machine(self) -> None:
446450
if self._sock.available():
447451
if self._debug:
448452
print("* DHCP: Parsing ACK")
449-
msg_type, xid = self.parse_dhcp_response()
453+
try:
454+
msg_type, xid = self.parse_dhcp_response()
455+
except ValueError as error:
456+
if self._debug:
457+
print(error)
450458
# Check if transaction ID matches, otherwise it may be
451459
# for another device
452460
if htonl(self._transaction_id) == int.from_bytes(xid, "big"):

optional_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
4+
pytest

tests/test_dhcp.py

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
#
33
# SPDX-License-Identifier: MIT
44
"""Tests to confirm that there are no changes in behaviour to public methods and functions."""
5-
65
# pylint: disable=no-self-use, redefined-outer-name, protected-access, invalid-name, too-many-arguments
76
import pytest
8-
97
from micropython import const
108
import adafruit_wiznet5k.adafruit_wiznet5k_dhcp as wiz_dhcp
119

@@ -277,7 +275,6 @@ def test_send_dhcp_message(
277275

278276

279277
class TestParseDhcpMessage:
280-
281278
# Basic case, no extra fields, one each of router and DNS.
282279
GOOD_DATA_01 = bytearray(
283280
b"\x02\x00\x00\x00\xff\xff\xff\x7f\x00\x00\x00\x00\x00\x00\x00\x00\xc0"
@@ -427,3 +424,89 @@ def test_parsing_failures(self, wiznet, wrench):
427424
self.BAD_DATA[236] = 0
428425
with pytest.raises(ValueError):
429426
dhcp_client.parse_dhcp_response()
427+
428+
429+
class TestStateMachine:
430+
@pytest.mark.parametrize(
431+
"dhcp_state, socket_state",
432+
(
433+
(wiz_dhcp.STATE_DHCP_START, "Socket"),
434+
(wiz_dhcp.STATE_DHCP_DISCOVER, None),
435+
(wiz_dhcp.STATE_DHCP_REQUEST, None),
436+
(wiz_dhcp.STATE_DHCP_LEASED, None),
437+
(wiz_dhcp.STATE_DHCP_REREQUEST, None),
438+
(wiz_dhcp.STATE_DHCP_RELEASE, None),
439+
(wiz_dhcp.STATE_DHCP_WAIT, None),
440+
),
441+
)
442+
def test_link_is_down_state_not_disconnected(
443+
self, mocker, wiznet, dhcp_state, socket_state
444+
):
445+
dhcp_client = wiz_dhcp.DHCP(wiznet, (1, 2, 3, 4, 5, 6))
446+
dhcp_client._eth.link_status = False
447+
dhcp_client._eth.ifconfig = (
448+
(1, 1, 1, 1),
449+
(1, 1, 1, 1),
450+
(1, 1, 1, 1),
451+
(1, 1, 1, 1),
452+
)
453+
dhcp_client._last_lease_time = 1
454+
dhcp_client.dhcp_server_ip = (192, 234, 1, 75)
455+
dhcp_client._dhcp_state = dhcp_state
456+
# If a socket exists, close() will be called, so add a Mock.
457+
if socket_state is not None:
458+
dhcp_client._sock = mocker.MagicMock()
459+
else:
460+
dhcp_client._dhcp_state = None
461+
# Test.
462+
dhcp_client._dhcp_state_machine()
463+
# DHCP state machine in correct state.
464+
assert dhcp_client._dhcp_state == wiz_dhcp.STATE_DHCP_DISCONN
465+
# Check that configurations are returned to defaults.
466+
assert dhcp_client._eth.ifconfig == (
467+
(0, 0, 0, 0),
468+
(0, 0, 0, 0),
469+
(0, 0, 0, 0),
470+
(0, 0, 0, 0),
471+
)
472+
assert dhcp_client._last_lease_time == 0
473+
assert dhcp_client.dhcp_server_ip == wiz_dhcp.BROADCAST_SERVER_ADDR
474+
assert dhcp_client._sock is None
475+
476+
def test_link_is_down_state_disconnected(self, wiznet):
477+
dhcp_client = wiz_dhcp.DHCP(wiznet, (1, 2, 3, 4, 5, 6))
478+
dhcp_client._eth.link_status = False
479+
dhcp_client._eth.ifconfig = (
480+
(1, 1, 1, 1),
481+
(1, 1, 1, 1),
482+
(1, 1, 1, 1),
483+
(1, 1, 1, 1),
484+
)
485+
dhcp_client._last_lease_time = 1
486+
dhcp_client.dhcp_server_ip = (192, 234, 1, 75)
487+
dhcp_client._sock = "socket"
488+
dhcp_client._dhcp_state = wiz_dhcp.STATE_DHCP_DISCONN
489+
# Test.
490+
dhcp_client._dhcp_state_machine()
491+
# DHCP state machine in correct state.
492+
assert dhcp_client._dhcp_state == wiz_dhcp.STATE_DHCP_DISCONN
493+
# Check that configurations are not altered because state has not changed.
494+
assert dhcp_client._eth.ifconfig == (
495+
(1, 1, 1, 1),
496+
(1, 1, 1, 1),
497+
(1, 1, 1, 1),
498+
(1, 1, 1, 1),
499+
)
500+
assert dhcp_client._last_lease_time == 1
501+
assert dhcp_client.dhcp_server_ip == (192, 234, 1, 75)
502+
assert dhcp_client._sock == "socket"
503+
504+
def test_link_is_up_state_disconnected(self, wiznet, wrench):
505+
dhcp_client = wiz_dhcp.DHCP(wiznet, (1, 2, 3, 4, 5, 6))
506+
wrench.socket.side_effect = [RuntimeError]
507+
dhcp_client._eth.link_status = True
508+
dhcp_client._dhcp_state = wiz_dhcp.STATE_DHCP_DISCONN
509+
# Test.
510+
dhcp_client._dhcp_state_machine()
511+
# Assume state is set to START then becomes WAIT after START fails to set a socket
512+
assert dhcp_client._dhcp_state == wiz_dhcp.STATE_DHCP_WAIT

0 commit comments

Comments
 (0)