famedlysdk/test/encryption/key_verification_test.dart

106 lines
3.5 KiB
Dart
Raw Normal View History

2020-06-03 10:16:01 +00:00
/*
* Ansible inventory script used at Famedly GmbH for managing many hosts
2020-06-04 15:51:49 +00:00
* Copyright (C) 2020 Famedly GmbH
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.
*
* 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:famedlysdk/encryption.dart';
2020-06-03 10:16:01 +00:00
import 'package:test/test.dart';
import 'package:olm/olm.dart' as olm;
import '../fake_matrix_api.dart';
import '../fake_database.dart';
2020-06-03 10:16:01 +00:00
void main() {
/// All Tests related to the ChatTime
group('Key Verification', () {
var olmEnabled = true;
try {
olm.init();
olm.Account();
} catch (_) {
olmEnabled = false;
print('[LibOlm] Failed to load LibOlm: ' + _.toString());
}
print('[LibOlm] Enabled: $olmEnabled');
var client = Client('testclient', debug: true, httpClient: FakeMatrixApi());
var room = Room(id: '!localpart:server.abc', client: client);
var updateCounter = 0;
KeyVerification keyVerification;
2020-06-03 10:16:01 +00:00
if (!olmEnabled) return;
test('setupClient', () async {
client.database = getDatabase();
await client.checkServer('https://fakeServer.notExisting');
await client.login('test', '1234');
keyVerification = KeyVerification(
encryption: client.encryption,
room: room,
userId: '@alice:example.com',
deviceId: 'ABCD',
onUpdate: () => updateCounter++,
);
});
2020-06-03 10:16:01 +00:00
test('acceptSas', () async {
await keyVerification.acceptSas();
});
test('acceptVerification', () async {
await keyVerification.acceptVerification();
});
test('cancel', () async {
await keyVerification.cancel('m.cancelcode');
expect(keyVerification.canceled, true);
expect(keyVerification.canceledCode, 'm.cancelcode');
expect(keyVerification.canceledReason, null);
});
test('handlePayload', () async {
await keyVerification.handlePayload('m.key.verification.request', {
'from_device': 'AliceDevice2',
'methods': ['m.sas.v1'],
'timestamp': 1559598944869,
'transaction_id': 'S0meUniqueAndOpaqueString'
});
await keyVerification.handlePayload('m.key.verification.start', {
'from_device': 'BobDevice1',
'method': 'm.sas.v1',
'transaction_id': 'S0meUniqueAndOpaqueString'
});
await keyVerification.handlePayload('m.key.verification.cancel', {
'code': 'm.user',
'reason': 'User rejected the key verification request',
'transaction_id': 'S0meUniqueAndOpaqueString'
});
});
test('rejectSas', () async {
await keyVerification.rejectSas();
});
test('rejectVerification', () async {
await keyVerification.rejectVerification();
});
test('start', () async {
await keyVerification.start();
});
test('verifyActivity', () async {
final verified = await keyVerification.verifyActivity();
expect(verified, true);
keyVerification?.dispose();
2020-06-03 10:16:01 +00:00
});
});
}