def test_write_image_diff_size_pil():
    """Writing out an image creates a file of a different size, if the file was generated by PIL originally."""
    clean_file = CLEAN_PNG_LOCATION
    dirty_file = "tests/dirtyImage_test_write_image_file_diff_size_pil.png"
    clean_file_pil = "tests/cleanImagePIL.png"
    with Image.open(clean_file) as pil_image:
        pil_image.save(clean_file_pil)

    stegs = Steganographer()
    clean_data = _open_image_file(clean_file_pil)
    dirty_data = clean_data[0], stegs._hide_string(
        clean_data[1], "Hidden text from test_write_image_diff_size_pil.")
    output_file = _write_image_file(dirty_file, clean_file_pil, dirty_data)

    # Getting the file sizes for the clean and dirty files.
    with open(clean_file_pil, 'rb') as clean:
        clean.seek(0, 2)
        clean_file_size = clean.tell()

    with open(output_file, 'rb') as dirty:
        dirty.seek(0, 2)
        dirty_file_size = dirty.tell()

    assert clean_file_size != dirty_file_size

    os.remove(output_file)
def test_hide_reveal_string_inverse(string_to_hide):
    """Anything hidden by _hide_string is revealed by _reveal_string."""
    clean_data = bytes(b'\x01' * 5000)
    stegs = Steganographer()
    stegs._header.data_len = len(string_to_hide.encode('utf-8'))
    revealed_string = stegs._reveal_string(
        stegs._hide_string(clean_data, string_to_hide))
    assert revealed_string == string_to_hide
def test_exact_data_with_string_inverse():
    """The string entered is the string returned. The storing data is the exact length needed."""
    test_string = "This is a test String"
    stegs = Steganographer()
    stegs._header.data_len = len(test_string)
    blank_data = bytes(b'\x01' * len(test_string) * stegs._BYTELEN)

    revealed_string = stegs._reveal_string(
        stegs._hide_string(blank_data, test_string))

    assert test_string == revealed_string
def test_short_data_with_string_inverse():
    """When the data is too small, by a full byte, everything that can be returned is returned."""
    test_string = "This is a test String"
    stegs = Steganographer()
    stegs._header.data_len = len(test_string)
    blank_data = bytes(b'\x01' *
                       (len(test_string) * stegs._BYTELEN - stegs._BYTELEN))

    revealed_string = stegs._reveal_string(
        stegs._hide_string(blank_data, test_string))

    assert test_string[:-1] == revealed_string
def test_hide_string():
    """Takes in a string and a bytes object and hides the string in that bytes object."""
    stegs = Steganographer()
    test_data = bytes(b'\x01' * stegs._BYTELEN * 3)
    solution_data = bytearray(stegs._BYTELEN * 3)
    solution_data[1] = 1
    solution_data[7] = 1
    solution_data[9] = 1
    solution_data[14] = 1
    solution_data[17] = 1
    solution_data[22] = 1
    solution_data[23] = 1

    assert stegs._hide_string(test_data, 'ABC') == solution_data
def test_write_image_same_image():
    """Writing out an image creates the same image when viewed generally."""
    clean_file = CLEAN_PNG_LOCATION
    dirty_file = "tests/dirtyImage_test_write_image_file_same_image.png"

    stegs = Steganographer()
    clean_data = _open_image_file(clean_file)
    dirty_data = clean_data[0], stegs._hide_string(
        clean_data[1], "Hidden text from test_write_image_same_image.")
    output_file = _write_image_file(dirty_file, clean_file, dirty_data)

    assert compare_images(clean_file, output_file) < 500

    os.remove(output_file)
def test_write_bin_diff_content():
    """The file written is different from the one read, after hiding a message."""
    clean_file = CLEAN_PNG_LOCATION
    dirty_file = "tests/dirtyImage_test_write_bin_file_diff_content.png"

    stegs = Steganographer()
    data = stegs._hide_string(_open_bin_file(clean_file),
                              "Hidden text from test_write_bin_diff_content.")
    _write_bin_file(dirty_file, data)

    with open(clean_file, 'rb') as clean, open(dirty_file, 'rb') as dirty:
        assert clean.read() != dirty.read()

    os.remove(dirty_file)
def test_short_partial_data_string_inverse():
    """When the data is too small, by a half byte, everything that can be returned is returned."""
    test_string = "This is a test String"
    stegs = Steganographer()
    stegs._header.data_len = len(test_string)
    solution_string = test_string[:-1] + chr(
        ord(test_string[-1]) >> stegs._BYTELEN // 2 << stegs._BYTELEN // 2)
    blank_data = bytes(
        b'\x01' * (len(test_string) * stegs._BYTELEN - stegs._BYTELEN // 2))

    revealed_string = stegs._reveal_string(
        stegs._hide_string(blank_data, test_string))

    assert solution_string == revealed_string
def test_write_image_diff_content():
    """Writing out an image creates a different image at the bit level."""
    clean_file = CLEAN_PNG_LOCATION
    dirty_file = "tests/dirtyImage_test_write_image_file_diff_content.png"

    stegs = Steganographer()
    clean_data = _open_image_file(clean_file)
    dirty_data = clean_data[0], stegs._hide_string(
        clean_data[1], "Hidden text from test_write_image_diff_content.")
    output_file = _write_image_file(dirty_file, clean_file, dirty_data)

    with open(clean_file, 'rb') as clean, open(output_file, 'rb') as dirty:
        assert clean.read() != dirty.read()

    os.remove(output_file)
Exemplo n.º 10
0
def test_write_image_file_valid():
    """The image created is not corrupt."""
    clean_file = CLEAN_PNG_LOCATION
    dirty_file = "tests/dirtyImage_test_write_image_file_valid_image.png"

    stegs = Steganographer()
    clean_data = _open_image_file(clean_file)
    dirty_data = clean_data[0], stegs._hide_string(
        clean_data[1], "Hidden text from test_write_image_file_valid.")
    output_file = _write_image_file(dirty_file, clean_file, dirty_data)

    try:
        Image.open(output_file)
    except OSError:
        pytest.fail("Image is corrupt " + output_file)

    os.remove(output_file)
Exemplo n.º 11
0
def test_write_bin_file_size_same():
    """The file written is the same size as the one read, after hiding a message."""
    clean_file = CLEAN_PNG_LOCATION
    dirty_file = "tests/dirtyImage_test_write_bin_file_size_same.png"

    stegs = Steganographer()
    data = stegs._hide_string(
        _open_bin_file(clean_file),
        "Hidden text from test_write_bin_file_size_same.")
    _write_bin_file(dirty_file, data)

    # Getting the file sizes for the clean and dirty files.
    with open(clean_file, 'rb') as clean:
        clean.seek(0, 2)
        clean_file_size = clean.tell()

    with open(dirty_file, 'rb') as dirty:
        dirty.seek(0, 2)
        dirty_file_size = dirty.tell()

    assert clean_file_size == dirty_file_size

    os.remove(dirty_file)