File: util.py

package info (click to toggle)
setools 4.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,600 kB
  • sloc: python: 24,485; makefile: 14
file content (50 lines) | stat: -rw-r--r-- 1,582 bytes parent folder | download
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
"""Unit test mixin classes."""
# Copyright 2015, Tresys Technology, LLC
#
# SPDX-License-Identifier: GPL-2.0-only
#
# pylint: disable=too-few-public-methods
import pytest
import setools


def validate_rule(rule: setools.policyrep.PolicyRule,
                  ruletype: setools.policyrep.PolicyEnum | str,
                  source: setools.policyrep.PolicySymbol | str,
                  target: setools.policyrep.PolicySymbol | str,
                  /, *,
                  tclass: setools.ObjClass | str | None = None,
                  perms: set[str] | setools.XpermSet | None = None,
                  default: setools.policyrep.PolicySymbol | str | None = None,
                  cond: str | None = None,
                  cond_block: bool | None = None,
                  xperm: str | None = None) -> None:

    """Validate a rule."""
    assert ruletype == rule.ruletype
    assert source == rule.source
    assert target == rule.target

    if tclass is not None:
        assert tclass == rule.tclass

    if perms is not None:
        assert perms == rule.perms

    elif default is not None:
        assert default == rule.default

    if cond:
        assert cond == rule.conditional
        assert cond_block == rule.conditional_block
    else:
        with pytest.raises(setools.exception.RuleNotConditional):
            rule.conditional
        with pytest.raises(setools.exception.RuleNotConditional):
            rule.conditional_block

    if xperm:
        assert xperm == rule.xperm_type
        assert rule.extended
    else:
        assert not rule.extended