fixed a potential inconsistency between a successful PGPKey.unlock and one that warns that the operation did nothing if it is invoked with an "as ..." clause; updated relevant tests to check that

This commit is contained in:
Michael Greene
2014-10-15 12:05:48 -07:00
parent 5c63f516f5
commit 123874aac4
3 changed files with 8 additions and 7 deletions

View File

@@ -1313,13 +1313,13 @@ class PGPKey(PGPObject, Armorable):
if self.is_public:
# we can't unprotect public keys because only private key material is ever protected
warnings.warn("Public keys cannot be passphrase-protected", stacklevel=3)
yield
yield self
return
if not self.is_protected:
# we can't unprotect private keys that are not protected, because there is no ciphertext to decrypt
warnings.warn("This key is not protected with a passphrase", stacklevel=3)
yield
yield self
return
try:

View File

@@ -230,7 +230,8 @@ class TestPGPKey(object):
assert sec.is_protected is False
# unlock with the correct passphrase
with enc.unlock('QwertyUiop'), self.assert_warnings():
with enc.unlock('QwertyUiop') as _unlocked, self.assert_warnings():
assert _unlocked is enc
assert enc.is_unlocked
def test_verify_detached(self, sigkey, sigsig, sigsubj):

View File

@@ -49,16 +49,16 @@ def targette_sec():
class TestPGPKey(object):
def test_unlock_pubkey(self, rsa_pub, recwarn):
with rsa_pub.unlock("QwertyUiop"):
pass
with rsa_pub.unlock("QwertyUiop") as _unlocked:
assert _unlocked is rsa_pub
w = recwarn.pop(UserWarning)
assert str(w.message) == "Public keys cannot be passphrase-protected"
assert w.filename == __file__
def test_unlock_not_protected(self, rsa_sec, recwarn):
with rsa_sec.unlock("QwertyUiop"):
pass
with rsa_sec.unlock("QwertyUiop") as _unlocked:
assert _unlocked is rsa_sec
w = recwarn.pop(UserWarning)
assert str(w.message) == "This key is not protected with a passphrase"