2022-08-04 14:30:09 +00:00
|
|
|
import 'package:omemo_dart/omemo_dart.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
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-04 14:30:09 +00:00
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
|
2022-08-05 14:52:02 +00:00
|
|
|
final bobOpks = (await bobSession.getDevice()).opks.values.toList();
|
|
|
|
bobSession.eventStream.listen((event) {
|
|
|
|
if (event is DeviceBundleModifiedEvent) {
|
|
|
|
deviceModified = true;
|
|
|
|
}
|
|
|
|
});
|
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-05 14:52:02 +00:00
|
|
|
await (await bobSession.getDevice()).toBundle(),
|
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);
|
|
|
|
|
|
|
|
// Alice sends the message to Bob
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// Bob decrypts it
|
|
|
|
final bobMessage = await bobSession.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-05 14:52:02 +00:00
|
|
|
(await aliceSession.getDevice()).id,
|
2022-08-05 11:34:42 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-08-04 14:30:09 +00:00
|
|
|
);
|
|
|
|
expect(messagePlaintext, bobMessage);
|
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
|
|
|
|
|
|
|
// 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-05 14:52:02 +00:00
|
|
|
(await bobSession.getDevice()).id,
|
2022-08-05 14:05:05 +00:00
|
|
|
bobResponseMessage.encryptedKeys,
|
|
|
|
);
|
|
|
|
expect(bobResponseText, aliceReceivedMessage);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Test using OMEMO sessions with only two devices for the receiver', () async {
|
|
|
|
const aliceJid = 'alice@server.example';
|
|
|
|
const bobJid = 'bob@other.server.example';
|
|
|
|
|
|
|
|
// Alice and Bob generate their sessions
|
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
|
|
|
|
// Bob's other device
|
|
|
|
final bobSession2 = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
|
|
|
|
|
|
|
|
// Alice encrypts a message for Bob
|
|
|
|
const messagePlaintext = 'Hello Bob!';
|
|
|
|
final aliceMessage = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
messagePlaintext,
|
|
|
|
newSessions: [
|
2022-08-05 14:52:02 +00:00
|
|
|
await (await bobSession.getDevice()).toBundle(),
|
|
|
|
await (await bobSession2.getDevice()).toBundle(),
|
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-05 14:52:02 +00:00
|
|
|
(await aliceSession.getDevice()).id,
|
2022-08-05 14:05:05 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
|
|
|
);
|
|
|
|
expect(messagePlaintext, bobMessage);
|
|
|
|
|
|
|
|
// 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-05 14:52:02 +00:00
|
|
|
(await bobSession.getDevice()).id,
|
2022-08-05 13:53:16 +00:00
|
|
|
bobResponseMessage.encryptedKeys,
|
|
|
|
);
|
|
|
|
expect(bobResponseText, aliceReceivedMessage);
|
2022-08-04 14:30:09 +00:00
|
|
|
});
|
|
|
|
}
|