mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			55 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			55 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Swift
		
	
| 
								 
											6 years ago
										 
									 | 
							
								import Foundation
							 | 
						||
| 
								 | 
							
								import SignalServiceKit
							 | 
						||
| 
								 | 
							
								import Curve25519Kit
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								enum LokiTestUtilities {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public static func setUpMockEnvironment() {
							 | 
						||
| 
								 | 
							
								        // Activate the mock Signal environment
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        ClearCurrentAppContextForTests()
							 | 
						||
| 
								 | 
							
								        SetCurrentAppContext(TestAppContext())
							 | 
						||
| 
								 | 
							
								        MockSSKEnvironment.activate()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        // Register a mock user
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        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()
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public static func generateKeyPair() -> ECKeyPair {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        return Curve25519.generateKeyPair()
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public static func getCurrentUserHexEncodedPublicKey() -> String {
							 | 
						||
| 
								 | 
							
								        return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public static func generateHexEncodedPublicKey() -> String {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        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)
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |