def test_get_cipher(self, aes_new_mock): cipher = aes_cipher.AESCipher(key="wruff wruff meow", iv="meow wruff wruff") cipher.mode = 'CFB' self.assertEqual(cipher._get_cipher(), aes_new_mock.return_value) aes_new_mock.assert_called_with("wruff wruff meow", AES.MODE_CFB, "meow wruff wruff") cipher = aes_cipher.AESCipher(key="wruff wruff meow", iv="meow wruff wruff") cipher.mode = 'ECB' self.assertEqual(cipher._get_cipher(), aes_new_mock.return_value) aes_new_mock.assert_called_with("wruff wruff meow", AES.MODE_ECB)
def test_generate_key(self): cipher = aes_cipher.AESCipher() self.assertEqual(len(cipher.generate_key()), 16) # test default. self.assertEqual(len(cipher.generate_key(16)), 16) self.assertEqual(len(cipher.generate_key(24)), 24) self.assertEqual(len(cipher.generate_key(32)), 32) self.assertRaises(AttributeError, cipher.generate_key, 48)
def test_decrypt(self, get_cipher_mock, unpad_mock, decode_mock): aes_cipher_mock = get_cipher_mock.return_value cipher = aes_cipher.AESCipher() self.assertEqual(cipher.decrypt("wruff"), unpad_mock.return_value) get_cipher_mock.assert_called_with() decode_mock.assert_called_with("wruff") aes_cipher_mock.decrypt.assert_called_with(decode_mock.return_value) unpad_mock.assert_called_with(aes_cipher_mock.decrypt.return_value)
def test_encrypt(self, get_cipher_mock, pad_mock, encode_mock): aes_cipher_mock = get_cipher_mock.return_value cipher = aes_cipher.AESCipher() self.assertEqual(cipher.encrypt("meow"), encode_mock.return_value) get_cipher_mock.assert_called_with() pad_mock.assert_called_with("meow", AES.block_size) aes_cipher_mock.encrypt.assert_called_with(pad_mock.return_value) encode_mock.assert_called_with(aes_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 = aes_cipher.AESCipher() cipher.mode = mode cipher.iv = cipher.generate_iv() random_device = random.Random.new() # Test various key sizes. for key_size in (16, 24, 32): 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 = aes_cipher.AESCipher() self._test_set_encoding(cipher)
def test_generate_iv(self): cipher = aes_cipher.AESCipher() self.assertEqual(len(cipher.generate_iv()), AES.block_size)
def test_mode_property(self): cipher = aes_cipher.AESCipher() # Expects Cipher FeedBack. self.assertEqual(cipher._mode.mode_id, AES.MODE_ECB) cipher.mode = 'CTR' self.assertEqual(cipher._mode.mode_id, AES.MODE_CTR)
def test_iv_property(self): cipher = aes_cipher.AESCipher(iv="meow wruff wruff", mode='CBC') self._test_init_iv(cipher, "meow wruff wruff") self._test_iv_setter(cipher, "wruff wruff meow", invalid_iv="wruff")
def test_key_property(self): cipher = aes_cipher.AESCipher(key="wruff wruff meow") self._test_init_key(cipher, "wruff wruff meow") self._test_key_setter(cipher, "meow wruff wruff", invalid_key="wruff")
def test_init(self): cipher = aes_cipher.AESCipher() self._test_init_no_args(cipher)