test_signature.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. # test_signature.py -- tests for signature.py
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for signature vendors."""
  22. import shutil
  23. import subprocess
  24. import unittest
  25. from dulwich.config import ConfigDict
  26. from dulwich.signature import (
  27. SIGNATURE_FORMAT_OPENPGP,
  28. SIGNATURE_FORMAT_SSH,
  29. SIGNATURE_FORMAT_X509,
  30. GPGCliSignatureVendor,
  31. GPGSignatureVendor,
  32. SignatureVendor,
  33. SSHCliSignatureVendor,
  34. SSHSigSignatureVendor,
  35. detect_signature_format,
  36. get_signature_vendor,
  37. get_signature_vendor_for_signature,
  38. )
  39. try:
  40. import gpg
  41. except ImportError:
  42. gpg = None
  43. class SignatureVendorTests(unittest.TestCase):
  44. """Tests for SignatureVendor base class."""
  45. def test_sign_not_implemented(self) -> None:
  46. """Test that sign raises NotImplementedError."""
  47. vendor = SignatureVendor()
  48. with self.assertRaises(NotImplementedError):
  49. vendor.sign(b"test data")
  50. def test_verify_not_implemented(self) -> None:
  51. """Test that verify raises NotImplementedError."""
  52. vendor = SignatureVendor()
  53. with self.assertRaises(NotImplementedError):
  54. vendor.verify(b"test data", b"fake signature")
  55. @unittest.skipIf(gpg is None, "gpg not available")
  56. class GPGSignatureVendorTests(unittest.TestCase):
  57. """Tests for GPGSignatureVendor."""
  58. def test_min_trust_level_from_config(self) -> None:
  59. """Test reading gpg.minTrustLevel from config."""
  60. config = ConfigDict()
  61. config.set((b"gpg",), b"minTrustLevel", b"marginal")
  62. vendor = GPGSignatureVendor(config=config)
  63. self.assertEqual(vendor.min_trust_level, "marginal")
  64. def test_min_trust_level_default(self) -> None:
  65. """Test default when gpg.minTrustLevel not in config."""
  66. vendor = GPGSignatureVendor()
  67. self.assertIsNone(vendor.min_trust_level)
  68. def test_sign_and_verify(self) -> None:
  69. """Test basic sign and verify cycle.
  70. Note: This test requires a GPG key to be configured in the test
  71. environment. It may be skipped in environments without GPG setup.
  72. """
  73. vendor = GPGSignatureVendor()
  74. test_data = b"test data to sign"
  75. try:
  76. # Sign the data
  77. signature = vendor.sign(test_data)
  78. self.assertIsInstance(signature, bytes)
  79. self.assertGreater(len(signature), 0)
  80. # Verify the signature
  81. vendor.verify(test_data, signature)
  82. except gpg.errors.GPGMEError as e:
  83. # Skip test if no GPG key is available
  84. self.skipTest(f"GPG key not available: {e}")
  85. def test_verify_invalid_signature(self) -> None:
  86. """Test that verify raises an error for invalid signatures."""
  87. vendor = GPGSignatureVendor()
  88. test_data = b"test data"
  89. invalid_signature = b"this is not a valid signature"
  90. with self.assertRaises(gpg.errors.GPGMEError):
  91. vendor.verify(test_data, invalid_signature)
  92. def test_sign_with_keyid(self) -> None:
  93. """Test signing with a specific key ID.
  94. Note: This test requires a GPG key to be configured in the test
  95. environment. It may be skipped in environments without GPG setup.
  96. """
  97. vendor = GPGSignatureVendor()
  98. test_data = b"test data to sign"
  99. try:
  100. # Try to get a key from the keyring
  101. with gpg.Context() as ctx:
  102. keys = list(ctx.keylist(secret=True))
  103. if not keys:
  104. self.skipTest("No GPG keys available for testing")
  105. key = keys[0]
  106. signature = vendor.sign(test_data, keyid=key.fpr)
  107. self.assertIsInstance(signature, bytes)
  108. self.assertGreater(len(signature), 0)
  109. # Verify the signature
  110. vendor.verify(test_data, signature)
  111. except gpg.errors.GPGMEError as e:
  112. self.skipTest(f"GPG key not available: {e}")
  113. class GPGCliSignatureVendorTests(unittest.TestCase):
  114. """Tests for GPGCliSignatureVendor."""
  115. def setUp(self) -> None:
  116. """Check if gpg command is available."""
  117. if shutil.which("gpg") is None:
  118. self.skipTest("gpg command not available")
  119. def test_sign_and_verify(self) -> None:
  120. """Test basic sign and verify cycle using CLI."""
  121. vendor = GPGCliSignatureVendor()
  122. test_data = b"test data to sign"
  123. try:
  124. # Sign the data
  125. signature = vendor.sign(test_data)
  126. self.assertIsInstance(signature, bytes)
  127. self.assertGreater(len(signature), 0)
  128. self.assertTrue(signature.startswith(b"-----BEGIN PGP SIGNATURE-----"))
  129. # Verify the signature
  130. vendor.verify(test_data, signature)
  131. except subprocess.CalledProcessError as e:
  132. # Skip test if no GPG key is available or configured
  133. self.skipTest(f"GPG signing failed: {e}")
  134. def test_verify_invalid_signature(self) -> None:
  135. """Test that verify raises an error for invalid signatures."""
  136. vendor = GPGCliSignatureVendor()
  137. test_data = b"test data"
  138. invalid_signature = b"this is not a valid signature"
  139. with self.assertRaises(subprocess.CalledProcessError):
  140. vendor.verify(test_data, invalid_signature)
  141. def test_sign_with_keyid(self) -> None:
  142. """Test signing with a specific key ID using CLI."""
  143. vendor = GPGCliSignatureVendor()
  144. test_data = b"test data to sign"
  145. try:
  146. # Try to get a key from the keyring
  147. result = subprocess.run(
  148. ["gpg", "--list-secret-keys", "--with-colons"],
  149. capture_output=True,
  150. check=True,
  151. text=True,
  152. )
  153. # Parse output to find a key fingerprint
  154. keyid = None
  155. for line in result.stdout.split("\n"):
  156. if line.startswith("fpr:"):
  157. keyid = line.split(":")[9]
  158. break
  159. if not keyid:
  160. self.skipTest("No GPG keys available for testing")
  161. signature = vendor.sign(test_data, keyid=keyid)
  162. self.assertIsInstance(signature, bytes)
  163. self.assertGreater(len(signature), 0)
  164. # Verify the signature
  165. vendor.verify(test_data, signature)
  166. except subprocess.CalledProcessError as e:
  167. self.skipTest(f"GPG key not available: {e}")
  168. def test_verify_with_keyids(self) -> None:
  169. """Test verifying with specific trusted key IDs."""
  170. vendor = GPGCliSignatureVendor()
  171. test_data = b"test data to sign"
  172. try:
  173. # Sign without specifying a key (use default)
  174. signature = vendor.sign(test_data)
  175. # Get the primary key fingerprint from the keyring
  176. result = subprocess.run(
  177. ["gpg", "--list-secret-keys", "--with-colons"],
  178. capture_output=True,
  179. check=True,
  180. text=True,
  181. )
  182. primary_keyid = None
  183. for line in result.stdout.split("\n"):
  184. if line.startswith("fpr:"):
  185. primary_keyid = line.split(":")[9]
  186. break
  187. if not primary_keyid:
  188. self.skipTest("No GPG keys available for testing")
  189. # Verify with the correct primary keyid - should succeed
  190. # (GPG shows primary key fingerprint even if signed by subkey)
  191. vendor.verify(test_data, signature, keyids=[primary_keyid])
  192. # Verify with a different keyid - should fail
  193. fake_keyid = "0" * 40 # Fake 40-character fingerprint
  194. with self.assertRaises(ValueError):
  195. vendor.verify(test_data, signature, keyids=[fake_keyid])
  196. except subprocess.CalledProcessError as e:
  197. self.skipTest(f"GPG key not available: {e}")
  198. def test_custom_gpg_command(self) -> None:
  199. """Test using a custom GPG command path."""
  200. vendor = GPGCliSignatureVendor(gpg_command="gpg")
  201. test_data = b"test data"
  202. try:
  203. signature = vendor.sign(test_data)
  204. self.assertIsInstance(signature, bytes)
  205. except subprocess.CalledProcessError as e:
  206. self.skipTest(f"GPG not available: {e}")
  207. def test_gpg_program_from_config(self) -> None:
  208. """Test reading gpg.program from config."""
  209. # Create a config with gpg.program set
  210. config = ConfigDict()
  211. config.set((b"gpg",), b"program", b"gpg2")
  212. vendor = GPGCliSignatureVendor(config=config)
  213. self.assertEqual(vendor.gpg_command, "gpg2")
  214. def test_gpg_program_override(self) -> None:
  215. """Test that gpg_command parameter overrides config."""
  216. config = ConfigDict()
  217. config.set((b"gpg",), b"program", b"gpg2")
  218. vendor = GPGCliSignatureVendor(config=config, gpg_command="gpg")
  219. self.assertEqual(vendor.gpg_command, "gpg")
  220. def test_gpg_program_default(self) -> None:
  221. """Test default gpg command when no config provided."""
  222. vendor = GPGCliSignatureVendor()
  223. self.assertEqual(vendor.gpg_command, "gpg")
  224. def test_gpg_program_default_when_not_in_config(self) -> None:
  225. """Test default gpg command when config doesn't have gpg.program."""
  226. config = ConfigDict()
  227. vendor = GPGCliSignatureVendor(config=config)
  228. self.assertEqual(vendor.gpg_command, "gpg")
  229. class GetSignatureVendorTests(unittest.TestCase):
  230. """Tests for get_signature_vendor function."""
  231. def test_default_format(self) -> None:
  232. """Test that default format is openpgp."""
  233. vendor = get_signature_vendor()
  234. self.assertIsInstance(vendor, (GPGSignatureVendor, GPGCliSignatureVendor))
  235. def test_explicit_openpgp_format(self) -> None:
  236. """Test explicitly requesting openpgp format."""
  237. vendor = get_signature_vendor(format="openpgp")
  238. self.assertIsInstance(vendor, (GPGSignatureVendor, GPGCliSignatureVendor))
  239. def test_format_from_config(self) -> None:
  240. """Test reading format from config."""
  241. config = ConfigDict()
  242. config.set((b"gpg",), b"format", b"openpgp")
  243. vendor = get_signature_vendor(config=config)
  244. self.assertIsInstance(vendor, (GPGSignatureVendor, GPGCliSignatureVendor))
  245. def test_format_case_insensitive(self) -> None:
  246. """Test that format is case-insensitive."""
  247. vendor = get_signature_vendor(format="OpenPGP")
  248. self.assertIsInstance(vendor, (GPGSignatureVendor, GPGCliSignatureVendor))
  249. def test_x509_not_supported(self) -> None:
  250. """Test that x509 format raises ValueError."""
  251. with self.assertRaises(ValueError) as cm:
  252. get_signature_vendor(format="x509")
  253. self.assertIn("X.509", str(cm.exception))
  254. def test_ssh_format_supported(self) -> None:
  255. """Test that ssh format is now supported."""
  256. vendor = get_signature_vendor(format="ssh")
  257. # Should be either SSHSigSignatureVendor or SSHCliSignatureVendor
  258. self.assertIsInstance(vendor, (SSHSigSignatureVendor, SSHCliSignatureVendor))
  259. def test_invalid_format(self) -> None:
  260. """Test that invalid format raises ValueError."""
  261. with self.assertRaises(ValueError) as cm:
  262. get_signature_vendor(format="invalid")
  263. self.assertIn("Unsupported", str(cm.exception))
  264. def test_config_passed_to_vendor(self) -> None:
  265. """Test that config is passed to the vendor."""
  266. config = ConfigDict()
  267. config.set((b"gpg",), b"program", b"gpg2")
  268. vendor = get_signature_vendor(format="openpgp", config=config)
  269. # If CLI vendor is used, check that config was passed
  270. if isinstance(vendor, GPGCliSignatureVendor):
  271. self.assertEqual(vendor.gpg_command, "gpg2")
  272. def test_ssh_format(self) -> None:
  273. """Test requesting SSH format."""
  274. vendor = get_signature_vendor(format="ssh")
  275. # Should be either SSHSigSignatureVendor or SSHCliSignatureVendor
  276. self.assertIsInstance(vendor, (SSHSigSignatureVendor, SSHCliSignatureVendor))
  277. class SSHSigSignatureVendorTests(unittest.TestCase):
  278. """Tests for SSHSigSignatureVendor (sshsig package implementation)."""
  279. def test_sign_not_supported(self) -> None:
  280. """Test that sign raises NotImplementedError with helpful message."""
  281. vendor = SSHSigSignatureVendor()
  282. with self.assertRaises(NotImplementedError) as cm:
  283. vendor.sign(b"test data", keyid="dummy")
  284. self.assertIn("SSHCliSignatureVendor", str(cm.exception))
  285. def test_verify_without_config_raises(self) -> None:
  286. """Test that verify without config or keyids raises ValueError."""
  287. vendor = SSHSigSignatureVendor()
  288. with self.assertRaises(ValueError) as cm:
  289. vendor.verify(b"test data", b"fake signature")
  290. self.assertIn("allowedSignersFile", str(cm.exception))
  291. def test_config_parsing(self) -> None:
  292. """Test parsing SSH config options."""
  293. config = ConfigDict()
  294. config.set((b"gpg", b"ssh"), b"allowedSignersFile", b"/path/to/allowed")
  295. config.set((b"gpg", b"ssh"), b"defaultKeyCommand", b"ssh-add -L")
  296. vendor = SSHSigSignatureVendor(config=config)
  297. self.assertEqual(vendor.allowed_signers_file, "/path/to/allowed")
  298. self.assertEqual(vendor.default_key_command, "ssh-add -L")
  299. def test_verify_with_cli_generated_signature(self) -> None:
  300. """Test verifying a signature created by SSH CLI vendor."""
  301. import os
  302. import tempfile
  303. if shutil.which("ssh-keygen") is None:
  304. self.skipTest("ssh-keygen not available")
  305. # Generate a test SSH key and signature using CLI vendor
  306. with tempfile.TemporaryDirectory() as tmpdir:
  307. private_key = os.path.join(tmpdir, "test_key")
  308. public_key = private_key + ".pub"
  309. allowed_signers = os.path.join(tmpdir, "allowed_signers")
  310. # Generate Ed25519 key
  311. subprocess.run(
  312. [
  313. "ssh-keygen",
  314. "-t",
  315. "ed25519",
  316. "-f",
  317. private_key,
  318. "-N",
  319. "",
  320. "-C",
  321. "test@example.com",
  322. ],
  323. capture_output=True,
  324. check=True,
  325. )
  326. # Create allowed_signers file
  327. with open(public_key) as pub:
  328. pub_key_content = pub.read().strip()
  329. with open(allowed_signers, "w") as allowed:
  330. allowed.write(f"* {pub_key_content}\n")
  331. # Sign with CLI vendor
  332. cli_config = ConfigDict()
  333. cli_config.set(
  334. (b"gpg", b"ssh"), b"allowedSignersFile", allowed_signers.encode()
  335. )
  336. cli_vendor = SSHCliSignatureVendor(config=cli_config)
  337. test_data = b"test data for sshsig verification"
  338. signature = cli_vendor.sign(test_data, keyid=private_key)
  339. # Verify with sshsig package vendor
  340. pkg_config = ConfigDict()
  341. pkg_config.set(
  342. (b"gpg", b"ssh"), b"allowedSignersFile", allowed_signers.encode()
  343. )
  344. pkg_vendor = SSHSigSignatureVendor(config=pkg_config)
  345. # This should succeed
  346. pkg_vendor.verify(test_data, signature)
  347. class SSHCliSignatureVendorTests(unittest.TestCase):
  348. """Tests for SSHCliSignatureVendor."""
  349. def setUp(self) -> None:
  350. """Check if ssh-keygen is available."""
  351. if shutil.which("ssh-keygen") is None:
  352. self.skipTest("ssh-keygen command not available")
  353. def test_ssh_program_from_config(self) -> None:
  354. """Test reading gpg.ssh.program from config."""
  355. config = ConfigDict()
  356. config.set((b"gpg", b"ssh"), b"program", b"/usr/bin/ssh-keygen")
  357. vendor = SSHCliSignatureVendor(config=config)
  358. self.assertEqual(vendor.ssh_command, "/usr/bin/ssh-keygen")
  359. def test_ssh_program_override(self) -> None:
  360. """Test that ssh_command parameter overrides config."""
  361. config = ConfigDict()
  362. config.set((b"gpg", b"ssh"), b"program", b"/usr/bin/ssh-keygen")
  363. vendor = SSHCliSignatureVendor(config=config, ssh_command="ssh-keygen")
  364. self.assertEqual(vendor.ssh_command, "ssh-keygen")
  365. def test_ssh_program_default(self) -> None:
  366. """Test default ssh-keygen command when no config provided."""
  367. vendor = SSHCliSignatureVendor()
  368. self.assertEqual(vendor.ssh_command, "ssh-keygen")
  369. def test_allowed_signers_from_config(self) -> None:
  370. """Test reading gpg.ssh.allowedSignersFile from config."""
  371. config = ConfigDict()
  372. config.set((b"gpg", b"ssh"), b"allowedSignersFile", b"/tmp/allowed_signers")
  373. vendor = SSHCliSignatureVendor(config=config)
  374. self.assertEqual(vendor.allowed_signers_file, "/tmp/allowed_signers")
  375. def test_sign_without_key_raises(self) -> None:
  376. """Test that signing without a key raises ValueError."""
  377. vendor = SSHCliSignatureVendor()
  378. with self.assertRaises(ValueError) as cm:
  379. vendor.sign(b"test data")
  380. self.assertIn("key", str(cm.exception).lower())
  381. def test_verify_without_allowed_signers_raises(self) -> None:
  382. """Test that verify without allowedSignersFile raises ValueError."""
  383. vendor = SSHCliSignatureVendor()
  384. with self.assertRaises(ValueError) as cm:
  385. vendor.verify(b"test data", b"fake signature")
  386. self.assertIn("allowedSignersFile", str(cm.exception))
  387. def test_sign_and_verify_with_ssh_key(self) -> None:
  388. """Test sign and verify cycle with SSH key."""
  389. import os
  390. import tempfile
  391. # Generate a test SSH key
  392. with tempfile.TemporaryDirectory() as tmpdir:
  393. private_key = os.path.join(tmpdir, "test_key")
  394. public_key = private_key + ".pub"
  395. allowed_signers = os.path.join(tmpdir, "allowed_signers")
  396. # Generate Ed25519 key (no passphrase)
  397. subprocess.run(
  398. [
  399. "ssh-keygen",
  400. "-t",
  401. "ed25519",
  402. "-f",
  403. private_key,
  404. "-N",
  405. "",
  406. "-C",
  407. "test@example.com",
  408. ],
  409. capture_output=True,
  410. check=True,
  411. )
  412. # Create allowed_signers file
  413. with open(public_key) as pub:
  414. pub_key_content = pub.read().strip()
  415. with open(allowed_signers, "w") as allowed:
  416. allowed.write(f"git {pub_key_content}\n")
  417. # Create vendor with config
  418. config = ConfigDict()
  419. config.set(
  420. (b"gpg", b"ssh"), b"allowedSignersFile", allowed_signers.encode()
  421. )
  422. vendor = SSHCliSignatureVendor(config=config)
  423. # Test signing and verification
  424. test_data = b"test data to sign with SSH"
  425. signature = vendor.sign(test_data, keyid=private_key)
  426. self.assertIsInstance(signature, bytes)
  427. self.assertGreater(len(signature), 0)
  428. self.assertTrue(signature.startswith(b"-----BEGIN SSH SIGNATURE-----"))
  429. # Verify the signature
  430. vendor.verify(test_data, signature)
  431. class DetectSignatureFormatTests(unittest.TestCase):
  432. """Tests for detect_signature_format function."""
  433. def test_detect_ssh_signature(self) -> None:
  434. """Test detecting SSH signature format."""
  435. ssh_sig = b"-----BEGIN SSH SIGNATURE-----\nfoo\n-----END SSH SIGNATURE-----"
  436. self.assertEqual(detect_signature_format(ssh_sig), SIGNATURE_FORMAT_SSH)
  437. def test_detect_pgp_signature(self) -> None:
  438. """Test detecting PGP signature format."""
  439. pgp_sig = b"-----BEGIN PGP SIGNATURE-----\nfoo\n-----END PGP SIGNATURE-----"
  440. self.assertEqual(detect_signature_format(pgp_sig), SIGNATURE_FORMAT_OPENPGP)
  441. def test_detect_x509_signature_pkcs7(self) -> None:
  442. """Test detecting X.509 PKCS7 signature format."""
  443. x509_sig = b"-----BEGIN PKCS7-----\nfoo\n-----END PKCS7-----"
  444. self.assertEqual(detect_signature_format(x509_sig), SIGNATURE_FORMAT_X509)
  445. def test_detect_x509_signature_signed_message(self) -> None:
  446. """Test detecting X.509 signed message format."""
  447. x509_sig = b"-----BEGIN SIGNED MESSAGE-----\nfoo\n-----END SIGNED MESSAGE-----"
  448. self.assertEqual(detect_signature_format(x509_sig), SIGNATURE_FORMAT_X509)
  449. def test_unknown_signature_format(self) -> None:
  450. """Test that unknown format raises ValueError."""
  451. with self.assertRaises(ValueError) as cm:
  452. detect_signature_format(b"not a signature")
  453. self.assertIn("Unable to detect", str(cm.exception))
  454. class GetSignatureVendorForSignatureTests(unittest.TestCase):
  455. """Tests for get_signature_vendor_for_signature function."""
  456. def test_get_vendor_for_ssh_signature(self) -> None:
  457. """Test getting vendor for SSH signature."""
  458. ssh_sig = b"-----BEGIN SSH SIGNATURE-----\nfoo\n-----END SSH SIGNATURE-----"
  459. vendor = get_signature_vendor_for_signature(ssh_sig)
  460. self.assertIsInstance(vendor, (SSHSigSignatureVendor, SSHCliSignatureVendor))
  461. def test_get_vendor_for_pgp_signature(self) -> None:
  462. """Test getting vendor for PGP signature."""
  463. pgp_sig = b"-----BEGIN PGP SIGNATURE-----\nfoo\n-----END PGP SIGNATURE-----"
  464. vendor = get_signature_vendor_for_signature(pgp_sig)
  465. self.assertIsInstance(vendor, (GPGSignatureVendor, GPGCliSignatureVendor))
  466. def test_get_vendor_for_x509_signature(self) -> None:
  467. """Test that X.509 signature raises ValueError (not supported)."""
  468. x509_sig = b"-----BEGIN PKCS7-----\nfoo\n-----END PKCS7-----"
  469. with self.assertRaises(ValueError) as cm:
  470. get_signature_vendor_for_signature(x509_sig)
  471. self.assertIn("X.509", str(cm.exception))
  472. def test_get_vendor_with_config(self) -> None:
  473. """Test that config is passed to vendor."""
  474. config = ConfigDict()
  475. config.set((b"gpg",), b"program", b"gpg2")
  476. pgp_sig = b"-----BEGIN PGP SIGNATURE-----\nfoo\n-----END PGP SIGNATURE-----"
  477. vendor = get_signature_vendor_for_signature(pgp_sig, config=config)
  478. # If CLI vendor is used, check config was passed
  479. if isinstance(vendor, GPGCliSignatureVendor):
  480. self.assertEqual(vendor.gpg_command, "gpg2")