aboutsummaryrefslogtreecommitdiff
path: root/python/tests/account_test.py
diff options
context:
space:
mode:
authorDamir Jelić <poljar@termina.org.uk>2018-07-08 12:19:16 +0200
committerHubert Chathi <hubert@uhoreg.ca>2018-07-18 17:44:32 -0400
commite3d66733712e161d9287ea3f0116e5b57477b0d8 (patch)
tree846678302027f772f5d8e0ef30d4e0ac79a526f9 /python/tests/account_test.py
parent2fccf44015dfb27865ddb50ed66afdedbd4e03e6 (diff)
python: Import improved python bindings.
This commit imports the python bindings from: https://github.com/poljar/python-olm The bindings are imported at commit c44b145818520d69eaaa350fb95afcb846125e0f Minor modifications were made while importing: - Removed travis config - Removed Arch Linux PKGBUILD - Removed the html docs, they can be rebuild by running make html in the docs folder - Slightly modified the README The new bindings feature some improvements over the old ones: - Python 2 and 3 support - Automatic memory management - Automatic memory clearing before it is freed - Type signatures via the python typing module - Full test coverage - Properties are utilized where it makes sense (e.g. account.id) Signed-off-by: Damir Jelić <poljar@termina.org.uk>
Diffstat (limited to 'python/tests/account_test.py')
-rw-r--r--python/tests/account_test.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/python/tests/account_test.py b/python/tests/account_test.py
new file mode 100644
index 0000000..4fef72c
--- /dev/null
+++ b/python/tests/account_test.py
@@ -0,0 +1,100 @@
+from builtins import int
+
+import pytest
+from hypothesis import given
+from hypothesis.strategies import text
+
+from olm import Account, OlmAccountError, OlmVerifyError, ed25519_verify
+from olm._compat import to_bytes
+
+
+class TestClass(object):
+ def test_to_bytes(self):
+ assert isinstance(to_bytes("a"), bytes)
+ assert isinstance(to_bytes(u"a"), bytes)
+ assert isinstance(to_bytes(b"a"), bytes)
+ assert isinstance(to_bytes(r"a"), bytes)
+ with pytest.raises(TypeError):
+ to_bytes(0)
+
+ def test_account_creation(self):
+ alice = Account()
+ assert alice.identity_keys
+ assert len(alice.identity_keys) == 2
+
+ def test_account_pickle(self):
+ alice = Account()
+ pickle = alice.pickle()
+ assert (alice.identity_keys == Account.from_pickle(pickle)
+ .identity_keys)
+
+ def test_invalid_unpickle(self):
+ with pytest.raises(ValueError):
+ Account.from_pickle(b"")
+
+ def test_passphrase_pickle(self):
+ alice = Account()
+ passphrase = "It's a secret to everybody"
+ pickle = alice.pickle(passphrase)
+ assert (alice.identity_keys == Account.from_pickle(
+ pickle, passphrase).identity_keys)
+
+ def test_wrong_passphrase_pickle(self):
+ alice = Account()
+ passphrase = "It's a secret to everybody"
+ pickle = alice.pickle(passphrase)
+
+ with pytest.raises(OlmAccountError):
+ Account.from_pickle(pickle, "")
+
+ def test_one_time_keys(self):
+ alice = Account()
+ alice.generate_one_time_keys(10)
+ one_time_keys = alice.one_time_keys
+ assert one_time_keys
+ assert len(one_time_keys["curve25519"]) == 10
+
+ def test_max_one_time_keys(self):
+ alice = Account()
+ assert isinstance(alice.max_one_time_keys, int)
+
+ def test_publish_one_time_keys(self):
+ alice = Account()
+ alice.generate_one_time_keys(10)
+ one_time_keys = alice.one_time_keys
+
+ assert one_time_keys
+ assert len(one_time_keys["curve25519"]) == 10
+
+ alice.mark_keys_as_published()
+ assert not alice.one_time_keys["curve25519"]
+
+ def test_clear(self):
+ alice = Account()
+ del alice
+
+ @given(text())
+ def test_valid_signature(self, message):
+ alice = Account()
+
+ signature = alice.sign(message)
+ signing_key = alice.identity_keys["ed25519"]
+
+ assert signature
+ assert signing_key
+
+ ed25519_verify(signing_key, message, signature)
+
+ @given(text())
+ def test_invalid_signature(self, message):
+ alice = Account()
+ bob = Account()
+
+ signature = alice.sign(message)
+ signing_key = bob.identity_keys["ed25519"]
+
+ assert signature
+ assert signing_key
+
+ with pytest.raises(OlmVerifyError):
+ ed25519_verify(signing_key, message, signature)