frindlymap/test/recovery_service_test.dart

87 lines
3.6 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:friendly_map/modules/keys/keys.dart';
import 'package:friendly_map/modules/sync/sync.dart';
import 'package:friendly_map/modules/transport/transport.dart';
import 'support/app_device.dart';
const pa = '+821011112222';
const pb = '+821022223333';
const phrase = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
Future<(AppDevice, AppDevice)> friends() async {
final hub = MemoryHub();
final a = await AppDevice.create(hub, '010-1111-2222', pa, '가');
final b = await AppDevice.create(hub, '010-2222-3333', pb, '나');
await a.app.friends.add(b.phone, b.name);
await AppDevice.settleAll();
await b.app.friends.accept(a.phone);
await AppDevice.settleAll();
return (a, b);
}
void main() {
test('조각을 나눠 두면 보관되고, 친구에게 맡기면 친구 기기에 그 조각이 보관된다', () async {
final (a, b) = await friends();
expect(await a.app.recovery.split(), null);
final codes = await RecoveryShares.split(phrase, n: 3, k: 2);
final s = await a.app.recovery.saveSplit(codes, 2);
expect(s.total, 3);
expect(s.entrusted, 0);
expect(s.free, [0, 1, 2]);
final bOnA = (await a.app.friends.people()).single;
expect(await a.app.recovery.entrust(bOnA.id, 0), SendResult.sent);
await AppDevice.settleAll();
expect((await a.app.recovery.split())!.given, {bOnA.id: 0});
final held = await b.app.recovery.held();
expect(held.length, 1);
expect(held.single.ownerPhone, pa);
expect(held.single.ownerName, '가');
expect(held.single.code, codes[0]);
});
test('한 조각은 한 사람에게만, 같은 사람에게 다시 맡기면 그 조각을 새로 보낸다', () async {
final (a, _) = await friends();
final codes = await RecoveryShares.split(phrase, n: 3, k: 2);
await a.app.recovery.saveSplit(codes, 2);
final p = (await a.app.friends.people()).single;
expect(await a.app.recovery.entrust(p.id, 1), SendResult.sent);
expect(await a.app.recovery.entrust(p.id, 2), SendResult.sent);
expect((await a.app.recovery.split())!.given, {p.id: 2});
expect(await a.app.recovery.entrust(p.id, 9), SendResult.noTarget);
});
test('나눈 조각이 없거나 모르는 사람이면 보내지 않는다', () async {
final (a, _) = await friends();
expect(await a.app.recovery.entrust('없는사람', 0), SendResult.noTarget);
await a.app.recovery.saveSplit(await RecoveryShares.split(phrase, n: 3, k: 2), 2);
expect(await a.app.recovery.entrust('없는사람', 0), SendResult.noTarget);
});
test('친구들이 맡긴 조각이 모이면 단어 12개로 되살아난다', () async {
final hub = MemoryHub();
final a = await AppDevice.create(hub, '010-1111-2222', pa, '가');
final f1 = await AppDevice.create(hub, '010-2222-3333', pb, '나');
final f2 = await AppDevice.create(hub, '010-3333-4444', '+821033334444', '다');
for (final f in [f1, f2]) {
await a.app.friends.add(f.phone, f.name);
await AppDevice.settleAll();
await f.app.friends.accept(a.phone);
await AppDevice.settleAll();
}
await a.app.recovery.saveSplit(await RecoveryShares.split(phrase, n: 3, k: 2), 2);
for (final p in await a.app.friends.people()) {
final free = (await a.app.recovery.split())!.free.first;
expect(await a.app.recovery.entrust(p.id, free), SendResult.sent);
}
await AppDevice.settleAll();
final codes = [
...(await f1.app.recovery.held()).map((h) => h.code),
...(await f2.app.recovery.held()).map((h) => h.code),
];
expect(await RecoveryShares.combine(codes), phrase);
});
}