File: test_checker.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 (81 lines) | stat: -rw-r--r-- 3,580 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
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
# Copyright 2020, Microsoft Corporation
#
# SPDX-License-Identifier: GPL-2.0-only
#
import os
from unittest.mock import Mock

import pytest
import setools


@pytest.mark.obj_args("tests/library/checker/checker.conf")
class TestPolicyChecker:

    def test_config_empty(self, compiled_policy: setools.SELinuxPolicy) -> None:
        """Test empty config file"""
        with pytest.raises(setools.exception.InvalidCheckerConfig):
            setools.checker.PolicyChecker(compiled_policy, os.devnull)

    def test_config_check_missing_type(self, compiled_policy: setools.SELinuxPolicy) -> None:
        """Test check missing check type"""
        with pytest.raises(setools.exception.InvalidCheckerModule):
            setools.checker.PolicyChecker(compiled_policy,
                                          "tests/library/checker/checker-missingtype.ini")

    def test_config_check_invalid_type(self, compiled_policy: setools.SELinuxPolicy) -> None:
        """Test check invalid check type"""
        with pytest.raises(setools.exception.InvalidCheckerModule):
            setools.checker.PolicyChecker(compiled_policy,
                                          "tests/library/checker/checker-invalidtype.ini")

    def test_config_check_invalid_option(self, compiled_policy: setools.SELinuxPolicy) -> None:
        """Test check invalid check option"""
        with pytest.raises(setools.exception.InvalidCheckOption):
            setools.checker.PolicyChecker(compiled_policy,
                                          "tests/library/checker/checker-invalidoption.ini")

    def test_config_check_invalid_value(self, compiled_policy: setools.SELinuxPolicy) -> None:
        """Test check invalid check type"""
        with pytest.raises(setools.exception.InvalidCheckValue):
            setools.checker.PolicyChecker(compiled_policy,
                                          "tests/library/checker/checker-invalidvalue.ini")

    def test_run_pass(self, compiled_policy: setools.SELinuxPolicy) -> None:
        """Test run with passing config."""
        with open(os.devnull, "w", encoding="utf-8") as fd:
            checker = setools.checker.PolicyChecker(compiled_policy,
                                                    "tests/library/checker/checker-valid.ini")

            # create additional disabled mock test
            newcheck = Mock()
            newcheck.checkname = "disabled"
            newcheck.disable = True
            newcheck.validate_config.return_value = None
            newcheck.run.return_value = []
            checker.checks.append(newcheck)

            assert 5 == len(checker.checks)
            result = checker.run(output=fd)
            assert 0 == result
            newcheck.run.assert_not_called()

    def test_run_fail(self, compiled_policy: setools.SELinuxPolicy) -> None:
        """Test run with failing config."""
        with open(os.devnull, "w", encoding="utf-8") as fd:
            checker = setools.checker.PolicyChecker(compiled_policy,
                                                    "tests/library/checker/checker-valid.ini")

            # create additional failing mock test
            newcheck = Mock()
            newcheck.checkname = "failing test"
            newcheck.disable = False
            newcheck.validate_config.return_value = None
            newcheck.run.return_value = list(range(13))
            checker.checks.append(newcheck)

            assert 5 == len(checker.checks)

            result = checker.run(output=fd)
            newcheck.run.assert_called()
            assert 13 == result