Skip to content

Commit

Permalink
Do ungodly things to make the tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkisemils committed Dec 13, 2024
1 parent 3ebf0a8 commit af5b003
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 50 deletions.
37 changes: 23 additions & 14 deletions ios/MullvadRustRuntimeTests/MullvadPostQuantum+Stubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ class NWTCPConnectionStub: NWTCPConnection {
}

class TunnelProviderStub: TunnelProvider {
func tunnelHandle() throws -> Int32 {
0
}

func wgFuncs() -> MullvadTypes.WgFuncPointers {
return MullvadTypes.WgFuncPointers(
open: { _, _, _ in return 0 },
close: { _, _ in return 0 },
receive: { _, _, _, _ in return 0 },
send: { _, _, _, _ in return 0 }
)
}

let tcpConnection: NWTCPConnectionStub

init(tcpConnection: NWTCPConnectionStub) {
Expand Down Expand Up @@ -55,15 +68,13 @@ class FailedNegotiatorStub: EphemeralPeerNegotiating {
}

func startNegotiation(
gatewayIP: IPv4Address,
devicePublicKey: WireGuardKitTypes.PublicKey,
presharedKey: WireGuardKitTypes.PrivateKey,
peerReceiver packetTunnel: any MullvadTypes.TunnelProvider,
tcpConnection: NWTCPConnection,
peerExchangeTimeout: MullvadTypes.Duration,
enablePostQuantum: Bool,
enableDaita: Bool
) -> Bool { false }
peerReceiver: any MullvadTypes.TunnelProvider,
ephemeralPeerParams: EphemeralPeerParameters
) -> Bool {
false
}

func cancelKeyNegotiation() {
onCancelKeyNegotiation?()
Expand All @@ -81,15 +92,13 @@ class SuccessfulNegotiatorStub: EphemeralPeerNegotiating {
}

func startNegotiation(
gatewayIP: IPv4Address,
devicePublicKey: WireGuardKitTypes.PublicKey,
presharedKey: WireGuardKitTypes.PrivateKey,
peerReceiver packetTunnel: any MullvadTypes.TunnelProvider,
tcpConnection: NWTCPConnection,
peerExchangeTimeout: MullvadTypes.Duration,
enablePostQuantum: Bool,
enableDaita: Bool
) -> Bool { true }
peerReceiver: any MullvadTypes.TunnelProvider,
ephemeralPeerParams: EphemeralPeerParameters
) -> Bool {
true
}

func cancelKeyNegotiation() {
onCancelKeyNegotiation?()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
)
}

func testSingleHopPostQuantumKeyExchange() throws {
func testSingleHopPostQuantumKeyExchange() async throws {
let reconfigurationExpectation = expectation(description: "Tunnel reconfiguration took place")
reconfigurationExpectation.expectedFulfillmentCount = 2

Expand All @@ -78,19 +78,19 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
}

keyExchangeActor.delegate = KeyExchangingResultStub(onReceivePostQuantumKey: { preSharedKey, privateKey in
postQuantumKeyExchangingPipeline.receivePostQuantumKey(preSharedKey, ephemeralKey: privateKey)
await postQuantumKeyExchangingPipeline.receivePostQuantumKey(preSharedKey, ephemeralKey: privateKey)
})

let connectionState = stubConnectionState(enableMultiHop: false, enablePostQuantum: true, enableDaita: false)
postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())
await postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())

wait(
for: [reconfigurationExpectation, negotiationSuccessful],
timeout: .UnitTest.invertedTimeout
)
}

func testSingleHopDaitaPeerExchange() throws {
func testSingleHopDaitaPeerExchange() async throws {
let reconfigurationExpectation = expectation(description: "Tunnel reconfiguration took place")
reconfigurationExpectation.expectedFulfillmentCount = 2

Expand All @@ -108,19 +108,19 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
}

keyExchangeActor.delegate = KeyExchangingResultStub(onReceiveEphemeralPeerPrivateKey: { privateKey in
postQuantumKeyExchangingPipeline.receiveEphemeralPeerPrivateKey(privateKey)
await postQuantumKeyExchangingPipeline.receiveEphemeralPeerPrivateKey(privateKey)
})

let connectionState = stubConnectionState(enableMultiHop: false, enablePostQuantum: false, enableDaita: true)
postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())
await postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())

wait(
for: [reconfigurationExpectation, negotiationSuccessful],
timeout: .UnitTest.invertedTimeout
)
}

func testMultiHopPostQuantumKeyExchange() throws {
func testMultiHopPostQuantumKeyExchange() async throws {
let reconfigurationExpectation = expectation(description: "Tunnel reconfiguration took place")
reconfigurationExpectation.expectedFulfillmentCount = 3

Expand All @@ -138,19 +138,19 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
}

keyExchangeActor.delegate = KeyExchangingResultStub(onReceivePostQuantumKey: { preSharedKey, privateKey in
postQuantumKeyExchangingPipeline.receivePostQuantumKey(preSharedKey, ephemeralKey: privateKey)
await postQuantumKeyExchangingPipeline.receivePostQuantumKey(preSharedKey, ephemeralKey: privateKey)
})

let connectionState = stubConnectionState(enableMultiHop: true, enablePostQuantum: true, enableDaita: false)
postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())
await postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())

wait(
for: [reconfigurationExpectation, negotiationSuccessful],
timeout: .UnitTest.invertedTimeout
)
}

func testMultiHopDaitaExchange() throws {
func testMultiHopDaitaExchange() async throws {
let reconfigurationExpectation = expectation(description: "Tunnel reconfiguration took place")
reconfigurationExpectation.expectedFulfillmentCount = 3

Expand All @@ -168,11 +168,11 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
}

keyExchangeActor.delegate = KeyExchangingResultStub(onReceiveEphemeralPeerPrivateKey: { privateKey in
postQuantumKeyExchangingPipeline.receiveEphemeralPeerPrivateKey(privateKey)
await postQuantumKeyExchangingPipeline.receiveEphemeralPeerPrivateKey(privateKey)
})

let connectionState = stubConnectionState(enableMultiHop: true, enablePostQuantum: false, enableDaita: true)
postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())
await postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())

wait(
for: [reconfigurationExpectation, negotiationSuccessful],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ final class EphemeralPeerExchangeActorStub: EphemeralPeerExchangeActorProtocol {
switch result {
case let .success((preSharedKey, ephemeralKey)):
if enablePostQuantum {
delegate?.receivePostQuantumKey(preSharedKey, ephemeralKey: ephemeralKey)
Task { await delegate?.receivePostQuantumKey(preSharedKey, ephemeralKey: ephemeralKey) }
} else {
delegate?.receiveEphemeralPeerPrivateKey(ephemeralKey)
Task { await delegate?.receiveEphemeralPeerPrivateKey(ephemeralKey) }
}
case .failure:
delegate?.ephemeralPeerExchangeFailed()
Expand Down
12 changes: 6 additions & 6 deletions ios/PacketTunnelCoreTests/Mocks/KeyExchangingResultStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

struct KeyExchangingResultStub: EphemeralPeerReceiving {
var onFailure: (() -> Void)?
var onReceivePostQuantumKey: ((PreSharedKey, PrivateKey) -> Void)?
var onReceiveEphemeralPeerPrivateKey: ((PrivateKey) -> Void)?
var onReceivePostQuantumKey: ((PreSharedKey, PrivateKey) async -> Void)?
var onReceiveEphemeralPeerPrivateKey: ((PrivateKey) async -> Void)?

func receivePostQuantumKey(_ key: PreSharedKey, ephemeralKey: PrivateKey) {
onReceivePostQuantumKey?(key, ephemeralKey)
func receivePostQuantumKey(_ key: PreSharedKey, ephemeralKey: PrivateKey) async {
await onReceivePostQuantumKey?(key, ephemeralKey)
}

public func receiveEphemeralPeerPrivateKey(_ ephemeralPeerPrivateKey: PrivateKey) {
onReceiveEphemeralPeerPrivateKey?(ephemeralPeerPrivateKey)
public func receiveEphemeralPeerPrivateKey(_ ephemeralPeerPrivateKey: PrivateKey) async {
await onReceiveEphemeralPeerPrivateKey?(ephemeralPeerPrivateKey)
}

func ephemeralPeerExchangeFailed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class MultiHopEphemeralPeerExchangerTests: XCTestCase {
)
}

func testEphemeralPeerExchangeFailsWhenNegotiationCannotStart() {
func testEphemeralPeerExchangeFailsWhenNegotiationCannotStart() async {
let expectedNegotiationFailure = expectation(description: "Negotiation failed.")

let reconfigurationExpectation = expectation(description: "Tunnel reconfiguration took place")
Expand Down Expand Up @@ -88,15 +88,15 @@ final class MultiHopEphemeralPeerExchangerTests: XCTestCase {
expectedNegotiationFailure.fulfill()
}

multiHopExchanger.start()
await multiHopExchanger.start()

wait(
for: [expectedNegotiationFailure, reconfigurationExpectation, negotiationSuccessful],
timeout: .UnitTest.invertedTimeout
)
}

func testEphemeralPeerExchangeSuccessWhenPostQuantumNegotiationStarts() throws {
func testEphemeralPeerExchangeSuccessWhenPostQuantumNegotiationStarts() async throws {
let unexpectedNegotiationFailure = expectation(description: "Negotiation failed.")
unexpectedNegotiationFailure.isInverted = true

Expand Down Expand Up @@ -124,17 +124,17 @@ final class MultiHopEphemeralPeerExchangerTests: XCTestCase {
}

peerExchangeActor.delegate = KeyExchangingResultStub(onReceivePostQuantumKey: { preSharedKey, ephemeralKey in
multiHopPeerExchanger.receivePostQuantumKey(preSharedKey, ephemeralKey: ephemeralKey)
await multiHopPeerExchanger.receivePostQuantumKey(preSharedKey, ephemeralKey: ephemeralKey)
})
multiHopPeerExchanger.start()
await multiHopPeerExchanger.start()

wait(
for: [unexpectedNegotiationFailure, reconfigurationExpectation, negotiationSuccessful],
timeout: .UnitTest.invertedTimeout
)
}

func testEphemeralPeerExchangeSuccessWhenDaitaNegotiationStarts() throws {
func testEphemeralPeerExchangeSuccessWhenDaitaNegotiationStarts() async throws {
let unexpectedNegotiationFailure = expectation(description: "Negotiation failed.")
unexpectedNegotiationFailure.isInverted = true

Expand Down Expand Up @@ -162,9 +162,9 @@ final class MultiHopEphemeralPeerExchangerTests: XCTestCase {
}

peerExchangeActor.delegate = KeyExchangingResultStub(onReceiveEphemeralPeerPrivateKey: { ephemeralKey in
multiHopPeerExchanger.receiveEphemeralPeerPrivateKey(ephemeralKey)
await multiHopPeerExchanger.receiveEphemeralPeerPrivateKey(ephemeralKey)
})
multiHopPeerExchanger.start()
await multiHopPeerExchanger.start()

wait(
for: [unexpectedNegotiationFailure, reconfigurationExpectation, negotiationSuccessful],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class SingleHopEphemeralPeerExchangerTests: XCTestCase {
exitRelay = SelectedRelay(endpoint: match.endpoint, hostname: match.relay.hostname, location: match.location)
}

func testEphemeralPeerExchangeFailsWhenNegotiationCannotStart() {
func testEphemeralPeerExchangeFailsWhenNegotiationCannotStart() async {
let expectedNegotiationFailure = expectation(description: "Negotiation failed.")

let reconfigurationExpectation = expectation(description: "Tunnel reconfiguration took place")
Expand Down Expand Up @@ -66,15 +66,15 @@ final class SingleHopEphemeralPeerExchangerTests: XCTestCase {
expectedNegotiationFailure.fulfill()
}

singleHopPostQuantumKeyExchanging.start()
await singleHopPostQuantumKeyExchanging.start()

wait(
for: [expectedNegotiationFailure, reconfigurationExpectation, negotiationSuccessful],
timeout: .UnitTest.invertedTimeout
)
}

func testEphemeralPeerExchangeSuccessWhenPostQuantumNegotiationStarts() throws {
func testEphemeralPeerExchangeSuccessWhenPostQuantumNegotiationStarts() async throws {
let unexpectedNegotiationFailure = expectation(description: "Negotiation failed.")
unexpectedNegotiationFailure.isInverted = true

Expand All @@ -101,17 +101,17 @@ final class SingleHopEphemeralPeerExchangerTests: XCTestCase {
}

keyExchangeActor.delegate = KeyExchangingResultStub(onReceivePostQuantumKey: { preSharedKey, ephemeralKey in
singleHopPostQuantumKeyExchanging.receivePostQuantumKey(preSharedKey, ephemeralKey: ephemeralKey)
await singleHopPostQuantumKeyExchanging.receivePostQuantumKey(preSharedKey, ephemeralKey: ephemeralKey)
})
singleHopPostQuantumKeyExchanging.start()
await singleHopPostQuantumKeyExchanging.start()

wait(
for: [unexpectedNegotiationFailure, reconfigurationExpectation, negotiationSuccessful],
timeout: .UnitTest.invertedTimeout
)
}

func testEphemeralPeerExchangeSuccessWhenDaitaNegotiationStarts() throws {
func testEphemeralPeerExchangeSuccessWhenDaitaNegotiationStarts() async throws {
let unexpectedNegotiationFailure = expectation(description: "Negotiation failed.")
unexpectedNegotiationFailure.isInverted = true

Expand All @@ -138,9 +138,9 @@ final class SingleHopEphemeralPeerExchangerTests: XCTestCase {
}

peerExchangeActor.delegate = KeyExchangingResultStub(onReceiveEphemeralPeerPrivateKey: { ephemeralKey in
multiHopPeerExchanger.receiveEphemeralPeerPrivateKey(ephemeralKey)
await multiHopPeerExchanger.receiveEphemeralPeerPrivateKey(ephemeralKey)
})
multiHopPeerExchanger.start()
await multiHopPeerExchanger.start()

wait(
for: [unexpectedNegotiationFailure, reconfigurationExpectation, negotiationSuccessful],
Expand Down

0 comments on commit af5b003

Please sign in to comment.