| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
"""Unit tests for the `debsigs-autosign` 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 |
from typing import Final |
| 19 |
|
| 20 |
|
| 21 |
def test_autosign(test_cfg: util.ConfigAggr) -> None: # noqa: F811 # pytest fixture |
| 22 |
"""Sign files in different ways.""" |
| 23 |
if not test_cfg.has_all_features( |
| 24 |
{"cmd-list": "1", "cmd-sign": "1", "i-gpg": "1", "tool-autosign": "0.1"}, |
| 25 |
): |
| 26 |
pytest.skip("No `debsigs-autosign` or `debsigs --list` support") |
| 27 |
|
| 28 |
with test_cfg.cfg_with_tempd() as cfg: |
| 29 |
pub_key: Final = test_cfg.pub_key |
| 30 |
pkg_orig: Final = test_cfg.changes_orig.debs[0] |
| 31 |
|
| 32 |
pkg_signed: Final = cfg.path.work / pkg_orig.name |
| 33 |
shutil.copy2(pkg_orig, pkg_signed) |
| 34 |
assert util.debsigs_list(cfg, pkg_signed) == [] |
| 35 |
|
| 36 |
env_with_path: Final = dict(cfg.env) |
| 37 |
env_with_path["PATH"] = f"{cfg.prog.debsigs.parent}:{env_with_path['PATH']}" |
| 38 |
|
| 39 |
subprocess.run( # noqa: S603 |
| 40 |
[cfg.prog.debsigs.parent / "debsigs-autosign", "maint"], |
| 41 |
check=True, |
| 42 |
cwd=cfg.path.work, |
| 43 |
encoding="UTF-8", |
| 44 |
env=env_with_path, |
| 45 |
input=f"{pkg_signed}\n", |
| 46 |
) |
| 47 |
lines_maint_only: Final = util.debsigs_list(cfg, pkg_signed) |
| 48 |
|
| 49 |
sign_key: Final = util.get_signing_subkey(pub_key) |
| 50 |
match lines_maint_only: |
| 51 |
case [single] if single.sig_type == "maint" and single.key_id == sign_key.key_id: |
| 52 |
pass |
| 53 |
|
| 54 |
case _: |
| 55 |
raise AssertionError(repr(lines_maint_only)) |
| 56 |
|
| 57 |
# OK, let's do that again... |
| 58 |
subprocess.run( # noqa: S603 |
| 59 |
[cfg.prog.debsigs.parent / "debsigs-autosign", "origin"], |
| 60 |
check=True, |
| 61 |
cwd=cfg.path.work, |
| 62 |
encoding="UTF-8", |
| 63 |
env=env_with_path, |
| 64 |
input=f"{pkg_signed}\n", |
| 65 |
) |
| 66 |
lines_both: Final = util.debsigs_list(cfg, pkg_signed) |
| 67 |
|
| 68 |
match lines_both: |
| 69 |
case [ |
| 70 |
maint, |
| 71 |
origin, |
| 72 |
] if ( |
| 73 |
maint.sig_type == "maint" |
| 74 |
and maint.key_id == sign_key.key_id |
| 75 |
and origin.sig_type == "origin" |
| 76 |
and origin.key_id == sign_key.key_id |
| 77 |
): |
| 78 |
pass |
| 79 |
|
| 80 |
case _: |
| 81 |
raise AssertionError(repr(lines_both)) |