def test_sign_and_encrypt(self): secret_keydata = open(self.key_sender_key, "r").read() # We get the public portion of the key sender = TempContext() sender.op_import(secret_keydata) result = sender.op_import_result() fpr = result.imports[0].fpr sink = gpg.Data() sender.op_export(fpr, 0, sink) sink.seek(0, 0) # This is the key that we will sign public_sender_key = sink.read() keys = get_usable_secret_keys(homedir=self.key_sender_homedir) assert_equals(1, len(keys)) key = keys[0] uids = key.uidslist # Now finally call the function under test uid_encrypted = list( sign_keydata_and_encrypt(public_sender_key, error_cb=None, homedir=self.key_receiver_homedir)) assert_equals(len(uids), len(uid_encrypted)) # We need to explicitly request signatures sender.set_keylist_mode(gpg.constants.KEYLIST_MODE_SIGS) uids_before = sender.get_key(fpr).uids sigs = [uid.signatures for uid in uids_before] sigs_before = [sig for signatures in sigs for sig in signatures] for uid, uid_enc in zip(uids_before, uid_encrypted): # The test doesn't work so well, because comments # are not rendered :-/ # assert_equals(uid, uid_enc[0]) assert_in(uid.name, uid_enc[0].uid) assert_in(uid.email, uid_enc[0].uid) ciphertext = uid_enc[1] log.debug("Decrypting %r", ciphertext) plaintext, result, vrfy = sender.decrypt(ciphertext) log.debug("Decrypt Result: %r", result) sender.op_import(plaintext) import_result = sender.op_import_result() log.debug("Import Result: %r", import_result) assert_equals(1, import_result.new_signatures) updated_key = sender.get_key(fpr) log.debug("updated key: %r", updated_key) log.debug("updated key sigs: %r", [(uid, uid.signatures) for uid in updated_key.uids]) uids_after = sender.get_key(fpr).uids sigs = [uid.signatures for uid in uids_after] sigs_after = [sig for signatures in sigs for sig in signatures] assert_greater(len(sigs_after), len(sigs_before))
def test_sign_and_encrypt(self): secret_keydata = open(self.key_sender_key, "r").read() # We get the public portion of the key sender = TempContext() sender.op_import(secret_keydata) result = sender.op_import_result() fpr = result.imports[0].fpr sink = gpg.Data() sender.op_export(fpr, 0, sink) sink.seek(0, 0) # This is the key that we will sign public_sender_key = sink.read() keys = get_usable_secret_keys(homedir=self.key_sender_homedir) assert_equals(1, len(keys)) key = keys[0] uids = key.uidslist # Now finally call the function under test uid_encrypted = list(sign_keydata_and_encrypt(public_sender_key, error_cb=None, homedir=self.key_receiver_homedir)) assert_equals(len(uids), len(uid_encrypted)) # We need to explicitly request signatures sender.set_keylist_mode(gpg.constants.KEYLIST_MODE_SIGS) uids_before = sender.get_key(fpr).uids sigs = [uid.signatures for uid in uids_before] sigs_before = [sig for signatures in sigs for sig in signatures] for uid, uid_enc in zip(uids_before, uid_encrypted): # The test doesn't work so well, because comments # are not rendered :-/ # assert_equals(uid, uid_enc[0]) assert_in(uid.name, uid_enc[0].uid) assert_in(uid.email, uid_enc[0].uid) ciphertext = uid_enc[1] log.debug("Decrypting %r", ciphertext) plaintext, result, vrfy = sender.decrypt(ciphertext) log.debug("Decrypt Result: %r", result) sender.op_import(plaintext) import_result = sender.op_import_result() log.debug("Import Result: %r", import_result) assert_equals(1, import_result.new_signatures) updated_key = sender.get_key(fpr) log.debug("updated key: %r", updated_key) log.debug("updated key sigs: %r", [(uid, uid.signatures) for uid in updated_key.uids]) uids_after = sender.get_key(fpr).uids sigs = [uid.signatures for uid in uids_after] sigs_after = [sig for signatures in sigs for sig in signatures] assert_greater(len(sigs_after), len(sigs_before))
def test_get_usable_key_fpr(self): fpr = self.originalkey.fingerprint keys = get_usable_secret_keys(fpr, homedir=self.homedir) assert_equals(1, len(keys)) key = keys[0] assert_equals(fpr, self.originalkey.fingerprint)
def test_get_usable_key_no_pattern(self): keys = get_usable_secret_keys(homedir=self.homedir) assert_equals(1, len(keys)) key = keys[0] assert_equals(self.originalkey, key)