[gnome-keysign: 2/12] tests: fix key import error



commit 4306cf67226cb48651fc2372ce60f5e2f88a88ef
Author: RyuzakiKK <aasonykk gmail com>
Date:   Sat Jan 19 22:03:39 2019 +0100

    tests: fix key import error
    
    If the required key was not already in the keyring these tests failed.
    
    The error was:
    keysign.gpgmeh: DEBUG: Exported
    'A2ECF6A72BCFD74F4165B389034094FB8FD495FB': b''
    ValueError: No data to export for
    A2ECF6A72BCFD74F4165B389034094FB8FD495FB (in None)

 tests/test_bluetooth.py | 33 +++++++++++++++++++-----------
 tests/test_wormhole.py  | 54 ++++++++++++++++++++++++++++++-------------------
 2 files changed, 54 insertions(+), 33 deletions(-)
---
diff --git a/tests/test_bluetooth.py b/tests/test_bluetooth.py
index 593b905..b6c200a 100644
--- a/tests/test_bluetooth.py
+++ b/tests/test_bluetooth.py
@@ -2,6 +2,8 @@ import os
 import logging
 import select
 import socket
+from subprocess import check_call
+import tempfile
 import unittest
 import gi
 gi.require_version('Gtk', '3.0')
@@ -17,7 +19,7 @@ try:
     HAVE_BT = True
 except ImportError:
     HAVE_BT = False
-from keysign.gpgmh import get_public_key_data, openpgpkey_from_data
+from keysign.gpgmeh import get_public_key_data, openpgpkey_from_data
 from keysign.util import mac_generate
 
 
@@ -39,10 +41,14 @@ def get_fixture_file(fixture):
 
 
 @unittest.skipUnless(HAVE_BT, "requires bluetooth module")
-def read_fixture_file(fixture):
+def import_key_from_file(fixture, homedir):
     fname = get_fixture_file(fixture)
-    data = open(fname, 'rb').read()
-    return data
+    original = open(fname, 'rb').read()
+    gpgcmd = ["gpg", "--homedir={}".format(homedir)]
+    # Now we import a single key
+    check_call(gpgcmd + ["--import", fname])
+
+    return openpgpkey_from_data(original)
 
 
 @deferred(timeout=15)
@@ -50,9 +56,10 @@ def read_fixture_file(fixture):
 @unittest.skipUnless(HAVE_BT, "requires bluetooth module")
 def test_bt():
     """This test requires two working Bluetooth devices"""
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
-    file_key_data = get_public_key_data(key.fingerprint)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
+    file_key_data = get_public_key_data(key.fingerprint, homedir=homedir)
     log.info("Running with key %r", key)
     hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data)
     # Start offering the key
@@ -76,8 +83,9 @@ def test_bt():
 @unittest.skipUnless(HAVE_BT, "requires bluetooth module")
 def test_bt_wrong_hmac():
     """This test requires two working Bluetooth devices"""
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
     log.info("Running with key %r", key)
     hmac = "wrong_hmac_eg_tampered_key"
     # Start offering the key
@@ -139,10 +147,11 @@ def test_bt_corrupted_key():
         except Exception as e:
             log.error("An error occurred: %s" % e)
 
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
     log.info("Running with key %r", key)
-    file_key_data = get_public_key_data(key.fingerprint)
+    file_key_data = get_public_key_data(key.fingerprint, homedir=homedir)
     hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data)
     # Start offering the key
     offer = BluetoothOffer(key)
diff --git a/tests/test_wormhole.py b/tests/test_wormhole.py
index 975ad25..7f1578c 100644
--- a/tests/test_wormhole.py
+++ b/tests/test_wormhole.py
@@ -18,6 +18,8 @@
 
 import os
 import logging
+from subprocess import check_call
+import tempfile
 import gi
 gi.require_version('Gtk', '3.0')
 
@@ -26,8 +28,8 @@ from nose.tools import *
 from wormhole.errors import WrongPasswordError, LonelyError
 from twisted.internet.defer import inlineCallbacks
 
-from keysign.gpgmh import openpgpkey_from_data
-from keysign.gpgmh import get_public_key_data
+from keysign.gpgmeh import openpgpkey_from_data
+from keysign.gpgmeh import get_public_key_data
 from keysign.offer import Offer
 from keysign.util import mac_generate
 from keysign.wormholeoffer import WormholeOffer
@@ -49,18 +51,23 @@ def get_fixture_file(fixture):
     return fname
 
 
-def read_fixture_file(fixture):
+def import_key_from_file(fixture, homedir):
     fname = get_fixture_file(fixture)
-    data = open(fname, 'rb').read()
-    return data
+    original = open(fname, 'rb').read()
+    gpgcmd = ["gpg", "--homedir={}".format(homedir)]
+    # Now we import a single key
+    check_call(gpgcmd + ["--import", fname])
 
+    return openpgpkey_from_data(original)
 
-@deferred(timeout=10)
+
+@deferred(timeout=200)
 @inlineCallbacks
 def test_wrmhl():
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
-    file_key_data = get_public_key_data(key.fingerprint)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
+    file_key_data = get_public_key_data(key.fingerprint, homedir=homedir)
     log.info("Running with key %r", key)
     # Start offering the key
     offer = WormholeOffer(key)
@@ -78,9 +85,10 @@ def test_wrmhl():
 @deferred(timeout=10)
 @inlineCallbacks
 def test_wrmhl_offline_code():
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
-    file_key_data = get_public_key_data(key.fingerprint)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
+    file_key_data = get_public_key_data(key.fingerprint, homedir=homedir)
     # We assume that this channel, at execution time, is free
     code = u"5556-penguin-paw-print"
     # Start offering the key
@@ -99,8 +107,9 @@ def test_wrmhl_offline_code():
 @deferred(timeout=10)
 @inlineCallbacks
 def test_wrmhl_wrong_code():
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
     log.info("Running with key %r", key)
     # Start offering the key
     offer = WormholeOffer(key)
@@ -118,8 +127,9 @@ def test_wrmhl_wrong_code():
 @deferred(timeout=10)
 @inlineCallbacks
 def test_wrmhl_wrong_hmac():
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
     log.info("Running with key %r", key)
     hmac = "wrong_hmac_eg_tampered_key"
     # Start offering the key
@@ -138,9 +148,10 @@ def test_wrmhl_wrong_hmac():
 @deferred(timeout=10)
 @inlineCallbacks
 def test_wrmhl_with_hmac():
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
-    file_key_data = get_public_key_data(key.fingerprint)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
+    file_key_data = get_public_key_data(key.fingerprint, homedir=homedir)
     log.info("Running with key %r", key)
     hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data)
     # Start offering the key
@@ -165,8 +176,9 @@ def test_offer_cancel():
         assert_is_not_none(message)
         assert_equal(type(message), LonelyError)
 
-    data = read_fixture_file("seckey-no-pw-1.asc")
-    key = openpgpkey_from_data(data)
+    # This should be a new, empty directory
+    homedir = tempfile.mkdtemp()
+    key = import_key_from_file("seckey-no-pw-1.asc", homedir)
     log.info("Running with key %r", key)
     # Start offering the key
     offer = Offer(key)


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