aboutsummaryrefslogtreecommitdiff
path: root/sigsum-witness_test.py
blob: 084aa78bb75ade402ba75cb8ea2b4d3956d5d7ed (plain)
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
import importlib

import pytest

witness = importlib.import_module("sigsum-witness")  # To import a module with a '-'


class Test_check_sigkeyfile:
    def test_file_ok(self, tmp_path):
        path = tmp_path / "key"
        path.touch(mode=0o700)
        assert witness.check_sigkeyfile(path) is None

    def test_file_notfound(self, tmp_path):
        path = tmp_path / "key"
        with pytest.raises(
            witness.SigKeyFileError,
            match=f"ERROR: File not found: {path}",
        ):
            witness.check_sigkeyfile(path)

    def test_notafile(self, tmp_path):
        path = tmp_path / "key"
        path.mkdir()
        with pytest.raises(
            witness.SigKeyFileError,
            match=f"ERROR: Signing key file {path} must be a regular file",
        ):
            witness.check_sigkeyfile(path)

    def test_file_badmode(self, tmp_path):
        path = tmp_path / "key"
        path.touch(mode=0o755)
        with pytest.raises(
            witness.SigKeyFileError,
            match=f"ERROR: Signing key file {path} permissions too lax: 0755",
        ):
            witness.check_sigkeyfile(path)

    def test_symlink_ok(self, tmp_path):
        filepath = tmp_path / "thekey"
        filepath.touch(mode=0o700)
        linkpath = tmp_path / "key"
        linkpath.symlink_to(filepath)
        assert witness.check_sigkeyfile(linkpath) is None

    def test_symlink_badmode(self, tmp_path):
        filepath = tmp_path / "thekey"
        filepath.touch(mode=0o755)
        linkpath = tmp_path / "key"
        linkpath.symlink_to(filepath)
        with pytest.raises(
            witness.SigKeyFileError,
            match=f"ERROR: Signing key file {linkpath} permissions too lax: 0755",
        ):
            witness.check_sigkeyfile(linkpath)

    def test_symlink_dangling(self, tmp_path):
        filepath = tmp_path / "thekey"
        linkpath = tmp_path / "key"
        linkpath.symlink_to(filepath)
        with pytest.raises(
            witness.SigKeyFileError,
            match=f"ERROR: File not found: {linkpath}",
        ):
            witness.check_sigkeyfile(linkpath)