Skip to content

Commit

Permalink
Merge pull request #1467 from matrix-org/andy/build_flag
Browse files Browse the repository at this point in the history
Shared history flag in crypto module
  • Loading branch information
Anderas committed May 19, 2022
2 parents 6cb7100 + a1c667a commit 03a153e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
5 changes: 5 additions & 0 deletions MatrixSDK/Crypto/MXCrypto.m
Expand Up @@ -1897,6 +1897,11 @@ - (BOOL)isRoomEncrypted:(NSString *)roomId

- (BOOL)isRoomSharingHistory:(NSString *)roomId
{
if (!MXSDKOptions.sharedInstance.enableRoomSharedHistoryOnInvite)
{
return NO;
}

MXRoom *room = [self.mxSession roomWithRoomId:roomId];
MXRoomHistoryVisibility visibility = room.summary.historyVisibility;
return [visibility isEqualToString:kMXRoomHistoryVisibilityWorldReadable] || [visibility isEqualToString:kMXRoomHistoryVisibilityShared];
Expand Down
Expand Up @@ -49,7 +49,8 @@ class MXMegolmEncryptionTests: XCTestCase {
return store?.inboundGroupSessions().last?.sharedHistory == true
}

func testResetsSessionIfRoomVisibilityChanges() {
func test_resetsSession_ifRoomVisibilityChanges() {
MXSDKOptions.sharedInstance().enableRoomSharedHistoryOnInvite = true

// The following tests that oubound session Id (and therefore the related inbound session Id)
// is reset whenever the room's history visibility changes from shared to not shared.
Expand Down Expand Up @@ -122,4 +123,78 @@ class MXMegolmEncryptionTests: XCTestCase {
}
}
}

func test_doesNotResetsSession_ifRoomVisibilityChanges_andFeatureDisabled() {
MXSDKOptions.sharedInstance().enableRoomSharedHistoryOnInvite = false

// The following tests that if the feature is disabled, then outbound session Id
// is not reset when room visibility changes
e2eData.doE2ETestWithAlice(inARoom: self) { session, roomId, expectation in

let room = session?.room(withRoomId: roomId)

// 1st set of messages
room?.sendTextMessages(messages: ["Hi", "Hello"]) { _ in

// After first few messages we only expect one inbound and one outbound session
let sessionIds1 = self.storedSessionIds(in: session)
XCTAssertEqual(sessionIds1.outbound.count, 1)
XCTAssertEqual(sessionIds1.inbound.count, 1)
XCTAssertEqual(sessionIds1.inbound, sessionIds1.outbound)
XCTAssertFalse(self.isSharedHistoryInLastSession(for: session))

// 2nd set of messages
room?.sendTextMessages(messages: ["Hi", "Hello"]) { _ in

// After second batch of messages nothing has changed that would require resetting
// of sessions, therefore sessionIds are unchanged
let sessionIds2 = self.storedSessionIds(in: session)
XCTAssertEqual(sessionIds2, sessionIds1)
XCTAssertFalse(self.isSharedHistoryInLastSession(for: session))

// Changing room visibility from shared by default to more restrictive will not reset session keys
room?.setHistoryVisibility(.joined) { _ in

// 3rd set of messages
room?.sendTextMessages(messages: ["Hi", "Hello"]) { _ in

// Sessions are identical as before
let sessionIds3 = self.storedSessionIds(in: session)
XCTAssertEqual(sessionIds3.outbound.count, 1)
XCTAssertEqual(sessionIds3.outbound, sessionIds2.outbound)
XCTAssertEqual(sessionIds3.inbound.count, 1)
XCTAssertEqual(sessionIds3.inbound, sessionIds1.outbound)
XCTAssertFalse(self.isSharedHistoryInLastSession(for: session))

// 4th set of messages
room?.sendTextMessages(messages: ["Hi", "Hello"]) { _ in
// After fourth batch of messages nothing has changed that would require resetting
// of sessions, therefore sessionIds are unchanged
let sessionIds4 = self.storedSessionIds(in: session)
XCTAssertEqual(sessionIds4, sessionIds3)
XCTAssertFalse(self.isSharedHistoryInLastSession(for: session))

// Final visibility change back to shared will still not reset sessions
room?.setHistoryVisibility(.worldReadable) { _ in
room?.sendTextMessages(messages: ["Hi", "Hello"]) { _ in

// Sessions are still identical as before
let sessionIds5 = self.storedSessionIds(in: session)
XCTAssertEqual(sessionIds5.outbound.count, 1)
XCTAssertEqual(sessionIds5.outbound, sessionIds4.outbound)
XCTAssertEqual(sessionIds5.inbound.count, 1)
XCTAssertEqual(sessionIds5.inbound, sessionIds1.outbound)
XCTAssertFalse(self.isSharedHistoryInLastSession(for: session))

session?.close()
expectation?.fulfill()
}
}
}
}
}
}
}
}
}
}
6 changes: 5 additions & 1 deletion MatrixSDKTests/MXCryptoTests.m
Expand Up @@ -3235,7 +3235,11 @@ - (void)testIsRoomSharingHistory
@[kMXRoomHistoryVisibilityWorldReadable, @(YES)]
];

// Visibility is set to shared by default
// Visibility is set to not shared by default
XCTAssertFalse([session.crypto isRoomSharingHistory:roomId]);

// But can be enabled with a build flag
MXSDKOptions.sharedInstance.enableRoomSharedHistoryOnInvite = YES;
XCTAssertTrue([session.crypto isRoomSharingHistory:roomId]);

MXRoom *room = [session roomWithRoomId:roomId];
Expand Down

0 comments on commit 03a153e

Please sign in to comment.