def test_01_create_a_tables_object(self): """ Tables 01: create a Tables object, check chains """ self.assertIsInstance(Tables(""), Tables) tables = Tables("") expect = { 'filter': { 'FORWARD': [], 'INPUT': [], 'OUTPUT': [] }, 'raw': { 'OUTPUT': [], 'PREROUTING': [] }, 'mangle': { 'FORWARD': [], 'INPUT': [], 'POSTROUTING': [], 'PREROUTING': [], 'OUTPUT': [] }, 'nat': { 'OUTPUT': [], 'PREROUTING': [], 'POSTROUTING': [] } } self.assertEquals(expect, tables.data)
def test_04_raw_table(self): """ Tables 04: raw OUTPUT entry """ tables = Tables("") line = "iptables -t raw -A OUTPUT" line = line + " -p tcp --dport 80 -j ACCEPT" tables.put_into_tables(line) expect = ['-A OUTPUT -p tcp --dport 80 -j ACCEPT '] self.assertEquals(expect, tables.data["raw"]["OUTPUT"])
def test_03_mangle_table(self): """ Tables 03: mangle INPUT entry """ tables = Tables("") line = "iptables -t mangle -A INPUT" line = line + " -p tcp --dport 80 -j ACCEPT" tables.put_into_tables(line) expect = ['-A INPUT -p tcp --dport 80 -j ACCEPT '] self.assertEquals(expect, tables.data["mangle"]["INPUT"])
def test_02_nat_prerouting(self): """ Tables 02: nat PREROUTING entry """ tables = Tables("") line = "iptables -t nat -A PREROUTING -s 10.0.0.0/21" line = line + " -p tcp --dport 80 -j SNAT --to-source 192.168.1.15" tables.put_into_tables(line) expect = ['-A PREROUTING -s 10.0.0.0/21 -p tcp --dport 80 -j SNAT --to-source 192.168.1.15 '] self.assertEquals(expect, tables.data["nat"]["PREROUTING"])
def test_02_nat_prerouting(self): """ Tables 02: nat PREROUTING entry """ tables = Tables("") line = "iptables -t nat -A PREROUTING -s 10.0.0.0/21" line = line + " -p tcp --dport 80 -j SNAT --to-source 192.168.1.15" tables.put_into_tables(line) expect = [ '-A PREROUTING -s 10.0.0.0/21 -p tcp --dport 80 -j SNAT --to-source 192.168.1.15 ' ] self.assertEquals(expect, tables.data["nat"]["PREROUTING"])
def test_05_not_existing_chain(self): """ Tables 05: INPUT to not existing chain """ tables = Tables("") line = "iptables -t raw -A NONEXIST" line = line + " -p tcp --dport 80 -j ACCEPT" happend = False try: self.assertRaises(ValueError, tables, tables.put_into_tables(line)) except: happend = True self.assertEquals(happend, True)
def test_08_reference_one(self): """ Tables 08: read default file: reference-one, check chains """ tables = Tables() expect = { 'filter': { 'FORWARD': [], 'INPUT': ['-A INPUT -p tcp --dport 23 -j ACCEPT '], 'USER_CHAIN': ['-A USER_CHAIN -p icmp -j DROP '], 'OUTPUT': [] }, 'raw': { 'OUTPUT': [], 'PREROUTING': [] }, 'mangle': { 'FORWARD': [], 'INPUT': [], 'POSTROUTING': [], 'PREROUTING': [], 'OUTPUT': [] }, 'nat': { 'OUTPUT': [], 'POSTROUTING': [ '-A POSTROUTING -s 10.0.0.0/21 -p tcp --dport 80 -j SNAT --to-source 192.168.1.15 ' ], 'PREROUTING': [ '-A PREROUTING -d 192.0.2.5/32 -p tcp --dport 443 -j DNAT --to-destination 10.0.0.5:1500 ' ] } } self.maxDiff = None self.assertEquals(expect, tables.data)
def test_07_read_empty_file(self): """ Tables 07: read empty file (in relation to iptables-commands) """ filename = "MANIFEST" tables = Tables(filename) expect = { 'filter': { 'FORWARD': [], 'INPUT': [], 'OUTPUT': [] }, 'raw': { 'OUTPUT': [], 'PREROUTING': [] }, 'mangle': { 'FORWARD': [], 'INPUT': [], 'POSTROUTING': [], 'PREROUTING': [], 'OUTPUT': [] }, 'nat': { 'OUTPUT': [], 'PREROUTING': [], 'POSTROUTING': [] } } self.assertEquals(expect, tables.data)
def test_06_read_not_existing_file(self): """ Tables 06: read non existing file """ filename = "not-exist-is-ok" happend = False try: self.assertRaises(ValueError, Tables(filename)) except: happend = True self.assertEquals(happend, True)
def test_10_shell_functions(self): """ Tables 10: read buggy file with shell functions """ expect = "Line 6:" sys_exit_val = False try: with patch('sys.stdout', new=StringIO()) as fake_out: tables = Tables('test-debian-bug-no-748638') except SystemExit: sys_exit_val = True finally: pass self.assertIn(expect, fake_out.getvalue()) self.assertTrue(sys_exit_val)
def test_09_shell_variables(self): """ Tables 09: read buggy file with shell variables """ expect = "Line 8:" sys_exit_val = False try: with patch('sys.stdout', new=StringIO()) as fake_out: tables = Tables('test-shell-variables') except SystemExit: sys_exit_val = True finally: pass self.assertIn(expect, fake_out.getvalue()) self.assertTrue(sys_exit_val)