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';
|
|
|
|
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;
|
|
|
|
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 18:16:18 +00:00
|
|
|
var otherClient =
|
|
|
|
Client('othertestclient', debug: true, 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;
|
|
|
|
await otherClient.checkServer('https://fakeServer.notExisting');
|
|
|
|
otherClient.connect(
|
|
|
|
newToken: 'abc',
|
|
|
|
newUserID: '@othertest:fakeServer.notExisting',
|
|
|
|
newHomeserver: otherClient.api.homeserver,
|
|
|
|
newDeviceName: 'Text Matrix Client',
|
|
|
|
newDeviceID: 'FOXDEVICE',
|
|
|
|
newOlmAccount: otherPickledOlmAccount,
|
|
|
|
);
|
|
|
|
|
2020-06-05 07:59:37 +00:00
|
|
|
await Future.delayed(Duration(milliseconds: 10));
|
2020-06-04 15:51:49 +00:00
|
|
|
device = DeviceKeys(
|
2020-06-05 07:59:37 +00:00
|
|
|
userId: client.userID,
|
|
|
|
deviceId: client.deviceID,
|
2020-06-04 15:51:49 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
verified: true,
|
|
|
|
blocked: false,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|