2020-02-15 07:48:41 +00:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
import '../test/fake_store.dart';
|
|
|
|
|
|
|
|
void main() => test();
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
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';
|
2020-02-15 07:48:41 +00:00
|
|
|
|
|
|
|
void test() async {
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ Login $testUserA ++++');
|
|
|
|
var testClientA = Client('TestClient', debug: false);
|
|
|
|
testClientA.storeAPI = FakeStore(testClientA, <String, dynamic>{});
|
2020-02-15 07:48:41 +00:00
|
|
|
await testClientA.checkServer(homeserver);
|
|
|
|
await testClientA.login(testUserA, testPasswordA);
|
2020-02-19 09:24:54 +00:00
|
|
|
assert(testClientA.encryptionEnabled);
|
2020-02-15 07:48:41 +00:00
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ Login $testUserB ++++');
|
|
|
|
var testClientB = Client('TestClient', debug: false);
|
|
|
|
testClientB.storeAPI = FakeStore(testClientB, <String, dynamic>{});
|
2020-02-15 07:48:41 +00:00
|
|
|
await testClientB.checkServer(homeserver);
|
|
|
|
await testClientB.login(testUserB, testPasswordA);
|
2020-02-19 09:24:54 +00:00
|
|
|
assert(testClientB.encryptionEnabled);
|
2020-02-15 07:48:41 +00:00
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ ($testUserA) Leave all rooms ++++');
|
2020-02-15 07:48:41 +00:00
|
|
|
while (testClientA.rooms.isNotEmpty) {
|
2020-03-30 09:08:38 +00:00
|
|
|
var room = testClientA.rooms.first;
|
2020-02-15 07:48:41 +00:00
|
|
|
if (room.canonicalAlias?.isNotEmpty ?? false) {
|
|
|
|
break;
|
|
|
|
}
|
2020-02-18 09:23:55 +00:00
|
|
|
try {
|
|
|
|
await room.leave();
|
|
|
|
await room.forget();
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
2020-02-15 07:48:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ ($testUserB) Leave all rooms ++++');
|
|
|
|
for (var i = 0; i < 3; i++) {
|
2020-02-18 09:23:55 +00:00
|
|
|
if (testClientB.rooms.isNotEmpty) {
|
2020-03-30 09:08:38 +00:00
|
|
|
var room = testClientB.rooms.first;
|
2020-02-18 09:23:55 +00:00
|
|
|
try {
|
|
|
|
await room.leave();
|
|
|
|
await room.forget();
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 07:48:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ 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-03-30 09:08:38 +00:00
|
|
|
print('++++ ($testUserA) Create room and invite $testUserB ++++');
|
2020-02-15 07:48:41 +00:00
|
|
|
await testClientA.createRoom(invite: [User(testUserB)]);
|
|
|
|
await Future.delayed(Duration(seconds: 1));
|
2020-03-30 09:08:38 +00:00
|
|
|
var room = testClientA.rooms.first;
|
2020-02-15 07:48:41 +00:00
|
|
|
assert(room != null);
|
2020-03-30 09:08:38 +00:00
|
|
|
final roomId = room.id;
|
2020-02-15 07:48:41 +00:00
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ ($testUserB) Join room ++++');
|
|
|
|
var inviteRoom = testClientB.getRoomById(roomId);
|
2020-02-15 07:48:41 +00:00
|
|
|
await inviteRoom.join();
|
|
|
|
await Future.delayed(Duration(seconds: 1));
|
|
|
|
assert(inviteRoom.membership == Membership.join);
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ ($testUserA) Enable encryption ++++');
|
2020-02-15 07:48:41 +00:00
|
|
|
assert(room.encrypted == false);
|
|
|
|
await room.enableEncryption();
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
assert(room.encrypted == true);
|
|
|
|
assert(room.outboundGroupSession == null);
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ ($testUserA) Check known olm devices ++++');
|
2020-02-15 07:48:41 +00:00
|
|
|
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]
|
|
|
|
.setVerified(true, testClientA);
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ 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-02-15 07:48:41 +00:00
|
|
|
|
|
|
|
print("++++ ($testUserA) Send encrypted message: '$testMessage' ++++");
|
|
|
|
await room.sendTextEvent(testMessage);
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
assert(room.outboundGroupSession != null);
|
2020-03-30 09:08:38 +00:00
|
|
|
var currentSessionIdA = room.outboundGroupSession.session_id();
|
2020-02-15 07:48:41 +00:00
|
|
|
assert(room.sessionKeys.containsKey(room.outboundGroupSession.session_id()));
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].length == 1);
|
|
|
|
assert(testClientB.olmSessions[testClientA.identityKey].length == 1);
|
2020-02-18 09:23:55 +00:00
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].first.session_id() ==
|
|
|
|
testClientB.olmSessions[testClientA.identityKey].first.session_id());
|
2020-02-15 07:48:41 +00:00
|
|
|
assert(inviteRoom.sessionKeys
|
|
|
|
.containsKey(room.outboundGroupSession.session_id()));
|
|
|
|
assert(room.lastMessage == testMessage);
|
|
|
|
assert(inviteRoom.lastMessage == testMessage);
|
|
|
|
print(
|
|
|
|
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
|
|
|
|
|
|
|
|
print("++++ ($testUserA) Send again encrypted message: '$testMessage2' ++++");
|
|
|
|
await room.sendTextEvent(testMessage2);
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].length == 1);
|
|
|
|
assert(testClientB.olmSessions[testClientA.identityKey].length == 1);
|
2020-02-18 09:23:55 +00:00
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].first.session_id() ==
|
|
|
|
testClientB.olmSessions[testClientA.identityKey].first.session_id());
|
|
|
|
|
2020-02-15 07:48:41 +00:00
|
|
|
assert(room.outboundGroupSession.session_id() == currentSessionIdA);
|
|
|
|
assert(inviteRoom.sessionKeys
|
|
|
|
.containsKey(room.outboundGroupSession.session_id()));
|
|
|
|
assert(room.lastMessage == testMessage2);
|
|
|
|
assert(inviteRoom.lastMessage == testMessage2);
|
|
|
|
print(
|
|
|
|
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
|
|
|
|
|
|
|
|
print("++++ ($testUserB) Send again encrypted message: '$testMessage3' ++++");
|
|
|
|
await inviteRoom.sendTextEvent(testMessage3);
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].length == 1);
|
|
|
|
assert(testClientB.olmSessions[testClientA.identityKey].length == 1);
|
|
|
|
assert(room.outboundGroupSession.session_id() == currentSessionIdA);
|
|
|
|
assert(inviteRoom.outboundGroupSession != null);
|
|
|
|
assert(inviteRoom.sessionKeys
|
|
|
|
.containsKey(inviteRoom.outboundGroupSession.session_id()));
|
|
|
|
assert(room.sessionKeys
|
|
|
|
.containsKey(inviteRoom.outboundGroupSession.session_id()));
|
|
|
|
assert(inviteRoom.lastMessage == testMessage3);
|
|
|
|
assert(room.lastMessage == testMessage3);
|
|
|
|
print(
|
|
|
|
"++++ ($testUserA) Received decrypted message: '${room.lastMessage}' ++++");
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ Login $testUserB in another client ++++');
|
|
|
|
var testClientC = Client('TestClient', debug: false);
|
|
|
|
testClientC.storeAPI = FakeStore(testClientC, <String, dynamic>{});
|
2020-02-18 10:17:08 +00:00
|
|
|
await testClientC.checkServer(homeserver);
|
|
|
|
await testClientC.login(testUserB, testPasswordA);
|
|
|
|
await Future.delayed(Duration(seconds: 3));
|
|
|
|
assert(room.outboundGroupSession == null);
|
|
|
|
|
|
|
|
print("++++ ($testUserA) Send again encrypted message: '$testMessage4' ++++");
|
|
|
|
await room.sendTextEvent(testMessage4);
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].length == 1);
|
|
|
|
assert(testClientB.olmSessions[testClientA.identityKey].length == 1);
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].first.session_id() ==
|
|
|
|
testClientB.olmSessions[testClientA.identityKey].first.session_id());
|
|
|
|
assert(testClientA.olmSessions[testClientC.identityKey].length == 1);
|
|
|
|
assert(testClientC.olmSessions[testClientA.identityKey].length == 1);
|
|
|
|
assert(testClientA.olmSessions[testClientC.identityKey].first.session_id() ==
|
|
|
|
testClientC.olmSessions[testClientA.identityKey].first.session_id());
|
|
|
|
assert(room.outboundGroupSession.session_id() != currentSessionIdA);
|
|
|
|
currentSessionIdA = room.outboundGroupSession.session_id();
|
|
|
|
assert(inviteRoom.sessionKeys
|
|
|
|
.containsKey(room.outboundGroupSession.session_id()));
|
|
|
|
assert(room.lastMessage == testMessage4);
|
|
|
|
assert(inviteRoom.lastMessage == testMessage4);
|
|
|
|
print(
|
|
|
|
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ Logout $testUserB another client ++++');
|
2020-02-18 10:17:08 +00:00
|
|
|
await testClientC.logout();
|
|
|
|
testClientC = null;
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
assert(room.outboundGroupSession == null);
|
|
|
|
assert(inviteRoom.outboundGroupSession == null);
|
|
|
|
|
|
|
|
print("++++ ($testUserA) Send again encrypted message: '$testMessage6' ++++");
|
|
|
|
await room.sendTextEvent(testMessage6);
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].length == 1);
|
|
|
|
assert(testClientB.olmSessions[testClientA.identityKey].length == 1);
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].first.session_id() ==
|
|
|
|
testClientB.olmSessions[testClientA.identityKey].first.session_id());
|
|
|
|
assert(room.outboundGroupSession.session_id() != currentSessionIdA);
|
|
|
|
currentSessionIdA = room.outboundGroupSession.session_id();
|
|
|
|
assert(inviteRoom.sessionKeys
|
|
|
|
.containsKey(room.outboundGroupSession.session_id()));
|
|
|
|
assert(room.lastMessage == testMessage6);
|
|
|
|
assert(inviteRoom.lastMessage == testMessage6);
|
|
|
|
print(
|
|
|
|
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ ($testUserA) Restore user ++++');
|
2020-02-18 09:23:55 +00:00
|
|
|
FakeStore clientAStore = testClientA.storeAPI;
|
|
|
|
testClientA = null;
|
2020-03-30 09:08:38 +00:00
|
|
|
testClientA = Client('TestClient', debug: false);
|
2020-02-18 09:23:55 +00:00
|
|
|
testClientA.storeAPI = FakeStore(testClientA, clientAStore.storeMap);
|
|
|
|
await Future.delayed(Duration(seconds: 3));
|
2020-03-30 09:08:38 +00:00
|
|
|
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-02-18 10:17:08 +00:00
|
|
|
assert(restoredRoom.sessionKeys.length == 4);
|
|
|
|
assert(restoredRoom.sessionKeys.length == room.sessionKeys.length);
|
2020-03-30 09:08:38 +00:00
|
|
|
for (var i = 0; i < restoredRoom.sessionKeys.length; i++) {
|
2020-02-18 10:17:08 +00:00
|
|
|
assert(restoredRoom.sessionKeys.keys.toList()[i] ==
|
|
|
|
room.sessionKeys.keys.toList()[i]);
|
|
|
|
}
|
2020-02-18 09:23:55 +00:00
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].length == 1);
|
|
|
|
assert(testClientB.olmSessions[testClientA.identityKey].length == 1);
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].first.session_id() ==
|
|
|
|
testClientB.olmSessions[testClientA.identityKey].first.session_id());
|
|
|
|
|
2020-02-18 10:17:08 +00:00
|
|
|
print("++++ ($testUserA) Send again encrypted message: '$testMessage5' ++++");
|
|
|
|
await restoredRoom.sendTextEvent(testMessage5);
|
2020-02-18 09:23:55 +00:00
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].length == 1);
|
|
|
|
assert(testClientB.olmSessions[testClientA.identityKey].length == 1);
|
|
|
|
assert(testClientA.olmSessions[testClientB.identityKey].first.session_id() ==
|
|
|
|
testClientB.olmSessions[testClientA.identityKey].first.session_id());
|
2020-02-18 10:17:08 +00:00
|
|
|
/*assert(restoredRoom.outboundGroupSession.session_id() == currentSessionIdA);
|
2020-02-18 09:23:55 +00:00
|
|
|
assert(inviteRoom.sessionKeys
|
2020-02-18 10:17:08 +00:00
|
|
|
.containsKey(restoredRoom.outboundGroupSession.session_id()));*/
|
|
|
|
assert(restoredRoom.lastMessage == testMessage5);
|
|
|
|
assert(inviteRoom.lastMessage == testMessage5);
|
|
|
|
assert(testClientB.getRoomById(roomId).lastMessage == testMessage5);
|
2020-02-18 09:23:55 +00:00
|
|
|
print(
|
|
|
|
"++++ ($testUserB) Received decrypted message: '${inviteRoom.lastMessage}' ++++");
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
print('++++ Logout $testUserA and $testUserB ++++');
|
2020-02-15 07:48:41 +00:00
|
|
|
await room.leave();
|
|
|
|
await room.forget();
|
|
|
|
await inviteRoom.leave();
|
|
|
|
await inviteRoom.forget();
|
|
|
|
await Future.delayed(Duration(seconds: 1));
|
|
|
|
await testClientA.jsonRequest(
|
2020-03-30 09:08:38 +00:00
|
|
|
type: HTTPType.POST, action: '/client/r0/logout/all');
|
2020-02-15 07:48:41 +00:00
|
|
|
await testClientB.jsonRequest(
|
2020-03-30 09:08:38 +00:00
|
|
|
type: HTTPType.POST, action: '/client/r0/logout/all');
|
2020-02-15 07:48:41 +00:00
|
|
|
testClientA = null;
|
|
|
|
testClientB = null;
|
2020-02-19 09:24:54 +00:00
|
|
|
return;
|
2020-02-15 07:48:41 +00:00
|
|
|
}
|