Compare commits
No commits in common. "d5d4aa9014a3bc16d774e4f122940a4fa6cb9455" and "73613e266f4e537238314c5d52744179577570d3" have entirely different histories.
d5d4aa9014
...
73613e266f
@ -65,7 +65,7 @@ void main() async {
|
||||
// a new session. Let's also assume that Bob only has one device. We may, however,
|
||||
// add more bundles to newSessions, if we know of more.
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
|
||||
|
@ -103,18 +103,6 @@ class OmemoSessionManager {
|
||||
|
||||
return dev!;
|
||||
}
|
||||
|
||||
/// Returns the id attribute of our own device. This is just a short-hand for
|
||||
/// ```await (session.getDevice()).id```.
|
||||
Future<int> getDeviceId() async {
|
||||
return _deviceLock.synchronized(() => _device.id);
|
||||
}
|
||||
|
||||
/// Returns the device as an OmemoBundle. This is just a short-hand for
|
||||
/// ```await (await session.getDevice()).toBundle()```.
|
||||
Future<OmemoBundle> getDeviceBundle() async {
|
||||
return _deviceLock.synchronized(() async => _device.toBundle());
|
||||
}
|
||||
|
||||
/// Add a session [ratchet] with the [deviceId] to the internal tracking state.
|
||||
Future<void> _addSession(String jid, int deviceId, OmemoDoubleRatchet ratchet) async {
|
||||
@ -122,22 +110,21 @@ class OmemoSessionManager {
|
||||
// Add the bundle Id
|
||||
if (!_deviceMap.containsKey(jid)) {
|
||||
_deviceMap[jid] = [deviceId];
|
||||
|
||||
// Commit the device map
|
||||
_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));
|
||||
}
|
||||
_deviceMap[jid]!.add(deviceId);
|
||||
}
|
||||
|
||||
// Commit the device map
|
||||
_eventStreamController.add(DeviceMapModifiedEvent(_deviceMap));
|
||||
|
||||
// Add the ratchet session
|
||||
final key = RatchetMapKey(jid, deviceId);
|
||||
_ratchetMap[key] = ratchet;
|
||||
if (!_ratchetMap.containsKey(key)) {
|
||||
_ratchetMap[key] = ratchet;
|
||||
} else {
|
||||
// TODO(PapaTutuWawa): What do we do now?
|
||||
throw Exception();
|
||||
}
|
||||
|
||||
// Commit the ratchet
|
||||
_eventStreamController.add(RatchetModifiedEvent(jid, deviceId, ratchet));
|
||||
|
@ -38,7 +38,7 @@ void main() {
|
||||
bobJid,
|
||||
messagePlaintext,
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
expect(aliceMessage.encryptedKeys.length, 1);
|
||||
@ -50,7 +50,7 @@ void main() {
|
||||
final bobMessage = await bobSession.decryptMessage(
|
||||
aliceMessage.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
(await aliceSession.getDevice()).id,
|
||||
aliceMessage.encryptedKeys,
|
||||
);
|
||||
expect(messagePlaintext, bobMessage);
|
||||
@ -81,7 +81,7 @@ void main() {
|
||||
final aliceReceivedMessage = await aliceSession.decryptMessage(
|
||||
bobResponseMessage.ciphertext,
|
||||
bobJid,
|
||||
await bobSession.getDeviceId(),
|
||||
(await bobSession.getDevice()).id,
|
||||
bobResponseMessage.encryptedKeys,
|
||||
);
|
||||
expect(bobResponseText, aliceReceivedMessage);
|
||||
@ -115,8 +115,8 @@ void main() {
|
||||
bobJid,
|
||||
messagePlaintext,
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await bobSession2.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
await (await bobSession2.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
expect(aliceMessage.encryptedKeys.length, 2);
|
||||
@ -130,7 +130,7 @@ void main() {
|
||||
final bobMessage = await bobSession.decryptMessage(
|
||||
aliceMessage.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
(await aliceSession.getDevice()).id,
|
||||
aliceMessage.encryptedKeys,
|
||||
);
|
||||
expect(messagePlaintext, bobMessage);
|
||||
@ -149,7 +149,7 @@ void main() {
|
||||
final aliceReceivedMessage = await aliceSession.decryptMessage(
|
||||
bobResponseMessage.ciphertext,
|
||||
bobJid,
|
||||
await bobSession.getDeviceId(),
|
||||
(await bobSession.getDevice()).id,
|
||||
bobResponseMessage.encryptedKeys,
|
||||
);
|
||||
expect(bobResponseText, aliceReceivedMessage);
|
||||
@ -192,8 +192,8 @@ void main() {
|
||||
[bobJid, aliceJid],
|
||||
messagePlaintext,
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await aliceSession2.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
await (await aliceSession2.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
expect(aliceMessage.encryptedKeys.length, 2);
|
||||
@ -205,7 +205,7 @@ void main() {
|
||||
final bobMessage = await bobSession.decryptMessage(
|
||||
aliceMessage.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession1.getDeviceId(),
|
||||
(await aliceSession1.getDevice()).id,
|
||||
aliceMessage.encryptedKeys,
|
||||
);
|
||||
expect(messagePlaintext, bobMessage);
|
||||
@ -214,7 +214,7 @@ void main() {
|
||||
final aliceMessage2 = await aliceSession2.decryptMessage(
|
||||
aliceMessage.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession1.getDeviceId(),
|
||||
(await aliceSession1.getDevice()).id,
|
||||
aliceMessage.encryptedKeys,
|
||||
);
|
||||
expect(messagePlaintext, aliceMessage2);
|
||||
@ -241,7 +241,7 @@ void main() {
|
||||
bobJid,
|
||||
null,
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
expect(aliceMessage.encryptedKeys.length, 1);
|
||||
@ -254,13 +254,13 @@ void main() {
|
||||
final bobMessage = await bobSession.decryptMessage(
|
||||
aliceMessage.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
(await aliceSession.getDevice()).id,
|
||||
aliceMessage.encryptedKeys,
|
||||
);
|
||||
expect(bobMessage, null);
|
||||
|
||||
// This call must not cause an exception
|
||||
bobSession.getRatchet(aliceJid, await aliceSession.getDeviceId());
|
||||
bobSession.getRatchet(aliceJid, (await aliceSession.getDevice()).id);
|
||||
});
|
||||
|
||||
test('Test rotating the Signed Prekey', () async {
|
||||
@ -316,7 +316,7 @@ void main() {
|
||||
bobJid,
|
||||
messagePlaintext,
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
expect(aliceMessage.encryptedKeys.length, 1);
|
||||
@ -331,7 +331,7 @@ void main() {
|
||||
final bobMessage = await bobSession.decryptMessage(
|
||||
aliceMessage.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
(await aliceSession.getDevice()).id,
|
||||
aliceMessage.encryptedKeys,
|
||||
);
|
||||
expect(messagePlaintext, bobMessage);
|
||||
@ -358,7 +358,7 @@ void main() {
|
||||
bobJid,
|
||||
null,
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
|
||||
@ -387,7 +387,7 @@ void main() {
|
||||
bobJid,
|
||||
'Hello Bob!',
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
|
||||
@ -397,7 +397,7 @@ void main() {
|
||||
await bobSession.decryptMessage(
|
||||
aliceMessage.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
(await aliceSession.getDevice()).id,
|
||||
aliceMessage.encryptedKeys,
|
||||
);
|
||||
|
||||
@ -416,7 +416,7 @@ void main() {
|
||||
final aliceReceivedMessage = await aliceSession.decryptMessage(
|
||||
bobResponseMessage.ciphertext,
|
||||
bobJid,
|
||||
await bobSession.getDeviceId(),
|
||||
(await bobSession.getDevice()).id,
|
||||
bobResponseMessage.encryptedKeys,
|
||||
);
|
||||
expect(messageText, aliceReceivedMessage);
|
||||
@ -448,14 +448,14 @@ void main() {
|
||||
bobJid,
|
||||
'Hallo Welt',
|
||||
newSessions: [
|
||||
await bobSession1.getDeviceBundle(),
|
||||
await bobSession2.getDeviceBundle(),
|
||||
await (await bobSession1.getDevice()).toBundle(),
|
||||
await (await bobSession2.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
|
||||
// One of those two sessions is broken, so Alice removes the session2 ratchet
|
||||
final id1 = await bobSession1.getDeviceId();
|
||||
final id2 = await bobSession2.getDeviceId();
|
||||
final id1 = (await bobSession1.getDevice()).id;
|
||||
final id2 = (await bobSession2.getDevice()).id;
|
||||
await aliceSession.removeRatchet(bobJid, id1);
|
||||
|
||||
final map = aliceSession.getRatchetMap();
|
||||
@ -485,12 +485,12 @@ void main() {
|
||||
bobJid,
|
||||
'Hallo Welt',
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
|
||||
// One of those two sessions is broken, so Alice removes the session2 ratchet
|
||||
final id = await bobSession.getDeviceId();
|
||||
final id = (await bobSession.getDevice()).id;
|
||||
await aliceSession.removeRatchet(bobJid, id);
|
||||
|
||||
final map = aliceSession.getRatchetMap();
|
||||
@ -520,13 +520,13 @@ void main() {
|
||||
bobJid,
|
||||
'Hallo Welt',
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
expect(
|
||||
await aliceSession.getUnacknowledgedRatchets(bobJid),
|
||||
[
|
||||
await bobSession.getDeviceId(),
|
||||
(await bobSession.getDevice()).id,
|
||||
],
|
||||
);
|
||||
|
||||
@ -537,81 +537,10 @@ void main() {
|
||||
// ...
|
||||
|
||||
// Alice marks the ratchet as acknowledged
|
||||
await aliceSession.ratchetAcknowledged(bobJid, await bobSession.getDeviceId());
|
||||
await aliceSession.ratchetAcknowledged(bobJid, (await bobSession.getDevice()).id);
|
||||
expect(
|
||||
(await aliceSession.getUnacknowledgedRatchets(bobJid)).isEmpty,
|
||||
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 bobSession.getDeviceBundle(),
|
||||
],
|
||||
);
|
||||
await bobSession.decryptMessage(
|
||||
msg1.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
msg1.encryptedKeys,
|
||||
);
|
||||
final aliceRatchet1 = aliceSession.getRatchet(
|
||||
bobJid,
|
||||
await bobSession.getDeviceId(),
|
||||
);
|
||||
final bobRatchet1 = bobSession.getRatchet(
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
);
|
||||
|
||||
// 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 bobSession.getDeviceBundle(),
|
||||
],
|
||||
);
|
||||
await bobSession.decryptMessage(
|
||||
msg2.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
msg2.encryptedKeys,
|
||||
);
|
||||
final aliceRatchet2 = aliceSession.getRatchet(
|
||||
bobJid,
|
||||
await bobSession.getDeviceId(),
|
||||
);
|
||||
final bobRatchet2 = bobSession.getRatchet(
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
);
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
|
@ -49,16 +49,16 @@ void main() {
|
||||
bobJid,
|
||||
'Hello Bob!',
|
||||
newSessions: [
|
||||
await bobSession.getDeviceBundle(),
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
],
|
||||
);
|
||||
await bobSession.decryptMessage(
|
||||
aliceMessage.ciphertext,
|
||||
aliceJid,
|
||||
await aliceSession.getDeviceId(),
|
||||
(await aliceSession.getDevice()).id,
|
||||
aliceMessage.encryptedKeys,
|
||||
);
|
||||
final aliceOld = aliceSession.getRatchet(bobJid, await bobSession.getDeviceId());
|
||||
final aliceOld = aliceSession.getRatchet(bobJid, (await bobSession.getDevice()).id);
|
||||
final aliceSerialised = await aliceOld.toJson();
|
||||
final aliceNew = OmemoDoubleRatchet.fromJson(aliceSerialised);
|
||||
|
||||
@ -79,8 +79,8 @@ void main() {
|
||||
);
|
||||
await oldSession.addSessionFromBundle(
|
||||
'bob@localhost',
|
||||
await bobSession.getDeviceId(),
|
||||
await bobSession.getDeviceBundle(),
|
||||
(await bobSession.getDevice()).id,
|
||||
await (await bobSession.getDevice()).toBundle(),
|
||||
);
|
||||
|
||||
// Serialise and deserialise
|
||||
|
Loading…
Reference in New Issue
Block a user