Skip to content

Commit

Permalink
Fix comparison of Match instances and update test_matches file
Browse files Browse the repository at this point in the history
  • Loading branch information
jllorente committed May 18, 2019
1 parent b898fa4 commit ce1936f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
4 changes: 1 addition & 3 deletions iptc/ip4tc.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,7 @@ def _check_alias(self):

def __eq__(self, match):
basesz = ct.sizeof(xt_entry_match)
if (self.match.u.match_size == match.match.u.match_size and
self.match.u.user.name == match.match.u.user.name and
self.match.u.user.revision == match.match.u.user.revision and
if (self.name == match.name and
self.match_buf[basesz:self.usersize] ==
match.match_buf[basesz:match.usersize]):
return True
Expand Down
54 changes: 28 additions & 26 deletions tests/test_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_match_create(self):
match = rule.create_match("udp")

for m in rule.matches:
self.failUnless(m == match)
self.assertEqual(m, match)

# check that we can change match parameters after creation
match.sport = "12345:55555"
Expand All @@ -29,7 +29,7 @@ def test_match_create(self):
m.sport = "12345:55555"
m.dport = "!33333"

self.failUnless(m == match)
self.assertEqual(m, match)

def test_match_compare(self):
m1 = iptc.Match(iptc.Rule(), "udp")
Expand All @@ -40,37 +40,37 @@ def test_match_compare(self):
m2.sport = "12345:55555"
m2.dport = "!33333"

self.failUnless(m1 == m2)
self.assertEqual(m1, m2)

m2.reset()
m2.sport = "12345:55555"
m2.dport = "33333"
self.failIf(m1 == m2)
self.assertNotEqual(m1, m2)

def test_match_parameters(self):
m = iptc.Match(iptc.Rule(), "udp")
m.sport = "12345:55555"
m.dport = "!33333"

self.failUnless(len(m.parameters) == 2)
self.assertEqual(len(m.parameters), 2)

for p in m.parameters:
self.failUnless(p == "sport" or p == "dport")
self.assertTrue(p == "sport" or p == "dport")

self.failUnless(m.parameters["sport"] == "12345:55555")
self.failUnless(m.parameters["dport"] == "!33333")
self.assertEqual(m.parameters["sport"], "12345:55555")
self.assertEqual(m.parameters["dport"], "!33333")

m.reset()
self.failUnless(len(m.parameters) == 0)
self.assertEqual(len(m.parameters), 0)

def test_get_all_parameters(self):
m = iptc.Match(iptc.Rule(), "udp")
m.sport = "12345:55555"
m.dport = "!33333"

params = m.get_all_parameters()
self.assertEquals(set(params['sport']), set(['12345:55555']))
self.assertEquals(set(params['dport']), set(['!', '33333']))
self.assertEqual(set(params['sport']), set(['12345:55555']))
self.assertEqual(set(params['dport']), set(['!', '33333']))


class TestMultiportMatch(unittest.TestCase):
Expand Down Expand Up @@ -103,14 +103,14 @@ def test_multiport(self):
self.chain.insert_rule(self.rule)
rule = self.chain.rules[0]
match = rule.matches[0]
self.assertEquals(match.dports, '1111,2222')
self.assertEqual(match.dports, '1111,2222')

def test_unicode_multiport(self):
self.match.dports = u'1111,2222'
self.chain.insert_rule(self.rule)
rule = self.chain.rules[0]
match = rule.matches[0]
self.assertEquals(match.dports, '1111,2222')
self.assertEqual(match.dports, '1111,2222')


class TestXTUdpMatch(unittest.TestCase):
Expand All @@ -135,9 +135,9 @@ def test_udp_port(self):
"!12345:12346", "0:1234", "! 1234", "!0:12345",
"!1234:65535"]:
self.match.sport = port
self.assertEquals(self.match.sport, port.replace(" ", ""))
self.assertEqual(self.match.sport, port.replace(" ", ""))
self.match.dport = port
self.assertEquals(self.match.dport, port.replace(" ", ""))
self.assertEqual(self.match.dport, port.replace(" ", ""))
self.match.reset()
for port in ["-1", "asdf", "!asdf"]:
try:
Expand Down Expand Up @@ -188,7 +188,7 @@ def tearDown(self):
def test_mark(self):
for mark in ["0x7b", "! 0x7b", "0x7b/0xfffefffe", "!0x7b/0xff00ff00"]:
self.match.mark = mark
self.assertEquals(self.match.mark, mark.replace(" ", ""))
self.assertEqual(self.match.mark, mark.replace(" ", ""))
self.match.reset()
for mark in ["0xffffffffff", "123/0xffffffff1", "!asdf", "1234:1233"]:
try:
Expand Down Expand Up @@ -232,7 +232,7 @@ def tearDown(self):
def test_limit(self):
for limit in ["1/sec", "5/min", "3/hour"]:
self.match.limit = limit
self.assertEquals(self.match.limit, limit)
self.assertEqual(self.match.limit, limit)
self.match.reset()
for limit in ["asdf", "123/1", "!1", "!1/second"]:
try:
Expand Down Expand Up @@ -284,7 +284,7 @@ def tearDown(self):
def test_icmpv6(self):
self.chain.insert_rule(self.rule)
rule = self.chain.rules[0]
self.assertEquals(self.rule, rule)
self.assertEqual(self.rule, rule)


class TestCommentMatch(unittest.TestCase):
Expand All @@ -310,7 +310,7 @@ def test_comment(self):
self.match.reset()
self.match.comment = comment
self.chain.insert_rule(self.rule)
self.assertEquals(self.match.comment, comment)
self.assertEqual(self.match.comment, comment)


class TestIprangeMatch(unittest.TestCase):
Expand Down Expand Up @@ -389,8 +389,10 @@ def test_state(self):
self.chain.insert_rule(self.rule)
rule = self.chain.rules[0]
m = rule.matches[0]
self.assertEquals(m.name, "state")
self.assertEquals(m.state, "RELATED,ESTABLISHED")
self.assertEqual(m.name, "state")
self.assertEqual(m.state, "RELATED,ESTABLISHED")
self.assertEqual(rule.matches[0].name, self.rule.matches[0].name)
self.assertEqual(rule, self.rule)


class TestXTConntrackMatch(unittest.TestCase):
Expand Down Expand Up @@ -425,7 +427,7 @@ def test_state(self):
rule = self.chain.rules[0]
m = rule.matches[0]
self.assertTrue(m.name, ["conntrack"])
self.assertEquals(m.ctstate, "NEW,RELATED")
self.assertEqual(m.ctstate, "NEW,RELATED")


class TestHashlimitMatch(unittest.TestCase):
Expand Down Expand Up @@ -464,10 +466,10 @@ def test_hashlimit(self):
rule = self.chain.rules[0]
m = rule.matches[0]
self.assertTrue(m.name, ["hashlimit"])
self.assertEquals(m.hashlimit_name, "foo")
self.assertEquals(m.hashlimit_mode, "srcip")
self.assertEquals(m.hashlimit_upto, "200/sec")
self.assertEquals(m.hashlimit_burst, "5")
self.assertEqual(m.hashlimit_name, "foo")
self.assertEqual(m.hashlimit_mode, "srcip")
self.assertEqual(m.hashlimit_upto, "200/sec")
self.assertEqual(m.hashlimit_burst, "5")


def suite():
Expand Down

0 comments on commit ce1936f

Please sign in to comment.