omemo_dart/test/omemo_test.dart

54 lines
1.5 KiB
Dart
Raw Normal View History

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
final aliceSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
final bobSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
// Alice encrypts a message for Bob
const messagePlaintext = 'Hello Bob!';
final aliceMessage = await aliceSession.encryptToJid(
bobJid,
messagePlaintext,
newSession: await bobSession.device.toBundle(),
);
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,
aliceSession.device.id,
aliceMessage.encryptedKeys,
2022-08-04 14:30:09 +00:00
);
expect(messagePlaintext, bobMessage);
// Bob responds to Alice
final 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,
bobSession.device.id,
bobResponseMessage.encryptedKeys,
);
expect(bobResponseText, aliceReceivedMessage);
2022-08-04 14:30:09 +00:00
});
}