|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 | 4 | """Tests to confirm that there are no changes in behaviour to public methods and functions."""
|
5 |
| - |
6 | 5 | # pylint: disable=no-self-use, redefined-outer-name, protected-access, invalid-name, too-many-arguments
|
7 | 6 | import pytest
|
8 |
| - |
9 | 7 | from micropython import const
|
10 | 8 | import adafruit_wiznet5k.adafruit_wiznet5k_dhcp as wiz_dhcp
|
11 | 9 |
|
@@ -277,7 +275,6 @@ def test_send_dhcp_message(
|
277 | 275 |
|
278 | 276 |
|
279 | 277 | class TestParseDhcpMessage:
|
280 |
| - |
281 | 278 | # Basic case, no extra fields, one each of router and DNS.
|
282 | 279 | GOOD_DATA_01 = bytearray(
|
283 | 280 | 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):
|
427 | 424 | self.BAD_DATA[236] = 0
|
428 | 425 | with pytest.raises(ValueError):
|
429 | 426 | 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