2020-02-04 13:41:13 +00:00
|
|
|
/*
|
2020-06-03 10:16:01 +00:00
|
|
|
* Ansible inventory script used at Famedly GmbH for managing many hosts
|
|
|
|
* Copyright (C) 2019, 2020 Famedly GmbH
|
2020-02-04 13:41:13 +00:00
|
|
|
*
|
2020-06-03 10:16:01 +00:00
|
|
|
* 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.
|
2020-02-04 13:41:13 +00:00
|
|
|
*
|
2020-06-03 10:16:01 +00:00
|
|
|
* 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.
|
2020-02-04 13:41:13 +00:00
|
|
|
*
|
2020-06-03 10:16:01 +00:00
|
|
|
* 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/>.
|
2020-02-04 13:41:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2020-08-06 09:35:02 +00:00
|
|
|
import 'package:famedlysdk/src/utils/logs.dart';
|
2020-02-04 13:41:13 +00:00
|
|
|
import 'package:test/test.dart';
|
2020-06-15 08:26:50 +00:00
|
|
|
import 'package:olm/olm.dart' as olm;
|
|
|
|
|
|
|
|
import './fake_client.dart';
|
|
|
|
import './fake_matrix_api.dart';
|
2020-02-04 13:41:13 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
/// All Tests related to device keys
|
2020-03-30 09:08:38 +00:00
|
|
|
group('Device keys', () {
|
|
|
|
test('fromJson', () async {
|
|
|
|
var rawJson = <String, dynamic>{
|
|
|
|
'user_id': '@alice:example.com',
|
|
|
|
'device_id': 'JLAFKJWSCS',
|
|
|
|
'algorithms': ['m.olm.v1.curve25519-aes-sha2', 'm.megolm.v1.aes-sha2'],
|
|
|
|
'keys': {
|
|
|
|
'curve25519:JLAFKJWSCS':
|
|
|
|
'3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI',
|
|
|
|
'ed25519:JLAFKJWSCS': 'lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI'
|
2020-02-04 13:41:13 +00:00
|
|
|
},
|
2020-03-30 09:08:38 +00:00
|
|
|
'signatures': {
|
|
|
|
'@alice:example.com': {
|
|
|
|
'ed25519:JLAFKJWSCS':
|
|
|
|
'dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/a+myXS367WT6NAIcBA'
|
2020-02-04 13:41:13 +00:00
|
|
|
}
|
|
|
|
},
|
2020-03-30 09:08:38 +00:00
|
|
|
'unsigned': {'device_display_name': "Alice's mobile phone"},
|
2020-02-04 13:41:13 +00:00
|
|
|
};
|
|
|
|
|
2020-05-22 09:13:58 +00:00
|
|
|
final key = DeviceKeys.fromJson(rawJson, null);
|
2020-06-06 11:47:37 +00:00
|
|
|
await key.setVerified(false, false);
|
|
|
|
await key.setBlocked(true);
|
2020-05-22 11:18:45 +00:00
|
|
|
expect(json.encode(key.toJson()), json.encode(rawJson));
|
2020-06-05 20:03:28 +00:00
|
|
|
expect(key.directVerified, false);
|
2020-05-22 09:13:58 +00:00
|
|
|
expect(key.blocked, true);
|
2020-06-15 08:26:50 +00:00
|
|
|
|
|
|
|
rawJson = <String, dynamic>{
|
|
|
|
'user_id': '@test:fakeServer.notExisting',
|
|
|
|
'usage': ['master'],
|
|
|
|
'keys': {
|
|
|
|
'ed25519:82mAXjsmbTbrE6zyShpR869jnrANO75H8nYY0nDLoJ8':
|
|
|
|
'82mAXjsmbTbrE6zyShpR869jnrANO75H8nYY0nDLoJ8',
|
|
|
|
},
|
|
|
|
'signatures': {},
|
|
|
|
};
|
|
|
|
final crossKey = CrossSigningKey.fromJson(rawJson, null);
|
|
|
|
expect(json.encode(crossKey.toJson()), json.encode(rawJson));
|
|
|
|
expect(crossKey.usage.first, 'master');
|
|
|
|
});
|
|
|
|
|
|
|
|
var olmEnabled = true;
|
|
|
|
try {
|
|
|
|
olm.init();
|
|
|
|
olm.Account();
|
|
|
|
} catch (_) {
|
|
|
|
olmEnabled = false;
|
2020-08-06 09:35:02 +00:00
|
|
|
Logs.error('[LibOlm] Failed to load LibOlm: ' + _.toString());
|
2020-06-15 08:26:50 +00:00
|
|
|
}
|
2020-08-06 09:35:02 +00:00
|
|
|
Logs.success('[LibOlm] Enabled: $olmEnabled');
|
2020-06-15 08:26:50 +00:00
|
|
|
|
|
|
|
if (!olmEnabled) return;
|
|
|
|
|
|
|
|
Client client;
|
|
|
|
|
|
|
|
test('setupClient', () async {
|
|
|
|
client = await getClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('set blocked / verified', () async {
|
|
|
|
final key =
|
|
|
|
client.userDeviceKeys[client.userID].deviceKeys['OTHERDEVICE'];
|
|
|
|
final masterKey = client.userDeviceKeys[client.userID].masterKey;
|
|
|
|
masterKey.setDirectVerified(true);
|
|
|
|
// we need to populate the ssss cache to be able to test signing easily
|
|
|
|
final handle = client.encryption.ssss.open();
|
|
|
|
handle.unlock(recoveryKey: SSSS_KEY);
|
|
|
|
await handle.maybeCacheAll();
|
|
|
|
|
|
|
|
expect(key.verified, true);
|
|
|
|
await key.setBlocked(true);
|
|
|
|
expect(key.verified, false);
|
|
|
|
await key.setBlocked(false);
|
|
|
|
expect(key.directVerified, false);
|
|
|
|
expect(key.verified, true); // still verified via cross-sgining
|
|
|
|
|
|
|
|
expect(masterKey.verified, true);
|
|
|
|
await masterKey.setBlocked(true);
|
|
|
|
expect(masterKey.verified, false);
|
|
|
|
await masterKey.setBlocked(false);
|
|
|
|
expect(masterKey.verified, true);
|
|
|
|
|
|
|
|
FakeMatrixApi.calledEndpoints.clear();
|
|
|
|
await key.setVerified(true);
|
|
|
|
await Future.delayed(Duration(milliseconds: 10));
|
|
|
|
expect(
|
|
|
|
FakeMatrixApi.calledEndpoints.keys
|
|
|
|
.any((k) => k == '/client/r0/keys/signatures/upload'),
|
|
|
|
true);
|
|
|
|
expect(key.directVerified, true);
|
|
|
|
|
|
|
|
FakeMatrixApi.calledEndpoints.clear();
|
|
|
|
await key.setVerified(false);
|
|
|
|
await Future.delayed(Duration(milliseconds: 10));
|
|
|
|
expect(
|
|
|
|
FakeMatrixApi.calledEndpoints.keys
|
|
|
|
.any((k) => k == '/client/r0/keys/signatures/upload'),
|
|
|
|
false);
|
|
|
|
expect(key.directVerified, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('verification based on signatures', () async {
|
|
|
|
final user = client.userDeviceKeys[client.userID];
|
|
|
|
user.masterKey.setDirectVerified(true);
|
|
|
|
expect(user.deviceKeys['GHTYAJCE'].crossVerified, true);
|
|
|
|
expect(user.deviceKeys['GHTYAJCE'].signed, true);
|
|
|
|
expect(user.getKey('GHTYAJCE').crossVerified, true);
|
|
|
|
expect(user.deviceKeys['OTHERDEVICE'].crossVerified, true);
|
|
|
|
expect(user.selfSigningKey.crossVerified, true);
|
|
|
|
expect(
|
|
|
|
user
|
|
|
|
.getKey('F9ypFzgbISXCzxQhhSnXMkc1vq12Luna3Nw5rqViOJY')
|
|
|
|
.crossVerified,
|
|
|
|
true);
|
|
|
|
expect(user.userSigningKey.crossVerified, true);
|
|
|
|
expect(user.verified, UserVerifiedStatus.verified);
|
|
|
|
user.masterKey.setDirectVerified(false);
|
|
|
|
expect(user.deviceKeys['GHTYAJCE'].crossVerified, false);
|
|
|
|
expect(user.deviceKeys['OTHERDEVICE'].crossVerified, false);
|
|
|
|
expect(user.verified, UserVerifiedStatus.unknown);
|
|
|
|
user.masterKey.setDirectVerified(true);
|
|
|
|
user.deviceKeys['GHTYAJCE'].signatures.clear();
|
|
|
|
expect(user.deviceKeys['GHTYAJCE'].verified,
|
|
|
|
true); // it's our own device, should be direct verified
|
|
|
|
expect(
|
|
|
|
user.deviceKeys['GHTYAJCE'].signed, false); // not verified for others
|
|
|
|
user.deviceKeys['OTHERDEVICE'].signatures.clear();
|
|
|
|
expect(user.verified, UserVerifiedStatus.unknownDevice);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('start verification', () async {
|
|
|
|
var req = client
|
|
|
|
.userDeviceKeys['@alice:example.com'].deviceKeys['JLAFKJWSCS']
|
|
|
|
.startVerification();
|
|
|
|
expect(req != null, true);
|
|
|
|
expect(req.room != null, false);
|
|
|
|
|
|
|
|
req =
|
|
|
|
await client.userDeviceKeys['@alice:example.com'].startVerification();
|
|
|
|
expect(req != null, true);
|
|
|
|
expect(req.room != null, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('dispose client', () async {
|
|
|
|
await client.dispose(closeDatabase: true);
|
2020-02-04 13:41:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|