slightly improved network functionality and added tests for network
This commit is contained in:
@@ -1,15 +1,16 @@
|
|||||||
import ipaddress
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
import ipaddress
|
||||||
|
|
||||||
|
|
||||||
class MinerNetworkRange:
|
class MinerNetworkRange:
|
||||||
"""A MinerNetwork that takes a range of IP addresses.
|
"""A MinerNetwork that takes a range of IP addresses.
|
||||||
|
|
||||||
:param ip_range: A range of IP addresses to put in the network, or a list of IPs
|
:param ip_range: A range of IP addresses to put in the network, or a list of IPs
|
||||||
Takes a string formatted as
|
Takes a string formatted as:
|
||||||
{ip_range_1_start}-{ip_range_1_end}, {ip_range_2_start}-{ip_range_2_end}
|
{ip_range_1_start}-{ip_range_1_end}, {ip_address_1},
|
||||||
Also takes a list of strings formatted as
|
{ip_range_2_start}-{ip_range_2_end}, {ip_address_2}...
|
||||||
[{ip1}, {ip2}, {ip3}, ...]
|
Also takes a list of strings formatted as:
|
||||||
|
[{ip_address_1}, {ip_address_2}, {ip_address_3}, ...]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, ip_range: Union[str, list]):
|
def __init__(self, ip_range: Union[str, list]):
|
||||||
@@ -17,17 +18,20 @@ class MinerNetworkRange:
|
|||||||
if isinstance(ip_range, str):
|
if isinstance(ip_range, str):
|
||||||
ip_ranges = ip_range.replace(" ", "").split(",")
|
ip_ranges = ip_range.replace(" ", "").split(",")
|
||||||
for item in ip_ranges:
|
for item in ip_ranges:
|
||||||
start, end = item.split("-")
|
if "-" in item:
|
||||||
start_ip = ipaddress.ip_address(start)
|
start, end = item.split("-")
|
||||||
end_ip = ipaddress.ip_address(end)
|
start_ip = ipaddress.ip_address(start)
|
||||||
networks = ipaddress.summarize_address_range(start_ip, end_ip)
|
end_ip = ipaddress.ip_address(end)
|
||||||
for network in networks:
|
networks = ipaddress.summarize_address_range(start_ip, end_ip)
|
||||||
self.host_ips.append(network.network_address)
|
for network in networks:
|
||||||
for host in network.hosts():
|
self.host_ips.append(network.network_address)
|
||||||
if host not in self.host_ips:
|
for host in network.hosts():
|
||||||
self.host_ips.append(host)
|
if host not in self.host_ips:
|
||||||
if network.broadcast_address not in self.host_ips:
|
self.host_ips.append(host)
|
||||||
self.host_ips.append(network.broadcast_address)
|
if network.broadcast_address not in self.host_ips:
|
||||||
|
self.host_ips.append(network.broadcast_address)
|
||||||
|
else:
|
||||||
|
self.host_ips.append(ipaddress.ip_address(item))
|
||||||
elif isinstance(ip_range, list):
|
elif isinstance(ip_range, list):
|
||||||
self.host_ips = [ipaddress.ip_address(ip_str) for ip_str in ip_range]
|
self.host_ips = [ipaddress.ip_address(ip_str) for ip_str in ip_range]
|
||||||
|
|
||||||
|
|||||||
9
tests/__init__.py
Normal file
9
tests/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from tests.network_tests import test_network
|
||||||
|
|
||||||
|
|
||||||
|
def test_all():
|
||||||
|
test_network()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_all()
|
||||||
70
tests/network_tests/__init__.py
Normal file
70
tests/network_tests/__init__.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import ipaddress
|
||||||
|
|
||||||
|
from network.net_range import MinerNetworkRange
|
||||||
|
from network import MinerNetwork
|
||||||
|
|
||||||
|
|
||||||
|
def test_network():
|
||||||
|
net_range_str = "192.168.1.29, 192.168.1.40-192.168.1.43, 192.168.1.60"
|
||||||
|
net_range_list = [
|
||||||
|
"192.168.1.29",
|
||||||
|
"192.168.1.40",
|
||||||
|
"192.168.1.41",
|
||||||
|
"192.168.1.42",
|
||||||
|
"192.168.1.43",
|
||||||
|
"192.168.1.60",
|
||||||
|
]
|
||||||
|
miner_net_1 = "192.168.1.0"
|
||||||
|
miner_net_1_mask = 29
|
||||||
|
|
||||||
|
#######################
|
||||||
|
# Network Range Tests #
|
||||||
|
#######################
|
||||||
|
|
||||||
|
net_range_1 = list(MinerNetworkRange(net_range_str).hosts())
|
||||||
|
|
||||||
|
net_range_2 = list(MinerNetworkRange(net_range_list).hosts())
|
||||||
|
|
||||||
|
correct_net_range = [
|
||||||
|
ipaddress.IPv4Address("192.168.1.29"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.40"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.41"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.42"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.43"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.60"),
|
||||||
|
]
|
||||||
|
|
||||||
|
assert net_range_1 == correct_net_range
|
||||||
|
assert net_range_2 == correct_net_range
|
||||||
|
|
||||||
|
print("Network Range test succeeded.")
|
||||||
|
|
||||||
|
#######################
|
||||||
|
# Miner Network Tests #
|
||||||
|
#######################
|
||||||
|
|
||||||
|
miner_net = list(
|
||||||
|
MinerNetwork(miner_net_1, mask=miner_net_1_mask).get_network().hosts()
|
||||||
|
)
|
||||||
|
|
||||||
|
miner_net_str = list(
|
||||||
|
MinerNetwork("192.168.1.1-192.168.1.5, 192.168.1.6").get_network().hosts()
|
||||||
|
)
|
||||||
|
|
||||||
|
correct_net = [
|
||||||
|
ipaddress.IPv4Address("192.168.1.1"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.2"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.3"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.4"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.5"),
|
||||||
|
ipaddress.IPv4Address("192.168.1.6"),
|
||||||
|
]
|
||||||
|
|
||||||
|
assert miner_net == correct_net
|
||||||
|
assert miner_net_str == correct_net
|
||||||
|
|
||||||
|
print("Miner Network test succeeded.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_network()
|
||||||
Reference in New Issue
Block a user