Added more unit tests for the OpenGroupManager

Added the ability to mock the identity manager (ie. return a mock key for the users key)
Swapped the Test Nonce16 & Nonce24 classes for Mock ones
Fixed an issue where the ContactUtilities call from the OpenGroupManager wasn't injecting the dependencies
Added unit tests for the handleDirectMessages and isUserModetatorOrAdmin methods
pull/592/head
Morgan Pretty 3 years ago
parent 37f4d2ecca
commit ff480a37d5

@ -782,6 +782,10 @@
FD078E5227E1760A000769AF /* OGMDependencyExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD078E5127E1760A000769AF /* OGMDependencyExtensions.swift */; };
FD078E5427E197CA000769AF /* OpenGroupManagerSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC2909D27D85751005DAE71 /* OpenGroupManagerSpec.swift */; };
FD078E5827E1B831000769AF /* TestIncomingMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD078E5727E1B831000769AF /* TestIncomingMessage.swift */; };
FD078E5A27E29F09000769AF /* MockNonce16Generator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD078E5927E29F09000769AF /* MockNonce16Generator.swift */; };
FD078E5C27E29F78000769AF /* MockNonce24Generator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD078E5B27E29F78000769AF /* MockNonce24Generator.swift */; };
FD078E5E27E2B9C2000769AF /* IdentityManagerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD078E5D27E2B9C2000769AF /* IdentityManagerProtocol.swift */; };
FD078E6027E2BB36000769AF /* MockIdentityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD078E5F27E2BB36000769AF /* MockIdentityManager.swift */; };
FD0BA51B27CD88EC00CC6805 /* BlindedIdMapping.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD0BA51A27CD88EC00CC6805 /* BlindedIdMapping.swift */; };
FD0BA51D27CDC34600CC6805 /* SOGSV4Migration.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD0BA51C27CDC34600CC6805 /* SOGSV4Migration.swift */; };
FD5D200F27AA2B6000FEA984 /* MessageRequestResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD5D200E27AA2B6000FEA984 /* MessageRequestResponse.swift */; };
@ -828,7 +832,7 @@
FDC2909827D7129B005DAE71 /* PersonalizationSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC2909727D7129B005DAE71 /* PersonalizationSpec.swift */; };
FDC2909A27D71376005DAE71 /* NonceGeneratorSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC2909927D71376005DAE71 /* NonceGeneratorSpec.swift */; };
FDC2909C27D713D2005DAE71 /* SodiumProtocolsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC2909B27D713D2005DAE71 /* SodiumProtocolsSpec.swift */; };
FDC290A027D85826005DAE71 /* TestThread.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC2909F27D85826005DAE71 /* TestThread.swift */; };
FDC290A027D85826005DAE71 /* TestContactThread.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC2909F27D85826005DAE71 /* TestContactThread.swift */; };
FDC290A227D85890005DAE71 /* TestInteraction.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC290A127D85890005DAE71 /* TestInteraction.swift */; };
FDC290A627D860CE005DAE71 /* Mock.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC290A527D860CE005DAE71 /* Mock.swift */; };
FDC290A827D9B46D005DAE71 /* NimbleExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC290A727D9B46D005DAE71 /* NimbleExtensions.swift */; };
@ -1928,6 +1932,10 @@
FD078E4E27E175F1000769AF /* DependencyExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DependencyExtensions.swift; sourceTree = "<group>"; };
FD078E5127E1760A000769AF /* OGMDependencyExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OGMDependencyExtensions.swift; sourceTree = "<group>"; };
FD078E5727E1B831000769AF /* TestIncomingMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestIncomingMessage.swift; sourceTree = "<group>"; };
FD078E5927E29F09000769AF /* MockNonce16Generator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockNonce16Generator.swift; sourceTree = "<group>"; };
FD078E5B27E29F78000769AF /* MockNonce24Generator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockNonce24Generator.swift; sourceTree = "<group>"; };
FD078E5D27E2B9C2000769AF /* IdentityManagerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentityManagerProtocol.swift; sourceTree = "<group>"; };
FD078E5F27E2BB36000769AF /* MockIdentityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockIdentityManager.swift; sourceTree = "<group>"; };
FD0BA51A27CD88EC00CC6805 /* BlindedIdMapping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlindedIdMapping.swift; sourceTree = "<group>"; };
FD0BA51C27CDC34600CC6805 /* SOGSV4Migration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SOGSV4Migration.swift; sourceTree = "<group>"; };
FD5D200E27AA2B6000FEA984 /* MessageRequestResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageRequestResponse.swift; sourceTree = "<group>"; };
@ -1977,7 +1985,7 @@
FDC2909927D71376005DAE71 /* NonceGeneratorSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonceGeneratorSpec.swift; sourceTree = "<group>"; };
FDC2909B27D713D2005DAE71 /* SodiumProtocolsSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SodiumProtocolsSpec.swift; sourceTree = "<group>"; };
FDC2909D27D85751005DAE71 /* OpenGroupManagerSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenGroupManagerSpec.swift; sourceTree = "<group>"; };
FDC2909F27D85826005DAE71 /* TestThread.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestThread.swift; sourceTree = "<group>"; };
FDC2909F27D85826005DAE71 /* TestContactThread.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestContactThread.swift; sourceTree = "<group>"; };
FDC290A127D85890005DAE71 /* TestInteraction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestInteraction.swift; sourceTree = "<group>"; };
FDC290A527D860CE005DAE71 /* Mock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mock.swift; sourceTree = "<group>"; };
FDC290A727D9B46D005DAE71 /* NimbleExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NimbleExtensions.swift; sourceTree = "<group>"; };
@ -3573,6 +3581,7 @@
C32C5BB9256DC7C4003C73A2 /* To Do */,
C3BBE0752554CDA60050F1E3 /* Configuration.swift */,
C3BBE07F2554CDD70050F1E3 /* Storage.swift */,
FD078E5D27E2B9C2000769AF /* IdentityManagerProtocol.swift */,
FDC4384D27B47FD600C60D73 /* Common Networking */,
B8B3201F258B1A540020074B /* Contacts */,
C32C5BCB256DC818003C73A2 /* Database */,
@ -4049,18 +4058,21 @@
isa = PBXGroup;
children = (
FDC438BC27BB2AB400C60D73 /* Mockable.swift */,
FD078E5F27E2BB36000769AF /* MockIdentityManager.swift */,
FDC4389C27BA01F000C60D73 /* MockStorage.swift */,
FD859EF327C2F49200510D0C /* MockSodium.swift */,
FD859EF527C2F52C00510D0C /* MockSign.swift */,
FD859EF727C2F58900510D0C /* MockAeadXChaCha20Poly1305Ietf.swift */,
FD859EF927C2F5C500510D0C /* MockGenericHash.swift */,
FD859EFB27C2F60700510D0C /* MockEd25519.swift */,
FD078E5927E29F09000769AF /* MockNonce16Generator.swift */,
FD078E5B27E29F78000769AF /* MockNonce24Generator.swift */,
FD83B9D127D59495005E1583 /* MockUserDefaults.swift */,
FD078E4C27E17156000769AF /* MockOGMCache.swift */,
FDC290B227DFF9F5005DAE71 /* TestOnionRequestAPI.swift */,
FDC290AE27DFEE97005DAE71 /* TestTransaction.swift */,
FDC2909F27D85826005DAE71 /* TestThread.swift */,
FDC290B627E00FDB005DAE71 /* TestGroupThread.swift */,
FDC2909F27D85826005DAE71 /* TestContactThread.swift */,
FDC290A127D85890005DAE71 /* TestInteraction.swift */,
FD078E5727E1B831000769AF /* TestIncomingMessage.swift */,
FDC290AB27DB0B1C005DAE71 /* MockedExtensions.swift */,
@ -5350,6 +5362,7 @@
C32C59C3256DB41F003C73A2 /* TSGroupModel.m in Sources */,
B8856ECE256F1E58001CE70E /* OWSPreferences.m in Sources */,
C3C2A7842553AAF300C340D1 /* SNProto.swift in Sources */,
FD078E5E27E2B9C2000769AF /* IdentityManagerProtocol.swift in Sources */,
C32C5DC0256DD743003C73A2 /* Poller.swift in Sources */,
C3C2A7682553A3D900C340D1 /* VisibleMessage+Contact.swift in Sources */,
C3A3A171256E1D25004D228D /* SSKReachabilityManager.swift in Sources */,
@ -5666,6 +5679,7 @@
FDC290AF27DFEE97005DAE71 /* TestTransaction.swift in Sources */,
FDC290B327DFF9F5005DAE71 /* TestOnionRequestAPI.swift in Sources */,
FDC2909127D709CA005DAE71 /* SOGSMessageSpec.swift in Sources */,
FD078E6027E2BB36000769AF /* MockIdentityManager.swift in Sources */,
FDC2909627D71252005DAE71 /* SOGSErrorSpec.swift in Sources */,
FDC2908727D7047F005DAE71 /* RoomSpec.swift in Sources */,
FD078E4F27E175F1000769AF /* DependencyExtensions.swift in Sources */,
@ -5674,7 +5688,7 @@
FD83B9C727CF3F10005E1583 /* CapabilitiesSpec.swift in Sources */,
FDC2909A27D71376005DAE71 /* NonceGeneratorSpec.swift in Sources */,
FD078E4827E02561000769AF /* CommonMockedExtensions.swift in Sources */,
FDC290A027D85826005DAE71 /* TestThread.swift in Sources */,
FDC290A027D85826005DAE71 /* TestContactThread.swift in Sources */,
FD859EF827C2F58900510D0C /* MockAeadXChaCha20Poly1305Ietf.swift in Sources */,
FDC2909827D7129B005DAE71 /* PersonalizationSpec.swift in Sources */,
FD859EF427C2F49200510D0C /* MockSodium.swift in Sources */,
@ -5692,11 +5706,13 @@
FDC438BD27BB2AB400C60D73 /* Mockable.swift in Sources */,
FD859EF627C2F52C00510D0C /* MockSign.swift in Sources */,
FDC2908927D70656005DAE71 /* RoomPollInfoSpec.swift in Sources */,
FD078E5A27E29F09000769AF /* MockNonce16Generator.swift in Sources */,
FDC2908D27D70905005DAE71 /* UpdateMessageRequestSpec.swift in Sources */,
FDC290A227D85890005DAE71 /* TestInteraction.swift in Sources */,
FD078E5427E197CA000769AF /* OpenGroupManagerSpec.swift in Sources */,
FDC4389D27BA01F000C60D73 /* MockStorage.swift in Sources */,
FD83B9D227D59495005E1583 /* MockUserDefaults.swift in Sources */,
FD078E5C27E29F78000769AF /* MockNonce24Generator.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

@ -3,7 +3,7 @@
import Foundation
@objc(SNBlindedIdMapping)
public class BlindedIdMapping: NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
public final class BlindedIdMapping: NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
@objc public let blindedId: String
@objc public let sessionId: String
@objc public let serverPublicKey: String

@ -0,0 +1,10 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import Curve25519Kit
public protocol IdentityManagerProtocol {
func identityKeyPair() -> ECKeyPair?
}
extension OWSIdentityManager: IdentityManagerProtocol {}

@ -211,7 +211,7 @@ public final class OpenGroupManager: NSObject {
) {
// Create the open group model and get or create the thread
let groupId: Data = LKGroupUtilities.getEncodedOpenGroupIDAsData("\(server).\(roomToken)")
let userPublicKey: String = getUserHexEncodedPublicKey()
let userPublicKey: String = getUserHexEncodedPublicKey(using: dependencies)
let initialModel: TSGroupModel = TSGroupModel(
title: (pollInfo.details?.name ?? ""),
memberIds: [ userPublicKey ],
@ -374,8 +374,6 @@ public final class OpenGroupManager: NSObject {
internal static func handleDirectMessages(
_ messages: [OpenGroupAPI.DirectMessage],
// We could infer where the messages come from based on their sender/recipient values but being since they
// are different endpoints being explicit here reduces the chance a future change will break things
fromOutbox: Bool,
on server: String,
isBackgroundPoll: Bool,
@ -393,7 +391,7 @@ public final class OpenGroupManager: NSObject {
// that quote older messages can't find those older messages
let sortedMessages: [OpenGroupAPI.DirectMessage] = messages
.sorted { lhs, rhs in lhs.id < rhs.id }
let latestMessageId: Int64 = (sortedMessages.last?.id ?? 0)
let latestMessageId: Int64 = sortedMessages[sortedMessages.count - 1].id
var mappingCache: [String: BlindedIdMapping] = [:] // Only want this cache to exist for the current loop
// Update the 'latestMessageId' value
@ -442,7 +440,7 @@ public final class OpenGroupManager: NSObject {
if let result: BlindedIdMapping = mappingCache[message.recipient] {
mapping = result
}
else if let result: BlindedIdMapping = ContactUtilities.mapping(for: message.recipient, serverPublicKey: serverPublicKey, using: transaction) {
else if let result: BlindedIdMapping = ContactUtilities.mapping(for: message.recipient, serverPublicKey: serverPublicKey, using: transaction, dependencies: dependencies) {
mapping = result
}
else {
@ -497,7 +495,7 @@ public final class OpenGroupManager: NSObject {
// case with only minor efficiency losses
switch sessionId.prefix {
case .standard:
guard publicKey == getUserHexEncodedPublicKey() else { return false }
guard publicKey == getUserHexEncodedPublicKey(using: dependencies) else { return false }
fallthrough
case .unblinded:
@ -523,7 +521,7 @@ public final class OpenGroupManager: NSObject {
// users 'standard', 'unblinded' or 'blinded' keys and as such we should check if any
// of them exist in the `modsAndAminKeys` Set
let possibleKeys: Set<String> = Set([
getUserHexEncodedPublicKey(),
getUserHexEncodedPublicKey(using: dependencies),
SessionId(.unblinded, publicKey: userEdKeyPair.publicKey).hexString,
SessionId(.blinded, publicKey: blindedKeyPair.publicKey).hexString
])
@ -698,6 +696,7 @@ extension OpenGroupManager {
public init(
cache: Atomic<OGMCacheType>? = nil,
onionApi: OnionRequestAPIType.Type? = nil,
identityManager: IdentityManagerProtocol? = nil,
storage: SessionMessagingKitStorageProtocol? = nil,
sodium: SodiumType? = nil,
aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType? = nil,
@ -713,6 +712,7 @@ extension OpenGroupManager {
super.init(
onionApi: onionApi,
identityManager: identityManager,
storage: storage,
sodium: sodium,
aeadXChaCha20Poly1305Ietf: aeadXChaCha20Poly1305Ietf,

@ -28,6 +28,8 @@ extension MessageReceiver {
}
internal static func decryptWithSessionBlindingProtocol(data: Data, isOutgoing: Bool, otherBlindedPublicKey: String, with openGroupPublicKey: String, userEd25519KeyPair: Box.KeyPair, using dependencies: Dependencies = Dependencies()) throws -> (plaintext: Data, senderX25519PublicKey: String) {
/// Ensure the data is at least long enough to have the required components
guard data.count > dependencies.nonceGenerator24.NonceBytes + 2 else { throw Error.decryptionFailed }
guard let blindedKeyPair = dependencies.sodium.blindedKeyPair(serverPublicKey: openGroupPublicKey, edKeyPair: userEd25519KeyPair, genericHash: dependencies.genericHash) else {
throw Error.decryptionFailed
}

@ -12,6 +12,12 @@ public class Dependencies {
set { _onionApi = newValue }
}
internal var _identityManager: IdentityManagerProtocol?
public var identityManager: IdentityManagerProtocol {
get { Dependencies.getValueSettingIfNull(&_identityManager) { OWSIdentityManager.shared() } }
set { _identityManager = newValue }
}
internal var _storage: SessionMessagingKitStorageProtocol?
public var storage: SessionMessagingKitStorageProtocol {
get { Dependencies.getValueSettingIfNull(&_storage) { SNMessagingKitConfiguration.shared.storage } }
@ -76,6 +82,7 @@ public class Dependencies {
public init(
onionApi: OnionRequestAPIType.Type? = nil,
identityManager: IdentityManagerProtocol? = nil,
storage: SessionMessagingKitStorageProtocol? = nil,
sodium: SodiumType? = nil,
aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType? = nil,
@ -88,6 +95,7 @@ public class Dependencies {
date: Date? = nil
) {
_onionApi = onionApi
_identityManager = identityManager
_storage = storage
_sodium = sodium
_aeadXChaCha20Poly1305Ietf = aeadXChaCha20Poly1305Ietf

@ -1,7 +1,9 @@
import Foundation
public func getUserHexEncodedPublicKey() -> String {
if let keyPair = OWSIdentityManager.shared().identityKeyPair() { // Can be nil under some circumstances
public func getUserHexEncodedPublicKey(using dependencies: Dependencies = Dependencies()) -> String {
if let keyPair = dependencies.identityManager.identityKeyPair() { // Can be nil under some circumstances
return keyPair.hexEncodedPublicKey
}
return ""
}

@ -10,18 +10,6 @@ import Nimble
@testable import SessionMessagingKit
class OpenGroupAPISpec: QuickSpec {
struct TestNonce16Generator: NonceGenerator16ByteType {
var NonceBytes: Int = 16
func nonce() -> Array<UInt8> { return Data(base64Encoded: "pK6YRtQApl4NhECGizF0Cg==")!.bytes }
}
struct TestNonce24Generator: NonceGenerator24ByteType {
var NonceBytes: Int = 24
func nonce() -> Array<UInt8> { return Data(base64Encoded: "pbTUizreT0sqJ2R2LloseQDyVL2RYztD")!.bytes }
}
// MARK: - Spec
override func spec() {
@ -31,6 +19,8 @@ class OpenGroupAPISpec: QuickSpec {
var mockSign: MockSign!
var mockGenericHash: MockGenericHash!
var mockEd25519: MockEd25519!
var mockNonce16Generator: MockNonce16Generator!
var mockNonce24Generator: MockNonce24Generator!
var dependencies: Dependencies!
var response: (OnionRequestResponseInfoType, Codable)? = nil
@ -46,6 +36,8 @@ class OpenGroupAPISpec: QuickSpec {
mockAeadXChaCha20Poly1305Ietf = MockAeadXChaCha20Poly1305Ietf()
mockSign = MockSign()
mockGenericHash = MockGenericHash()
mockNonce16Generator = MockNonce16Generator()
mockNonce24Generator = MockNonce24Generator()
mockEd25519 = MockEd25519()
dependencies = Dependencies(
onionApi: TestOnionRequestAPI.self,
@ -55,8 +47,8 @@ class OpenGroupAPISpec: QuickSpec {
sign: mockSign,
genericHash: mockGenericHash,
ed25519: mockEd25519,
nonceGenerator16: TestNonce16Generator(),
nonceGenerator24: TestNonce24Generator(),
nonceGenerator16: mockNonce16Generator,
nonceGenerator24: mockNonce24Generator,
date: Date(timeIntervalSince1970: 1234567890)
)
@ -137,6 +129,13 @@ class OpenGroupAPISpec: QuickSpec {
.thenReturn("TestSogsSignature".bytes)
mockSign.when { $0.signature(message: anyArray(), secretKey: anyArray()) }.thenReturn("TestSignature".bytes)
mockEd25519.when { try $0.sign(data: anyArray(), keyPair: any()) }.thenReturn("TestStandardSignature".bytes)
mockNonce16Generator
.when { $0.nonce() }
.thenReturn(Data(base64Encoded: "pK6YRtQApl4NhECGizF0Cg==")!.bytes)
mockNonce24Generator
.when { $0.nonce() }
.thenReturn(Data(base64Encoded: "pbTUizreT0sqJ2R2LloseQDyVL2RYztD")!.bytes)
}
afterEach {

@ -8,6 +8,7 @@ import SessionSnodeKit
extension Dependencies {
public func with(
onionApi: OnionRequestAPIType.Type? = nil,
identityManager: IdentityManagerProtocol? = nil,
storage: SessionMessagingKitStorageProtocol? = nil,
sodium: SodiumType? = nil,
aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType? = nil,
@ -21,6 +22,7 @@ extension Dependencies {
) -> Dependencies {
return Dependencies(
onionApi: (onionApi ?? self._onionApi),
identityManager: (identityManager ?? self._identityManager),
storage: (storage ?? self._storage),
sodium: (sodium ?? self._sodium),
aeadXChaCha20Poly1305Ietf: (aeadXChaCha20Poly1305Ietf ?? self._aeadXChaCha20Poly1305Ietf),

@ -0,0 +1,9 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
@testable import SessionMessagingKit
class MockIdentityManager: Mock<IdentityManagerProtocol>, IdentityManagerProtocol {
func identityKeyPair() -> ECKeyPair? { return accept() as? ECKeyPair }
}

@ -0,0 +1,11 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
@testable import SessionMessagingKit
class MockNonce16Generator: Mock<NonceGenerator16ByteType>, NonceGenerator16ByteType {
var NonceBytes: Int = 16
func nonce() -> Array<UInt8> { return accept() as! [UInt8] }
}

@ -0,0 +1,11 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
@testable import SessionMessagingKit
class MockNonce24Generator: Mock<NonceGenerator24ByteType>, NonceGenerator24ByteType {
var NonceBytes: Int = 24
func nonce() -> Array<UInt8> { return accept() as! [UInt8] }
}

@ -25,3 +25,11 @@ extension OpenGroupAPI.Server: Mocked {
extension VisibleMessage: Mocked {
static var mockValue: VisibleMessage = VisibleMessage()
}
extension BlindedIdMapping: Mocked {
static var mockValue: BlindedIdMapping = BlindedIdMapping(
blindedId: any(),
sessionId: any(),
serverPublicKey: any()
)
}

@ -9,6 +9,7 @@ extension OpenGroupManager.OGMDependencies {
public func with(
cache: Atomic<OGMCacheType>? = nil,
onionApi: OnionRequestAPIType.Type? = nil,
identityManager: IdentityManagerProtocol? = nil,
storage: SessionMessagingKitStorageProtocol? = nil,
sodium: SodiumType? = nil,
aeadXChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType? = nil,
@ -23,6 +24,7 @@ extension OpenGroupManager.OGMDependencies {
return OpenGroupManager.OGMDependencies(
cache: (cache ?? self._mutableCache),
onionApi: (onionApi ?? self._onionApi),
identityManager: (identityManager ?? self._identityManager),
storage: (storage ?? self._storage),
sodium: (sodium ?? self._sodium),
aeadXChaCha20Poly1305Ietf: (aeadXChaCha20Poly1305Ietf ?? self._aeadXChaCha20Poly1305Ietf),

@ -0,0 +1,51 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import SessionMessagingKit
// FIXME: Turn this into a protocol to make mocking possible
class TestContactThread: TSContactThread, Mockable {
// MARK: - Mockable
enum DataKey: Hashable {
case uniqueId
case interactions
}
typealias Key = DataKey
var mockData: [DataKey: Any] = [:]
var numSaveCalls: Int = 0
var didCallRemoveAllThreadInteractions: Bool = false
var didCallRemove: Bool = false
// MARK: - TSContactThread
override var uniqueId: String? {
get { (mockData[.uniqueId] as? String) }
set {}
}
override func enumerateInteractions(_ block: @escaping (TSInteraction) -> Void) {
((mockData[.interactions] as? [TSInteraction]) ?? []).forEach(block)
}
override func enumerateInteractions(with transaction: YapDatabaseReadTransaction, using block: @escaping (TSInteraction, UnsafeMutablePointer<ObjCBool>) -> Void) {
var stop: ObjCBool = false
for interaction in ((mockData[.interactions] as? [TSInteraction]) ?? []) {
block(interaction, &stop)
if stop.boolValue { break }
}
}
override func removeAllThreadInteractions(with transaction: YapDatabaseReadWriteTransaction) {
didCallRemoveAllThreadInteractions = true
}
override func remove(with transaction: YapDatabaseReadWriteTransaction) {
didCallRemove = true
}
override func save(with transaction: YapDatabaseReadWriteTransaction) { numSaveCalls += 1 }
}

@ -1,26 +0,0 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import SessionMessagingKit
// FIXME: Turn this into a protocol to make mocking possible
class TestThread: TSThread, Mockable {
// MARK: - Mockable
enum DataKey: Hashable {
case interactions
}
typealias Key = DataKey
var mockData: [DataKey: Any] = [:]
var didCallSave: Bool = false
// MARK: - TSThread
override func enumerateInteractions(_ block: @escaping (TSInteraction) -> Void) {
((mockData[.interactions] as? [TSInteraction]) ?? []).forEach(block)
}
override func save(with transaction: YapDatabaseReadWriteTransaction) { didCallSave = true }
}
Loading…
Cancel
Save