omemo_dart/test/omemo_test.dart

50 lines
1.4 KiB
Dart
Raw Normal View History

2022-08-04 14:30:09 +00:00
import 'dart:convert';
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
final aliceSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
final bobSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
// Perform the X3DH
2022-08-04 14:57:12 +00:00
final kex = await aliceSession.addSessionFromBundle(
2022-08-04 14:42:12 +00:00
bobJid,
bobSession.device.id,
2022-08-04 14:30:09 +00:00
await bobSession.device.toBundle(),
);
2022-08-04 14:42:12 +00:00
await bobSession.addSessionFromKeyExchange(
aliceJid,
aliceSession.device.id,
2022-08-04 14:57:12 +00:00
kex,
2022-08-04 14:30:09 +00:00
);
// Alice encrypts a message for Bob
const messagePlaintext = 'Hello Bob!';
final aliceMessage = await aliceSession.encryptToJid(bobJid, messagePlaintext);
expect(aliceMessage.encryptedKeys.length, 1);
// Alice sends the message to Bob
// ...
// Bob decrypts it
final bobMessage = await bobSession.decryptMessage(
aliceMessage.ciphertext,
aliceJid,
aliceSession.device.id,
[
EncryptedKey(
bobSession.device.id,
base64.encode(aliceMessage.encryptedKeys[bobSession.device.id]!),
),
],
);
expect(messagePlaintext, bobMessage);
});
}