|
|
@@ -21,9 +21,11 @@
|
|
|
|
|
|
"""Tests for signature vendors."""
|
|
|
|
|
|
+import shutil
|
|
|
+import subprocess
|
|
|
import unittest
|
|
|
|
|
|
-from dulwich.signature import GPGSignatureVendor, SignatureVendor
|
|
|
+from dulwich.signature import GPGCliSignatureVendor, GPGSignatureVendor, SignatureVendor
|
|
|
|
|
|
try:
|
|
|
import gpg
|
|
|
@@ -106,3 +108,121 @@ class GPGSignatureVendorTests(unittest.TestCase):
|
|
|
vendor.verify(test_data, signature)
|
|
|
except gpg.errors.GPGMEError as e:
|
|
|
self.skipTest(f"GPG key not available: {e}")
|
|
|
+
|
|
|
+
|
|
|
+class GPGCliSignatureVendorTests(unittest.TestCase):
|
|
|
+ """Tests for GPGCliSignatureVendor."""
|
|
|
+
|
|
|
+ def setUp(self) -> None:
|
|
|
+ """Check if gpg command is available."""
|
|
|
+ if shutil.which("gpg") is None:
|
|
|
+ self.skipTest("gpg command not available")
|
|
|
+
|
|
|
+ def test_sign_and_verify(self) -> None:
|
|
|
+ """Test basic sign and verify cycle using CLI."""
|
|
|
+ vendor = GPGCliSignatureVendor()
|
|
|
+ test_data = b"test data to sign"
|
|
|
+
|
|
|
+ try:
|
|
|
+ # Sign the data
|
|
|
+ signature = vendor.sign(test_data)
|
|
|
+ self.assertIsInstance(signature, bytes)
|
|
|
+ self.assertGreater(len(signature), 0)
|
|
|
+ self.assertTrue(signature.startswith(b"-----BEGIN PGP SIGNATURE-----"))
|
|
|
+
|
|
|
+ # Verify the signature
|
|
|
+ vendor.verify(test_data, signature)
|
|
|
+ except subprocess.CalledProcessError as e:
|
|
|
+ # Skip test if no GPG key is available or configured
|
|
|
+ self.skipTest(f"GPG signing failed: {e}")
|
|
|
+
|
|
|
+ def test_verify_invalid_signature(self) -> None:
|
|
|
+ """Test that verify raises an error for invalid signatures."""
|
|
|
+ vendor = GPGCliSignatureVendor()
|
|
|
+ test_data = b"test data"
|
|
|
+ invalid_signature = b"this is not a valid signature"
|
|
|
+
|
|
|
+ with self.assertRaises(subprocess.CalledProcessError):
|
|
|
+ vendor.verify(test_data, invalid_signature)
|
|
|
+
|
|
|
+ def test_sign_with_keyid(self) -> None:
|
|
|
+ """Test signing with a specific key ID using CLI."""
|
|
|
+ vendor = GPGCliSignatureVendor()
|
|
|
+ test_data = b"test data to sign"
|
|
|
+
|
|
|
+ try:
|
|
|
+ # Try to get a key from the keyring
|
|
|
+ result = subprocess.run(
|
|
|
+ ["gpg", "--list-secret-keys", "--with-colons"],
|
|
|
+ capture_output=True,
|
|
|
+ check=True,
|
|
|
+ text=True,
|
|
|
+ )
|
|
|
+
|
|
|
+ # Parse output to find a key fingerprint
|
|
|
+ keyid = None
|
|
|
+ for line in result.stdout.split("\n"):
|
|
|
+ if line.startswith("fpr:"):
|
|
|
+ keyid = line.split(":")[9]
|
|
|
+ break
|
|
|
+
|
|
|
+ if not keyid:
|
|
|
+ self.skipTest("No GPG keys available for testing")
|
|
|
+
|
|
|
+ signature = vendor.sign(test_data, keyid=keyid)
|
|
|
+ self.assertIsInstance(signature, bytes)
|
|
|
+ self.assertGreater(len(signature), 0)
|
|
|
+
|
|
|
+ # Verify the signature
|
|
|
+ vendor.verify(test_data, signature)
|
|
|
+ except subprocess.CalledProcessError as e:
|
|
|
+ self.skipTest(f"GPG key not available: {e}")
|
|
|
+
|
|
|
+ def test_verify_with_keyids(self) -> None:
|
|
|
+ """Test verifying with specific trusted key IDs."""
|
|
|
+ vendor = GPGCliSignatureVendor()
|
|
|
+ test_data = b"test data to sign"
|
|
|
+
|
|
|
+ try:
|
|
|
+ # Sign without specifying a key (use default)
|
|
|
+ signature = vendor.sign(test_data)
|
|
|
+
|
|
|
+ # Get the primary key fingerprint from the keyring
|
|
|
+ result = subprocess.run(
|
|
|
+ ["gpg", "--list-secret-keys", "--with-colons"],
|
|
|
+ capture_output=True,
|
|
|
+ check=True,
|
|
|
+ text=True,
|
|
|
+ )
|
|
|
+
|
|
|
+ primary_keyid = None
|
|
|
+ for line in result.stdout.split("\n"):
|
|
|
+ if line.startswith("fpr:"):
|
|
|
+ primary_keyid = line.split(":")[9]
|
|
|
+ break
|
|
|
+
|
|
|
+ if not primary_keyid:
|
|
|
+ self.skipTest("No GPG keys available for testing")
|
|
|
+
|
|
|
+ # Verify with the correct primary keyid - should succeed
|
|
|
+ # (GPG shows primary key fingerprint even if signed by subkey)
|
|
|
+ vendor.verify(test_data, signature, keyids=[primary_keyid])
|
|
|
+
|
|
|
+ # Verify with a different keyid - should fail
|
|
|
+ fake_keyid = "0" * 40 # Fake 40-character fingerprint
|
|
|
+ with self.assertRaises(ValueError):
|
|
|
+ vendor.verify(test_data, signature, keyids=[fake_keyid])
|
|
|
+
|
|
|
+ except subprocess.CalledProcessError as e:
|
|
|
+ self.skipTest(f"GPG key not available: {e}")
|
|
|
+
|
|
|
+ def test_custom_gpg_command(self) -> None:
|
|
|
+ """Test using a custom GPG command path."""
|
|
|
+ vendor = GPGCliSignatureVendor(gpg_command="gpg")
|
|
|
+ test_data = b"test data"
|
|
|
+
|
|
|
+ try:
|
|
|
+ signature = vendor.sign(test_data)
|
|
|
+ self.assertIsInstance(signature, bytes)
|
|
|
+ except subprocess.CalledProcessError as e:
|
|
|
+ self.skipTest(f"GPG not available: {e}")
|