def test_parser(self): self.assertEqual( m.Rule('permit', m.Filter( ['ip'], [ipaddr.IPv4Network('127.0.0.0/8')], [ipaddr.IPv4Network('255.255.255.255/32')])), m.Rule.from_string('permit ip 127.0.0.0/8 255.255.255.255')) self.assertEqual( m.Rule('deny', m.Filter( ['tcp'], sources=[ipaddr.IPv4Network('127.0.0.0/8')], destinations=[ipaddr.IPv4Network('255.255.255.255/32')], sports=m.Ports("80"), dports=m.Ports("23")), extensions=['established']), m.Rule.from_string('deny tcp 127.0.0.0/8 80 255.255.255.255 23 established'))
def test_to_string_and_back(self): ports = m.Ports([1,23,42,18000],[(100,200),(1024,4096)]) # To string self.assertItemsEqual(str(ports), "1,23,42,18000,100-200,1024-4096") # and back self.assertItemsEqual(m.Ports(str(ports)), ports)
def test_meeting_ranges(self): # Meeting borders ports = m.Ports("1-1024,1024-2342") self.assertItemsEqual(ports, m.Ports([], [(1, 2342)]))
def test_partial_overlapping_ranges(self): # Partial overlap ports = m.Ports("1-1024,512-2342") self.assertItemsEqual(ports, m.Ports([], [(1, 2342)]))
def test_complete_overlapping_ranges(self): # Complete overlap ports = m.Ports("1-1024,512-1000") self.assertItemsEqual(ports, m.Ports([], [(1, 1024)]))
def test_multiple_same_ports(self): ports = m.Ports("23,23") self.assertItemsEqual(ports, m.Ports([23]))
def test_multiple_ports_and_ranges(self): ports = m.Ports("1-1024,8080,4000-5000,2342") self.assertItemsEqual(ports, m.Ports([2342, 8080], [(1, 1024), (4000, 5000)]))
def test_sigle_port_contained_in_range(self): ports = m.Ports("1-1024,80") self.assertItemsEqual(ports, m.Ports([], [(1, 1024)]))
def test_single_and_range_combination(self): ports = m.Ports("1-1024,8080") self.assertItemsEqual(ports, m.Ports([8080], [(1, 1024)]))
def test_multiple_port_ranges(self): ports = m.Ports("1-1024,2000-2342") self.assertItemsEqual(ports, m.Ports([], [(1, 1024),(2000, 2342)]))
def test_parser(self): self.assertEqual( m.Filter(['tcp', 'udp'], [ipaddr.IPv4Network('127.0.0.0/8')], [ipaddr.IPv4Network('255.255.255.255/32')], sports=m.Ports("80"), dports=m.Ports("23")), m.Filter.from_string('tcp,udp 127.0.0.0/8 80 255.255.255.255 23'))
def test_port_range(self): ports = m.Ports("1-1024") self.assertItemsEqual(ports, m.Ports([], [(1, 1024)]))
def test_multiple_ports(self): ports = m.Ports("80,443,8080") self.assertItemsEqual(ports, m.Ports([80,443,8080]))
def test_single_port(self): self.assertEqual(m.Ports("80"), m.Ports([80]))