From 6ffbbbfbbee195d5ade8300be12a97d2624e080e Mon Sep 17 00:00:00 2001 From: Sitaram Kalluri Date: Tue, 30 Jul 2024 13:31:15 +0530 Subject: [PATCH 1/2] fix: Add "sharedKeyEnc" to the metadata --- .../lib/src/response/at_notification.dart | 1 + .../lib/src/test_initializers.dart | 18 ++-- .../test/notify_with_isolate_test.dart | 82 +++++++++++++++++++ 3 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 tests/at_end2end_test/test/notify_with_isolate_test.dart diff --git a/packages/at_client/lib/src/response/at_notification.dart b/packages/at_client/lib/src/response/at_notification.dart index 6b2a4d362..05f7bab0b 100644 --- a/packages/at_client/lib/src/response/at_notification.dart +++ b/packages/at_client/lib/src/response/at_notification.dart @@ -33,6 +33,7 @@ class AtNotification { json['metadata'][AtConstants.sharedKeyEncryptedEncryptingKeyName]; metadata.skeEncAlgo = json['metadata'][AtConstants.sharedKeyEncryptedEncryptingAlgo]; + metadata.sharedKeyEnc = json['metadata'][AtConstants.sharedKeyEncrypted]; } return AtNotification(json['id'], json['key'], json['from'], json['to'], diff --git a/tests/at_end2end_test/lib/src/test_initializers.dart b/tests/at_end2end_test/lib/src/test_initializers.dart index 98a4a69be..905e20051 100644 --- a/tests/at_end2end_test/lib/src/test_initializers.dart +++ b/tests/at_end2end_test/lib/src/test_initializers.dart @@ -19,8 +19,9 @@ class TestSuiteInitializer { return _singleton; } - Future testInitializer( - String atSign, String namespace, String authType) async { + Future testInitializer(String atSign, String namespace, String authType, + {bool enableInitialSync = true, + AtClientPreference? atClientPreference}) async { try { late AtChops atChops; AtAuthResponse? atAuthResponse; @@ -36,18 +37,21 @@ class TestSuiteInitializer { atChops = createAtChopsFromDemoKeys(atSign); } + atClientPreference ??= + TestPreferences.getInstance().getPreference(atSign); // Create the atClientManager for the atSign var atClientManager = await AtClientManager.getInstance() - .setCurrentAtSign(atSign, namespace, - TestPreferences.getInstance().getPreference(atSign), + .setCurrentAtSign(atSign, namespace, atClientPreference, atChops: atChops, enrollmentId: atAuthResponse?.enrollmentId); // Set Encryption Keys for currentAtSign await AtEncryptionKeysLoader.getInstance() .setEncryptionKeys(atClientManager.atClient, atSign); - await E2ESyncService.getInstance().syncData( - atClientManager.atClient.syncService, - syncOptions: SyncOptions()..waitForFullSyncToComplete = true); + if (enableInitialSync) { + await E2ESyncService.getInstance().syncData( + atClientManager.atClient.syncService, + syncOptions: SyncOptions()..waitForFullSyncToComplete = true); + } // verify if the public key is in the local secondary var result = await atClientManager.atClient diff --git a/tests/at_end2end_test/test/notify_with_isolate_test.dart b/tests/at_end2end_test/test/notify_with_isolate_test.dart new file mode 100644 index 000000000..34c30025f --- /dev/null +++ b/tests/at_end2end_test/test/notify_with_isolate_test.dart @@ -0,0 +1,82 @@ +import 'dart:io'; +import 'dart:isolate'; + +import 'package:at_client/at_client.dart'; +import 'package:at_end2end_test/config/config_util.dart'; +import 'package:at_end2end_test/src/test_initializers.dart'; +import 'package:at_end2end_test/utils/test_constants.dart'; +import 'package:test/test.dart'; + +String currentAtSign = ConfigUtil.getYaml()['atSign']['firstAtSign']; +String sharedWithAtSign = ConfigUtil.getYaml()['atSign']['secondAtSign']; +String authType = ConfigUtil.getYaml()['authType']; + +void main() { + String notifyKey = '$sharedWithAtSign:phone.wavi$currentAtSign'; + String value = '+91 9868123123'; + + test('A test to send and receive notification with isolate', () async { + ReceivePort mainIsolateReceivePort = ReceivePort('MainIsolateReceivePort'); + + // Spawn an isolate to listen for notifications + Isolate childIsolate = + await Isolate.spawn(initSharedAtSign, mainIsolateReceivePort.sendPort); + // Listen for messages from isolate + mainIsolateReceivePort.listen(expectAsync1((data) { + expect(data.value, value); + expect(data.key, notifyKey); + expect(data.from, currentAtSign); + expect(data.to, sharedWithAtSign); + childIsolate.kill(); + })); + + // Initialize another atSign to send notifications + await TestSuiteInitializer.getInstance().testInitializer( + currentAtSign, TestConstants.namespace, authType, + enableInitialSync: false, + atClientPreference: getAtClientPreferences(currentAtSign)); + + NotificationResult notificationResult = await AtClientManager.getInstance() + .atClient + .notificationService + .notify(NotificationParams.forUpdate(AtKey.fromString(notifyKey), + value: value)); + + expect(notificationResult.notificationStatusEnum, + NotificationStatusEnum.delivered); + }); + + tearDown(() { + // Remove hive directories + Directory('test/hive/$currentAtSign').deleteSync(recursive: true); + Directory('test/hive/$sharedWithAtSign').deleteSync(recursive: true); + }); +} + +Future initSharedAtSign(SendPort mainIsolateSendPort) async { + await TestSuiteInitializer.getInstance().testInitializer( + sharedWithAtSign, TestConstants.namespace, authType, + enableInitialSync: false, + atClientPreference: getAtClientPreferences(sharedWithAtSign)); + + AtClientManager.getInstance() + .atClient + .notificationService + .subscribe(shouldDecrypt: true) + .listen((onData) { + // Ignore stats notifications + if (onData.id == '-1') { + return; + } + mainIsolateSendPort.send(onData); + }); +} + +AtClientPreference getAtClientPreferences(String atSign) { + var atClientPreference = AtClientPreference(); + atClientPreference.hiveStoragePath = 'test/hive/$atSign'; + atClientPreference.commitLogPath = 'test/hive/$atSign/commit/'; + atClientPreference.isLocalStoreRequired = true; + atClientPreference.rootDomain = ConfigUtil.getYaml()['root_server']['url']; + return atClientPreference; +} From 7bd511599fecd34b839333341ea6c93344bb2124 Mon Sep 17 00:00:00 2001 From: Sitaram Kalluri Date: Tue, 30 Jul 2024 13:53:46 +0530 Subject: [PATCH 2/2] fix: Modify the namespace for e2e test --- tests/at_end2end_test/test/notify_with_isolate_test.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/at_end2end_test/test/notify_with_isolate_test.dart b/tests/at_end2end_test/test/notify_with_isolate_test.dart index 34c30025f..c26979107 100644 --- a/tests/at_end2end_test/test/notify_with_isolate_test.dart +++ b/tests/at_end2end_test/test/notify_with_isolate_test.dart @@ -12,7 +12,8 @@ String sharedWithAtSign = ConfigUtil.getYaml()['atSign']['secondAtSign']; String authType = ConfigUtil.getYaml()['authType']; void main() { - String notifyKey = '$sharedWithAtSign:phone.wavi$currentAtSign'; + String notifyKey = + '$sharedWithAtSign:phone.${TestConstants.namespace}$currentAtSign'; String value = '+91 9868123123'; test('A test to send and receive notification with isolate', () async {