'# Completed by iptables_manager\n' % IPTABLES_ARG)
+def _generate_raw_dump(iptables_args):
+ return ('# Generated by iptables_manager\n'
+ '*raw\n'
+ ':%(bn)s-OUTPUT - [0:0]\n'
+ ':%(bn)s-PREROUTING - [0:0]\n'
+ '[0:0] -A PREROUTING -j %(bn)s-PREROUTING\n'
+ '[0:0] -A OUTPUT -j %(bn)s-OUTPUT\n'
+ 'COMMIT\n'
+ '# Completed by iptables_manager\n' % iptables_args)
+
+RAW_DUMP = _generate_raw_dump(IPTABLES_ARG)
+
+
class IptablesManagerStateFulTestCase(base.BaseTestCase):
def setUp(self):
'COMMIT\n'
'# Completed by iptables_manager\n' % iptables_args)
+ raw_dump = _generate_raw_dump(iptables_args)
+
expected_calls_and_values = [
(mock.call(['iptables-save', '-c'],
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=nat_dump + filter_dump_mod,
+ process_input=raw_dump + nat_dump + filter_dump_mod,
root_helper=self.root_helper),
None),
(mock.call(['iptables-save', '-c'],
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=nat_dump + filter_dump,
+ process_input=raw_dump + nat_dump + filter_dump,
root_helper=self.root_helper),
None),
]
'COMMIT\n'
'# Completed by iptables_manager\n' % iptables_args)
+ raw_dump = _generate_raw_dump(iptables_args)
+
expected_calls_and_values = [
(mock.call(['iptables-save', '-c'],
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=nat_dump + filter_dump_mod,
+ process_input=raw_dump + nat_dump + filter_dump_mod,
root_helper=self.root_helper),
None),
(mock.call(['iptables-save', '-c'],
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=nat_dump + filter_dump,
+ process_input=raw_dump + nat_dump + filter_dump,
root_helper=self.root_helper),
None),
]
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=NAT_DUMP + filter_dump_mod,
+ process_input=RAW_DUMP + NAT_DUMP + filter_dump_mod,
root_helper=self.root_helper),
None),
(mock.call(['iptables-save', '-c'],
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=NAT_DUMP + FILTER_DUMP,
+ process_input=RAW_DUMP + NAT_DUMP + FILTER_DUMP,
root_helper=self.root_helper),
None),
]
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=NAT_DUMP + filter_dump_mod,
+ process_input=RAW_DUMP + NAT_DUMP + filter_dump_mod,
root_helper=self.root_helper),
None),
(mock.call(['iptables-save', '-c'],
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=NAT_DUMP + FILTER_DUMP,
+ process_input=RAW_DUMP + NAT_DUMP + FILTER_DUMP,
root_helper=self.root_helper
),
None),
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=NAT_DUMP + filter_dump_mod,
+ process_input=RAW_DUMP + NAT_DUMP + filter_dump_mod,
root_helper=self.root_helper),
None),
(mock.call(['iptables-save', '-c'],
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=NAT_DUMP + FILTER_DUMP,
+ process_input=RAW_DUMP + NAT_DUMP + FILTER_DUMP,
root_helper=self.root_helper),
None),
]
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=nat_dump_mod + FILTER_DUMP,
+ process_input=RAW_DUMP + nat_dump_mod + FILTER_DUMP,
root_helper=self.root_helper),
None),
(mock.call(['iptables-save', '-c'],
root_helper=self.root_helper),
''),
(mock.call(['iptables-restore', '-c'],
- process_input=nat_dump + FILTER_DUMP,
+ process_input=RAW_DUMP + nat_dump + FILTER_DUMP,
root_helper=self.root_helper),
None),
]
def test_add_nat_rule_with_ipv6(self):
self._test_add_nat_rule_helper(True)
+ def _test_add_raw_rule_helper(self, use_ipv6):
+ self.iptables = iptables_manager.IptablesManager(
+ root_helper=self.root_helper,
+ use_ipv6=use_ipv6)
+ self.execute = mock.patch.object(self.iptables, "execute").start()
+
+ raw_dump_mod = ('# Generated by iptables_manager\n'
+ '*raw\n'
+ ':%(bn)s-OUTPUT - [0:0]\n'
+ ':%(bn)s-PREROUTING - [0:0]\n'
+ ':%(bn)s-raw - [0:0]\n'
+ '[0:0] -A PREROUTING -j %(bn)s-PREROUTING\n'
+ '[0:0] -A OUTPUT -j %(bn)s-OUTPUT\n'
+ '[0:0] -A %(bn)s-PREROUTING -j CT --notrack\n'
+ 'COMMIT\n'
+ '# Completed by iptables_manager\n'
+ % IPTABLES_ARG)
+
+ expected_calls_and_values = [
+ (mock.call(['iptables-save', '-c'],
+ root_helper=self.root_helper),
+ ''),
+ (mock.call(['iptables-restore', '-c'],
+ process_input=raw_dump_mod + NAT_DUMP + FILTER_DUMP,
+ root_helper=self.root_helper),
+ None),
+ (mock.call(['iptables-save', '-c'],
+ root_helper=self.root_helper),
+ ''),
+ (mock.call(['iptables-restore', '-c'],
+ process_input=RAW_DUMP + NAT_DUMP + FILTER_DUMP,
+ root_helper=self.root_helper),
+ None),
+ ]
+ if use_ipv6:
+ self._extend_with_ip6tables_filter(expected_calls_and_values,
+ FILTER_DUMP)
+
+ tools.setup_mock_calls(self.execute, expected_calls_and_values)
+
+ self.iptables.ipv4['raw'].add_chain('raw')
+ self.iptables.ipv4['raw'].add_rule('PREROUTING',
+ '-j CT --notrack')
+
+ self.iptables.apply()
+
+ self.iptables.ipv4['raw'].remove_rule('PREROUTING',
+ '-j CT --notrack')
+ self.iptables.ipv4['raw'].remove_chain('raw')
+
+ self.iptables.apply()
+
+ tools.verify_mock_calls(self.execute, expected_calls_and_values)
+
+ def test_add_raw_rule(self):
+ self._test_add_raw_rule_helper(False)
+
+ def test_add_raw_rule_with_ipv6(self):
+ self._test_add_raw_rule_helper(True)
+
def test_add_rule_to_a_nonexistent_chain(self):
self.assertRaises(LookupError, self.iptables.ipv4['filter'].add_rule,
'nonexistent', '-j DROP')
'-n', '-v', '-x'],
root_helper=self.root_helper),
iptables_dump),
+ (mock.call(['iptables', '-t', 'raw', '-L', 'OUTPUT', '-n',
+ '-v', '-x'],
+ root_helper=self.root_helper),
+ ''),
(mock.call(['iptables', '-t', 'nat', '-L', 'OUTPUT', '-n',
'-v', '-x'],
root_helper=self.root_helper),
'-n', '-v', '-x', '-Z'],
root_helper=self.root_helper),
iptables_dump),
+ (mock.call(['iptables', '-t', 'raw', '-L', 'OUTPUT', '-n',
+ '-v', '-x', '-Z'],
+ root_helper=self.root_helper),
+ ''),
(mock.call(['iptables', '-t', 'nat', '-L', 'OUTPUT', '-n',
'-v', '-x', '-Z'],
root_helper=self.root_helper),