| 1 |
# test/test_crypto.py |
| 2 |
# Part of ‘dput’, a Debian package upload toolkit. |
| 3 |
# |
| 4 |
# This is free software, and you are welcome to redistribute it under |
| 5 |
# certain conditions; see the end of this file for copyright |
| 6 |
# information, grant of license, and disclaimer of warranty. |
| 7 |
|
| 8 |
""" Unit tests for ‘crypto’ module. """ |
| 9 |
|
| 10 |
import doctest |
| 11 |
import functools |
| 12 |
import operator |
| 13 |
import sys |
| 14 |
import textwrap |
| 15 |
import unittest.mock |
| 16 |
|
| 17 |
import gpg |
| 18 |
import gpg.results |
| 19 |
import testscenarios |
| 20 |
import testtools |
| 21 |
|
| 22 |
import dput.crypto |
| 23 |
|
| 24 |
from .helper import ( |
| 25 |
patch_system_interfaces, |
| 26 |
set_fake_file_scenario, |
| 27 |
setup_fake_file_fixtures, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
def make_gpg_signature_scenarios(): |
| 32 |
""" Make a collection of scenarios for `gpg.result.Signature` instances. |
| 33 |
|
| 34 |
:return: A sequence of tuples (name, scenario) of scenarios |
| 35 |
for use with `testscenarios` test cases. |
| 36 |
|
| 37 |
Each scenario is a mapping of attributes to be applied to the |
| 38 |
test case. |
| 39 |
""" |
| 40 |
scenarios = [ |
| 41 |
('signature-good validity-unknown', { |
| 42 |
'verify_result': unittest.mock.MagicMock( |
| 43 |
gpg.results.VerifyResult, |
| 44 |
file_name=None, |
| 45 |
signatures=[ |
| 46 |
unittest.mock.MagicMock( |
| 47 |
gpg.results.Signature, |
| 48 |
fpr="BADBEEF2FACEDCADF00DBEEFDECAFBAD", |
| 49 |
status=gpg.errors.NO_ERROR, |
| 50 |
summary=functools.reduce( |
| 51 |
operator.ior, [gpg.constants.SIGSUM_GREEN]), |
| 52 |
validity=gpg.constants.VALIDITY_UNKNOWN, |
| 53 |
), |
| 54 |
], |
| 55 |
), |
| 56 |
'expected_character': "good", |
| 57 |
'expected_description': ( |
| 58 |
"Good signature from F00DBEEFDECAFBAD"), |
| 59 |
}), |
| 60 |
('signature-good validity-never', { |
| 61 |
'verify_result': unittest.mock.MagicMock( |
| 62 |
gpg.results.VerifyResult, |
| 63 |
file_name=None, |
| 64 |
signatures=[ |
| 65 |
unittest.mock.MagicMock( |
| 66 |
gpg.results.Signature, |
| 67 |
fpr="BADBEEF2FACEDCADF00DBEEFDECAFBAD", |
| 68 |
status=gpg.errors.NO_ERROR, |
| 69 |
summary=functools.reduce( |
| 70 |
operator.ior, [gpg.constants.SIGSUM_GREEN]), |
| 71 |
validity=gpg.constants.VALIDITY_NEVER, |
| 72 |
), |
| 73 |
], |
| 74 |
), |
| 75 |
'expected_character': "good", |
| 76 |
'expected_description': ( |
| 77 |
"Good signature from F00DBEEFDECAFBAD"), |
| 78 |
}), |
| 79 |
('signature-good validity-full key-expired', { |
| 80 |
'verify_result': unittest.mock.MagicMock( |
| 81 |
gpg.results.VerifyResult, |
| 82 |
file_name=None, |
| 83 |
signatures=[ |
| 84 |
unittest.mock.MagicMock( |
| 85 |
gpg.results.Signature, |
| 86 |
fpr="BADBEEF2FACEDCADF00DBEEFDECAFBAD", |
| 87 |
status=gpg.errors.NO_ERROR, |
| 88 |
summary=functools.reduce(operator.ior, [ |
| 89 |
gpg.constants.SIGSUM_GREEN, |
| 90 |
gpg.constants.SIGSUM_KEY_EXPIRED, |
| 91 |
]), |
| 92 |
validity=gpg.constants.VALIDITY_FULL, |
| 93 |
), |
| 94 |
], |
| 95 |
), |
| 96 |
'expected_character': "good", |
| 97 |
'expected_description': ( |
| 98 |
"Good signature from F00DBEEFDECAFBAD"), |
| 99 |
}), |
| 100 |
('signature-good validity-full', { |
| 101 |
'verify_result': unittest.mock.MagicMock( |
| 102 |
gpg.results.VerifyResult, |
| 103 |
file_name=None, |
| 104 |
signatures=[ |
| 105 |
unittest.mock.MagicMock( |
| 106 |
gpg.results.Signature, |
| 107 |
fpr="BADBEEF2FACEDCADF00DBEEFDECAFBAD", |
| 108 |
status=gpg.errors.NO_ERROR, |
| 109 |
summary=functools.reduce(operator.ior, [ |
| 110 |
gpg.constants.SIGSUM_VALID, |
| 111 |
gpg.constants.SIGSUM_GREEN, |
| 112 |
]), |
| 113 |
validity=gpg.constants.VALIDITY_FULL, |
| 114 |
), |
| 115 |
], |
| 116 |
), |
| 117 |
'expected_character': "valid", |
| 118 |
'expected_description': ( |
| 119 |
"Valid signature from F00DBEEFDECAFBAD"), |
| 120 |
}), |
| 121 |
('signature-bad', { |
| 122 |
'verify_result': unittest.mock.MagicMock( |
| 123 |
gpg.results.VerifyResult, |
| 124 |
file_name=None, |
| 125 |
signatures=[ |
| 126 |
unittest.mock.MagicMock( |
| 127 |
gpg.results.Signature, |
| 128 |
fpr="BADBEEF2FACEDCADF00DBEEFDECAFBAD", |
| 129 |
status=gpg.errors.BAD_SIGNATURE, |
| 130 |
summary=functools.reduce( |
| 131 |
operator.ior, [gpg.constants.SIGSUM_RED]), |
| 132 |
validity=gpg.constants.VALIDITY_FULL, |
| 133 |
), |
| 134 |
], |
| 135 |
), |
| 136 |
'expected_character': "bad", |
| 137 |
'expected_description': ( |
| 138 |
"Bad signature from F00DBEEFDECAFBAD"), |
| 139 |
}), |
| 140 |
('signature-key-missing', { |
| 141 |
'verify_result': unittest.mock.MagicMock( |
| 142 |
gpg.results.VerifyResult, |
| 143 |
file_name=None, |
| 144 |
signatures=[ |
| 145 |
unittest.mock.MagicMock( |
| 146 |
gpg.results.Signature, |
| 147 |
fpr="BADBEEF2FACEDCADF00DBEEFDECAFBAD", |
| 148 |
status=gpg.errors.NO_PUBKEY, |
| 149 |
summary=functools.reduce( |
| 150 |
operator.ior, |
| 151 |
[gpg.constants.SIGSUM_KEY_MISSING]), |
| 152 |
validity=gpg.constants.VALIDITY_UNKNOWN, |
| 153 |
), |
| 154 |
], |
| 155 |
), |
| 156 |
'expected_character': dput.crypto.SignatureVerifyError, |
| 157 |
'expected_description': ( |
| 158 |
"Error checking signature from F00DBEEFDECAFBAD:" |
| 159 |
" SignatureVerifyError: {:d}".format( |
| 160 |
gpg.constants.SIGSUM_KEY_MISSING) |
| 161 |
), |
| 162 |
}), |
| 163 |
] |
| 164 |
|
| 165 |
return scenarios |
| 166 |
|
| 167 |
|
| 168 |
class characterise_signature_TestCase( |
| 169 |
testscenarios.WithScenarios, |
| 170 |
testtools.TestCase): |
| 171 |
""" Test cases for function `characterise_signature`. """ |
| 172 |
|
| 173 |
scenarios = make_gpg_signature_scenarios() |
| 174 |
|
| 175 |
def test_gives_expected_character(self): |
| 176 |
""" Should give the expected character for signature. """ |
| 177 |
test_args = { |
| 178 |
'signature': self.verify_result.signatures[0], |
| 179 |
} |
| 180 |
if ( |
| 181 |
isinstance(self.expected_character, type) |
| 182 |
and issubclass(self.expected_character, Exception) |
| 183 |
): |
| 184 |
self.assertRaises( |
| 185 |
self.expected_character, |
| 186 |
dput.crypto.characterise_signature, |
| 187 |
**test_args) |
| 188 |
else: |
| 189 |
result = dput.crypto.characterise_signature(**test_args) |
| 190 |
self.assertEqual(result, self.expected_character) |
| 191 |
|
| 192 |
|
| 193 |
class describe_signature_TestCase( |
| 194 |
testscenarios.WithScenarios, |
| 195 |
testtools.TestCase): |
| 196 |
""" Test cases for function `describe_signature`. """ |
| 197 |
|
| 198 |
scenarios = make_gpg_signature_scenarios() |
| 199 |
|
| 200 |
def test_gives_expected_description(self): |
| 201 |
""" Should return expected description for signature. """ |
| 202 |
test_args = { |
| 203 |
'signature': self.verify_result.signatures[0], |
| 204 |
} |
| 205 |
result = dput.crypto.describe_signature(**test_args) |
| 206 |
self.assertEqual(result, self.expected_description) |
| 207 |
|
| 208 |
|
| 209 |
def make_gpg_verify_scenarios(): |
| 210 |
""" Make a collection of scenarios for ‘Context.verify’ method. |
| 211 |
|
| 212 |
:return: A collection of scenarios for tests. |
| 213 |
|
| 214 |
The collection is a mapping from scenario name to a dictionary of |
| 215 |
scenario attributes. |
| 216 |
""" |
| 217 |
signatures_by_name = { |
| 218 |
name: scenario['verify_result'] |
| 219 |
for (name, scenario) in make_gpg_signature_scenarios()} |
| 220 |
|
| 221 |
scenarios_by_name = { |
| 222 |
'goodsig': { |
| 223 |
'result': [ |
| 224 |
None, |
| 225 |
signatures_by_name['signature-good validity-unknown'], |
| 226 |
], |
| 227 |
}, |
| 228 |
'validsig': { |
| 229 |
'result': [ |
| 230 |
None, |
| 231 |
signatures_by_name['signature-good validity-full'], |
| 232 |
], |
| 233 |
}, |
| 234 |
'badsig': { |
| 235 |
'exception': gpg.errors.GPGMEError( |
| 236 |
gpg._gpgme.gpgme_err_make( |
| 237 |
gpg.errors.SOURCE_GPGME, gpg.errors.BAD_SIGNATURE), |
| 238 |
"Bad signature"), |
| 239 |
}, |
| 240 |
'errsig': { |
| 241 |
'exception': gpg.errors.GPGMEError( |
| 242 |
gpg._gpgme.gpgme_err_make( |
| 243 |
gpg.errors.SOURCE_GPGME, gpg.errors.SIG_EXPIRED), |
| 244 |
"Signature expired"), |
| 245 |
}, |
| 246 |
'nodata': { |
| 247 |
'exception': gpg.errors.GPGMEError( |
| 248 |
gpg._gpgme.gpgme_err_make( |
| 249 |
gpg.errors.SOURCE_GPGME, gpg.errors.NO_DATA), |
| 250 |
"No data"), |
| 251 |
}, |
| 252 |
'bogus': { |
| 253 |
'exception': ValueError, |
| 254 |
}, |
| 255 |
} |
| 256 |
|
| 257 |
scenarios = { |
| 258 |
'default': scenarios_by_name['goodsig'], |
| 259 |
} |
| 260 |
scenarios.update( |
| 261 |
(name, scenario) |
| 262 |
for (name, scenario) in scenarios_by_name.items()) |
| 263 |
|
| 264 |
return scenarios |
| 265 |
|
| 266 |
|
| 267 |
def setup_gpg_verify_fixtures(testcase): |
| 268 |
""" Set up fixtures for GPG interaction behaviour. """ |
| 269 |
scenarios = make_gpg_verify_scenarios() |
| 270 |
testcase.gpg_verify_scenarios = scenarios |
| 271 |
|
| 272 |
|
| 273 |
class check_file_signature_TestCase(testtools.TestCase): |
| 274 |
""" Test cases for `check_file_signature` function. """ |
| 275 |
|
| 276 |
def setUp(self): |
| 277 |
""" Set up test fixtures. """ |
| 278 |
super().setUp() |
| 279 |
patch_system_interfaces(self) |
| 280 |
|
| 281 |
setup_fake_file_fixtures(self) |
| 282 |
set_fake_file_scenario(self, 'exist-minimal') |
| 283 |
|
| 284 |
self.set_test_args() |
| 285 |
|
| 286 |
self.patch_gpg_context() |
| 287 |
|
| 288 |
setup_gpg_verify_fixtures(self) |
| 289 |
self.set_gpg_verify_scenario('default') |
| 290 |
|
| 291 |
def set_test_args(self): |
| 292 |
""" Set the arguments for the test call to the function. """ |
| 293 |
self.test_args = dict( |
| 294 |
infile=self.file_double.fake_file, |
| 295 |
) |
| 296 |
|
| 297 |
def patch_gpg_context(self): |
| 298 |
""" Patch the ‘gpg.Context’ class for this test case. """ |
| 299 |
class_patcher = unittest.mock.patch.object(gpg, 'Context') |
| 300 |
class_patcher.start() |
| 301 |
self.addCleanup(class_patcher.stop) |
| 302 |
|
| 303 |
def set_gpg_verify_scenario(self, name): |
| 304 |
""" Set the status scenario for the ‘Context.verify’ call. """ |
| 305 |
scenario = self.gpg_verify_scenarios[name] |
| 306 |
mock_class = gpg.Context |
| 307 |
self.mock_gpg_context = mock_class.return_value |
| 308 |
mock_func = self.mock_gpg_context.verify |
| 309 |
if 'exception' in scenario: |
| 310 |
mock_func.side_effect = scenario['exception'] |
| 311 |
else: |
| 312 |
mock_func.return_value = scenario['result'] |
| 313 |
|
| 314 |
def assert_stderr_contains_gpg_error(self, code): |
| 315 |
""" Assert the `stderr` content contains the GPG message. """ |
| 316 |
expected_output = textwrap.dedent("""\ |
| 317 |
gpg: {path}: error {code}: ... |
| 318 |
""").format( |
| 319 |
path=self.file_double.path, code=code) |
| 320 |
self.assertThat( |
| 321 |
sys.stderr.getvalue(), |
| 322 |
testtools.matchers.DocTestMatches( |
| 323 |
expected_output, doctest.ELLIPSIS)) |
| 324 |
|
| 325 |
def test_calls_gpg_verify_with_expected_args(self): |
| 326 |
""" Should call `gpg.Context.verify` with expected args. """ |
| 327 |
dput.crypto.check_file_signature(**self.test_args) |
| 328 |
gpg.Context.return_value.verify.assert_called_with( |
| 329 |
self.file_double.fake_file) |
| 330 |
|
| 331 |
def test_calls_sys_exit_if_gnupg_reports_bad_signature(self): |
| 332 |
""" Should call `sys.exit` if GnuPG reports bad signature. """ |
| 333 |
self.set_gpg_verify_scenario('badsig') |
| 334 |
with testtools.ExpectedException(gpg.errors.GPGMEError): |
| 335 |
dput.crypto.check_file_signature(**self.test_args) |
| 336 |
self.assert_stderr_contains_gpg_error(gpg.errors.BAD_SIGNATURE) |
| 337 |
|
| 338 |
def test_calls_sys_exit_if_gnupg_reports_sig_expired(self): |
| 339 |
""" Should call `sys.exit` if GnuPG reports signature expired. """ |
| 340 |
self.set_gpg_verify_scenario('errsig') |
| 341 |
with testtools.ExpectedException(gpg.errors.GPGMEError): |
| 342 |
dput.crypto.check_file_signature(**self.test_args) |
| 343 |
self.assert_stderr_contains_gpg_error(gpg.errors.SIG_EXPIRED) |
| 344 |
|
| 345 |
def test_calls_sys_exit_if_gnupg_reports_nodata(self): |
| 346 |
""" Should call `sys.exit` if GnuPG reports no data. """ |
| 347 |
self.set_gpg_verify_scenario('nodata') |
| 348 |
with testtools.ExpectedException(gpg.errors.GPGMEError): |
| 349 |
dput.crypto.check_file_signature(**self.test_args) |
| 350 |
self.assert_stderr_contains_gpg_error(gpg.errors.NO_DATA) |
| 351 |
|
| 352 |
def test_outputs_message_if_gnupg_reports_goodsig(self): |
| 353 |
""" Should output a message if GnuPG reports a good signature. """ |
| 354 |
self.set_gpg_verify_scenario('goodsig') |
| 355 |
dput.crypto.check_file_signature(**self.test_args) |
| 356 |
expected_output = textwrap.dedent("""\ |
| 357 |
gpg: {path}: Good signature from ... |
| 358 |
""").format(path=self.file_double.path) |
| 359 |
self.assertThat( |
| 360 |
sys.stderr.getvalue(), |
| 361 |
testtools.matchers.DocTestMatches( |
| 362 |
expected_output, doctest.ELLIPSIS)) |
| 363 |
|
| 364 |
def test_outputs_message_if_gnupg_reports_validsig(self): |
| 365 |
""" Should output a message if GnuPG reports a valid signature. """ |
| 366 |
self.set_gpg_verify_scenario('validsig') |
| 367 |
dput.crypto.check_file_signature(**self.test_args) |
| 368 |
expected_output = textwrap.dedent("""\ |
| 369 |
gpg: {path}: Valid signature from ... |
| 370 |
""").format(path=self.file_double.path) |
| 371 |
self.assertThat( |
| 372 |
sys.stderr.getvalue(), |
| 373 |
testtools.matchers.DocTestMatches( |
| 374 |
expected_output, doctest.ELLIPSIS)) |
| 375 |
|
| 376 |
|
| 377 |
# Copyright © 2015–2026 Ben Finney <[email protected]> |
| 378 |
# |
| 379 |
# This is free software: you may copy, modify, and/or distribute this work |
| 380 |
# under the terms of the GNU General Public License as published by the |
| 381 |
# Free Software Foundation; version 3 of that license or any later version. |
| 382 |
# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details. |
| 383 |
|
| 384 |
|
| 385 |
# Local variables: |
| 386 |
# coding: utf-8 |
| 387 |
# mode: python |
| 388 |
# End: |
| 389 |
# vim: fileencoding=utf-8 filetype=python : |