split off into fake client

This commit is contained in:
Sorunome 2020-06-05 09:59:37 +02:00
parent e14cd61d6d
commit 0b1d6ae8dd
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
10 changed files with 99 additions and 93 deletions

View File

@ -20,8 +20,7 @@ import 'package:famedlysdk/famedlysdk.dart';
import 'package:test/test.dart';
import 'package:olm/olm.dart' as olm;
import '../fake_matrix_api.dart';
import '../fake_database.dart';
import '../fake_client.dart';
void main() {
group('Encrypt/Decrypt room message', () {
@ -37,16 +36,14 @@ void main() {
if (!olmEnabled) return;
var client = Client('testclient', debug: true, httpClient: FakeMatrixApi());
Client client;
final roomId = '!726s6s6q:example.com';
Room room;
Map<String, dynamic> payload;
final now = DateTime.now();
test('setupClient', () async {
client.database = getDatabase();
await client.checkServer('https://fakeServer.notExisting');
await client.login('test', '1234');
client = await getClient();
room = client.getRoomById(roomId);
});

View File

@ -20,14 +20,11 @@ import 'package:famedlysdk/famedlysdk.dart';
import 'package:test/test.dart';
import 'package:olm/olm.dart' as olm;
import '../fake_client.dart';
import '../fake_matrix_api.dart';
import '../fake_database.dart';
void main() {
// key @test:fakeServer.notExisting
const pickledOlmAccount =
'N2v1MkIFGcl0mQpo2OCwSopxPQJ0wnl7oe7PKiT4141AijfdTIhRu+ceXzXKy3Kr00nLqXtRv7kid6hU4a+V0rfJWLL0Y51+3Rp/ORDVnQy+SSeo6Fn4FHcXrxifJEJ0djla5u98fBcJ8BSkhIDmtXRPi5/oJAvpiYn+8zMjFHobOeZUAxYR0VfQ9JzSYBsSovoQ7uFkNks1M4EDUvHtu/BjDjz0C3ioDgrrFdoSrn+GSeF5FGKsNu8OLkQ9Lq5+BrUutK5QSJI19uoZj2sj/OixvIpnun8XxYpXo7cfh9MEtKI8ob7lLM2OpZ8BogU70ORgkwthsPSOtxQGPhx8+y5Sg7B6KGlU';
// key @othertest:fakeServer.notExisting
const otherPickledOlmAccount =
'VWhVApbkcilKAEGppsPDf9nNVjaK8/IxT3asSR0sYg0S5KgbfE8vXEPwoiKBX2cEvwX3OessOBOkk+ZE7TTbjlrh/KEd31p8Wo+47qj0AP+Ky+pabnhi+/rTBvZy+gfzTqUfCxZrkzfXI9Op4JnP6gYmy7dVX2lMYIIs9WCO1jcmIXiXum5jnfXu1WLfc7PZtO2hH+k9CDKosOFaXRBmsu8k/BGXPSoWqUpvu6WpEG9t5STk4FeAzA';
@ -44,31 +41,16 @@ void main() {
if (!olmEnabled) return;
var client = Client('testclient', debug: true, httpClient: FakeMatrixApi());
Client client;
var otherClient =
Client('othertestclient', debug: true, httpClient: FakeMatrixApi());
DeviceKeys device;
Map<String, dynamic> payload;
test('setupClient', () async {
client.database = getDatabase();
client = await getClient();
otherClient.database = client.database;
await client.checkServer('https://fakeServer.notExisting');
await otherClient.checkServer('https://fakeServer.notExisting');
final resp = await client.api.login(
type: 'm.login.password',
user: 'test',
password: '1234',
initialDeviceDisplayName: 'Fluffy Matrix Client',
);
client.connect(
newToken: resp.accessToken,
newUserID: resp.userId,
newHomeserver: client.api.homeserver,
newDeviceName: 'Text Matrix Client',
newDeviceID: resp.deviceId,
newOlmAccount: pickledOlmAccount,
);
otherClient.connect(
newToken: 'abc',
newUserID: '@othertest:fakeServer.notExisting',
@ -78,14 +60,14 @@ void main() {
newOlmAccount: otherPickledOlmAccount,
);
await Future.delayed(Duration(milliseconds: 50));
await Future.delayed(Duration(milliseconds: 10));
device = DeviceKeys(
userId: resp.userId,
deviceId: resp.deviceId,
userId: client.userID,
deviceId: client.deviceID,
algorithms: ['m.olm.v1.curve25519-aes-sha2', 'm.megolm.v1.aes-sha2'],
keys: {
'curve25519:${resp.deviceId}': client.identityKey,
'ed25519:${resp.deviceId}': client.fingerprintKey,
'curve25519:${client.deviceID}': client.identityKey,
'ed25519:${client.deviceID}': client.fingerprintKey,
},
verified: true,
blocked: false,

View File

@ -19,9 +19,10 @@
import 'dart:convert';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:test/test.dart';
import 'package:olm/olm.dart' as olm;
import '../fake_client.dart';
import '../fake_matrix_api.dart';
import '../fake_database.dart';
Map<String, dynamic> jsonDecode(dynamic payload) {
if (payload is String) {
@ -38,18 +39,22 @@ Map<String, dynamic> jsonDecode(dynamic payload) {
void main() {
/// All Tests related to device keys
group('Key Request', () {
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;
final validSessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU';
final validSenderKey = '3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI';
test('Create Request', () async {
var matrix =
Client('testclient', debug: true, httpClient: FakeMatrixApi());
matrix.database = getDatabase();
await matrix.checkServer('https://fakeServer.notExisting');
await matrix.login('test', '1234');
if (!matrix.encryptionEnabled) {
await matrix.dispose(closeDatabase: true);
return;
}
var matrix = await getClient();
final requestRoom = matrix.getRoomById('!726s6s6q:example.com');
await matrix.encryption.keyManager
.request(requestRoom, 'sessionId', validSenderKey);
@ -75,15 +80,7 @@ void main() {
await matrix.dispose(closeDatabase: true);
});
test('Reply To Request', () async {
var matrix =
Client('testclient', debug: true, httpClient: FakeMatrixApi());
matrix.database = getDatabase();
await matrix.checkServer('https://fakeServer.notExisting');
await matrix.login('test', '1234');
if (!matrix.encryptionEnabled) {
await matrix.dispose(closeDatabase: true);
return;
}
var matrix = await getClient();
matrix.setUserId('@alice:example.com'); // we need to pretend to be alice
FakeMatrixApi.calledEndpoints.clear();
await matrix
@ -224,15 +221,7 @@ void main() {
await matrix.dispose(closeDatabase: true);
});
test('Receive shared keys', () async {
var matrix =
Client('testclient', debug: true, httpClient: FakeMatrixApi());
matrix.database = getDatabase();
await matrix.checkServer('https://fakeServer.notExisting');
await matrix.login('test', '1234');
if (!matrix.encryptionEnabled) {
await matrix.dispose(closeDatabase: true);
return;
}
var matrix = await getClient();
final requestRoom = matrix.getRoomById('!726s6s6q:example.com');
await matrix.encryption.keyManager
.request(requestRoom, validSessionId, validSenderKey);

View File

@ -21,8 +21,7 @@ import 'package:famedlysdk/encryption.dart';
import 'package:test/test.dart';
import 'package:olm/olm.dart' as olm;
import '../fake_matrix_api.dart';
import '../fake_database.dart';
import '../fake_client.dart';
void main() {
/// All Tests related to the ChatTime
@ -37,17 +36,17 @@ void main() {
}
print('[LibOlm] Enabled: $olmEnabled');
var client = Client('testclient', debug: true, httpClient: FakeMatrixApi());
var room = Room(id: '!localpart:server.abc', client: client);
if (!olmEnabled) return;
Client client;
Room room;
var updateCounter = 0;
KeyVerification keyVerification;
if (!olmEnabled) return;
test('setupClient', () async {
client.database = getDatabase();
await client.checkServer('https://fakeServer.notExisting');
await client.login('test', '1234');
client = await getClient();
room = Room(id: '!localpart:server.abc', client: client);
keyVerification = KeyVerification(
encryption: client.encryption,
room: room,

View File

@ -21,8 +21,8 @@ import 'package:famedlysdk/famedlysdk.dart';
import 'package:test/test.dart';
import 'package:olm/olm.dart' as olm;
import '../fake_client.dart';
import '../fake_matrix_api.dart';
import '../fake_database.dart';
void main() {
group('Olm Manager', () {
@ -38,12 +38,10 @@ void main() {
if (!olmEnabled) return;
var client = Client('testclient', debug: true, httpClient: FakeMatrixApi());
Client client;
test('setupClient', () async {
client.database = getDatabase();
await client.checkServer('https://fakeServer.notExisting');
await client.login('test', '1234');
client = await getClient();
});
test('signatures', () async {

View File

@ -220,7 +220,7 @@ void main() {
event.status = -1;
final resp2 = await event.sendAgain(txid: '1234');
expect(resp1, null);
expect(resp2, '42');
expect(resp2, '\$event0');
await matrix.dispose(closeDatabase: true);
});

48
test/fake_client.dart Normal file
View File

@ -0,0 +1,48 @@
/*
* 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 'fake_matrix_api.dart';
import 'fake_database.dart';
// key @test:fakeServer.notExisting
const pickledOlmAccount =
'N2v1MkIFGcl0mQpo2OCwSopxPQJ0wnl7oe7PKiT4141AijfdTIhRu+ceXzXKy3Kr00nLqXtRv7kid6hU4a+V0rfJWLL0Y51+3Rp/ORDVnQy+SSeo6Fn4FHcXrxifJEJ0djla5u98fBcJ8BSkhIDmtXRPi5/oJAvpiYn+8zMjFHobOeZUAxYR0VfQ9JzSYBsSovoQ7uFkNks1M4EDUvHtu/BjDjz0C3ioDgrrFdoSrn+GSeF5FGKsNu8OLkQ9Lq5+BrUutK5QSJI19uoZj2sj/OixvIpnun8XxYpXo7cfh9MEtKI8ob7lLM2OpZ8BogU70ORgkwthsPSOtxQGPhx8+y5Sg7B6KGlU';
Future<Client> getClient() async {
final client = Client('testclient', debug: true, httpClient: FakeMatrixApi());
client.database = getDatabase();
await client.checkServer('https://fakeServer.notExisting');
final resp = await client.api.login(
type: 'm.login.password',
user: 'test',
password: '1234',
initialDeviceDisplayName: 'Fluffy Matrix Client',
);
client.connect(
newToken: resp.accessToken,
newUserID: resp.userId,
newHomeserver: client.api.homeserver,
newDeviceName: 'Text Matrix Client',
newDeviceID: resp.deviceId,
newOlmAccount: pickledOlmAccount,
);
await Future.delayed(Duration(milliseconds: 10));
return client;
}

View File

@ -25,6 +25,7 @@ import 'package:http/testing.dart';
class FakeMatrixApi extends MockClient {
static final calledEndpoints = <String, List<dynamic>>{};
static int eventCounter = 0;
FakeMatrixApi()
: super((request) async {
@ -1754,13 +1755,13 @@ class FakeMatrixApi extends MockClient {
'/client/r0/directory/room/%23testalias%3Aexample.com': (var reqI) => {},
'/client/r0/rooms/%21localpart%3Aserver.abc/send/m.room.message/testtxid':
(var reqI) => {
'event_id': '42',
'event_id': '\$event${FakeMatrixApi.eventCounter++}',
},
'/client/r0/rooms/!localpart%3Aexample.com/typing/%40alice%3Aexample.com':
(var req) => {},
'/client/r0/rooms/%211234%3Aexample.com/send/m.room.message/1234':
(var reqI) => {
'event_id': '42',
'event_id': '\$event${FakeMatrixApi.eventCounter++}',
},
'/client/r0/user/%40alice%3Aexample.com/rooms/%21localpart%3Aexample.com/tags/testtag':
(var req) => {},

View File

@ -26,7 +26,7 @@ import 'package:famedlysdk/src/database/database.dart'
show DbRoom, DbRoomState, DbRoomAccountData;
import 'package:test/test.dart';
import 'fake_matrix_api.dart';
import 'fake_client.dart';
import 'dart:typed_data';
@ -37,15 +37,7 @@ void main() {
/// All Tests related to the Event
group('Room', () {
test('Login', () async {
matrix = Client('testclient', debug: true, httpClient: FakeMatrixApi());
final checkResp =
await matrix.checkServer('https://fakeServer.notExisting');
final loginResp = await matrix.login('test', '1234');
expect(checkResp, true);
expect(loginResp, true);
matrix = await getClient();
});
test('Create from json', () async {
@ -315,7 +307,7 @@ void main() {
test('getTimeline', () async {
final timeline = await room.getTimeline();
expect(timeline.events.length, 1);
expect(timeline.events.length, 0);
});
test('getUserByMXID', () async {
@ -338,13 +330,13 @@ void main() {
final dynamic resp = await room.sendEvent(
{'msgtype': 'm.text', 'body': 'hello world'},
txid: 'testtxid');
expect(resp, '42');
expect(resp, '\$event0');
});
test('sendEvent', () async {
final dynamic resp =
await room.sendTextEvent('Hello world', txid: 'testtxid');
expect(resp, '42');
expect(resp, '\$event1');
});
// Not working because there is no real file to test it...

View File

@ -143,7 +143,7 @@ void main() {
expect(updateCount, 5);
expect(insertList, [0, 0, 0]);
expect(insertList.length, timeline.events.length);
expect(timeline.events[0].eventId, '42');
expect(timeline.events[0].eventId, '\$event0');
expect(timeline.events[0].status, 1);
client.onEvent.add(EventUpdate(
@ -155,7 +155,7 @@ void main() {
'content': {'msgtype': 'm.text', 'body': 'test'},
'sender': '@alice:example.com',
'status': 2,
'event_id': '42',
'event_id': '\$event0',
'unsigned': {'transaction_id': '1234'},
'origin_server_ts': DateTime.now().millisecondsSinceEpoch
},
@ -166,7 +166,7 @@ void main() {
expect(updateCount, 6);
expect(insertList, [0, 0, 0]);
expect(insertList.length, timeline.events.length);
expect(timeline.events[0].eventId, '42');
expect(timeline.events[0].eventId, '\$event0');
expect(timeline.events[0].status, 2);
});