Example #1
0
def test_process_single_file(tmpdir, patch_process_single_operation, mode,
                             decode_input, encode_output, expected_multiplier):
    patch_process_single_operation.return_value = identifiers.OperationResult.SUCCESS
    source = tmpdir.join("source")
    source.write("some data")
    kwargs = GOOD_IOHANDLER_KWARGS.copy()
    kwargs.update(dict(decode_input=decode_input, encode_output=encode_output))
    handler = io_handling.IOHandler(**kwargs)
    destination = tmpdir.join("destination")
    initial_kwargs = dict(mode=mode, a=sentinel.a, b=sentinel.b)
    expected_length = int(os.path.getsize(str(source)) * expected_multiplier)
    updated_kwargs = dict(mode=mode,
                          a=sentinel.a,
                          b=sentinel.b,
                          source_length=expected_length)
    with patch("aws_encryption_sdk_cli.internal.io_handling.open",
               create=True) as mock_open:
        handler.process_single_file(stream_args=initial_kwargs,
                                    source=str(source),
                                    destination=str(destination))
    mock_open.assert_called_once_with(str(source), "rb")
    patch_process_single_operation.assert_called_once_with(
        stream_args=updated_kwargs,
        source=mock_open.return_value.__enter__.return_value,
        destination=str(destination))
Example #2
0
def test_single_io_write_stream_no_buffering(tmpdir,
                                             patch_aws_encryption_sdk_stream,
                                             patch_json_ready_header,
                                             patch_json_ready_header_auth):
    patch_aws_encryption_sdk_stream.return_value.__enter__.return_value.__iter__ = MagicMock(
        return_value=iter((sentinel.chunk_1, sentinel.chunk_2)))

    mock_source = MagicMock()
    mock_destination = MagicMock()
    kwargs = GOOD_IOHANDLER_KWARGS.copy()
    kwargs["buffer_output"] = False
    handler = io_handling.IOHandler(**kwargs)

    handler._single_io_write(
        stream_args={
            "mode": "encrypt",
            "a": sentinel.a,
            "b": sentinel.b
        },
        source=mock_source,
        destination_writer=mock_destination,
    )

    patch_aws_encryption_sdk_stream.return_value.__enter__.return_value.__iter__.assert_called_once(
    )
    mock_destination.__enter__.return_value.write.assert_has_calls(
        [call(sentinel.chunk_1),
         call(sentinel.chunk_2)])
Example #3
0
def test_f_should_write_file_does_not_exist(tmpdir, interactive, no_overwrite):
    target = tmpdir.join("target")
    assert not os.path.exists(str(target))
    # Should always be true regardless of input if file does not exist
    kwargs = GOOD_IOHANDLER_KWARGS.copy()
    kwargs.update(dict(interactive=interactive, no_overwrite=no_overwrite))
    handler = io_handling.IOHandler(**kwargs)

    assert handler._should_write_file(str(target))
Example #4
0
def test_should_write_file_does_exist(tmpdir, patch_input, interactive,
                                      no_overwrite, user_input, expected):
    target_file = tmpdir.join("target")
    target_file.write(b"")
    patch_input.return_value = user_input
    kwargs = GOOD_IOHANDLER_KWARGS.copy()
    kwargs.update(dict(interactive=interactive, no_overwrite=no_overwrite))
    handler = io_handling.IOHandler(**kwargs)

    should_write = handler._should_write_file(str(target_file))

    if expected:
        assert should_write
    else:
        assert not should_write
Example #5
0
def test_single_io_write_stream_encode_output(tmpdir,
                                              patch_aws_encryption_sdk_stream,
                                              patch_json_ready_header,
                                              patch_json_ready_header_auth):
    patch_aws_encryption_sdk_stream.return_value = io.BytesIO(DATA)
    patch_aws_encryption_sdk_stream.return_value.header = MagicMock(
        encryption_context=sentinel.encryption_context)
    target_file = tmpdir.join("target")
    mock_source = MagicMock()
    kwargs = GOOD_IOHANDLER_KWARGS.copy()
    kwargs["encode_output"] = True
    handler = io_handling.IOHandler(**kwargs)
    with open(str(target_file), "wb") as destination_writer:
        handler._single_io_write(
            stream_args={
                "mode": "encrypt",
                "a": sentinel.a,
                "b": sentinel.b
            },
            source=mock_source,
            destination_writer=destination_writer,
        )

    assert target_file.read("rb") == base64.b64encode(DATA)
Example #6
0
def test_iohandler_attrs_fail(kwargs):
    _kwargs = GOOD_IOHANDLER_KWARGS.copy()
    _kwargs.update(kwargs)

    with pytest.raises(TypeError):
        io_handling.IOHandler(**_kwargs)
Example #7
0
def test_iohandler_attrs_good():
    io_handling.IOHandler(**GOOD_IOHANDLER_KWARGS)
Example #8
0
def standard_handler():
    return io_handling.IOHandler(**GOOD_IOHANDLER_KWARGS)