famedlysdk/test/encryption/encrypt_decrypt_to_device_test.dart

119 lines
4.4 KiB
Dart
Raw Normal View History

2020-06-04 15:51:49 +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 'package:famedlysdk/famedlysdk.dart';
2020-08-06 09:35:02 +00:00
import 'package:famedlysdk/src/utils/logs.dart';
2020-06-04 15:51:49 +00:00
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 15:51:49 +00:00
import '../fake_matrix_api.dart';
void main() {
2020-06-05 07:59:37 +00:00
// key @othertest:fakeServer.notExisting
2020-06-04 18:16:18 +00:00
const otherPickledOlmAccount =
'VWhVApbkcilKAEGppsPDf9nNVjaK8/IxT3asSR0sYg0S5KgbfE8vXEPwoiKBX2cEvwX3OessOBOkk+ZE7TTbjlrh/KEd31p8Wo+47qj0AP+Ky+pabnhi+/rTBvZy+gfzTqUfCxZrkzfXI9Op4JnP6gYmy7dVX2lMYIIs9WCO1jcmIXiXum5jnfXu1WLfc7PZtO2hH+k9CDKosOFaXRBmsu8k/BGXPSoWqUpvu6WpEG9t5STk4FeAzA';
2020-06-04 15:51:49 +00:00
group('Encrypt/Decrypt to-device messages', () {
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 15:51:49 +00:00
}
2020-08-06 09:35:02 +00:00
Logs.success('[LibOlm] Enabled: $olmEnabled');
2020-06-04 15:51:49 +00:00
if (!olmEnabled) return;
2020-06-05 07:59:37 +00:00
Client client;
2020-08-06 06:55:35 +00:00
var otherClient = Client('othertestclient', httpClient: FakeMatrixApi());
2020-06-04 15:51:49 +00:00
DeviceKeys device;
Map<String, dynamic> payload;
test('setupClient', () async {
2020-06-05 07:59:37 +00:00
client = await getClient();
2020-06-04 15:51:49 +00:00
otherClient.database = client.database;
2020-10-23 09:34:08 +00:00
await otherClient.checkHomeserver('https://fakeServer.notExisting');
2020-06-04 15:51:49 +00:00
otherClient.connect(
newToken: 'abc',
newUserID: '@othertest:fakeServer.notExisting',
2020-08-11 16:11:51 +00:00
newHomeserver: otherClient.homeserver,
2020-06-04 15:51:49 +00:00
newDeviceName: 'Text Matrix Client',
newDeviceID: 'FOXDEVICE',
newOlmAccount: otherPickledOlmAccount,
);
2020-06-05 07:59:37 +00:00
await Future.delayed(Duration(milliseconds: 10));
2020-06-05 20:03:28 +00:00
device = DeviceKeys.fromJson({
'user_id': client.userID,
'device_id': client.deviceID,
'algorithms': ['m.olm.v1.curve25519-aes-sha2', 'm.megolm.v1.aes-sha2'],
'keys': {
2020-06-05 07:59:37 +00:00
'curve25519:${client.deviceID}': client.identityKey,
'ed25519:${client.deviceID}': client.fingerprintKey,
2020-06-04 15:51:49 +00:00
},
2020-06-05 20:03:28 +00:00
}, client);
2020-06-04 15:51:49 +00:00
});
test('encryptToDeviceMessage', () async {
2020-06-04 18:16:18 +00:00
payload = await otherClient.encryption
.encryptToDeviceMessage([device], 'm.to_device', {'hello': 'foxies'});
2020-06-04 15:51:49 +00:00
});
test('encryptToDeviceMessagePayload', () async {
// just a hard test if nothing errors
2020-06-04 18:16:18 +00:00
await otherClient.encryption.encryptToDeviceMessagePayload(
device, 'm.to_device', {'hello': 'foxies'});
2020-06-04 15:51:49 +00:00
});
test('decryptToDeviceEvent', () async {
final encryptedEvent = ToDeviceEvent(
sender: '@othertest:fakeServer.notExisting',
type: EventTypes.Encrypted,
content: payload[client.userID][client.deviceID],
);
2020-06-04 18:16:18 +00:00
final decryptedEvent =
await client.encryption.decryptToDeviceEvent(encryptedEvent);
2020-06-04 15:51:49 +00:00
expect(decryptedEvent.type, 'm.to_device');
expect(decryptedEvent.content['hello'], 'foxies');
});
test('decryptToDeviceEvent nocache', () async {
client.encryption.olmManager.olmSessions.clear();
2020-06-04 18:16:18 +00:00
payload = await otherClient.encryption.encryptToDeviceMessage(
[device], 'm.to_device', {'hello': 'superfoxies'});
2020-06-04 15:51:49 +00:00
final encryptedEvent = ToDeviceEvent(
sender: '@othertest:fakeServer.notExisting',
type: EventTypes.Encrypted,
content: payload[client.userID][client.deviceID],
);
2020-06-04 18:16:18 +00:00
final decryptedEvent =
await client.encryption.decryptToDeviceEvent(encryptedEvent);
2020-06-04 15:51:49 +00:00
expect(decryptedEvent.type, 'm.to_device');
expect(decryptedEvent.content['hello'], 'superfoxies');
});
2020-06-05 09:32:02 +00:00
test('dispose client', () async {
await client.dispose(closeDatabase: true);
await otherClient.dispose(closeDatabase: true);
});
2020-06-04 15:51:49 +00:00
});
}