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'
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'
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'
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)
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'
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()
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'
def test_key_stays_int(self): k = ScalarEncryptionKey(1) assert k.get() == 1
def test_key_is_converted_to_int(self): k = ScalarEncryptionKey('1') assert k.get() == 49
def test_xor_encodes_printable_properly(self): xor = Xor(ScalarEncryptionKey(3)) result = xor.encode_char('a') assert result == 'b'
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 == '~'
def test_cesar_encodes_printables_properly_with_positive_rollover(self): cesar = Cesar(ScalarEncryptionKey(7)) result = cesar.encode_char('z') assert result == '"'
def test_cesar_encodes_printables_properly_with_negative_key(self): cesar = Cesar(ScalarEncryptionKey(-3)) result = cesar.encode_char('a') assert result == '^'
"""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())