예제 #1
0
    def test_load_all(self, account, curve_key, ed_key, device):
        curve_key = account.identity_keys['curve25519']
        session = olm.OutboundSession(account, curve_key, curve_key)
        out_session = MegolmOutboundSession()
        out_session.add_device(self.device_id)
        in_session = MegolmInboundSession(out_session.session_key, ed_key)
        device_keys_to_save = {self.user_id: {self.device_id: device}}

        self.store.save_inbound_session(self.room_id, curve_key, in_session)
        self.store.save_olm_session(curve_key, session)
        self.store.save_outbound_session(self.room_id, out_session)
        self.store.save_megolm_outbound_devices(self.room_id, {self.device_id})
        self.store.save_device_keys(device_keys_to_save)

        device = OlmDevice(
            None, self.user_id, self.device_id, store_conf=self.store_conf, load_all=True)

        assert session.id in {s.id for s in device.olm_sessions[curve_key]}
        saved_in_session = \
            device.megolm_inbound_sessions[self.room_id][curve_key][in_session.id]
        assert saved_in_session.id == in_session.id
        saved_out_session = device.megolm_outbound_sessions[self.room_id]
        assert saved_out_session.id == out_session.id
        assert saved_out_session.devices == out_session.devices
        assert device.device_keys[self.user_id][self.device_id].curve25519 == \
            device.curve25519
예제 #2
0
def test_megolm_outbound_session():
    session = MegolmOutboundSession()
    assert session.max_messages == 100
    assert session.max_age == timedelta(days=7)

    session = MegolmOutboundSession(max_messages=1, max_age=100000)
    assert session.max_messages == 1
    assert session.max_age == timedelta(milliseconds=100000)

    assert not session.devices

    session.add_device('test')
    assert 'test' in session.devices

    session.add_devices({'test2', 'test3'})
    assert 'test2' in session.devices and 'test3' in session.devices

    assert not session.should_rotate()

    session.encrypt('message')
    assert session.should_rotate()

    session.max_messages = 2
    assert not session.should_rotate()
    session.creation_time = datetime.now() - timedelta(milliseconds=100000)
    assert session.should_rotate()
예제 #3
0
    def test_megolm_get_recipients(self, device):
        device.device_keys[self.alice][
            self.alice_device_id] = self.alice_device

        user_devices, _ = device.megolm_get_recipients(self.room)
        assert user_devices == {self.alice: [self.alice_device_id]}

        session = MegolmOutboundSession()
        device.megolm_outbound_sessions[self.room_id] = session

        user_devices, removed = device.megolm_get_recipients(
            self.room, session)
        assert user_devices == {
            self.alice: [self.alice_device_id]
        } and not removed

        self.alice_device.blacklisted = True
        _, removed = device.megolm_get_recipients(self.room, session)
        assert not removed
        session.add_device(self.alice_device_id)
        _, removed = device.megolm_get_recipients(self.room, session)
        assert removed and self.room_id not in device.megolm_outbound_sessions
        self.alice_device.blacklisted = False

        self.room.verify_devices = True
        with pytest.raises(E2EUnknownDevices) as e:
            device.megolm_get_recipients(self.room)
        assert e.value.user_devices == {self.alice: [self.alice_device]}
        self.room.verify_devices = False
예제 #4
0
    def test_send_encrypted_message(self, device):
        message_url = HOSTNAME + MATRIX_V2_API_PATH + \
            '/rooms/{}/send/m.room.encrypted/1'.format(quote(self.room.room_id))
        responses.add(responses.PUT, message_url, json={})
        session = MegolmOutboundSession()
        session.add_device(self.alice_device_id)
        device.megolm_outbound_sessions[self.room_id] = session

        device.send_encrypted_message(self.room, {'test': 'test'})
예제 #5
0
    def test_megolm_outbound_persistence(self, device):
        session = MegolmOutboundSession(max_messages=2, max_age=100000)
        session.message_count = 1
        session.add_device(self.device_id)
        sessions = {}

        self.store.load_outbound_sessions(sessions)
        assert not sessions
        assert not self.store.get_outbound_session(self.room_id)

        self.store.save_outbound_session(self.room_id, session)
        self.store.save_megolm_outbound_devices(self.room_id, {self.device_id})
        self.store.load_outbound_sessions(sessions)
        assert sessions[self.room_id].id == session.id
        assert sessions[self.room_id].devices == session.devices
        assert sessions[self.room_id].creation_time == session.creation_time
        assert sessions[self.room_id].max_messages == session.max_messages
        assert sessions[self.room_id].message_count == session.message_count
        assert sessions[self.room_id].max_age == session.max_age

        saved_session = self.store.get_outbound_session(self.room_id)
        assert saved_session.id == session.id
        assert saved_session.devices == session.devices
        assert saved_session.creation_time == session.creation_time
        assert saved_session.max_messages == session.max_messages
        assert saved_session.message_count == session.message_count
        assert saved_session.max_age == session.max_age

        sessions.clear()
        saved_session = self.store.get_outbound_session(self.room_id, sessions)
        assert sessions[self.room_id].id == session.id

        self.store.remove_outbound_session(self.room_id)
        assert not self.store.get_outbound_session(self.room_id)

        self.store.save_outbound_session(self.room_id, session)
        saved_session = self.store.get_outbound_session(self.room_id)
        # Verify the saved devices have been erased with the session
        assert not saved_session.devices

        room = Room(None, self.room_id)
        with pytest.raises(AttributeError):
            device.megolm_build_encrypted_event(room, {})
        assert device.megolm_outbound_sessions[self.room_id].id == session.id

        self.store.remove_olm_account()
        assert not self.store.get_outbound_session(self.room_id)