File: constraintquery.py

package info (click to toggle)
setools 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,900 kB
  • sloc: python: 20,968; makefile: 14
file content (128 lines) | stat: -rw-r--r-- 4,775 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright 2015, 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 os
import unittest

from setools import ConstraintQuery

from .policyrep.util import compile_policy


class ConstraintQueryTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.p = compile_policy("tests/constraintquery.conf")

    @classmethod
    def tearDownClass(cls):
        os.unlink(cls.p.path)

    def test_000_unset(self):
        """Constraint query with no criteria."""
        allconstraint = sorted(c.tclass for c in self.p.constraints())

        q = ConstraintQuery(self.p)
        qconstraint = sorted(c.tclass for c in q.results())

        self.assertListEqual(allconstraint, qconstraint)

    def test_001_ruletype(self):
        """Constraint query with rule type match."""
        q = ConstraintQuery(self.p, ruletype=["mlsconstrain"])

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test1"], constraint)

    @unittest.skip("Setting tclass to a string is no longer supported.")
    def test_010_class_exact(self):
        """Constraint query with exact object class match."""
        q = ConstraintQuery(self.p, tclass="test10")

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test10"], constraint)

    def test_011_class_list(self):
        """Constraint query with object class list match."""
        q = ConstraintQuery(self.p, tclass=["test11a", "test11b"])

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test11a", "test11b"], constraint)

    def test_012_class_regex(self):
        """Constraint query with object class regex match."""
        q = ConstraintQuery(self.p, tclass="test12(a|c)", tclass_regex=True)

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test12a", "test12c"], constraint)

    def test_020_perms_any(self):
        """Constraint query with permission set intersection match."""
        q = ConstraintQuery(self.p, perms=["test20ap", "test20bp"])

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test20a", "test20b"], constraint)

    def test_021_perms_equal(self):
        """Constraint query with permission set equality match."""
        q = ConstraintQuery(self.p, perms=["test21ap", "test21bp"], perms_equal=True)

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test21c"], constraint)

    def test_030_role_match_single(self):
        """Constraint query with role match."""
        q = ConstraintQuery(self.p, role="test30r")

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test30"], constraint)

    def test_031_role_match_regex(self):
        """Constraint query with regex role match."""
        q = ConstraintQuery(self.p, role="test31r.", role_regex=True)

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test31a", "test31b"], constraint)

    def test_040_type_match_single(self):
        """Constraint query with type match."""
        q = ConstraintQuery(self.p, type_="test40t")

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test40"], constraint)

    def test_041_type_match_regex(self):
        """Constraint query with regex type match."""
        q = ConstraintQuery(self.p, type_="test41t.", type_regex=True)

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test41a", "test41b"], constraint)

    def test_050_user_match_single(self):
        """Constraint query with user match."""
        q = ConstraintQuery(self.p, user="test50u")

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test50"], constraint)

    def test_051_user_match_regex(self):
        """Constraint query with regex user match."""
        q = ConstraintQuery(self.p, user="test51u.", user_regex=True)

        constraint = sorted(c.tclass for c in q.results())
        self.assertListEqual(["test51a", "test51b"], constraint)