Skip to content

Commit

Permalink
Fixes the unit tests for NetworkProtectionIPCTunnelController and add…
Browse files Browse the repository at this point in the history
…s more tests
  • Loading branch information
diegoreymendez committed Apr 18, 2024
1 parent f1af096 commit f37cc5f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extension NetworkProtectionIPCTunnelController: TunnelController {

func handleFailure(_ error: Error) {
log(error)
pixelKit?.fire(StartAttempt.failure(error), frequency: .dailyAndCount)
pixelKit?.fire(StopAttempt.failure(error), frequency: .dailyAndCount)
}

do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import Foundation
import PixelKit
import XCTest

public final class PixelKitMock: PixelFiring {

Expand All @@ -42,8 +43,8 @@ public final class PixelKitMock: PixelFiring {
actualFireCalls.append(fireCall)
}

public var expectationsMet: Bool {
expectedFireCalls == actualFireCalls
public func verifyExpectations(file: StaticString, line: UInt) {
XCTAssertEqual(expectedFireCalls, actualFireCalls, file: file, line: line)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ final class NetworkProtectionIPCTunnelControllerTests: XCTestCase {

await controller.start()

XCTAssertTrue(pixelKit.expectationsMet)
pixelKit.verifyExpectations(file: #file, line: #line)
}

func testStartTunnelLoginItemFailure() async {
let error = NSError(domain: "test", code: 1)
let expectedError = NetworkProtectionIPCTunnelController.RequestError.internalLoginItemError(error)

let pixelKit = PixelKitMock(expecting: [
.init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.begin, frequency: .standard),
.init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.failure(error), frequency: .dailyAndCount)
.init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.failure(expectedError), frequency: .dailyAndCount)
])

let controller = NetworkProtectionIPCTunnelController(
featureVisibility: MockFeatureVisibility(),
loginItemsManager: MockLoginItemsManager(mockResult: .failure(error)),
Expand All @@ -58,7 +61,24 @@ final class NetworkProtectionIPCTunnelControllerTests: XCTestCase {

await controller.start()

XCTAssertTrue(pixelKit.expectationsMet)
pixelKit.verifyExpectations(file: #file, line: #line)
}

func testStartTunnelIPCFailure() async {
let error = NSError(domain: "test", code: 1)
let pixelKit = PixelKitMock(expecting: [
.init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.begin, frequency: .standard),
.init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.failure(error), frequency: .dailyAndCount)
])
let controller = NetworkProtectionIPCTunnelController(
featureVisibility: MockFeatureVisibility(),
loginItemsManager: MockLoginItemsManager(mockResult: .success),
ipcClient: MockIPCClient(error: error),
pixelKit: pixelKit)

await controller.start()

pixelKit.verifyExpectations(file: #file, line: #line)
}

// MARK: - Tunnel Stop Tests
Expand All @@ -76,15 +96,18 @@ final class NetworkProtectionIPCTunnelControllerTests: XCTestCase {

await controller.stop()

XCTAssertTrue(pixelKit.expectationsMet)
pixelKit.verifyExpectations(file: #file, line: #line)
}

func testStopTunnelLoginItemFailure() async {
let error = NSError(domain: "test", code: 1)
let expectedError = NetworkProtectionIPCTunnelController.RequestError.internalLoginItemError(error)

let pixelKit = PixelKitMock(expecting: [
.init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.begin, frequency: .standard),
.init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.failure(error), frequency: .dailyAndCount)
.init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.failure(expectedError), frequency: .dailyAndCount)
])

let controller = NetworkProtectionIPCTunnelController(
featureVisibility: MockFeatureVisibility(),
loginItemsManager: MockLoginItemsManager(mockResult: .failure(error)),
Expand All @@ -93,6 +116,23 @@ final class NetworkProtectionIPCTunnelControllerTests: XCTestCase {

await controller.stop()

XCTAssertTrue(pixelKit.expectationsMet)
pixelKit.verifyExpectations(file: #file, line: #line)
}

func testStopTunnelIPCFailure() async {
let error = NSError(domain: "test", code: 1)
let pixelKit = PixelKitMock(expecting: [
.init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.begin, frequency: .standard),
.init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.failure(error), frequency: .dailyAndCount)
])
let controller = NetworkProtectionIPCTunnelController(
featureVisibility: MockFeatureVisibility(),
loginItemsManager: MockLoginItemsManager(mockResult: .success),
ipcClient: MockIPCClient(error: error),
pixelKit: pixelKit)

await controller.stop()

pixelKit.verifyExpectations(file: #file, line: #line)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,23 @@ struct MockConnectionErrorObserver: ConnectionErrorObserver {
}

struct MockIPCClient: NetworkProtectionIPCClient {
var ipcStatusObserver: NetworkProtection.ConnectionStatusObserver = MockConnectionStatusObserver()

var ipcServerInfoObserver: NetworkProtection.ConnectionServerInfoObserver = MockServerInfoObserver()
private let error: Error?

var ipcStatusObserver: NetworkProtection.ConnectionStatusObserver = MockConnectionStatusObserver()
var ipcServerInfoObserver: NetworkProtection.ConnectionServerInfoObserver = MockServerInfoObserver()
var ipcConnectionErrorObserver: NetworkProtection.ConnectionErrorObserver = MockConnectionErrorObserver()

func start() {
// Intentional no-op
init(error: Error? = nil) {
self.error = error
}

func stop() {
// Intentional no-op
func start(completion: @escaping (Error?) -> Void) {
completion(error)
}

func stop(completion: @escaping (Error?) -> Void) {
completion(error)
}
}

Expand Down

0 comments on commit f37cc5f

Please sign in to comment.