famedlysdk/test/encryption/olm_manager_test.dart

133 lines
4.7 KiB
Dart
Raw Permalink Normal View History

2020-06-04 16:36:07 +00:00
/*
* Ansible inventory script used at Famedly GmbH for managing many hosts
* Copyright (C) 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 'dart:convert';
import 'package:famedlysdk/famedlysdk.dart';
2020-08-06 09:35:02 +00:00
import 'package:famedlysdk/src/utils/logs.dart';
2020-06-04 16:36:07 +00:00
import 'package:test/test.dart';
import 'package:olm/olm.dart' as olm;
2020-09-16 08:39:04 +00:00
import 'package:famedlysdk/encryption/utils/json_signature_check_extension.dart';
2020-06-04 16:36:07 +00:00
2020-06-05 07:59:37 +00:00
import '../fake_client.dart';
2020-06-04 16:36:07 +00:00
import '../fake_matrix_api.dart';
void main() {
group('Olm Manager', () {
var olmEnabled = true;
try {
olm.init();
olm.Account();
} catch (_) {
olmEnabled = false;
2020-08-06 09:35:02 +00:00
Logs.warning('[LibOlm] Failed to load LibOlm: ' + _.toString());
2020-06-04 16:36:07 +00:00
}
2020-08-06 09:35:02 +00:00
Logs.success('[LibOlm] Enabled: $olmEnabled');
2020-06-04 16:36:07 +00:00
if (!olmEnabled) return;
2020-06-05 07:59:37 +00:00
Client client;
2020-06-04 16:36:07 +00:00
test('setupClient', () async {
2020-06-05 07:59:37 +00:00
client = await getClient();
2020-06-04 16:36:07 +00:00
});
test('signatures', () async {
final payload = <String, dynamic>{
'fox': 'floof',
};
final signedPayload = client.encryption.olmManager.signJson(payload);
2020-06-04 18:16:18 +00:00
expect(
2020-09-16 08:39:04 +00:00
signedPayload.checkJsonSignature(
client.fingerprintKey, client.userID, client.deviceID),
2020-06-04 18:16:18 +00:00
true);
2020-06-04 16:36:07 +00:00
});
test('uploadKeys', () async {
FakeMatrixApi.calledEndpoints.clear();
2020-06-04 18:16:18 +00:00
final res =
await client.encryption.olmManager.uploadKeys(uploadDeviceKeys: true);
2020-06-04 16:36:07 +00:00
expect(res, true);
2020-06-04 18:16:18 +00:00
var sent = json.decode(
FakeMatrixApi.calledEndpoints['/client/r0/keys/upload'].first);
2020-06-04 16:36:07 +00:00
expect(sent['device_keys'] != null, true);
expect(sent['one_time_keys'] != null, true);
expect(sent['one_time_keys'].keys.length, 66);
FakeMatrixApi.calledEndpoints.clear();
await client.encryption.olmManager.uploadKeys();
2020-06-04 18:16:18 +00:00
sent = json.decode(
FakeMatrixApi.calledEndpoints['/client/r0/keys/upload'].first);
2020-06-04 16:36:07 +00:00
expect(sent['device_keys'] != null, false);
FakeMatrixApi.calledEndpoints.clear();
await client.encryption.olmManager.uploadKeys(oldKeyCount: 20);
2020-06-04 18:16:18 +00:00
sent = json.decode(
FakeMatrixApi.calledEndpoints['/client/r0/keys/upload'].first);
2020-06-04 16:36:07 +00:00
expect(sent['one_time_keys'].keys.length, 46);
});
test('handleDeviceOneTimeKeysCount', () async {
FakeMatrixApi.calledEndpoints.clear();
2020-06-04 18:16:18 +00:00
client.encryption.olmManager
.handleDeviceOneTimeKeysCount({'signed_curve25519': 20});
2020-06-04 16:36:07 +00:00
await Future.delayed(Duration(milliseconds: 50));
2020-06-04 18:16:18 +00:00
expect(
FakeMatrixApi.calledEndpoints.containsKey('/client/r0/keys/upload'),
true);
2020-06-04 16:36:07 +00:00
FakeMatrixApi.calledEndpoints.clear();
2020-06-04 18:16:18 +00:00
client.encryption.olmManager
.handleDeviceOneTimeKeysCount({'signed_curve25519': 70});
2020-06-04 16:36:07 +00:00
await Future.delayed(Duration(milliseconds: 50));
2020-06-04 18:16:18 +00:00
expect(
FakeMatrixApi.calledEndpoints.containsKey('/client/r0/keys/upload'),
false);
2020-06-04 16:36:07 +00:00
});
2020-06-05 08:21:45 +00:00
2020-06-23 09:35:43 +00:00
test('restoreOlmSession', () async {
client.encryption.olmManager.olmSessions.clear();
await client.encryption.olmManager
.restoreOlmSession(client.userID, client.identityKey);
expect(client.encryption.olmManager.olmSessions.length, 1);
client.encryption.olmManager.olmSessions.clear();
await client.encryption.olmManager
.restoreOlmSession(client.userID, 'invalid');
expect(client.encryption.olmManager.olmSessions.length, 0);
client.encryption.olmManager.olmSessions.clear();
await client.encryption.olmManager
.restoreOlmSession('invalid', client.identityKey);
expect(client.encryption.olmManager.olmSessions.length, 0);
});
2020-06-05 08:21:45 +00:00
test('startOutgoingOlmSessions', () async {
// start an olm session.....with ourself!
2020-06-23 09:35:43 +00:00
client.encryption.olmManager.olmSessions.clear();
2020-06-05 08:56:51 +00:00
await client.encryption.olmManager.startOutgoingOlmSessions(
[client.userDeviceKeys[client.userID].deviceKeys[client.deviceID]]);
expect(
client.encryption.olmManager.olmSessions
.containsKey(client.identityKey),
true);
2020-06-05 08:21:45 +00:00
});
2020-06-05 09:32:02 +00:00
test('dispose client', () async {
await client.dispose(closeDatabase: true);
});
2020-06-04 16:36:07 +00:00
});
}