-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- coding: utf-8 -*- | ||
# vim:ts=4:sw=4:expandtab | ||
"""""" | ||
import pytest | ||
|
||
from dhcpcanon.dhcpcanon import DHCPCAnon | ||
|
||
|
||
@pytest.fixture | ||
def dhcpcanon_maker(request): | ||
""" return a function which creates initialized dhcpcanon instances. """ | ||
|
||
def maker(): | ||
dhcpcanon = DHCPCAnon("lo", "127.0.0.1", "127.0.0.1") | ||
dhcpcanon.parse_args(iface='lo', | ||
server_port=8000, client_port=8001, | ||
# client_ip='127.0.0.1', | ||
server_ip='127.0.0.1', | ||
# server_mac="00:01:02:03:04:05", | ||
client_mac="00:0a:0b:0c:0d:0f") | ||
return dhcpcanon | ||
return maker | ||
|
||
|
||
@pytest.fixture | ||
def dhcpcanon(dhcpcanon_maker): | ||
""" return an initialized dhcpcanon instance. """ | ||
return dhcpcanon_maker() | ||
|
||
|
||
@pytest.fixture() | ||
def datadir(request): | ||
""" get, read, open test files from the "data" directory. """ | ||
class D: | ||
def __init__(self, basepath): | ||
self.basepath = basepath | ||
|
||
def open(self, name, mode="r"): | ||
return self.basepath.join(name).open(mode) | ||
|
||
def join(self, name): | ||
return self.basepath.join(name).strpath | ||
|
||
def read_bytes(self, name): | ||
with self.open(name, "rb") as f: | ||
return f.read() | ||
|
||
def read(self, name): | ||
with self.open(name, "r") as f: | ||
return f.read() | ||
|
||
return D(request.fspath.dirpath("data")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
# vim:ts=4:sw=4:expandtab | ||
"""""" | ||
import logging | ||
from scapy.all import Ether, IP, UDP, BOOTP, DHCP | ||
|
||
FORMAT = "%(levelname)s: %(filename)s:%(lineno)s - %(funcName)s - " + \ | ||
"%(message)s" | ||
logging.basicConfig(format=FORMAT, level=logging.DEBUG) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TestDHCPCAnon: | ||
|
||
def test_intialize(self, dhcpcanon): | ||
assert dhcpcanon.client_ip == '0.0.0.0' | ||
# TODO: assert more | ||
|
||
def test_gen_discover(self, dhcpcanon, datadir): | ||
dhcp_discover = ( | ||
Ether(src="00:0a:0b:0c:0d:0f", dst="ff:ff:ff:ff:ff:ff") / | ||
IP(src="0.0.0.0", dst="255.255.255.255") / | ||
UDP(sport=8001, dport=8000) / | ||
BOOTP(chaddr=['\x00\n\x0b\x0c\r\x0f'], options='c\x82Sc') / | ||
DHCP(options=[ | ||
('message-type', 'discover'), | ||
'end'] | ||
) | ||
) | ||
discover = dhcpcanon.gen_discover() | ||
assert discover == dhcp_discover | ||
|
||
def test_parse_offer(self, dhcpcanon, datadir): | ||
dhcp_offer = ( | ||
Ether(src="00:01:02:03:04:05", dst="00:0a:0b:0c:0d:0f") / | ||
IP(src="0.0.0.0", dst="127.0.0.1") / | ||
UDP(sport=8000, dport=8001) / | ||
BOOTP(op=2, yiaddr="127.0.0.1", siaddr="127.0.0.1", | ||
giaddr='0.0.0.0', xid=1234) / | ||
DHCP(options=[ | ||
('message-type', 'offer'), | ||
('subnet_mask', "255.0.0.0"), | ||
('server_id', "127.0.0.1"), | ||
('lease_time', 1800), | ||
('domain', "localnet"), | ||
('name_server', "127.0.0.1"), | ||
'end'] | ||
) | ||
) | ||
dhcpcanon.parse_Offer(dhcp_offer) | ||
assert dhcpcanon.client_ip_offered == '127.0.0.1' |