From a09cee599e6dbd49464c9393433219a1176c7cb8 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Fri, 11 Aug 2023 14:28:19 +0300 Subject: [PATCH] Address PR review comments --- MatrixSDK.xcodeproj/project.pbxproj | 24 +- .../Dehydration/DehydrationService.swift | 6 +- MatrixSDK/Crypto/MXCryptoV2.swift | 4 +- MatrixSDK/JSONModels/MXJSONModels.m | 8 +- MatrixSDK/MXRestClient.h | 27 + MatrixSDK/MXRestClient.m | 2 +- .../MXCryptoMachineUnitTests.swift | 6 +- .../CryptoMachine/MXCryptoProtocolStubs.swift | 8 + MatrixSDKTests/MXDehydrationTests.m | 527 ------------------ Podfile.lock | 2 +- 10 files changed, 57 insertions(+), 557 deletions(-) delete mode 100644 MatrixSDKTests/MXDehydrationTests.m diff --git a/MatrixSDK.xcodeproj/project.pbxproj b/MatrixSDK.xcodeproj/project.pbxproj index 643661e11e..e2df01a296 100644 --- a/MatrixSDK.xcodeproj/project.pbxproj +++ b/MatrixSDK.xcodeproj/project.pbxproj @@ -43,6 +43,8 @@ 1838928927031D1D003F0C4F /* MXSendReplyEventStringLocalizerProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 1838928527031D1D003F0C4F /* MXSendReplyEventStringLocalizerProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; 18937E7C273A5AE500902626 /* MXPollRelationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18937E79273A5AB000902626 /* MXPollRelationTests.m */; }; 18937E7D273A5AE500902626 /* MXPollRelationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18937E79273A5AB000902626 /* MXPollRelationTests.m */; }; + 189B8D102A864C250088D7CE /* DehydrationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 189B8D0F2A864C250088D7CE /* DehydrationService.swift */; }; + 189B8D112A864C250088D7CE /* DehydrationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 189B8D0F2A864C250088D7CE /* DehydrationService.swift */; }; 18B22A7027707CDD00482170 /* MXEventContentLocation.h in Headers */ = {isa = PBXBuildFile; fileRef = 18B22A6E27707CDD00482170 /* MXEventContentLocation.h */; settings = {ATTRIBUTES = (Public, ); }; }; 18B22A7127707CDD00482170 /* MXEventContentLocation.h in Headers */ = {isa = PBXBuildFile; fileRef = 18B22A6E27707CDD00482170 /* MXEventContentLocation.h */; settings = {ATTRIBUTES = (Public, ); }; }; 18B22A7227707CDD00482170 /* MXEventContentLocation.m in Sources */ = {isa = PBXBuildFile; fileRef = 18B22A6F27707CDD00482170 /* MXEventContentLocation.m */; }; @@ -680,8 +682,6 @@ 3A59A52C25A7B1B000DDA1FC /* MXOutboundSessionInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A59A52825A7B1B000DDA1FC /* MXOutboundSessionInfo.m */; }; 3A7509BB26FC61DF00B85773 /* MXSpaceNotificationCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7509BA26FC61DF00B85773 /* MXSpaceNotificationCounter.swift */; }; 3A7509BC26FC61DF00B85773 /* MXSpaceNotificationCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7509BA26FC61DF00B85773 /* MXSpaceNotificationCounter.swift */; }; - 3A7B8D0E267FCF7200D9DD96 /* MXDehydrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A7B8CFD267FCD9B00D9DD96 /* MXDehydrationTests.m */; }; - 3A7B8D13267FCF7300D9DD96 /* MXDehydrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A7B8CFD267FCD9B00D9DD96 /* MXDehydrationTests.m */; }; 3A858DDA2750EE3F006322C1 /* MXHomeserverCapabilitiesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A858DD92750EE3F006322C1 /* MXHomeserverCapabilitiesService.swift */; }; 3A858DDD275121DB006322C1 /* MXHomeserverCapabilitiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A858DDB275120D1006322C1 /* MXHomeserverCapabilitiesTests.swift */; }; 3A858DDE275121DC006322C1 /* MXHomeserverCapabilitiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A858DDB275120D1006322C1 /* MXHomeserverCapabilitiesTests.swift */; }; @@ -698,10 +698,6 @@ 3AB5EBB5270B332B0058703A /* MXSpaceStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AB5EBB3270B332B0058703A /* MXSpaceStore.swift */; }; 3AB5EBB7270ED1C00058703A /* MXSpaceFileStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AB5EBB6270ED1C00058703A /* MXSpaceFileStore.swift */; }; 3AB5EBB8270ED1C00058703A /* MXSpaceFileStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AB5EBB6270ED1C00058703A /* MXSpaceFileStore.swift */; }; - 3AC135D92640335100EE1E74 /* MXDehydrationService.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AC135D72640335100EE1E74 /* MXDehydrationService.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3AC135DA2640335100EE1E74 /* MXDehydrationService.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AC135D72640335100EE1E74 /* MXDehydrationService.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3AC135DB2640335100EE1E74 /* MXDehydrationService.m in Sources */ = {isa = PBXBuildFile; fileRef = 3AC135D82640335100EE1E74 /* MXDehydrationService.m */; }; - 3AC135DC2640335100EE1E74 /* MXDehydrationService.m in Sources */ = {isa = PBXBuildFile; fileRef = 3AC135D82640335100EE1E74 /* MXDehydrationService.m */; }; 3AC13802264482A100EE1E74 /* MXExportedOlmDevice.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AC13800264482A100EE1E74 /* MXExportedOlmDevice.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3AC13803264482A100EE1E74 /* MXExportedOlmDevice.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AC13800264482A100EE1E74 /* MXExportedOlmDevice.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3AC13804264482A100EE1E74 /* MXExportedOlmDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 3AC13801264482A100EE1E74 /* MXExportedOlmDevice.m */; }; @@ -2166,6 +2162,7 @@ 1838928427031D1D003F0C4F /* MXRoomNameStringLocalizerProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXRoomNameStringLocalizerProtocol.h; sourceTree = ""; }; 1838928527031D1D003F0C4F /* MXSendReplyEventStringLocalizerProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXSendReplyEventStringLocalizerProtocol.h; sourceTree = ""; }; 18937E79273A5AB000902626 /* MXPollRelationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXPollRelationTests.m; sourceTree = ""; }; + 189B8D0F2A864C250088D7CE /* DehydrationService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DehydrationService.swift; sourceTree = ""; }; 18B22A6E27707CDD00482170 /* MXEventContentLocation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXEventContentLocation.h; sourceTree = ""; }; 18B22A6F27707CDD00482170 /* MXEventContentLocation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXEventContentLocation.m; sourceTree = ""; }; 18C26C3C273C031900805154 /* PollAggregator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PollAggregator.swift; sourceTree = ""; }; @@ -2670,7 +2667,6 @@ 3A59A52725A7B1B000DDA1FC /* MXOutboundSessionInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXOutboundSessionInfo.h; sourceTree = ""; }; 3A59A52825A7B1B000DDA1FC /* MXOutboundSessionInfo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXOutboundSessionInfo.m; sourceTree = ""; }; 3A7509BA26FC61DF00B85773 /* MXSpaceNotificationCounter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXSpaceNotificationCounter.swift; sourceTree = ""; }; - 3A7B8CFD267FCD9B00D9DD96 /* MXDehydrationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXDehydrationTests.m; sourceTree = ""; }; 3A858DD92750EE3F006322C1 /* MXHomeserverCapabilitiesService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXHomeserverCapabilitiesService.swift; sourceTree = ""; }; 3A858DDB275120D1006322C1 /* MXHomeserverCapabilitiesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXHomeserverCapabilitiesTests.swift; sourceTree = ""; }; 3A858DE027517C0E006322C1 /* MXRoomCapabilityType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXRoomCapabilityType.swift; sourceTree = ""; }; @@ -2679,8 +2675,6 @@ 3A9E2B4228EB3960000DB2A7 /* MXMatrixVersionsUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXMatrixVersionsUnitTests.swift; sourceTree = ""; }; 3AB5EBB3270B332B0058703A /* MXSpaceStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXSpaceStore.swift; sourceTree = ""; }; 3AB5EBB6270ED1C00058703A /* MXSpaceFileStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXSpaceFileStore.swift; sourceTree = ""; }; - 3AC135D72640335100EE1E74 /* MXDehydrationService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXDehydrationService.h; sourceTree = ""; }; - 3AC135D82640335100EE1E74 /* MXDehydrationService.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXDehydrationService.m; sourceTree = ""; }; 3AC13800264482A100EE1E74 /* MXExportedOlmDevice.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXExportedOlmDevice.h; sourceTree = ""; }; 3AC13801264482A100EE1E74 /* MXExportedOlmDevice.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXExportedOlmDevice.m; sourceTree = ""; }; 3AD4F230274B922C003F47FE /* MXRoomAliasAvailabilityChecker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXRoomAliasAvailabilityChecker.swift; sourceTree = ""; }; @@ -4389,7 +4383,6 @@ 324DD2BA246C3ADE00377005 /* MXCryptoSecretStorageTests.m */, 322D01C322492B0700150C68 /* MXCryptoShareTests.m */, 322A51D71D9E846800C8536D /* MXCryptoTests.m */, - 3A7B8CFD267FCD9B00D9DD96 /* MXDehydrationTests.m */, 3281E89D19E299C000976E1A /* MXErrorUnitTests.m */, B146D4FC21A5C0BC00D8C2C6 /* MXEventScanStoreUnitTests.m */, 32A31BC020D3F4C4005916C7 /* MXFilterTests.m */, @@ -4656,8 +4649,7 @@ 3AC1379326432ED500EE1E74 /* Dehydration */ = { isa = PBXGroup; children = ( - 3AC135D72640335100EE1E74 /* MXDehydrationService.h */, - 3AC135D82640335100EE1E74 /* MXDehydrationService.m */, + 189B8D0F2A864C250088D7CE /* DehydrationService.swift */, ); path = Dehydration; sourceTree = ""; @@ -6047,7 +6039,6 @@ 18B22A7027707CDD00482170 /* MXEventContentLocation.h in Headers */, EC60EDC6265CFEA800B39A4E /* MXRoomSyncUnreadNotifications.h in Headers */, 329FB1791A0A74B100A5E88E /* MXTools.h in Headers */, - 3AC135D92640335100EE1E74 /* MXDehydrationService.h in Headers */, 322691321E5EF77D00966A6E /* MXDeviceListOperation.h in Headers */, EC60EE06265CFFF400B39A4E /* MXGroupSyncProfile.h in Headers */, 32481A841C03572900782AD3 /* MXRoomAccountData.h in Headers */, @@ -6550,7 +6541,6 @@ B19A309F240424BD00FB6F35 /* MXQRCodeTransaction_Private.h in Headers */, 3AC13803264482A100EE1E74 /* MXExportedOlmDevice.h in Headers */, B14EF3262397E90400758AF0 /* MXEventScanStoreDelegate.h in Headers */, - 3AC135DA2640335100EE1E74 /* MXDehydrationService.h in Headers */, B14EF3292397E90400758AF0 /* MXAutoDiscovery.h in Headers */, EC8A53D725B1BCC6004E0802 /* MXThirdPartyUserInstance.h in Headers */, EC60EDD1265CFECC00B39A4E /* MXRoomSyncSummary.h in Headers */, @@ -6942,6 +6932,7 @@ 32A1513F1DAF768D00400192 /* MXOlmInboundGroupSession.m in Sources */, 3259CFE626026A6F00C365DB /* MXRestClient+Extensions.swift in Sources */, EC60ED91265CFD3B00B39A4E /* MXRoomSync.m in Sources */, + 189B8D102A864C250088D7CE /* DehydrationService.swift in Sources */, 3AB5EBB4270B332B0058703A /* MXSpaceStore.swift in Sources */, EC8A53A125B1BC77004E0802 /* MXCallSelectAnswerEventContent.m in Sources */, B14EECE72577F76100448735 /* MXLoginSSOFlow.m in Sources */, @@ -6997,7 +6988,6 @@ EC60ED87265CFD0700B39A4E /* MXRoomsSyncResponse.m in Sources */, 3213301E228B190F0070BA9B /* MXRealmAggregationsMapper.m in Sources */, 8EC511062568216B00EC4E5B /* MXTaggedEventInfo.m in Sources */, - 3AC135DB2640335100EE1E74 /* MXDehydrationService.m in Sources */, 32792BDD2296B90A00F4FC9D /* MXAggregatedEditsUpdater.m in Sources */, ECDBE69028E5D961000C83AF /* MXClientInformationService.swift in Sources */, ED44F01428180EAB00452A5D /* MXSharedHistoryKeyManager.swift in Sources */, @@ -7523,7 +7513,6 @@ ECAE7AEC24ED75F1002FA813 /* MXHTTPAdditionalHeadersUnitTests.m in Sources */, 32AF9292241112850008A0FD /* MXCryptoSecretShareTests.m in Sources */, ED8F1D342885ADE200F897E7 /* MXCryptoProtocolStubs.swift in Sources */, - 3A7B8D0E267FCF7200D9DD96 /* MXDehydrationTests.m in Sources */, 3281E8A019E2CC1200976E1A /* MXHTTPClientTests.m in Sources */, 321809B919EEBF3000377451 /* MXEventTests.m in Sources */, B1B44319283D00CA00BB26F4 /* MXMegolmDecryptionUnitTests.swift in Sources */, @@ -7614,6 +7603,7 @@ B19EC8A3260E134A00543BEC /* MXRoomInitialStateEventBuilder.swift in Sources */, B14EF1CA2397E90400758AF0 /* MXOlmInboundGroupSession.m in Sources */, B14EF1CB2397E90400758AF0 /* MXRoomAccountData.m in Sources */, + 189B8D112A864C250088D7CE /* DehydrationService.swift in Sources */, 3AB5EBB5270B332B0058703A /* MXSpaceStore.swift in Sources */, B14EF1CC2397E90400758AF0 /* MXEventAnnotationChunk.m in Sources */, B14EF1CD2397E90400758AF0 /* MXRealmEventScanStore.m in Sources */, @@ -8034,7 +8024,6 @@ B1EE98D22804883B00AB63F0 /* MXGeoURIComponents.swift in Sources */, 323F877E25546170009E9E67 /* MXBaseProfiler.m in Sources */, ECDA763627B527BA000C48CF /* MXEvent+Extensions.swift in Sources */, - 3AC135DC2640335100EE1E74 /* MXDehydrationService.m in Sources */, B141DF0F283CDD180023867A /* Realm+MatrixSDK.swift in Sources */, B14EF27C2397E90400758AF0 /* MXRestClient.swift in Sources */, 32AF929A24115D8B0008A0FD /* MXPendingSecretShareRequest.m in Sources */, @@ -8193,7 +8182,6 @@ B1E09A292397FD080057C069 /* MXLoggerUnitTests.m in Sources */, B1E09A492398028D0057C069 /* MXSelfSignedHomeserverTests.m in Sources */, 32B0E3E523A384D40054FF1A /* MXAggregatedReferenceTests.m in Sources */, - 3A7B8D13267FCF7300D9DD96 /* MXDehydrationTests.m in Sources */, ED7019E32886C29400FC31B9 /* Device+Stub.swift in Sources */, 32D5D16423E400A600E3E37C /* MXRoomSummaryTrustTests.m in Sources */, B1E09A1B2397FCE90057C069 /* MXAggregatedReactionTests.m in Sources */, diff --git a/MatrixSDK/Crypto/Dehydration/DehydrationService.swift b/MatrixSDK/Crypto/Dehydration/DehydrationService.swift index bb2467d05e..6f4ccb9c77 100644 --- a/MatrixSDK/Crypto/Dehydration/DehydrationService.swift +++ b/MatrixSDK/Crypto/Dehydration/DehydrationService.swift @@ -77,7 +77,7 @@ public class DehydrationService: NSObject { // And attempt to delete the dehydrated device but ignore failures try? await deleteDehydratedDevice(deviceId: deviceId) case .failure(let error): - // If not dehydrated devices are available just continue and create a new one + // If no dehydrated devices are available just continue and create a new one if case .noDehydratedDeviceAvailable = error { break } else { @@ -152,7 +152,9 @@ public class DehydrationService: NSObject { private func rehydrateDevice(pickleKeyData: [UInt8]) async -> Result<(deviceId: String, rehydratedDevice: RehydratedDeviceProtocol), DehydrationServiceError> { await withCheckedContinuation { continuation in - self.restClient.retrieveDehydratedDevice { dehydratedDevice in + self.restClient.retrieveDehydratedDevice { [weak self] dehydratedDevice in + guard let self else { return } + MXLog.info("Successfully retrieved dehydrated device with id: \(dehydratedDevice.deviceId)") guard let deviceDataJSON = MXTools.serialiseJSONObject(dehydratedDevice.deviceData) else { diff --git a/MatrixSDK/Crypto/MXCryptoV2.swift b/MatrixSDK/Crypto/MXCryptoV2.swift index 8001b1e047..60fbda52b5 100644 --- a/MatrixSDK/Crypto/MXCryptoV2.swift +++ b/MatrixSDK/Crypto/MXCryptoV2.swift @@ -144,8 +144,8 @@ class MXCryptoV2: NSObject, MXCrypto { ) dehydrationService = DehydrationService(restClient: restClient, - secretStorage: secretStorage, - dehydratedDevices: machine.dehydratedDevices()) + secretStorage: secretStorage, + dehydratedDevices: machine.dehydratedDevices()) log.debug("Initialized Crypto module") } diff --git a/MatrixSDK/JSONModels/MXJSONModels.m b/MatrixSDK/JSONModels/MXJSONModels.m index eee2798888..0052544372 100644 --- a/MatrixSDK/JSONModels/MXJSONModels.m +++ b/MatrixSDK/JSONModels/MXJSONModels.m @@ -2181,8 +2181,8 @@ @implementation MXDehydratedDeviceResponse + (instancetype)modelFromJSON:(NSDictionary *)JSONDictionary { MXDehydratedDeviceResponse *dehydratedDevice = [[MXDehydratedDeviceResponse alloc] init]; - dehydratedDevice.deviceId = JSONDictionary[@"device_id"]; - dehydratedDevice.deviceData = JSONDictionary[@"device_data"]; + MXJSONModelSetString(dehydratedDevice.deviceId, JSONDictionary[@"device_id"]); + MXJSONModelSetDictionary(dehydratedDevice.deviceData, JSONDictionary[@"device_data"]); return dehydratedDevice; } @@ -2193,8 +2193,8 @@ @implementation MXDehydratedDeviceEventsResponse + (instancetype)modelFromJSON:(NSDictionary *)JSONDictionary { MXDehydratedDeviceEventsResponse *dehydratedDevice = [[MXDehydratedDeviceEventsResponse alloc] init]; - dehydratedDevice.events = JSONDictionary[@"events"]; - dehydratedDevice.nextBatch = JSONDictionary[@"next_batch"]; + MXJSONModelSetArray(dehydratedDevice.events, JSONDictionary[@"events"]); + MXJSONModelSetString(dehydratedDevice.nextBatch, JSONDictionary[@"next_batch"]); return dehydratedDevice; } diff --git a/MatrixSDK/MXRestClient.h b/MatrixSDK/MXRestClient.h index dcc954d8b0..981c3fc992 100644 --- a/MatrixSDK/MXRestClient.h +++ b/MatrixSDK/MXRestClient.h @@ -2518,16 +2518,43 @@ Note: Clients should consider avoiding this endpoint for URLs posted in encrypte #pragma mark - Device Dehydration +/** + Creates a new dehydrated device on the current user's account with the given parameters, coming out of the RustCryptoSDK + @param parameters the device data as received from the RustCryptoSDK + @param success A block object called when the operation succeeds. It provides the ID of the newly dehydrated device. + @param failure A block object called when the operation fails. + @return a MXHTTPOperation instance. + */ - (MXHTTPOperation*)createDehydratedDevice:(MXDehydratedDeviceCreationParameters *)parameters success:(void (^)(NSString * _Nonnull deviceId))success failure:(void (^)(NSError * _Nonnull error))failure; +/** + Get the dehydrated device of the current account. + @param success A block object called when the operation succeeds. It provides a `MXDehydratedDeviceResponse` instance of the current account. + @param failure A block object called when the operation fails. + @return a MXHTTPOperation instance. + */ - (MXHTTPOperation*)retrieveDehydratedDeviceWithSuccess:(void (^)(MXDehydratedDeviceResponse * _Nonnull dehydratedDevice))success failure:(void (^)(NSError * _Nonnull error))failure; +/** + Delete the current dehydrated device + @param success A block object called when the operation succeeds + @param failure A block object called when the operation fails. + @return a MXHTTPOperation instance. + */ - (MXHTTPOperation*)deleteDehydratedDeviceWithSuccess:(void (^)(void))success failure:(void (^)(NSError * _Nonnull error))failure; +/** + Retrieves the to device events stored on the backend for the given dehydrated device. Results are batched so multiple invocations might be necessary + @param deviceId The dehydrated device id in question + @param nextBatch Pagination token for retrieving more events + @param success A block object called when the operation succeeds. It provides the events and a next batch token + @param failure A block object called when the operation fails. + @return a MXHTTPOperation instance. + */ - (MXHTTPOperation*)retrieveDehydratedDeviceEventsForDeviceId:(NSString *)deviceId nextBatch:(NSString *)nextBatch success:(void (^)(MXDehydratedDeviceEventsResponse * _Nonnull dehydratedDeviceEventsResponse))success diff --git a/MatrixSDK/MXRestClient.m b/MatrixSDK/MXRestClient.m index bd8daa84d4..fc73875a27 100644 --- a/MatrixSDK/MXRestClient.m +++ b/MatrixSDK/MXRestClient.m @@ -4999,7 +4999,7 @@ - (MXHTTPOperation*)createDehydratedDevice:(MXDehydratedDeviceCreationParameters success:^(NSDictionary *JSONResponse) { __block NSString *deviceId; [self dispatchProcessing:^{ - deviceId = JSONResponse[@"device_id"]; + MXJSONModelSetString(deviceId, JSONResponse[@"device_id"]) } andCompletion:^{ success(deviceId); }]; diff --git a/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoMachineUnitTests.swift b/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoMachineUnitTests.swift index 91dd3f51cc..9456290585 100644 --- a/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoMachineUnitTests.swift +++ b/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoMachineUnitTests.swift @@ -156,7 +156,8 @@ class MXCryptoMachineUnitTests: XCTestCase { toDevice: nil, deviceLists: nil, deviceOneTimeKeysCounts: [:], - unusedFallbackKeys: nil + unusedFallbackKeys: nil, + nextBatchToken: "" ) XCTAssertEqual(result.events.count, 0) } @@ -174,7 +175,8 @@ class MXCryptoMachineUnitTests: XCTestCase { toDevice: toDevice, deviceLists: deviceList, deviceOneTimeKeysCounts: [:], - unusedFallbackKeys: nil + unusedFallbackKeys: nil, + nextBatchToken: "" ) XCTAssertEqual(result.events.count, 1) } diff --git a/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoProtocolStubs.swift b/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoProtocolStubs.swift index f6788ad758..8ab364c5b9 100644 --- a/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoProtocolStubs.swift +++ b/MatrixSDKTests/Crypto/CryptoMachine/MXCryptoProtocolStubs.swift @@ -41,6 +41,10 @@ class DevicesSourceStub: CryptoIdentityStub, MXCryptoDevicesSource { func devices(userId: String) -> [Device] { return devices[userId]?.map { $0.value } ?? [] } + + func dehydratedDevices() -> DehydratedDevicesProtocol { + fatalError() + } } class UserIdentitySourceStub: CryptoIdentityStub, MXCryptoUserIdentitySource { @@ -155,6 +159,10 @@ class CryptoCrossSigningStub: CryptoIdentityStub, MXCryptoCrossSigning { func devices(userId: String) -> [Device] { return devices[userId]?.map { $0.value } ?? [] } + + func dehydratedDevices() -> DehydratedDevicesProtocol { + fatalError() + } } class CryptoVerificationStub: CryptoIdentityStub { diff --git a/MatrixSDKTests/MXDehydrationTests.m b/MatrixSDKTests/MXDehydrationTests.m deleted file mode 100644 index effa122d2b..0000000000 --- a/MatrixSDKTests/MXDehydrationTests.m +++ /dev/null @@ -1,527 +0,0 @@ -// -// Copyright 2021 The Matrix.org Foundation C.I.C -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#import - -#import "MatrixSDKTestsData.h" -#import "MatrixSDKTestsE2EData.h" - -#import "MXSession.h" -#import "MXCrypto_Private.h" -#import "MXCrossSigning_Private.h" - -#import "MXSDKOptions.h" - -#import "MXKeyProvider.h" -#import "MXAesKeyData.h" -#import "MXRawDataKey.h" - -#import -#import "MXDehydrationService.h" -#import "MatrixSDKTestsSwiftHeader.h" - -// Do not bother with retain cycles warnings in tests -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Warc-retain-cycles" - -@interface MXDehydrationTests : XCTestCase - -@property (nonatomic, strong) MatrixSDKTestsData *matrixSDKTestsData; -@property (nonatomic, strong) MatrixSDKTestsE2EData *matrixSDKTestsE2EData; - -@property (nonatomic, strong) NSData *dehydrationKey; - -@property (nonatomic, strong) MXDehydrationService *dehydrationService; - -@end - -@implementation MXDehydrationTests - -- (void)setUp -{ - [super setUp]; - - _matrixSDKTestsData = [[MatrixSDKTestsData alloc] init]; - _matrixSDKTestsE2EData = [[MatrixSDKTestsE2EData alloc] initWithMatrixSDKTestsData:_matrixSDKTestsData]; - - _dehydrationKey = [@"6fXK17pQFUrFqOnxt3wrqz8RHkQUT9vQ" dataUsingEncoding:NSUTF8StringEncoding]; - _dehydrationService = [MXDehydrationService new]; -} - -- (void)tearDown -{ - _matrixSDKTestsData = nil; - _matrixSDKTestsE2EData = nil; - _dehydrationService = nil; - - [super tearDown]; -} - -// Check device dehydration -// - Have e2e Alice -// - Alice creates a dehydrated device -// - Alice downloads their own devices keys -// -> Alice must see their dehydrated device --(void)testDehydrateDevice -{ - // - Have e2e Alice - MXWeakify(self); - [self.matrixSDKTestsE2EData doE2ETestWithAliceInARoom:self readyToTest:^(MXSession *mxSession, NSString *roomId, XCTestExpectation *expectation) { - MXStrongifyAndReturnIfNil(self); - [mxSession.crypto.crossSigning setupWithPassword:MXTESTS_ALICE_PWD success:^{ - - // - Alice creates a dehydrated device - [self.dehydrationService dehydrateDeviceWithMatrixRestClient:mxSession.matrixRestClient crossSigning:mxSession.legacyCrypto.legacyCrossSigning dehydrationKey:self.dehydrationKey success:^(NSString *dehydratedDeviceId) { - // - Alice downloads their own devices keys - [mxSession.crypto downloadKeys:@[mxSession.myUserId] forceDownload:YES success:^(MXUsersDevicesMap *usersDevicesInfoMap, NSDictionary *crossSigningKeysMap) { - - // -> Alice must see her dehydrated device - XCTAssertEqual([usersDevicesInfoMap deviceIdsForUser:mxSession.myUserId].count, 2); - - MXDeviceInfo *dehydratedDevice = [usersDevicesInfoMap objectForDevice:dehydratedDeviceId forUser:mxSession.myUserId]; - XCTAssertNotNil(dehydratedDevice); - XCTAssertEqualObjects(dehydratedDevice.deviceId, dehydratedDeviceId); - XCTAssertTrue([mxSession.legacyCrypto.legacyCrossSigning isDeviceVerified:dehydratedDevice]); - - [expectation fulfill]; - } failure:^(NSError * error) { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError * error) { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError * error) { - XCTFail(@"Failed setting up cross-signing with error: %@", error); - [expectation fulfill]; - }]; - }]; -} - -// Check that others can see a dehydrated device -// - Alice and Bob are in an e2e room -// - Bob creates a dehydrated device and logs out -// - Alice downloads Bob's devices keys -// -> Alice must see Bob's dehydrated device --(void)testDehydrateDeviceSeenByOther -{ - // - Alice and Bob are in an e2e room - [self.matrixSDKTestsE2EData doE2ETestWithAliceAndBobInARoom:self cryptedBob:YES warnOnUnknowDevices:NO readyToTest:^(MXSession *aliceSession, MXSession *bobSession, NSString *roomId, XCTestExpectation *expectation) { - [bobSession.crypto.crossSigning setupWithPassword:MXTESTS_BOB_PWD success:^{ - - NSString *bobUserId = bobSession.myUserId; - - // - Bob creates a dehydrated device and logs out - [self.dehydrationService dehydrateDeviceWithMatrixRestClient:bobSession.matrixRestClient crossSigning:bobSession.legacyCrypto.legacyCrossSigning dehydrationKey:self.dehydrationKey success:^(NSString *bobDehydratedDeviceId) { - dispatch_async(dispatch_get_main_queue(), ^{ - [bobSession logout:^{ - - // - Alice download Bob's devices keys - [aliceSession.crypto downloadKeys:@[bobUserId] forceDownload:YES success:^(MXUsersDevicesMap *usersDevicesInfoMap, NSDictionary *crossSigningKeysMap) { - - NSLog(@"[MXCryptoTest] User devices: %@", [usersDevicesInfoMap deviceIdsForUser:bobUserId]); - - // -> Alice must see Bob's dehydrated device - XCTAssertEqual([usersDevicesInfoMap deviceIdsForUser:bobUserId].count, 1); - - MXDeviceInfo *bobDehydratedDevice = [usersDevicesInfoMap objectForDevice:bobDehydratedDeviceId forUser:bobUserId]; - XCTAssertNotNil(bobDehydratedDevice); - XCTAssertEqualObjects(bobDehydratedDevice.deviceId, bobDehydratedDeviceId); - - [expectation fulfill]; - - } failure:^(NSError * error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError * error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - }); - } failure:^(NSError * error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError * error) { - XCTFail(@"Failed setting up cross-signing with error: %@", error); - }]; - }]; -} - -// Check that device rehydration fails silently if no dehydrated device exists -// - Bob logs in (no device dehydration) -// - Bob tries to rehydrate a device -// -> Bob should start his session normally --(void)testDeviceRehydrationWithoutDehydratedDevice -{ - [MXSDKOptions sharedInstance].enableCryptoWhenStartingMXSession = NO; - - // - Bob logs in (no device dehydration) - [self.matrixSDKTestsData doMXRestClientTestWithBob:self readyToTest:^(MXRestClient *bobRestClient, XCTestExpectation *expectation) { - MXSession *mxSession = [[MXSession alloc] initWithMatrixRestClient:bobRestClient]; - [self.matrixSDKTestsData retain:mxSession]; - - // - Bob tries to rehydrate a device - [self.dehydrationService rehydrateDeviceWithMatrixRestClient:mxSession.matrixRestClient dehydrationKey:self.dehydrationKey success:^(NSString *deviceId) { - XCTFail(@"No rehydrated device should be found."); - [expectation fulfill]; - - } failure:^(NSError *error) { - if ([error.domain isEqual:MXDehydrationServiceErrorDomain] && error.code == MXDehydrationServiceNothingToRehydrateErrorCode) - { - [mxSession start:^{ - // -> Bob should start his session normally - [expectation fulfill]; - - } failure:^(NSError *error) { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - }]; - } - else - { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - } - }]; - }]; -} - -// Check if a dehydrated device can be properly rehydrated -// - Alice is in an e2e room -// - Alice setup a dehydrated device -// - Alice logs off and logs in back -// - Alice rehydrate her device -// -> The rehydrated device must have the same properties --(void)testDehydrateDeviceAndClaimDehydratedDevice -{ - // - Alice is in an e2e room - [MXSDKOptions sharedInstance].enableCryptoWhenStartingMXSession = YES; - [self.matrixSDKTestsE2EData doE2ETestWithAliceInARoom:self readyToTest:^(MXSession *aliceSession, NSString *roomId, XCTestExpectation *expectation) { - [aliceSession.crypto.crossSigning setupWithPassword:MXTESTS_ALICE_PWD success:^{ - - NSString *aliceSessionDevice = aliceSession.myDeviceId; - // - Alice setup a dehydrated device - [self.dehydrationService dehydrateDeviceWithMatrixRestClient:aliceSession.matrixRestClient crossSigning:aliceSession.legacyCrypto.legacyCrossSigning dehydrationKey:self.dehydrationKey success:^(NSString *dehydratedDeviceId) { - dispatch_async(dispatch_get_main_queue(), ^{ - // - Alice logs off and logs in back - [self.matrixSDKTestsData loginUserOnANewDevice:self credentials:nil withPassword:MXTESTS_ALICE_PWD sessionToLogout:aliceSession newSessionStore:nil startNewSession:NO e2e:YES onComplete:^(MXSession *aliceSession2) { - - NSString *aliceSession2Device = aliceSession2.myDeviceId; - // - Alice rehydrate her device - [self.dehydrationService rehydrateDeviceWithMatrixRestClient:aliceSession2.matrixRestClient dehydrationKey:self.dehydrationKey success:^(NSString *deviceId) { - // -> The rehydrated device must have the same properties - if (!deviceId) - { - XCTFail(@"device rehydration shouldn't be canceled"); - [expectation fulfill]; - return; - } - aliceSession2.credentials.deviceId = deviceId; - - XCTAssertNotEqualObjects(aliceSessionDevice, aliceSession2Device); - XCTAssertNotEqualObjects(aliceSession2Device, dehydratedDeviceId); - XCTAssertNotEqualObjects(aliceSession2.myDeviceId, aliceSession2Device); - XCTAssertEqualObjects(aliceSession2.myDeviceId, dehydratedDeviceId); - - [aliceSession2 start:^{ - XCTAssertNotNil(aliceSession2.crypto); - XCTAssertEqualObjects(aliceSession2.legacyCrypto.myDevice.deviceId, dehydratedDeviceId); - XCTAssertEqualObjects(aliceSession2.legacyCrypto.store.deviceId, dehydratedDeviceId); - XCTAssertTrue([aliceSession2.crypto.crossSigning canTrustCrossSigning]); - } failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - - [expectation fulfill]; - } failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - }]; - }); - } failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError *error) { - XCTFail(@"Failed setting up cross-signing with error: %@", error); - }]; - }]; -} - -// Check that a user can receive live message with a rehydrated session -// - Alice and Bob are in an e2e room -// - Alice creates a dehydrated device -// - Alice logs out and logs on -// - Alice rehydrates the new session with the dehydrated device -// - And starts her new session with e2e enabled -// - Bob sends a message -// -> Alice must be able to receive and decrypt the message sent by Bob --(void)testReceiveLiveMessageAfterDeviceRehydration -{ - // - Alice and Bob are in an e2e room - [MXSDKOptions sharedInstance].enableCryptoWhenStartingMXSession = YES; - [self.matrixSDKTestsE2EData doE2ETestWithAliceAndBobInARoomWithCryptedMessages:self cryptedBob:YES readyToTest:^(MXSession *aliceSession, MXSession *bobSession, NSString *roomId, XCTestExpectation *expectation) { - [aliceSession.crypto.crossSigning setupWithPassword:MXTESTS_ALICE_PWD success:^{ - - // - Alice creates a dehydrated device - [self.dehydrationService dehydrateDeviceWithMatrixRestClient:aliceSession.matrixRestClient crossSigning:aliceSession.legacyCrypto.legacyCrossSigning dehydrationKey:self.dehydrationKey success:^(NSString *deviceId) { - dispatch_async(dispatch_get_main_queue(), ^{ - // - Alice logs out and logs on - [self.matrixSDKTestsData loginUserOnANewDevice:self credentials:nil withPassword:MXTESTS_ALICE_PWD sessionToLogout:aliceSession newSessionStore:nil startNewSession:NO e2e:YES onComplete:^(MXSession *aliceSession2) { - - MXRestClient *aliceRestClient = aliceSession2.matrixRestClient; - - MXSession *aliceSession3 = [[MXSession alloc] initWithMatrixRestClient:aliceRestClient]; - [self.matrixSDKTestsData retain:aliceSession3]; - - [aliceSession2 close]; - - // - Alice rehydrates the new session with the dehydrated device - [self.dehydrationService rehydrateDeviceWithMatrixRestClient:aliceSession3.matrixRestClient dehydrationKey:self.dehydrationKey success:^(NSString *rehydratedDeviceId) { - if (!rehydratedDeviceId) - { - XCTFail(@"device rehydration shouldn't be canceled"); - [expectation fulfill]; - return; - } - aliceSession3.credentials.deviceId = rehydratedDeviceId; - - [MXSDKOptions sharedInstance].enableCryptoWhenStartingMXSession = YES; - - // - And starts her new session with e2e enabled - [aliceSession3 setStore:[MXMemoryStore new] success:^{ - - [aliceSession3 start:^{ - MXRoom *roomFromBobPOV = [bobSession roomWithRoomId:roomId]; - MXRoom *roomFromAlice3POV = [aliceSession3 roomWithRoomId:roomId]; - - XCTAssertNotNil(roomFromBobPOV, @"roomFromBobPOV shouldn't be nil"); - - if (!roomFromAlice3POV) - { - XCTFail(@"Not able to get room with Alice's session"); - [expectation fulfill]; - return; - } - - NSString *messageFromBob = @"Hello I'm Bob!"; - - [roomFromAlice3POV liveTimeline:^(id liveTimeline) { - // -> Alice must be able to receive and decrypt the message sent by Bob - [liveTimeline listenToEventsOfTypes:@[kMXEventTypeStringRoomMessage, kMXEventTypeStringRoomEncrypted] onEvent:^(MXEvent *event, MXTimelineDirection direction, MXRoomState *roomState) { - - XCTAssertEqual(0, [self checkEncryptedEvent:event roomId:roomId clearMessage:messageFromBob senderSession:bobSession]); - - [expectation fulfill]; - - }]; - }]; - - // - Bob sends a message - [roomFromBobPOV sendTextMessage:messageFromBob threadId:nil success:nil failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError *error) { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError *error) { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError *error) { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - }]; - }]; - }); - } failure:^(NSError *error) { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError *error) { - XCTFail(@"Failed setting up cross-signing with error: %@", error); - }]; - }]; - return; -} - -// Check that others can see a dehydrated device -// - Alice and Bob are in an e2e room -// - Bob creates a dehydrated device and logs out -// - Alice sends a message -// - Bob logs in on a new device -// - Bob rehydrates the new session with the dehydrated device -// - And starts their new session with e2e enabled -// -> Bob must be able to decrypt the message sent by Alice --(void)testReceiveMessageWhileBeingSignedOffWithDeviceRehydration -{ - // - Alice and Bob are in an e2e room - [self.matrixSDKTestsE2EData doE2ETestWithAliceAndBobInARoom:self cryptedBob:YES warnOnUnknowDevices:NO readyToTest:^(MXSession *aliceSession, MXSession *bobSession, NSString *roomId, XCTestExpectation *expectation) { - [bobSession.crypto.crossSigning setupWithPassword:MXTESTS_BOB_PWD success:^{ - MXCredentials *bobCredentials = bobSession.credentials; - - // - Bob creates a dehydrated device and logs out - [self.dehydrationService dehydrateDeviceWithMatrixRestClient:bobSession.matrixRestClient crossSigning:bobSession.legacyCrypto.legacyCrossSigning dehydrationKey:self.dehydrationKey success:^(NSString *bobDehydratedDeviceId) { - dispatch_async(dispatch_get_main_queue(), ^{ - [bobSession logout:^{ - [bobSession close]; - - // - Alice sends a message - NSString *message = @"Hello I'm Alice!"; - MXRoom *roomFromAlicePOV = [aliceSession roomWithRoomId:roomId]; - [roomFromAlicePOV sendTextMessage:message threadId:nil success:^(NSString *eventId) { - - // - Bob logs in on a new device - [self.matrixSDKTestsData loginUserOnANewDevice:self credentials:bobCredentials withPassword:MXTESTS_BOB_PWD sessionToLogout:nil newSessionStore:nil startNewSession:NO e2e:YES onComplete:^(MXSession *bobSession2) { - - // - Bob rehydrates the new session with the dehydrated device - [self.dehydrationService rehydrateDeviceWithMatrixRestClient:bobSession2.matrixRestClient dehydrationKey:self.dehydrationKey success:^(NSString *deviceId) { - if (!deviceId) - { - XCTFail(@"device rehydration shouldn't be canceled"); - [expectation fulfill]; - return; - } - bobSession2.credentials.deviceId = deviceId; - - // - And starts their new session with e2e enabled - [MXSDKOptions sharedInstance].enableCryptoWhenStartingMXSession = YES; - [bobSession2 start:^{ - [MXSDKOptions sharedInstance].enableCryptoWhenStartingMXSession = NO; - - // -> Bob must be able to decrypt the message sent by Alice - [bobSession2 eventWithEventId:eventId inRoom:roomId success:^(MXEvent *event) { - - XCTAssertEqual(event.wireEventType, MXEventTypeRoomEncrypted); - XCTAssertEqual(event.eventType, MXEventTypeRoomMessage); - XCTAssertEqualObjects(event.content[kMXMessageBodyKey], message); - XCTAssertNil(event.decryptionError); - - [expectation fulfill]; - - } failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - - } failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - - } failure:^(NSError *error) { - XCTFail(@"The request should not fail - NSError: %@", error); - [expectation fulfill]; - }]; - }]; - } failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - - } failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - }); - } failure:^(NSError *error) { - XCTFail(@"Cannot set up intial test conditions - error: %@", error); - [expectation fulfill]; - }]; - } failure:^(NSError *error) { - XCTFail(@"Failed setting up cross-signing with error: %@", error); - }]; - }]; -} - -// Test for pickling / unpinling OLM acount -// - create a new OLM Acount -// - pickle the OLM account -// - unpickle the pickled account -// -> identity keys must be the same --(void)testDataPickling -{ - // - create a new OLM Acount - OLMAccount *account = [[OLMAccount alloc] initNewAccount]; - NSDictionary *e2eKeys = [account identityKeys]; - - [account generateOneTimeKeys:50]; - NSDictionary *oneTimeKeys = [account oneTimeKeys]; - - [account generateFallbackKey]; - NSDictionary *fallbackKey = [account fallbackKey]; - - // - pickle the OLM account - NSData *key = [@"6fXK17pQFUrFqOnxt3wrqz8RHkQUT9vQ" dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error = nil; - NSString *serializedAccount = [account serializeDataWithKey:key error:&error]; - - XCTAssertNil(error, "serializeDataWithKey failed due to error %@", error); - - // - unpickle the pickled account - OLMAccount *deserializedAccount = [[OLMAccount alloc] initWithSerializedData:serializedAccount key:key error:&error]; - NSDictionary *deserializedE2eKeys = [deserializedAccount identityKeys]; - NSDictionary *deserializedOneTimeKeys = [deserializedAccount oneTimeKeys]; - NSDictionary *deserializedFallbackKey = [deserializedAccount fallbackKey]; - - // -> identity keys must be the same - XCTAssertNil(error, "initWithSerializedData failed due to error %@", error); - XCTAssert([e2eKeys[@"ed25519"] isEqual:deserializedE2eKeys[@"ed25519"]], @"wrong deserialized ed25519 key %@ != %@", e2eKeys[@"ed25519"], deserializedE2eKeys[@"ed25519"]); - XCTAssert([e2eKeys[@"curve25519"] isEqual:deserializedE2eKeys[@"curve25519"]], @"wrong deserialized curve25519 key %@ != %@", e2eKeys[@"curve25519"], deserializedE2eKeys[@"curve25519"]); - - XCTAssert([oneTimeKeys isEqualToDictionary:deserializedOneTimeKeys]); - XCTAssert([fallbackKey isEqualToDictionary:deserializedFallbackKey]); -} - -#pragma mark - Private methods - -- (NSUInteger)checkEncryptedEvent:(MXEvent*)event roomId:(NSString*)roomId clearMessage:(NSString*)clearMessage senderSession:(MXSession*)senderSession -{ - NSUInteger failureCount = self.testRun.failureCount; - - // Check raw event (encrypted) data as sent by the hs - XCTAssertEqual(event.wireEventType, MXEventTypeRoomEncrypted); - XCTAssertNil(event.wireContent[kMXMessageBodyKey], @"No body field in an encrypted content"); - XCTAssertEqualObjects(event.wireContent[@"algorithm"], kMXCryptoMegolmAlgorithm); - XCTAssertNotNil(event.wireContent[@"ciphertext"]); - XCTAssertNotNil(event.wireContent[@"session_id"]); - XCTAssertNotNil(event.wireContent[@"sender_key"]); - XCTAssertEqualObjects(event.wireContent[@"device_id"], senderSession.legacyCrypto.store.deviceId); - - // Check decrypted data - XCTAssert(event.eventId); - XCTAssertEqualObjects(event.roomId, roomId); - XCTAssertEqual(event.eventType, MXEventTypeRoomMessage); - XCTAssertLessThan(event.age, 10000); - XCTAssertEqualObjects(event.content[kMXMessageBodyKey], clearMessage); - XCTAssertEqualObjects(event.sender, senderSession.myUser.userId); - XCTAssertNil(event.decryptionError); - - // Return the number of failures in this method - return self.testRun.failureCount - failureCount; -} - -@end - -#pragma clang diagnostic pop - diff --git a/Podfile.lock b/Podfile.lock index 99f9debfc7..5dbcefd7eb 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -73,4 +73,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: e70d3694981265116ff81a851fb0c1c9654995cd -COCOAPODS: 1.11.3 +COCOAPODS: 1.12.1