Skip to content

Commit c5519c1

Browse files
author
ldx
committed
Refactor a few test cases.
1 parent 17b638b commit c5519c1

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

iptc/test/test_iptc.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
is_table6_available = iptc.is_table6_available
99

1010

11+
def _check_chains(testcase, *chains):
12+
for chain in chains:
13+
if chain is None:
14+
continue
15+
for ch in [c for c in chains if c != chain and c is not None]:
16+
testcase.assertNotEquals(id(chain), id(ch))
17+
18+
1119
class TestTable6(unittest.TestCase):
1220
def setUp(self):
1321
pass
@@ -33,18 +41,7 @@ def test_table6(self):
3341
if is_table6_available(iptc.Table6.RAW):
3442
raw = iptc.Table6("raw")
3543
self.assertEquals(id(raw), id(iptc.Table6(iptc.Table6.RAW)))
36-
if filt and security:
37-
self.assertNotEquals(id(filt), id(security))
38-
if filt and mangle:
39-
self.assertNotEquals(id(filt), id(mangle))
40-
if filt and raw:
41-
self.assertNotEquals(id(filt), id(raw))
42-
if security and mangle:
43-
self.assertNotEquals(id(security), id(mangle))
44-
if security and raw:
45-
self.assertNotEquals(id(security), id(raw))
46-
if mangle and raw:
47-
self.assertNotEquals(id(mangle), id(raw))
44+
_check_chains(self, filt, security, mangle, raw)
4845

4946
def test_table6_autocommit(self):
5047
table = iptc.Table(iptc.Table.FILTER, False)
@@ -90,18 +87,7 @@ def test_table(self):
9087
if is_table_available(iptc.Table.RAW):
9188
raw = iptc.Table("raw")
9289
self.assertEquals(id(raw), id(iptc.Table(iptc.Table.RAW)))
93-
if filt and nat:
94-
self.assertNotEquals(id(filt), id(nat))
95-
if filt and mangle:
96-
self.assertNotEquals(id(filt), id(mangle))
97-
if filt and raw:
98-
self.assertNotEquals(id(filt), id(raw))
99-
if nat and mangle:
100-
self.assertNotEquals(id(nat), id(mangle))
101-
if nat and raw:
102-
self.assertNotEquals(id(nat), id(raw))
103-
if mangle and raw:
104-
self.assertNotEquals(id(mangle), id(raw))
90+
_check_chains(self, filt, nat, mangle, raw)
10591

10692
def test_refresh(self):
10793
rule = iptc.Rule()
@@ -205,7 +191,7 @@ def test_builtin_chain(self):
205191
self.assertTrue(table.builtin_chain("PREROUTING"))
206192
self.assertTrue(table.builtin_chain("OUTPUT"))
207193

208-
def test_chains(self):
194+
def test_chain_filter(self):
209195
if is_table_available(iptc.Table.FILTER):
210196
table = iptc.Table(iptc.Table.FILTER)
211197
table.autocommit = True
@@ -214,6 +200,7 @@ def test_chains(self):
214200
if chain.name not in ["INPUT", "FORWARD", "OUTPUT"]:
215201
self.failIf(chain.is_builtin())
216202

203+
def test_chain_nat(self):
217204
if is_table_available(iptc.Table.NAT):
218205
table = iptc.Table(iptc.Table.NAT)
219206
table.autocommit = True
@@ -223,6 +210,7 @@ def test_chains(self):
223210
"OUTPUT"]:
224211
self.failIf(chain.is_builtin())
225212

213+
def test_chain_mangle(self):
226214
if is_table_available(iptc.Table.MANGLE):
227215
table = iptc.Table(iptc.Table.MANGLE)
228216
table.autocommit = True
@@ -232,6 +220,7 @@ def test_chains(self):
232220
"FORWARD", "OUTPUT"]:
233221
self.failIf(chain.is_builtin())
234222

223+
def test_chain_raw(self):
235224
if is_table_available(iptc.Table.RAW):
236225
table = iptc.Table(iptc.Table.RAW)
237226
table.autocommit = True
@@ -240,7 +229,7 @@ def test_chains(self):
240229
if chain.name not in ["PREROUTING", "OUTPUT"]:
241230
self.failIf(chain.is_builtin())
242231

243-
def test_chain_counters(self):
232+
def _get_tables(self):
244233
tables = []
245234
if is_table_available(iptc.Table.FILTER):
246235
tables.append(iptc.Table(iptc.Table.FILTER))
@@ -250,6 +239,10 @@ def test_chain_counters(self):
250239
tables.append(iptc.Table(iptc.Table.MANGLE))
251240
if is_table_available(iptc.Table.RAW):
252241
tables.append(iptc.Table(iptc.Table.RAW))
242+
return tables
243+
244+
def test_chain_counters(self):
245+
tables = self._get_tables()
253246
for chain in (chain for table in tables for chain in table.chains):
254247
counters = chain.get_counters()
255248
fails = 0
@@ -268,7 +261,7 @@ def test_create_chain(self):
268261
iptc.Table(iptc.Table.FILTER).delete_chain(chain)
269262
self.failIf(iptc.Table(iptc.Table.FILTER).is_chain(chain))
270263

271-
def test_chain_policy(self):
264+
def test_filter_policy(self):
272265
if is_table_available(iptc.Table.FILTER):
273266
table = iptc.Table(iptc.Table.FILTER)
274267
input_chain = iptc.Chain(table, "INPUT")
@@ -288,6 +281,7 @@ def test_chain_policy(self):
288281
else:
289282
self.fail("managed to set INPUT policy to RETURN")
290283

284+
def test_nat_policy(self):
291285
if is_table_available(iptc.Table.NAT):
292286
table = iptc.Table(iptc.Table.NAT)
293287
prerouting_chain = iptc.Chain(table, "PREROUTING")
@@ -456,25 +450,32 @@ def test_rule_standard_target(self):
456450
self.chain.insert_rule(rule)
457451
self.chain.delete_rule(rule)
458452

459-
def test_rule_iterate(self):
453+
def test_rule_iterate_filter(self):
460454
if is_table6_available(iptc.Table6.FILTER):
461455
for r in (rule for chain in iptc.Table6(iptc.Table6.FILTER).chains
462456
for rule in chain.rules if rule):
463457
pass
458+
459+
def test_rule_iterate_raw(self):
464460
if is_table6_available(iptc.Table6.RAW):
465461
for r in (rule for chain in iptc.Table6(iptc.Table6.RAW).chains
466462
for rule in chain.rules if rule):
467463
pass
464+
465+
def test_rule_iterate_mangle(self):
468466
if is_table6_available(iptc.Table6.MANGLE):
469467
for r in (rule for chain in iptc.Table6(iptc.Table6.MANGLE).chains
470468
for rule in chain.rules if rule):
471469
pass
470+
471+
def test_rule_iterate_security(self):
472472
if is_table6_available(iptc.Table6.SECURITY):
473473
for r in (rule for chain in
474474
iptc.Table6(iptc.Table6.SECURITY).chains
475475
for rule in chain.rules if rule):
476476
pass
477477

478+
def test_rule_insert(self):
478479
rules = []
479480

480481
rule = iptc.Rule6()
@@ -646,20 +647,25 @@ def test_rule_standard_target(self):
646647
self.chain.insert_rule(rule)
647648
self.chain.delete_rule(rule)
648649

649-
def test_rule_iterate(self):
650+
def test_rule_iterate_filter(self):
650651
if is_table_available(iptc.Table.FILTER):
651652
for r in (rule for chain in iptc.Table(iptc.Table.FILTER).chains
652653
for rule in chain.rules if rule):
653654
pass
655+
656+
def test_rule_iterate_nat(self):
654657
if is_table_available(iptc.Table.NAT):
655658
for r in (rule for chain in iptc.Table(iptc.Table.NAT).chains
656659
for rule in chain.rules if rule):
657660
pass
661+
662+
def test_rule_iterate_mangle(self):
658663
if is_table_available(iptc.Table.MANGLE):
659664
for r in (rule for chain in iptc.Table(iptc.Table.MANGLE).chains
660665
for rule in chain.rules if rule):
661666
pass
662667

668+
def test_rule_insert(self):
663669
rules = []
664670

665671
rule = iptc.Rule()

iptc/test/test_targets.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def tearDown(self):
183183
self.chain.flush()
184184
self.chain.delete()
185185

186-
def test_mode(self):
186+
def test_set_tos(self):
187187
for tos in ["0x12/0xff", "0x12/0x0f"]:
188188
self.target.set_tos = tos
189189
self.assertEquals(self.target.set_tos, tos)
@@ -196,6 +196,8 @@ def test_mode(self):
196196
self.target.set_tos = tos[0]
197197
self.assertEquals(self.target.set_tos, tos[1])
198198
self.target.reset()
199+
200+
def test_tos_mode(self):
199201
for tos in ["0x04"]:
200202
self.target.and_tos = tos
201203
self.assertEquals(self.target.set_tos, "0x00/0xfb")

0 commit comments

Comments
 (0)