def test_invalid_key_decrypt(self): """Test that decrypt_str fails when using the wrong key""" eh = EncryptHelper(EncryptHelper.generate_key()) enc = eh.encrypt_str(self.txt) # Make sure we can actually decrypt the data when using the original key self.assertEqual(eh.decrypt_str(enc), self.txt) # Now with a different key, decrypt_str should throw EncryptionError eh2 = EncryptHelper(EncryptHelper.generate_key()) with self.assertRaises(EncryptionError): eh2.decrypt_str(enc)
def test_generate_key_enc_dec(self): """Test :py:meth:`.EncryptHelper.generate_key` key works for encryption and decryption""" key = EncryptHelper.generate_key() self.assertIs(type(key), str) eh = EncryptHelper(key) enc = eh.encrypt_str(self.txt) self.assertIs(type(enc), str) self.assertNotEqual(enc, self.txt) dec = eh.decrypt_str(enc) self.assertEqual(dec, self.txt)
def test_is_encrypted(self): """Test that is_encrypted returns True for encrypted data, and False for non-encrypted""" eh = EncryptHelper(EncryptHelper.generate_key()) enc = eh.encrypt_str(self.txt) self.assertTrue(eh.is_encrypted(enc)) self.assertFalse(eh.is_encrypted(self.txt))
def test_password_key_gensalt(self): """Test that we can reproduce the same key from password_key's auto-generated salt""" key1, kd1 = EncryptHelper.password_key('Example') key2, kd2 = EncryptHelper.password_key('Example', kd1['salt']) self.assertEqual(key1, key2)
def test_password_key_equal(self): """Test that password_key returns the same key when ran with the same arguments""" key1, kd1 = EncryptHelper.password_key('ExamplePass', 'hello') key2, kd2 = EncryptHelper.password_key('ExamplePass', 'hello') self.assertEqual(key1, key2)
def test_password_key_diffpass(self): """Test that password_key returns two different keys for two passwords with the same salt""" key1, kd1 = EncryptHelper.password_key('ExamplePass', 'world') key2, kd2 = EncryptHelper.password_key('OtherPass', 'world') self.assertNotEqual(key1, key2)
def test_password_key_diffsalt(self): """Test that password_key returns two different keys for passwords with different salts""" key1, kd1 = EncryptHelper.password_key('ExamplePass', 'hello') key2, kd2 = EncryptHelper.password_key('ExamplePass', 'world') self.assertNotEqual(key1, key2)
def test_corrupt_key_encrypt(self): """Test that encrypt_str fails when using a corrupted key""" eh = EncryptHelper('ThisIsNotAKey') with self.assertRaises(EncryptKeyMissing): eh.encrypt_str(self.txt)