예제 #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
 def test_key_stays_int(self):
     k = ScalarEncryptionKey(1)
     assert k.get() == 1
예제 #9
0
 def test_key_is_converted_to_int(self):
     k = ScalarEncryptionKey('1')
     assert k.get() == 49
예제 #10
0
 def test_xor_encodes_printable_properly(self):
     xor = Xor(ScalarEncryptionKey(3))
     result = xor.encode_char('a')
     assert result == 'b'
예제 #11
0
 def test_cesar_encodes_printables_properly_with_negative_multiple_rollover(
         self):
     cesar = Cesar(ScalarEncryptionKey(-2 * ascii_codes_table_size - 7))
     result = cesar.encode_char('&')
     assert result == '~'
예제 #12
0
 def test_cesar_encodes_printables_properly_with_positive_rollover(self):
     cesar = Cesar(ScalarEncryptionKey(7))
     result = cesar.encode_char('z')
     assert result == '"'
예제 #13
0
 def test_cesar_encodes_printables_properly_with_negative_key(self):
     cesar = Cesar(ScalarEncryptionKey(-3))
     result = cesar.encode_char('a')
     assert result == '^'
예제 #14
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())