Example #1
0
    def test_key(self):
        user_id = faker.mx_id()
        device_id = faker.device_id()
        fp_key = faker.olm_key_pair()["ed25519"]
        key = Ed25519Key(user_id, device_id, fp_key)

        assert (key.to_line() == "{} {} matrix-ed25519 {}\n".format(
            user_id, device_id, fp_key))

        loaded_key = Key.from_line(key.to_line())
        assert isinstance(loaded_key, Ed25519Key)

        assert key.user_id == loaded_key.user_id
        assert key.device_id == loaded_key.device_id
        assert key.key == loaded_key.key
        assert key == loaded_key
Example #2
0
 def new_user(self):
     return faker.mx_id(), faker.name(), faker.avatar_url()
Example #3
0
import pytest

from nio.crypto import Sas, SasState, OlmDevice
from nio.exceptions import LocalProtocolError
from nio.events import (KeyVerificationStart, KeyVerificationAccept,
                        KeyVerificationKey, KeyVerificationMac)
from helpers import faker

alice_id = faker.mx_id()
alice_device_id = faker.device_id()
alice_keys = faker.olm_key_pair()

bob_id = faker.mx_id()
bob_device_id = faker.device_id()
bob_keys = faker.olm_key_pair()

alice_device = OlmDevice(alice_id, alice_device_id, alice_keys["ed25519"],
                         alice_keys["curve25519"])

bob_device = OlmDevice(bob_id, bob_device_id, bob_keys["ed25519"],
                       bob_keys["curve25519"])


class TestClass(object):
    def test_sas_creation(self):
        alice = Sas(alice_id, alice_device_id, alice_keys["ed25519"],
                    bob_device)

        with pytest.raises(LocalProtocolError):
            alice.accept_verification()
Example #4
0
 def new_user(self):
     return faker.mx_id(), faker.name()
Example #5
0
    def test_sas_not_ok_events(self):
        alice = Sas(
            alice_id,
            alice_device_id,
            alice_keys["ed25519"],
            bob_device,
        )
        start = {
            "sender": alice_id,
            "content": alice.start_verification().content
        }
        start_event = KeyVerificationStart.from_dict(start)

        bob = Sas.from_key_verification_start(bob_id, bob_device_id,
                                              bob_keys["ed25519"],
                                              alice_device, start_event)
        accept = {
            "sender": bob_id,
            "content": bob.accept_verification().content
        }
        accept_event = KeyVerificationAccept.from_dict(accept)
        accept_event.sender = faker.mx_id()
        alice.receive_accept_event(accept_event)
        assert alice.canceled

        alice.state = SasState.created
        accept_event.sender = bob_id
        accept_event.transaction_id = "fake_id"
        alice.receive_accept_event(accept_event)
        assert alice.canceled

        accept_event.transaction_id = alice.transaction_id
        alice.receive_accept_event(accept_event)
        assert alice.canceled

        alice.state = SasState.created
        accept_event.hash = "fake_hash"
        alice.receive_accept_event(accept_event)
        assert alice.canceled

        alice.state = SasState.created
        accept_event.hash = Sas._hash_v1
        alice.receive_accept_event(accept_event)
        alice_key = {"sender": alice_id, "content": alice.share_key().content}
        alice_key_event = KeyVerificationKey.from_dict(alice_key)

        alice_key_event.sender = faker.mx_id()
        bob.receive_key_event(alice_key_event)
        assert bob.canceled

        bob.set_their_pubkey(alice.pubkey)
        bob.state = SasState.key_received
        bob.chosen_mac_method = Sas._mac_normal

        alice.chosen_mac_method = Sas._mac_normal
        alice.set_their_pubkey(bob.pubkey)
        alice.state = SasState.key_received

        bob.accept_sas()
        bob_mac = {"sender": bob_id, "content": bob.get_mac().content}

        mac_event = KeyVerificationMac.from_dict(bob_mac)

        mac_event.sender = faker.mx_id()
        alice.receive_mac_event(mac_event)
        assert alice.canceled