87 lines
3.4 KiB
Dart
87 lines
3.4 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:friendly_map/modules/keys/keys.dart';
|
|
import 'package:friendly_map/modules/platform/platform.dart';
|
|
|
|
class _MemoryStore implements SecureKeyStore {
|
|
final Map<String, String> _m = {};
|
|
@override
|
|
Future<String?> read(String name) async => _m[name];
|
|
@override
|
|
Future<void> write(String name, String value) async => _m[name] = value;
|
|
@override
|
|
Future<void> delete(String name) async => _m.remove(name);
|
|
}
|
|
|
|
void main() {
|
|
test('처음에는 키가 없고, 만들면 12단어가 나온다', () async {
|
|
final p = LocalKeyProvider(_MemoryStore());
|
|
expect(await p.isSetUp(), false);
|
|
final phrase = await p.setUpNew();
|
|
expect(phrase.split(' ').length, 12);
|
|
expect(isValidRecoveryPhrase(phrase), true);
|
|
expect(await p.isSetUp(), true);
|
|
expect(await p.showPhrase(), phrase);
|
|
});
|
|
|
|
test('키를 만들기 전에는 저장소 암호를 줄 수 없다', () async {
|
|
final p = LocalKeyProvider(_MemoryStore());
|
|
expect(() => p.databasePassword(), throwsStateError);
|
|
});
|
|
|
|
test('같은 복구 코드로 복구하면 같은 저장소 암호가 나온다', () async {
|
|
final a = LocalKeyProvider(_MemoryStore());
|
|
final phrase = await a.setUpNew();
|
|
final pwA = await a.databasePassword();
|
|
|
|
final b = LocalKeyProvider(_MemoryStore());
|
|
expect(await b.restore(phrase), true);
|
|
expect(await b.databasePassword(), pwA);
|
|
});
|
|
|
|
test('다른 코드면 다른 암호, 같은 기기에서는 항상 같은 암호', () async {
|
|
final a = LocalKeyProvider(_MemoryStore());
|
|
await a.setUpNew();
|
|
final b = LocalKeyProvider(_MemoryStore());
|
|
await b.setUpNew();
|
|
expect(await a.databasePassword(), await a.databasePassword());
|
|
expect(await a.databasePassword() == await b.databasePassword(), false);
|
|
expect((await a.databasePassword()).length, 64);
|
|
});
|
|
|
|
test('잘못된 코드는 복구되지 않는다', () async {
|
|
final p = LocalKeyProvider(_MemoryStore());
|
|
expect(await p.restore('abc def'), false);
|
|
expect(await p.isSetUp(), false);
|
|
});
|
|
|
|
test('공백과 대소문자가 달라도 같은 코드로 본다', () async {
|
|
final a = LocalKeyProvider(_MemoryStore());
|
|
final phrase = await a.setUpNew();
|
|
final messy = ' ${phrase.toUpperCase().replaceAll(' ', ' ')} ';
|
|
final b = LocalKeyProvider(_MemoryStore());
|
|
expect(await b.restore(messy), true);
|
|
expect(await b.databasePassword(), await a.databasePassword());
|
|
});
|
|
|
|
test('용도별 값은 서로 다르고, 복구해도 같은 값이 나온다', () async {
|
|
final a = LocalKeyProvider(_MemoryStore());
|
|
final phrase = await a.setUpNew();
|
|
expect(await a.derive('x') == await a.derive('y'), false);
|
|
expect(await a.mailboxToken() == await a.databasePassword(), false);
|
|
final b = LocalKeyProvider(_MemoryStore());
|
|
await b.restore(phrase);
|
|
expect(await b.derive('x'), await a.derive('x'));
|
|
expect(await b.mailboxToken(), await a.mailboxToken());
|
|
});
|
|
|
|
test('복구 코드로 새 기기를 만들어도 같은 신원 키가 나온다', () async {
|
|
final a = LocalKeyProvider(_MemoryStore());
|
|
final phrase = await a.setUpNew();
|
|
final b = LocalKeyProvider(_MemoryStore());
|
|
await b.restore(phrase);
|
|
final ka = await LocalIdentityProvider(IdentityKeys(a.derive)).myPublicKey();
|
|
final kb = await LocalIdentityProvider(IdentityKeys(b.derive)).myPublicKey();
|
|
expect(ka, kb);
|
|
});
|
|
}
|