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()
|
|
|
|
}
|
|
|
|
|
2020-05-05 06:01:07 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|