halkyon/packages/halkyon_app/test/shared_space/key_pins_test.dart

105 lines
3.6 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:halkyon_app/halkyon_app.dart';
/// The public keys a device pins for each person (Halkyon's ADR 0044, D18).
void main() {
late Directory folder;
late File file;
PublicKeys keys(int mark) => (
encryption: Uint8List.fromList(List.filled(32, mark)),
signing: Uint8List.fromList(List.filled(32, mark + 1)),
);
setUp(() async {
folder = await Directory.systemTemp.createTemp('key_pins');
file = File('${folder.path}/person/key_pins.json');
});
tearDown(() => folder.delete(recursive: true));
test('the first keys seen stay pinned, on the next start too; others '
'are a change until trusted anew', () async {
final pins = KeyPins(FileKeyPinStore(file));
expect(await pins.check('bob', keys(1)), PinCheck.firstSeen);
expect(await pins.check('bob', keys(1)), PinCheck.same);
expect(await pins.check('bob', keys(3)), PinCheck.changed);
final later = KeyPins(FileKeyPinStore(file));
expect(await later.check('bob', keys(3)), PinCheck.changed);
expect(await later.check('bob', keys(1)), PinCheck.same);
await later.trustAnew('bob', keys(3));
expect(
await KeyPins(FileKeyPinStore(file)).check('bob', keys(3)),
PinCheck.same,
);
});
test('compared keys are marked so, until they are trusted anew', () async {
final pins = KeyPins(FileKeyPinStore(file));
await pins.check('bob', keys(1));
expect((await pins.of('bob'))!.verified, isFalse);
await pins.markVerified('bob');
expect((await KeyPins(FileKeyPinStore(file)).of('bob'))!.verified, isTrue);
await pins.trustAnew('bob', keys(3));
expect((await pins.of('bob'))!.verified, isFalse);
});
test('a file not holding pins counts as none', () async {
await file.parent.create(recursive: true);
await file.writeAsString('{not json');
var book = await FileKeyPinStore(file).read();
expect(book.pins, isEmpty);
expect(book.spaces, isEmpty);
await file.writeAsString(
'{"pins": {"bob": {"encryption": "%%", "signing": "", '
'"verified": false, "earlierSigning": []}, "carol": 3}, '
'"spaces": {"s": 4}}',
);
book = await FileKeyPinStore(file).read();
expect(book.pins, isEmpty);
expect(book.spaces, isEmpty);
});
test('signatures count in a space from everyone seen there, by every '
'signing key pinned for them, earlier ones too (D20)', () async {
final pins = KeyPins(FileKeyPinStore(file));
await pins.check('bob', keys(1));
await pins.check('carol', keys(5));
await pins.check('mallory', keys(9));
await pins.sawIn('space', ['bob', 'carol']);
await pins.trustAnew('bob', keys(3));
final later = KeyPins(FileKeyPinStore(file));
final signers = await later.signersIn('space');
expect(
signers,
unorderedEquals([keys(3).signing, keys(1).signing, keys(5).signing]),
);
expect(signers, isNot(contains(keys(9).signing)));
expect(await later.signersIn('elsewhere'), isEmpty);
});
test(
'someone gone from a space still counts for what they signed there',
() async {
final pins = KeyPins(FileKeyPinStore(file));
await pins.check('bob', keys(1));
await pins.sawIn('space', ['bob']);
await pins.sawIn('space', <String>[]);
expect(await pins.signersIn('space'), [keys(1).signing]);
},
);
test('a seen person without pinned keys counts for nothing', () async {
final pins = KeyPins(FileKeyPinStore(file));
await pins.sawIn('space', ['bob']);
expect(await pins.signersIn('space'), isEmpty);
});
}