refactor: nullsafe event test

This commit is contained in:
Nicolas Werner 2021-10-28 01:53:24 +02:00
parent ebd5bc3d24
commit 6abd9e7e22
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
9 changed files with 227 additions and 528 deletions

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020, 2021 Famedly GmbH

View File

@ -186,7 +186,7 @@ class Event extends MatrixEvent {
prevContent: prevContent,
content: content,
type: jsonPayload['type'],
eventId: jsonPayload['event_id'],
eventId: jsonPayload['event_id'] ?? '',
roomId: jsonPayload['room_id'],
senderId: jsonPayload['sender'],
originServerTs: jsonPayload.containsKey('origin_server_ts')

View File

@ -568,7 +568,7 @@ class Room {
/// Sends a normal text message to this room. Returns the event ID generated
/// by the server for this message.
Future<String?> sendTextEvent(String message,
{required String? txid,
{String? txid,
Event? inReplyTo,
String? editEventId,
bool parseMarkdown = true,

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
@ -29,7 +28,7 @@ import 'package:test/test.dart';
import 'fake_client.dart';
import 'fake_matrix_api.dart';
import 'fake_matrix_localizations.dart';
import 'matrix_default_localizations.dart';
void main() {
/// All Tests related to the Event
@ -249,10 +248,10 @@ void main() {
final event = Event.fromJson(redactJsonObj, room);
event.setRedactionEvent(redactedBecause);
expect(event.redacted, true);
expect(event.redactedBecause.toJson(), redactedBecause.toJson());
expect(event.redactedBecause?.toJson(), redactedBecause.toJson());
expect(event.content.isEmpty, true);
redactionEventJson.remove('redacts');
expect(event.unsigned['redacted_because'], redactionEventJson);
expect(event.unsigned?['redacted_because'], redactionEventJson);
}
});
@ -280,7 +279,7 @@ void main() {
event.status = EventStatus.error;
final resp2 = await event.sendAgain(txid: '1234');
expect(resp1, null);
expect(resp2.startsWith('\$event'), true);
expect(resp2?.startsWith('\$event'), true);
await matrix.dispose(closeDatabase: true);
});
@ -295,7 +294,7 @@ void main() {
final event = Event.fromJson(
jsonObj, Room(id: '!1234:example.com', client: matrix));
String exception;
String? exception;
try {
await event.requestKey();
} catch (e) {
@ -330,7 +329,7 @@ void main() {
jsonObj['state_key'] = '@alice:example.com';
final event = Event.fromJson(
jsonObj, Room(id: '!localpart:server.abc', client: client));
expect(event.stateKeyUser.id, '@alice:example.com');
expect(event.stateKeyUser?.id, '@alice:example.com');
});
test('canRedact', () async {
expect(event.canRedact, true);
@ -364,7 +363,8 @@ void main() {
}
}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Removed by Example');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -392,7 +392,8 @@ void main() {
'type': 'm.sticker',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example sent a sticker');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -405,7 +406,8 @@ void main() {
'type': 'm.room.redaction',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example redacted an event');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -420,7 +422,8 @@ void main() {
'type': 'm.room.aliases',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the room aliases');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -435,7 +438,8 @@ void main() {
'type': 'm.room.aliases',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the room aliases');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -448,7 +452,8 @@ void main() {
'type': 'm.room.canonical_alias',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the room invitation link');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -469,7 +474,8 @@ void main() {
'type': 'm.room.create',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example created the chat');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -485,7 +491,8 @@ void main() {
'type': 'm.room.tombstone',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Room has been upgraded');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -498,7 +505,8 @@ void main() {
'type': 'm.room.join_rules',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the join rules to Anyone can join');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -515,7 +523,8 @@ void main() {
'type': 'm.room.member',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Alice joined the chat');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -527,7 +536,8 @@ void main() {
'state_key': '@alice:example.org',
'type': 'm.room.member'
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example has invited Alice');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -542,7 +552,8 @@ void main() {
'prev_content': {'membership': 'join'},
}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example kicked Alice');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -557,7 +568,8 @@ void main() {
'prev_content': {'membership': 'join'},
}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example banned Alice');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -572,7 +584,8 @@ void main() {
'prev_content': {'membership': 'invite'},
}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Alice accepted the invitation');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -587,7 +600,8 @@ void main() {
'prev_content': {'membership': 'join'},
}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example has invited Alice');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -602,7 +616,8 @@ void main() {
'prev_content': {'membership': 'invite'},
}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example has withdrawn the invitation for Alice');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -617,7 +632,8 @@ void main() {
'prev_content': {'membership': 'invite'},
}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Alice rejected the invitation');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -641,7 +657,8 @@ void main() {
'type': 'm.room.power_levels',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the chat permissions');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -654,7 +671,8 @@ void main() {
'type': 'm.room.name',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the chat name to The room name');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -667,7 +685,8 @@ void main() {
'type': 'm.room.topic',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the chat description to A room topic');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -683,7 +702,8 @@ void main() {
'type': 'm.room.avatar',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the chat avatar');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -696,7 +716,8 @@ void main() {
'type': 'm.room.history_visibility',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example changed the history visibility to Visible for all participants');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -713,8 +734,8 @@ void main() {
'type': 'm.room.encryption',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()),
'Example activatedEndToEndEncryption. needPantalaimonWarning');
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example activated end to end encryption. Need pantalaimon');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -731,7 +752,7 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()),
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'This is an example text message');
expect(event.isEventTypeKnown, true);
@ -749,7 +770,7 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()),
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'* thinks this is an example emote');
expect(event.isEventTypeKnown, true);
@ -767,7 +788,7 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()),
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'This is an example notice');
expect(event.isEventTypeKnown, true);
@ -785,7 +806,8 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example sent a picture');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -803,7 +825,8 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example sent a file');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -824,7 +847,8 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example sent an audio');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -849,7 +873,8 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example shared the location');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -879,7 +904,8 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Example sent a video');
expect(event.isEventTypeKnown, true);
event = Event.fromJson({
@ -891,7 +917,8 @@ void main() {
'type': 'unknown.event.type',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations()), null);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()),
'Unknown event unknown.event.type');
expect(event.isEventTypeKnown, false);
});
@ -913,7 +940,7 @@ void main() {
'unsigned': {'age': 1234}
}, room);
expect(
event.getLocalizedBody(FakeMatrixLocalizations(),
event.getLocalizedBody(MatrixDefaultLocalizations(),
plaintextBody: true),
'**This is an example text message**');
@ -941,10 +968,11 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations(), hideEdit: true),
expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), hideEdit: true),
'This is an example text message');
expect(
event.getLocalizedBody(FakeMatrixLocalizations(),
event.getLocalizedBody(MatrixDefaultLocalizations(),
hideEdit: true, plaintextBody: true),
'**This is an example text message**');
@ -962,10 +990,11 @@ void main() {
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room);
expect(event.getLocalizedBody(FakeMatrixLocalizations(), hideReply: true),
expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), hideReply: true),
'hmm, fox');
expect(
event.getLocalizedBody(FakeMatrixLocalizations(),
event.getLocalizedBody(MatrixDefaultLocalizations(),
hideReply: true, plaintextBody: true),
'hmm, *fox*');
});
@ -976,6 +1005,8 @@ void main() {
'body': 'blah',
'msgtype': 'm.text',
},
'type': 'm.room.message',
'sender': '@example:example.org',
'event_id': '\$source',
}, null);
final edit1 = Event.fromJson({
@ -987,6 +1018,8 @@ void main() {
'rel_type': RelationshipTypes.edit,
},
},
'type': 'm.room.message',
'sender': '@example:example.org',
'event_id': '\$edit1',
}, null);
final edit2 = Event.fromJson({
@ -998,9 +1031,11 @@ void main() {
'rel_type': RelationshipTypes.edit,
},
},
'type': 'm.room.message',
'sender': '@example:example.org',
'event_id': '\$edit2',
}, null);
final room = Room(client: client);
final room = Room(client: client, id: '!id:fakeserver.nonexisting');
final timeline =
Timeline(events: <Event>[event, edit1, edit2], room: room);
expect(event.hasAggregatedEvents(timeline, RelationshipTypes.edit), true);
@ -1096,7 +1131,7 @@ void main() {
'event_id': '\$edit3',
'sender': '@bob:example.org',
}, null);
final room = Room(client: client);
final room = Room(id: '!localpart:server.abc', client: client);
// no edits
var displayEvent =
event.getDisplayEvent(Timeline(events: <Event>[event], room: room));
@ -1127,8 +1162,9 @@ void main() {
'sender': '@alice:example.org',
'unsigned': {
'redacted_because': {
'evnet_id': '\$redact',
'event_id': '\$redact',
'sender': '@alice:example.org',
'type': 'm.room.redaction',
},
},
}, null);
@ -1143,7 +1179,7 @@ void main() {
return {
'/_matrix/media/r0/download/example.org/file': FILE_BUFF,
'/_matrix/media/r0/download/example.org/thumb': THUMBNAIL_BUFF,
}[uri.path];
}[uri.path]!;
};
await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
@ -1160,7 +1196,7 @@ void main() {
}, room);
var buffer = await event.downloadAndDecryptAttachment(
downloadCallback: downloadCallback);
expect(buffer.bytes, FILE_BUFF);
expect(buffer?.bytes, FILE_BUFF);
expect(event.attachmentOrThumbnailMxcUrl().toString(),
'mxc://example.org/file');
expect(event.attachmentOrThumbnailMxcUrl(getThumbnail: true).toString(),
@ -1215,11 +1251,11 @@ void main() {
buffer = await event.downloadAndDecryptAttachment(
downloadCallback: downloadCallback);
expect(buffer.bytes, FILE_BUFF);
expect(buffer?.bytes, FILE_BUFF);
buffer = await event.downloadAndDecryptAttachment(
getThumbnail: true, downloadCallback: downloadCallback);
expect(buffer.bytes, THUMBNAIL_BUFF);
expect(buffer?.bytes, THUMBNAIL_BUFF);
});
test('encrypted attachments', () async {
if (!olmEnabled) return;
@ -1234,7 +1270,7 @@ void main() {
return {
'/_matrix/media/r0/download/example.com/file': FILE_BUFF_ENC,
'/_matrix/media/r0/download/example.com/thumb': THUMB_BUFF_ENC,
}[uri.path];
}[uri.path]!;
};
final room = Room(id: '!localpart:server.abc', client: await getClient());
var event = Event.fromJson({
@ -1262,7 +1298,7 @@ void main() {
}, room);
var buffer = await event.downloadAndDecryptAttachment(
downloadCallback: downloadCallback);
expect(buffer.bytes, FILE_BUFF_DEC);
expect(buffer?.bytes, FILE_BUFF_DEC);
event = Event.fromJson({
'type': EventTypes.Message,
@ -1315,11 +1351,11 @@ void main() {
expect(event.thumbnailMxcUrl.toString(), 'mxc://example.com/thumb');
buffer = await event.downloadAndDecryptAttachment(
downloadCallback: downloadCallback);
expect(buffer.bytes, FILE_BUFF_DEC);
expect(buffer?.bytes, FILE_BUFF_DEC);
buffer = await event.downloadAndDecryptAttachment(
getThumbnail: true, downloadCallback: downloadCallback);
expect(buffer.bytes, THUMB_BUFF_DEC);
expect(buffer?.bytes, THUMB_BUFF_DEC);
await room.client.dispose(closeDatabase: true);
});
@ -1330,7 +1366,7 @@ void main() {
serverHits++;
return {
'/_matrix/media/r0/download/example.org/newfile': FILE_BUFF,
}[uri.path];
}[uri.path]!;
};
await client.checkHomeserver('https://fakeserver.notexisting',
checkWellKnown: false);
@ -1352,14 +1388,14 @@ void main() {
var buffer = await event.downloadAndDecryptAttachment(
downloadCallback: downloadCallback);
expect(await event.isAttachmentInLocalStore(),
event.room.client.database.supportsFileStoring);
expect(buffer.bytes, FILE_BUFF);
event.room?.client.database?.supportsFileStoring);
expect(buffer?.bytes, FILE_BUFF);
expect(serverHits, 1);
buffer = await event.downloadAndDecryptAttachment(
downloadCallback: downloadCallback);
expect(buffer.bytes, FILE_BUFF);
expect(buffer?.bytes, FILE_BUFF);
expect(
serverHits, event.room.client.database.supportsFileStoring ? 1 : 2);
serverHits, event.room!.client.database!.supportsFileStoring ? 1 : 2);
await room.client.dispose(closeDatabase: true);
});

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2020 Famedly GmbH

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
@ -39,7 +38,7 @@ Map<String, dynamic> decodeJson(dynamic data) {
class FakeMatrixApi extends MockClient {
static final calledEndpoints = <String, List<dynamic>>{};
static int eventCounter = 0;
static sdk.Client client;
static sdk.Client? client;
static bool failToDevice = false;
FakeMatrixApi()
@ -85,9 +84,10 @@ class FakeMatrixApi extends MockClient {
if (!calledEndpoints.containsKey(action)) {
calledEndpoints[action] = <dynamic>[];
}
calledEndpoints[action].add(data);
if (api.containsKey(method) && api[method].containsKey(action)) {
res = api[method][action](data);
calledEndpoints[action]?.add(data);
final act = api[method]?[action];
if (act != null) {
res = act(data);
if (res is Map && res.containsKey('errcode')) {
if (res['errcode'] == 'M_NOT_FOUND') {
statusCode = 404;
@ -126,12 +126,12 @@ class FakeMatrixApi extends MockClient {
..accountData = [
sdk.BasicEvent(content: decodeJson(data), type: type)
];
if (client.database != null) {
await client.database.transaction(() async {
await client.handleSync(syncUpdate);
if (client?.database != null) {
await client?.database?.transaction(() async {
await client?.handleSync(syncUpdate);
});
} else {
await client.handleSync(syncUpdate);
await client?.handleSync(syncUpdate);
}
res = {};
} else {
@ -2101,15 +2101,15 @@ class FakeMatrixApi extends MockClient {
}) {
if (jsonBody[keyType] != null) {
final key =
sdk.CrossSigningKey.fromJson(jsonBody[keyType], client);
client.userDeviceKeys[client.userID].crossSigningKeys
sdk.CrossSigningKey.fromJson(jsonBody[keyType], client!);
client!.userDeviceKeys[client!.userID!]?.crossSigningKeys
.removeWhere((k, v) => v.usage.contains(key.usage.first));
client.userDeviceKeys[client.userID]
.crossSigningKeys[key.publicKey] = key;
client!.userDeviceKeys[client!.userID!]
?.crossSigningKeys[key.publicKey!] = key;
}
}
// and generate a fake sync
client.handleSync(sdk.SyncUpdate(nextBatch: ''));
client!.handleSync(sdk.SyncUpdate(nextBatch: ''));
}
return {};
},

View File

@ -1,334 +0,0 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'package:matrix/matrix.dart';
class FakeMatrixLocalizations extends MatrixLocalizations {
@override
String acceptedTheInvitation(String targetName) {
// TODO: implement acceptedTheInvitation
return null;
}
@override
String activatedEndToEndEncryption(String senderName) {
// TODO: implement activatedEndToEndEncryption
return '$senderName activatedEndToEndEncryption';
}
@override
// TODO: implement anyoneCanJoin
String get anyoneCanJoin => null;
@override
String bannedUser(String senderName, String targetName) {
// TODO: implement bannedUser
return null;
}
@override
String changedTheChatAvatar(String senderName) {
// TODO: implement changedTheChatAvatar
return null;
}
@override
String changedTheChatDescriptionTo(String senderName, String content) {
// TODO: implement changedTheChatDescriptionTo
return null;
}
@override
String changedTheChatNameTo(String senderName, String content) {
// TODO: implement changedTheChatNameTo
return null;
}
@override
String changedTheChatPermissions(String senderName) {
// TODO: implement changedTheChatPermissions
return null;
}
@override
String changedTheDisplaynameTo(String targetName, String newDisplayname) {
// TODO: implement changedTheDisplaynameTo
return null;
}
@override
String changedTheGuestAccessRules(String senderName) {
// TODO: implement changedTheGuestAccessRules
return null;
}
@override
String changedTheGuestAccessRulesTo(
String senderName, String localizedString) {
// TODO: implement changedTheGuestAccessRulesTo
return null;
}
@override
String changedTheHistoryVisibility(String senderName) {
// TODO: implement changedTheHistoryVisibility
return null;
}
@override
String changedTheHistoryVisibilityTo(
String senderName, String localizedString) {
// TODO: implement changedTheHistoryVisibilityTo
return null;
}
@override
String changedTheJoinRules(String senderName) {
// TODO: implement changedTheJoinRules
return null;
}
@override
String changedTheJoinRulesTo(String senderName, String localizedString) {
// TODO: implement changedTheJoinRulesTo
return null;
}
@override
String changedTheProfileAvatar(String targetName) {
// TODO: implement changedTheProfileAvatar
return null;
}
@override
String changedTheRoomAliases(String senderName) {
// TODO: implement changedTheRoomAliases
return null;
}
@override
String changedTheRoomInvitationLink(String senderName) {
// TODO: implement changedTheRoomInvitationLink
return null;
}
@override
// TODO: implement channelCorruptedDecryptError
String get channelCorruptedDecryptError => null;
@override
String couldNotDecryptMessage(String errorText) {
// TODO: implement couldNotDecryptMessage
return null;
}
@override
String createdTheChat(String senderName) {
// TODO: implement createdTheChat
return null;
}
@override
// TODO: implement emptyChat
String get emptyChat => null;
@override
// TODO: implement encryptionNotEnabled
String get encryptionNotEnabled => null;
@override
// TODO: implement fromJoining
String get fromJoining => null;
@override
// TODO: implement fromTheInvitation
String get fromTheInvitation => null;
@override
String groupWith(String displayname) {
// TODO: implement groupWith
return null;
}
@override
// TODO: implement guestsAreForbidden
String get guestsAreForbidden => null;
@override
// TODO: implement guestsCanJoin
String get guestsCanJoin => null;
@override
String hasWithdrawnTheInvitationFor(String senderName, String targetName) {
// TODO: implement hasWithdrawnTheInvitationFor
return null;
}
@override
String invitedUser(String senderName, String targetName) {
// TODO: implement invitedUser
return null;
}
@override
// TODO: implement invitedUsersOnly
String get invitedUsersOnly => null;
@override
String joinedTheChat(String targetName) {
// TODO: implement joinedTheChat
return null;
}
@override
String kicked(String senderName, String targetName) {
// TODO: implement kicked
return null;
}
@override
String kickedAndBanned(String senderName, String targetName) {
// TODO: implement kickedAndBanned
return null;
}
@override
// TODO: implement needPantalaimonWarning
String get needPantalaimonWarning => 'needPantalaimonWarning';
@override
// TODO: implement noPermission
String get noPermission => 'noPermission';
@override
String redactedAnEvent(String senderName) {
// TODO: implement redactedAnEvent
return null;
}
@override
String rejectedTheInvitation(String targetName) {
// TODO: implement rejectedTheInvitation
return null;
}
@override
String removedBy(String calcDisplayname) {
// TODO: implement removedBy
return null;
}
@override
// TODO: implement roomHasBeenUpgraded
String get roomHasBeenUpgraded => null;
@override
String sentAFile(String senderName) {
// TODO: implement sentAFile
return null;
}
@override
String sentAPicture(String senderName) {
// TODO: implement sentAPicture
return null;
}
@override
String sentASticker(String senderName) {
// TODO: implement sentASticker
return null;
}
@override
String sentAVideo(String senderName) {
// TODO: implement sentAVideo
return null;
}
@override
String sentAnAudio(String senderName) {
// TODO: implement sentAnAudio
return null;
}
@override
String sharedTheLocation(String senderName) {
// TODO: implement sharedTheLocation
return null;
}
@override
String unbannedUser(String senderName, String targetName) {
// TODO: implement unbannedUser
return null;
}
@override
// TODO: implement unknownEncryptionAlgorithm
String get unknownEncryptionAlgorithm => null;
@override
String unknownEvent(String typeKey) {
// TODO: implement unknownEvent
return null;
}
@override
String userLeftTheChat(String targetName) {
// TODO: implement userLeftTheChat
return null;
}
@override
// TODO: implement visibleForAllParticipants
String get visibleForAllParticipants => null;
@override
// TODO: implement visibleForEveryone
String get visibleForEveryone => null;
@override
// TODO: implement you
String get you => null;
@override
String answeredTheCall(String senderName) {
// TODO: implement answeredTheCall
return null;
}
@override
String endedTheCall(String senderName) {
// TODO: implement endedTheCall
return null;
}
@override
String sentCallInformations(String senderName) {
// TODO: implement sentCallInformations
return null;
}
@override
String startedACall(String senderName) {
// TODO: implement startedACall
return null;
}
}

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH

View File

@ -1,4 +1,3 @@
// @dart=2.9
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020, 2021 Famedly GmbH
@ -30,7 +29,7 @@ const String testMessage5 = 'Hello earth';
const String testMessage6 = 'Hello mars';
void test() async {
Client testClientA, testClientB;
Client? testClientA, testClientB;
try {
await olm.init();
@ -78,19 +77,19 @@ void test() async {
Logs().i('++++ Check if own olm device is verified by default ++++');
assert(testClientA.userDeviceKeys.containsKey(TestUser.username));
assert(testClientA.userDeviceKeys[TestUser.username].deviceKeys
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(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
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);
assert(testClientB.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.verified);
assert(!testClientB.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.blocked);
Logs().i('++++ (Alice) Create room and invite Bob ++++');
await testClientA.createRoom(invite: [TestUser.username2]);
@ -100,7 +99,7 @@ void test() async {
final roomId = room.id;
Logs().i('++++ (Bob) Join room ++++');
final inviteRoom = testClientB.getRoomById(roomId);
final inviteRoom = testClientB.getRoomById(roomId)!;
await inviteRoom.join();
await Future.delayed(Duration(seconds: 1));
assert(inviteRoom.membership == Membership.join);
@ -110,114 +109,116 @@ void test() async {
await room.enableEncryption();
await Future.delayed(Duration(seconds: 5));
assert(room.encrypted == true);
assert(room.client.encryption.keyManager.getOutboundGroupSession(room.id) ==
null);
assert(
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
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(!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
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);
assert(!testClientB.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.verified);
assert(!testClientB.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.blocked);
await testClientA
.userDeviceKeys[TestUser.username2].deviceKeys[testClientB.deviceID]
.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
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!]!.verified);
assert(testClientB.userDeviceKeys.containsKey(TestUser.username2));
assert(testClientB.userDeviceKeys[TestUser.username2].deviceKeys
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!]!.verified);
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
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
.getInboundGroupSession(room.id, currentSessionIdA, '') !=
null);*/
assert(testClientA.encryption.olmManager
.olmSessions[testClientB.identityKey].length ==
assert(testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length ==
1);
assert(testClientB.encryption.olmManager
.olmSessions[testClientA.identityKey].length ==
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
1);
assert(testClientA.encryption.olmManager
.olmSessions[testClientB.identityKey].first.sessionId ==
testClientB.encryption.olmManager.olmSessions[testClientA.identityKey]
assert(testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.first.sessionId ==
testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]!
.first.sessionId);
/*assert(inviteRoom.client.encryption.keyManager
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);*/
assert(room.lastEvent.body == testMessage);
assert(inviteRoom.lastEvent.body == testMessage);
assert(room.lastEvent!.body == testMessage);
assert(inviteRoom.lastEvent!.body == testMessage);
Logs().i(
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent.body}' ++++");
"++++ (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 ==
assert(testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length ==
1);
assert(testClientB.encryption.olmManager
.olmSessions[testClientA.identityKey].length ==
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
1);
assert(testClientA.encryption.olmManager
.olmSessions[testClientB.identityKey].first.sessionId ==
testClientB.encryption.olmManager.olmSessions[testClientA.identityKey]
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
assert(room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id() ==
currentSessionIdA);
/*assert(room.client.encryption.keyManager
.getInboundGroupSession(room.id, currentSessionIdA, '') !=
null);*/
assert(room.lastEvent.body == testMessage2);
assert(inviteRoom.lastEvent.body == testMessage2);
assert(room.lastEvent!.body == testMessage2);
assert(inviteRoom.lastEvent!.body == testMessage2);
Logs().i(
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent.body}' ++++");
"++++ (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 ==
assert(testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length ==
1);
assert(testClientB.encryption.olmManager
.olmSessions[testClientA.identityKey].length ==
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
1);
assert(room.client.encryption.keyManager
.getOutboundGroupSession(room.id)
.outboundGroupSession
assert(room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id() ==
currentSessionIdA);
final inviteRoomOutboundGroupSession = inviteRoom
.client.encryption.keyManager
.getOutboundGroupSession(inviteRoom.id);
.client.encryption!.keyManager
.getOutboundGroupSession(inviteRoom.id)!;
assert(inviteRoomOutboundGroupSession != null);
/*assert(inviteRoom.client.encryption.keyManager.getInboundGroupSession(
@ -230,10 +231,10 @@ void test() async {
inviteRoomOutboundGroupSession.outboundGroupSession.session_id(),
'') !=
null);*/
assert(inviteRoom.lastEvent.body == testMessage3);
assert(room.lastEvent.body == testMessage3);
assert(inviteRoom.lastEvent!.body == testMessage3);
assert(room.lastEvent!.body == testMessage3);
Logs().i(
"++++ (Alice) Received decrypted message: '${room.lastEvent.body}' ++++");
"++++ (Alice) Received decrypted message: '${room.lastEvent!.body}' ++++");
Logs().i('++++ Login Bob in another client ++++');
var testClientC = Client('TestClientC', databaseBuilder: getDatabase);
@ -246,78 +247,77 @@ void test() async {
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 ==
assert(testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length ==
1);
assert(testClientB.encryption.olmManager
.olmSessions[testClientA.identityKey].length ==
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
1);
assert(testClientA.encryption.olmManager
.olmSessions[testClientB.identityKey].first.sessionId ==
testClientB.encryption.olmManager.olmSessions[testClientA.identityKey]
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 ==
assert(testClientA.encryption!.olmManager
.olmSessions[testClientC.identityKey]!.length ==
1);
assert(testClientC.encryption.olmManager
.olmSessions[testClientA.identityKey].length ==
assert(testClientC.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
1);
assert(testClientA.encryption.olmManager
.olmSessions[testClientC.identityKey].first.sessionId ==
testClientC.encryption.olmManager.olmSessions[testClientA.identityKey]
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
assert(room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id() !=
currentSessionIdA);
currentSessionIdA = room.client.encryption.keyManager
.getOutboundGroupSession(room.id)
.outboundGroupSession
currentSessionIdA = room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id();
/*assert(inviteRoom.client.encryption.keyManager
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);*/
assert(room.lastEvent.body == testMessage4);
assert(inviteRoom.lastEvent.body == testMessage4);
assert(room.lastEvent!.body == testMessage4);
assert(inviteRoom.lastEvent!.body == testMessage4);
Logs().i(
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent.body}' ++++");
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++");
Logs().i('++++ Logout Bob another client ++++');
await testClientC.dispose(closeDatabase: false);
await testClientC.logout();
testClientC = null;
await Future.delayed(Duration(seconds: 5));
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 ==
assert(testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length ==
1);
assert(testClientB.encryption.olmManager
.olmSessions[testClientA.identityKey].length ==
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
1);
assert(testClientA.encryption.olmManager
.olmSessions[testClientB.identityKey].first.sessionId ==
testClientB.encryption.olmManager.olmSessions[testClientA.identityKey]
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
assert(room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id() !=
currentSessionIdA);
currentSessionIdA = room.client.encryption.keyManager
.getOutboundGroupSession(room.id)
.outboundGroupSession
currentSessionIdA = room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id();
/*assert(inviteRoom.client.encryption.keyManager
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);*/
assert(room.lastEvent.body == testMessage6);
assert(inviteRoom.lastEvent.body == testMessage6);
assert(room.lastEvent!.body == testMessage6);
assert(inviteRoom.lastEvent!.body == testMessage6);
Logs().i(
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent.body}' ++++");
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++");
await room.leave();
await room.forget();
@ -329,8 +329,8 @@ void test() async {
rethrow;
} finally {
Logs().i('++++ Logout Alice and Bob ++++');
if (testClientA?.isLogged() ?? false) await testClientA.logoutAll();
if (testClientA?.isLogged() ?? false) await testClientB.logoutAll();
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;