From 4d6a81b0c8671c33e4a7dd378f07a4737e25387b Mon Sep 17 00:00:00 2001 From: Murali Date: Fri, 5 Apr 2024 13:59:01 +0530 Subject: [PATCH 1/3] fix: unit test for max length in local secondary --- packages/at_client/pubspec.yaml | 13 +++++ .../at_client/test/local_secondary_test.dart | 53 +++++++++++++++++++ .../at_client/test/test_utils/test_utils.dart | 10 ++++ 3 files changed, 76 insertions(+) create mode 100644 packages/at_client/test/test_utils/test_utils.dart diff --git a/packages/at_client/pubspec.yaml b/packages/at_client/pubspec.yaml index 33ad3af3f..5ef4ec3ac 100644 --- a/packages/at_client/pubspec.yaml +++ b/packages/at_client/pubspec.yaml @@ -40,6 +40,19 @@ dependencies: meta: ^1.8.0 version: ^3.0.2 +#TODO replace with published version +dependency_overrides: + at_persistence_secondary_server: + git: + url: https://github.com/atsign-foundation/at_server + path: packages/at_persistence_secondary_server + ref: trunk + at_commons: + git: + url: https://github.com/atsign-foundation/at_libraries + path: packages/at_commons + ref: keylength_validation_changes + dev_dependencies: lints: ^2.1.1 test: ^1.21.4 diff --git a/packages/at_client/test/local_secondary_test.dart b/packages/at_client/test/local_secondary_test.dart index 0007ca78f..e4a04e4eb 100644 --- a/packages/at_client/test/local_secondary_test.dart +++ b/packages/at_client/test/local_secondary_test.dart @@ -8,6 +8,8 @@ import 'package:crypton/crypton.dart'; import 'package:test/test.dart'; import 'package:mocktail/mocktail.dart'; +import 'test_utils/test_utils.dart'; + class MockSecondaryKeyStore extends Mock implements SecondaryKeyStore { static const String hiddenKey1 = 'public:__location.wavi@alice'; static const String hiddenKey2 = '_profilePic.wavi@alice'; @@ -163,6 +165,57 @@ void main() { expect(executeResult!.startsWith('data:'), true); }); + test('test update verb builder max key length check', () async { + final atClientManager = AtClientManager(atSign); + final preference = AtClientPreference() + ..syncRegex = '.wavi' + ..hiveStoragePath = 'test/hive'; + AtClient atClient = await AtClientImpl.create(atSign, 'wavi', preference, + atClientManager: atClientManager); + final localSecondary = LocalSecondary(atClient); + var key = TestUtils.createRandomString(250); + final verbBuilder = UpdateVerbBuilder() + ..atKey = (AtKey() + ..key = key + ..sharedBy = atSign + ..metadata = (Metadata()..isPublic = true)) + ..value = 'alice@gmail.com'; + expect( + () async => + await localSecondary.executeVerb(verbBuilder, sync: false), + throwsA(predicate((dynamic e) => + e is DataStoreException && + e.message == + 'key length ${'public:'.length + key.length + atSign.length} is greater than max allowed 248 chars'))); + }); + + test('test update verb builder max key length check for cached key', + () async { + final atClientManager = AtClientManager(atSign); + final preference = AtClientPreference() + ..syncRegex = '.wavi' + ..hiveStoragePath = 'test/hive'; + AtClient atClient = await AtClientImpl.create(atSign, 'wavi', preference, + atClientManager: atClientManager); + final localSecondary = LocalSecondary(atClient); + var key = TestUtils.createRandomString(250); + final verbBuilder = UpdateVerbBuilder() + ..atKey = (AtKey() + ..key = key + ..sharedBy = atSign + ..metadata = (Metadata() + ..isCached = true + ..isPublic = true)) + ..value = 'alice@gmail.com'; + expect( + () async => + await localSecondary.executeVerb(verbBuilder, sync: false), + throwsA(predicate((dynamic e) => + e is DataStoreException && + e.message == + 'key length ${'cached:'.length + 'public:'.length + key.length + atSign.length} is greater than max allowed 255 chars'))); + }); + test('test llookup verb builder', () async { final atClientManager = AtClientManager(atSign); final preference = AtClientPreference() diff --git a/packages/at_client/test/test_utils/test_utils.dart b/packages/at_client/test/test_utils/test_utils.dart new file mode 100644 index 000000000..f169eefdc --- /dev/null +++ b/packages/at_client/test/test_utils/test_utils.dart @@ -0,0 +1,10 @@ +import 'dart:math'; + +class TestUtils { + static String createRandomString(int length) { + final String characters = + '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'; + return String.fromCharCodes(Iterable.generate(length, + (index) => characters.codeUnitAt(Random().nextInt(characters.length)))); + } +} From d5a9fc31ad0abdde61c9bc070b898b3907f283e1 Mon Sep 17 00:00:00 2001 From: Murali Date: Thu, 11 Apr 2024 13:18:20 +0530 Subject: [PATCH 2/3] fix: added unit test --- packages/at_client/pubspec.yaml | 9 ++------- .../at_client/test/at_client_impl_test.dart | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/at_client/pubspec.yaml b/packages/at_client/pubspec.yaml index 5ef4ec3ac..397aec53c 100644 --- a/packages/at_client/pubspec.yaml +++ b/packages/at_client/pubspec.yaml @@ -31,22 +31,17 @@ dependencies: async: ^2.9.0 at_utf7: ^1.0.0 at_base2e15: ^1.0.0 - at_commons: ^4.0.1 + at_commons: ^4.0.5 at_utils: ^3.0.16 at_chops: ^2.0.0 at_lookup: ^3.0.46 at_persistence_spec: ^2.0.14 - at_persistence_secondary_server: ^3.0.61 + at_persistence_secondary_server: ^3.0.62 meta: ^1.8.0 version: ^3.0.2 #TODO replace with published version dependency_overrides: - at_persistence_secondary_server: - git: - url: https://github.com/atsign-foundation/at_server - path: packages/at_persistence_secondary_server - ref: trunk at_commons: git: url: https://github.com/atsign-foundation/at_libraries diff --git a/packages/at_client/test/at_client_impl_test.dart b/packages/at_client/test/at_client_impl_test.dart index 8ca60f7ec..490cfed03 100644 --- a/packages/at_client/test/at_client_impl_test.dart +++ b/packages/at_client/test/at_client_impl_test.dart @@ -9,6 +9,8 @@ import 'package:at_persistence_secondary_server/at_persistence_secondary_server. import 'package:mocktail/mocktail.dart'; import 'package:test/test.dart'; +import 'test_utils/test_utils.dart'; + class MockAtCompactionJob extends Mock implements AtCompactionJob { bool isCronScheduled = false; @@ -370,4 +372,21 @@ void main() { AtClientImpl.atClientInstanceMap.remove(atSign); }); }); + group('A group of test to validate max length of a key', () { + MockRemoteSecondary mockRemoteSecondary = MockRemoteSecondary(); + test('test max length for put method', () async { + AtClient? client = await AtClientImpl.create( + '@alice', 'buzz', AtClientPreference(), + remoteSecondary: mockRemoteSecondary); + var key = TestUtils.createRandomString(250); + var atKey = AtKey.fromString('$key@alice'); + + expect( + () async => await client.put(atKey, 'hello'), + throwsA(predicate((dynamic e) => + e is AtClientException && + e.message == + 'Key length exceeds maximum permissible length of 248 characters'))); + }); + }); } From 91378aa0255a348e3ea59cca85b9da5e31add7ee Mon Sep 17 00:00:00 2001 From: Murali Date: Thu, 11 Apr 2024 15:20:44 +0530 Subject: [PATCH 3/3] fix: removed dependency overrides for at_commons --- packages/at_client/pubspec.yaml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/at_client/pubspec.yaml b/packages/at_client/pubspec.yaml index 397aec53c..1e301bff2 100644 --- a/packages/at_client/pubspec.yaml +++ b/packages/at_client/pubspec.yaml @@ -31,7 +31,7 @@ dependencies: async: ^2.9.0 at_utf7: ^1.0.0 at_base2e15: ^1.0.0 - at_commons: ^4.0.5 + at_commons: ^4.0.6 at_utils: ^3.0.16 at_chops: ^2.0.0 at_lookup: ^3.0.46 @@ -40,14 +40,6 @@ dependencies: meta: ^1.8.0 version: ^3.0.2 -#TODO replace with published version -dependency_overrides: - at_commons: - git: - url: https://github.com/atsign-foundation/at_libraries - path: packages/at_commons - ref: keylength_validation_changes - dev_dependencies: lints: ^2.1.1 test: ^1.21.4