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';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'package:olm/olm.dart' as olm;
|
|
|
|
|
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;
|
|
|
|
print('[LibOlm] Failed to load LibOlm: ' + _.toString());
|
|
|
|
}
|
|
|
|
print('[LibOlm] Enabled: $olmEnabled');
|
|
|
|
|
|
|
|
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(
|
|
|
|
client.encryption.olmManager.checkJsonSignature(client.fingerprintKey,
|
|
|
|
signedPayload, client.userID, client.deviceID),
|
|
|
|
true);
|
|
|
|
expect(
|
|
|
|
client.encryption.olmManager.checkJsonSignature(
|
|
|
|
client.fingerprintKey, payload, client.userID, client.deviceID),
|
|
|
|
false);
|
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
|
|
|
|
|
|
|
test('startOutgoingOlmSessions', () async {
|
|
|
|
// start an olm session.....with ourself!
|
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
|
|
|
});
|
|
|
|
}
|