halkyon/packages/halkyon_keys/test/safety_code_test.dart

64 lines
2 KiB
Dart
Raw Permalink Normal View History

import 'package:halkyon_keys/halkyon_keys.dart';
import 'package:sodium/sodium_sumo.dart';
import 'package:test/test.dart';
Future<void> main() async {
final sodium = await SodiumSumoInit.init();
PublicKeys keysOf(AccountKeys account) => (
encryption: account.encryptionPublicKey,
signing: account.signingPublicKey,
);
group('safety codes (ADR 0044, D18)', () {
final alice = AccountKeys.generate(sodium);
final bob = AccountKeys.generate(sodium);
test('both people see the same 60 digits, in twelve groups of five', () {
final code = safetyCode(sodium, keysOf(alice), keysOf(bob));
expect(code, safetyCode(sodium, keysOf(bob), keysOf(alice)));
expect(code, matches(RegExp(r'^\d{5}( \d{5}){11}$')));
});
test('another key on either side, of either kind, gives another '
'code', () {
final code = safetyCode(sodium, keysOf(alice), keysOf(bob));
final mallory = AccountKeys.generate(sodium);
expect(safetyCode(sodium, keysOf(alice), keysOf(mallory)), isNot(code));
expect(
safetyCode(sodium, keysOf(alice), (
encryption: mallory.encryptionPublicKey,
signing: bob.signingPublicKey,
)),
isNot(code),
);
expect(
safetyCode(sodium, keysOf(alice), (
encryption: bob.encryptionPublicKey,
signing: mallory.signingPublicKey,
)),
isNot(code),
);
});
test("half of the code is each person's own, whoever they compare "
'with', () {
final carol = AccountKeys.generate(sodium);
final withBob = safetyCode(
sodium,
keysOf(alice),
keysOf(bob),
).replaceAll(' ', '');
final withCarol = safetyCode(
sodium,
keysOf(alice),
keysOf(carol),
).replaceAll(' ', '');
final halves = {
withBob.substring(0, 30),
withBob.substring(30),
}.intersection({withCarol.substring(0, 30), withCarol.substring(30)});
expect(halves, hasLength(1));
});
});
}