예제 #1
0
async def test_dhcp_renewal_match_hostname_and_macaddress(hass):
    """Test renewal matching based on hostname and macaddress."""
    dhcp_watcher = dhcp.DHCPWatcher(
        hass,
        {},
        [{
            "domain": "mock-domain",
            "hostname": "irobot-*",
            "macaddress": "501479*"
        }],
    )

    packet = Ether(RAW_DHCP_RENEWAL)

    with patch.object(hass.config_entries.flow, "async_init") as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)
        # Ensure no change is ignored
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 1
    assert mock_init.mock_calls[0][1][0] == "mock-domain"
    assert mock_init.mock_calls[0][2]["context"] == {
        "source": config_entries.SOURCE_DHCP
    }
    assert mock_init.mock_calls[0][2]["data"] == {
        dhcp.IP_ADDRESS: "192.168.1.120",
        dhcp.HOSTNAME: "irobot-ae9ec12dd3b04885bcbfa36afb01e1cc",
        dhcp.MAC_ADDRESS: "50147903852c",
    }
예제 #2
0
async def test_dhcp_match_hostname_and_macaddress(hass):
    """Test matching based on hostname and macaddress."""
    dhcp_watcher = dhcp.DHCPWatcher(
        hass,
        {},
        [{
            "domain": "mock-domain",
            "hostname": "connect",
            "macaddress": "B8B7F1*"
        }],
    )

    packet = Ether(RAW_DHCP_REQUEST)

    with patch.object(hass.config_entries.flow,
                      "async_init",
                      return_value=mock_coro()) as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)
        # Ensure no change is ignored
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 1
    assert mock_init.mock_calls[0][1][0] == "mock-domain"
    assert mock_init.mock_calls[0][2]["context"] == {"source": "dhcp"}
    assert mock_init.mock_calls[0][2]["data"] == {
        dhcp.IP_ADDRESS: "192.168.210.56",
        dhcp.HOSTNAME: "connect",
        dhcp.MAC_ADDRESS: "b8b7f16db533",
    }
예제 #3
0
async def test_dhcp_nomatch_non_dhcp_packet(hass):
    """Test matching does not throw on a non-dhcp packet."""
    dhcp_watcher = dhcp.DHCPWatcher(hass, {}, [{
        "domain": "mock-domain",
        "hostname": "nomatch*"
    }])

    packet = Ether(b"")

    with patch.object(hass.config_entries.flow, "async_init") as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 0
예제 #4
0
async def test_dhcp_nomatch_hostname(hass):
    """Test not matching based on hostname only."""
    dhcp_watcher = dhcp.DHCPWatcher(hass, {}, [{
        "domain": "mock-domain",
        "hostname": "nomatch*"
    }])

    packet = Ether(RAW_DHCP_REQUEST)

    with patch.object(hass.config_entries.flow, "async_init") as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 0
예제 #5
0
async def test_dhcp_nomatch(hass):
    """Test not matching based on macaddress only."""
    dhcp_watcher = dhcp.DHCPWatcher(
        hass, {}, [{"domain": "mock-domain", "macaddress": "ABC123*"}]
    )

    packet = Ether(RAW_DHCP_REQUEST)

    with patch.object(
        hass.config_entries.flow, "async_init", return_value=mock_coro()
    ) as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 0
예제 #6
0
async def _async_get_handle_dhcp_packet(hass, integration_matchers):
    dhcp_watcher = dhcp.DHCPWatcher(
        hass,
        {},
        integration_matchers,
    )
    handle_dhcp_packet = None

    def _mock_sniffer(*args, **kwargs):
        nonlocal handle_dhcp_packet
        handle_dhcp_packet = kwargs["prn"]
        return MagicMock()

    with patch("homeassistant.components.dhcp._verify_l2socket_setup",
               ), patch("scapy.arch.common.compile_filter"), patch(
                   "scapy.sendrecv.AsyncSniffer", _mock_sniffer):
        await dhcp_watcher.async_start()

    return handle_dhcp_packet
예제 #7
0
async def test_dhcp_match_hostname(hass):
    """Test matching based on hostname only."""
    dhcp_watcher = dhcp.DHCPWatcher(hass, {}, [{
        "domain": "mock-domain",
        "hostname": "connect"
    }])

    packet = Ether(RAW_DHCP_REQUEST)

    with patch.object(hass.config_entries.flow, "async_init") as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 1
    assert mock_init.mock_calls[0][1][0] == "mock-domain"
    assert mock_init.mock_calls[0][2]["context"] == {"source": "dhcp"}
    assert mock_init.mock_calls[0][2]["data"] == {
        dhcp.IP_ADDRESS: "192.168.210.56",
        dhcp.HOSTNAME: "connect",
        dhcp.MAC_ADDRESS: "b8b7f16db533",
    }
예제 #8
0
async def test_dhcp_invalid_option(hass):
    """Test we ignore invalid hostname option."""
    dhcp_watcher = dhcp.DHCPWatcher(hass, {}, [{
        "domain": "mock-domain",
        "hostname": "nomatch*"
    }])

    packet = Ether(RAW_DHCP_REQUEST)

    packet[DHCP].options = [
        ("message-type", 3),
        ("max_dhcp_size", 1500),
        ("requested_addr", "192.168.208.55"),
        ("server_id", "192.168.208.1"),
        ("param_req_list", [1, 3, 28, 6]),
        ("hostname"),
    ]

    with patch.object(hass.config_entries.flow, "async_init") as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 0
예제 #9
0
async def test_dhcp_nomatch_non_dhcp_request_packet(hass):
    """Test nothing happens with the wrong message-type."""
    dhcp_watcher = dhcp.DHCPWatcher(hass, {}, [{
        "domain": "mock-domain",
        "hostname": "nomatch*"
    }])

    packet = Ether(RAW_DHCP_REQUEST)

    packet[DHCP].options = [
        ("message-type", 4),
        ("max_dhcp_size", 1500),
        ("requested_addr", "192.168.210.56"),
        ("server_id", "192.168.208.1"),
        ("param_req_list", [1, 3, 28, 6]),
        ("hostname", b"connect"),
    ]

    with patch.object(hass.config_entries.flow, "async_init") as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 0
예제 #10
0
async def test_dhcp_match_macaddress_without_hostname(hass):
    """Test matching based on macaddress only."""
    dhcp_watcher = dhcp.DHCPWatcher(hass, {}, [{
        "domain": "mock-domain",
        "macaddress": "606BBD*"
    }])

    packet = Ether(RAW_DHCP_REQUEST_WITHOUT_HOSTNAME)

    with patch.object(hass.config_entries.flow, "async_init") as mock_init:
        dhcp_watcher.handle_dhcp_packet(packet)

    assert len(mock_init.mock_calls) == 1
    assert mock_init.mock_calls[0][1][0] == "mock-domain"
    assert mock_init.mock_calls[0][2]["context"] == {
        "source": config_entries.SOURCE_DHCP
    }
    assert mock_init.mock_calls[0][2]["data"] == {
        dhcp.IP_ADDRESS: "192.168.107.151",
        dhcp.HOSTNAME: "",
        dhcp.MAC_ADDRESS: "606bbd59e4b4",
    }