def test_get_cipher(self, CASTCipher_new_mock): cipher = cast_cipher.CASTCipher(key="wruff wruff meow", iv="wruff!!!") cipher.mode = 'CFB' self.assertEqual(cipher._get_cipher(), CASTCipher_new_mock.return_value) CASTCipher_new_mock.assert_called_with("wruff wruff meow", CAST.MODE_CFB, "wruff!!!") cipher = cast_cipher.CASTCipher(key="wruff wruff meow", iv="wruff!!!") cipher.mode = 'ECB' self.assertEqual(cipher._get_cipher(), CASTCipher_new_mock.return_value) CASTCipher_new_mock.assert_called_with("wruff wruff meow", CAST.MODE_ECB)
def test_generate_key(self): cipher = cast_cipher.CASTCipher() self.assertEqual(len(cipher.generate_key()), 16) # test default. self.assertEqual(len(cipher.generate_key(5)), 5) self.assertEqual(len(cipher.generate_key(8)), 8) self.assertEqual(len(cipher.generate_key(12)), 12) self.assertRaises(AttributeError, cipher.generate_key, 32)
def test_decrypt(self, get_cipher_mock, unpad_mock, decode_mock): cast_cipher_mock = get_cipher_mock.return_value cipher = cast_cipher.CASTCipher() self.assertEqual(cipher.decrypt("wruff"), unpad_mock.return_value) get_cipher_mock.assert_called_with() decode_mock.assert_called_with("wruff") cast_cipher_mock.decrypt.assert_called_with(decode_mock.return_value) unpad_mock.assert_called_with(cast_cipher_mock.decrypt.return_value)
def test_encrypt(self, get_cipher_mock, pad_mock, encode_mock): cast_cipher_mock = get_cipher_mock.return_value cipher = cast_cipher.CASTCipher() self.assertEqual(cipher.encrypt("meow"), encode_mock.return_value) get_cipher_mock.assert_called_with() pad_mock.assert_called_with("meow", CAST.block_size) cast_cipher_mock.encrypt.assert_called_with(pad_mock.return_value) encode_mock.assert_called_with(cast_cipher_mock.encrypt.return_value)
def _test_encryption(self, mode): """This will actually execute encrypting/decrypting data. Key and IV generation is derived entirely from class staticmethods. """ cipher = cast_cipher.CASTCipher() cipher.mode = mode cipher.iv = cipher.generate_iv() random_device = random.Random.new() # Test various key sizes. for key_size in (5, 12, 16): cipher.key = cipher.generate_key(key_size) util.test_cipher_encryption(self, cipher, random_device.read(2000)) # Test encoders. for encoder in (base_encoders.NullEncoder, binary_encoders.Base64Encoder, binary_encoders.URLSafeBase64Encoder): cipher.set_encoding(encoder) util.test_cipher_encryption(self, cipher, random_device.read(2000))
def test_set_encoding(self): cipher = cast_cipher.CASTCipher() self._test_set_encoding(cipher)
def test_generate_iv(self): cipher = cast_cipher.CASTCipher() self.assertEqual(len(cipher.generate_iv()), CAST.block_size)
def test_mode_property(self): cipher = cast_cipher.CASTCipher() # Expects Cipher FeedBack. self.assertEqual(cipher._mode.mode_id, CAST.MODE_ECB) cipher.mode = 'CTR' self.assertEqual(cipher._mode.mode_id, CAST.MODE_CTR)
def test_iv_property(self): cipher = cast_cipher.CASTCipher(iv="wruff!!!", mode='CBC') self._test_init_iv(cipher, "wruff!!!") self._test_iv_setter(cipher, "wruff!!!", invalid_iv="meow")
def test_key_property(self): cipher = cast_cipher.CASTCipher(key="wruff wruff meow") self._test_init_key(cipher, "wruff wruff meow") self._test_key_setter(cipher, "meow wruff wruff", invalid_key="meow" * 50)
def test_init(self): cipher = cast_cipher.CASTCipher() self._test_init_no_args(cipher)