aboutsummaryrefslogtreecommitdiff
path: root/python/tests/pk_test.py
diff options
context:
space:
mode:
authorDamir Jelić <poljar@termina.org.uk>2018-11-28 14:54:09 +0100
committerHubert Chathi <hubert@uhoreg.ca>2019-04-08 15:04:32 -0400
commitf160d693b627a1159ed5837a4e9630100ab3f67e (patch)
tree140bd42754fa01744957d8f3da05fcd3f66a02ac /python/tests/pk_test.py
parent0883a922ff39981e27cff7e677a6f79a5f324fab (diff)
python: Add PK bindings.
This patch adds bindings to the PK part of the Olm library contained in the pk.h header file. Encryption, decryption as well as pickling/unpickling of the decryption object is supported. Signed-off-by: Damir Jelić <poljar@termina.org.uk>
Diffstat (limited to 'python/tests/pk_test.py')
-rw-r--r--python/tests/pk_test.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/python/tests/pk_test.py b/python/tests/pk_test.py
new file mode 100644
index 0000000..f2aa147
--- /dev/null
+++ b/python/tests/pk_test.py
@@ -0,0 +1,49 @@
+import pytest
+
+from olm import PkDecryption, PkDecryptionError, PkEncryption
+
+
+class TestClass(object):
+ def test_invalid_encryption(self):
+ with pytest.raises(ValueError):
+ PkEncryption("")
+
+ def test_decrytion(self):
+ decryption = PkDecryption()
+ encryption = PkEncryption(decryption.public_key)
+ plaintext = "It's a secret to everybody."
+ message = encryption.encrypt(plaintext)
+ decrypted_plaintext = decryption.decrypt(message)
+ isinstance(decrypted_plaintext, str)
+ assert plaintext == decrypted_plaintext
+
+ def test_invalid_decrytion(self):
+ decryption = PkDecryption()
+ encryption = PkEncryption(decryption.public_key)
+ plaintext = "It's a secret to everybody."
+ message = encryption.encrypt(plaintext)
+ message.ephemeral_key = "?"
+ with pytest.raises(PkDecryptionError):
+ decryption.decrypt(message)
+
+ def test_pickling(self):
+ decryption = PkDecryption()
+ encryption = PkEncryption(decryption.public_key)
+ plaintext = "It's a secret to everybody."
+ message = encryption.encrypt(plaintext)
+
+ pickle = decryption.pickle()
+ unpickled = PkDecryption.from_pickle(pickle)
+ decrypted_plaintext = unpickled.decrypt(message)
+ assert plaintext == decrypted_plaintext
+
+ def test_invalid_unpickling(self):
+ with pytest.raises(ValueError):
+ PkDecryption.from_pickle("")
+
+ def test_invalid_pass_pickling(self):
+ decryption = PkDecryption()
+ pickle = decryption.pickle("Secret")
+
+ with pytest.raises(PkDecryptionError):
+ PkDecryption.from_pickle(pickle, "Not secret")