| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
"""Unit tests for the main functionality of the `debsigs` tool.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import shutil |
| 8 |
import subprocess # noqa: S404 |
| 9 |
import typing |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from . import util |
| 14 |
from .util import test_cfg # noqa: F401 # pytest fixture |
| 15 |
|
| 16 |
|
| 17 |
if typing.TYPE_CHECKING: |
| 18 |
import pathlib |
| 19 |
from typing import Final |
| 20 |
|
| 21 |
from testsigs import defs |
| 22 |
|
| 23 |
|
| 24 |
def test_list(test_cfg: util.ConfigAggr) -> None: # noqa: F811 # pytest fixture |
| 25 |
"""Make sure `debsigs --list` on an unsigned package does not return anything.""" |
| 26 |
with test_cfg.cfg_with_tempd() as cfg: |
| 27 |
if not test_cfg.has_feature("cmd-list", "1"): |
| 28 |
pytest.skip("No `debsigs --list` support") |
| 29 |
|
| 30 |
assert util.debsigs_list(cfg, test_cfg.changes_orig.debs[0]) == [] |
| 31 |
|
| 32 |
|
| 33 |
def _copy_and_sign_package(cfg: defs.Config, pkg_orig: pathlib.Path, sigtype: str) -> pathlib.Path: |
| 34 |
"""Copy the package file, run `debsigs --sign`.""" |
| 35 |
pkg_signed: Final = cfg.path.work / pkg_orig.name |
| 36 |
shutil.copy2(pkg_orig, pkg_signed) |
| 37 |
|
| 38 |
subprocess.check_call( # noqa: S603 |
| 39 |
[cfg.prog.debsigs, f"--sign={sigtype}", "--", pkg_signed.name], |
| 40 |
cwd=cfg.path.work, |
| 41 |
env=cfg.env, |
| 42 |
) |
| 43 |
|
| 44 |
cmp_res: Final = subprocess.run( # noqa: S603 |
| 45 |
["cmp", "--", pkg_orig, pkg_signed], # noqa: S607 |
| 46 |
check=False, |
| 47 |
cwd=cfg.path.work, |
| 48 |
env=cfg.env, |
| 49 |
) |
| 50 |
if cmp_res.returncode == 0: |
| 51 |
raise RuntimeError(repr((cfg.prog.debsigs, pkg_orig, pkg_signed))) |
| 52 |
|
| 53 |
return pkg_signed |
| 54 |
|
| 55 |
|
| 56 |
def _test_ar_sections(cfg: defs.Config, pkg_orig: pathlib.Path, pkg_signed: pathlib.Path) -> None: |
| 57 |
"""Make sure the signed Debian package has exactly one more archive section.""" |
| 58 |
sections_orig: Final = subprocess.check_output( # noqa: S603 |
| 59 |
["ar", "t", "--", pkg_orig], # noqa: S607 |
| 60 |
cwd=cfg.path.work, |
| 61 |
encoding="UTF-8", |
| 62 |
env=cfg.env, |
| 63 |
).splitlines() |
| 64 |
if any(name.startswith("_gpg") for name in sections_orig): |
| 65 |
raise RuntimeError(repr(sections_orig)) |
| 66 |
|
| 67 |
sections_signed: Final = subprocess.check_output( # noqa: S603 |
| 68 |
["ar", "t", "--", pkg_signed], # noqa: S607 |
| 69 |
cwd=cfg.path.work, |
| 70 |
encoding="UTF-8", |
| 71 |
env=cfg.env, |
| 72 |
).splitlines() |
| 73 |
if sections_signed != [*sections_orig, "_gpgorigin"]: |
| 74 |
raise RuntimeError(repr((sections_orig, sections_signed))) |
| 75 |
|
| 76 |
|
| 77 |
def _test_with_debsigs(tcfg: util.ConfigAggr, cfg: defs.Config, pkg_signed: pathlib.Path) -> None: |
| 78 |
"""Run `debsigs --verify` now that it has actually been implemented.""" |
| 79 |
if not tcfg.has_feature("cmd-verify", "1"): |
| 80 |
pytest.skip("No `debsigs --verify` support") |
| 81 |
|
| 82 |
subprocess.check_call( # noqa: S603 |
| 83 |
[cfg.prog.debsigs, "--verify", pkg_signed], |
| 84 |
cwd=cfg.path.work, |
| 85 |
env=cfg.env, |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
def _test_with_debsig_verify( |
| 90 |
cfg: defs.Config, |
| 91 |
keys: defs.KeyFiles, |
| 92 |
pkg_signed: pathlib.Path, |
| 93 |
sign_fpr: str, |
| 94 |
) -> None: |
| 95 |
"""Prepare the policy file for `debsig-verify` and run it.""" |
| 96 |
# Copied over from debsig-verify's `test/policies/*/generic.pol` |
| 97 |
policy_text: Final = f"""<?xml version="1.0"?> |
| 98 |
<!DOCTYPE Policy SYSTEM "https://www.debian.org/debsig/1.0/policy.dtd"> |
| 99 |
<Policy xmlns="https://www.debian.org/debsig/1.0/"> |
| 100 |
|
| 101 |
<!-- This is mainly a sanity check, since our filename is that of the ID |
| 102 |
anyway. --> |
| 103 |
<Origin Name="origin" id="{sign_fpr}" Description="Debsig testing"/> |
| 104 |
|
| 105 |
<!-- This is required to match in order for this policy to be used. We |
| 106 |
reject the release Type, since we want a different rule set for |
| 107 |
that. --> |
| 108 |
<Selection> |
| 109 |
<Required Type="origin" File="{keys.public_bin.name}" id="{sign_fpr}"/> |
| 110 |
</Selection> |
| 111 |
|
| 112 |
<!-- Once we decide to use this policy, this must pass in order to verify |
| 113 |
the package. --> |
| 114 |
<Verification MinOptional="0"> |
| 115 |
<Required Type="origin" File="{keys.public_bin.name}" id="{sign_fpr}"/> |
| 116 |
</Verification> |
| 117 |
|
| 118 |
</Policy> |
| 119 |
""" |
| 120 |
print(policy_text) # noqa: T201 |
| 121 |
|
| 122 |
poldir: Final = cfg.path.work / "policies" |
| 123 |
poldir.mkdir(mode=0o755, exist_ok=True) |
| 124 |
polsub: Final = poldir / sign_fpr |
| 125 |
polsub.mkdir(mode=0o755, exist_ok=True) |
| 126 |
(polsub / "origin.pol").write_text(policy_text, encoding="UTF-8") |
| 127 |
|
| 128 |
shutil.copy2(keys.public_bin, polsub) |
| 129 |
|
| 130 |
subprocess.check_call( # noqa: S603 |
| 131 |
[ |
| 132 |
cfg.prog.debsig_verify, |
| 133 |
"-d", |
| 134 |
"--policies-dir", |
| 135 |
poldir, |
| 136 |
"--keyrings-dir", |
| 137 |
poldir, |
| 138 |
"--list-policies", |
| 139 |
pkg_signed, |
| 140 |
], |
| 141 |
cwd=cfg.path.work, |
| 142 |
env=cfg.env, |
| 143 |
) |
| 144 |
|
| 145 |
subprocess.check_call( # noqa: S603 |
| 146 |
[ |
| 147 |
cfg.prog.debsig_verify, |
| 148 |
"-d", |
| 149 |
"--policies-dir", |
| 150 |
poldir, |
| 151 |
"--keyrings-dir", |
| 152 |
poldir, |
| 153 |
pkg_signed, |
| 154 |
], |
| 155 |
cwd=cfg.path.work, |
| 156 |
env=cfg.env, |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
def test_sign_and_verify(test_cfg: util.ConfigAggr) -> None: # noqa: F811 # pytest fixture |
| 161 |
"""Prepare the working directory, run the tests.""" |
| 162 |
if not test_cfg.has_all_features({"cmd-list": "1", "cmd-sign": "1", "i-gpg": "1"}): |
| 163 |
pytest.skip("No `debsigs --list`, `--sign`, or GnuPG support") |
| 164 |
|
| 165 |
with test_cfg.cfg_with_tempd() as cfg: |
| 166 |
keys: Final = test_cfg.keys |
| 167 |
pub_key: Final = test_cfg.pub_key |
| 168 |
pkg_orig: Final = test_cfg.changes_orig.debs[0] |
| 169 |
|
| 170 |
sign_key: Final = util.get_signing_subkey(pub_key) |
| 171 |
|
| 172 |
sigs_empty: Final = util.debsigs_list(cfg, pkg_orig) |
| 173 |
if sigs_empty: |
| 174 |
raise RuntimeError(repr((pkg_orig, sigs_empty))) |
| 175 |
|
| 176 |
pkg_signed: Final = _copy_and_sign_package(cfg, pkg_orig, "origin") |
| 177 |
sigs_single_origin: Final = util.debsigs_list(cfg, pkg_signed) |
| 178 |
match sigs_single_origin: |
| 179 |
case [single] if single.sig_type == "origin" and single.key_id == sign_key.key_id: |
| 180 |
pass |
| 181 |
|
| 182 |
case _: |
| 183 |
raise RuntimeError(repr((pkg_signed, sigs_single_origin, pub_key))) |
| 184 |
|
| 185 |
_test_ar_sections(cfg, pkg_orig, pkg_signed) |
| 186 |
_test_with_debsigs(test_cfg, cfg, pkg_signed) |
| 187 |
_test_with_debsig_verify(cfg, keys, pkg_signed, sign_key.fpr) |