def test_03_add_special(self): block = Block() block.add_special("!") self.assertEqual(block._index, 1) self.assertEqual(block._specials[0].char, "!") self.assertEqual(block._specials[0].index, 0) self.assertEqual(len(block._chars), 0)
def test_07_has_content(self): block = Block() self.assertFalse(block.has_content) block = Block() block.add_char("a") self.assertTrue(block.has_content) block = Block() block.add_special(".") self.assertTrue(block.has_content)
def test_02_add_char(self): block = Block() block.add_char("a") self.assertEqual(block._index, 1) self.assertEqual(block._chars[0].char, "a") self.assertEqual(block._chars[0].index, 0) self.assertEqual(len(block._specials), 0) block.add_char("b") # more than 2 chars should raise ValueError with self.assertRaises(ValueError): block.add_char("c")
def test_04_add_both(self): block = Block() block.add_char("a") block.add_special("!") block.add_char("b") self.assertEqual(block._chars[0].char, "a") self.assertEqual(block._chars[0].index, 0) self.assertEqual(block._specials[0].char, "!") self.assertEqual(block._specials[0].index, 1) self.assertEqual(block._chars[1].char, "b") self.assertEqual(block._chars[1].index, 2) self.assertEqual(block._index, 3)
def test_06_ready(self): block = Block() self.assertFalse(block.ready) block.add_char("a") self.assertFalse(block.ready) block.add_char("b") self.assertTrue(block.ready) with self.assertRaises(ValueError): block.add_char("c") # even with an insert exception, block that is ready stays ready self.assertTrue(block.ready)
def test_05_current_char(self): block = Block() self.assertEqual(block.current_char, None) block.add_char("a") self.assertEqual(block.current_char, "a") block.add_char("b") self.assertEqual(block.current_char, None)
def _iterator(self): block = Block() valid_chars = re.compile(r"^[a-z]$") for char in self._cipher_text: if not valid_chars.match(char): block.add_special(char) continue block.add_char(char) if block.ready: yield block block = Block() # if block ends with a single character, add padding char and yield it if block.current_char: raise ValueError("Invalid cipher_text, couldn't decrypt block!") # if block has no chars but has special chars, yield it elif block.has_content: yield block
def test_02_col(self): block = Block() block.add_char("s") block.add_char("x") # test increment and roll over Rule.col(self.playfair_key, block, Rule.ENCRYPT) self.assertEqual(block.char(0), "x") self.assertEqual(block.char(1), "c") # test increment and roll over Rule.col(self.playfair_key, block, Rule.DECRYPT) self.assertEqual(block.char(0), "s") self.assertEqual(block.char(1), "x")
def test_01_row(self): block = Block() block.add_char("d") block.add_char("e") # test increment and roll over Rule.row(self.playfair_key, block, Rule.ENCRYPT) self.assertEqual(block.char(0), "e") self.assertEqual(block.char(1), "a") # test increment and roll over Rule.row(self.playfair_key, block, Rule.DECRYPT) self.assertEqual(block.char(0), "d") self.assertEqual(block.char(1), "e")
def _iterator(self): block = Block() valid_chars = re.compile(r"^[a-z]$") for char in self._plain_text: if not valid_chars.match(char): block.add_special(char) continue if char == self._playfair_key.substitute_char: char = self._playfair_key.substitute_by if char == block.current_char: block.add_char(self._playfair_key.padding_char) yield block block = Block() block.add_char(char) if block.ready: yield block block = Block() # if block ends with a single character, add padding char and yield it if block.current_char: block.add_char(self._playfair_key.padding_char) yield block # if block has no chars but has special chars, yield it elif block.has_content: yield block
def test_03_rect(self): block = Block() block.add_char("i") block.add_char("r") # test rect function Rule.rect(self.playfair_key, block, Rule.ENCRYPT) self.assertEqual(block.char(0), "g") self.assertEqual(block.char(1), "t") # test rect function Rule.rect(self.playfair_key, block, Rule.DECRYPT) self.assertEqual(block.char(0), "i") self.assertEqual(block.char(1), "r") block = Block() block.add_char("w") block.add_char("e") # test rect function Rule.rect(self.playfair_key, block, Rule.ENCRYPT) self.assertEqual(block.char(0), "z") self.assertEqual(block.char(1), "b") # test rect function Rule.rect(self.playfair_key, block, Rule.DECRYPT) self.assertEqual(block.char(0), "w") self.assertEqual(block.char(1), "e")
def test_03_construct_cipher_text(self): playfair_encrypt = PlayFairEncrypt(self._playfair_key) playfair_encrypt._blocks = [] block = Block() block.add_char("a") block.add_char("b") playfair_encrypt._blocks.append(block) block = Block() block.add_special(",") block.add_char("c") block.add_special(".") block.add_char("d") block.add_special("1") playfair_encrypt._blocks.append(block) self.assertEqual(playfair_encrypt._construct_cipher_text(), "ab,c.d1")
def test_03_construct_cipher_text(self): playfair_decrypt = PlayFairDecrypt(self._playfair_key) playfair_decrypt._blocks = [] # add plain block block = Block() block.add_char("a") block.add_char("b") playfair_decrypt._blocks.append(block) # add block with specials block = Block() block.add_special(",") block.add_char("c") block.add_special(".") block.add_char("d") block.add_special("1") playfair_decrypt._blocks.append(block) # add blocks with padding chars (foo) block = Block() block.add_char("f") block.add_char("o") playfair_decrypt._blocks.append(block) block = Block() block.add_char("x") block.add_char("o") playfair_decrypt._blocks.append(block) # add block with substitute_by block = Block() block.add_char("i") block.add_char("a") playfair_decrypt._blocks.append(block) # add block ending with padding_char block = Block() block.add_char("o") block.add_char("x") playfair_decrypt._blocks.append(block) plain_text = "ab,c.d1foojao" self.assertEqual(playfair_decrypt._construct_plain_text(), plain_text)
def test_08_char(self): block = Block() # block is not yet ready, raises exception with self.assertRaises(ValueError): block.char(0) block.add_char("a") block.add_char("b") # requesting invalid locations with self.assertRaises(ValueError): block.char(-1) with self.assertRaises(ValueError): block.char(2) # finally a valid request after block is ready self.assertEqual(block.char(0), "a") self.assertEqual(block.char(1), "b")
def test_01_create_input_block(self): block = Block() self.assertIsNotNone(block) self.assertEqual(len(block._chars), 0) self.assertEqual(len(block._specials), 0) self.assertEqual(block._index, 0)
def test_09_text(self): # test 2 char 2 special block block = Block() block.add_char("a") block.add_special(",") block.add_char("b") block.add_special(".") self.assertEqual(block.text, "a,b.") # test 2 char block block = Block() block.add_char("a") block.add_char("b") self.assertEqual(block.text, "ab") # test 2 special block block = Block() block.add_special("!") block.add_special("@") self.assertEqual(block.text, "!@")