more coverage

This commit is contained in:
Michael Greene
2014-09-30 15:27:14 -07:00
parent e52d130704
commit b1204fb5ac
8 changed files with 168 additions and 53 deletions

View File

@@ -811,7 +811,7 @@ class PGPMessage(PGPObject, Armorable):
self += self.dash_unescape(unarmored['cleartext'])
while len(data) > 0:
pkt = Packet(data)
if not isinstance(pkt, Signature):
if not isinstance(pkt, Signature): # pragma: no cover
warnings.warn("Discarded unexpected packet: {:s}".format(pkt.__class__.__name__), stacklevel=2)
continue
self += PGPSignature() + pkt
@@ -1289,9 +1289,9 @@ class PGPKey(PGPObject, Armorable):
# some type checking
if not isinstance(subject, (type(None), PGPMessage, PGPKey, PGPUID, PGPSignature, six.string_types, bytes, bytearray)):
raise ValueError("Unexpected subject value: {:s}".format(str(type(subject))))
raise TypeError("Unexpected subject value: {:s}".format(str(type(subject))))
if not isinstance(signature, (type(None), PGPSignature)):
raise ValueError("Unexpected signature value: {:s}".format(str(type(signature))))
raise TypeError("Unexpected signature value: {:s}".format(str(type(signature))))
def _filter_sigs(sigs):
_ids = {self.fingerprint.keyid} | set(self.subkeys)
@@ -1648,13 +1648,7 @@ class PGPKeyring(collections.Container, collections.Iterable, collections.Sized)
if isinstance(identifier, PGPSignature):
identifier = identifier.signer
if identifier in self:
pgpkey = self._get_key(identifier)
else:
raise KeyError(identifier)
yield pgpkey
yield self._get_key(identifier)
def fingerprints(self, keyhalf='any', keytype='any'):
return list({pk.fingerprint for pk in self._keys.values()

View File

@@ -13,10 +13,6 @@ from pgpy import PGPMessage
from pgpy import PGPSignature
from pgpy import PGPUID
from pgpy.errors import PGPDecryptionError
from pgpy.errors import PGPEncryptionError
from pgpy.errors import PGPError
from pgpy.constants import CompressionAlgorithm
from pgpy.constants import ImageEncoding
from pgpy.constants import PubKeyAlgorithm
@@ -26,6 +22,8 @@ from pgpy.constants import SymmetricKeyAlgorithm
from pgpy.constants import KeyFlags
from pgpy.constants import HashAlgorithm
from pgpy.errors import PGPError
def _pgpmessage(f):
msg = PGPMessage()
@@ -33,11 +31,6 @@ def _pgpmessage(f):
msg.parse(ff.read())
return msg
def _pgpmessage_new(f):
with open(f, 'r') as ff:
msg = PGPMessage.new(ff.read())
return msg
def _pgpkey(f):
key = PGPKey()
with open(f, 'r') as ff:
@@ -55,45 +48,12 @@ def _read(f, mode='r'):
return ff.read()
class TestExceptions(object):
def test_pgpkey_verify_wrongkey(self):
wrongkey = _pgpkey('tests/testdata/signatures/aptapproval-test.key.asc')
sig = _pgpsignature('tests/testdata/signatures/debian-sid.sig.asc')
with pytest.raises(PGPError):
wrongkey.verify(_read('tests/testdata/signatures/debian-sid.subj'), sig)
def test_pgpkey_decrypt_unencrypted_message(self, recwarn):
lit = _pgpmessage_new('tests/testdata/lit')
key = _pgpkey('tests/testdata/keys/rsa.1.sec.asc')
key.decrypt(lit)
w = recwarn.pop(UserWarning)
assert str(w.message) == "This message is not encrypted"
assert w.filename == __file__
def test_pgpmessage_decrypt_unsupported_algorithm(self):
msg = _pgpmessage('tests/testdata/message.enc.twofish.asc')
with pytest.raises(PGPDecryptionError):
msg.decrypt("QwertyUiop")
def test_pgpmessage_decrypt_wrongpass(self):
msg = _pgpmessage(next(f for f in glob.glob('tests/testdata/messages/message*.pass*.asc')))
with pytest.raises(PGPDecryptionError):
msg.decrypt("TheWrongPassword")
def test_pgpmessage_encrypt_unsupported_algorithm(self):
lit = _pgpmessage_new('tests/testdata/lit')
with pytest.raises(PGPEncryptionError):
lit.encrypt("QwertyUiop", cipher=SymmetricKeyAlgorithm.Twofish256)
class TestPGPMessage(object):
params = {
'comp_alg': [ CompressionAlgorithm.Uncompressed, CompressionAlgorithm.ZIP, CompressionAlgorithm.ZLIB,
CompressionAlgorithm.BZ2 ],
'enc_msg': [ _pgpmessage(f) for f in glob.glob('tests/testdata/messages/message*.pass*.asc') ],
'lit': [ _pgpmessage_new('tests/testdata/lit') ],
'lit': [ PGPMessage.new(_read('tests/testdata/lit')) ],
}
def test_new_message(self, comp_alg, write_clean, gpg_import, gpg_print):
with open('tests/testdata/lit', 'r') as litf:

161
tests/test_10_exceptions.py Normal file
View File

@@ -0,0 +1,161 @@
""" explicitly test error scenarios
"""
import pytest
import glob
from pgpy import PGPKey
from pgpy import PGPKeyring
from pgpy import PGPMessage
from pgpy import PGPSignature
from pgpy import PGPUID
from pgpy.constants import SymmetricKeyAlgorithm
from pgpy.errors import PGPDecryptionError
from pgpy.errors import PGPEncryptionError
from pgpy.errors import PGPError
def _pgpkey(f):
key = PGPKey()
with open(f, 'r') as ff:
key.parse(ff.read())
return key
def _pgpmessage(f):
msg = PGPMessage()
with open(f, 'r') as ff:
msg.parse(ff.read())
return msg
def _pgpsignature(f):
sig = PGPSignature()
with open(f, 'r') as ff:
sig.parse(ff.read())
return sig
def _read(f, mode='r'):
with open(f, mode) as ff:
return ff.read()
class TestPGPKey(object):
rsa_1_sec = _pgpkey('tests/testdata/keys/rsa.1.sec.asc')
rsa_2_sec = _pgpkey('tests/testdata/keys/rsa.2.sec.asc')
def test_verify_wrongkey(self):
wrongkey = _pgpkey('tests/testdata/signatures/aptapproval-test.key.asc')
sig = _pgpsignature('tests/testdata/signatures/debian-sid.sig.asc')
with pytest.raises(PGPError):
wrongkey.verify(_read('tests/testdata/signatures/debian-sid.subj'), sig)
def test_decrypt_unencrypted_message(self, recwarn):
lit = PGPMessage.new('tests/testdata/lit')
self.rsa_1_sec.decrypt(lit)
w = recwarn.pop(UserWarning)
assert str(w.message) == "This message is not encrypted"
assert w.filename == __file__
def test_decrypt_wrongkey(self):
msg = _pgpmessage('tests/testdata/messages/message.rsa.cast5.asc')
with pytest.raises(PGPError):
self.rsa_2_sec.decrypt(msg)
def test_add_valueerror(self):
with pytest.raises(TypeError):
self.rsa_1_sec += 12
def test_contains_valueerror(self):
with pytest.raises(TypeError):
12 in self.rsa_1_sec
def test_fail_del_uid(self):
with pytest.raises(PGPError):
self.rsa_1_sec.del_uid("ASDFDSGSAJGKSAJG")
def test_verify_typeerror(self):
with pytest.raises(TypeError):
self.rsa_1_sec.verify(12)
with pytest.raises(TypeError):
self.rsa_1_sec.verify("asdf", signature=12)
def test_verify_nosigs(self):
msg = PGPMessage.new('tests/testdata/lit')
with pytest.raises(PGPError):
self.rsa_1_sec.verify(msg)
def test_parse_wrong_magic(self):
keytext = _read('tests/testdata/keys/rsa.1.sec.asc').replace('KEY', 'EKY')
key = PGPKey()
with pytest.raises(ValueError):
key.parse(keytext)
class TestPGPKeyring(object):
kr = PGPKeyring(_read('tests/testdata/pubtest.asc'))
def test_key_keyerror(self):
with pytest.raises(KeyError):
with self.kr.key('DEADBEA7CAFED00D'):
pass
class TestPGPMessage(object):
def test_decrypt_unsupported_algorithm(self):
msg = _pgpmessage('tests/testdata/message.enc.twofish.asc')
with pytest.raises(PGPDecryptionError):
msg.decrypt("QwertyUiop")
def test_decrypt_wrongpass(self):
msg = _pgpmessage(next(f for f in glob.glob('tests/testdata/messages/message*.pass*.asc')))
with pytest.raises(PGPDecryptionError):
msg.decrypt("TheWrongPassword")
def test_decrypt_unencrypted(self):
msg = _pgpmessage('tests/testdata/messages/message.signed.asc')
with pytest.raises(PGPError):
msg.decrypt("Password")
def test_encrypt_unsupported_algorithm(self):
lit = PGPMessage.new('tests/testdata/lit')
with pytest.raises(PGPEncryptionError):
lit.encrypt("QwertyUiop", cipher=SymmetricKeyAlgorithm.Twofish256)
def test_parse_wrong_magic(self):
msgtext = _read('tests/testdata/messages/message.signed.asc').replace('MESSAGE', 'EMSSAGE')
msg = PGPMessage()
with pytest.raises(ValueError):
msg.parse(msgtext)
class TestPGPSignature(object):
def test_add_typeerror(self):
with pytest.raises(TypeError):
PGPSignature() + 12
def test_parse_wrong_magic(self):
sigtext = _read('tests/testdata/blocks/signature.expired.asc').replace('SIGNATURE', 'SIGANTURE')
sig = PGPSignature()
with pytest.raises(ValueError):
sig.parse(sigtext)
def test_parse_wrong_contents(self):
notsigtext = _read('tests/testdata/blocks/message.compressed.asc').replace('MESSAGE', 'SIGNATURE')
sig = PGPSignature()
with pytest.raises(ValueError):
sig.parse(notsigtext)
class TestPGPUID(object):
def test_bad_new(self):
with pytest.raises(ValueError):
PGPUID.new()
def test_add_typeerror(self):
u = PGPUID.new(name="Asdf Qwert")
with pytest.raises(TypeError):
u += 12