Skip to content

Commit

Permalink
Add setup for writing tests for TunnelManager
Browse files Browse the repository at this point in the history
  • Loading branch information
buggmagnet committed Oct 10, 2023
1 parent 6d2ed0d commit b2fa3f1
Show file tree
Hide file tree
Showing 57 changed files with 1,210 additions and 459 deletions.
50 changes: 44 additions & 6 deletions ios/MullvadREST/RESTAPIProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,46 @@ import MullvadTypes
import struct WireGuardKitTypes.IPAddressRange
import class WireGuardKitTypes.PublicKey

public protocol APIQuerying {
func getAddressList(
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping ProxyCompletionHandler<[AnyIPEndpoint]>
) -> Cancellable

func getRelays(
etag: String?,
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping ProxyCompletionHandler<REST.ServerRelaysCacheResponse>
) -> Cancellable

func createApplePayment(
accountNumber: String,
receiptString: Data
) -> any RESTRequestExecutor<REST.CreateApplePaymentResponse>

func createApplePayment(
accountNumber: String,
receiptString: Data,
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping ProxyCompletionHandler<REST.CreateApplePaymentResponse>
) -> Cancellable

func sendProblemReport(
_ body: REST.ProblemReportRequest,
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping ProxyCompletionHandler<Void>
) -> Cancellable

func submitVoucher(
voucherCode: String,
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping ProxyCompletionHandler<REST.SubmitVoucherResponse>
) -> Cancellable
}

extension REST {
public final class APIProxy: Proxy<AuthProxyConfiguration> {
public final class APIProxy: Proxy<AuthProxyConfiguration>, APIQuerying {
public init(configuration: AuthProxyConfiguration) {
super.init(
name: "APIProxy",
Expand All @@ -27,7 +65,7 @@ extension REST {

public func getAddressList(
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping CompletionHandler<[AnyIPEndpoint]>
completionHandler: @escaping ProxyCompletionHandler<[AnyIPEndpoint]>
) -> Cancellable {
let requestHandler = AnyRequestHandler { endpoint in
try self.requestFactory.createRequest(
Expand All @@ -54,7 +92,7 @@ extension REST {
public func getRelays(
etag: String?,
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping CompletionHandler<ServerRelaysCacheResponse>
completionHandler: @escaping ProxyCompletionHandler<ServerRelaysCacheResponse>
) -> Cancellable {
let requestHandler = AnyRequestHandler { endpoint in
var requestBuilder = try self.requestFactory.createRequestBuilder(
Expand Down Expand Up @@ -170,7 +208,7 @@ extension REST {
accountNumber: String,
receiptString: Data,
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping CompletionHandler<CreateApplePaymentResponse>
completionHandler: @escaping ProxyCompletionHandler<CreateApplePaymentResponse>
) -> Cancellable {
return createApplePayment(accountNumber: accountNumber, receiptString: receiptString).execute(
retryStrategy: retryStrategy,
Expand All @@ -181,7 +219,7 @@ extension REST {
public func sendProblemReport(
_ body: ProblemReportRequest,
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping CompletionHandler<Void>
completionHandler: @escaping ProxyCompletionHandler<Void>
) -> Cancellable {
let requestHandler = AnyRequestHandler { endpoint in
var requestBuilder = try self.requestFactory.createRequestBuilder(
Expand Down Expand Up @@ -222,7 +260,7 @@ extension REST {
voucherCode: String,
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping CompletionHandler<SubmitVoucherResponse>
completionHandler: @escaping ProxyCompletionHandler<SubmitVoucherResponse>
) -> Cancellable {
let requestHandler = AnyRequestHandler(
createURLRequest: { endpoint, authorization in
Expand Down
15 changes: 12 additions & 3 deletions ios/MullvadREST/RESTAccessTokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ import MullvadLogging
import MullvadTypes
import Operations

public protocol AccessTokenManagement {
func getAccessToken(
accountNumber: String,
completionHandler: @escaping ProxyCompletionHandler<REST.AccessTokenData>
) -> Cancellable

func invalidateAllTokens()
}

extension REST {
public final class AccessTokenManager {
public final class AccessTokenManager: AccessTokenManagement {
private let logger = Logger(label: "REST.AccessTokenManager")
private let operationQueue = AsyncOperationQueue.makeSerial()
private let dispatchQueue = DispatchQueue(label: "REST.AccessTokenManager.dispatchQueue")
Expand All @@ -23,9 +32,9 @@ extension REST {
proxy = authenticationProxy
}

func getAccessToken(
public func getAccessToken(
accountNumber: String,
completionHandler: @escaping (Result<REST.AccessTokenData, Swift.Error>) -> Void
completionHandler: @escaping ProxyCompletionHandler<REST.AccessTokenData>
) -> Cancellable {
let operation =
ResultBlockOperation<REST.AccessTokenData>(dispatchQueue: dispatchQueue) { finish -> Cancellable in
Expand Down
29 changes: 25 additions & 4 deletions ios/MullvadREST/RESTAccountsProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@
import Foundation
import MullvadTypes

public protocol AccountHandling {
func createAccount(
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<REST.NewAccountData>
) -> Cancellable

func getAccountData(accountNumber: String) -> any RESTRequestExecutor<Account>

func getAccountData(
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Account>
) -> Cancellable

func deleteAccount(
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Void>
) -> Cancellable
}

extension REST {
public final class AccountsProxy: Proxy<AuthProxyConfiguration> {
public final class AccountsProxy: Proxy<AuthProxyConfiguration>, AccountHandling {
public init(configuration: AuthProxyConfiguration) {
super.init(
name: "AccountsProxy",
Expand All @@ -25,7 +46,7 @@ extension REST {

public func createAccount(
retryStrategy: REST.RetryStrategy,
completion: @escaping CompletionHandler<NewAccountData>
completion: @escaping ProxyCompletionHandler<NewAccountData>
) -> Cancellable {
let requestHandler = AnyRequestHandler { endpoint in
try self.requestFactory.createRequest(
Expand Down Expand Up @@ -81,7 +102,7 @@ extension REST {
public func getAccountData(
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping CompletionHandler<Account>
completion: @escaping ProxyCompletionHandler<Account>
) -> Cancellable {
return getAccountData(accountNumber: accountNumber).execute(
retryStrategy: retryStrategy,
Expand All @@ -92,7 +113,7 @@ extension REST {
public func deleteAccount(
accountNumber: String,
retryStrategy: RetryStrategy,
completion: @escaping CompletionHandler<Void>
completion: @escaping ProxyCompletionHandler<Void>
) -> Cancellable {
let requestHandler = AnyRequestHandler(createURLRequest: { endpoint, authorization in
var requestBuilder = try self.requestFactory.createRequestBuilder(
Expand Down
4 changes: 2 additions & 2 deletions ios/MullvadREST/RESTAuthenticationProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension REST {
func getAccessToken(
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping CompletionHandler<AccessTokenData>
completion: @escaping ProxyCompletionHandler<AccessTokenData>
) -> Cancellable {
let requestHandler = AnyRequestHandler { endpoint in
var requestBuilder = try self.requestFactory.createRequestBuilder(
Expand Down Expand Up @@ -57,7 +57,7 @@ extension REST {
}
}

struct AccessTokenData: Decodable {
public struct AccessTokenData: Decodable {
let accessToken: String
let expiry: Date
}
Expand Down
49 changes: 43 additions & 6 deletions ios/MullvadREST/RESTDevicesProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,45 @@ import MullvadTypes
import struct WireGuardKitTypes.IPAddressRange
import class WireGuardKitTypes.PublicKey

public protocol DeviceHandling {
func getDevice(
accountNumber: String,
identifier: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable

func getDevices(
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<[Device]>
) -> Cancellable

func createDevice(
accountNumber: String,
request: REST.CreateDeviceRequest,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable

func deleteDevice(
accountNumber: String,
identifier: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Bool>
) -> Cancellable

func rotateDeviceKey(
accountNumber: String,
identifier: String,
publicKey: PublicKey,
retryStrategy: REST.RetryStrategy,
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable
}

extension REST {
public final class DevicesProxy: Proxy<AuthProxyConfiguration> {
public final class DevicesProxy: Proxy<AuthProxyConfiguration>, DeviceHandling {
public init(configuration: AuthProxyConfiguration) {
super.init(
name: "DevicesProxy",
Expand All @@ -31,7 +68,7 @@ extension REST {
accountNumber: String,
identifier: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping CompletionHandler<Device>
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable {
let requestHandler = AnyRequestHandler(
createURLRequest: { endpoint, authorization in
Expand Down Expand Up @@ -87,7 +124,7 @@ extension REST {
public func getDevices(
accountNumber: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping CompletionHandler<[Device]>
completion: @escaping ProxyCompletionHandler<[Device]>
) -> Cancellable {
let requestHandler = AnyRequestHandler(
createURLRequest: { endpoint, authorization in
Expand Down Expand Up @@ -125,7 +162,7 @@ extension REST {
accountNumber: String,
request: CreateDeviceRequest,
retryStrategy: REST.RetryStrategy,
completion: @escaping CompletionHandler<Device>
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable {
let requestHandler = AnyRequestHandler(
createURLRequest: { endpoint, authorization in
Expand Down Expand Up @@ -164,7 +201,7 @@ extension REST {
accountNumber: String,
identifier: String,
retryStrategy: REST.RetryStrategy,
completion: @escaping CompletionHandler<Bool>
completion: @escaping ProxyCompletionHandler<Bool>
) -> Cancellable {
let requestHandler = AnyRequestHandler(
createURLRequest: { endpoint, authorization in
Expand Down Expand Up @@ -226,7 +263,7 @@ extension REST {
identifier: String,
publicKey: PublicKey,
retryStrategy: REST.RetryStrategy,
completion: @escaping CompletionHandler<Device>
completion: @escaping ProxyCompletionHandler<Device>
) -> Cancellable {
let requestHandler = AnyRequestHandler(
createURLRequest: { endpoint, authorization in
Expand Down
12 changes: 6 additions & 6 deletions ios/MullvadREST/RESTProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import Foundation
import MullvadTypes
import Operations

public typealias ProxyCompletionHandler<Success> = (Result<Success, Swift.Error>) -> Void

extension REST {
public class Proxy<ConfigurationType: ProxyConfiguration> {
public typealias CompletionHandler<Success> = (Result<Success, Swift.Error>) -> Void

/// Synchronization queue used by network operations.
let dispatchQueue: DispatchQueue

Expand Down Expand Up @@ -49,7 +49,7 @@ extension REST {
retryStrategy: REST.RetryStrategy,
requestHandler: RESTRequestHandler,
responseHandler: some RESTResponseHandler<Success>,
completionHandler: @escaping CompletionHandler<Success>
completionHandler: @escaping ProxyCompletionHandler<Success>
) -> Cancellable {
let executor = makeRequestExecutor(
name: name,
Expand Down Expand Up @@ -89,7 +89,7 @@ extension REST {
/// Creates new network operation but does not schedule it for execution.
func makeOperation(
retryStrategy: REST.RetryStrategy,
completionHandler: ((Result<Success, Swift.Error>) -> Void)? = nil
completionHandler: ProxyCompletionHandler<Success>? = nil
) -> NetworkOperation<Success> {
return NetworkOperation(
name: getTaskIdentifier(name: name),
Expand All @@ -110,7 +110,7 @@ extension REST {

func execute(
retryStrategy: REST.RetryStrategy,
completionHandler: @escaping (Result<Success, Swift.Error>) -> Void
completionHandler: @escaping ProxyCompletionHandler<Success>
) -> Cancellable {
let operation = operationFactory.makeOperation(
retryStrategy: retryStrategy,
Expand All @@ -137,7 +137,7 @@ extension REST {
}
}

func execute(completionHandler: @escaping (Result<Success, Swift.Error>) -> Void) -> Cancellable {
func execute(completionHandler: @escaping ProxyCompletionHandler<Success>) -> Cancellable {
return execute(retryStrategy: .noRetry, completionHandler: completionHandler)
}

Expand Down
6 changes: 3 additions & 3 deletions ios/MullvadREST/RESTProxyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ extension REST {
self.configuration = configuration
}

public func createAPIProxy() -> REST.APIProxy {
public func createAPIProxy() -> APIQuerying {
REST.APIProxy(configuration: configuration)
}

public func createAccountsProxy() -> REST.AccountsProxy {
public func createAccountsProxy() -> AccountHandling {
REST.AccountsProxy(configuration: configuration)
}

public func createDevicesProxy() -> REST.DevicesProxy {
public func createDevicesProxy() -> DeviceHandling {
REST.DevicesProxy(configuration: configuration)
}
}
Expand Down
4 changes: 2 additions & 2 deletions ios/MullvadTransport/TransportProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import RelaySelector

public final class TransportProvider: RESTTransportProvider {
private let urlSessionTransport: URLSessionTransport
private let relayCache: RelayCache
private let relayCache: RelayCacheProtocol
private let logger = Logger(label: "TransportProvider")
private let addressCache: REST.AddressCache
private let shadowsocksCache: ShadowsocksConfigurationCache
Expand All @@ -28,7 +28,7 @@ public final class TransportProvider: RESTTransportProvider {

public init(
urlSessionTransport: URLSessionTransport,
relayCache: RelayCache,
relayCache: RelayCacheProtocol,
addressCache: REST.AddressCache,
shadowsocksCache: ShadowsocksConfigurationCache,
transportStrategy: TransportStrategy = .init(),
Expand Down
Loading

0 comments on commit b2fa3f1

Please sign in to comment.