Beispiel #1
0
    def test_db_upgrade(self, tempdir):
        user = "******"
        device_id = "DEVICE_ID"
        user2 = "alice"
        device_id2 = "ALICE_ID"

        store = MatrixStore(user, device_id, tempdir, database_name="test.db")
        account = OlmAccount()
        session = OutboundSession(account, BOB_CURVE, BOB_ONETIME)
        out_group = OutboundGroupSession()
        in_group = InboundGroupSession(out_group.session_key,
                                       account.identity_keys["ed25519"],
                                       account.identity_keys["curve25519"],
                                       TEST_ROOM, TEST_FORWARDING_CHAIN)
        devices = self.example_devices
        assert len(devices) == 11

        store.save_account(account)
        store.save_session(BOB_CURVE, session)
        store.save_inbound_group_session(in_group)
        store.save_device_keys(devices)

        store2 = MatrixStore(user2,
                             device_id2,
                             tempdir,
                             database_name="test.db")
        account2 = OlmAccount()
        store2.save_account(account2)
        del store

        store = MatrixStore(user, device_id, tempdir, database_name="test.db")
        loaded_account = store.load_account()

        assert account.identity_keys == loaded_account.identity_keys
        session_store = store.load_sessions()
        loaded_session = session_store.get(BOB_CURVE)
        session_store = store.load_inbound_group_sessions()

        assert loaded_session
        assert session.id == loaded_session.id

        loaded_session = session_store.get(TEST_ROOM,
                                           account.identity_keys["curve25519"],
                                           in_group.id)
        device_store = store.load_device_keys()

        # pdb.set_trace()

        assert loaded_session
        assert in_group.id == loaded_session.id
        assert (sorted(
            loaded_session.forwarding_chain) == sorted(TEST_FORWARDING_CHAIN))
        bob_device = device_store[BOB_ID][BOB_DEVICE]
        assert bob_device
        assert bob_device.user_id == BOB_ID
        assert bob_device.id == BOB_DEVICE
        assert bob_device.ed25519 == BOB_ONETIME
        assert bob_device.curve25519 == BOB_CURVE
        assert not bob_device.deleted
        assert len(device_store.users) == 11
Beispiel #2
0
    def test_session_store_order(self):
        alice = OlmAccount()
        bob = OlmAccount()
        bob_curve = bob.identity_keys["curve25519"]
        bob.generate_one_time_keys(2)

        store = SessionStore()

        first, second = bob.one_time_keys["curve25519"].values()

        session2 = OutboundSession(alice, bob_curve, second)
        session = OutboundSession(alice, bob_curve, first)

        assert session.id != session2.id

        assert session not in store

        assert store.add(bob_curve, session)
        assert len(store[bob_curve]) == 1
        assert session in store
        assert store.add(bob_curve, session2) is True
        print(store.values())
        assert len(store[bob_curve]) == 2

        session_a, session_b = store[bob_curve]

        assert session_a.use_time > session_b.use_time
Beispiel #3
0
    def test_session(self):
        account = OlmAccount()
        session = OutboundSession(account, BOB_CURVE, BOB_ONETIME)

        assert session.id == Session.from_pickle(session.pickle(),
                                                 session.creation_time).id
        assert not session.expired
Beispiel #4
0
    def test_new_store_account_saving(self, matrix_store):
        account = OlmAccount()
        matrix_store.save_account(account)

        store2 = MatrixStore(matrix_store.user_id, matrix_store.device_id,
                             matrix_store.store_path)
        loaded_account = store2.load_account()

        assert account.identity_keys == loaded_account.identity_keys
Beispiel #5
0
def panstore(tempdir):
    for _ in range(10):
        store = SqliteStore(faker.mx_id(), faker.device_id(), tempdir, "",
                            "pan.db")
        account = OlmAccount()
        store.save_account(account)

    store = PanStore(tempdir, "pan.db")
    return store
Beispiel #6
0
    def test_olm_session_encryption(self):
        alice = OlmAccount()
        bob = OlmAccount()
        plaintext = "It's a secret to everybody"
        bob_curve = bob.identity_keys["curve25519"]

        bob.generate_one_time_keys(1)
        bob_onetime = list(bob.one_time_keys["curve25519"].values())[0]

        session = OutboundSession(alice, bob_curve, bob_onetime)
        creation_time = session.use_time

        # Encrypt a message and check that the use time increased.
        message = session.encrypt(plaintext)
        assert session.use_time >= creation_time

        inbound = InboundSession(bob, message)
        creation_time = inbound.use_time

        # Decrypt a message and check that the use time increased.
        decrypted_plaintext = inbound.decrypt(message)
        assert inbound.use_time >= creation_time

        assert decrypted_plaintext == plaintext

        pickle = inbound.pickle("")

        unpickled = Session.from_pickle(pickle, inbound.creation_time, "",
                                        inbound.use_time)

        use_time = unpickled.use_time
        message = unpickled.encrypt(plaintext)

        assert unpickled.use_time > use_time

        pickle = session.pickle("")
        unpickled = Session.from_pickle(pickle, session.creation_time, "",
                                        session.use_time)
        use_time = unpickled.use_time
        decrypted_plaintext = unpickled.decrypt(message)
        assert unpickled.use_time >= use_time
        assert decrypted_plaintext == plaintext
Beispiel #7
0
    def test_group_session_store(self):
        store = GroupSessionStore()
        account = OlmAccount()

        out_group = OutboundGroupSession()
        session = InboundGroupSession(out_group.session_key,
                                      account.identity_keys["ed25519"],
                                      BOB_CURVE, TEST_ROOM)

        assert session not in store
        assert not store.get(TEST_ROOM, BOB_CURVE, session.id)

        assert store.add(session)

        assert store.get(TEST_ROOM, BOB_CURVE, session.id)
        assert session in store

        assert not store.add(session)

        assert store[TEST_ROOM] == {BOB_CURVE: {session.id: session}}
Beispiel #8
0
    def test_session_store(self):
        account = OlmAccount()
        store = SessionStore()
        session = OutboundSession(account, BOB_CURVE, BOB_ONETIME)

        assert session not in store
        assert len(store.values()) == 0
        assert not store.get(BOB_CURVE)

        assert store.add(BOB_CURVE, session)

        assert len(store.values()) == 1
        assert session in store

        assert not store.add(BOB_CURVE, session)

        assert len(store.values()) == 1
        assert session in store

        assert (BOB_CURVE, [session]) == list(store.items())[0]
Beispiel #9
0
 def _create_ephemeral_account(self):
     store = self.ephemeral_store
     account = OlmAccount()
     store.save_account(account)
     return account
Beispiel #10
0
def sqlmemorystore():
    store = SqliteMemoryStore("ephemeral", "DEVICEID")
    account = OlmAccount()
    store.save_account(account)
    return store
Beispiel #11
0
def sqlstore(tempdir):
    store = SqliteStore("ephemeral", "DEVICEID", tempdir)
    account = OlmAccount()
    store.save_account(account)
    return store
Beispiel #12
0
def store(tempdir):
    store = DefaultStore("ephemeral", "DEVICEID", tempdir)
    account = OlmAccount()
    store.save_account(account)
    return store
Beispiel #13
0
 def olm_key_pair(self):
     return OlmAccount().identity_keys
Beispiel #14
0
    def test_account(self):
        account = OlmAccount()

        assert (account.identity_keys == OlmAccount.from_pickle(
            account.pickle()).identity_keys)