2022-09-15 11:17:30 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2022-08-04 14:30:09 +00:00
|
|
|
import 'package:omemo_dart/omemo_dart.dart';
|
2022-08-08 16:03:05 +00:00
|
|
|
import 'package:omemo_dart/src/trust/always.dart';
|
2022-08-09 12:45:04 +00:00
|
|
|
import 'package:omemo_dart/src/trust/never.dart';
|
2022-08-04 14:30:09 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
void main() {
|
2022-09-15 11:17:30 +00:00
|
|
|
Logger.root
|
|
|
|
..level = Level.ALL
|
|
|
|
..onRecord.listen((record) {
|
|
|
|
// ignore: avoid_print
|
|
|
|
print('${record.level.name}: ${record.message}');
|
|
|
|
});
|
|
|
|
|
2022-09-15 21:31:56 +00:00
|
|
|
test('Test replacing a onetime prekey', () async {
|
|
|
|
const aliceJid = 'alice@server.example';
|
2023-01-01 15:49:19 +00:00
|
|
|
final device = await OmemoDevice.generateNewDevice(aliceJid, opkAmount: 1);
|
2022-09-15 21:31:56 +00:00
|
|
|
|
|
|
|
final newDevice = await device.replaceOnetimePrekey(0);
|
|
|
|
|
|
|
|
expect(device.jid, newDevice.jid);
|
|
|
|
expect(device.id, newDevice.id);
|
|
|
|
|
|
|
|
var opksMatch = true;
|
|
|
|
if (newDevice.opks.length != device.opks.length) {
|
|
|
|
opksMatch = false;
|
|
|
|
} else {
|
|
|
|
for (final entry in device.opks.entries) {
|
|
|
|
final m = await newDevice.opks[entry.key]?.equals(entry.value) ?? false;
|
|
|
|
if (!m) opksMatch = false;
|
|
|
|
}
|
|
|
|
}
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-09-15 21:31:56 +00:00
|
|
|
expect(opksMatch, true);
|
|
|
|
expect(await device.ik.equals(newDevice.ik), true);
|
|
|
|
expect(await device.spk.equals(newDevice.spk), true);
|
|
|
|
|
2023-06-12 17:20:43 +00:00
|
|
|
final oldSpkMatch = device.oldSpk != null
|
|
|
|
? await device.oldSpk!.equals(newDevice.oldSpk!)
|
|
|
|
: newDevice.oldSpk == null;
|
2022-09-15 21:31:56 +00:00
|
|
|
expect(oldSpkMatch, true);
|
|
|
|
expect(listsEqual(device.spkSignature, newDevice.spkSignature), true);
|
|
|
|
});
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-04 14:30:09 +00:00
|
|
|
test('Test using OMEMO sessions with only one device per user', () async {
|
|
|
|
const aliceJid = 'alice@server.example';
|
|
|
|
const bobJid = 'bob@other.server.example';
|
|
|
|
// Alice and Bob generate their sessions
|
2022-08-05 14:52:02 +00:00
|
|
|
var deviceModified = false;
|
2022-08-06 11:51:07 +00:00
|
|
|
var ratchetModified = 0;
|
|
|
|
var deviceMapModified = 0;
|
2022-08-08 16:03:05 +00:00
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-05 14:52:02 +00:00
|
|
|
final bobOpks = (await bobSession.getDevice()).opks.values.toList();
|
|
|
|
bobSession.eventStream.listen((event) {
|
2022-08-06 11:51:07 +00:00
|
|
|
if (event is DeviceModifiedEvent) {
|
2022-08-05 14:52:02 +00:00
|
|
|
deviceModified = true;
|
2022-08-06 11:51:07 +00:00
|
|
|
} else if (event is RatchetModifiedEvent) {
|
|
|
|
ratchetModified++;
|
2022-12-25 21:59:53 +00:00
|
|
|
} else if (event is DeviceListModifiedEvent) {
|
2022-08-06 11:51:07 +00:00
|
|
|
deviceMapModified++;
|
2022-08-05 14:52:02 +00:00
|
|
|
}
|
|
|
|
});
|
2022-08-04 14:30:09 +00:00
|
|
|
|
|
|
|
// Alice encrypts a message for Bob
|
|
|
|
const messagePlaintext = 'Hello Bob!';
|
2022-08-05 11:34:42 +00:00
|
|
|
final aliceMessage = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
messagePlaintext,
|
2022-08-05 14:05:05 +00:00
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-05 14:05:05 +00:00
|
|
|
],
|
2022-08-05 11:34:42 +00:00
|
|
|
);
|
2022-08-04 14:30:09 +00:00
|
|
|
expect(aliceMessage.encryptedKeys.length, 1);
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-04 14:30:09 +00:00
|
|
|
// Alice sends the message to Bob
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Bob decrypts it
|
|
|
|
final bobMessage = await bobSession.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-05 11:34:42 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-04 14:30:09 +00:00
|
|
|
);
|
|
|
|
expect(messagePlaintext, bobMessage);
|
2022-08-06 11:51:07 +00:00
|
|
|
// The ratchet should be modified two times: Once for when the ratchet is created and
|
|
|
|
// other time for when the message is decrypted
|
|
|
|
expect(ratchetModified, 2);
|
|
|
|
// Bob's device map should be modified once
|
|
|
|
expect(deviceMapModified, 1);
|
2022-08-05 14:52:02 +00:00
|
|
|
// The event should be triggered
|
|
|
|
expect(deviceModified, true);
|
|
|
|
// Bob should have replaced his OPK
|
|
|
|
expect(
|
|
|
|
listsEqual(bobOpks, (await bobSession.getDevice()).opks.values.toList()),
|
|
|
|
false,
|
|
|
|
);
|
2022-08-05 13:53:16 +00:00
|
|
|
|
2022-10-22 10:41:41 +00:00
|
|
|
// Ratchets are acked
|
2023-06-12 17:20:43 +00:00
|
|
|
await aliceSession.ratchetAcknowledged(
|
|
|
|
bobJid, await bobSession.getDeviceId());
|
|
|
|
await bobSession.ratchetAcknowledged(
|
|
|
|
aliceJid, await aliceSession.getDeviceId());
|
|
|
|
|
2022-08-05 13:53:16 +00:00
|
|
|
// Bob responds to Alice
|
2022-08-05 14:05:05 +00:00
|
|
|
const bobResponseText = 'Oh, hello Alice!';
|
|
|
|
final bobResponseMessage = await bobSession.encryptToJid(
|
|
|
|
aliceJid,
|
|
|
|
bobResponseText,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Bob sends the message to Alice
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Alice decrypts it
|
|
|
|
final aliceReceivedMessage = await aliceSession.decryptMessage(
|
|
|
|
bobResponseMessage.ciphertext,
|
|
|
|
bobJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceId(),
|
2022-08-05 14:05:05 +00:00
|
|
|
bobResponseMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-05 14:05:05 +00:00
|
|
|
);
|
|
|
|
expect(bobResponseText, aliceReceivedMessage);
|
|
|
|
});
|
|
|
|
|
2023-06-12 17:20:43 +00:00
|
|
|
test('Test using OMEMO sessions with only two devices for the receiver',
|
|
|
|
() async {
|
2022-08-05 14:05:05 +00:00
|
|
|
const aliceJid = 'alice@server.example';
|
|
|
|
const bobJid = 'bob@other.server.example';
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-05 14:05:05 +00:00
|
|
|
// Alice and Bob generate their sessions
|
2022-08-08 16:03:05 +00:00
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-05 14:05:05 +00:00
|
|
|
// Bob's other device
|
2022-08-08 16:03:05 +00:00
|
|
|
final bobSession2 = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-05 14:05:05 +00:00
|
|
|
|
|
|
|
// Alice encrypts a message for Bob
|
|
|
|
const messagePlaintext = 'Hello Bob!';
|
|
|
|
final aliceMessage = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
messagePlaintext,
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
|
|
|
await bobSession2.getDeviceBundle(),
|
2022-08-05 14:05:05 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
expect(aliceMessage.encryptedKeys.length, 2);
|
|
|
|
expect(aliceMessage.encryptedKeys[0].kex, true);
|
|
|
|
expect(aliceMessage.encryptedKeys[1].kex, true);
|
|
|
|
|
|
|
|
// Alice sends the message to Bob
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Bob decrypts it
|
|
|
|
final bobMessage = await bobSession.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-05 14:05:05 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-05 14:05:05 +00:00
|
|
|
);
|
|
|
|
expect(messagePlaintext, bobMessage);
|
|
|
|
|
2022-10-22 10:41:41 +00:00
|
|
|
// Ratchets are acked
|
2023-06-12 17:20:43 +00:00
|
|
|
await aliceSession.ratchetAcknowledged(
|
|
|
|
bobJid, await bobSession.getDeviceId());
|
|
|
|
await bobSession.ratchetAcknowledged(
|
|
|
|
aliceJid, await aliceSession.getDeviceId());
|
|
|
|
|
2022-08-05 14:05:05 +00:00
|
|
|
// Bob responds to Alice
|
|
|
|
const bobResponseText = 'Oh, hello Alice!';
|
2022-08-05 13:53:16 +00:00
|
|
|
final bobResponseMessage = await bobSession.encryptToJid(
|
|
|
|
aliceJid,
|
|
|
|
bobResponseText,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Bob sends the message to Alice
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Alice decrypts it
|
|
|
|
final aliceReceivedMessage = await aliceSession.decryptMessage(
|
|
|
|
bobResponseMessage.ciphertext,
|
|
|
|
bobJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceId(),
|
2022-08-05 13:53:16 +00:00
|
|
|
bobResponseMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-05 13:53:16 +00:00
|
|
|
);
|
|
|
|
expect(bobResponseText, aliceReceivedMessage);
|
2022-08-08 12:26:25 +00:00
|
|
|
|
|
|
|
// Alice checks the fingerprints
|
|
|
|
final fingerprints = await aliceSession.getHexFingerprintsForJid(bobJid);
|
|
|
|
// Check that they the fingerprints are correct
|
|
|
|
expect(fingerprints.length, 2);
|
|
|
|
expect(fingerprints[0] != fingerprints[1], true);
|
|
|
|
// Check that those two calls do not throw an exception
|
|
|
|
aliceSession
|
|
|
|
..getRatchet(bobJid, fingerprints[0].deviceId)
|
|
|
|
..getRatchet(bobJid, fingerprints[1].deviceId);
|
2022-08-04 14:30:09 +00:00
|
|
|
});
|
2022-08-08 12:44:05 +00:00
|
|
|
|
|
|
|
test('Test using OMEMO sessions with encrypt to self', () async {
|
|
|
|
const aliceJid = 'alice@server.example';
|
|
|
|
const bobJid = 'bob@other.server.example';
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-08 12:44:05 +00:00
|
|
|
// Alice and Bob generate their sessions
|
2022-08-08 16:03:05 +00:00
|
|
|
final aliceSession1 = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final aliceSession2 = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-08 12:44:05 +00:00
|
|
|
|
|
|
|
// Alice encrypts a message for Bob
|
|
|
|
const messagePlaintext = 'Hello Bob!';
|
|
|
|
final aliceMessage = await aliceSession1.encryptToJids(
|
|
|
|
[bobJid, aliceJid],
|
|
|
|
messagePlaintext,
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
|
|
|
await aliceSession2.getDeviceBundle(),
|
2022-08-08 12:44:05 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
expect(aliceMessage.encryptedKeys.length, 2);
|
|
|
|
|
|
|
|
// Alice sends the message to Bob
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Bob decrypts it
|
|
|
|
final bobMessage = await bobSession.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession1.getDeviceId(),
|
2022-08-08 12:44:05 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-08 12:44:05 +00:00
|
|
|
);
|
|
|
|
expect(messagePlaintext, bobMessage);
|
|
|
|
|
|
|
|
// Alice's other device decrypts it
|
|
|
|
final aliceMessage2 = await aliceSession2.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession1.getDeviceId(),
|
2022-08-08 12:44:05 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-08 12:44:05 +00:00
|
|
|
);
|
|
|
|
expect(messagePlaintext, aliceMessage2);
|
|
|
|
});
|
2022-08-08 12:59:46 +00:00
|
|
|
|
2022-09-14 21:50:54 +00:00
|
|
|
test('Test sending empty OMEMO messages', () async {
|
2022-08-08 12:59:46 +00:00
|
|
|
const aliceJid = 'alice@server.example';
|
|
|
|
const bobJid = 'bob@other.server.example';
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-08 12:59:46 +00:00
|
|
|
// Alice and Bob generate their sessions
|
2022-08-08 16:03:05 +00:00
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-08 12:59:46 +00:00
|
|
|
|
|
|
|
// Alice encrypts a message for Bob
|
|
|
|
final aliceMessage = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
null,
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-08 12:59:46 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
expect(aliceMessage.encryptedKeys.length, 1);
|
|
|
|
expect(aliceMessage.ciphertext, null);
|
|
|
|
|
|
|
|
// Alice sends the message to Bob
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Bob decrypts it
|
|
|
|
final bobMessage = await bobSession.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-08 12:59:46 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-08 12:59:46 +00:00
|
|
|
);
|
|
|
|
expect(bobMessage, null);
|
|
|
|
|
|
|
|
// This call must not cause an exception
|
2022-08-18 13:30:31 +00:00
|
|
|
bobSession.getRatchet(aliceJid, await aliceSession.getDeviceId());
|
2022-08-08 12:59:46 +00:00
|
|
|
});
|
2022-08-08 13:09:44 +00:00
|
|
|
|
|
|
|
test('Test rotating the Signed Prekey', () async {
|
|
|
|
// Generate the session
|
|
|
|
const aliceJid = 'alice@some.server';
|
2022-08-08 16:03:05 +00:00
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-08 13:09:44 +00:00
|
|
|
|
|
|
|
// Setup an event listener
|
|
|
|
final oldDevice = await aliceSession.getDevice();
|
2023-01-01 15:49:19 +00:00
|
|
|
OmemoDevice? newDevice;
|
2022-08-08 13:09:44 +00:00
|
|
|
aliceSession.eventStream.listen((event) {
|
|
|
|
if (event is DeviceModifiedEvent) {
|
|
|
|
newDevice = event.device;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Rotate the Signed Prekey
|
|
|
|
await aliceSession.rotateSignedPrekey();
|
|
|
|
|
|
|
|
// Just for safety...
|
|
|
|
await Future<void>.delayed(const Duration(seconds: 2));
|
|
|
|
|
|
|
|
expect(await oldDevice.equals(newDevice!), false);
|
|
|
|
expect(await newDevice!.equals(await aliceSession.getDevice()), true);
|
2022-08-08 13:32:08 +00:00
|
|
|
|
|
|
|
expect(await newDevice!.oldSpk!.equals(oldDevice.spk), true);
|
|
|
|
expect(newDevice!.oldSpkId, oldDevice.spkId);
|
2022-08-08 13:09:44 +00:00
|
|
|
});
|
2022-08-08 13:48:09 +00:00
|
|
|
|
|
|
|
test('Test accepting a session with an old SPK', () async {
|
|
|
|
const aliceJid = 'alice@server.example';
|
|
|
|
const bobJid = 'bob@other.server.example';
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-08 13:48:09 +00:00
|
|
|
// Alice and Bob generate their sessions
|
2022-08-08 16:03:05 +00:00
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-08 13:48:09 +00:00
|
|
|
|
|
|
|
// Alice encrypts a message for Bob
|
|
|
|
const messagePlaintext = 'Hello Bob!';
|
|
|
|
final aliceMessage = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
messagePlaintext,
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-08 13:48:09 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
expect(aliceMessage.encryptedKeys.length, 1);
|
|
|
|
|
|
|
|
// Alice loses her Internet connection. Bob rotates his SPK.
|
|
|
|
await bobSession.rotateSignedPrekey();
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-08 13:48:09 +00:00
|
|
|
// Alice regains her Internet connection and sends the message to Bob
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Bob decrypts it
|
|
|
|
final bobMessage = await bobSession.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-08 13:48:09 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-08 13:48:09 +00:00
|
|
|
);
|
|
|
|
expect(messagePlaintext, bobMessage);
|
|
|
|
});
|
2022-08-09 12:45:04 +00:00
|
|
|
|
|
|
|
test('Test trust bypassing with empty OMEMO messages', () async {
|
|
|
|
const aliceJid = 'alice@server.example';
|
|
|
|
const bobJid = 'bob@other.server.example';
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-09 12:45:04 +00:00
|
|
|
// Alice and Bob generate their sessions
|
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
NeverTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
NeverTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice encrypts an empty message for Bob
|
|
|
|
final aliceMessage = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
null,
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-09 12:45:04 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
// Despite Alice not trusting Bob's device, we should have encrypted it for his
|
|
|
|
// untrusted device.
|
|
|
|
expect(aliceMessage.encryptedKeys.length, 1);
|
2022-08-11 10:02:33 +00:00
|
|
|
});
|
2022-08-10 15:24:35 +00:00
|
|
|
|
|
|
|
test('Test by sending multiple messages back and forth', () 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: 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice encrypts a message for Bob
|
|
|
|
final aliceMessage = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
'Hello Bob!',
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-10 15:24:35 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice sends the message to Bob
|
|
|
|
// ...
|
|
|
|
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-10 15:24:35 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-10 15:24:35 +00:00
|
|
|
);
|
|
|
|
|
2022-10-22 10:41:41 +00:00
|
|
|
// Ratchets are acked
|
2023-06-12 17:20:43 +00:00
|
|
|
await aliceSession.ratchetAcknowledged(
|
|
|
|
bobJid, await bobSession.getDeviceId());
|
|
|
|
await bobSession.ratchetAcknowledged(
|
|
|
|
aliceJid, await aliceSession.getDeviceId());
|
|
|
|
|
2022-08-10 15:24:35 +00:00
|
|
|
for (var i = 0; i < 100; i++) {
|
|
|
|
final messageText = 'Test Message #$i';
|
|
|
|
// Bob responds to Alice
|
|
|
|
final bobResponseMessage = await bobSession.encryptToJid(
|
|
|
|
aliceJid,
|
|
|
|
messageText,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Bob sends the message to Alice
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Alice decrypts it
|
|
|
|
final aliceReceivedMessage = await aliceSession.decryptMessage(
|
|
|
|
bobResponseMessage.ciphertext,
|
|
|
|
bobJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceId(),
|
2022-08-10 15:24:35 +00:00
|
|
|
bobResponseMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-10 15:24:35 +00:00
|
|
|
);
|
|
|
|
expect(messageText, aliceReceivedMessage);
|
|
|
|
}
|
|
|
|
});
|
2022-08-16 11:27:21 +00:00
|
|
|
|
|
|
|
group('Test removing a ratchet', () {
|
|
|
|
test('Test removing a ratchet when the user has multiple', () async {
|
|
|
|
const aliceJid = 'alice@server.local';
|
|
|
|
const bobJid = 'bob@some.server.local';
|
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession1 = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession2 = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice sends a message to those two Bobs
|
2022-08-16 11:46:08 +00:00
|
|
|
await aliceSession.encryptToJid(
|
2022-08-16 11:27:21 +00:00
|
|
|
bobJid,
|
|
|
|
'Hallo Welt',
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession1.getDeviceBundle(),
|
|
|
|
await bobSession2.getDeviceBundle(),
|
2022-08-16 11:27:21 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
// One of those two sessions is broken, so Alice removes the session2 ratchet
|
2022-08-18 13:30:31 +00:00
|
|
|
final id1 = await bobSession1.getDeviceId();
|
|
|
|
final id2 = await bobSession2.getDeviceId();
|
2022-08-16 11:27:21 +00:00
|
|
|
await aliceSession.removeRatchet(bobJid, id1);
|
|
|
|
|
2022-08-16 11:46:08 +00:00
|
|
|
final map = aliceSession.getRatchetMap();
|
2022-08-16 11:27:21 +00:00
|
|
|
expect(map.containsKey(RatchetMapKey(bobJid, id1)), false);
|
|
|
|
expect(map.containsKey(RatchetMapKey(bobJid, id2)), true);
|
|
|
|
final deviceMap = await aliceSession.getDeviceMap();
|
|
|
|
expect(deviceMap.containsKey(bobJid), true);
|
|
|
|
expect(deviceMap[bobJid], [id2]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Test removing a ratchet when the user has only one', () async {
|
|
|
|
const aliceJid = 'alice@server.local';
|
|
|
|
const bobJid = 'bob@some.server.local';
|
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice sends a message to those two Bobs
|
2022-08-16 11:46:08 +00:00
|
|
|
await aliceSession.encryptToJid(
|
2022-08-16 11:27:21 +00:00
|
|
|
bobJid,
|
|
|
|
'Hallo Welt',
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-16 11:27:21 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
// One of those two sessions is broken, so Alice removes the session2 ratchet
|
2022-08-18 13:30:31 +00:00
|
|
|
final id = await bobSession.getDeviceId();
|
2022-08-16 11:27:21 +00:00
|
|
|
await aliceSession.removeRatchet(bobJid, id);
|
|
|
|
|
2022-08-16 11:46:08 +00:00
|
|
|
final map = aliceSession.getRatchetMap();
|
2022-08-16 11:27:21 +00:00
|
|
|
expect(map.containsKey(RatchetMapKey(bobJid, id)), false);
|
|
|
|
final deviceMap = await aliceSession.getDeviceMap();
|
|
|
|
expect(deviceMap.containsKey(bobJid), false);
|
|
|
|
});
|
|
|
|
});
|
2022-08-16 12:02:04 +00:00
|
|
|
|
|
|
|
test('Test acknowledging a ratchet', () 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: 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice sends Bob a message
|
|
|
|
await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
'Hallo Welt',
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-16 12:02:04 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
await aliceSession.getUnacknowledgedRatchets(bobJid),
|
|
|
|
[
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceId(),
|
2022-08-16 12:02:04 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
// Bob sends alice an empty message
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Alice decrypts it
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Alice marks the ratchet as acknowledged
|
2023-06-12 17:20:43 +00:00
|
|
|
await aliceSession.ratchetAcknowledged(
|
|
|
|
bobJid, await bobSession.getDeviceId());
|
2022-08-16 12:02:04 +00:00
|
|
|
expect(
|
2022-08-18 14:21:53 +00:00
|
|
|
(await aliceSession.getUnacknowledgedRatchets(bobJid))!.isEmpty,
|
2022-08-16 12:02:04 +00:00
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
2022-08-18 13:20:32 +00:00
|
|
|
|
|
|
|
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: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-18 13:20:32 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg1.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-18 13:20:32 +00:00
|
|
|
msg1.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
0,
|
2022-08-18 13:20:32 +00:00
|
|
|
);
|
|
|
|
final aliceRatchet1 = aliceSession.getRatchet(
|
|
|
|
bobJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceId(),
|
2022-08-18 13:20:32 +00:00
|
|
|
);
|
|
|
|
final bobRatchet1 = bobSession.getRatchet(
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-18 13:20:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-18 13:20:32 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg2.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-18 13:20:32 +00:00
|
|
|
msg2.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
getTimestamp(),
|
2022-08-18 13:20:32 +00:00
|
|
|
);
|
|
|
|
final aliceRatchet2 = aliceSession.getRatchet(
|
|
|
|
bobJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceId(),
|
2022-08-18 13:20:32 +00:00
|
|
|
);
|
|
|
|
final bobRatchet2 = bobSession.getRatchet(
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-18 13:20:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Both should only have one ratchet
|
|
|
|
expect(aliceSession.getRatchetMap().length, 1);
|
|
|
|
expect(bobSession.getRatchetMap().length, 1);
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-08-18 13:20:32 +00:00
|
|
|
// The ratchets should both be different
|
|
|
|
expect(await aliceRatchet1.equals(aliceRatchet2), false);
|
|
|
|
expect(await bobRatchet1.equals(bobRatchet2), false);
|
|
|
|
});
|
2022-09-14 19:58:41 +00:00
|
|
|
|
2022-10-22 10:41:41 +00:00
|
|
|
test('Test resending key exchanges', () 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(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
// The first message should be a kex message
|
|
|
|
expect(msg1.encryptedKeys.first.kex, true);
|
|
|
|
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg1.ciphertext,
|
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
|
|
|
msg1.encryptedKeys,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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?",
|
|
|
|
);
|
|
|
|
expect(msg2.encryptedKeys.first.kex, true);
|
|
|
|
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg2.ciphertext,
|
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
|
|
|
msg2.encryptedKeys,
|
|
|
|
getTimestamp(),
|
|
|
|
);
|
|
|
|
});
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-10-02 12:56:20 +00:00
|
|
|
test('Test receiving old messages including a KEX', () async {
|
2022-09-14 19:58:41 +00:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
|
2022-10-02 12:56:20 +00:00
|
|
|
final bobsReceivedMessages = List<EncryptionResult>.empty(growable: true);
|
2022-10-02 17:23:58 +00:00
|
|
|
final bobsReceivedMessagesTimestamps = List<int>.empty(growable: true);
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-09-14 19:58:41 +00:00
|
|
|
// Alice sends Bob a message
|
|
|
|
final msg1 = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
'Hallo Welt',
|
|
|
|
newSessions: [
|
|
|
|
await bobSession.getDeviceBundle(),
|
|
|
|
],
|
|
|
|
);
|
2022-10-02 12:56:20 +00:00
|
|
|
bobsReceivedMessages.add(msg1);
|
2022-10-02 17:23:58 +00:00
|
|
|
final t1 = getTimestamp();
|
|
|
|
bobsReceivedMessagesTimestamps.add(t1);
|
|
|
|
|
2022-09-14 19:58:41 +00:00
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg1.ciphertext,
|
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
|
|
|
msg1.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
t1,
|
2022-09-14 19:58:41 +00:00
|
|
|
);
|
|
|
|
|
2022-10-22 10:41:41 +00:00
|
|
|
// Ratchets are acked
|
2023-06-12 17:20:43 +00:00
|
|
|
await aliceSession.ratchetAcknowledged(
|
|
|
|
bobJid, await bobSession.getDeviceId());
|
|
|
|
await bobSession.ratchetAcknowledged(
|
|
|
|
aliceJid, await aliceSession.getDeviceId());
|
|
|
|
|
2022-09-14 19:58:41 +00:00
|
|
|
// Bob responds
|
|
|
|
final msg2 = await bobSession.encryptToJid(
|
|
|
|
aliceJid,
|
|
|
|
'Hello!',
|
|
|
|
);
|
2022-10-02 17:23:58 +00:00
|
|
|
|
2022-09-14 19:58:41 +00:00
|
|
|
await aliceSession.decryptMessage(
|
|
|
|
msg2.ciphertext,
|
|
|
|
bobJid,
|
|
|
|
await bobSession.getDeviceId(),
|
|
|
|
msg2.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
getTimestamp(),
|
2022-09-14 19:58:41 +00:00
|
|
|
);
|
2023-06-12 17:20:43 +00:00
|
|
|
|
2022-10-02 12:56:20 +00:00
|
|
|
// Send some messages between the two
|
|
|
|
for (var i = 0; i < 100; i++) {
|
|
|
|
final msg = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
'Hello $i',
|
|
|
|
);
|
|
|
|
bobsReceivedMessages.add(msg);
|
2022-10-02 17:23:58 +00:00
|
|
|
final t = getTimestamp();
|
|
|
|
bobsReceivedMessagesTimestamps.add(t);
|
2022-10-02 12:56:20 +00:00
|
|
|
final result = await bobSession.decryptMessage(
|
|
|
|
msg.ciphertext,
|
2022-09-14 19:58:41 +00:00
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
2022-10-02 12:56:20 +00:00
|
|
|
msg.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
t,
|
2022-09-14 19:58:41 +00:00
|
|
|
);
|
|
|
|
|
2022-10-02 12:56:20 +00:00
|
|
|
expect(result, 'Hello $i');
|
|
|
|
}
|
2022-09-14 19:58:41 +00:00
|
|
|
|
2022-10-02 12:56:20 +00:00
|
|
|
// Due to some issue with the transport protocol, the messages to Bob are received
|
|
|
|
// again.
|
|
|
|
final ratchetPreError = bobSession
|
2023-06-12 17:20:43 +00:00
|
|
|
.getRatchet(aliceJid, await aliceSession.getDeviceId())
|
|
|
|
.clone();
|
2022-10-02 17:23:58 +00:00
|
|
|
var invalidKex = 0;
|
2022-10-02 12:56:20 +00:00
|
|
|
var errorCounter = 0;
|
2022-10-02 17:23:58 +00:00
|
|
|
for (var i = 0; i < bobsReceivedMessages.length; i++) {
|
|
|
|
final msg = bobsReceivedMessages[i];
|
2022-10-02 12:56:20 +00:00
|
|
|
try {
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg.ciphertext,
|
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
|
|
|
msg.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
bobsReceivedMessagesTimestamps[i],
|
2022-10-02 12:56:20 +00:00
|
|
|
);
|
|
|
|
expect(true, false);
|
2022-10-02 17:23:58 +00:00
|
|
|
} on InvalidMessageHMACException catch (_) {
|
2022-10-02 15:03:39 +00:00
|
|
|
errorCounter++;
|
|
|
|
} on InvalidKeyExchangeException catch (_) {
|
2022-10-02 17:23:58 +00:00
|
|
|
invalidKex++;
|
2022-10-02 12:56:20 +00:00
|
|
|
}
|
2022-09-14 19:58:41 +00:00
|
|
|
}
|
2022-10-02 12:56:20 +00:00
|
|
|
final ratchetPostError = bobSession
|
2023-06-12 17:20:43 +00:00
|
|
|
.getRatchet(aliceJid, await aliceSession.getDeviceId())
|
|
|
|
.clone();
|
2022-09-14 19:58:41 +00:00
|
|
|
|
2022-10-02 12:56:20 +00:00
|
|
|
// The 100 messages including the initial KEX message
|
2022-10-02 17:23:58 +00:00
|
|
|
expect(invalidKex, 1);
|
|
|
|
expect(errorCounter, 100);
|
2022-10-02 12:56:20 +00:00
|
|
|
expect(await ratchetPreError.equals(ratchetPostError), true);
|
|
|
|
|
2022-09-14 19:58:41 +00:00
|
|
|
final msg3 = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
'Are you okay?',
|
|
|
|
);
|
|
|
|
final result = await bobSession.decryptMessage(
|
|
|
|
msg3.ciphertext,
|
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
|
|
|
msg3.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
104,
|
2022-09-14 19:58:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(result, 'Are you okay?');
|
|
|
|
});
|
2022-10-22 10:41:41 +00:00
|
|
|
|
|
|
|
test("Test ignoring a new KEX when we haven't acket it yet", () 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: 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice sends Bob a message
|
|
|
|
final msg1 = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
'Hallo Welt',
|
|
|
|
newSessions: [
|
|
|
|
await bobSession.getDeviceBundle(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
expect(msg1.encryptedKeys.first.kex, true);
|
|
|
|
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg1.ciphertext,
|
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
|
|
|
msg1.encryptedKeys,
|
|
|
|
getTimestamp(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Alice sends another message before the ack can reach us
|
|
|
|
final msg2 = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
'ANSWER ME!',
|
|
|
|
);
|
|
|
|
expect(msg2.encryptedKeys.first.kex, true);
|
|
|
|
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg2.ciphertext,
|
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
|
|
|
msg2.encryptedKeys,
|
|
|
|
getTimestamp(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Now the acks reach us
|
2023-06-12 17:20:43 +00:00
|
|
|
await aliceSession.ratchetAcknowledged(
|
|
|
|
bobJid, await bobSession.getDeviceId());
|
|
|
|
await bobSession.ratchetAcknowledged(
|
|
|
|
aliceJid, await aliceSession.getDeviceId());
|
2022-10-22 10:41:41 +00:00
|
|
|
|
|
|
|
// Alice sends another message
|
|
|
|
final msg3 = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
"You read the message, didn't you?",
|
|
|
|
);
|
|
|
|
expect(msg3.encryptedKeys.first.kex, false);
|
|
|
|
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
msg3.ciphertext,
|
|
|
|
aliceJid,
|
|
|
|
await aliceSession.getDeviceId(),
|
|
|
|
msg3.encryptedKeys,
|
|
|
|
getTimestamp(),
|
|
|
|
);
|
|
|
|
|
|
|
|
for (var i = 0; i < 100; i++) {
|
|
|
|
final messageText = 'Test Message #$i';
|
|
|
|
// Bob responds to Alice
|
|
|
|
final bobResponseMessage = await bobSession.encryptToJid(
|
|
|
|
aliceJid,
|
|
|
|
messageText,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Bob sends the message to Alice
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Alice decrypts it
|
|
|
|
final aliceReceivedMessage = await aliceSession.decryptMessage(
|
|
|
|
bobResponseMessage.ciphertext,
|
|
|
|
bobJid,
|
|
|
|
await bobSession.getDeviceId(),
|
|
|
|
bobResponseMessage.encryptedKeys,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
expect(messageText, aliceReceivedMessage);
|
|
|
|
}
|
|
|
|
});
|
2022-08-04 14:30:09 +00:00
|
|
|
}
|