summaryrefslogtreecommitdiff
path: root/src/leap/common/tests
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2013-05-13 23:06:35 +0900
committerKali Kaneko <kali@leap.se>2013-05-21 01:57:26 +0900
commit7c6a87acaead5f54e1f2155ecf0a089eff97d654 (patch)
tree57faff0c4f476557c3e457514f5aed99abfbc3de /src/leap/common/tests
parent2a78ab3c730ba652153deaed55cfa8d92089a979 (diff)
use temporary openpgpwrapper as a context manager
in this way we implicitely catch any exception during the wrapped call, and ensure that the destructor is always called.
Diffstat (limited to 'src/leap/common/tests')
-rw-r--r--src/leap/common/tests/test_keymanager.py47
1 files changed, 21 insertions, 26 deletions
diff --git a/src/leap/common/tests/test_keymanager.py b/src/leap/common/tests/test_keymanager.py
index 48f7273..a7aa1ca 100644
--- a/src/leap/common/tests/test_keymanager.py
+++ b/src/leap/common/tests/test_keymanager.py
@@ -30,16 +30,15 @@ except ImportError:
from leap.common.testing.basetest import BaseLeapTest
from leap.soledad import Soledad
-from leap.soledad.crypto import SoledadCrypto
-
+#from leap.soledad.crypto import SoledadCrypto
from leap.common.keymanager import (
KeyManager,
openpgp,
KeyNotFound,
NoPasswordGiven,
- TAGS_INDEX,
- TAGS_AND_PRIVATE_INDEX,
+ #TAGS_INDEX,
+ #TAGS_AND_PRIVATE_INDEX,
)
from leap.common.keymanager.openpgp import OpenPGPKey
from leap.common.keymanager.keys import (
@@ -240,14 +239,16 @@ class OpenPGPCryptoTestCase(KeyManagerWithSoledadTestCase):
KeyNotFound, pgp.get_key, ADDRESS, private=True)
def test_openpgp_encrypt_decrypt_sym(self):
- cyphertext = openpgp.encrypt_sym('data', 'pass')
+ cyphertext = openpgp.encrypt_sym(
+ 'data', passphrase='pass')
self.assertTrue(cyphertext is not None)
self.assertTrue(cyphertext != '')
self.assertTrue(cyphertext != 'data')
self.assertTrue(openpgp.is_encrypted_sym(cyphertext))
self.assertFalse(openpgp.is_encrypted_asym(cyphertext))
self.assertTrue(openpgp.is_encrypted(cyphertext))
- plaintext = openpgp.decrypt_sym(cyphertext, 'pass')
+ plaintext = openpgp.decrypt_sym(
+ cyphertext, passphrase='pass')
self.assertEqual('data', plaintext)
def test_verify_with_private_raises(self):
@@ -298,7 +299,7 @@ class OpenPGPCryptoTestCase(KeyManagerWithSoledadTestCase):
pubkey = pgp.get_key(ADDRESS, private=False)
self.assertRaises(
AssertionError,
- openpgp.encrypt_sym, data, '123', sign=pubkey)
+ openpgp.encrypt_sym, data, passphrase='123', sign=pubkey)
def test_decrypt_asym_verify_with_private_raises(self):
pgp = openpgp.OpenPGPScheme(self._soledad)
@@ -321,7 +322,7 @@ class OpenPGPCryptoTestCase(KeyManagerWithSoledadTestCase):
pubkey = pgp.get_key(ADDRESS, private=False)
encrypted_and_signed = openpgp.encrypt_asym(data, pubkey, sign=privkey)
pgp.put_ascii_key(PUBLIC_KEY_2)
- wrongkey = pgp.get_key('anotheruser@leap.se')
+ wrongkey = pgp.get_key(ADDRESS_2)
self.assertRaises(
errors.InvalidSignature,
openpgp.verify, encrypted_and_signed, wrongkey)
@@ -332,19 +333,8 @@ class OpenPGPCryptoTestCase(KeyManagerWithSoledadTestCase):
data = 'data'
privkey = pgp.get_key(ADDRESS, private=True)
encrypted_and_signed = openpgp.encrypt_sym(data, '123', sign=privkey)
- self.assertRaises(
- AssertionError,
- openpgp.decrypt_sym,
- encrypted_and_signed, 'decrypt', verify=privkey)
-
- def test_decrypt_sym_verify_with_private_raises(self):
- pgp = openpgp.OpenPGPScheme(self._soledad)
- pgp.put_ascii_key(PRIVATE_KEY)
- data = 'data'
- privkey = pgp.get_key(ADDRESS, private=True)
- encrypted_and_signed = openpgp.encrypt_sym(data, '123', sign=privkey)
pgp.put_ascii_key(PUBLIC_KEY_2)
- wrongkey = pgp.get_key('anotheruser@leap.se')
+ wrongkey = pgp.get_key(ADDRESS_2)
self.assertRaises(
errors.InvalidSignature,
openpgp.verify, encrypted_and_signed, wrongkey)
@@ -385,8 +375,7 @@ class OpenPGPCryptoTestCase(KeyManagerWithSoledadTestCase):
self.assertEqual(data, res)
-class KeyManagerKeyManagementTestCase(
- KeyManagerWithSoledadTestCase):
+class KeyManagerKeyManagementTestCase(KeyManagerWithSoledadTestCase):
def test_get_all_keys_in_db(self):
km = self._key_manager()
@@ -477,15 +466,15 @@ class KeyManagerKeyManagementTestCase(
headers = {'content-type': 'application/json'}
def json(self):
- return {'address': 'anotheruser@leap.se', 'keys': []}
+ return {'address': ADDRESS_2, 'keys': []}
km._fetcher.get = Mock(
return_value=Response())
# do the fetch
- km.fetch_keys_from_server('anotheruser@leap.se')
+ km.fetch_keys_from_server(ADDRESS_2)
# and verify the call
km._fetcher.get.assert_called_once_with(
- km._nickserver_url + '/key/' + 'anotheruser@leap.se',
+ km._nickserver_url + '/key/' + ADDRESS_2,
)
def test_refresh_keys(self):
@@ -495,11 +484,13 @@ class KeyManagerKeyManagementTestCase(
km.fetch_keys_from_server = Mock(return_value=[])
km.refresh_keys()
km.fetch_keys_from_server.assert_called_once_with(
- 'leap@leap.se'
+ ADDRESS
)
# Key material for testing
+
+# key 24D18DDF: public key "Leap Test Key <leap@leap.se>"
KEY_FINGERPRINT = "E36E738D69173C13D709E44F2F455E2824D18DDF"
PUBLIC_KEY = """
-----BEGIN PGP PUBLIC KEY BLOCK-----
@@ -662,6 +653,7 @@ RZXoH+FTg9UAW87eqU610npOkT6cRaBxaMK/mDtGNdc=
-----END PGP PRIVATE KEY BLOCK-----
"""
+# key 7FEE575A: public key "anotheruser <anotheruser@leap.se>"
PUBLIC_KEY_2 = """
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.10 (GNU/Linux)
@@ -719,3 +711,6 @@ THx7N776fcYHGumbqUMYrxrcZSbNveE6SaK8fphRam1dewM0
=a5gs
-----END PGP PRIVATE KEY BLOCK-----
"""
+import unittest
+if __name__ == "__main__":
+ unittest.main()