-
Notifications
You must be signed in to change notification settings - Fork 590
/
test_dhcp_helper_cli.py
132 lines (104 loc) · 4.69 KB
/
test_dhcp_helper_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Copyright (C) 2022 The Magma Authors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; If not, see <http://www.gnu.org/licenses/>.
"""
from typing import Optional
from unittest.mock import patch
import pytest
from dhcp_helper_cli.dhcp_helper_cli import (
DhcpHelperCli,
DHCPState,
MacAddress,
release_arg_handler,
)
from scapy.layers.dhcp import BOOTP, DHCP
from scapy.layers.l2 import Dot1Q, Ether
MACSTRING = "12:34:56:78:90:AB"
MAC = MacAddress(MACSTRING)
VLAN = 0
IFACE = "dhcp0"
IPv4 = 0x0800
DHCP_OFFER_PKT = Ether(src="00:00:00:00:00:00", dst=MAC)
DHCP_OFFER_PKT /= BOOTP(yiaddr="1.2.3.4", chaddr=MAC.as_hex())
DHCP_OFFER_PKT /= DHCP(options=[("message-type", DHCPState.OFFER), "end"])
DHCP_ACK_PKT = Ether(src="00:00:00:00:00:00", dst=MAC)
DHCP_ACK_PKT /= BOOTP(yiaddr="1.2.3.4", chaddr=MAC.as_hex())
DHCP_ACK_PKT /= DHCP(options=[("message-type", DHCPState.ACK), "end"])
DHCP_RELEASE_PKT = Ether(src="00:00:00:00:00:00", dst=MAC)
DHCP_RELEASE_PKT /= BOOTP(yiaddr="1.2.3.4", chaddr=MAC.as_hex(), ciaddr="4.5.6.7")
DHCP_RELEASE_PKT /= DHCP(options=[("message-type", DHCPState.RELEASE), "end"])
@pytest.fixture()
def dhcp_helper_cli_fixture(mac: MacAddress = MAC, vlan: int = VLAN, iface=IFACE, ip: Optional[str] = None, server_ip: Optional[str] = None):
with patch("dhcp_helper_cli.dhcp_helper_cli.AsyncSniffer"):
return DhcpHelperCli(mac, vlan, iface, ip, server_ip)
@patch("dhcp_helper_cli.dhcp_helper_cli.sendp")
def test_send_dhcp_discover(sendp_mock, dhcp_helper_cli_fixture):
dhcp_helper_cli_fixture.send_dhcp_discover()
pkt = sendp_mock.call_args[0][0]
assert pkt[Ether].type == IPv4
assert pkt[Ether].src == MACSTRING.lower()
assert Dot1Q not in pkt
assert ('message-type', 'discover') in pkt[DHCP].options
assert dhcp_helper_cli_fixture._state == DHCPState.DISCOVER
@patch("dhcp_helper_cli.dhcp_helper_cli.sendp")
def test_send_dhcp_request(sendp_mock, dhcp_helper_cli_fixture):
dhcp_helper_cli_fixture._state = DHCPState.OFFER
dhcp_helper_cli_fixture.send_dhcp_request()
pkt = sendp_mock.call_args[0][0]
assert pkt[Ether].type == IPv4
assert pkt[Ether].src == MACSTRING.lower()
assert Dot1Q not in pkt
assert ('message-type', 'request') in pkt[DHCP].options
assert dhcp_helper_cli_fixture._state == DHCPState.REQUEST
@patch("dhcp_helper_cli.dhcp_helper_cli.sendp")
def test_send_dhcp_release(sendp_mock, dhcp_helper_cli_fixture):
dhcp_helper_cli_fixture._state = DHCPState.ACK
dhcp_helper_cli_fixture.send_dhcp_release()
pkt = sendp_mock.call_args[0][0]
assert pkt[Ether].type == IPv4
assert pkt[Ether].src == MACSTRING.lower()
assert Dot1Q not in pkt
assert ('message-type', 'release') in pkt[DHCP].options
assert dhcp_helper_cli_fixture._state == DHCPState.RELEASE
def create_send_dhcp_pkt_mock(dhcp_helper_cli_fixture):
def mocked_send_dhcp_pkt(dhcp_opts, ciaddr: Optional[str] = None) -> None:
if ('message-type', 'discover') in dhcp_opts:
dhcp_helper_cli_fixture._pkt_queue.put(DHCP_OFFER_PKT)
if ('message-type', 'request') in dhcp_opts:
dhcp_helper_cli_fixture._pkt_queue.put(DHCP_ACK_PKT)
if ('message-type', 'release') in dhcp_opts:
dhcp_helper_cli_fixture._pkt_queue.put(DHCP_RELEASE_PKT)
return mocked_send_dhcp_pkt
def test_allocate(dhcp_helper_cli_fixture):
with patch.object(
dhcp_helper_cli_fixture,
"send_dhcp_pkt",
create_send_dhcp_pkt_mock(dhcp_helper_cli_fixture),
):
dhcp_helper_cli_fixture.allocate()
assert dhcp_helper_cli_fixture._state == DHCPState.ACK
def test_release(dhcp_helper_cli_fixture):
with patch.object(
dhcp_helper_cli_fixture,
"send_dhcp_pkt",
create_send_dhcp_pkt_mock(dhcp_helper_cli_fixture),
):
dhcp_helper_cli_fixture.release()
assert dhcp_helper_cli_fixture._state == DHCPState.RELEASE
def test_renew(dhcp_helper_cli_fixture):
with patch.object(
dhcp_helper_cli_fixture,
"send_dhcp_pkt",
create_send_dhcp_pkt_mock(dhcp_helper_cli_fixture),
):
dhcp_helper_cli_fixture.renew()
assert dhcp_helper_cli_fixture._state == DHCPState.ACK