Last active
February 25, 2017 23:44
-
-
Save dmendiza/ebf83852a7ed7e5b694b06308b46cace to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def apply_port_exclusions(include_ports, exclude_ports): | |
inc_ports = _reduced(include_ports) | |
ex_low, ex_high = exclude_ports | |
result = list() | |
for pair in inc_ports: | |
low, high = pair | |
pair_range = range(low, high + 1) | |
if high < ex_low or ex_high < low: | |
# this pair is not excluded | |
result.append(pair) | |
elif ex_low <= low and ex_high >= high: | |
# this pair is excluded | |
continue | |
if ex_low in pair_range: | |
if ex_low == low: | |
pass | |
else: | |
result.append([low, ex_low - 1]) | |
if ex_high in pair_range: | |
if ex_high == high: | |
pass | |
else: | |
result.append([ex_high + 1, high]) | |
return result | |
def _reduced(ports): | |
""" | |
Returns a new list of port low-high pairs reduced so that pairs that | |
are adjacent to each other are combined into a single pair. | |
i.e. _reduced([[a, b], [b + 1, c]]) returns [[a, c]] | |
The new list is sorted in ascending order. | |
""" | |
if ports: | |
reduced = list() | |
s_ports = sorted(ports) | |
reduced.append(s_ports.pop(0)) | |
for pair in s_ports: | |
low, high = pair | |
previous = reduced.pop() | |
plow, phigh = previous | |
if low == phigh + 1: | |
# pairs are adjacent | |
reduced.append([plow, high]) | |
else: | |
reduced.append(previous) | |
reduced.append(pair) | |
return reduced | |
else: | |
return ports |
This file contains hidden or 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
import unittest | |
import port_tools | |
class TestPortExclusions(unittest.TestCase): | |
def test_first_input(self): | |
include_ports = [[80, 80], [22, 23], [8000, 9000]] | |
exclude_ports = [8080, 8080] | |
result = port_tools.apply_port_exclusions( | |
include_ports, exclude_ports | |
) | |
self.assertEqual([[22, 23], [80, 80], [8000, 8079], [8081, 9000]], | |
result) | |
def test_second_input(self): | |
include_ports = [[8000, 9000], [80, 80], [22, 23]] | |
exclude_ports = [1024, 1024] | |
result = port_tools.apply_port_exclusions( | |
include_ports, exclude_ports | |
) | |
self.assertEqual([[22, 23], [80, 80], [8000, 9000]], result) | |
def test_third_input(self): | |
include_ports = [[1,65535]] | |
exclude_ports = [1000,2000] | |
result = port_tools.apply_port_exclusions( | |
include_ports, exclude_ports | |
) | |
self.assertEqual([[1, 999], [2001, 65535]], result) | |
def test_fourth_input(self): | |
include_ports = [[1,1], [3, 65535], [2, 2]] | |
exclude_ports = [500, 2500] | |
result = port_tools.apply_port_exclusions( | |
include_ports, exclude_ports | |
) | |
self.assertEqual([[1, 499], [2501, 65535]], result) | |
def test_fifth_input(self): | |
include_ports = [] | |
exclude_ports = [8080, 8080] | |
result = port_tools.apply_port_exclusions( | |
include_ports, exclude_ports | |
) | |
self.assertEqual([], result) | |
def test_low_edge_case(self): | |
include_ports = [[1, 1000]] | |
exclude_ports = [1, 1] | |
result = port_tools.apply_port_exclusions( | |
include_ports, exclude_ports | |
) | |
self.assertEqual([[2, 1000]], result) | |
def test_high_edge_case(self): | |
include_ports = [[1, 1000]] | |
exclude_ports = [1000, 1000] | |
result = port_tools.apply_port_exclusions( | |
include_ports, exclude_ports | |
) | |
self.assertEqual([[1, 999]], result) | |
def test_reduced(self): | |
result = port_tools._reduced([[1, 1], [2, 2]]) | |
self.assertEqual([[1, 2]], result) | |
def test_reduced_empty_list(self): | |
result = port_tools._reduced([]) | |
self.assertEqual([], result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment