chore: use proper matchers in integration tests

This commit is contained in:
Nicolas Werner 2022-12-12 05:23:38 +01:00
parent 90e8d5b118
commit 8c2d7d78d8
No known key found for this signature in database
3 changed files with 349 additions and 286 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@
*.swo
*.swn
*.dylib
*.bakmacoscompat
.DS_Store
.atom/
.buildlog/

View File

@ -382,6 +382,14 @@ class KeyManager {
.where((e) => !e.value)
.map((e) => e.key))
: <String>{};
// check if a device got removed
if (oldDeviceIds.difference(newDeviceIds).isNotEmpty) {
wipe = true;
break;
}
// check if any new devices need keys
final newDevices = newDeviceIds.difference(oldDeviceIds);
if (newDeviceIds.isNotEmpty) {
devicesToReceive.addAll(newDeviceKeys.where(

View File

@ -16,14 +16,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'dart:io';
import 'package:hive/hive.dart';
import 'package:olm/olm.dart' as olm;
import 'package:test/test.dart';
import 'package:matrix/matrix.dart';
import '../test/fake_database.dart';
import 'test_config.dart';
void main() => test();
const String testMessage = 'Hello world';
const String testMessage2 = 'Hello moon';
const String testMessage3 = 'Hello sun';
@ -31,7 +33,8 @@ const String testMessage4 = 'Hello star';
const String testMessage5 = 'Hello earth';
const String testMessage6 = 'Hello mars';
void test() async {
void main() => group('Integration tests', () {
test('E2EE', () async {
Client? testClientA, testClientB;
try {
@ -45,19 +48,19 @@ void test() async {
testClientA = Client('TestClientA', databaseBuilder: getDatabase);
await testClientA.checkHomeserver(Uri.parse(TestUser.homeserver));
await testClientA.login(LoginType.mLoginPassword,
identifier:
AuthenticationUserIdentifier(user: TestUser.username.localpart!),
identifier: AuthenticationUserIdentifier(
user: TestUser.username.localpart!),
password: TestUser.password);
assert(testClientA.encryptionEnabled);
expect(testClientA.encryptionEnabled, true);
Logs().i('++++ Login Bob ++++');
testClientB = Client('TestClientB', databaseBuilder: getDatabase);
await testClientB.checkHomeserver(Uri.parse(TestUser.homeserver));
await testClientB.login(LoginType.mLoginPassword,
identifier:
AuthenticationUserIdentifier(user: TestUser.username2.localpart!),
identifier: AuthenticationUserIdentifier(
user: TestUser.username2.localpart!),
password: TestUser.password2);
assert(testClientB.encryptionEnabled);
expect(testClientB.encryptionEnabled, true);
Logs().i('++++ (Alice) Leave all rooms ++++');
while (testClientA.rooms.isNotEmpty) {
@ -83,20 +86,28 @@ 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
.containsKey(testClientA.deviceID));
assert(testClientA.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.verified);
assert(!testClientA.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.blocked);
assert(testClientB.userDeviceKeys.containsKey(TestUser.username2));
assert(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys
.containsKey(testClientB.deviceID));
assert(testClientB.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.verified);
assert(!testClientB.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.blocked);
expect(testClientA.userDeviceKeys, contains(TestUser.username));
expect(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys,
contains(testClientA.deviceID));
expect(
testClientA.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.verified,
isTrue);
expect(
!testClientA.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.blocked,
isTrue);
expect(testClientB.userDeviceKeys, contains(TestUser.username2));
expect(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys,
contains(testClientB.deviceID));
expect(
testClientB.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.verified,
isTrue);
expect(
!testClientB.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.blocked,
isTrue);
Logs().i('++++ (Alice) Create room and invite Bob ++++');
await testClientA.startDirectChat(
@ -111,225 +122,267 @@ void test() async {
final inviteRoom = testClientB.getRoomById(roomId)!;
await inviteRoom.join();
await Future.delayed(Duration(seconds: 1));
assert(inviteRoom.membership == Membership.join);
expect(inviteRoom.membership, Membership.join);
Logs().i('++++ (Alice) Enable encryption ++++');
assert(room.encrypted == false);
expect(room.encrypted, false);
await room.enableEncryption();
await Future.delayed(Duration(seconds: 5));
assert(room.encrypted == true);
assert(
room.client.encryption!.keyManager.getOutboundGroupSession(room.id) ==
expect(room.encrypted, isTrue);
expect(
room.client.encryption!.keyManager
.getOutboundGroupSession(room.id),
null);
Logs().i('++++ (Alice) Check known olm devices ++++');
assert(testClientA.userDeviceKeys.containsKey(TestUser.username2));
assert(testClientA.userDeviceKeys[TestUser.username2]!.deviceKeys
.containsKey(testClientB.deviceID));
assert(!testClientA.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.verified);
assert(!testClientA.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.blocked);
assert(testClientB.userDeviceKeys.containsKey(TestUser.username));
assert(testClientB.userDeviceKeys[TestUser.username]!.deviceKeys
.containsKey(testClientA.deviceID));
assert(!testClientB.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.verified);
assert(!testClientB.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.blocked);
expect(testClientA.userDeviceKeys, contains(TestUser.username2));
expect(testClientA.userDeviceKeys[TestUser.username2]!.deviceKeys,
contains(testClientB.deviceID));
expect(
testClientA.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.verified,
isFalse);
expect(
testClientA.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.blocked,
isFalse);
expect(testClientB.userDeviceKeys, contains(TestUser.username));
expect(testClientB.userDeviceKeys[TestUser.username]!.deviceKeys,
contains(testClientA.deviceID));
expect(
testClientB.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.verified,
isFalse);
expect(
testClientB.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.blocked,
isFalse);
await Future.wait([
testClientA.updateUserDeviceKeys(),
testClientB.updateUserDeviceKeys(),
]);
await testClientA
.userDeviceKeys[TestUser.username2]!.deviceKeys[testClientB.deviceID!]!
await testClientA.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!
.setVerified(true);
Logs().i('++++ Check if own olm device is verified by default ++++');
assert(testClientA.userDeviceKeys.containsKey(TestUser.username));
assert(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys
.containsKey(testClientA.deviceID));
assert(testClientA.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.verified);
assert(testClientB.userDeviceKeys.containsKey(TestUser.username2));
assert(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys
.containsKey(testClientB.deviceID));
assert(testClientB.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.verified);
expect(testClientA.userDeviceKeys, contains(TestUser.username));
expect(testClientA.userDeviceKeys[TestUser.username]!.deviceKeys,
contains(testClientA.deviceID));
expect(
testClientA.userDeviceKeys[TestUser.username]!
.deviceKeys[testClientA.deviceID!]!.verified,
isTrue);
expect(testClientB.userDeviceKeys, contains(TestUser.username2));
expect(testClientB.userDeviceKeys[TestUser.username2]!.deviceKeys,
contains(testClientB.deviceID));
expect(
testClientB.userDeviceKeys[TestUser.username2]!
.deviceKeys[testClientB.deviceID!]!.verified,
isTrue);
Logs().i("++++ (Alice) Send encrypted message: '$testMessage' ++++");
await room.sendTextEvent(testMessage);
await Future.delayed(Duration(seconds: 5));
assert(
room.client.encryption!.keyManager.getOutboundGroupSession(room.id) !=
null);
expect(
room.client.encryption!.keyManager
.getOutboundGroupSession(room.id),
isNotNull);
var currentSessionIdA = room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id();
/*assert(room.client.encryption.keyManager
/*expect(room.client.encryption.keyManager
.getInboundGroupSession(room.id, currentSessionIdA, '') !=
null);*/
assert(testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length ==
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length,
1);
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
expect(
testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length,
1);
assert(testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.first.sessionId ==
testClientB.encryption!.olmManager.olmSessions[testClientA.identityKey]!
.first.sessionId);
/*assert(inviteRoom.client.encryption.keyManager
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.first.sessionId,
testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.first.sessionId);
/*expect(inviteRoom.client.encryption.keyManager
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);*/
assert(room.lastEvent!.body == testMessage);
assert(inviteRoom.lastEvent!.body == testMessage);
expect(room.lastEvent!.body, testMessage);
expect(inviteRoom.lastEvent!.body, testMessage);
Logs().i(
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++");
Logs().i("++++ (Alice) Send again encrypted message: '$testMessage2' ++++");
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 ==
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length,
1);
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
expect(
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);
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.first.sessionId,
testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.first.sessionId);
assert(room.client.encryption!.keyManager
expect(
room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id() ==
.session_id(),
currentSessionIdA);
/*assert(room.client.encryption.keyManager
/*expect(room.client.encryption.keyManager
.getInboundGroupSession(room.id, currentSessionIdA, '') !=
null);*/
assert(room.lastEvent!.body == testMessage2);
assert(inviteRoom.lastEvent!.body == testMessage2);
expect(room.lastEvent!.body, testMessage2);
expect(inviteRoom.lastEvent!.body, testMessage2);
Logs().i(
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++");
Logs().i("++++ (Bob) Send again encrypted message: '$testMessage3' ++++");
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 ==
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length,
1);
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
expect(
testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length,
1);
assert(room.client.encryption!.keyManager
expect(
room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id() ==
.session_id(),
currentSessionIdA);
final inviteRoomOutboundGroupSession = inviteRoom
.client.encryption!.keyManager
.getOutboundGroupSession(inviteRoom.id)!;
assert(inviteRoomOutboundGroupSession.isValid);
/*assert(inviteRoom.client.encryption.keyManager.getInboundGroupSession(
expect(inviteRoomOutboundGroupSession.isValid, isTrue);
/*expect(inviteRoom.client.encryption.keyManager.getInboundGroupSession(
inviteRoom.id,
inviteRoomOutboundGroupSession.outboundGroupSession.session_id(),
'') !=
null);
assert(room.client.encryption.keyManager.getInboundGroupSession(
expect(room.client.encryption.keyManager.getInboundGroupSession(
room.id,
inviteRoomOutboundGroupSession.outboundGroupSession.session_id(),
'') !=
null);*/
assert(inviteRoom.lastEvent!.body == testMessage3);
assert(room.lastEvent!.body == testMessage3);
expect(inviteRoom.lastEvent!.body, testMessage3);
expect(room.lastEvent!.body, testMessage3);
Logs().i(
"++++ (Alice) Received decrypted message: '${room.lastEvent!.body}' ++++");
Logs().i('++++ Login Bob in another client ++++');
final testClientC = Client('TestClientC', databaseBuilder: getDatabase);
final testClientC =
Client('TestClientC', databaseBuilder: getDatabase);
await testClientC.checkHomeserver(Uri.parse(TestUser.homeserver));
// Workaround: Logging in with displayname instead of mxid
// We can't sign in using the displayname, since that breaks e2ee on dendrite: https://github.com/matrix-org/dendrite/issues/2914
await testClientC.login(LoginType.mLoginPassword,
identifier: AuthenticationUserIdentifier(user: TestUser.displayname2),
identifier: AuthenticationUserIdentifier(
user: TestUser.username2.localpart!),
password: TestUser.password2);
await Future.delayed(Duration(seconds: 3));
Logs().i("++++ (Alice) Send again encrypted message: '$testMessage4' ++++");
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 ==
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length,
1);
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
expect(
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 ==
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.first.sessionId,
testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.first.sessionId);
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientC.identityKey]!.length,
1);
assert(testClientC.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
expect(
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
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientC.identityKey]!.first.sessionId,
testClientC.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.first.sessionId);
expect(
room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id() !=
.session_id(),
currentSessionIdA);
currentSessionIdA = room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id();
/*assert(inviteRoom.client.encryption.keyManager
/*expect(inviteRoom.client.encryption.keyManager
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);*/
assert(room.lastEvent!.body == testMessage4);
assert(inviteRoom.lastEvent!.body == testMessage4);
expect(room.lastEvent!.body, testMessage4);
expect(inviteRoom.lastEvent!.body, testMessage4);
Logs().i(
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++");
Logs().i('++++ Logout Bob another client ++++');
Logs()
.i('++++ Logout Bob another client ${testClientC.deviceID} ++++');
await testClientC.dispose(closeDatabase: false);
await testClientC.logout();
await Future.delayed(Duration(seconds: 5));
Logs().i("++++ (Alice) Send again encrypted message: '$testMessage6' ++++");
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 ==
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.length,
1);
assert(testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.length ==
expect(
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
expect(
testClientA.encryption!.olmManager
.olmSessions[testClientB.identityKey]!.first.sessionId,
testClientB.encryption!.olmManager
.olmSessions[testClientA.identityKey]!.first.sessionId);
// This does not work on conduit because of a server bug: https://gitlab.com/famedly/conduit/-/issues/325
if (Platform.environment['HOMESERVER'] != 'conduit') {
expect(
room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id() !=
currentSessionIdA);
.session_id(),
isNot(currentSessionIdA));
}
currentSessionIdA = room.client.encryption!.keyManager
.getOutboundGroupSession(room.id)!
.outboundGroupSession!
.session_id();
/*assert(inviteRoom.client.encryption.keyManager
/*expect(inviteRoom.client.encryption.keyManager
.getInboundGroupSession(inviteRoom.id, currentSessionIdA, '') !=
null);*/
assert(room.lastEvent!.body == testMessage6);
assert(inviteRoom.lastEvent!.body == testMessage6);
expect(room.lastEvent!.body, testMessage6);
expect(inviteRoom.lastEvent!.body, testMessage6);
Logs().i(
"++++ (Bob) Received decrypted message: '${inviteRoom.lastEvent!.body}' ++++");
@ -351,4 +404,5 @@ void test() async {
testClientB = null;
}
return;
}
});
}, timeout: Timeout(Duration(minutes: 6)));