Merge lp:~michael.nelson/launchpad/ppa-generate-key-failure into lp:launchpad

Proposed by Michael Nelson
Status: Merged
Approved by: Michael Nelson
Approved revision: no longer in the source branch.
Merged at revision: 10834
Proposed branch: lp:~michael.nelson/launchpad/ppa-generate-key-failure
Merge into: lp:launchpad
Diff against target: 60 lines (+31/-2)
2 files modified
lib/canonical/launchpad/utilities/ftests/test_gpghandler.py (+24/-2)
lib/canonical/launchpad/utilities/gpghandler.py (+7/-0)
To merge this branch: bzr merge lp:~michael.nelson/launchpad/ppa-generate-key-failure
Reviewer Review Type Date Requested Status
Abel Deuring (community) code Approve
Review via email: mp+24871@code.launchpad.net

Commit message

Ensure only byte-strings are passed to gpgme.Context().keylist()

Description of the change

This branch fixes bug 576405 by ensuring that IGPGHandler.localKeys() passes only byte-strings to gpgme.Context().keylist().

To test:
bin/test -vv -m test_gpghandler

Initially I refactored IGPGHandler.generateKey() to make it more testable (ie. to be able to test everything outside of the gpgme.Context().genkey() call), but deleted this in the end in favour of a direct test of the issue.

This will need to be CP'd, so if you review this and I'm not around, please feel free to send it off to land on devel.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) wrote :

Isn't this going to explode if the first PPA's display name contains non-ASCII characters?

Revision history for this message
Michael Nelson (michael.nelson) wrote :

Right - I had understood that we were only using localKeys() with fingerprints and email addresses, but better to be safe.

I couldn't find much documentation for gpgme, and so have just encoded the string in utf-8.

Thanks William.

Revision history for this message
Abel Deuring (adeuring) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/canonical/launchpad/utilities/ftests/test_gpghandler.py'
2--- lib/canonical/launchpad/utilities/ftests/test_gpghandler.py 2009-06-25 05:30:52 +0000
3+++ lib/canonical/launchpad/utilities/ftests/test_gpghandler.py 2010-05-07 12:26:30 +0000
4@@ -6,8 +6,8 @@
5
6 from zope.component import getUtility
7
8-from canonical.launchpad.ftests import login, ANONYMOUS, logout
9-from canonical.launchpad.ftests import keys_for_tests
10+from canonical.launchpad.ftests import (
11+ ANONYMOUS, keys_for_tests, login, logout)
12 from canonical.launchpad.interfaces.gpghandler import IGPGHandler
13 from canonical.testing import LaunchpadFunctionalLayer
14
15@@ -97,6 +97,28 @@
16 [key] = filtered_keys
17 self.assertEqual(key.fingerprint, secret_target_fpr)
18
19+ def test_unicode_filter(self):
20+ """Using a unicode filter works also.
21+
22+ XXX michaeln 2010-05-07 bug=576405
23+ Recent versions of gpgme return unicode fingerprints, but
24+ at the same time, gpgme.Context().keylist falls over if
25+ it receives a unicode string.
26+ """
27+ self.populateKeyring()
28+
29+ target_fpr = u'340CA3BB270E2716C9EE0B768E7EB7086C64A8C5'
30+
31+ # Finding a key by its unicode fingerprint.
32+ filtered_keys = self.gpg_handler.localKeys(target_fpr)
33+ [key] = filtered_keys
34+ self.assertEqual(key.fingerprint, target_fpr)
35+
36+ def test_non_ascii_filter(self):
37+ """localKeys should not error if passed non-ascii unicode strings."""
38+ filtered_keys = self.gpg_handler.localKeys(u'non-ascii \u8463')
39+ self.failUnlessRaises(StopIteration, filtered_keys.next)
40+
41 def testTestkeyrings(self):
42 """Do we have the expected test keyring files"""
43 self.assertEqual(len(list(keys_for_tests.test_keyrings())), 1)
44
45=== modified file 'lib/canonical/launchpad/utilities/gpghandler.py'
46--- lib/canonical/launchpad/utilities/gpghandler.py 2010-04-26 16:00:31 +0000
47+++ lib/canonical/launchpad/utilities/gpghandler.py 2010-05-07 12:26:30 +0000
48@@ -399,6 +399,13 @@
49 already knows about.
50 """
51 ctx = gpgme.Context()
52+
53+ # XXX michaeln 2010-05-07 bug=576405
54+ # Currently gpgme.Context().keylist fails if passed a unicode
55+ # string even though that's what is returned for fingerprints.
56+ if type(filter) == unicode:
57+ filter = filter.encode('utf-8')
58+
59 for key in ctx.keylist(filter, secret):
60 yield PymeKey.newFromGpgmeKey(key)
61