Exemplo n.º 1
0
 def setUp(self):
     self.keydir = "tests/data/keys"
     self.key_fingerprint = 'C227D0EC9289CB9D1F06A9A85933EB9BDA9B62BB'
     self.key_ring = keyring.KeyRing(self.keydir)
     self.key_finder = keyfinder.KeyFinder(self.key_ring)
     self.message = "Hello world."
     self.e = encrypter.Encrypter(self.keydir)
Exemplo n.º 2
0
 def encrypt(self, plaintext, keyid):
     fingerprint = self.key_finder.get_fingerprint_by_keyid(keyid)
     if fingerprint is None:
         return None
     else:
         agent = encrypter.Encrypter(self.key_dir)
         result = agent.execute(plaintext, fingerprint)
         return result
Exemplo n.º 3
0
 def test_decrypt_message_result_not_valid_passphrase(self):
     self.encrypter = encrypter.Encrypter(self.keydir)
     self.message = "Hello world."
     encrypt_result = self.encrypter.execute(self.message,
                                             self.key_fingerprint)
     self.ciphertext = str(encrypt_result)
     self.passphrase = 'passphddddrase'
     result = self.d.execute(self.ciphertext, self.passphrase)
     self.assertFalse(result.ok)
 def setUp(self):
     self.message = "Hello world"
     self.key_dir = 'tests/data/keys'
     self.key_ring = keyring.KeyRing(self.key_dir)
     self.encrypt_agent = encrypter.Encrypter(self.key_dir)
     self.encrypted_msg = str(
         self.encrypt_agent.execute(self.message, '55A45A99FE45E540'))
     self.mock_valid_payload = dict(ciphertext=self.encrypted_msg,
                                    request_nonce='req_nonce_string',
                                    response_nonce='resp_nonce_string')
     self.mock_database_table = MagicMock()
     self.passphrase = 'passphrase'
     self.x = contactresponsevalidator.ContactResponseValidator(
         self.mock_valid_payload, self.key_dir, self.mock_database_table,
         self.passphrase)
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
 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
Exemplo n.º 7
0
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)