Page MenuHomePhabricator

Implement method in NotificationsCryptoModule to serialize CryptoModule instance and atomically store it in a file.
ClosedPublic

Authored by marcin on Feb 20 2023, 4:39 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Apr 28, 9:05 AM
Unknown Object (File)
Sun, Apr 28, 9:05 AM
Unknown Object (File)
Sun, Apr 28, 9:05 AM
Unknown Object (File)
Sun, Apr 28, 9:05 AM
Unknown Object (File)
Sun, Apr 28, 9:05 AM
Unknown Object (File)
Sun, Apr 28, 9:02 AM
Unknown Object (File)
Sun, Apr 28, 8:20 AM
Unknown Object (File)
Mar 28 2024, 5:25 PM
Subscribers

Details

Summary

This differential implements CryptoModule serialization and atomic write to a file under given path. Its implementation ensures that write is made atomically and in fault-tolerant manner.

Test Plan

Temporarily make this and deserialization method public. In CommCoreModule in initializeCryptoAccount method pass created CryptoModule instance, path returned from PlatformSpecificTools::getNotifications... and some random string (as pickling key) to this method. Then
pass the same pickling key and path returned from PlatformSpecificTools::getNotifications... to NotificationsCryptoModule::deserializeCryptoModule. Using XCode debugger or logging ensure initial CryptoModule matches the one returned from this method.
Note that it is an e2e test that also tests previous differential in more reliable manner.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

marcin published this revision for review.Feb 20 2023, 4:50 AM
marcin added inline comments.
native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp
69 ↗(On Diff #22712)

We use this argument to create a temporary file path. We could create temporary file path using random string but what if any of the steps between temporary file creation and temporary file rename fails? We would be left with some garbage that should be deleted. We could use directory tree search POSIX api to get the path of such garbage temporary file but it would add additional complexity to the solution.
I expect that when calling this from main app we will provide "Comm" as calling process name, and "NotificationService" when calling from NotificationService.

native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp
119 ↗(On Diff #22880)

If file at path already exists, this is expected to overwrite it, right?

Reading the docs says this behavior is stdlib implementation-dependant:

If newname names an existing file, the function may either fail or override the existing file, depending on the specific system and library implementation.

native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp
119 ↗(On Diff #22880)

Can you link the docs you found? The ones I was using are explicit that existing file will be replaced:

  1. https://man7.org/linux/man-pages/man2/rename.2.html
  2. https://pubs.opengroup.org/onlinepubs/000095399/functions/rename.html
native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp
119 ↗(On Diff #22880)

I optimistically suppose that most implementations replace the file anyway. If we want to be 100% sure, we could simply test it on iOS and Android.

native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp
119 ↗(On Diff #22880)

Running the test plan on a freshly installed app should test it since on a freshly installed app the file (under the path returned by PlatformSpecificTools::getNotificationsCryptoAccountPath) does not exists so rename method is called with non-existing file path as newname argument. I executed this test plan on a fresh install and it worked as expected.

bartek added inline comments.
native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp
119 ↗(On Diff #22880)

Ok, thanks for explaining

This revision is now accepted and ready to land.Feb 24 2023, 5:01 AM