From 32c6e0ec6e0d42cae2e8488c6a99cd412188e108 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Thu, 28 Oct 2021 03:01:19 +0200 Subject: [PATCH] refactor: port client and related tests to nullsafety --- test/client_test.dart | 82 +++++++++++++++++++-------------- test/device_keys_list_test.dart | 75 +++++++++++++++--------------- test/matrix_database_test.dart | 19 ++++---- test/timeline_test.dart | 13 +++--- 4 files changed, 100 insertions(+), 89 deletions(-) diff --git a/test/client_test.dart b/test/client_test.dart index 2e535cba..1fe8e29f 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -1,4 +1,3 @@ -// @dart=2.9 /* * Famedly Matrix SDK * Copyright (C) 2019, 2020 Famedly GmbH @@ -35,7 +34,7 @@ import 'fake_database.dart'; import 'fake_matrix_api.dart'; void main() { - Client matrix; + late Client matrix; Future> eventUpdateListFuture; Future> toDeviceUpdateListFuture; @@ -128,7 +127,7 @@ void main() { expect(matrix.getDirectChatFromUserId('@bob:example.com'), '!726s6s6q:example.com'); expect(matrix.rooms[1].directChatMatrixID, '@bob:example.com'); - expect(matrix.directChats, matrix.accountData['m.direct'].content); + expect(matrix.directChats, matrix.accountData['m.direct']?.content); expect(matrix.presences.length, 1); expect(matrix.rooms[1].ephemerals.length, 2); expect(matrix.rooms[1].typingUsers.length, 1); @@ -139,29 +138,30 @@ void main() { Client.supportedGroupEncryptionAlgorithms.first); expect( matrix.rooms[1].roomAccountData['m.receipt'] - .content['@alice:example.com']['ts'], + ?.content['@alice:example.com']['ts'], 1436451550453); expect( matrix.rooms[1].roomAccountData['m.receipt'] - .content['@alice:example.com']['event_id'], + ?.content['@alice:example.com']['event_id'], '7365636s6r6432:example.com'); expect(matrix.rooms.length, 2); expect(matrix.rooms[1].canonicalAlias, - "#famedlyContactDiscovery:${matrix.userID.split(":")[1]}"); - expect(matrix.presences['@alice:example.com'].presence.presence, + "#famedlyContactDiscovery:${matrix.userID!.split(":")[1]}"); + expect(matrix.presences['@alice:example.com']?.presence.presence, PresenceType.online); expect(presenceCounter, 1); expect(accountDataCounter, 9); await Future.delayed(Duration(milliseconds: 50)); expect(matrix.userDeviceKeys.length, 4); - expect(matrix.userDeviceKeys['@alice:example.com'].outdated, false); - expect(matrix.userDeviceKeys['@alice:example.com'].deviceKeys.length, 2); + expect(matrix.userDeviceKeys['@alice:example.com']?.outdated, false); + expect(matrix.userDeviceKeys['@alice:example.com']?.deviceKeys.length, 2); expect( - matrix.userDeviceKeys['@alice:example.com'].deviceKeys['JLAFKJWSCS'] - .verified, + matrix.userDeviceKeys['@alice:example.com']?.deviceKeys['JLAFKJWSCS'] + ?.verified, false); await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', 'device_lists': { 'changed': [ '@alice:example.com', @@ -173,9 +173,10 @@ void main() { })); await Future.delayed(Duration(milliseconds: 50)); expect(matrix.userDeviceKeys.length, 3); - expect(matrix.userDeviceKeys['@alice:example.com'].outdated, true); + expect(matrix.userDeviceKeys['@alice:example.com']?.outdated, true); await matrix.handleSync(SyncUpdate.fromJson({ + 'next_batch': 'fakesync', 'rooms': { 'join': { '!726s6s6q:example.com': { @@ -199,7 +200,7 @@ void main() { expect( matrix.getRoomByAlias( - "#famedlyContactDiscovery:${matrix.userID.split(":")[1]}"), + "#famedlyContactDiscovery:${matrix.userID!.split(":")[1]}"), null); }); @@ -340,7 +341,7 @@ void main() { expect(archive[0].id, '!5345234234:example.com'); expect(archive[0].membership, Membership.leave); expect(archive[0].name, 'The room name'); - expect(archive[0].lastEvent.body, 'This is an example text message'); + expect(archive[0].lastEvent?.body, 'This is an example text message'); expect(archive[0].roomAccountData.length, 1); expect(archive[1].id, '!5345234235:example.com'); expect(archive[1].membership, Membership.leave); @@ -364,7 +365,7 @@ void main() { } FakeMatrixApi.calledEndpoints.clear(); await matrix.sendToDeviceEncrypted( - matrix.userDeviceKeys['@alice:example.com'].deviceKeys.values + matrix.userDeviceKeys['@alice:example.com']!.deviceKeys.values .toList(), 'm.message', { @@ -382,7 +383,7 @@ void main() { } FakeMatrixApi.calledEndpoints.clear(); await matrix.sendToDeviceEncryptedChunked( - matrix.userDeviceKeys['@alice:example.com'].deviceKeys.values + matrix.userDeviceKeys['@alice:example.com']!.deviceKeys.values .toList(), 'm.message', { @@ -483,11 +484,11 @@ void main() { expect( json.decode(FakeMatrixApi .calledEndpoints['/client/r0/sendToDevice/foxies/floof_txnid'] - [0])['messages'], + ?[0])['messages'], foxContent); expect( json.decode(FakeMatrixApi.calledEndpoints[ - '/client/r0/sendToDevice/raccoon/raccoon_txnid'][0])['messages'], + '/client/r0/sendToDevice/raccoon/raccoon_txnid']?[0])['messages'], raccoonContent); FakeMatrixApi.calledEndpoints.clear(); await client.sendToDevice('bunny', 'bunny_txnid', bunnyContent); @@ -502,7 +503,7 @@ void main() { expect( json.decode(FakeMatrixApi .calledEndpoints['/client/r0/sendToDevice/bunny/bunny_txnid'] - [0])['messages'], + ?[0])['messages'], bunnyContent); await client.dispose(closeDatabase: true); }); @@ -546,16 +547,16 @@ void main() { expect( json.decode(FakeMatrixApi .calledEndpoints['/client/r0/sendToDevice/foxies/floof_txnid'] - [0])['messages'], + ?[0])['messages'], foxContent); expect( json.decode(FakeMatrixApi.calledEndpoints[ - '/client/r0/sendToDevice/raccoon/raccoon_txnid'][0])['messages'], + '/client/r0/sendToDevice/raccoon/raccoon_txnid']?[0])['messages'], raccoonContent); expect( json.decode(FakeMatrixApi .calledEndpoints['/client/r0/sendToDevice/bunny/bunny_txnid'] - [0])['messages'], + ?[0])['messages'], bunnyContent); await client.dispose(closeDatabase: true); }); @@ -598,9 +599,10 @@ void main() { expect(client2.deviceName, client1.deviceName); expect(client2.rooms.length, 2); if (client2.encryptionEnabled) { - expect(client2.encryption.fingerprintKey, - client1.encryption.fingerprintKey); - expect(client2.encryption.identityKey, client1.encryption.identityKey); + expect(client2.encryption?.fingerprintKey, + client1.encryption?.fingerprintKey); + expect( + client2.encryption?.identityKey, client1.encryption?.identityKey); expect(client2.rooms[1].id, client1.rooms[1].id); } @@ -629,16 +631,18 @@ void main() { final response = await client.uploadContent(Uint8List(0), filename: 'file.jpeg'); expect(response.toString(), 'mxc://example.com/AQwafuaFswefuhsfAFAgsw'); - expect(await client.database.getFile(response) != null, - client.database.supportsFileStoring); + expect(await client.database?.getFile(response) != null, + client.database?.supportsFileStoring); await client.dispose(closeDatabase: true); }); test('object equality', () async { final time1 = DateTime.fromMillisecondsSinceEpoch(1); final time2 = DateTime.fromMillisecondsSinceEpoch(0); - final user1 = User('@user1:example.org', room: Room(id: '!room1')); - final user2 = User('@user2:example.org', room: Room(id: '!room1')); + final user1 = + User('@user1:example.org', room: Room(id: '!room1', client: matrix)); + final user2 = + User('@user2:example.org', room: Room(id: '!room1', client: matrix)); // receipts expect(Receipt(user1, time1) == Receipt(user1, time1), true); expect(Receipt(user1, time1) == Receipt(user1, time2), false); @@ -649,19 +653,29 @@ void main() { expect(user1 == user1, true); expect(user1 == user2, false); expect( - user1 == User('@user1:example.org', room: Room(id: '!room2')), false); + user1 == + User('@user1:example.org', + room: Room(id: '!room2', client: matrix)), + false); expect( user1 == User('@user1:example.org', - room: Room(id: '!room1'), membership: 'leave'), + room: Room(id: '!room1', client: matrix), + membership: 'leave'), false); // ignore: unrelated_type_equality_checks expect(user1 == 'beep', false); // rooms - expect(Room(id: '!room1') == Room(id: '!room1'), true); - expect(Room(id: '!room1') == Room(id: '!room2'), false); + expect( + Room(id: '!room1', client: matrix) == + Room(id: '!room1', client: matrix), + true); + expect( + Room(id: '!room1', client: matrix) == + Room(id: '!room2', client: matrix), + false); // ignore: unrelated_type_equality_checks - expect(Room(id: '!room1') == 'beep', false); + expect(Room(id: '!room1', client: matrix) == 'beep', false); }); test('clearCache', () async { diff --git a/test/device_keys_list_test.dart b/test/device_keys_list_test.dart index ec8a176e..98560862 100644 --- a/test/device_keys_list_test.dart +++ b/test/device_keys_list_test.dart @@ -1,4 +1,3 @@ -// @dart=2.9 /* * Famedly Matrix SDK * Copyright (C) 2019, 2020 Famedly GmbH @@ -34,7 +33,7 @@ void main() { var olmEnabled = true; - Client client; + late Client client; test('setupClient', () async { try { @@ -135,8 +134,8 @@ void main() { test('set blocked / verified', () async { if (!olmEnabled) return; final key = - client.userDeviceKeys[client.userID].deviceKeys['OTHERDEVICE']; - client.userDeviceKeys[client.userID].deviceKeys['UNSIGNEDDEVICE'] = + client.userDeviceKeys[client.userID]!.deviceKeys['OTHERDEVICE']!; + client.userDeviceKeys[client.userID]?.deviceKeys['UNSIGNEDDEVICE'] = DeviceKeys.fromJson({ 'user_id': '@test:fakeServer.notExisting', 'device_id': 'UNSIGNEDDEVICE', @@ -157,10 +156,10 @@ void main() { }, }, }, client); - final masterKey = client.userDeviceKeys[client.userID].masterKey; + final masterKey = client.userDeviceKeys[client.userID]!.masterKey!; masterKey.setDirectVerified(true); // we need to populate the ssss cache to be able to test signing easily - final handle = client.encryption.ssss.open(); + final handle = client.encryption!.ssss.open(); await handle.unlock(recoveryKey: ssssKey); await handle.maybeCacheAll(); @@ -174,16 +173,16 @@ void main() { expect(key.verified, true); // still verified via cross-sgining expect(key.encryptToDevice, true); expect( - client.userDeviceKeys[client.userID].deviceKeys['UNSIGNEDDEVICE'] - .encryptToDevice, + client.userDeviceKeys[client.userID]?.deviceKeys['UNSIGNEDDEVICE'] + ?.encryptToDevice, false); expect(masterKey.verified, true); await masterKey.setBlocked(true); expect(masterKey.verified, false); expect( - client.userDeviceKeys[client.userID].deviceKeys['UNSIGNEDDEVICE'] - .encryptToDevice, + client.userDeviceKeys[client.userID]?.deviceKeys['UNSIGNEDDEVICE'] + ?.encryptToDevice, true); await masterKey.setBlocked(false); expect(masterKey.verified, true); @@ -205,57 +204,57 @@ void main() { .any((k) => k == '/client/r0/keys/signatures/upload'), false); expect(key.directVerified, false); - client.userDeviceKeys[client.userID].deviceKeys.remove('UNSIGNEDDEVICE'); + client.userDeviceKeys[client.userID]?.deviceKeys.remove('UNSIGNEDDEVICE'); }); test('verification based on signatures', () async { if (!olmEnabled) return; - final user = client.userDeviceKeys[client.userID]; - user.masterKey.setDirectVerified(true); - expect(user.deviceKeys['GHTYAJCE'].crossVerified, true); - expect(user.deviceKeys['GHTYAJCE'].signed, true); - expect(user.getKey('GHTYAJCE').crossVerified, true); - expect(user.deviceKeys['OTHERDEVICE'].crossVerified, true); - expect(user.selfSigningKey.crossVerified, true); + final user = client.userDeviceKeys[client.userID]!; + user.masterKey?.setDirectVerified(true); + expect(user.deviceKeys['GHTYAJCE']?.crossVerified, true); + expect(user.deviceKeys['GHTYAJCE']?.signed, true); + expect(user.getKey('GHTYAJCE')?.crossVerified, true); + expect(user.deviceKeys['OTHERDEVICE']?.crossVerified, true); + expect(user.selfSigningKey?.crossVerified, true); expect( user .getKey('F9ypFzgbISXCzxQhhSnXMkc1vq12Luna3Nw5rqViOJY') - .crossVerified, + ?.crossVerified, true); - expect(user.userSigningKey.crossVerified, true); + expect(user.userSigningKey?.crossVerified, true); expect(user.verified, UserVerifiedStatus.verified); - user.masterKey.setDirectVerified(false); - expect(user.deviceKeys['GHTYAJCE'].crossVerified, false); - expect(user.deviceKeys['OTHERDEVICE'].crossVerified, false); + user.masterKey?.setDirectVerified(false); + expect(user.deviceKeys['GHTYAJCE']?.crossVerified, false); + expect(user.deviceKeys['OTHERDEVICE']?.crossVerified, false); expect(user.verified, UserVerifiedStatus.unknown); - user.deviceKeys['OTHERDEVICE'].setDirectVerified(true); + user.deviceKeys['OTHERDEVICE']?.setDirectVerified(true); expect(user.verified, UserVerifiedStatus.verified); - user.deviceKeys['OTHERDEVICE'].setDirectVerified(false); + user.deviceKeys['OTHERDEVICE']?.setDirectVerified(false); - user.masterKey.setDirectVerified(true); - user.deviceKeys['GHTYAJCE'].signatures[client.userID] - .removeWhere((k, v) => k != 'ed25519:GHTYAJCE'); - expect(user.deviceKeys['GHTYAJCE'].verified, + user.masterKey?.setDirectVerified(true); + user.deviceKeys['GHTYAJCE']?.signatures?[client.userID] + ?.removeWhere((k, v) => k != 'ed25519:GHTYAJCE'); + expect(user.deviceKeys['GHTYAJCE']?.verified, true); // it's our own device, should be direct verified - expect( - user.deviceKeys['GHTYAJCE'].signed, false); // not verified for others - user.deviceKeys['OTHERDEVICE'].signatures.clear(); + expect(user.deviceKeys['GHTYAJCE']?.signed, + false); // not verified for others + user.deviceKeys['OTHERDEVICE']?.signatures?.clear(); expect(user.verified, UserVerifiedStatus.unknownDevice); }); test('start verification', () async { if (!olmEnabled) return; var req = client - .userDeviceKeys['@alice:example.com'].deviceKeys['JLAFKJWSCS'] - .startVerification(); + .userDeviceKeys['@alice:example.com']?.deviceKeys['JLAFKJWSCS'] + ?.startVerification(); expect(req != null, true); - expect(req.room != null, false); + expect(req?.room != null, false); - req = - await client.userDeviceKeys['@alice:example.com'].startVerification(); + req = await client.userDeviceKeys['@alice:example.com'] + ?.startVerification(); expect(req != null, true); - expect(req.room != null, true); + expect(req?.room != null, true); }); test('dispose client', () async { diff --git a/test/matrix_database_test.dart b/test/matrix_database_test.dart index 4d4e8b9e..03d9cc17 100644 --- a/test/matrix_database_test.dart +++ b/test/matrix_database_test.dart @@ -1,4 +1,3 @@ -// @dart=2.9 /* * Famedly Matrix SDK * Copyright (C) 2020 Famedly GmbH @@ -27,7 +26,7 @@ import 'fake_database.dart'; void main() { group('Databse', () { Logs().level = Level.error; - final room = Room(id: '!room:blubb'); + final room = Room(id: '!room:blubb', client: Client('testclient')); test('setupDatabase', () async { final database = await getDatabase(null); await database.insertClient( @@ -59,7 +58,7 @@ void main() { ); await database.storeEventUpdate(update, client); var event = await database.getEventById('\$event-1', room); - expect(event.eventId, '\$event-1'); + expect(event?.eventId, '\$event-1'); // insert a transaction id update = EventUpdate( @@ -76,7 +75,7 @@ void main() { ); await database.storeEventUpdate(update, client); event = await database.getEventById('transaction-1', room); - expect(event.eventId, 'transaction-1'); + expect(event?.eventId, 'transaction-1'); update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, @@ -112,7 +111,7 @@ void main() { ); await database.storeEventUpdate(update, client); event = await database.getEventById('\$event-3', room); - expect(event.eventId, '\$event-3'); + expect(event?.eventId, '\$event-3'); update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, @@ -130,8 +129,8 @@ void main() { ); await database.storeEventUpdate(update, client); event = await database.getEventById('\$event-3', room); - expect(event.eventId, '\$event-3'); - expect(event.status, EventStatus.sent); + expect(event?.eventId, '\$event-3'); + expect(event?.status, EventStatus.sent); event = await database.getEventById('transaction-2', room); expect(event, null); @@ -150,7 +149,7 @@ void main() { ); await database.storeEventUpdate(update, client); event = await database.getEventById('\$event-4', room); - expect(event.eventId, '\$event-4'); + expect(event?.eventId, '\$event-4'); update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, @@ -168,8 +167,8 @@ void main() { ); await database.storeEventUpdate(update, client); event = await database.getEventById('\$event-4', room); - expect(event.eventId, '\$event-4'); - expect(event.status, EventStatus.synced); + expect(event?.eventId, '\$event-4'); + expect(event?.status, EventStatus.synced); event = await database.getEventById('transaction-3', room); expect(event, null); }); diff --git a/test/timeline_test.dart b/test/timeline_test.dart index 0532ae17..6e2be8b8 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -1,4 +1,3 @@ -// @dart=2.9 /* * Famedly Matrix SDK * Copyright (C) 2019, 2020 Famedly GmbH @@ -37,9 +36,9 @@ void main() { final insertList = []; var olmEnabled = true; - Client client; - Room room; - Timeline timeline; + late Client client; + late Room room; + late Timeline timeline; test('create stuff', () async { try { await olm.init(); @@ -233,18 +232,18 @@ void main() { test('getEventById', () async { var event = await timeline.getEventById('abc'); - expect(event.content, {'msgtype': 'm.text', 'body': 'Testcase'}); + expect(event?.content, {'msgtype': 'm.text', 'body': 'Testcase'}); event = await timeline.getEventById('not_found'); expect(event, null); event = await timeline.getEventById('unencrypted_event'); - expect(event.body, 'This is an example text message'); + expect(event?.body, 'This is an example text message'); if (olmEnabled) { event = await timeline.getEventById('encrypted_event'); // the event is invalid but should have traces of attempting to decrypt - expect(event.messageType, MessageTypes.BadEncrypted); + expect(event?.messageType, MessageTypes.BadEncrypted); } });