From 8c2d7d78d885879a15acd66cd9d097642204b77f Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 12 Dec 2022 05:23:38 +0100 Subject: [PATCH] chore: use proper matchers in integration tests --- .gitignore | 1 + lib/encryption/key_manager.dart | 8 + test_driver/matrixsdk_test.dart | 618 +++++++++++++++++--------------- 3 files changed, 345 insertions(+), 282 deletions(-) diff --git a/.gitignore b/.gitignore index f0e9a7062..68373a03c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.swo *.swn *.dylib +*.bakmacoscompat .DS_Store .atom/ .buildlog/ diff --git a/lib/encryption/key_manager.dart b/lib/encryption/key_manager.dart index 786a76b5d..56b2485a9 100644 --- a/lib/encryption/key_manager.dart +++ b/lib/encryption/key_manager.dart @@ -382,6 +382,14 @@ class KeyManager { .where((e) => !e.value) .map((e) => e.key)) : {}; + + // check if a device got removed + if (oldDeviceIds.difference(newDeviceIds).isNotEmpty) { + wipe = true; + break; + } + + // check if any new devices need keys final newDevices = newDeviceIds.difference(oldDeviceIds); if (newDeviceIds.isNotEmpty) { devicesToReceive.addAll(newDeviceKeys.where( diff --git a/test_driver/matrixsdk_test.dart b/test_driver/matrixsdk_test.dart index a8a9be68d..7bad66808 100644 --- a/test_driver/matrixsdk_test.dart +++ b/test_driver/matrixsdk_test.dart @@ -16,14 +16,16 @@ * along with this program. If not, see . */ +import 'dart:io'; + import 'package:hive/hive.dart'; import 'package:olm/olm.dart' as olm; +import 'package:test/test.dart'; import 'package:matrix/matrix.dart'; import '../test/fake_database.dart'; import 'test_config.dart'; -void main() => test(); const String testMessage = 'Hello world'; const String testMessage2 = 'Hello moon'; const String testMessage3 = 'Hello sun'; @@ -31,324 +33,376 @@ const String testMessage4 = 'Hello star'; const String testMessage5 = 'Hello earth'; const String testMessage6 = 'Hello mars'; -void test() async { - Client? testClientA, testClientB; +void main() => group('Integration tests', () { + test('E2EE', () async { + Client? testClientA, testClientB; - try { - Hive.init(null); + try { + Hive.init(null); - await olm.init(); - olm.Account(); - Logs().i('[LibOlm] Enabled'); + await olm.init(); + olm.Account(); + Logs().i('[LibOlm] Enabled'); - Logs().i('++++ Login Alice at ++++'); - testClientA = Client('TestClientA', databaseBuilder: getDatabase); - await testClientA.checkHomeserver(Uri.parse(TestUser.homeserver)); - await testClientA.login(LoginType.mLoginPassword, - identifier: - AuthenticationUserIdentifier(user: TestUser.username.localpart!), - password: TestUser.password); - assert(testClientA.encryptionEnabled); + Logs().i('++++ Login Alice at ++++'); + testClientA = Client('TestClientA', databaseBuilder: getDatabase); + await testClientA.checkHomeserver(Uri.parse(TestUser.homeserver)); + await testClientA.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier( + user: TestUser.username.localpart!), + password: TestUser.password); + expect(testClientA.encryptionEnabled, true); - Logs().i('++++ Login Bob ++++'); - testClientB = Client('TestClientB', databaseBuilder: getDatabase); - await testClientB.checkHomeserver(Uri.parse(TestUser.homeserver)); - await testClientB.login(LoginType.mLoginPassword, - identifier: - AuthenticationUserIdentifier(user: TestUser.username2.localpart!), - password: TestUser.password2); - assert(testClientB.encryptionEnabled); + Logs().i('++++ Login Bob ++++'); + testClientB = Client('TestClientB', databaseBuilder: getDatabase); + await testClientB.checkHomeserver(Uri.parse(TestUser.homeserver)); + await testClientB.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier( + user: TestUser.username2.localpart!), + password: TestUser.password2); + expect(testClientB.encryptionEnabled, true); - Logs().i('++++ (Alice) Leave all rooms ++++'); - while (testClientA.rooms.isNotEmpty) { - final room = testClientA.rooms.first; - if (room.canonicalAlias.isNotEmpty) { - break; - } - try { - await room.leave(); - await room.forget(); - } catch (_) {} - } + Logs().i('++++ (Alice) Leave all rooms ++++'); + while (testClientA.rooms.isNotEmpty) { + final room = testClientA.rooms.first; + if (room.canonicalAlias.isNotEmpty) { + break; + } + try { + await room.leave(); + await room.forget(); + } catch (_) {} + } - Logs().i('++++ (Bob) Leave all rooms ++++'); - for (var i = 0; i < 3; i++) { - if (testClientB.rooms.isNotEmpty) { - final room = testClientB.rooms.first; - try { - await room.leave(); - await room.forget(); - } catch (_) {} - } - } + Logs().i('++++ (Bob) Leave all rooms ++++'); + for (var i = 0; i < 3; i++) { + if (testClientB.rooms.isNotEmpty) { + final room = testClientB.rooms.first; + try { + await room.leave(); + await room.forget(); + } catch (_) {} + } + } - Logs().i('++++ Check if own olm device is verified by default ++++'); - assert(testClientA.userDeviceKeys.containsKey(TestUser.username)); - assert(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys - .containsKey(testClientA.deviceID)); - assert(testClientA.userDeviceKeys[TestUser.username]! - .deviceKeys[testClientA.deviceID!]!.verified); - assert(!testClientA.userDeviceKeys[TestUser.username]! - .deviceKeys[testClientA.deviceID!]!.blocked); - assert(testClientB.userDeviceKeys.containsKey(TestUser.username2)); - assert(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys - .containsKey(testClientB.deviceID)); - assert(testClientB.userDeviceKeys[TestUser.username2]! - .deviceKeys[testClientB.deviceID!]!.verified); - assert(!testClientB.userDeviceKeys[TestUser.username2]! - .deviceKeys[testClientB.deviceID!]!.blocked); + Logs().i('++++ Check if own olm device is verified by default ++++'); + expect(testClientA.userDeviceKeys, contains(TestUser.username)); + expect(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys, + contains(testClientA.deviceID)); + expect( + testClientA.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.verified, + isTrue); + expect( + !testClientA.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.blocked, + isTrue); + expect(testClientB.userDeviceKeys, contains(TestUser.username2)); + expect(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys, + contains(testClientB.deviceID)); + expect( + testClientB.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.verified, + isTrue); + expect( + !testClientB.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.blocked, + isTrue); - Logs().i('++++ (Alice) Create room and invite Bob ++++'); - await testClientA.startDirectChat( - TestUser.username2, - enableEncryption: false, - ); - await Future.delayed(Duration(seconds: 1)); - final room = testClientA.rooms.first; - final roomId = room.id; + Logs().i('++++ (Alice) Create room and invite Bob ++++'); + await testClientA.startDirectChat( + TestUser.username2, + enableEncryption: false, + ); + await Future.delayed(Duration(seconds: 1)); + final room = testClientA.rooms.first; + final roomId = room.id; - Logs().i('++++ (Bob) Join room ++++'); - final inviteRoom = testClientB.getRoomById(roomId)!; - await inviteRoom.join(); - await Future.delayed(Duration(seconds: 1)); - assert(inviteRoom.membership == Membership.join); + Logs().i('++++ (Bob) Join room ++++'); + final inviteRoom = testClientB.getRoomById(roomId)!; + await inviteRoom.join(); + await Future.delayed(Duration(seconds: 1)); + expect(inviteRoom.membership, Membership.join); - Logs().i('++++ (Alice) Enable encryption ++++'); - assert(room.encrypted == false); - await room.enableEncryption(); - await Future.delayed(Duration(seconds: 5)); - assert(room.encrypted == true); - assert( - room.client.encryption!.keyManager.getOutboundGroupSession(room.id) == - null); + Logs().i('++++ (Alice) Enable encryption ++++'); + expect(room.encrypted, false); + await room.enableEncryption(); + await Future.delayed(Duration(seconds: 5)); + expect(room.encrypted, isTrue); + expect( + room.client.encryption!.keyManager + .getOutboundGroupSession(room.id), + null); - Logs().i('++++ (Alice) Check known olm devices ++++'); - assert(testClientA.userDeviceKeys.containsKey(TestUser.username2)); - assert(testClientA.userDeviceKeys[TestUser.username2]!.deviceKeys - .containsKey(testClientB.deviceID)); - assert(!testClientA.userDeviceKeys[TestUser.username2]! - .deviceKeys[testClientB.deviceID!]!.verified); - assert(!testClientA.userDeviceKeys[TestUser.username2]! - .deviceKeys[testClientB.deviceID!]!.blocked); - assert(testClientB.userDeviceKeys.containsKey(TestUser.username)); - assert(testClientB.userDeviceKeys[TestUser.username]!.deviceKeys - .containsKey(testClientA.deviceID)); - assert(!testClientB.userDeviceKeys[TestUser.username]! - .deviceKeys[testClientA.deviceID!]!.verified); - assert(!testClientB.userDeviceKeys[TestUser.username]! - .deviceKeys[testClientA.deviceID!]!.blocked); - await Future.wait([ - testClientA.updateUserDeviceKeys(), - testClientB.updateUserDeviceKeys(), - ]); - await testClientA - .userDeviceKeys[TestUser.username2]!.deviceKeys[testClientB.deviceID!]! - .setVerified(true); + Logs().i('++++ (Alice) Check known olm devices ++++'); + expect(testClientA.userDeviceKeys, contains(TestUser.username2)); + expect(testClientA.userDeviceKeys[TestUser.username2]!.deviceKeys, + contains(testClientB.deviceID)); + expect( + testClientA.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.verified, + isFalse); + expect( + testClientA.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.blocked, + isFalse); + expect(testClientB.userDeviceKeys, contains(TestUser.username)); + expect(testClientB.userDeviceKeys[TestUser.username]!.deviceKeys, + contains(testClientA.deviceID)); + expect( + testClientB.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.verified, + isFalse); + expect( + testClientB.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.blocked, + isFalse); + await Future.wait([ + testClientA.updateUserDeviceKeys(), + testClientB.updateUserDeviceKeys(), + ]); + await testClientA.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]! + .setVerified(true); - Logs().i('++++ Check if own olm device is verified by default ++++'); - assert(testClientA.userDeviceKeys.containsKey(TestUser.username)); - assert(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys - .containsKey(testClientA.deviceID)); - assert(testClientA.userDeviceKeys[TestUser.username]! - .deviceKeys[testClientA.deviceID!]!.verified); - assert(testClientB.userDeviceKeys.containsKey(TestUser.username2)); - assert(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys - .containsKey(testClientB.deviceID)); - assert(testClientB.userDeviceKeys[TestUser.username2]! - .deviceKeys[testClientB.deviceID!]!.verified); + Logs().i('++++ Check if own olm device is verified by default ++++'); + expect(testClientA.userDeviceKeys, contains(TestUser.username)); + expect(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys, + contains(testClientA.deviceID)); + expect( + testClientA.userDeviceKeys[TestUser.username]! + .deviceKeys[testClientA.deviceID!]!.verified, + isTrue); + expect(testClientB.userDeviceKeys, contains(TestUser.username2)); + expect(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys, + contains(testClientB.deviceID)); + expect( + testClientB.userDeviceKeys[TestUser.username2]! + .deviceKeys[testClientB.deviceID!]!.verified, + isTrue); - Logs().i("++++ (Alice) Send encrypted message: '$testMessage' ++++"); - await room.sendTextEvent(testMessage); - await Future.delayed(Duration(seconds: 5)); - assert( - room.client.encryption!.keyManager.getOutboundGroupSession(room.id) != - null); - var currentSessionIdA = room.client.encryption!.keyManager - .getOutboundGroupSession(room.id)! - .outboundGroupSession! - .session_id(); - /*assert(room.client.encryption.keyManager + Logs().i("++++ (Alice) Send encrypted message: '$testMessage' ++++"); + await room.sendTextEvent(testMessage); + await Future.delayed(Duration(seconds: 5)); + expect( + room.client.encryption!.keyManager + .getOutboundGroupSession(room.id), + isNotNull); + var currentSessionIdA = room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(); + /*expect(room.client.encryption.keyManager .getInboundGroupSession(room.id, currentSessionIdA, '') != null);*/ - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.length == - 1); - assert(testClientB.encryption!.olmManager - .olmSessions[testClientA.identityKey]!.length == - 1); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.first.sessionId == - testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]! - .first.sessionId); - /*assert(inviteRoom.client.encryption.keyManager + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length, + 1); + expect( + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length, + 1); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.first.sessionId, + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.first.sessionId); + /*expect(inviteRoom.client.encryption.keyManager .getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') != null);*/ - assert(room.lastEvent!.body == testMessage); - assert(inviteRoom.lastEvent!.body == testMessage); - Logs().i( - "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + expect(room.lastEvent!.body, testMessage); + expect(inviteRoom.lastEvent!.body, testMessage); + Logs().i( + "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); - Logs().i("++++ (Alice) Send again encrypted message: '$testMessage2' ++++"); - await room.sendTextEvent(testMessage2); - await Future.delayed(Duration(seconds: 5)); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.length == - 1); - assert(testClientB.encryption!.olmManager - .olmSessions[testClientA.identityKey]!.length == - 1); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.first.sessionId == - testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]! - .first.sessionId); + Logs().i( + "++++ (Alice) Send again encrypted message: '$testMessage2' ++++"); + await room.sendTextEvent(testMessage2); + await Future.delayed(Duration(seconds: 5)); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length, + 1); + expect( + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length, + 1); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.first.sessionId, + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.first.sessionId); - assert(room.client.encryption!.keyManager - .getOutboundGroupSession(room.id)! - .outboundGroupSession! - .session_id() == - currentSessionIdA); - /*assert(room.client.encryption.keyManager + expect( + room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(), + currentSessionIdA); + /*expect(room.client.encryption.keyManager .getInboundGroupSession(room.id, currentSessionIdA, '') != null);*/ - assert(room.lastEvent!.body == testMessage2); - assert(inviteRoom.lastEvent!.body == testMessage2); - Logs().i( - "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + expect(room.lastEvent!.body, testMessage2); + expect(inviteRoom.lastEvent!.body, testMessage2); + Logs().i( + "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); - Logs().i("++++ (Bob) Send again encrypted message: '$testMessage3' ++++"); - await inviteRoom.sendTextEvent(testMessage3); - await Future.delayed(Duration(seconds: 5)); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.length == - 1); - assert(testClientB.encryption!.olmManager - .olmSessions[testClientA.identityKey]!.length == - 1); - assert(room.client.encryption!.keyManager - .getOutboundGroupSession(room.id)! - .outboundGroupSession! - .session_id() == - currentSessionIdA); - final inviteRoomOutboundGroupSession = inviteRoom - .client.encryption!.keyManager - .getOutboundGroupSession(inviteRoom.id)!; + Logs().i( + "++++ (Bob) Send again encrypted message: '$testMessage3' ++++"); + await inviteRoom.sendTextEvent(testMessage3); + await Future.delayed(Duration(seconds: 5)); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length, + 1); + expect( + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length, + 1); + expect( + room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(), + currentSessionIdA); + final inviteRoomOutboundGroupSession = inviteRoom + .client.encryption!.keyManager + .getOutboundGroupSession(inviteRoom.id)!; - assert(inviteRoomOutboundGroupSession.isValid); - /*assert(inviteRoom.client.encryption.keyManager.getInboundGroupSession( + expect(inviteRoomOutboundGroupSession.isValid, isTrue); + /*expect(inviteRoom.client.encryption.keyManager.getInboundGroupSession( inviteRoom.id, inviteRoomOutboundGroupSession.outboundGroupSession.session_id(), '') != null); - assert(room.client.encryption.keyManager.getInboundGroupSession( + expect(room.client.encryption.keyManager.getInboundGroupSession( room.id, inviteRoomOutboundGroupSession.outboundGroupSession.session_id(), '') != null);*/ - assert(inviteRoom.lastEvent!.body == testMessage3); - assert(room.lastEvent!.body == testMessage3); - Logs().i( - "++++ (Alice) Received decrypted message: '${room.lastEvent!.body}' ++++"); + expect(inviteRoom.lastEvent!.body, testMessage3); + expect(room.lastEvent!.body, testMessage3); + Logs().i( + "++++ (Alice) Received decrypted message: '${room.lastEvent!.body}' ++++"); - Logs().i('++++ Login Bob in another client ++++'); - final testClientC = Client('TestClientC', databaseBuilder: getDatabase); - await testClientC.checkHomeserver(Uri.parse(TestUser.homeserver)); - // Workaround: Logging in with displayname instead of mxid - await testClientC.login(LoginType.mLoginPassword, - identifier: AuthenticationUserIdentifier(user: TestUser.displayname2), - password: TestUser.password2); - await Future.delayed(Duration(seconds: 3)); + Logs().i('++++ Login Bob in another client ++++'); + final testClientC = + Client('TestClientC', databaseBuilder: getDatabase); + await testClientC.checkHomeserver(Uri.parse(TestUser.homeserver)); + // We can't sign in using the displayname, since that breaks e2ee on dendrite: https://github.com/matrix-org/dendrite/issues/2914 + await testClientC.login(LoginType.mLoginPassword, + identifier: AuthenticationUserIdentifier( + user: TestUser.username2.localpart!), + password: TestUser.password2); + await Future.delayed(Duration(seconds: 3)); - Logs().i("++++ (Alice) Send again encrypted message: '$testMessage4' ++++"); - await room.sendTextEvent(testMessage4); - await Future.delayed(Duration(seconds: 5)); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.length == - 1); - assert(testClientB.encryption!.olmManager - .olmSessions[testClientA.identityKey]!.length == - 1); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.first.sessionId == - testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]! - .first.sessionId); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientC.identityKey]!.length == - 1); - assert(testClientC.encryption!.olmManager - .olmSessions[testClientA.identityKey]!.length == - 1); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientC.identityKey]!.first.sessionId == - testClientC.encryption!.olmManager.olmSessions[testClientA.identityKey]! - .first.sessionId); - assert(room.client.encryption!.keyManager - .getOutboundGroupSession(room.id)! - .outboundGroupSession! - .session_id() != - currentSessionIdA); - currentSessionIdA = room.client.encryption!.keyManager - .getOutboundGroupSession(room.id)! - .outboundGroupSession! - .session_id(); - /*assert(inviteRoom.client.encryption.keyManager + Logs().i( + "++++ (Alice) Send again encrypted message: '$testMessage4' ++++"); + await room.sendTextEvent(testMessage4); + await Future.delayed(Duration(seconds: 5)); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length, + 1); + expect( + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length, + 1); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.first.sessionId, + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.first.sessionId); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientC.identityKey]!.length, + 1); + expect( + testClientC.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length, + 1); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientC.identityKey]!.first.sessionId, + testClientC.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.first.sessionId); + expect( + room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(), + currentSessionIdA); + /*expect(inviteRoom.client.encryption.keyManager .getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') != null);*/ - assert(room.lastEvent!.body == testMessage4); - assert(inviteRoom.lastEvent!.body == testMessage4); - Logs().i( - "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + expect(room.lastEvent!.body, testMessage4); + expect(inviteRoom.lastEvent!.body, testMessage4); + Logs().i( + "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + + Logs() + .i('++++ Logout Bob another client ${testClientC.deviceID} ++++'); + await testClientC.dispose(closeDatabase: false); + await testClientC.logout(); + await Future.delayed(Duration(seconds: 5)); - Logs().i('++++ Logout Bob another client ++++'); - await testClientC.dispose(closeDatabase: false); - await testClientC.logout(); - await Future.delayed(Duration(seconds: 5)); + Logs().i( + "++++ (Alice) Send again encrypted message: '$testMessage6' ++++"); + await room.sendTextEvent(testMessage6); + await Future.delayed(Duration(seconds: 5)); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.length, + 1); + expect( + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.length, + 1); + expect( + testClientA.encryption!.olmManager + .olmSessions[testClientB.identityKey]!.first.sessionId, + testClientB.encryption!.olmManager + .olmSessions[testClientA.identityKey]!.first.sessionId); - Logs().i("++++ (Alice) Send again encrypted message: '$testMessage6' ++++"); - await room.sendTextEvent(testMessage6); - await Future.delayed(Duration(seconds: 5)); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.length == - 1); - assert(testClientB.encryption!.olmManager - .olmSessions[testClientA.identityKey]!.length == - 1); - assert(testClientA.encryption!.olmManager - .olmSessions[testClientB.identityKey]!.first.sessionId == - testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]! - .first.sessionId); - assert(room.client.encryption!.keyManager - .getOutboundGroupSession(room.id)! - .outboundGroupSession! - .session_id() != - currentSessionIdA); - currentSessionIdA = room.client.encryption!.keyManager - .getOutboundGroupSession(room.id)! - .outboundGroupSession! - .session_id(); - /*assert(inviteRoom.client.encryption.keyManager + // This does not work on conduit because of a server bug: https://gitlab.com/famedly/conduit/-/issues/325 + if (Platform.environment['HOMESERVER'] != 'conduit') { + expect( + room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(), + isNot(currentSessionIdA)); + } + currentSessionIdA = room.client.encryption!.keyManager + .getOutboundGroupSession(room.id)! + .outboundGroupSession! + .session_id(); + /*expect(inviteRoom.client.encryption.keyManager .getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') != null);*/ - assert(room.lastEvent!.body == testMessage6); - assert(inviteRoom.lastEvent!.body == testMessage6); - Logs().i( - "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); + expect(room.lastEvent!.body, testMessage6); + expect(inviteRoom.lastEvent!.body, testMessage6); + Logs().i( + "++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++"); - await room.leave(); - await room.forget(); - await inviteRoom.leave(); - await inviteRoom.forget(); - await Future.delayed(Duration(seconds: 1)); - } catch (e, s) { - Logs().e('Test failed', e, s); - rethrow; - } finally { - Logs().i('++++ Logout Alice and Bob ++++'); - if (testClientA?.isLogged() ?? false) await testClientA!.logoutAll(); - if (testClientA?.isLogged() ?? false) await testClientB!.logoutAll(); - await testClientA?.dispose(closeDatabase: false); - await testClientB?.dispose(closeDatabase: false); - testClientA = null; - testClientB = null; - } - return; -} + await room.leave(); + await room.forget(); + await inviteRoom.leave(); + await inviteRoom.forget(); + await Future.delayed(Duration(seconds: 1)); + } catch (e, s) { + Logs().e('Test failed', e, s); + rethrow; + } finally { + Logs().i('++++ Logout Alice and Bob ++++'); + if (testClientA?.isLogged() ?? false) await testClientA!.logoutAll(); + if (testClientA?.isLogged() ?? false) await testClientB!.logoutAll(); + await testClientA?.dispose(closeDatabase: false); + await testClientB?.dispose(closeDatabase: false); + testClientA = null; + testClientB = null; + } + return; + }); + }, timeout: Timeout(Duration(minutes: 6)));