diff --git a/setools/policyrep/fscontext.py b/setools/policyrep/fscontext.py index 8d34936..c41600f 100644 --- a/setools/policyrep/fscontext.py +++ b/setools/policyrep/fscontext.py @@ -49,6 +49,17 @@ class Genfscon(FSContext): def __str__(self): return "genfscon {0.fs} {0.path} {0.context}".format(self) + def __eq__(self, other): + # Libqpol allocates new C objects in the + # genfscons iterator, so pointer comparison + # in the PolicySymbol object doesn't work. + try: + return (self.fs == other.fs and + self.path == other.path and + self.context == other.context) + except AttributeError: + return (str(self) == str(other)) + @property def filetype(self): """The file type for this genfscon statement.""" diff --git a/setools/policyrep/netcontext.py b/setools/policyrep/netcontext.py index 77272b9..4f4bfe6 100644 --- a/setools/policyrep/netcontext.py +++ b/setools/policyrep/netcontext.py @@ -69,6 +69,17 @@ class Nodecon(NetContext): def __str__(self): return "nodecon {0.address} {0.netmask} {0.context}".format(self) + def __eq__(self, other): + # Libqpol allocates new C objects in the + # nodecons iterator, so pointer comparison + # in the PolicySymbol object doesn't work. + try: + return (self.address == other.address and + self.netmask == other.netmask and + self.context == other.context) + except AttributeError: + return (str(self) == str(other)) + @property def ip_version(self): """ diff --git a/tests/commonquery.py b/tests/commonquery.py index e4e8403..24c14b9 100644 --- a/tests/commonquery.py +++ b/tests/commonquery.py @@ -29,14 +29,12 @@ class CommonQueryTest(unittest.TestCase): def test_000_unset(self): """Common query with no criteria.""" # query with no parameters gets all types. - for numcommons, c in enumerate(self.p.commons(), start=1): - pass + commons = sorted(self.p.commons()) q = CommonQuery(self.p) - for q_numcommons, u in enumerate(q.results(), start=1): - pass + q_commons = sorted(q.results()) - self.assertEqual(numcommons, q_numcommons) + self.assertListEqual(commons, q_commons) def test_001_name_exact(self): """Common query with exact name match.""" diff --git a/tests/fsusequery.py b/tests/fsusequery.py index 0c7d196..004ae2c 100644 --- a/tests/fsusequery.py +++ b/tests/fsusequery.py @@ -29,14 +29,12 @@ class FSUseQueryTest(unittest.TestCase): def test_000_unset(self): """fs_use_* query with no criteria""" # query with no parameters gets all fs_use_*. - for numrules, s in enumerate(self.p.fs_uses(), start=1): - pass + fsu = sorted(self.p.fs_uses()) q = FSUseQuery(self.p) - for q_numrules, s in enumerate(q.results(), start=1): - pass + q_fsu = sorted(q.results()) - self.assertEqual(numrules, q_numrules) + self.assertListEqual(fsu, q_fsu) def test_001_fs_exact(self): """fs_use_* query with exact fs match""" diff --git a/tests/genfsconquery.py b/tests/genfsconquery.py index 5c4c0f8..52c0516 100644 --- a/tests/genfsconquery.py +++ b/tests/genfsconquery.py @@ -29,14 +29,12 @@ class GenfsconQueryTest(unittest.TestCase): def test_000_unset(self): """Genfscon query with no criteria""" # query with no parameters gets all genfs. - for numrules, s in enumerate(self.p.genfscons(), start=1): - pass + genfs = sorted(self.p.genfscons()) q = GenfsconQuery(self.p) - for q_numrules, s in enumerate(q.results(), start=1): - pass + q_genfs = sorted(q.results()) - self.assertEqual(numrules, q_numrules) + self.assertListEqual(genfs, q_genfs) def test_001_fs_exact(self): """Genfscon query with exact fs match""" diff --git a/tests/initsidquery.py b/tests/initsidquery.py index b3f5417..0ce23f3 100644 --- a/tests/initsidquery.py +++ b/tests/initsidquery.py @@ -29,14 +29,12 @@ class InitialSIDQueryTest(unittest.TestCase): def test_000_unset(self): """Initial SID query with no criteria""" # query with no parameters gets all SIDs. - for numrules, s in enumerate(self.p.initialsids(), start=1): - pass + sids = sorted(self.p.initialsids()) q = InitialSIDQuery(self.p) - for q_numrules, s in enumerate(q.results(), start=1): - pass + q_sids = sorted(q.results()) - self.assertEqual(numrules, q_numrules) + self.assertListEqual(sids, q_sids) def test_001_name_exact(self): """Initial SID query with exact match""" diff --git a/tests/mlsrulequery.py b/tests/mlsrulequery.py index 5cc90f2..64cfa68 100644 --- a/tests/mlsrulequery.py +++ b/tests/mlsrulequery.py @@ -36,14 +36,12 @@ class MLSRuleQueryTest(unittest.TestCase): def test_000_unset(self): """MLS rule query with no criteria.""" # query with no parameters gets all MLS rules. - for numrules, r in enumerate(self.p.mlsrules(), start=1): - pass + rules = sorted(self.p.mlsrules()) q = MLSRuleQuery(self.p) - for q_numrules, r in enumerate(q.results(), start=1): - pass + q_rules = sorted(q.results()) - self.assertEqual(numrules, q_numrules) + self.assertListEqual(rules, q_rules) def test_001_source_direct(self): """MLS rule query with exact, direct, source match.""" diff --git a/tests/netifconquery.py b/tests/netifconquery.py index f94bc9c..78df6d9 100644 --- a/tests/netifconquery.py +++ b/tests/netifconquery.py @@ -29,14 +29,12 @@ class NetifconQueryTest(unittest.TestCase): def test_000_unset(self): """Netifcon query with no criteria""" # query with no parameters gets all netifs. - for numrules, s in enumerate(self.p.netifcons(), start=1): - pass + netifs = sorted(self.p.netifcons()) q = NetifconQuery(self.p) - for q_numrules, s in enumerate(q.results(), start=1): - pass + q_netifs = sorted(q.results()) - self.assertEqual(numrules, q_numrules) + self.assertListEqual(netifs, q_netifs) def test_001_name_exact(self): """Netifcon query with exact match""" diff --git a/tests/nodeconquery.py b/tests/nodeconquery.py index 838b25f..eb0e478 100644 --- a/tests/nodeconquery.py +++ b/tests/nodeconquery.py @@ -31,14 +31,12 @@ class NodeconQueryTest(unittest.TestCase): def test_000_unset(self): """Nodecon query with no criteria""" # query with no parameters gets all nodecons. - for numrules, s in enumerate(self.p.nodecons(), start=1): - pass + nodecons = sorted(self.p.nodecons()) q = NodeconQuery(self.p) - for q_numrules, s in enumerate(q.results(), start=1): - pass + q_nodecons = sorted(q.results()) - self.assertEqual(numrules, q_numrules) + self.assertListEqual(nodecons, q_nodecons) def test_001_ip_version(self): """Nodecon query with IP version match.""" diff --git a/tests/objclassquery.py b/tests/objclassquery.py index aa8f606..2fde943 100644 --- a/tests/objclassquery.py +++ b/tests/objclassquery.py @@ -29,14 +29,12 @@ class ObjClassQueryTest(unittest.TestCase): def test_000_unset(self): """Class query with no criteria.""" # query with no parameters gets all types. - for numclasses, c in enumerate(self.p.classes(), start=1): - pass + classes = sorted(self.p.classes()) q = ObjClassQuery(self.p) - for q_numclasses, u in enumerate(q.results(), start=1): - pass + q_classes = sorted(q.results()) - self.assertEqual(numclasses, q_numclasses) + self.assertListEqual(classes, q_classes) def test_001_name_exact(self): """Class query with exact name match.""" diff --git a/tests/permissivequery.py b/tests/permissivequery.py index 93a1dda..4606b8b 100644 --- a/tests/permissivequery.py +++ b/tests/permissivequery.py @@ -29,14 +29,12 @@ class PolCapQueryTest(unittest.TestCase): def test_000_unset(self): """Policy capability query with no criteria""" # query with no parameters gets all permissives - for numtypes, t in enumerate(self.p.permissives(), start=1): - pass + types = sorted(self.p.permissives()) q = PermissiveQuery(self.p) - for q_numtypes, t in enumerate(q.results(), start=1): - pass + q_types = sorted(q.results()) - self.assertEqual(numtypes, q_numtypes) + self.assertListEqual(types, q_types) def test_001_name_exact(self): """Permissive query with exact match""" diff --git a/tests/polcapquery.py b/tests/polcapquery.py index df5b56e..e04538b 100644 --- a/tests/polcapquery.py +++ b/tests/polcapquery.py @@ -29,10 +29,10 @@ class PolCapQueryTest(unittest.TestCase): def test_000_unset(self): """Policy capability query with no criteria""" # query with no parameters gets all capabilities. - allcaps = sorted(str(c) for c in self.p.polcaps()) + allcaps = sorted(self.p.polcaps()) q = PolCapQuery(self.p) - qcaps = sorted(str(c) for c in q.results()) + qcaps = sorted(q.results()) self.assertListEqual(allcaps, qcaps) diff --git a/tests/rbacrulequery.py b/tests/rbacrulequery.py index 74c9605..49bfd93 100644 --- a/tests/rbacrulequery.py +++ b/tests/rbacrulequery.py @@ -30,14 +30,12 @@ class RBACRuleQueryTest(unittest.TestCase): def test_000_unset(self): """RBAC rule query with no criteria.""" # query with no parameters gets all RBAC rules. - for numrules, r in enumerate(self.p.rbacrules(), start=1): - pass + rules = sorted(self.p.rbacrules()) q = RBACRuleQuery(self.p) - for q_numrules, r in enumerate(q.results(), start=1): - pass + q_rules = sorted(q.results()) - self.assertEqual(numrules, q_numrules) + self.assertListEqual(rules, q_rules) def test_001_source_direct(self): """RBAC rule query with exact, direct, source match.""" diff --git a/tests/rolequery.py b/tests/rolequery.py index 6716879..30dabe1 100644 --- a/tests/rolequery.py +++ b/tests/rolequery.py @@ -29,15 +29,14 @@ class RoleQueryTest(unittest.TestCase): def test_000_unset(self): """Role query with no criteria.""" # query with no parameters gets all types. - for numroles, r in enumerate(self.p.roles(), start=1): - pass + roles = sorted(self.p.roles()) + # remove object_r, as it is skipped from the role query + roles.remove("object_r") q = RoleQuery(self.p) - for q_numroles, t in enumerate(q.results(), start=1): - pass + q_roles = sorted(q.results()) - # numroles-1 as object_r is skipped from the role query - self.assertEqual(numroles - 1, q_numroles) + self.assertListEqual(roles, q_roles) def test_001_name_exact(self): """Role query with exact name match.""" diff --git a/tests/terulequery.py b/tests/terulequery.py index 1a24c95..6fa9260 100644 --- a/tests/terulequery.py +++ b/tests/terulequery.py @@ -30,14 +30,12 @@ class TERuleQueryTest(unittest.TestCase): def test_000_unset(self): """TE rule query with no criteria.""" # query with no parameters gets all TE rules. - for numrules, r in enumerate(self.p.terules(), start=1): - pass + rules = sorted(self.p.terules()) q = TERuleQuery(self.p) - for q_numrules, r in enumerate(q.results(), start=1): - pass + q_rules = sorted(q.results()) - self.assertEqual(numrules, q_numrules) + self.assertListEqual(rules, q_rules) def test_001_source_direct(self): """TE rule query with exact, direct, source match.""" diff --git a/tests/typequery.py b/tests/typequery.py index 2a1ea5d..fde2ab3 100644 --- a/tests/typequery.py +++ b/tests/typequery.py @@ -29,10 +29,10 @@ class TypeQueryTest(unittest.TestCase): def test_000_unset(self): """Type query with no criteria.""" # query with no parameters gets all types. - alltypes = sorted(str(t) for t in self.p.types()) + alltypes = sorted(self.p.types()) q = TypeQuery(self.p) - qtypes = sorted(str(t) for t in q.results()) + qtypes = sorted(q.results()) self.assertListEqual(alltypes, qtypes) diff --git a/tests/userquery.py b/tests/userquery.py index 144a913..558ebcf 100644 --- a/tests/userquery.py +++ b/tests/userquery.py @@ -29,10 +29,10 @@ class UserQueryTest(unittest.TestCase): def test_000_unset(self): """User query with no criteria.""" # query with no parameters gets all types. - allusers = sorted(str(u) for u in self.p.users()) + allusers = sorted(self.p.users()) q = UserQuery(self.p) - qusers = sorted(str(u) for u in q.results()) + qusers = sorted(q.results()) self.assertListEqual(allusers, qusers)