setools/tests/commonquery.py
2014-10-24 21:23:13 -04:00

75 lines
2.6 KiB
Python

# Copyright 2014, Tresys Technology, LLC
#
# This file is part of SETools.
#
# SETools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# SETools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SETools. If not, see <http://www.gnu.org/licenses/>.
#
import unittest
from setools import SELinuxPolicy
from setools.commonquery import CommonQuery
class CommonQueryTest(unittest.TestCase):
def setUp(self):
self.p = SELinuxPolicy("tests/commonquery.conf")
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
q = CommonQuery(self.p)
for q_numcommons, u in enumerate(q.results(), start=1):
pass
self.assertEqual(numcommons, q_numcommons)
def test_001_name_exact(self):
"""Common query with exact name match."""
q = CommonQuery(self.p, name="test1")
commons = sorted(str(c) for c in q.results())
self.assertListEqual(["test1"], commons)
def test_002_name_regex(self):
"""Common query with regex name match."""
q = CommonQuery(self.p, name="test2(a|b)", name_regex=True)
commons = sorted(str(c) for c in q.results())
self.assertListEqual(["test2a", "test2b"], commons)
def test_010_perm_indirect_intersect(self):
"""Common query with intersect permission name patch."""
q = CommonQuery(self.p, perms=set(["null"]), perms_equal=False)
commons = sorted(str(c) for c in q.results())
self.assertListEqual(["test10a", "test10b"], commons)
def test_011_perm_indirect_equal(self):
"""Common query with equal permission name patch."""
q = CommonQuery(self.p, perms=set(["read", "write"]), perms_equal=True)
commons = sorted(str(c) for c in q.results())
self.assertListEqual(["test11a"], commons)
def test_012_perm_indirect_regex(self):
"""Common query with regex permission name patch."""
q = CommonQuery(self.p, perms="sig.+", perms_regex=True)
commons = sorted(str(c) for c in q.results())
self.assertListEqual(["test12a", "test12b"], commons)