async def test_room_key_on_client_sync_stream(self, client): await client.receive_response(self.login_response) await client.receive_response( SyncResponse.from_dict(self.initial_sync_response)) await client.receive_response( KeysUploadResponse.from_dict(self.keys_upload_response)) await client.receive_response( KeysQueryResponse.from_dict(self.keys_query_response)) BobId = "@bob:example.org" Bob_device = "BOBDEVICE" bob_olm = Olm(BobId, Bob_device, SqliteMemoryStore("ephemeral", "DEVICEID")) alice_device = OlmDevice(client.user_id, client.device_id, client.olm.account.identity_keys) bob_device = OlmDevice(bob_olm.user_id, bob_olm.device_id, bob_olm.account.identity_keys) client.olm.device_store.add(bob_device) bob_olm.device_store.add(alice_device) bob_olm.store.save_device_keys( {client.user_id: { client.device_id: alice_device }}) client.olm.account.generate_one_time_keys(1) one_time = list( client.olm.account.one_time_keys["curve25519"].values())[0] client.olm.account.mark_keys_as_published() bob_olm.create_session(one_time, alice_device.curve25519) _, to_device = bob_olm.share_group_session( TEST_ROOM_ID, [client.user_id], ignore_unverified_devices=True) outbound_session = bob_olm.outbound_group_sessions[TEST_ROOM_ID] olm_content = to_device["messages"][client.user_id][client.device_id] payload = { "sender": bob_olm.user_id, "type": "m.room.encrypted", "content": olm_content, } sync_response = self.empty_sync sync_response["to_device"]["events"].append(payload) session = client.olm.inbound_group_store.get(TEST_ROOM_ID, bob_device.curve25519, outbound_session.id) assert not session client.handle_to_device_from_sync_body(sync_response) session = client.olm.inbound_group_store.get(TEST_ROOM_ID, bob_device.curve25519, outbound_session.id) assert session
def test_export(self, tempdir): user_id = "ephemeral" device_id = "DEVICEID" file = path.join(tempdir, "keys_file") store = DefaultStore(user_id, device_id, tempdir, "") olm = Olm(user_id, device_id, store) olm.create_outbound_group_session(TEST_ROOM) out_session = olm.outbound_group_sessions[TEST_ROOM] assert olm.inbound_group_store.get( TEST_ROOM, olm.account.identity_keys["curve25519"], out_session.id) olm.export_keys(file, "pass") alice_store = DefaultStore("alice", device_id, tempdir, "") alice = Olm("alice", device_id, alice_store) assert not alice.inbound_group_store.get( TEST_ROOM, olm.account.identity_keys["curve25519"], out_session.id) alice.import_keys(file, "pass") assert alice.inbound_group_store.get( TEST_ROOM, olm.account.identity_keys["curve25519"], out_session.id)
def test_invalid_json(self, tempdir): device_id = "DEVICEID" file = path.join(tempdir, "keys_file") encrypt_and_save(b"{sessions: [{}]}", file, "pass", count=10) alice_store = DefaultStore("alice", device_id, tempdir, "") alice = Olm("alice", device_id, alice_store) with pytest.raises(EncryptionError): alice.import_keys(file, "pass")
def test_unencrypted_import(self, tempdir): device_id = "DEVICEID" file = path.join(tempdir, "keys_file") with open(file, "w") as f: f.write("{}") alice_store = DefaultStore("alice", device_id, tempdir, "") alice = Olm("alice", device_id, alice_store) with pytest.raises(EncryptionError): alice.import_keys(file, "pass")
def test_invalid_json_schema(self, tempdir): device_id = "DEVICEID" file = path.join(tempdir, "keys_file") payload = {"sessions": [{"algorithm": "test"}]} encrypt_and_save(json.dumps(payload).encode(), file, "pass", count=10) alice_store = DefaultStore("alice", device_id, tempdir, "") alice = Olm("alice", device_id, alice_store) with pytest.raises(EncryptionError): alice.import_keys(file, "pass")
def olm_machine(): key_pair = Account().identity_keys bob_device = OlmDevice(BOB_DEVICE, BOB_DEVICE_ID, key_pair) store = SqliteMemoryStore(ALICE_ID, ALICE_DEVICE_ID) client = Olm(ALICE_ID, ALICE_DEVICE_ID, store) client.device_store.add(bob_device) store.save_device_keys(client.device_store) return client