def test_execute_method_valid(self): self.message = "Hello world." self.passphrase = 'passphrase' self.s = signer.Signer(self.keydir) sig = self.s.execute(self.message, self.key_fingerprint, self.passphrase) result = self.v.execute(str(sig)) self.assertTrue(result.valid)
def __init__(self, contact_response_host_address, key_home_dir, passphrase): self.key_home_dir = key_home_dir self.gpgsigner = signer.Signer(self.key_home_dir) self.contact_response_host = contact_response_host_address self.contact_response_port = settings.port_contact_response_server self.passphrase = passphrase
def test_sign_method_returns_valid_sig_of_message(self): sign_agent = signer.Signer(self.home_dir) target = sign_agent.execute(self.msg, self.host_key_id, self.passphrase) result = self.x.sign() print(result) print(target) self.assertEqual(str(result), str(target))
def test_execute_method_not_valid(self): self.message = "Hello world." self.passphrase = 'passsphrase' self.s = signer.Signer(self.keydir) sig = self.s.execute(self.message, self.key_fingerprint, self.passphrase, detach=True) data = bytes(self.message, 'utf-8') with tempfile.NamedTemporaryFile() as tmp_file: tmp_file.write(bytes(str(sig), 'utf-8')) tmp_file.seek(0) verify_detached = self.x.execute(tmp_file.name, data) self.assertFalse(verify_detached.valid)
def __init__(self, message_server_host_address, key_home_dir, peer_pub_key, host_fingerprint, message_text, passphrase): self.message_server_host_address = message_server_host_address self.message_server_host_port = settings.port_message_server self.key_home_dir = key_home_dir self.peer_gpg_pub_key = peer_pub_key # this is the SEND-TO val, i.e. addressee self.host_fingerprint = host_fingerprint # this is the SENT-FROM val, i.e. addresser self.message_text = message_text self.passphrase = passphrase self.gpgsigner = signer.Signer(self.key_home_dir) self.temp_keyring = tempkeyring.TempKeyRing() self.encrypt_agent = encrypter.Encrypter(self.key_home_dir) self.nonce = None self.data_dict = None self.sig_dict = None self.target_fingerprint = None self.ciphertext_dict = None self.valid = None self.error = None
def __init__(self, message_server_host_address, key_home_dir, request_data_record, passphrase): self.request_data_record = request_data_record self.message_server_address = message_server_host_address self.message_server_port = settings.port_message_server self.key_home_dir = key_home_dir self.passphrase = passphrase self.gpgsigner = signer.Signer(self.key_home_dir) self.temp_dir = self.create_temp_dir() self.temp_dir_name = self.temp_dir.name self.key_ring = keyring.KeyRing(self.temp_dir_name) self.encrypt_agent = encrypter.Encrypter(self.temp_dir_name) self.target_fingerprint = None self.request_nonce = None self.response_nonce = None self.data_dict = None self.encoded_data_dict = None self.sig = None self.error = None self.valid = None self.sig_dict = None self.encoded_sig_dict = None
def test_verify_method_returns_result_of_verification(self): sign_agent = signer.Signer(self.home_dir) target = sign_agent.execute(self.msg, self.host_key_id, self.passphrase) result = self.x.verify(str(target)) self.assertTrue(result.valid)
class TestClassBasics(unittest.TestCase): """ Set everything up as class attributes, then copy the class instance in setup, this is much faster to run. """ message = "Hello world" key_dir = 'tests/data/keys' key_ring = keyring.KeyRing(key_dir) encrypt_agent = encrypter.Encrypter(key_dir) encrypted_msg = str(encrypt_agent.execute(message, '190DB52959AC3560')) mock_valid_payload = dict(ciphertext=encrypted_msg, nonce='nonce_string') gpg_signer = signer.Signer(key_dir) valid_sig = gpg_signer.execute(message, None, 'passphrase', detach=True) valid_sig_dict = dict(sig=str(valid_sig), data=message) passphrase = 'passphrase' valid_obj = messagevalidator.MessageValidator(mock_valid_payload, key_dir, passphrase) def setUp(self): self.x = copy.deepcopy(self.valid_obj) def test_instance(self): self.assertIsInstance(self.x, messagevalidator.MessageValidator) def test_target_payload_attribute_set(self): self.assertEqual(self.x.message_payload, self.mock_valid_payload) def test_key_dir_attribute_set(self): self.assertEqual(self.x.key_dir, self.key_dir) def test_passphrase_attr_set(self): self.assertEqual(self.x.passphrase, self.passphrase) def test_error_attribute_set_none(self): self.assertIsNone(self.x.error) def test_valid_attribute_set_none(self): self.assertIsNone(self.x.valid) def test_data_dict_attribute_set_none(self): self.assertIsNone(self.x.data_dict) def test_verify_result_attribute_set_none(self): self.assertIsNone(self.x.verify_result) def test_gpg_verifier_attribute_is_verifier_home_set(self): self.assertIsInstance(self.x.gpg_verifier, detachedverifier.DetachedVerifier) self.assertEqual(self.x.key_dir, self.x.gpg_verifier.home) def test_set_error_method_sets_error_and_valid(self): msg = 'Error Message' self.x.set_error(msg) self.assertEqual(self.x.error, msg) self.assertIs(self.x.valid, False) def test_check_payload_keys_method_checks_payload_keys_sets_attributes( self): result = self.x.check_payload_keys(self.x.message_payload) self.assertIs(result, True) def test_check_payload_keys_method_returns_false_on_invalid_dict(self): bad_dict = dict() result = self.x.check_payload_keys(bad_dict) self.assertIs(result, False) def test_check_payload_keys_method_returns_false_on_bad_data(self): bad_data = 'xxx' result = self.x.check_payload_keys(bad_data) self.assertIs(result, False) @patch('net.message.messagevalidator.decrypter.Decrypter') def test_decrypt_ciphertext_method_calls_decrypter_with_key_dir( self, target): mock_ciphertext = 'xxx' self.x.decrypt_ciphertext(mock_ciphertext) target.assert_called_with(self.x.key_dir) @patch.object(messagevalidator.decrypter.Decrypter, 'execute') def test_decrypt_ciphertext_calls_execute_with_args(self, target): passphrase = 'passphrase' self.x.decrypt_ciphertext(self.encrypted_msg) target.assert_called_with(self.encrypted_msg, self.passphrase) @patch.object(messagevalidator.decrypter.Decrypter, 'execute') def test_decrypt_ciphertext_sets_error_returns_false_if_bad_response( self, sub): class MockBadResult: ok = False status = 'status_message' sub.return_value = MockBadResult() passphrase = 'passphrase' target = self.x.set_error = MagicMock() result = self.x.decrypt_ciphertext(self.encrypted_msg) target.assert_called_with(MockBadResult.status) self.assertIs(result, False) @patch.object(messagevalidator.decrypter.Decrypter, 'execute') def test_decrypt_ciphertext_returns_plaintext_on_valid_result(self, sub): class MockBadResult: ok = True status = 'status_message' sub.return_value = MockBadResult() passphrase = 'passphrase' result = self.x.decrypt_ciphertext(self.encrypted_msg) self.assertEqual(result, str(sub.return_value)) def test_decode_json_decodes_returns_valid_json(self): msg_dict = dict(msg='hello world') encoded = json.dumps(msg_dict) result = self.x.decode_json_string(encoded) self.assertEqual(result, msg_dict) def test_decode_json_returns_false_on_invalid(self): msg_dict = dict(msg='hello world') result = self.x.decode_json_string(msg_dict) self.assertIs(result, False) def test_check_sig_dict_keys_returns_true_on_valid(self): sig_dict = dict(sig='sig', data='data') result = self.x.check_sig_dict_keys(sig_dict) self.assertIs(result, True) def test_check_sig_dict_keys_returns_false_on_invalid(self): sig_dict = dict(xxx='sig', data='data') result = self.x.check_sig_dict_keys(sig_dict) self.assertIs(result, False) def test_check_sig_dict_keys_returns_false_on_bad_input(self): bad = 123 result = self.x.check_sig_dict_keys(bad) self.assertIs(result, False) def test_verify_sig_dict_verifies_valid_sig_dict_returns_true(self): result = self.x.verify_sig_dict(self.valid_sig_dict) self.assertIs(result, True) def test_verify_sig_dict_sets_error_returns_false_on_invalid(self): sig_dict = dict(sig='sig', data='data') target = self.x.set_error = MagicMock() result = self.x.verify_sig_dict(sig_dict) self.assertIs(result, False) self.assertTrue(target.called) def test_verify_sig_dict_sets_verify_result_attribute(self): result = self.x.verify_sig_dict(self.valid_sig_dict) self.assertIsNotNone(self.x.verify_result) def test_check_data_dict_keys_returns_true_on_valid(self): data_dict = dict(address_host='host-string', address_port='port_string', nonce='nonce_string', message='message_string') result = self.x.check_data_dict_keys(data_dict) self.assertTrue(result) def test_check_data_dict_keys_returns_false_on_not_valid(self): data_dict = dict(xxxx='host-string', address_port='port_string', nonce='nonce_string', message='message_string') result = self.x.check_data_dict_keys(data_dict) self.assertIs(result, False) def test_validate_calls_check_payload_keys_returns_false_if_false_and_sets_error( self): target_1 = self.x.check_payload_keys = MagicMock(return_value=False) target_2 = self.x.set_error = MagicMock() result = self.x.validate() target_1.assert_called_with(self.x.message_payload) self.assertTrue(target_2.called) self.assertIs(result, False) def test_validate_calls_decrypt_ciphertext_returns_false_if_false(self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value=False) result = self.x.validate() decrypt_method_mock.assert_called_with( self.x.message_payload['ciphertext']) self.assertIs(result, False) def test_validate_calls_json_decode_if_decrypt_not_false(self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock() result = self.x.validate() json_decode_mock.assert_any_call(decrypt_method_mock.return_value) def test_validate_sets_error_returns_false_if_json_decode_of_decrypt_is_false( self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value=False) result = self.x.validate() self.assertTrue(set_error_mock.called) self.assertIs(result, False) def test_validate_calls_check_sig_dict_keys_if_json_decode_of_decrypt_not_false( self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value=self.valid_sig_dict) verify_sig_mock = self.x.verify_sig_dict = MagicMock() check_sig_dict_keys_mock = self.x.check_sig_dict_keys = MagicMock() result = self.x.validate() self.assertTrue(check_sig_dict_keys_mock.called) def test_validate_sets_error_returns_false_if_sig_dict_key_check_returns_false( self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value='xxxx') check_sig_dict_keys_mock = self.x.check_sig_dict_keys = MagicMock( return_value=False) result = self.x.validate() self.assertTrue(set_error_mock.called) self.assertIs(result, False) def test_validate_calls_verify_sig_dict_with_sig_dict_if_sig_dict_keys_valid( self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value=self.valid_sig_dict) check_sig_dict_keys_mock = self.x.check_sig_dict_keys = MagicMock( return_value=True) verify_sig_mock = self.x.verify_sig_dict = MagicMock() result = self.x.validate() self.assertTrue(verify_sig_mock.called) def test_validate_returns_false_if_verify_sig_dict_is_false(self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value='xxxx') check_sig_dict_keys_mock = self.x.check_sig_dict_keys = MagicMock( return_value=True) verify_sig_mock = self.x.verify_sig_dict = MagicMock( return_value=False) result = self.x.validate() self.assertIs(result, False) def test_validate_decodes_data_dict_string(self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value=self.valid_sig_dict) check_sig_dict_keys_mock = self.x.check_sig_dict_keys = MagicMock( return_value=True) verify_sig_mock = self.x.verify_sig_dict = MagicMock(return_value=True) result = self.x.validate() json_decode_mock.assert_any_call(json_decode_mock.return_value['data']) def test_validate_calls_check_data_keys_if_verify_sig_is_true(self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value=self.valid_sig_dict) check_sig_dict_keys_mock = self.x.check_sig_dict_keys = MagicMock( return_value=True) verify_sig_mock = self.x.verify_sig_dict = MagicMock(return_value=True) check_data_keys_mock = self.x.check_data_dict_keys = MagicMock() result = self.x.validate() self.assertTrue(check_data_keys_mock.called) def test_validate_sets_error_returns_false_if_data_dict_check_is_false( self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value=self.valid_sig_dict) check_sig_dict_keys_mock = self.x.check_sig_dict_keys = MagicMock( return_value=True) verify_sig_mock = self.x.verify_sig_dict = MagicMock(return_value=True) check_data_keys_mock = self.x.check_data_dict_keys = MagicMock( return_value=False) result = self.x.validate() self.assertTrue(set_error_mock.called) self.assertIs(result, False) def test_validate_sets_valid_true_sets_data_dict_attribute_if_data_dict_check_true( self): check_payload_key_mock = self.x.check_payload_keys = MagicMock( return_value=True) set_error_mock = self.x.set_error = MagicMock() decrypt_method_mock = self.x.decrypt_ciphertext = MagicMock( return_value='xxxxx') json_decode_mock = self.x.decode_json_string = MagicMock( return_value=self.valid_sig_dict) check_sig_dict_keys_mock = self.x.check_sig_dict_keys = MagicMock( return_value=True) verify_sig_mock = self.x.verify_sig_dict = MagicMock(return_value=True) check_data_keys_mock = self.x.check_data_dict_keys = MagicMock( return_value=True) result = self.x.validate() self.assertTrue(self.x.valid) self.assertIs(result, True) self.assertIsNotNone(self.x.data_dict) def test_with_valid_message_from_factory(self): msg_dict = build_valid_message_payload_with_factory() validator = messagevalidator.MessageValidator(msg_dict, self.key_dir, self.passphrase) result = validator.validate() self.assertIs(result, True)
def setUp(self): self.keydir = "tests/data/keys" self.key_fingerprint = 'AA74BBFE8A31ADBC0E9ED26B190DB52959AC3560' self.s = signer.Signer(self.keydir)
def sign(self): sign_agent = signer.Signer(self.home_dir) result = sign_agent.execute(self.msg, self.host_key_id, self.passphrase) return result
def sign(self, message, keyid, passphrase, detach=False): agent = signer.Signer(self.key_dir) result = agent.execute(message, keyid, passphrase, detach=detach) return result