@@ -241,6 +241,54 @@ def test_insert(self):
241241 self .fail ("inserted rule does not match original" )
242242
243243
244+ class TestDnatTarget (unittest .TestCase ):
245+ def setUp (self ):
246+ self .rule = iptc .Rule ()
247+ self .rule .dst = "127.0.0.2"
248+ self .rule .protocol = "tcp"
249+ self .rule .in_interface = "eth0"
250+
251+ self .match = iptc .Match (self .rule , "tcp" )
252+ self .rule .add_match (self .match )
253+
254+ self .target = iptc .Target (self .rule , "DNAT" )
255+ self .rule .target = self .target
256+
257+ self .chain = iptc .Chain (iptc .Table (iptc .Table .MANGLE ),
258+ "iptc_test_dnat" )
259+ iptc .Table (iptc .Table .MANGLE ).create_chain (self .chain )
260+
261+ def tearDown (self ):
262+ self .chain .flush ()
263+ self .chain .delete ()
264+
265+ def test_mode (self ):
266+ for dst in ["1.2.3.4" , "199.199.199.199-199.199.199.255" ,
267+ "1.2.3.4:5678" , "1.2.3.4:5678-5688" ]:
268+ self .target .to_destination = dst
269+ self .assertEquals (self .target .to_destination , dst )
270+ self .target .reset ()
271+ self .target .to_destination = dst
272+ self .target .random = "1"
273+ self .assertEquals (self .target .to_destination , dst )
274+ self .target .reset ()
275+ self .target .to_destination = dst
276+ self .target .persistent = "1"
277+ self .assertEquals (self .target .to_destination , dst )
278+ self .target .reset ()
279+
280+ def test_insert (self ):
281+ self .target .reset ()
282+ self .target .to_destination = "1.2.3.4"
283+ self .rule .target = self .target
284+
285+ self .chain .insert_rule (self .rule )
286+
287+ for r in self .chain .rules :
288+ if r != self .rule :
289+ self .fail ("inserted rule does not match original" )
290+
291+
244292class TestIPTMasqueradeTarget (unittest .TestCase ):
245293 def setUp (self ):
246294 self .rule = iptc .Rule ()
@@ -302,8 +350,10 @@ def suite():
302350 TestIPTRedirectTarget )
303351 suite_masq = unittest .TestLoader ().loadTestsFromTestCase (
304352 TestIPTMasqueradeTarget )
353+ suite_dnat = unittest .TestLoader ().loadTestsFromTestCase (
354+ TestDnatTarget )
305355 return unittest .TestSuite ([suite_target , suite_cluster , suite_redir ,
306- suite_tos , suite_masq ])
356+ suite_tos , suite_masq , suite_dnat ])
307357 else :
308358 return unittest .TestSuite ([suite_target , suite_cluster , suite_tos ])
309359
0 commit comments