Refine mock VPN actions and events
This commit is contained in:
parent
36f0b2c03d
commit
769a79c4c0
|
@ -30,12 +30,24 @@ import NetworkExtension
|
||||||
public class MockVPN: VPN {
|
public class MockVPN: VPN {
|
||||||
private var tunnelBundleIdentifier: String?
|
private var tunnelBundleIdentifier: String?
|
||||||
|
|
||||||
private var currentIsEnabled = false
|
private var isEnabled: Bool {
|
||||||
|
didSet {
|
||||||
|
notifyReinstall(isEnabled)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var vpnStatus: VPNStatus {
|
||||||
|
didSet {
|
||||||
|
notifyStatus(vpnStatus)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private let delayNanoseconds: UInt64
|
private let delayNanoseconds: UInt64
|
||||||
|
|
||||||
public init(delay: Int = 1) {
|
public init(delay: Int = 1) {
|
||||||
delayNanoseconds = DispatchTimeInterval.seconds(delay).nanoseconds
|
delayNanoseconds = DispatchTimeInterval.seconds(delay).nanoseconds
|
||||||
|
isEnabled = false
|
||||||
|
vpnStatus = .disconnected
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: VPN
|
// MARK: VPN
|
||||||
|
@ -49,13 +61,18 @@ public class MockVPN: VPN {
|
||||||
extra: NetworkExtensionExtra?
|
extra: NetworkExtensionExtra?
|
||||||
) {
|
) {
|
||||||
self.tunnelBundleIdentifier = tunnelBundleIdentifier
|
self.tunnelBundleIdentifier = tunnelBundleIdentifier
|
||||||
notifyReinstall(true)
|
isEnabled = true
|
||||||
notifyStatus(.disconnected)
|
vpnStatus = .disconnected
|
||||||
}
|
}
|
||||||
|
|
||||||
public func reconnect(after: DispatchTimeInterval) async throws {
|
public func reconnect(after: DispatchTimeInterval) async throws {
|
||||||
|
if vpnStatus == .connected {
|
||||||
|
vpnStatus = .disconnecting
|
||||||
await delay()
|
await delay()
|
||||||
notifyStatus(.connected)
|
}
|
||||||
|
vpnStatus = .connecting
|
||||||
|
await delay()
|
||||||
|
vpnStatus = .connected
|
||||||
}
|
}
|
||||||
|
|
||||||
public func reconnect(
|
public func reconnect(
|
||||||
|
@ -65,31 +82,34 @@ public class MockVPN: VPN {
|
||||||
after: DispatchTimeInterval
|
after: DispatchTimeInterval
|
||||||
) async throws {
|
) async throws {
|
||||||
self.tunnelBundleIdentifier = tunnelBundleIdentifier
|
self.tunnelBundleIdentifier = tunnelBundleIdentifier
|
||||||
notifyReinstall(true)
|
isEnabled = true
|
||||||
|
if vpnStatus == .connected {
|
||||||
|
vpnStatus = .disconnecting
|
||||||
await delay()
|
await delay()
|
||||||
notifyStatus(.connecting)
|
}
|
||||||
|
vpnStatus = .connecting
|
||||||
await delay()
|
await delay()
|
||||||
notifyStatus(.connected)
|
vpnStatus = .connected
|
||||||
}
|
}
|
||||||
|
|
||||||
public func disconnect() async {
|
public func disconnect() async {
|
||||||
notifyReinstall(false)
|
guard vpnStatus != .disconnected else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vpnStatus = .disconnecting
|
||||||
await delay()
|
await delay()
|
||||||
notifyStatus(.disconnecting)
|
vpnStatus = .disconnected
|
||||||
await delay()
|
isEnabled = false
|
||||||
notifyStatus(.disconnected)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func uninstall() async {
|
public func uninstall() async {
|
||||||
await delay()
|
vpnStatus = .disconnected
|
||||||
notifyReinstall(false)
|
isEnabled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Helpers
|
// MARK: Helpers
|
||||||
|
|
||||||
private func notifyReinstall(_ isEnabled: Bool) {
|
private func notifyReinstall(_ isEnabled: Bool) {
|
||||||
currentIsEnabled = isEnabled
|
|
||||||
|
|
||||||
var notification = Notification(name: VPNNotification.didReinstall)
|
var notification = Notification(name: VPNNotification.didReinstall)
|
||||||
notification.vpnBundleIdentifier = tunnelBundleIdentifier
|
notification.vpnBundleIdentifier = tunnelBundleIdentifier
|
||||||
notification.vpnIsEnabled = isEnabled
|
notification.vpnIsEnabled = isEnabled
|
||||||
|
@ -99,7 +119,7 @@ public class MockVPN: VPN {
|
||||||
private func notifyStatus(_ status: VPNStatus) {
|
private func notifyStatus(_ status: VPNStatus) {
|
||||||
var notification = Notification(name: VPNNotification.didChangeStatus)
|
var notification = Notification(name: VPNNotification.didChangeStatus)
|
||||||
notification.vpnBundleIdentifier = tunnelBundleIdentifier
|
notification.vpnBundleIdentifier = tunnelBundleIdentifier
|
||||||
notification.vpnIsEnabled = currentIsEnabled
|
notification.vpnIsEnabled = isEnabled
|
||||||
notification.vpnStatus = status
|
notification.vpnStatus = status
|
||||||
NotificationCenter.default.post(notification)
|
NotificationCenter.default.post(notification)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue