From f2ec7bd759400fc7739a028723b5e9dc3a321c34 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Mon, 12 Jun 2023 19:13:38 +0200 Subject: [PATCH] fix: Initial receiving ratchet has no trust data --- lib/src/omemo/omemomanager.dart | 3 ++ test/omemomanager_test.dart | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/lib/src/omemo/omemomanager.dart b/lib/src/omemo/omemomanager.dart index c4070d3..99d18ef 100644 --- a/lib/src/omemo/omemomanager.dart +++ b/lib/src/omemo/omemomanager.dart @@ -203,6 +203,9 @@ class OmemoManager { getTimestamp(), ); + // Notify the trust manager + await trustManager.onNewSession(jid, deviceId); + return ratchet; } diff --git a/test/omemomanager_test.dart b/test/omemomanager_test.dart index 0424e78..e3e95a2 100644 --- a/test/omemomanager_test.dart +++ b/test/omemomanager_test.dart @@ -5,6 +5,15 @@ import 'package:omemo_dart/src/protobuf/omemo_key_exchange.dart'; import 'package:omemo_dart/src/trust/always.dart'; import 'package:test/test.dart'; +class TestingTrustManager extends AlwaysTrustingTrustManager { + final Map devices = {}; + + @override + Future onNewSession(String jid, int deviceId) async { + devices[jid] = deviceId; + } +} + void main() { Logger.root ..level = Level.ALL @@ -1350,4 +1359,68 @@ void main() { expect(bobResult4.error, null); expect(bobResult4.payload, 'Hi Bob'); }); + + test('Test correct trust behaviour on receiving', () async { + const aliceJid = 'alice@server1'; + const bobJid = 'bob@server2'; + + final aliceDevice = + await OmemoDevice.generateNewDevice(aliceJid, opkAmount: 1); + final bobDevice = await OmemoDevice.generateNewDevice(bobJid, opkAmount: 1); + + final aliceManager = OmemoManager( + aliceDevice, + AlwaysTrustingTrustManager(), + (result, recipientJid) async {}, + (jid) async { + expect(jid, bobJid); + return [bobDevice.id]; + }, + (jid, id) async { + expect(jid, bobJid); + return bobDevice.toBundle(); + }, + (jid) async {}, + ); + final bobManager = OmemoManager( + bobDevice, + TestingTrustManager(), + (result, recipientJid) async {}, + (jid) async { + expect(jid, aliceJid); + return [aliceDevice.id]; + }, + (jid, id) async { + expect(jid, aliceJid); + return aliceDevice.toBundle(); + }, + (jid) async {}, + ); + + // Alice sends Bob a message + final aliceResult1 = await aliceManager.onOutgoingStanza( + const OmemoOutgoingStanza( + [bobJid], + 'Hello World!', + ), + ); + + // Bob decrypts Alice's message + final bobResult1 = await bobManager.onIncomingStanza( + OmemoIncomingStanza( + aliceJid, + aliceDevice.id, + DateTime.now().millisecondsSinceEpoch, + aliceResult1.encryptedKeys, + base64.encode(aliceResult1.ciphertext!), + ), + ); + expect(bobResult1.error, null); + + // Bob should have some trust state + expect( + (bobManager.trustManager as TestingTrustManager).devices[aliceJid], + await aliceManager.getDeviceId(), + ); + }); }