def test_get_bit_pairs(self): # T -> 0b1010100 # e -> 0b1100101 # s -> 0b1110011 # t -> 0b1110100 # -> 0b100000 # t -> 0b1110100 # e -> 0b1100101 # x -> 0b1111000 # t -> 0b1110100 text = 'Test text' expected_output = ( '01', '01', '01', '00', '01', '10', '01', '01', '01', '11', '00', '11', '01', '11', '01', '00', '00', '10', '00', '00', '01', '11', '01', '00', '01', '10', '01', '01', '01', '11', '10', '00', '01', '11', '01', '00', ) sample_img = np.zeros((5, 5, 3), np.uint8) obj = Encrypter(sample_img, text) pairs = obj.get_bit_pairs(text) self.assertEqual(pairs, expected_output)
def test_set_bit_no_actual_change(self): initial = "10010100" # 148 in binary output = Encrypter.set_bit(int(initial, 2), 6, 0) self.assertEqual(bin(output)[2:], initial) initial = "10010100" # 148 in binary output = Encrypter.set_bit(int(initial, 2), 2, 1) self.assertEqual(bin(output)[2:], initial)
def test_code_pixel(self): sample_img = np.zeros((5, 5, 3), np.uint8) text = 'Test text' obj = Encrypter(sample_img, text) self.assertEqual(sample_img.item(0, 0, 0), 0) obj.code_pixel(0, 0, 0, '11') # '0b11' = 3 in decimal first_px = sample_img.item(0, 0, 0) self.assertEqual(first_px, 3)
def test_decode_message(self): msg = 'Test message' img = np.zeros((10, 10, 3), np.uint8) encrypter = Encrypter(img, msg) encrypter.encode() decrypter = Decrypter(encrypter.img) result = decrypter.decode_message() self.assertEqual(result, msg)
def test_encode(self): # Just one character because encoding requires 32 bits for # input end message text = 'x' # 01111000 - (1, 3, 2, 0) (bit pairs decimal repr) img = np.zeros((6, 6, 1), np.uint8) obj = Encrypter(img, text) obj.encode() self.assertEqual(img.item(0, 0, 0), 1) self.assertEqual(img[0][1], 3) self.assertEqual(img[0][2], 2) self.assertEqual(img[0][3], 0)
def test_convert_to_bytes_string(self): char = 'a' # 7 bits py_bin = bin(ord(char)) self.assertEqual(len(py_bin[2:]), 7) my_bin = Encrypter.convert_to_bytes_string(char) self.assertEqual(len(my_bin), 8) self.assertEqual(py_bin[2:], my_bin[1:])
def test_end_message_found(self): sample_img = np.zeros((5, 5, 3), np.uint8) decrypter = Decrypter(sample_img) msg = '0110001001010011' # just random self.assertFalse(decrypter.end_message_found(msg)) msg = 'test' + Decrypter.ENDMSG_CODE bin_msg = '' for x in msg: binary = Encrypter.convert_to_bytes_string(x) bin_msg += binary self.assertTrue(decrypter.end_message_found(bin_msg[-64:]))
def test_can_encrypt_method(self): # 32 pixels are required for end message code # 4 x 4 x 3 = 48; (48 - 32) / 4 = 4 -> 4 characters possible img_small = np.zeros((4, 4, 3), np.uint8) # 5 x 5 x 3 = 75; (75 - 32) / 4 ~= 10+ -> 10 characters possible img_ok = np.zeros((5, 5, 3), np.uint8) # 6x6 - minimum image size for encryption img_one_channel = np.zeros((6, 6, 1), np.uint8) text_small = "test" text = "10letters!" text_long = "Too long string" self.assertTrue(Encrypter.can_encrypt(img_small, text_small)) self.assertFalse(Encrypter.can_encrypt(img_small, text)) self.assertTrue(Encrypter.can_encrypt(img_ok, text)) self.assertFalse(Encrypter.can_encrypt(img_ok, text_long)) self.assertFalse(Encrypter.can_encrypt(img_one_channel, text)) self.assertTrue(Encrypter.can_encrypt(img_one_channel, "1"))
def test_set_bit_to_0(self): initial = "10010100" # 148 in binary index = 2 expected_value = "10010000" output = Encrypter.set_bit(int(initial, 2), index, 0) self.assertEqual(bin(output)[2:], expected_value)