diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-11-05 01:45:06 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-11-05 01:45:06 +0100 |
commit | 2a8202e74846d191a321cca1202175af9db6107d (patch) | |
tree | a6f455caf07da1186851f343a237a4c4e4484f46 /python/tests | |
parent | 8efa0ec17d8c262f9c3fd7603e8074f74a053708 (diff) |
Diffstat (limited to 'python/tests')
-rw-r--r-- | python/tests/account_test.py | 114 | ||||
-rw-r--r-- | python/tests/group_session_test.py | 128 | ||||
-rw-r--r-- | python/tests/pk_test.py | 65 | ||||
-rw-r--r-- | python/tests/sas_test.py | 99 | ||||
-rw-r--r-- | python/tests/session_test.py | 152 | ||||
-rw-r--r-- | python/tests/utils_test.py | 25 |
6 files changed, 0 insertions, 583 deletions
diff --git a/python/tests/account_test.py b/python/tests/account_test.py deleted file mode 100644 index f3b71eb..0000000 --- a/python/tests/account_test.py +++ /dev/null @@ -1,114 +0,0 @@ -from builtins import int - -import pytest - -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 - - def test_valid_signature(self): - message = "It's a secret to everybody" - alice = Account() - - signature = alice.sign(message) - signing_key = alice.identity_keys["ed25519"] - - assert signature - assert signing_key - - ed25519_verify(signing_key, message, signature) - - def test_invalid_signature(self): - message = "It's a secret to everybody" - 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) - - def test_signature_verification_twice(self): - message = "It's a secret to everybody" - alice = Account() - - signature = alice.sign(message) - signing_key = alice.identity_keys["ed25519"] - - assert signature - assert signing_key - - ed25519_verify(signing_key, message, signature) - assert signature == alice.sign(message) - - ed25519_verify(signing_key, message, signature) - assert signature == alice.sign(message) diff --git a/python/tests/group_session_test.py b/python/tests/group_session_test.py deleted file mode 100644 index 4632a60..0000000 --- a/python/tests/group_session_test.py +++ /dev/null @@ -1,128 +0,0 @@ -# -*- coding: utf-8 -*- -import pytest - -from olm import InboundGroupSession, OlmGroupSessionError, OutboundGroupSession - - -class TestClass(object): - def test_session_create(self): - OutboundGroupSession() - - def test_session_id(self): - session = OutboundGroupSession() - assert isinstance(session.id, str) - - def test_session_index(self): - session = OutboundGroupSession() - assert isinstance(session.message_index, int) - assert session.message_index == 0 - - def test_outbound_pickle(self): - session = OutboundGroupSession() - pickle = session.pickle() - - assert (session.id == OutboundGroupSession.from_pickle( - pickle).id) - - def test_invalid_unpickle(self): - with pytest.raises(ValueError): - OutboundGroupSession.from_pickle(b"") - - with pytest.raises(ValueError): - InboundGroupSession.from_pickle(b"") - - def test_inbound_create(self): - outbound = OutboundGroupSession() - InboundGroupSession(outbound.session_key) - - def test_invalid_decrypt(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - - with pytest.raises(ValueError): - inbound.decrypt("") - - def test_inbound_pickle(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - pickle = inbound.pickle() - InboundGroupSession.from_pickle(pickle) - - def test_inbound_export(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - imported = InboundGroupSession.import_session( - inbound.export_session(inbound.first_known_index) - ) - assert "Test", 0 == imported.decrypt(outbound.encrypt("Test")) - - def test_first_index(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - index = inbound.first_known_index - assert isinstance(index, int) - - def test_encrypt(self, benchmark): - benchmark.weave(OutboundGroupSession.encrypt, lazy=True) - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - assert "Test", 0 == inbound.decrypt(outbound.encrypt("Test")) - - def test_decrypt(self, benchmark): - benchmark.weave(InboundGroupSession.decrypt, lazy=True) - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - assert "Test", 0 == inbound.decrypt(outbound.encrypt("Test")) - - def test_decrypt_twice(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - outbound.encrypt("Test 1") - message, index = inbound.decrypt(outbound.encrypt("Test 2")) - assert isinstance(index, int) - assert ("Test 2", 1) == (message, index) - - def test_decrypt_failure(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - eve_outbound = OutboundGroupSession() - with pytest.raises(OlmGroupSessionError): - inbound.decrypt(eve_outbound.encrypt("Test")) - - def test_id(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - assert outbound.id == inbound.id - - def test_inbound_fail(self): - with pytest.raises(TypeError): - InboundGroupSession() - - def test_oubtound_pickle_fail(self): - outbound = OutboundGroupSession() - pickle = outbound.pickle("Test") - - with pytest.raises(OlmGroupSessionError): - OutboundGroupSession.from_pickle(pickle) - - def test_outbound_clear(self): - session = OutboundGroupSession() - del session - - def test_inbound_clear(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - del inbound - - def test_invalid_unicode_decrypt(self): - outbound = OutboundGroupSession() - inbound = InboundGroupSession(outbound.session_key) - - text = outbound.encrypt(b"\xed") - plaintext, _ = inbound.decrypt(text) - - print(plaintext) - assert plaintext == u"�" - - plaintext, _ = inbound.decrypt(text, "ignore") - assert plaintext == "" diff --git a/python/tests/pk_test.py b/python/tests/pk_test.py deleted file mode 100644 index ef87465..0000000 --- a/python/tests/pk_test.py +++ /dev/null @@ -1,65 +0,0 @@ -# -*- coding: utf-8 -*- -import pytest - -from olm import (PkDecryption, PkDecryptionError, PkEncryption, PkSigning, - ed25519_verify) - - -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") - - def test_signing(self): - seed = PkSigning.generate_seed() - signing = PkSigning(seed) - message = "This statement is true" - signature = signing.sign(message) - ed25519_verify(signing.public_key, message, signature) - - def test_invalid_unicode_decrypt(self): - decryption = PkDecryption() - encryption = PkEncryption(decryption.public_key) - message = encryption.encrypt(b"\xed") - plaintext = decryption.decrypt(message) - assert plaintext == u"�" diff --git a/python/tests/sas_test.py b/python/tests/sas_test.py deleted file mode 100644 index 9001e67..0000000 --- a/python/tests/sas_test.py +++ /dev/null @@ -1,99 +0,0 @@ -from builtins import bytes - -import pytest - -from olm import OlmSasError, Sas - -MESSAGE = "Test message" -EXTRA_INFO = "extra_info" - - -class TestClass(object): - def test_sas_creation(self): - sas = Sas() - assert sas.pubkey - - def test_other_key_setting(self): - sas_alice = Sas() - sas_bob = Sas() - - assert not sas_alice.other_key_set - sas_alice.set_their_pubkey(sas_bob.pubkey) - assert sas_alice.other_key_set - - def test_bytes_generating(self): - sas_alice = Sas() - sas_bob = Sas(sas_alice.pubkey) - - assert sas_bob.other_key_set - - with pytest.raises(OlmSasError): - sas_alice.generate_bytes(EXTRA_INFO, 5) - - sas_alice.set_their_pubkey(sas_bob.pubkey) - - with pytest.raises(ValueError): - sas_alice.generate_bytes(EXTRA_INFO, 0) - - alice_bytes = sas_alice.generate_bytes(EXTRA_INFO, 5) - bob_bytes = sas_bob.generate_bytes(EXTRA_INFO, 5) - - assert alice_bytes == bob_bytes - - def test_mac_generating(self): - sas_alice = Sas() - sas_bob = Sas() - - with pytest.raises(OlmSasError): - sas_alice.calculate_mac(MESSAGE, EXTRA_INFO) - - sas_alice.set_their_pubkey(sas_bob.pubkey) - sas_bob.set_their_pubkey(sas_alice.pubkey) - - alice_mac = sas_alice.calculate_mac(MESSAGE, EXTRA_INFO) - bob_mac = sas_bob.calculate_mac(MESSAGE, EXTRA_INFO) - - assert alice_mac == bob_mac - - def test_cross_language_mac(self): - """Test MAC generating with a predefined key pair. - - This test imports a private and public key from the C test and checks - if we are getting the same MAC that the C code calculated. - """ - alice_private = [ - 0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D, - 0x3C, 0x16, 0xC1, 0x72, 0x51, 0xB2, 0x66, 0x45, - 0xDF, 0x4C, 0x2F, 0x87, 0xEB, 0xC0, 0x99, 0x2A, - 0xB1, 0x77, 0xFB, 0xA5, 0x1D, 0xB9, 0x2C, 0x2A - ] - - bob_key = "3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08" - message = "Hello world!" - extra_info = "MAC" - expected_mac = "2nSMTXM+TStTU3RUVTNSVVZUTlNWVlpVVGxOV1ZscFY" - - sas_alice = Sas() - sas_alice._create_sas(bytes(alice_private), 32) - sas_alice.set_their_pubkey(bob_key) - - alice_mac = sas_alice.calculate_mac(message, extra_info) - - assert alice_mac == expected_mac - - def test_long_mac_generating(self): - sas_alice = Sas() - sas_bob = Sas() - - with pytest.raises(OlmSasError): - sas_alice.calculate_mac_long_kdf(MESSAGE, EXTRA_INFO) - - sas_alice.set_their_pubkey(sas_bob.pubkey) - sas_bob.set_their_pubkey(sas_alice.pubkey) - - alice_mac = sas_alice.calculate_mac_long_kdf(MESSAGE, EXTRA_INFO) - bob_mac = sas_bob.calculate_mac_long_kdf(MESSAGE, EXTRA_INFO) - bob_short_mac = sas_bob.calculate_mac(MESSAGE, EXTRA_INFO) - - assert alice_mac == bob_mac - assert alice_mac != bob_short_mac diff --git a/python/tests/session_test.py b/python/tests/session_test.py deleted file mode 100644 index b856585..0000000 --- a/python/tests/session_test.py +++ /dev/null @@ -1,152 +0,0 @@ -# -*- coding: utf-8 -*- -import pytest - -from olm import (Account, InboundSession, OlmMessage, OlmPreKeyMessage, - OlmSessionError, OutboundSession, Session) - - -class TestClass(object): - def _create_session(self): - alice = Account() - bob = Account() - bob.generate_one_time_keys(1) - id_key = bob.identity_keys["curve25519"] - one_time = list(bob.one_time_keys["curve25519"].values())[0] - session = OutboundSession(alice, id_key, one_time) - return alice, bob, session - - def test_session_create(self): - _, _, session_1 = self._create_session() - _, _, session_2 = self._create_session() - assert session_1 - assert session_2 - assert session_1.id != session_2.id - assert isinstance(session_1.id, str) - - def test_session_clear(self): - _, _, session = self._create_session() - del session - - def test_invalid_session_create(self): - with pytest.raises(TypeError): - Session() - - def test_session_pickle(self): - alice, bob, session = self._create_session() - Session.from_pickle(session.pickle()).id == session.id - - def test_session_invalid_pickle(self): - with pytest.raises(ValueError): - Session.from_pickle(b"") - - def test_wrong_passphrase_pickle(self): - alice, bob, session = self._create_session() - passphrase = "It's a secret to everybody" - pickle = alice.pickle(passphrase) - - with pytest.raises(OlmSessionError): - Session.from_pickle(pickle, "") - - def test_encrypt(self): - plaintext = "It's a secret to everybody" - alice, bob, session = self._create_session() - message = session.encrypt(plaintext) - - assert (repr(message) - == "OlmPreKeyMessage({})".format(message.ciphertext)) - - assert (str(message) - == "PRE_KEY {}".format(message.ciphertext)) - - bob_session = InboundSession(bob, message) - assert plaintext == bob_session.decrypt(message) - - def test_empty_message(self): - with pytest.raises(ValueError): - OlmPreKeyMessage("") - empty = OlmPreKeyMessage("x") - empty.ciphertext = "" - alice, bob, session = self._create_session() - - with pytest.raises(ValueError): - session.decrypt(empty) - - def test_inbound_with_id(self): - plaintext = "It's a secret to everybody" - alice, bob, session = self._create_session() - message = session.encrypt(plaintext) - alice_id = alice.identity_keys["curve25519"] - bob_session = InboundSession(bob, message, alice_id) - assert plaintext == bob_session.decrypt(message) - - def test_two_messages(self): - plaintext = "It's a secret to everybody" - alice, bob, session = self._create_session() - message = session.encrypt(plaintext) - alice_id = alice.identity_keys["curve25519"] - bob_session = InboundSession(bob, message, alice_id) - bob.remove_one_time_keys(bob_session) - assert plaintext == bob_session.decrypt(message) - - bob_plaintext = "Grumble, Grumble" - bob_message = bob_session.encrypt(bob_plaintext) - - assert (repr(bob_message) - == "OlmMessage({})".format(bob_message.ciphertext)) - - assert bob_plaintext == session.decrypt(bob_message) - - def test_matches(self): - plaintext = "It's a secret to everybody" - alice, bob, session = self._create_session() - message = session.encrypt(plaintext) - alice_id = alice.identity_keys["curve25519"] - bob_session = InboundSession(bob, message, alice_id) - assert plaintext == bob_session.decrypt(message) - - message_2nd = session.encrypt("Hey! Listen!") - - assert bob_session.matches(message_2nd) is True - assert bob_session.matches(message_2nd, alice_id) is True - - def test_invalid(self): - alice, bob, session = self._create_session() - message = OlmMessage("x") - - with pytest.raises(TypeError): - session.matches(message) - - message = OlmPreKeyMessage("x") - message.ciphertext = "" - - with pytest.raises(ValueError): - session.matches(message) - - with pytest.raises(ValueError): - InboundSession(bob, message) - - with pytest.raises(ValueError): - OutboundSession(alice, "", "x") - - with pytest.raises(ValueError): - OutboundSession(alice, "x", "") - - def test_doesnt_match(self): - plaintext = "It's a secret to everybody" - alice, bob, session = self._create_session() - message = session.encrypt(plaintext) - alice_id = alice.identity_keys["curve25519"] - bob_session = InboundSession(bob, message, alice_id) - - _, _, new_session = self._create_session() - - new_message = new_session.encrypt(plaintext) - assert bob_session.matches(new_message) is False - - def test_invalid_unicode_decrypt(self): - alice, bob, session = self._create_session() - message = session.encrypt(b"\xed") - - bob_session = InboundSession(bob, message) - plaintext = bob_session.decrypt(message) - assert plaintext == u"�" diff --git a/python/tests/utils_test.py b/python/tests/utils_test.py deleted file mode 100644 index 86fb34f..0000000 --- a/python/tests/utils_test.py +++ /dev/null @@ -1,25 +0,0 @@ -import base64 -import hashlib - -from future.utils import bytes_to_native_str - -from olm import sha256 -from olm._compat import to_bytes - - -class TestClass(object): - def test_sha256(self): - input1 = "It's a secret to everybody" - input2 = "It's a secret to nobody" - - first_hash = sha256(input1) - second_hash = sha256(input2) - - hashlib_hash = base64.b64encode( - hashlib.sha256(to_bytes(input1)).digest() - ) - - hashlib_hash = bytes_to_native_str(hashlib_hash[:-1]) - - assert first_hash != second_hash - assert hashlib_hash == first_hash |