feat: Ratchets should overwrite each other

This commit is contained in:
PapaTutuWawa 2022-08-18 15:20:32 +02:00
parent 73613e266f
commit ee7b09bdb0
2 changed files with 83 additions and 11 deletions

View File

@ -110,21 +110,22 @@ class OmemoSessionManager {
// Add the bundle Id // Add the bundle Id
if (!_deviceMap.containsKey(jid)) { if (!_deviceMap.containsKey(jid)) {
_deviceMap[jid] = [deviceId]; _deviceMap[jid] = [deviceId];
} else {
_deviceMap[jid]!.add(deviceId);
}
// Commit the device map // Commit the device map
_eventStreamController.add(DeviceMapModifiedEvent(_deviceMap)); _eventStreamController.add(DeviceMapModifiedEvent(_deviceMap));
} else {
// Prevent having the same device multiple times in the list
if (!_deviceMap[jid]!.contains(deviceId)) {
_deviceMap[jid]!.add(deviceId);
// Commit the device map
_eventStreamController.add(DeviceMapModifiedEvent(_deviceMap));
}
}
// Add the ratchet session // Add the ratchet session
final key = RatchetMapKey(jid, deviceId); final key = RatchetMapKey(jid, deviceId);
if (!_ratchetMap.containsKey(key)) { _ratchetMap[key] = ratchet;
_ratchetMap[key] = ratchet;
} else {
// TODO(PapaTutuWawa): What do we do now?
throw Exception();
}
// Commit the ratchet // Commit the ratchet
_eventStreamController.add(RatchetModifiedEvent(jid, deviceId, ratchet)); _eventStreamController.add(RatchetModifiedEvent(jid, deviceId, ratchet));

View File

@ -543,4 +543,75 @@ void main() {
true, true,
); );
}); });
test('Test overwriting sessions', () async {
const aliceJid = 'alice@server.example';
const bobJid = 'bob@other.server.example';
// Alice and Bob generate their sessions
final aliceSession = await OmemoSessionManager.generateNewIdentity(
aliceJid,
AlwaysTrustingTrustManager(),
opkAmount: 1,
);
final bobSession = await OmemoSessionManager.generateNewIdentity(
bobJid,
AlwaysTrustingTrustManager(),
opkAmount: 2,
);
// Alice sends Bob a message
final msg1 = await aliceSession.encryptToJid(
bobJid,
'Hallo Welt',
newSessions: [
await (await bobSession.getDevice()).toBundle(),
],
);
await bobSession.decryptMessage(
msg1.ciphertext,
aliceJid,
(await aliceSession.getDevice()).id,
msg1.encryptedKeys,
);
final aliceRatchet1 = aliceSession.getRatchet(
bobJid,
(await bobSession.getDevice()).id,
);
final bobRatchet1 = bobSession.getRatchet(
aliceJid,
(await aliceSession.getDevice()).id,
);
// Alice is impatient and immediately sends another message before the original one
// can be acknowledged by Bob
final msg2 = await aliceSession.encryptToJid(
bobJid,
"Why don't you answer?",
newSessions: [
await (await bobSession.getDevice()).toBundle(),
],
);
await bobSession.decryptMessage(
msg2.ciphertext,
aliceJid,
(await aliceSession.getDevice()).id,
msg2.encryptedKeys,
);
final aliceRatchet2 = aliceSession.getRatchet(
bobJid,
(await bobSession.getDevice()).id,
);
final bobRatchet2 = bobSession.getRatchet(
aliceJid,
(await aliceSession.getDevice()).id,
);
// Both should only have one ratchet
expect(aliceSession.getRatchetMap().length, 1);
expect(bobSession.getRatchetMap().length, 1);
// The ratchets should both be different
expect(await aliceRatchet1.equals(aliceRatchet2), false);
expect(await bobRatchet1.equals(bobRatchet2), false);
});
} }