2022-09-11 11:33:45 +00:00
|
|
|
import 'dart:convert';
|
2022-08-05 15:32:59 +00:00
|
|
|
import 'package:omemo_dart/omemo_dart.dart';
|
2022-08-08 16:03:05 +00:00
|
|
|
import 'package:omemo_dart/src/trust/always.dart';
|
2022-08-05 15:32:59 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2022-09-15 11:41:33 +00:00
|
|
|
Map<String, dynamic> jsonify(Map<String, dynamic> map) {
|
|
|
|
return jsonDecode(jsonEncode(map)) as Map<String, dynamic>;
|
|
|
|
}
|
|
|
|
|
2022-08-05 15:32:59 +00:00
|
|
|
void main() {
|
2022-08-05 16:14:10 +00:00
|
|
|
test('Test serialising and deserialising the Device', () async {
|
2022-08-05 15:32:59 +00:00
|
|
|
// Generate a random session
|
2022-08-08 16:03:05 +00:00
|
|
|
final oldSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
'user@test.server',
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-05 15:32:59 +00:00
|
|
|
final oldDevice = await oldSession.getDevice();
|
2022-09-15 11:41:33 +00:00
|
|
|
final serialised = jsonify(await oldDevice.toJson());
|
2022-08-05 15:32:59 +00:00
|
|
|
|
2023-01-01 15:49:19 +00:00
|
|
|
final newDevice = OmemoDevice.fromJson(serialised);
|
2022-08-06 10:24:26 +00:00
|
|
|
expect(await oldDevice.equals(newDevice), true);
|
2022-08-05 15:32:59 +00:00
|
|
|
});
|
2022-08-05 16:14:10 +00:00
|
|
|
|
2022-08-08 13:32:08 +00:00
|
|
|
test('Test serialising and deserialising the Device after rotating the SPK', () async {
|
|
|
|
// Generate a random session
|
2022-08-08 16:03:05 +00:00
|
|
|
final oldSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
'user@test.server',
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-08 13:32:08 +00:00
|
|
|
final oldDevice = await (await oldSession.getDevice()).replaceSignedPrekey();
|
2022-09-15 11:41:33 +00:00
|
|
|
final serialised = jsonify(await oldDevice.toJson());
|
2022-08-08 13:32:08 +00:00
|
|
|
|
2023-01-01 15:49:19 +00:00
|
|
|
final newDevice = OmemoDevice.fromJson(serialised);
|
2022-08-08 13:32:08 +00:00
|
|
|
expect(await oldDevice.equals(newDevice), true);
|
|
|
|
});
|
|
|
|
|
2022-08-05 16:14:10 +00:00
|
|
|
test('Test serialising and deserialising the OmemoDoubleRatchet', () async {
|
|
|
|
// Generate a random ratchet
|
|
|
|
const aliceJid = 'alice@server.example';
|
|
|
|
const bobJid = 'bob@other.server.example';
|
2022-08-08 16:03:05 +00:00
|
|
|
final aliceSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
aliceJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
bobJid,
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 1,
|
|
|
|
);
|
2022-08-05 16:14:10 +00:00
|
|
|
final aliceMessage = await aliceSession.encryptToJid(
|
|
|
|
bobJid,
|
|
|
|
'Hello Bob!',
|
|
|
|
newSessions: [
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-05 16:14:10 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
await bobSession.decryptMessage(
|
|
|
|
aliceMessage.ciphertext,
|
|
|
|
aliceJid,
|
2022-08-18 13:30:31 +00:00
|
|
|
await aliceSession.getDeviceId(),
|
2022-08-05 16:14:10 +00:00
|
|
|
aliceMessage.encryptedKeys,
|
2022-10-02 17:23:58 +00:00
|
|
|
getTimestamp(),
|
2022-08-05 16:14:10 +00:00
|
|
|
);
|
2022-08-18 13:30:31 +00:00
|
|
|
final aliceOld = aliceSession.getRatchet(bobJid, await bobSession.getDeviceId());
|
2022-09-15 11:41:33 +00:00
|
|
|
final aliceSerialised = jsonify(await aliceOld.toJson());
|
2022-08-05 16:14:10 +00:00
|
|
|
final aliceNew = OmemoDoubleRatchet.fromJson(aliceSerialised);
|
|
|
|
|
2022-08-06 10:24:26 +00:00
|
|
|
expect(await aliceOld.equals(aliceNew), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Test serialising and deserialising the OmemoSessionManager', () async {
|
|
|
|
// Generate a random session
|
2022-08-08 16:03:05 +00:00
|
|
|
final oldSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
'a@server',
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 4,
|
|
|
|
);
|
|
|
|
final bobSession = await OmemoSessionManager.generateNewIdentity(
|
|
|
|
'b@other.server',
|
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
opkAmount: 4,
|
|
|
|
);
|
2022-08-06 10:24:26 +00:00
|
|
|
await oldSession.addSessionFromBundle(
|
|
|
|
'bob@localhost',
|
2022-08-18 13:30:31 +00:00
|
|
|
await bobSession.getDeviceId(),
|
|
|
|
await bobSession.getDeviceBundle(),
|
2022-08-06 10:24:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Serialise and deserialise
|
2022-09-15 11:41:33 +00:00
|
|
|
final serialised = jsonify(await oldSession.toJsonWithoutSessions());
|
2022-09-15 11:22:50 +00:00
|
|
|
final newSession = OmemoSessionManager.fromJsonWithoutSessions(
|
2022-08-08 16:03:05 +00:00
|
|
|
serialised,
|
2022-09-15 11:22:50 +00:00
|
|
|
// NOTE: At this point, we don't care about this attribute
|
|
|
|
{},
|
2022-08-08 16:03:05 +00:00
|
|
|
AlwaysTrustingTrustManager(),
|
|
|
|
);
|
2022-08-06 10:24:26 +00:00
|
|
|
|
|
|
|
final oldDevice = await oldSession.getDevice();
|
|
|
|
final newDevice = await newSession.getDevice();
|
|
|
|
expect(await oldDevice.equals(newDevice), true);
|
2022-08-11 10:02:21 +00:00
|
|
|
expect(await oldSession.getDeviceMap(), await newSession.getDeviceMap());
|
2022-08-05 16:14:10 +00:00
|
|
|
});
|
2022-09-11 11:33:45 +00:00
|
|
|
|
|
|
|
test('Test serializing and deserializing RatchetMapKey', () {
|
|
|
|
const test1 = RatchetMapKey('user@example.org', 1234);
|
|
|
|
final result1 = RatchetMapKey.fromJsonKey(test1.toJsonKey());
|
|
|
|
expect(result1.jid, test1.jid);
|
|
|
|
expect(result1.deviceId, test1.deviceId);
|
|
|
|
|
|
|
|
const test2 = RatchetMapKey('user@example.org/hallo:welt', 3333);
|
|
|
|
final result2 = RatchetMapKey.fromJsonKey(test2.toJsonKey());
|
|
|
|
expect(result2.jid, test2.jid);
|
|
|
|
expect(result2.deviceId, test2.deviceId);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Test serializing and deserializing the components of the BTBV manager', () async {
|
|
|
|
// Caroline's BTBV manager
|
|
|
|
final btbv = MemoryBTBVTrustManager();
|
|
|
|
// Example data
|
|
|
|
const aliceJid = 'alice@some.server';
|
|
|
|
const bobJid = 'bob@other.server';
|
|
|
|
|
|
|
|
await btbv.onNewSession(aliceJid, 1);
|
|
|
|
await btbv.setDeviceTrust(aliceJid, 1, BTBVTrustState.verified);
|
|
|
|
await btbv.onNewSession(aliceJid, 2);
|
|
|
|
await btbv.onNewSession(bobJid, 3);
|
|
|
|
await btbv.onNewSession(bobJid, 4);
|
|
|
|
|
2022-09-15 11:41:33 +00:00
|
|
|
final serialized = jsonify(await btbv.toJson());
|
2022-09-11 11:33:45 +00:00
|
|
|
final deviceList = BlindTrustBeforeVerificationTrustManager.deviceListFromJson(
|
2022-09-15 11:41:33 +00:00
|
|
|
serialized,
|
2022-09-11 11:33:45 +00:00
|
|
|
);
|
|
|
|
expect(btbv.devices, deviceList);
|
|
|
|
|
|
|
|
final trustCache = BlindTrustBeforeVerificationTrustManager.trustCacheFromJson(
|
2022-09-15 11:41:33 +00:00
|
|
|
serialized,
|
2022-09-11 11:33:45 +00:00
|
|
|
);
|
|
|
|
expect(btbv.trustCache, trustCache);
|
|
|
|
|
|
|
|
final enableCache = BlindTrustBeforeVerificationTrustManager.enableCacheFromJson(
|
2022-09-15 11:41:33 +00:00
|
|
|
serialized,
|
2022-09-11 11:33:45 +00:00
|
|
|
);
|
|
|
|
expect(btbv.enablementCache, enableCache);
|
|
|
|
});
|
2022-08-05 15:32:59 +00:00
|
|
|
}
|