def test__returns_server_identifier_if_included(self): server_ip = factory.make_ip_address(ipv6=False) packet = factory.make_dhcp_packet( include_server_identifier=True, server_ip=server_ip) dhcp = DHCP(packet) self.assertThat(dhcp.is_valid(), Equals(True)) self.assertThat(dhcp.server_identifier, Equals(IPAddress(server_ip)))
def test_is_valid_returns_false_for_truncated_option_value(self): packet = factory.make_dhcp_packet(truncated_option_value=True) dhcp = DHCP(packet) self.assertThat(dhcp.is_valid(), Equals(False)) self.assertThat( dhcp.invalid_reason, DocTestMatches("Truncated DHCP option value.") )
def test_is_valid_returns_false_for_invalid_cookie(self): packet = factory.make_dhcp_packet(bad_cookie=True) dhcp = DHCP(packet) self.assertThat(dhcp.is_valid(), Equals(False)) self.assertThat( dhcp.invalid_reason, DocTestMatches("Invalid DHCP cookie.") )
def test__is_valid_returns_false_for_truncated_option_length(self): packet = factory.make_dhcp_packet(truncated_option_length=True) dhcp = DHCP(packet) self.assertThat(dhcp.is_valid(), Equals(False)) self.assertThat( dhcp.invalid_reason, DocTestMatches("Truncated length field in DHCP option."), )
def test__server_identifier_none_if_not_included(self): packet = factory.make_dhcp_packet( include_server_identifier=False) dhcp = DHCP(packet) self.assertThat(dhcp.is_valid(), Equals(True)) self.assertThat(dhcp.server_identifier, Is(None))
def test__is_valid_return_true_for_valid_packet(self): packet = factory.make_dhcp_packet() dhcp = DHCP(packet) self.assertThat(dhcp.is_valid(), Equals(True))
def test__send_requests_and_await_replies(self): # This test is a bit large because it covers the entire functionality # of the `send_requests_and_await_replies()` method. (It could be # split apart into multiple tests, but the large amount of setup work # and interdependencies makes that a maintenance burden.) mock_socket = patch_socket(self) mock_socket.bind = mock.MagicMock() mock_socket.recvfrom = mock.MagicMock() mock_socket.setsockopt = mock.MagicMock() mock_socket.settimeout = mock.MagicMock() # Pretend we were successful at deferring the DHCP requests. self.patch_autospec(detect_module, "blockingCallFromThread") # This method normally blocks for ~10 seconds, so take control of the # monotonic clock and make sure the last call to `recvfrom()` happens # just as we hit the reply timeout. mock_time_monotonic = self.patch(detect_module.time.monotonic) mock_time_monotonic.side_effect = ( # Start time (before loop starts). 10, # First reply (truncated packet). 11, # Second reply (not a match to our transaction). 12, # Third reply (Matching reply with server identifier option). 13, # First socket timeout (need to make sure the loop continues). 14, # Second socket timeout (hey, we're done!). 10 + detect_module.REPLY_TIMEOUT, ) mock_xid = factory.make_bytes(4) valid_dhcp_reply = factory.make_dhcp_packet( transaction_id=mock_xid, include_server_identifier=True, server_ip="127.1.1.1", ) mock_get_xid = self.patch(detect_module.make_dhcp_transaction_id) mock_get_xid.return_value = mock_xid # Valid DHCP packet, but not a match because it doesn't have a # Server Identifier option. valid_non_match = DHCPDiscoverPacket(mac="01:02:03:04:05:06", transaction_id=mock_xid).packet mock_socket.recvfrom.side_effect = ( # Truncated packet, to test logging. (b"", ("127.0.0.1", BOOTP_SERVER_PORT)), (valid_non_match, ("127.0.0.2", BOOTP_SERVER_PORT)), (valid_dhcp_reply, ("127.0.0.3", BOOTP_SERVER_PORT)), socket.timeout, socket.timeout, ) logger = self.useFixture(TwistedLoggerFixture()) monitor = DHCPRequestMonitor("lo", Clock()) result = monitor.send_requests_and_await_replies() self.assertThat( mock_socket.setsockopt, MockCallsMatch( call(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1), call(socket.SOL_SOCKET, socket.SO_BROADCAST, 1), ), ) self.assertThat(mock_socket.bind, MockCallsMatch(call(("", 68)))) self.assertThat( mock_socket.settimeout, MockCallsMatch(call(detect_module.SOCKET_TIMEOUT)), ) self.assertThat( mock_socket.recvfrom, MockCallsMatch(call(2048), call(2048), call(2048), call(2048), call(2048)), ) # One of the response packets was truncated. self.assertThat( logger.output, DocTestMatches("Invalid DHCP response...Truncated..."), ) self.assertThat(result, HasLength(1)) # Ensure we record the fact that the reply packet came from a different # IP address than the server claimed to be. self.assertThat(result, Contains(DHCPServer("127.1.1.1", "127.0.0.3")))