added tests for encryptor.py

This commit is contained in:
Abubakar Abid 2022-01-23 22:47:18 -06:00
parent c1c71663a3
commit b16cd78f40
2 changed files with 34 additions and 0 deletions

View File

@ -19,6 +19,7 @@ def encrypt(
IV = Random.new().read(AES.block_size) # generate IV
encryptor = AES.new(key, AES.MODE_CBC, IV)
padding = AES.block_size - len(source) % AES.block_size # calculate needed padding
print(type(source), type(padding))
source += bytes([padding]) * padding # Python 2.x: source += chr(padding) * padding
data = IV + encryptor.encrypt(source) # store the IV at the beginning and encrypt
return data

33
test/test_encryptor.py Normal file
View File

@ -0,0 +1,33 @@
import os
import unittest
from gradio import encryptor, processing_utils
from gradio.test_data import BASE64_IMAGE
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
class TestKeyGenerator(unittest.TestCase):
def test_same_pass(self):
key1 = encryptor.get_key("test")
key2 = encryptor.get_key("test")
self.assertEquals(key1, key2)
def test_diff_pass(self):
key1 = encryptor.get_key("test")
key2 = encryptor.get_key("diff_test")
self.assertNotEquals(key1, key2)
class TestEncryptorDecryptor(unittest.TestCase):
def test_same_pass(self):
key = encryptor.get_key("test")
data, _ = processing_utils.decode_base64_to_binary(BASE64_IMAGE)
encrypted_data = encryptor.encrypt(key, data)
decrypted_data = encryptor.decrypt(key, encrypted_data)
self.assertEquals(data, decrypted_data)
if __name__ == "__main__":
unittest.main()