session-ios/Loki/LokiTestUtilities.swift

55 lines
2.4 KiB
Swift
Raw Normal View History

2020-05-04 08:00:23 +02:00
import Foundation
import SignalServiceKit
import Curve25519Kit
2020-05-04 08:17:18 +02:00
enum LokiTestUtilities {
2020-05-04 08:00:23 +02:00
2020-05-04 08:17:18 +02:00
public static func setUpMockEnvironment() {
// Activate the mock Signal environment
2020-05-04 08:00:23 +02:00
ClearCurrentAppContextForTests()
SetCurrentAppContext(TestAppContext())
MockSSKEnvironment.activate()
2020-05-04 08:17:18 +02:00
// Register a mock user
2020-05-04 08:00:23 +02:00
let identityManager = OWSIdentityManager.shared()
let seed = Randomness.generateRandomBytes(16)!
let keyPair = Curve25519.generateKeyPair(fromSeed: seed + seed)
let databaseConnection = identityManager.value(forKey: "dbConnection") as! YapDatabaseConnection
databaseConnection.setObject(keyPair, forKey: OWSPrimaryStorageIdentityKeyStoreIdentityKey, inCollection: OWSPrimaryStorageIdentityKeyStoreCollection)
TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = keyPair.hexEncodedPublicKey
TSAccountManager.sharedInstance().didRegister()
}
2020-05-04 08:17:18 +02:00
public static func generateKeyPair() -> ECKeyPair {
2020-05-04 08:00:23 +02:00
return Curve25519.generateKeyPair()
}
public static func getCurrentUserHexEncodedPublicKey() -> String {
return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
}
2020-05-04 08:17:18 +02:00
public static func generateHexEncodedPublicKey() -> String {
2020-05-04 08:00:23 +02:00
return generateKeyPair().hexEncodedPublicKey
}
public static func getDevice(for hexEncodedPublicKey: String) -> DeviceLink.Device? {
guard let signature = Data.getSecureRandomData(ofSize: 64) else { return nil }
return DeviceLink.Device(hexEncodedPublicKey: hexEncodedPublicKey, signature: signature)
}
public static func createContactThread(for hexEncodedPublicKey: String) -> TSContactThread {
return TSContactThread.getOrCreateThread(contactId: hexEncodedPublicKey)
}
public static func createGroupThread(groupType: GroupType) -> TSGroupThread? {
let hexEncodedGroupID = Randomness.generateRandomBytes(kGroupIdLength)!.toHexString()
let groupID: Data
switch groupType {
case .closedGroup: groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(hexEncodedGroupID)
case .openGroup: groupID = LKGroupUtilities.getEncodedOpenGroupIDAsData(hexEncodedGroupID)
case .rssFeed: groupID = LKGroupUtilities.getEncodedRSSFeedIDAsData(hexEncodedGroupID)
default: return nil
}
return TSGroupThread.getOrCreateThread(withGroupId: groupID, groupType: groupType)
}
}