2015-03-06 14:19:35 +00:00
|
|
|
# Copyright 2015, Tresys Technology, LLC
|
|
|
|
#
|
2021-11-20 19:03:26 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
2015-03-06 14:19:35 +00:00
|
|
|
#
|
2018-01-18 22:14:40 +00:00
|
|
|
import os
|
2015-03-06 14:19:35 +00:00
|
|
|
import unittest
|
|
|
|
|
2018-01-18 22:14:40 +00:00
|
|
|
from setools import CategoryQuery
|
|
|
|
|
|
|
|
from .policyrep.util import compile_policy
|
2015-03-06 14:19:35 +00:00
|
|
|
|
|
|
|
|
2015-03-07 14:39:01 +00:00
|
|
|
class CategoryQueryTest(unittest.TestCase):
|
2015-03-06 14:19:35 +00:00
|
|
|
|
2015-03-18 13:44:16 +00:00
|
|
|
@classmethod
|
2015-04-02 18:50:21 +00:00
|
|
|
def setUpClass(cls):
|
2018-01-18 22:14:40 +00:00
|
|
|
cls.p = compile_policy("tests/categoryquery.conf")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
os.unlink(cls.p.path)
|
2015-03-06 14:19:35 +00:00
|
|
|
|
|
|
|
def test_000_unset(self):
|
|
|
|
"""MLS category query with no criteria."""
|
|
|
|
# query with no parameters gets all categories.
|
2015-03-07 14:39:01 +00:00
|
|
|
allcats = sorted(str(c) for c in self.p.categories())
|
2015-03-06 14:19:35 +00:00
|
|
|
|
2015-03-07 14:39:01 +00:00
|
|
|
q = CategoryQuery(self.p)
|
2015-03-06 14:19:35 +00:00
|
|
|
qcats = sorted(str(c) for c in q.results())
|
|
|
|
|
|
|
|
self.assertListEqual(allcats, qcats)
|
|
|
|
|
|
|
|
def test_001_name_exact(self):
|
|
|
|
"""MLS category query with exact name match."""
|
2015-03-07 14:39:01 +00:00
|
|
|
q = CategoryQuery(self.p, name="test1")
|
2015-03-06 14:19:35 +00:00
|
|
|
|
|
|
|
cats = sorted(str(c) for c in q.results())
|
|
|
|
self.assertListEqual(["test1"], cats)
|
|
|
|
|
|
|
|
def test_002_name_regex(self):
|
|
|
|
"""MLS category query with regex name match."""
|
2015-03-07 14:39:01 +00:00
|
|
|
q = CategoryQuery(self.p, name="test2(a|b)", name_regex=True)
|
2015-03-06 14:19:35 +00:00
|
|
|
|
|
|
|
cats = sorted(str(c) for c in q.results())
|
|
|
|
self.assertListEqual(["test2a", "test2b"], cats)
|
|
|
|
|
|
|
|
def test_010_alias_exact(self):
|
|
|
|
"""MLS category query with exact alias match."""
|
2015-03-07 14:39:01 +00:00
|
|
|
q = CategoryQuery(self.p, alias="test10a")
|
2015-03-06 14:19:35 +00:00
|
|
|
|
|
|
|
cats = sorted(str(t) for t in q.results())
|
|
|
|
self.assertListEqual(["test10c1"], cats)
|
|
|
|
|
|
|
|
def test_011_alias_regex(self):
|
|
|
|
"""MLS category query with regex alias match."""
|
2015-03-07 14:39:01 +00:00
|
|
|
q = CategoryQuery(self.p, alias="test11(a|b)", alias_regex=True)
|
2015-03-06 14:19:35 +00:00
|
|
|
|
|
|
|
cats = sorted(str(t) for t in q.results())
|
|
|
|
self.assertListEqual(["test11c1", "test11c2"], cats)
|