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());
// Alice encrypts a message for (var i = 0; i < 100; i++) {
final aliceRatchetResult1 = await alicesRatchet.ratchetEncrypt(utf8.encode('Hello Bob')); final messageText = 'Hello, dear $i';
print('Alice sent the message');
// Alice sends it to Bob if (i % 2 == 0) {
// ... // Alice encrypts a message
final aliceRatchetResult = await alicesRatchet.ratchetEncrypt(utf8.encode(messageText));
print('Alice sent the message');
// Bob tries to decrypt it // Alice sends it to Bob
final bobRatchetResult1 = await bobsRatchet.ratchetDecrypt( // ...
aliceRatchetResult1.header,
aliceRatchetResult1.ciphertext,
);
print('Bob decrypted the message');
expect(utf8.encode('Hello Bob'), bobRatchetResult1); // Bob tries to decrypt it
final bobRatchetResult = await bobsRatchet.ratchetDecrypt(
aliceRatchetResult.header,
aliceRatchetResult.ciphertext,
);
print('Bob decrypted the message');
// Bob sends a message to Alice expect(utf8.encode(messageText), bobRatchetResult);
final bobRatchetResult2 = await bobsRatchet.ratchetEncrypt(utf8.encode('Hello Alice')); } else {
print('Bob sent the message'); // Bob sends a message to Alice
final bobRatchetResult = await bobsRatchet.ratchetEncrypt(utf8.encode(messageText));
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);
}
}
}); });
} }