test: Test 100 rounds of messages between the two ratchets

This commit is contained in:
PapaTutuWawa 2022-08-03 16:32:39 +02:00
parent 2fc2623092
commit 37155d2a87

View File

@ -74,7 +74,9 @@ void main() {
print('X3DH key exchange done'); print('X3DH key exchange done');
// Alice and Bob now share sk as a common secret and ad // Alice and Bob now share sk as a common secret and ad
// Build a session
final alicesRatchet = await OmemoDoubleRatchet.initiateNewSession( final alicesRatchet = await OmemoDoubleRatchet.initiateNewSession(
spkBob.pk, spkBob.pk,
resultAlice.sk, resultAlice.sk,
@ -87,38 +89,43 @@ void main() {
); );
expect(alicesRatchet.sessionAd, bobsRatchet.sessionAd); expect(alicesRatchet.sessionAd, bobsRatchet.sessionAd);
//expect(await alicesRatchet.dhr.getBytes(), await ikBob.pk.getBytes());
for (var i = 0; i < 100; i++) {
final messageText = 'Hello, dear $i';
if (i % 2 == 0) {
// Alice encrypts a message // Alice encrypts a message
final aliceRatchetResult1 = await alicesRatchet.ratchetEncrypt(utf8.encode('Hello Bob')); final aliceRatchetResult = await alicesRatchet.ratchetEncrypt(utf8.encode(messageText));
print('Alice sent the message'); print('Alice sent the message');
// Alice sends it to Bob // Alice sends it to Bob
// ... // ...
// Bob tries to decrypt it // Bob tries to decrypt it
final bobRatchetResult1 = await bobsRatchet.ratchetDecrypt( final bobRatchetResult = await bobsRatchet.ratchetDecrypt(
aliceRatchetResult1.header, aliceRatchetResult.header,
aliceRatchetResult1.ciphertext, aliceRatchetResult.ciphertext,
); );
print('Bob decrypted the message'); print('Bob decrypted the message');
expect(utf8.encode('Hello Bob'), bobRatchetResult1); expect(utf8.encode(messageText), bobRatchetResult);
} else {
// Bob sends a message to Alice // Bob sends a message to Alice
final bobRatchetResult2 = await bobsRatchet.ratchetEncrypt(utf8.encode('Hello Alice')); final bobRatchetResult = await bobsRatchet.ratchetEncrypt(utf8.encode(messageText));
print('Bob sent the message'); print('Bob sent the message');
// Bobs sends it to Alice // Bobs sends it to Alice
// ... // ...
// Alice tries to decrypt it // Alice tries to decrypt it
final aliceRatchetResult2 = await alicesRatchet.ratchetDecrypt( final aliceRatchetResult = await alicesRatchet.ratchetDecrypt(
bobRatchetResult2.header, bobRatchetResult.header,
bobRatchetResult2.ciphertext, bobRatchetResult.ciphertext,
); );
print('Alice decrypted the message'); print('Alice decrypted the message');
expect(utf8.encode('Hello Alice'), aliceRatchetResult2); expect(utf8.encode(messageText), aliceRatchetResult);
}
}
}); });
} }