-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1087 from atsign-foundation/fix_malformed_keys
fix: remove invalid keys on server start-up
- Loading branch information
Showing
4 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/at_secondary_server/test/remove_malformed_keys_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'dart:collection'; | ||
import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart'; | ||
import 'package:at_secondary/src/server/at_secondary_impl.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:at_persistence_secondary_server/src/keystore/hive_keystore.dart'; | ||
|
||
HashMap<String, String> dummyKeyStore = HashMap(); | ||
|
||
class MockHiveKeyStore extends Mock implements HiveKeystore { | ||
@override | ||
List<String> getKeys({String? regex}) { | ||
return dummyKeyStore.keys.toList(); | ||
} | ||
|
||
@override | ||
Future<int?> remove(String key) async { | ||
dummyKeyStore.remove(key); | ||
return 1; | ||
} | ||
} | ||
|
||
class MockSecondaryPersistenceStore extends Mock | ||
implements SecondaryPersistenceStore { | ||
@override | ||
HiveKeystore? getSecondaryKeyStore() { | ||
return MockHiveKeyStore(); | ||
} | ||
} | ||
|
||
void main() { | ||
group( | ||
'A group of test to verify remove the malformed keys on server start-up', | ||
() { | ||
setUp(() { | ||
dummyKeyStore.putIfAbsent( | ||
'public:cached:public:publickey@alice', () => 'dummy_value'); | ||
dummyKeyStore.putIfAbsent('public:publickey@alice', () => 'dummy_value'); | ||
dummyKeyStore.putIfAbsent('public:publickey', () => 'dummy_value'); | ||
dummyKeyStore.putIfAbsent('@alice:phone@bob', () => 'dummy_value'); | ||
}); | ||
test('A test to verify only malformed keys are removed', () async { | ||
AtSecondaryServerImpl.getInstance().secondaryPersistenceStore = | ||
MockSecondaryPersistenceStore(); | ||
await AtSecondaryServerImpl.getInstance().removeMalformedKeys(); | ||
expect(dummyKeyStore.length, 2); | ||
expect(dummyKeyStore.containsKey('public:publickey@alice'), true); | ||
expect(dummyKeyStore.containsKey('@alice:phone@bob'), true); | ||
expect(dummyKeyStore.containsKey('public:cached:public:publickey@alice'), | ||
false); | ||
expect(dummyKeyStore.containsKey('public:publickey'), false); | ||
}); | ||
}); | ||
} |