Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.
Improve GrandSlam testability, remove KeychainAccess dep, and other refactoring
kabiroberai20c664665877
diff --git a/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift b/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swiftindex a16acb7..34b299c 100644--- a/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift+++ b/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift@@ -70,7 +70,7 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera) {createCertificate { result inguard let value = result.get(withErrorHandler: completion) else { return }- SigningKeychain[self.context.team.id] = value+ self.context.signingInfoManager[self.context.team.id] = valuecompletion(.success(value))}}@@ -101,7 +101,7 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOperareturn self.createAndSaveCertificate(completion: completion)}- guard let signingInfo = SigningKeychain[self.context.team.id],+ guard let signingInfo = self.context.signingInfoManager[self.context.team.id],let serialNumber = try? signingInfo.certificate.serialNumber(),certificate.serialNumber.rawValue == serialNumber,certificate.expiry > Date()diff --git a/Supersign/Installation/AppInstaller.swift b/Supersign/Installation/AppInstaller.swiftindex f0fcca3..e104798 100644--- a/Supersign/Installation/AppInstaller.swift+++ b/Supersign/Installation/AppInstaller.swift@@ -61,10 +61,10 @@ public class AppInstaller {switch self {case .connecting(let progress):return progress- case .installing(_, let progress):- return progress ?? 0case .uploading(let progress):return progress+ case .installing(_, let progress):+ return progress ?? 0}}}@@ -98,11 +98,12 @@ public class AppInstaller {let installer = try IPAInstaller(connection: connection)progress(.connecting(6/6))- let location = try uploader.upload(ipa: ipa, withBundleID: bundleID) { currentProgress in+ let uploaded = try uploader.upload(ipa: ipa, withBundleID: bundleID) { currentProgress inprogress(.uploading(currentProgress))}+ defer { uploaded.delete() }- try installer.install(deviceLocation: location, bundleID: bundleID) { currentProgress in+ try installer.install(uploaded: uploaded, bundleID: bundleID) { currentProgress inprogress(.installing(currentProgress.details, currentProgress.progress))}}diff --git a/Supersign/Installation/Connection.swift b/Supersign/Installation/Connection.swiftindex dd2d595..9d56fcc 100644--- a/Supersign/Installation/Connection.swift+++ b/Supersign/Installation/Connection.swift@@ -9,19 +9,19 @@import Foundationimport SwiftyMobileDevice-class Connection {+public class Connection {private static let label = "supersign"- private(set) var isClosed = false+ public private(set) var isClosed = falseprivate let usbmuxHandler: USBMuxHandler- let device: Device- let client: LockdownClient+ public let device: Device+ public let client: LockdownClientprivate let heartbeatHandler: HeartbeatHandler- init(udid: String, pairingKeys: URL, progress: (Double) -> Void) throws {- progress(0/0)+ public init(udid: String, pairingKeys: URL, progress: (Double) -> Void) throws {+ progress(0/4)usbmuxHandler = USBMuxHandler(udid: udid, pairingKeys: pairingKeys)progress(1/4)@@ -38,7 +38,7 @@ class Connection {deinit { close() }- func close() {+ public func close() {guard !isClosed else { return }isClosed = true@@ -46,7 +46,7 @@ class Connection {usbmuxHandler.stop()}- func startClient<T: Service>(_ type: T.Type = T.self, sendEscrowBag: Bool = false) throws -> T {+ public func startClient<T: Service>(_ type: T.Type = T.self, sendEscrowBag: Bool = false) throws -> T {try .init(device: device, service: .init(client: client, sendEscrowBag: sendEscrowBag))}diff --git a/Supersign/Installation/HeartbeatHandler.swift b/Supersign/Installation/HeartbeatHandler.swiftindex e93b098..795a05d 100644--- a/Supersign/Installation/HeartbeatHandler.swift+++ b/Supersign/Installation/HeartbeatHandler.swift@@ -9,7 +9,7 @@import Foundationimport SwiftyMobileDevice-class HeartbeatHandler {+public class HeartbeatHandler {private struct Error: Swift.Error {}@@ -39,32 +39,41 @@ class HeartbeatHandler {}}+ // the duration to wait after a failed heartbeat, before restartingprivate static let restartInterval: TimeInterval = 1-// private static let requestCommand = "Marco"-// private static let responseCommand = "Polo"+ // the timeout when receiving the first heartbeat packet+ private static let initialTimeout: TimeInterval = 30+ // the timeout when receiving subsequent heartbeat packets+ private static let repeatedTimeout: TimeInterval = 5// each handler should have its own heartbeat queueprivate let heartbeatQueue = DispatchQueue(label: "com.kabiroberai.Supersign.heartbeat-queue")- private(set) var isStopped = false+ public private(set) var isStopped = false- let device: Device- let client: LockdownClient- init(device: Device, client: LockdownClient) {+ public let device: Device+ public let client: LockdownClient+ public init(device: Device, client: LockdownClient) {self.device = deviceself.client = clientstart()}- private func beat(client: HeartbeatClient, idx: Int) throws {+ // no need for a custom deinit because the `guard let self = self` in the loop stops it+ // when the handler deinits++ private func beat(client: HeartbeatClient, iteration: Int) throws {// allow a 30 second timeout for the first heartbeat- let received = try client.receive(ReceivedPacket.self, timeout: idx == 0 ? 30 : 5)+ let received = try client.receive(+ ReceivedPacket.self,+ timeout: iteration == 0 ? Self.initialTimeout : Self.repeatedTimeout+ )guard received.command == .marco else { throw Error() }try client.send(SentPacket(command: .polo))- sleep(.init(received.interval))+ Thread.sleep(forTimeInterval: .init(received.interval))}private func start() {@@ -74,40 +83,30 @@ class HeartbeatHandler {} catch {// we sleep instead of asyncAfter to allow the initial startHeartbeat// call to block until a connection is established- let timeInterval = Self.restartInterval- let sec = timeInterval.rounded(.down)- let nsec = (timeInterval - sec) * 1_000_000_000- var time = timespec(- tv_sec: .init(sec),- tv_nsec: .init(nsec)- )- nanosleep(&time, nil)+ Thread.sleep(forTimeInterval: Self.restartInterval)start()return}self.heartbeatQueue.async { [weak self] in- var idx = 0- while true {+ for iteration in 0... {// only retains `self` for this iteration. This way, if the reference to- // the handler is dropped,+ // the handler is dropped, the heartbeat stopsguard let self = self, !self.isStopped else { break }do {- try self.beat(client: heartbeatClient, idx: idx)- idx += 1+ try self.beat(client: heartbeatClient, iteration: iteration)} catch {- // dispatching async should prevent stack overflows and allow the existing- // heartbeatClient to deinit- self.heartbeatQueue.asyncAfter(deadline: .now() + Self.restartInterval) {+ // dispatching async should prevent stack overflows.+ // We return to break out of the current loop.+ return self.heartbeatQueue.asyncAfter(deadline: .now() + Self.restartInterval) {self.start()}- break}}}}- func stop() {+ public func stop() {isStopped = true}diff --git a/Supersign/Installation/IPAInstaller.swift b/Supersign/Installation/IPAInstaller.swiftindex aa3b5f3..591aac3 100644--- a/Supersign/Installation/IPAInstaller.swift+++ b/Supersign/Installation/IPAInstaller.swift@@ -9,15 +9,15 @@import Foundationimport SwiftyMobileDevice-class IPAInstaller {+public class IPAInstaller {private let client: InstallationProxyClient- init(connection: Connection) throws {+ public init(connection: Connection) throws {self.client = try connection.startClient()}- func install(- deviceLocation: URL,+ public func install(+ uploaded: IPAUploader.UploadedIPA,bundleID: String,progress: @escaping (InstallationProxyClient.InstallProgress) -> Void) throws {@@ -25,7 +25,7 @@ class IPAInstaller {var error: Error?let semaphore = DispatchSemaphore(value: 0)- client.install(package: deviceLocation, options: options, progress: progress) { result in+ client.install(package: uploaded.location, options: options, progress: progress) { result indefer { semaphore.signal() }if case let .failure(e) = result { error = e }}diff --git a/Supersign/Installation/IPAUploader.swift b/Supersign/Installation/IPAUploader.swiftindex 43d937d..04a47ed 100644--- a/Supersign/Installation/IPAUploader.swift+++ b/Supersign/Installation/IPAUploader.swift@@ -9,12 +9,29 @@import Foundationimport SwiftyMobileDevice-class IPAUploader {+public class IPAUploader {private static let packagePath = URL(fileURLWithPath: "PublicStaging")+ public class UploadedIPA {+ public let uploader: IPAUploader+ let location: URL+ fileprivate init(uploader: IPAUploader, location: URL) {+ self.uploader = uploader+ self.location = location+ }++ public private(set) var isDeleted: Bool = false++ deinit { delete() }+ public func delete() {+ guard !isDeleted else { return }+ try? uploader.client.removeItem(at: location)+ }+ }+private let client: AFCClient- init(connection: Connection) throws {+ public init(connection: Connection) throws {self.client = try connection.startClient()}@@ -39,7 +56,7 @@ class IPAUploader {} while !buf.isEmpty}- func upload(ipa: URL, withBundleID bundleID: String, progress: (Double) -> Void) throws -> URL {+ public func upload(ipa: URL, withBundleID bundleID: String, progress: (Double) -> Void) throws -> UploadedIPA {if try !client.fileExists(at: Self.packagePath) {try client.createDirectory(at: Self.packagePath)}@@ -47,7 +64,7 @@ class IPAUploader {let dest = Self.packagePath.appendingPathComponent(bundleID)try upload(ipa, to: dest, progress: progress)- return dest+ return UploadedIPA(uploader: self, location: dest)}}diff --git a/Supersign/Signer/SigningContext.swift b/Supersign/Signer/SigningContext.swiftindex 7a12220..96d1bce 100644--- a/Supersign/Signer/SigningContext.swift+++ b/Supersign/Signer/SigningContext.swift@@ -12,22 +12,25 @@ public struct SigningContext {public let udid: Stringpublic let team: DeveloperServicesTeam- public let signerImpl: SignerImpl- public let platform: DeveloperServicesPlatformpublic let client: DeveloperServicesClient+ public let signingInfoManager: SigningInfoManager+ public let platform: DeveloperServicesPlatform+ public let signerImpl: SignerImplpublic init(udid: String,team: DeveloperServicesTeam,- signerImpl: SignerImpl? = nil,+ client: DeveloperServicesClient,+ signingInfoManager: SigningInfoManager,platform: DeveloperServicesPlatform = .current,- client: DeveloperServicesClient = .shared+ signerImpl: SignerImpl? = nil) throws {self.udid = udidself.team = team- self.signerImpl = try signerImpl ?? .first()- self.platform = platformself.client = client+ self.signingInfoManager = signingInfoManager+ self.platform = platform+ self.signerImpl = try signerImpl ?? .first()}}diff --git a/Supersign/SignerImpl/SignerImpl.swift b/Supersign/SignerImpl/SignerImpl.swiftindex cfbbccb..a2c0db4 100644--- a/Supersign/SignerImpl/SignerImpl.swift+++ b/Supersign/SignerImpl/SignerImpl.swift@@ -53,12 +53,12 @@ public struct SignerImpl {analyze = signer.analyze}- private static func all() -> AnySequence<SignerImpl> {- .init(CLinkedList(first: signer_list).lazy.map(SignerImpl.init))+ public static func all() -> AnyCollection<SignerImpl> {+ return AnyCollection(CLinkedList(first: signer_list).lazy.map(SignerImpl.init))}public static func first() throws -> SignerImpl {- try all().makeIterator().next().orThrow(Error.notFound)+ try all().first.orThrow(Error.notFound)}private func _sign(diff --git a/Supersign/Utilities/KeyValueStorage.swift b/Supersign/Utilities/KeyValueStorage.swiftnew file mode 100644index 0000000..c892c60--- /dev/null+++ b/Supersign/Utilities/KeyValueStorage.swift@@ -0,0 +1,112 @@+//+// KeyValueStorage.swift+// Supersign+//+// Created by Kabir Oberai on 08/04/20.+// Copyright © 2020 Kabir Oberai. All rights reserved.+//++import Foundation++public enum KeyValueStorageError: Error {+ case stringConversionFailure+}++public protocol KeyValueStorage {+ func data(forKey key: String) throws -> Data?+ func setData(_ data: Data?, forKey key: String) throws+ // default implementations provided+ func string(forKey key: String) throws -> String?+ func setString(_ string: String?, forKey key: String) throws+}++extension KeyValueStorage {+ public func string(forKey key: String) throws -> String? {+ try data(forKey: key).map {+ try String(data: $0, encoding: .utf8).orThrow(KeyValueStorageError.stringConversionFailure)+ }+ }++ public func setString(_ string: String?, forKey key: String) throws {+ let data = try string.map { try $0.data(using: .utf8).orThrow(KeyValueStorageError.stringConversionFailure) }+ try setData(data, forKey: key)+ }++ public subscript(dataForKey key: String) -> Data? {+ get { try? data(forKey: key) }+ nonmutating set { try? setData(newValue, forKey: key) }+ }++ public subscript(stringForKey key: String) -> String? {+ get { try? string(forKey: key) }+ nonmutating set { try? setString(newValue, forKey: key) }+ }+}++#if canImport(Security)+import Security++public struct KeychainKeyValueStorage: KeyValueStorage {++ public let service: String?+ public init(service: String? = nil) {+ self.service = service+ }++ private let lock = NSLock()++ private static func check(_ result: OSStatus) throws {+ if result != errSecSuccess {+ let info: [String: Any]?+ if #available(iOS 11.3, *), let message = SecCopyErrorMessageString(result, nil) {+ info = [NSLocalizedDescriptionKey: message as String]+ } else {+ info = nil+ }+ throw NSError(domain: NSOSStatusErrorDomain, code: .init(result), userInfo: info)+ }+ }++ private func makeQuery(forKey key: String, _ parameters: [CFString: Any]) -> CFDictionary {+ var query: [CFString: Any] = [+ kSecClass: kSecClassGenericPassword,+ kSecAttrAccount: key+ ]+ query[kSecAttrService] = service+ query.merge(parameters) { _, b in b }+ return query as CFDictionary+ }++ public func data(forKey key: String) throws -> Data? {+ lock.lock()+ defer { lock.unlock() }++ let query = makeQuery(forKey: key, [+ kSecReturnData: true+ ])++ var result: AnyObject?+ try Self.check(SecItemCopyMatching(query, &result))++ return result as? Data+ }++ public func setData(_ data: Data?, forKey key: String) throws {+ lock.lock()+ defer { lock.unlock() }++ // remove old before setting new value+ try Self.check(SecItemDelete(makeQuery(forKey: key, [:])))++ guard let data = data else { return }++ let query = makeQuery(forKey: key, [+ kSecValueData: data,+ kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlock+ ])++ try Self.check(SecItemAdd(query, nil))+ }++}+#endifdiff --git a/Supersign/Utilities/SigningInfo.swift b/Supersign/Utilities/SigningInfo.swiftnew file mode 100644index 0000000..f3b4313--- /dev/null+++ b/Supersign/Utilities/SigningInfo.swift@@ -0,0 +1,66 @@+//+// SigningInfo.swift+// Supersign+//+// Created by Kabir Oberai on 12/10/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++public struct SigningInfo {+ public let privateKey: PrivateKey+ public let certificate: Certificate+}++public protocol SigningInfoManager {+ func info(forTeamID teamID: DeveloperServicesTeam.ID) throws -> SigningInfo?+ func setInfo(_ info: SigningInfo?, forTeamID teamID: DeveloperServicesTeam.ID) throws+}++extension SigningInfoManager {+ subscript(teamID: DeveloperServicesTeam.ID) -> SigningInfo? {+ get {+ try? info(forTeamID: teamID)+ }+ nonmutating set {+ try? setInfo(newValue, forTeamID: teamID)+ }+ }+}++public struct KeyValueSigningInfoManager: SigningInfoManager {+ private enum KeyType: String {+ case certificate+ case privateKey+ }++ private func key(_ teamID: DeveloperServicesTeam.ID, _ keyType: KeyType) -> String {+ "\(teamID).\(keyType.rawValue)"+ }++ public let storage: KeyValueStorage+ public init(storage: KeyValueStorage) {+ self.storage = storage+ }++ public func info(forTeamID teamID: DeveloperServicesTeam.ID) throws -> SigningInfo? {+ guard let privKeyData = try storage.data(forKey: key(teamID, .privateKey)),+ let certData = try storage.data(forKey: key(teamID, .certificate))+ else { return nil }+ let cert = try Certificate(data: certData)+ let privKey = PrivateKey(data: privKeyData)+ return SigningInfo(privateKey: privKey, certificate: cert)+ }++ public func setInfo(_ info: SigningInfo?, forTeamID teamID: DeveloperServicesTeam.ID) throws {+ try storage.setData(+ info?.certificate.data(),+ forKey: key(teamID, .certificate)+ )+ try storage.setData(+ info?.privateKey.data,+ forKey: key(teamID, .privateKey)+ )+ }+}diff --git a/SupersignTests/Tests/SupersignDeveloperServicesTests.swift b/SupersignTests/Tests/SupersignDeveloperServicesTests.swiftindex 3480f0e..33cb890 100644--- a/SupersignTests/Tests/SupersignDeveloperServicesTests.swift+++ b/SupersignTests/Tests/SupersignDeveloperServicesTests.swift@@ -30,10 +30,13 @@ class SupersignDeveloperServicesTests: XCTestCase {let team = teams.first { $0.status == "active" && $0.memberships.contains { $0.platform == .iOS } }!let bundle = Bundle(for: Self.self)- let source = try! XCTUnwrap(bundle.url(forResource: "test", withExtension: "app"))+ let source = try XCTUnwrap(bundle.url(forResource: "test", withExtension: "app"))let context = try XCTTry(SigningContext(- udid: "00008030-001409AA0298802E", team: team, signerImpl: .first(), client: client+ udid: Config.current.udid,+ team: team,+ client: client,+ signingInfoManager: TestSigningInfoManager()))let waiter = ResultWaiter<DeveloperServicesProvisioningOperation.Response>(description: "Failed to provision")diff --git a/SupersignTests/Tests/SupersignGrandSlamTests.swift b/SupersignTests/Tests/SupersignGrandSlamTests.swiftnew file mode 100644index 0000000..1659063--- /dev/null+++ b/SupersignTests/Tests/SupersignGrandSlamTests.swift@@ -0,0 +1,70 @@+//+// SupersignGrandSlamTests.swift+// SupersignTests+//+// Created by Kabir Oberai on 10/04/20.+// Copyright © 2020 Kabir Oberai. All rights reserved.+//++import XCTest+@testable import Supersign++class TwoFactorAuthenticator: TwoFactorAuthDelegate {+ func fetchCode(completion: @escaping (String) -> Void) {+ print("Code: ", terminator: "")+ completion(readLine() ?? "")+ }+}++class SupersignGrandSlamTests: XCTestCase {++ var authenticator: TwoFactorAuthenticator!+ var client: GrandSlamClient!++ override func setUp() {+ super.setUp()+ client = GrandSlamClient(+ deviceInfo: Config.current.deviceInfo,+ customAnisetteDataProvider: DirectAnisetteDataProvider()+ )+ authenticator = TwoFactorAuthenticator()+ }++ override func tearDown() {+ super.tearDown()+ client = nil+ authenticator = nil+ }++ func testAuthentication() throws {+ let authWaiter = ResultWaiter<GrandSlamLoginData>(description: "Login timed out")+ GrandSlamAuthenticateOperation(+ client: client,+ username: Config.current.appleID.username,+ password: Config.current.appleID.password,+ twoFactorDelegate: authenticator+ ).authenticate(completion: authWaiter.completion)+ let loginData = try XCTTry(authWaiter.wait(timeout: 10000))+ XCTAssertFalse(loginData.adsid.isEmpty)+ XCTAssertFalse(loginData.cookie.isEmpty)+ XCTAssertFalse(loginData.identityToken.isEmpty)+ XCTAssertFalse(loginData.idmsToken.isEmpty)+ XCTAssertFalse(loginData.sk.isEmpty)++ let now = Date() // *before* the actual request is made++ let tokWaiter = ResultWaiter<[String: GrandSlamFetchAppTokensOperation.Token]>(+ description: "Token request timed out"+ )+ GrandSlamFetchAppTokensOperation(+ client: client,+ apps: ["com.apple.gs.xcode.auth"],+ loginData: loginData+ ).perform(completion: tokWaiter.completion)+ let tokens = try XCTTry(tokWaiter.wait(timeout: 10000))+ let token = try XCTUnwrap(tokens["com.apple.gs.xcode.auth"], "Xcode token absent from response")+ XCTAssertGreaterThanOrEqual(token.expiry, now)+ XCTAssertFalse(token.value.isEmpty)+ }++}diff --git a/SupersignTests/Tests/SupersignSigningTests.swift b/SupersignTests/Tests/SupersignSigningTests.swiftindex 8e4ba76..57151d9 100644--- a/SupersignTests/Tests/SupersignSigningTests.swift+++ b/SupersignTests/Tests/SupersignSigningTests.swift@@ -47,12 +47,16 @@ class SupersignSigningTests: XCTestCase {let team = teams.first { $0.status == "active" && $0.memberships.contains { $0.platform == .iOS } }!let context = try XCTTry(SigningContext(- udid: "00008030-001409AA0298802E", team: team, signerImpl: signerImpl, client: client+ udid: Config.current.udid,+ team: team,+ client: client,+ signingInfoManager: TestSigningInfoManager(),+ signerImpl: signerImpl))let signer = Signer(context: context)let signingWaiter = ResultWaiter<()>(description: "Failed to sign app")- signer.sign(app: app, progress: { _ in }, completion: signingWaiter.completion)+ signer.sign(app: app, status: { _ in }, progress: { _ in }, completion: signingWaiter.completion)try signingWaiter.wait(timeout: 10000)// TODO: Ensure entitlements are correct in new app, also codesign -vvvvdiff --git a/SupersignTests/Utilities/Config.swift b/SupersignTests/Utilities/Config.swiftindex 8397aa8..ad07bdf 100644--- a/SupersignTests/Utilities/Config.swift+++ b/SupersignTests/Utilities/Config.swift@@ -7,6 +7,7 @@//import Foundation+import Supersignstruct Config: Decodable {struct AppleID: Decodable {@@ -15,9 +16,11 @@ struct Config: Decodable {/// for non-login tests, just provide a token already so that the 2fa prompt isn't invokedlet token: String}- let appleID: AppleID+ let appleID: AppleID+ let deviceInfo: DeviceInfolet preferredTeam: String+ let udid: Stringstatic let current: Config = {class ConfigDummy {}diff --git a/SupersignTests/Utilities/DeveloperServicesClient+Testing.swift b/SupersignTests/Utilities/DeveloperServicesClient+Testing.swiftindex d50f1fb..f68b99f 100644--- a/SupersignTests/Utilities/DeveloperServicesClient+Testing.swift+++ b/SupersignTests/Utilities/DeveloperServicesClient+Testing.swift@@ -10,7 +10,7 @@ import Foundationimport XCTestimport Supersign-struct CredentialsManagerMock: DeveloperServicesCredentialsManagerProtocol {+struct CredentialsManagerMock: DeveloperServicesCredentialsManager {// store login in memorypublic var token: DeveloperServicesCredentialsToken?}diff --git a/SupersignTests/config/config-template.json b/SupersignTests/config/config-template.jsonindex 91a4b85..060f993 100644--- a/SupersignTests/config/config-template.json+++ b/SupersignTests/config/config-template.json@@ -4,5 +4,25 @@"password": "","token": ""},- "preferredTeam": ""+ "deviceInfo": {+ "clientInfo": {+ "macOSBuild": "",+ "darwinVersion": "",+ "macOSVersion": "",+ "akdVersion": "",+ "cfNetworkVersion": "",+ "authKitVersion": "",+ "modelID": "",+ },+ "xcodeInfo": {+ "version": "",+ "build": ""+ },+ "deviceID": "",+ "mlbSerialNumber": "",+ "romAddress": "",+ "serialNumber": ""+ },+ "preferredTeam": "",+ "udid": "",}