def test_exact_data_with_data_inverse():
    """The data entered is the data returned. The storing data is the exact length needed."""
    test_string = "This is a test String"
    test_data = bytes(test_string, 'utf-8')
    stegs = Steganographer()
    stegs._header.data_len = len(test_string)
    blank_data = bytes(b'\x01' * len(test_string) * stegs._BYTELEN)

    revealed_data = stegs._reveal_data(stegs._hide_data(blank_data, test_data))

    assert test_data == revealed_data
def test_hide_reveal_data_inverse(string_to_hide):
    """Anything hidden by _hide_data is revealed by _reveal_data."""
    clean_data = bytes(b'\x01' * 5000)
    data_to_hide = bytes(string_to_hide, 'utf-8')

    stegs = Steganographer()
    stegs._header.data_len = len(string_to_hide.encode('utf-8'))
    revealed_data = stegs._reveal_data(
        stegs._hide_data(clean_data, data_to_hide))

    assert revealed_data == data_to_hide
def test_short_data_with_data_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"
    test_data = bytes(test_string, 'utf-8')
    stegs = Steganographer()
    stegs._header.data_len = len(test_string)
    blank_data = bytes(b'\x01' *
                       (len(test_string) * stegs._BYTELEN - stegs._BYTELEN))

    revealed_data = stegs._reveal_data(stegs._hide_data(blank_data, test_data))

    assert test_data[:-1] == revealed_data
def test_retrieve_header():
    """The header is retrieved as expected"""
    stegs = Steganographer()
    test_message = "12345".encode('utf-8')
    test_data_len = len(test_message)
    test_bits_used = 1
    test_file_name = "test_retrieve_header.txt"
    test_file_name_len = len(test_file_name)
    test_data = bytes(b'\x01' * 1000)

    test_header = stegs._generate_header(test_data_len, test_bits_used,
                                         test_file_name)
    hidden_data = stegs._hide_data(
        test_data[:stegs._header.header_length * stegs._BYTELEN], test_header)
    hidden_data += stegs._hide_data(
        test_data[stegs._header.header_length * stegs._BYTELEN:], test_message)
    header_retrieved = stegs._retrieve_header(hidden_data)

    assert header_retrieved is True
    assert stegs._header.data_len == test_data_len
    assert stegs._header.bits_used == test_bits_used
    assert stegs._header.file_name_len == test_file_name_len
    assert stegs._header.file_name.decode('utf-8') == test_file_name
def test_short_partial_data_w_data_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"
    test_data = bytes(test_string, 'utf-8')
    solution_data = bytearray(test_data)
    stegs = Steganographer()
    stegs._header.data_len = len(test_string)
    solution_data[
        -1] = solution_data[-1] >> stegs._BYTELEN // 2 << stegs._BYTELEN // 2
    blank_data = bytes(
        b'\x01' * (len(test_string) * stegs._BYTELEN - stegs._BYTELEN // 2))

    revealed_data = stegs._reveal_data(stegs._hide_data(blank_data, test_data))

    assert solution_data == revealed_data
def test_hide_data_partial():
    """Will work when given a bytes object that is too short to contain the full data to be hidden."""
    stegs = Steganographer()
    test_data = bytes(b'\x01' * stegs._BYTELEN * 3)
    data_to_hide = bytes('ABC', 'utf-8')
    stegs._header.data_len = len(data_to_hide)
    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

    # Testing when only half a byte is passed in for the data that contains the hidden text.
    assert stegs._hide_data(test_data[:4], data_to_hide) == solution_data[:4]
def test_hide_data():
    """Will hide one bytes object inside another."""
    stegs = Steganographer()
    test_data = bytes(b'\x01' * stegs._BYTELEN * 4)
    data_to_hide = bytes('ABC', 'utf-8')
    stegs._header.data_len = len(data_to_hide)
    solution_data = bytearray(stegs._BYTELEN * 4)
    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
    solution_data[24:] = b'\x01' * stegs._BYTELEN

    assert stegs._hide_data(test_data, data_to_hide) == solution_data