add online key backup test

This commit is contained in:
Sorunome 2020-06-13 19:48:38 +02:00
parent 9f3e069687
commit c233d57f9f
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
4 changed files with 116 additions and 23 deletions

View file

@ -398,21 +398,24 @@ class KeyManager {
} }
/// Request a certain key from another device /// Request a certain key from another device
Future<void> request(Room room, String sessionId, String senderKey) async { Future<void> request(Room room, String sessionId, String senderKey,
{bool tryOnlineBackup = true}) async {
if (tryOnlineBackup) {
// let's first check our online key backup store thingy... // let's first check our online key backup store thingy...
var hadPreviously = var hadPreviously =
getInboundGroupSession(room.id, sessionId, senderKey) != null; getInboundGroupSession(room.id, sessionId, senderKey) != null;
try { try {
await loadSingleKey(room.id, sessionId); await loadSingleKey(room.id, sessionId);
} catch (err, stacktrace) { } catch (err, stacktrace) {
print( print('[KeyManager] Failed to access online key backup: ' +
'[KeyManager] Failed to access online key backup: ' + err.toString()); err.toString());
print(stacktrace); print(stacktrace);
} }
if (!hadPreviously && if (!hadPreviously &&
getInboundGroupSession(room.id, sessionId, senderKey) != null) { getInboundGroupSession(room.id, sessionId, senderKey) != null) {
return; // we managed to load the session from online backup, no need to care about it now return; // we managed to load the session from online backup, no need to care about it now
} }
}
// while we just send the to-device event to '*', we still need to save the // while we just send the to-device event to '*', we still need to save the
// devices themself to know where to send the cancel to after receiving a reply // devices themself to know where to send the cancel to after receiving a reply
final devices = await room.getUserDeviceKeys(); final devices = await room.getUserDeviceKeys();

View file

@ -56,8 +56,9 @@ void main() {
test('Create Request', () async { test('Create Request', () async {
var matrix = await getClient(); var matrix = await getClient();
final requestRoom = matrix.getRoomById('!726s6s6q:example.com'); final requestRoom = matrix.getRoomById('!726s6s6q:example.com');
await matrix.encryption.keyManager await matrix.encryption.keyManager.request(
.request(requestRoom, 'sessionId', validSenderKey); requestRoom, 'sessionId', validSenderKey,
tryOnlineBackup: false);
var foundEvent = false; var foundEvent = false;
for (var entry in FakeMatrixApi.calledEndpoints.entries) { for (var entry in FakeMatrixApi.calledEndpoints.entries) {
final payload = jsonDecode(entry.value.first); final payload = jsonDecode(entry.value.first);
@ -223,8 +224,9 @@ void main() {
test('Receive shared keys', () async { test('Receive shared keys', () async {
var matrix = await getClient(); var matrix = await getClient();
final requestRoom = matrix.getRoomById('!726s6s6q:example.com'); final requestRoom = matrix.getRoomById('!726s6s6q:example.com');
await matrix.encryption.keyManager await matrix.encryption.keyManager.request(
.request(requestRoom, validSessionId, validSenderKey); requestRoom, validSessionId, validSenderKey,
tryOnlineBackup: false);
final session = await matrix.encryption.keyManager final session = await matrix.encryption.keyManager
.loadInboundGroupSession( .loadInboundGroupSession(
@ -279,8 +281,9 @@ void main() {
false); false);
// unknown device // unknown device
await matrix.encryption.keyManager await matrix.encryption.keyManager.request(
.request(requestRoom, validSessionId, validSenderKey); requestRoom, validSessionId, validSenderKey,
tryOnlineBackup: false);
matrix.encryption.keyManager.clearInboundGroupSessions(); matrix.encryption.keyManager.clearInboundGroupSessions();
event = ToDeviceEvent( event = ToDeviceEvent(
sender: '@alice:example.com', sender: '@alice:example.com',
@ -304,8 +307,9 @@ void main() {
false); false);
// no encrypted content // no encrypted content
await matrix.encryption.keyManager await matrix.encryption.keyManager.request(
.request(requestRoom, validSessionId, validSenderKey); requestRoom, validSessionId, validSenderKey,
tryOnlineBackup: false);
matrix.encryption.keyManager.clearInboundGroupSessions(); matrix.encryption.keyManager.clearInboundGroupSessions();
event = ToDeviceEvent( event = ToDeviceEvent(
sender: '@alice:example.com', sender: '@alice:example.com',

View file

@ -0,0 +1,73 @@
/*
* 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 'package:famedlysdk/famedlysdk.dart';
import 'package:test/test.dart';
import 'package:olm/olm.dart' as olm;
import '../fake_client.dart';
void main() {
group('Online Key Backup', () {
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;
Client client;
final roomId = '!726s6s6q:example.com';
final sessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU';
final senderKey = 'JBG7ZaPn54OBC7TuIEiylW3BZ+7WcGQhFBPB9pogbAg';
test('setupClient', () async {
client = await getClient();
});
test('basic things', () async {
expect(client.encryption.keyManager.enabled, true);
expect(await client.encryption.keyManager.isCached(), false);
final handle = client.encryption.ssss.open();
handle.unlock(recoveryKey: SSSS_KEY);
await handle.maybeCacheAll();
expect(await client.encryption.keyManager.isCached(), true);
});
test('load key', () async {
client.encryption.keyManager.clearInboundGroupSessions();
await client.encryption.keyManager
.request(client.getRoomById(roomId), sessionId, senderKey);
expect(
client.encryption.keyManager
.getInboundGroupSession(roomId, sessionId, senderKey) !=
null,
true);
});
test('dispose client', () async {
await client.dispose(closeDatabase: true);
});
});
}

View file

@ -1533,12 +1533,25 @@ class FakeMatrixApi extends MockClient {
'/client/unstable/room_keys/version': (var req) => { '/client/unstable/room_keys/version': (var req) => {
'algorithm': 'm.megolm_backup.v1.curve25519-aes-sha2', 'algorithm': 'm.megolm_backup.v1.curve25519-aes-sha2',
'auth_data': { 'auth_data': {
'public_key': 'GXYaxqhNhUK28zUdxOmEsFRguz+PzBsDlTLlF0O0RkM' 'public_key': 'GXYaxqhNhUK28zUdxOmEsFRguz+PzBsDlTLlF0O0RkM',
'signatures': {},
}, },
'count': 0, 'count': 0,
'etag': '0', 'etag': '0',
'version': '5', 'version': '5',
}, },
'/client/unstable/room_keys/keys/${Uri.encodeComponent('!726s6s6q:example.com')}/${Uri.encodeComponent('ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU')}?version=5':
(var req) => {
'first_message_index': 0,
'forwarded_count': 0,
'is_verified': true,
'session_data': {
'ephemeral': 'fwRxYh+seqLykz5mQCLypJ4/59URdcFJ2s69OU1dGRc',
'ciphertext':
'19jkQYlbgdP+VL9DH3qY/Dvpk6onJZgf+6frZFl1TinPCm9OMK9AZZLuM1haS9XLAUK1YsREgjBqfl6T+Tq8JlJ5ONZGg2Wttt24sGYc0iTMZJ8rXcNDeKMZhM96ETyjufJSeYoXLqifiVLDw9rrVBmNStF7PskYp040em+0OZ4pF85Cwsdf7l9V7MMynzh9BoXqVUCBiwT03PNYH9AEmNUxXX+6ZwCpe/saONv8MgGt5uGXMZIK29phA3D8jD6uV/WOHsB8NjHNq9FrfSEAsl+dAcS4uiYie4BKSSeQN+zGAQqu1MMW4OAdxGOuf8WpIINx7n+7cKQfxlmc/Cgg5+MmIm2H0oDwQ+Xu7aSxp1OCUzbxQRdjz6+tnbYmZBuH0Ov2RbEvC5tDb261LRqKXpub0llg5fqKHl01D0ahv4OAQgRs5oU+4mq+H2QGTwIFGFqP9tCRo0I+aICawpxYOfoLJpFW6KvEPnM2Lr3sl6Nq2fmkz6RL5F7nUtzxN8OKazLQpv8DOYzXbi7+ayEsqS0/EINetq7RfCqgjrEUgfNWYuFXWqvUT8lnxLdNu+8cyrJqh1UquFjXWTw1kWcJ0pkokVeBtK9YysCnF1UYh/Iv3rl2ZoYSSLNtuvMSYlYHggZ8xV8bz9S3X2/NwBycBiWIy5Ou/OuSX7trIKgkkmda0xjBWEM1a2acVuqu2OFbMn2zFxm2a3YwKP//OlIgMg',
'mac': 'QzKV/fgAs4U',
},
},
}, },
'POST': { 'POST': {
'/client/r0/delete_devices': (var req) => {}, '/client/r0/delete_devices': (var req) => {},