[gnome-keysign: 31/75] tests: gpgmeh: Sanitise UID from gpgme before comparing



commit 76ce4ef18c7bfcf1a457a9f9ca5fcc600ac2703b
Author: Tobias Mueller <muelli cryptobitch de>
Date:   Sun Jul 23 10:43:34 2017 +0200

    tests: gpgmeh: Sanitise UID from gpgme before comparing
    
    Otherwise it fails like this:
    
    ======================================================================
    ERROR: We want to produce as many signatures as possible
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    File "/tmp/gks/lib/python2.7/site-packages/nose/case.py", line 197, in
    runTest
        self.test(*self.arg)
    File "/tmp/gnome-keysign/tests/test_gpgmeh.py", line 508, in
    test_sign_and_encrypt_double_secret
        assert_in(uid.name, uid_enc_str)
    File "/usr/lib64/python2.7/unittest/case.py", line 838, in assertIn
        if member not in container:
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 2:
    ordinal not in range(128)
    -------------------- >> begin captured logging << --------------------
    keysign.gpgmeh: INFO: Old homedir: '/tmp/tmpN2nxck'
    keysign.gpgmeh: DEBUG: Import <gpg.results.ImportResult object at
    0x7efea5f0ae50>
    root: INFO: Returning None for u'KEY_CONSIDERED'
    u'8D74E09668E09ACC958C9F40B6073C38922AC960 0'
    root: INFO: Returning None for u'KEY_CONSIDERED'
    u'8D74E09668E09ACC958C9F40B6073C38922AC960 0'
    keysign.gpgmeh: INFO: edit_cb: u'GET_LINE' u'keyedit.prompt' 'None'
    keysign.gpgmeh: INFO: edit_cb data: u'uid 0'
    root: INFO: Returning None for u'GOT_IT' u''
    keysign.gpgmeh: INFO: edit_cb: u'GET_LINE' u'keyedit.prompt' 'None'
    keysign.gpgmeh: INFO: edit_cb data: u'sign'
    root: INFO: Returning None for u'GOT_IT' u''
    root: INFO: Returning None for u'KEY_CONSIDERED'
    u'FB7426B135FDB7B8FA5FFFDB5ACBD4E8F9E80249 0'
    root: INFO: Returning None for u'KEY_CONSIDERED'
    u'CFD49FE68C0924D830BE4BFDA5C89B8B383EE754 0'
    keysign.gpgmeh: INFO: edit_cb: u'GET_BOOL' u'sign_uid.okay' 'None'
    keysign.gpgmeh: INFO: edit_cb data: u'Y'
    root: INFO: Returning None for u'GOT_IT' u''
    keysign.gpgmeh: INFO: edit_cb: u'GET_BOOL' u'sign_uid.okay' 'None'
    keysign.gpgmeh: INFO: edit_cb data: u'Y'
    root: INFO: Returning None for u'GOT_IT' u''
    keysign.gpgmeh: INFO: edit_cb: u'GET_LINE' u'keyedit.prompt' 'None'
    keysign.gpgmeh: INFO: edit_cb data: u'save'
    root: INFO: Returning None for u'GOT_IT' u''
    root: INFO: Closing for u''
    keysign.gpgmeh: DEBUG: Sink after signing:
    
'pub:-:1024:1:B6073C38922AC960:1494264830:0::-:::esca\nfpr:::::::::8D74E09668E09ACC958C9F40B6073C38922AC960:\nsub:-:1024:1:96EA4689DF6E78D0:1494264830:0:::::esa\nfpr:::::::::1130F9FA989134A27A49E59996EA4689DF6E78D0:\nuid:-::::::::fo\xf6e\xe9ba
    <foo@bma.d>:::S9 S8 S7 S3 S2 H8 H2 H9 H10 H11 Z2 Z3 Z1,mdc,no-ks-
    
modify:1,p::\npub:-:1024:1:B6073C38922AC960:1494264830:0::-:::esca\nfpr:::::::::8D74E09668E09ACC958C9F40B6073C38922AC960:\nsub:-:1024:1:96EA4689DF6E78D0:1494264830:0:::::esa\nfpr:::::::::1130F9FA989134A27A49E59996EA4689DF6E78D0:\nuid:-::::::::fo\xf6e\xe9ba
    <foo@bma.d>:::S9 S8 S7 S3 S2 H8 H2 H9 H10 H11 Z2 Z3 Z1,mdc,no-ks-
    
modify:1,p::\npub:-:1024:1:B6073C38922AC960:1494264830:0::-:::esca\nfpr:::::::::8D74E09668E09ACC958C9F40B6073C38922AC960:\nsub:-:1024:1:96EA4689DF6E78D0:1494264830:0:::::esa\nfpr:::::::::1130F9FA989134A27A49E59996EA4689DF6E78D0:\nuid:-::::::::fo\xf6e\xe9ba
    <foo@bma.d>:::S9 S8 S7 S3 S2 H8 H2 H9 H10 H11 Z2 Z3 Z1,mdc,no-ks-
    
modify:1,pm::\npub:-:1024:1:B6073C38922AC960:1494264830:0::-:::esca\nfpr:::::::::8D74E09668E09ACC958C9F40B6073C38922AC960:\nsub:-:1024:1:96EA4689DF6E78D0:1494264830:0:::::esa\nfpr:::::::::1130F9FA989134A27A49E59996EA4689DF6E78D0:\nuid:-::::::::fo\xf6e\xe9ba
    <foo@bma.d>:::S9 S8 S7 S3 S2 H8 H2 H9 H10 H11 Z2 Z3 Z1,mdc,no-ks-
    modify:1,pm::\n'
    keysign.gpgkey: DEBUG: UID from gpgme: 'fo\xf6e\xe9ba <foo@bma.d>'
    test_gpgmeh: INFO: Signatures: {'fo\xf6e\xe9ba <foo@bma.d>': [<Swig
    Object of type 'gpgme_key_sig_t' at 0x7efea5f14960>]}
    test_gpgmeh: INFO: Uid enc str: u'fo?e?ba <foo@bma.d>'
    test_gpgmeh: INFO: Uid name: 'fo\xf6e\xe9ba'
    --------------------- >> end captured logging << ---------------------

 tests/test_gpgmeh.py | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
---
diff --git a/tests/test_gpgmeh.py b/tests/test_gpgmeh.py
index 01ed433..501b3ef 100644
--- a/tests/test_gpgmeh.py
+++ b/tests/test_gpgmeh.py
@@ -36,6 +36,8 @@ from keysign.gpgmeh import get_usable_secret_keys
 from keysign.gpgmeh import get_public_key_data
 from keysign.gpgmeh import sign_keydata_and_encrypt
 
+from keysign.gpgkey import to_valid_utf8_string
+
 log = logging.getLogger(__name__)
 thisdir = os.path.dirname(os.path.realpath(__file__))
 parentdir = os.path.join(thisdir, "..")
@@ -381,8 +383,9 @@ def get_signatures_for_uids_on_key(ctx, key):
     # What happens if keylist returns multiple keys, e.g. because there
     # is another key with a UID named as the fpr?  How can I make sure I
     # get the signatures of any given key?
-    keys = list(ctx.keylist(key.fpr, mode=(gpg.constants.keylist.mode.LOCAL
-                                           |gpg.constants.keylist.mode.SIGS)))
+    ctx.set_keylist_mode(gpg.constants.keylist.mode.LOCAL
+                         | gpg.constants.keylist.mode.SIGS)
+    keys = list(ctx.keylist(key.fpr))
     assert len(keys) == 1
     uid_sigs = {uid.uid: [s for s in uid.signatures] for uid in keys[0].uids}
     log.info("Signatures: %r", uid_sigs)
@@ -501,9 +504,15 @@ class TestSignAndEncrypt:
                                     sender_key).values() for s in l]
         for uid, uid_enc in zip(uids_before, uid_encrypted):
             uid_enc_str = uid_enc[0].uid
+            log.info("Uid enc str: %r", uid_enc_str)
+            log.info("Uid name: %r", uid.name)
             # FIXME: assert_equals(uid, uid_enc[0])
-            assert_in(uid.name, uid_enc_str)
-            assert_in(uid.email, uid_enc_str)
+            # It's a bit weird to re-use the string treatment here.
+            # But gpgme may return unencodable bytes (and uid, here, is
+            # coming straight from gpgme).  We opted for our UID wrapper
+            # to return consumable strings, i.e. safe to encode
+            assert_in(to_valid_utf8_string(uid.name), uid_enc_str)
+            assert_in(to_valid_utf8_string(uid.email), uid_enc_str)
             ciphertext = uid_enc[1]
             log.debug("Decrypting %r", ciphertext)
             plaintext, result, vrfy = sender.decrypt(ciphertext)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]