famedlysdk/test_driver/famedlysdk_test.dart

355 lines
14 KiB
Dart
Raw Normal View History

import 'package:famedlysdk/famedlysdk.dart';
2020-06-03 10:16:01 +00:00
import 'package:famedlysdk/matrix_api.dart';
2020-08-06 09:35:02 +00:00
import 'package:famedlysdk/src/utils/logs.dart';
2020-05-15 18:40:17 +00:00
import '../test/fake_database.dart';
void main() => test();
const String homeserver = 'https://matrix.test.famedly.de';
const String testUserA = '@tick:test.famedly.de';
const String testPasswordA = 'test';
const String testUserB = '@trick:test.famedly.de';
const String testPasswordB = 'test';
const String testMessage = 'Hello world';
const String testMessage2 = 'Hello moon';
const String testMessage3 = 'Hello sun';
const String testMessage4 = 'Hello star';
const String testMessage5 = 'Hello earth';
const String testMessage6 = 'Hello mars';
void test() async {
2020-08-06 09:35:02 +00:00
Logs.success('++++ Login $testUserA ++++');
2020-08-06 06:55:35 +00:00
var testClientA = Client('TestClientA');
2020-05-15 18:40:17 +00:00
testClientA.database = getDatabase();
await testClientA.checkServer(homeserver);
2020-08-11 16:11:51 +00:00
await testClientA.login(user: testUserA, password: testPasswordA);
2020-02-19 09:24:54 +00:00
assert(testClientA.encryptionEnabled);
2020-08-06 09:35:02 +00:00
Logs.success('++++ Login $testUserB ++++');
2020-08-06 06:55:35 +00:00
var testClientB = Client('TestClientB');
2020-05-15 18:40:17 +00:00
testClientB.database = getDatabase();
await testClientB.checkServer(homeserver);
2020-08-11 16:11:51 +00:00
await testClientB.login(user: testUserB, password: testPasswordA);
2020-02-19 09:24:54 +00:00
assert(testClientB.encryptionEnabled);
2020-08-06 09:35:02 +00:00
Logs.success('++++ ($testUserA) Leave all rooms ++++');
while (testClientA.rooms.isNotEmpty) {
var room = testClientA.rooms.first;
if (room.canonicalAlias?.isNotEmpty ?? false) {
break;
}
2020-02-18 09:23:55 +00:00
try {
await room.leave();
await room.forget();
2020-06-03 10:16:01 +00:00
} catch (_) {}
}
2020-08-06 09:35:02 +00:00
Logs.success('++++ ($testUserB) Leave all rooms ++++');
for (var i = 0; i < 3; i++) {
2020-02-18 09:23:55 +00:00
if (testClientB.rooms.isNotEmpty) {
var room = testClientB.rooms.first;
2020-02-18 09:23:55 +00:00
try {
await room.leave();
await room.forget();
2020-06-03 10:16:01 +00:00
} catch (_) {}
2020-02-18 09:23:55 +00:00
}
}
2020-08-06 09:35:02 +00:00
Logs.success('++++ Check if own olm device is verified by default ++++');
2020-02-19 09:24:54 +00:00
assert(testClientA.userDeviceKeys.containsKey(testUserA));
assert(testClientA.userDeviceKeys[testUserA].deviceKeys
.containsKey(testClientA.deviceID));
assert(testClientA
.userDeviceKeys[testUserA].deviceKeys[testClientA.deviceID].verified);
assert(!testClientA
.userDeviceKeys[testUserA].deviceKeys[testClientA.deviceID].blocked);
assert(testClientB.userDeviceKeys.containsKey(testUserB));
assert(testClientB.userDeviceKeys[testUserB].deviceKeys
.containsKey(testClientB.deviceID));
assert(testClientB
.userDeviceKeys[testUserB].deviceKeys[testClientB.deviceID].verified);
assert(!testClientB
.userDeviceKeys[testUserB].deviceKeys[testClientB.deviceID].blocked);
2020-08-06 09:35:02 +00:00
Logs.success('++++ ($testUserA) Create room and invite $testUserB ++++');
2020-08-11 16:11:51 +00:00
await testClientA.createRoom(invite: [testUserB]);
await Future.delayed(Duration(seconds: 1));
var room = testClientA.rooms.first;
assert(room != null);
final roomId = room.id;
2020-08-06 09:35:02 +00:00
Logs.success('++++ ($testUserB) Join room ++++');
var inviteRoom = testClientB.getRoomById(roomId);
await inviteRoom.join();
await Future.delayed(Duration(seconds: 1));
assert(inviteRoom.membership == Membership.join);
2020-08-06 09:35:02 +00:00
Logs.success('++++ ($testUserA) 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);
2020-08-06 09:35:02 +00:00
Logs.success('++++ ($testUserA) Check known olm devices ++++');
assert(testClientA.userDeviceKeys.containsKey(testUserB));
assert(testClientA.userDeviceKeys[testUserB].deviceKeys
.containsKey(testClientB.deviceID));
2020-02-19 09:24:54 +00:00
assert(!testClientA
.userDeviceKeys[testUserB].deviceKeys[testClientB.deviceID].verified);
assert(!testClientA
.userDeviceKeys[testUserB].deviceKeys[testClientB.deviceID].blocked);
assert(testClientB.userDeviceKeys.containsKey(testUserA));
assert(testClientB.userDeviceKeys[testUserA].deviceKeys
.containsKey(testClientA.deviceID));
assert(!testClientB
.userDeviceKeys[testUserA].deviceKeys[testClientA.deviceID].verified);
assert(!testClientB
.userDeviceKeys[testUserA].deviceKeys[testClientA.deviceID].blocked);
await testClientA.userDeviceKeys[testUserB].deviceKeys[testClientB.deviceID]
2020-05-21 13:32:06 +00:00
.setVerified(true);
2020-02-19 09:24:54 +00:00
2020-08-06 09:35:02 +00:00
Logs.success('++++ Check if own olm device is verified by default ++++');
2020-02-19 09:24:54 +00:00
assert(testClientA.userDeviceKeys.containsKey(testUserA));
assert(testClientA.userDeviceKeys[testUserA].deviceKeys
.containsKey(testClientA.deviceID));
assert(testClientA
.userDeviceKeys[testUserA].deviceKeys[testClientA.deviceID].verified);
assert(testClientB.userDeviceKeys.containsKey(testUserB));
assert(testClientB.userDeviceKeys[testUserB].deviceKeys
.containsKey(testClientB.deviceID));
assert(testClientB
.userDeviceKeys[testUserB].deviceKeys[testClientB.deviceID].verified);
2020-08-06 09:35:02 +00:00
Logs.success("++++ ($testUserA) 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
.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
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);
assert(room.lastMessage == testMessage);
assert(inviteRoom.lastMessage == testMessage);
2020-08-06 09:35:02 +00:00
Logs.success(
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
2020-08-06 09:35:02 +00:00
Logs.success(
"++++ ($testUserA) 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);
2020-02-18 09:23:55 +00:00
assert(room.client.encryption.keyManager
.getOutboundGroupSession(room.id)
.outboundGroupSession
.session_id() ==
currentSessionIdA);
assert(room.client.encryption.keyManager
.getInboundGroupSession(room.id, currentSessionIdA, '') !=
null);
assert(room.lastMessage == testMessage2);
assert(inviteRoom.lastMessage == testMessage2);
2020-08-06 09:35:02 +00:00
Logs.success(
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
2020-08-06 09:35:02 +00:00
Logs.success(
"++++ ($testUserB) 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);
var inviteRoomOutboundGroupSession = inviteRoom.client.encryption.keyManager
.getOutboundGroupSession(inviteRoom.id);
assert(inviteRoomOutboundGroupSession != null);
assert(inviteRoom.client.encryption.keyManager.getInboundGroupSession(
inviteRoom.id,
inviteRoomOutboundGroupSession.outboundGroupSession.session_id(),
'') !=
null);
assert(room.client.encryption.keyManager.getInboundGroupSession(
room.id,
inviteRoomOutboundGroupSession.outboundGroupSession.session_id(),
'') !=
null);
assert(inviteRoom.lastMessage == testMessage3);
assert(room.lastMessage == testMessage3);
2020-08-06 09:35:02 +00:00
Logs.success(
"++++ ($testUserA) Received decrypted message: '${room.lastMessage}' ++++");
2020-08-06 09:35:02 +00:00
Logs.success('++++ Login $testUserB in another client ++++');
2020-08-06 06:55:35 +00:00
var testClientC = Client('TestClientC', database: getDatabase());
2020-02-18 10:17:08 +00:00
await testClientC.checkServer(homeserver);
2020-08-11 16:11:51 +00:00
await testClientC.login(user: testUserB, password: testPasswordA);
2020-02-18 10:17:08 +00:00
await Future.delayed(Duration(seconds: 3));
2020-08-06 09:35:02 +00:00
Logs.success(
"++++ ($testUserA) Send again encrypted message: '$testMessage4' ++++");
2020-02-18 10:17:08 +00:00
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
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);
2020-02-18 10:17:08 +00:00
assert(room.lastMessage == testMessage4);
assert(inviteRoom.lastMessage == testMessage4);
2020-08-06 09:35:02 +00:00
Logs.success(
2020-02-18 10:17:08 +00:00
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
2020-08-06 09:35:02 +00:00
Logs.success('++++ Logout $testUserB another client ++++');
2020-06-03 10:16:01 +00:00
await testClientC.dispose();
2020-02-18 10:17:08 +00:00
await testClientC.logout();
testClientC = null;
await Future.delayed(Duration(seconds: 5));
2020-08-06 09:35:02 +00:00
Logs.success(
"++++ ($testUserA) Send again encrypted message: '$testMessage6' ++++");
2020-02-18 10:17:08 +00:00
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
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);
2020-02-18 10:17:08 +00:00
assert(room.lastMessage == testMessage6);
assert(inviteRoom.lastMessage == testMessage6);
2020-08-06 09:35:02 +00:00
Logs.success(
2020-02-18 10:17:08 +00:00
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
2020-08-06 09:35:02 +00:00
/* Logs.success('++++ ($testUserA) Restore user ++++');
2020-06-03 10:16:01 +00:00
await testClientA.dispose();
2020-02-18 09:23:55 +00:00
testClientA = null;
2020-06-03 10:16:01 +00:00
testClientA = Client(
'TestClientA',
debug: false,
database: getDatabase(),
);
2020-05-15 18:40:17 +00:00
testClientA.connect();
2020-02-18 09:23:55 +00:00
await Future.delayed(Duration(seconds: 3));
var restoredRoom = testClientA.rooms.first;
2020-02-18 09:23:55 +00:00
assert(room != null);
assert(restoredRoom.id == room.id);
assert(restoredRoom.outboundGroupSession.session_id() ==
room.outboundGroupSession.session_id());
2020-05-15 18:40:17 +00:00
assert(restoredRoom.inboundGroupSessions.length == 4);
2020-05-22 10:12:18 +00:00
assert(restoredRoom.inboundGroupSessions.length ==
room.inboundGroupSessions.length);
2020-05-15 18:40:17 +00:00
for (var i = 0; i < restoredRoom.inboundGroupSessions.length; i++) {
assert(restoredRoom.inboundGroupSessions.keys.toList()[i] ==
room.inboundGroupSessions.keys.toList()[i]);
2020-02-18 10:17:08 +00:00
}
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.session_id() ==
testClientB.encryption.olmManager.olmSessions[testClientA.identityKey].first.session_id());
2020-02-18 09:23:55 +00:00
2020-08-06 09:35:02 +00:00
Logs.success("++++ ($testUserA) Send again encrypted message: '$testMessage5' ++++");
2020-02-18 10:17:08 +00:00
await restoredRoom.sendTextEvent(testMessage5);
2020-02-18 09:23:55 +00:00
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.session_id() ==
testClientB.encryption.olmManager.olmSessions[testClientA.identityKey].first.session_id());
2020-02-18 10:17:08 +00:00
assert(restoredRoom.lastMessage == testMessage5);
assert(inviteRoom.lastMessage == testMessage5);
assert(testClientB.getRoomById(roomId).lastMessage == testMessage5);
2020-08-06 09:35:02 +00:00
Logs.success(
2020-06-03 10:16:01 +00:00
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");*/
2020-02-18 09:23:55 +00:00
2020-08-06 09:35:02 +00:00
Logs.success('++++ Logout $testUserA and $testUserB ++++');
await room.leave();
await room.forget();
await inviteRoom.leave();
await inviteRoom.forget();
await Future.delayed(Duration(seconds: 1));
2020-06-03 10:16:01 +00:00
await testClientA.dispose();
await testClientB.dispose();
2020-08-11 16:11:51 +00:00
await testClientA.logoutAll();
await testClientB.logoutAll();
testClientA = null;
testClientB = null;
2020-02-19 09:24:54 +00:00
return;
}