forked from Disroot/gpg-lacre
GnuPG module: make key-listing more thorough
- Flush key-collecting structures each time a new public key entry is found. This will avoid adding sub-keys and overwriting main keys with them. - Use parseaddr from email.utils to parse emails (and drop realname part). - Record logs produced during unit tests. - Fix a small bug in test code. Also: add basic information about available test identities to testing documentation.
This commit is contained in:
parent
7c2d32bf3c
commit
72217e38ea
3 changed files with 32 additions and 4 deletions
|
@ -34,6 +34,7 @@ from email.utils import parseaddr
|
|||
|
||||
LINE_FINGERPRINT = 'fpr'
|
||||
LINE_USER_ID = 'uid'
|
||||
LINE_PUBLIC_KEY = 'pub'
|
||||
|
||||
POS_FINGERPRINT = 9
|
||||
POS_UID = 9
|
||||
|
@ -73,16 +74,28 @@ def public_keys(keyhome, *, key_id=None):
|
|||
p.wait()
|
||||
|
||||
keys = dict()
|
||||
collected = set()
|
||||
|
||||
fingerprint = None
|
||||
email = None
|
||||
|
||||
for line in p.stdout.readlines():
|
||||
line = line.decode(sys.getdefaultencoding())
|
||||
if line[0:3] == LINE_FINGERPRINT:
|
||||
|
||||
if line[0:3] == LINE_PUBLIC_KEY:
|
||||
# New identity has started, reset state.
|
||||
fingerprint = None
|
||||
email = None
|
||||
|
||||
if line[0:3] == LINE_FINGERPRINT and not fingerprint:
|
||||
fingerprint = _extract_fingerprint(line)
|
||||
|
||||
if line[0:3] == LINE_USER_ID:
|
||||
email = _parse_uid_line(line)
|
||||
if not (fingerprint is None or email is None):
|
||||
|
||||
if fingerprint and email and not email in collected:
|
||||
keys[fingerprint] = email
|
||||
collected.add(email)
|
||||
fingerprint = None
|
||||
email = None
|
||||
|
||||
|
@ -104,7 +117,8 @@ def _parse_uid_line(line: str):
|
|||
if len(userid_line) <= POS_UID:
|
||||
return None
|
||||
else:
|
||||
return userid_line[POS_UID]
|
||||
(_, email) = parseaddr(userid_line[POS_UID])
|
||||
return email
|
||||
|
||||
|
||||
def _to_bytes(s) -> bytes:
|
||||
|
|
|
@ -49,3 +49,11 @@ verifying that the correct key has been used. That's because we don't know
|
|||
|
||||
When things go wrong, be sure to study `test/logs/e2e.log` and
|
||||
`test/logs/gpg-mailgate.log` files -- they contain some useful information.
|
||||
|
||||
## Test identities
|
||||
|
||||
There are several identities in test/keyhome and in the test database:
|
||||
|
||||
* alice@disposlab: 1CD245308F0963D038E88357973CF4D9387C44D7
|
||||
* bob@disposlab: 19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67
|
||||
* evan@disposlab: 530B1BB2D0CC7971648198BBA4774E507D3AF5BC
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
import GnuPG
|
||||
import logging
|
||||
|
||||
import unittest
|
||||
|
||||
class GnuPGUtilitiesTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Record GnuPG logs:
|
||||
logging.basicConfig(filename='test/logs/unittest.log', level=logging.DEBUG,
|
||||
format='%(asctime)s %(pathname)s:%(lineno)d %(levelname)s [%(funcName)s] %(message)s')
|
||||
|
||||
def test_build_default_command(self):
|
||||
cmd = GnuPG._build_command("test/keyhome")
|
||||
self.assertEqual(cmd, ["gpg", "--homedir", "test/keyhome"])
|
||||
|
@ -38,7 +44,7 @@ class GnuPGUtilitiesTest(unittest.TestCase):
|
|||
|
||||
def test_add_delete_key(self):
|
||||
self.assertDictEqual(GnuPG.public_keys('test/keyhome.other'), { })
|
||||
GnuPG.add_key('test/keyhome', self._load('test/keys/bob@disposlab.pub'))
|
||||
GnuPG.add_key('test/keyhome.other', self._load('test/keys/bob@disposlab.pub'))
|
||||
self.assertDictEqual(GnuPG.public_keys('test/keyhome.other'), {
|
||||
'19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67': 'bob@disposlab',
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue