Пример #1
0
    def test_file_is_cesar_encoded_to_string(self, file_read_mock):

        string_writer = StringWriter()

        encoder = Encoder(FileReader('path'), string_writer,
                          Cesar(ScalarEncryptionKey(2)))
        encoder.encode()
        result_string = string_writer.get()

        assert result_string == 'vguv"og'
Пример #2
0
    def test_string_is_xor_encoded_to_string(self):

        string_writer = StringWriter()

        encoder = Encoder(StringReader('test me'), string_writer,
                          Xor(ScalarEncryptionKey(3)))
        encoder.encode()
        result_string = string_writer.get()

        assert result_string == 'wfpw#nf'
Пример #3
0
    def test_string_is_cesar_encoded_to_string(self):

        string_writer = StringWriter()

        encoder = Encoder(StringReader('test me'), string_writer,
                          Cesar(ScalarEncryptionKey(2)))
        encoder.encode()
        result_string = string_writer.get()

        assert result_string == 'vguv"og'
Пример #4
0
    def test_not_printable_char_is_not_cesar_encoded(self):

        null = chr(0x01)
        string_writer = StringWriter()

        encoder = Encoder(StringReader('a{}c'.format(null)), string_writer,
                          Cesar(ScalarEncryptionKey(2)))
        encoder.encode()
        result_string = string_writer.get()

        assert result_string == 'c{}e'.format(null)
Пример #5
0
    def test_encoding_is_stopped_on_stop_predicate(self):

        string_reader = StringReader('aaaaabccccc')
        strint_writer = StringWriter()
        coder = Cesar(ScalarEncryptionKey(1))
        is_end_of_encoding = lambda x: x == 'b'

        encoder = Encoder(string_reader, strint_writer, coder)
        encoder.encode(is_end_of_encoding)
        result_string = strint_writer.get()

        assert result_string == 'bbbbbc'
Пример #6
0
    def test_string_is_cesar_encoded_to_file(self, file_read_mock):

        file_writer = FileWriter('path')

        encoder = Encoder(StringReader('test'), file_writer,
                          Cesar(ScalarEncryptionKey(2)))
        encoder.encode()
        file_writer.finish()

        calls = [call('v'), call('g'), call('u'), call('v')]

        self.open_mock.return_value.write.assert_has_calls(calls)
        self.open_mock.return_value.close.assert_called_once()
Пример #7
0
    def test_header_is_not_encoded(self):

        string_reader = StringReader('some header \n test me')
        string_writer = StringWriter()
        coder = Xor(ScalarEncryptionKey(3))
        is_end_of_header = lambda x: x == '\n'

        header_rewriter = NullCoder(string_reader, string_writer)
        body_encoder = Encoder(string_reader, string_writer, coder)

        encoder = HeadedEncoder(header_rewriter, body_encoder,
                                is_end_of_header)
        encoder.encode()
        result_string = string_writer.get()

        assert result_string == 'some header \n#wfpw#nf'
Пример #8
0
"""Examples"""

from text_encoder import Encoder
from text_encoder import StringReader, FileWriter, ConsoleWriter, StringWriter
from text_encoder import Cesar, ScalarEncryptionKey

# Typical usage

encoder = Encoder(StringReader('test me'), ConsoleWriter(),
                  Cesar(ScalarEncryptionKey(2)))
encoder.encode()

# File as an output

file_writer = FileWriter(r'C:\Documents\encoding_output.txt')

encoder = Encoder(StringReader('test me'), file_writer,
                  Cesar(ScalarEncryptionKey(2)))
encoder.encode()
file_writer.finish()

# String as an output

string_writer = StringWriter()

encoder = Encoder(StringReader('test me'), string_writer,
                  Cesar(ScalarEncryptionKey(2)))
encoder.encode()
print(string_writer.get())