Skip to content

Commit

Permalink
fixup: Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkisemils committed Dec 20, 2024
1 parent ff1788d commit 506b06d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
negotiationSuccessful.fulfill()
}

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

let connectionState = stubConnectionState(enableMultiHop: false, enablePostQuantum: true, enableDaita: false)
await postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())
Expand Down Expand Up @@ -107,9 +112,13 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
negotiationSuccessful.fulfill()
}

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

let connectionState = stubConnectionState(enableMultiHop: false, enablePostQuantum: false, enableDaita: true)
await postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())
Expand Down Expand Up @@ -137,9 +146,14 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
negotiationSuccessful.fulfill()
}

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

let connectionState = stubConnectionState(enableMultiHop: true, enablePostQuantum: true, enableDaita: false)
await postQuantumKeyExchangingPipeline.startNegotiation(connectionState, privateKey: PrivateKey())
Expand Down Expand Up @@ -167,8 +181,8 @@ final class EphemeralPeerExchangingPipelineTests: XCTestCase {
negotiationSuccessful.fulfill()
}

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

let connectionState = stubConnectionState(enableMultiHop: true, enablePostQuantum: false, enableDaita: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,25 @@ final class EphemeralPeerExchangeActorStub: EphemeralPeerExchangeActorProtocol {
var delegate: EphemeralPeerReceiving?

func startNegotiation(with privateKey: PrivateKey, enablePostQuantum: Bool, enableDaita: Bool) {
let daita = enableDaita
? DaitaV2Parameters(
machines: "test",
maximumEvents: 1,
maximumActions: 1,
maximumPadding: 1.0,
maximumBlocking: 1.0
)
: nil
switch result {
case let .success((preSharedKey, ephemeralKey)):
if enablePostQuantum {
Task { await delegate?.receivePostQuantumKey(preSharedKey, ephemeralKey: ephemeralKey) }
Task { await delegate?.receivePostQuantumKey(
preSharedKey,
ephemeralKey: ephemeralKey,
daitaParameters: daita
) }
} else {
Task { await delegate?.receiveEphemeralPeerPrivateKey(ephemeralKey) }
Task { await delegate?.receiveEphemeralPeerPrivateKey(ephemeralKey, daitaParameters: daita) }
}
case .failure:
delegate?.ephemeralPeerExchangeFailed()
Expand Down
19 changes: 13 additions & 6 deletions ios/PacketTunnelCoreTests/Mocks/KeyExchangingResultStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@

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

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

public func receiveEphemeralPeerPrivateKey(_ ephemeralPeerPrivateKey: PrivateKey) async {
await onReceiveEphemeralPeerPrivateKey?(ephemeralPeerPrivateKey)
public func receiveEphemeralPeerPrivateKey(
_ ephemeralPeerPrivateKey: PrivateKey,
daitaParameters daitaParamters: MullvadTypes.DaitaV2Parameters?
) async {
await onReceiveEphemeralPeerPrivateKey?(ephemeralPeerPrivateKey, daitaParamters)
}

func ephemeralPeerExchangeFailed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,14 @@ final class MultiHopEphemeralPeerExchangerTests: XCTestCase {
negotiationSuccessful.fulfill()
}

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

wait(
Expand Down Expand Up @@ -161,8 +166,8 @@ final class MultiHopEphemeralPeerExchangerTests: XCTestCase {
negotiationSuccessful.fulfill()
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,14 @@ final class SingleHopEphemeralPeerExchangerTests: XCTestCase {
negotiationSuccessful.fulfill()
}

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

wait(
Expand Down Expand Up @@ -137,8 +142,8 @@ final class SingleHopEphemeralPeerExchangerTests: XCTestCase {
negotiationSuccessful.fulfill()
}

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

Expand Down

0 comments on commit 506b06d

Please sign in to comment.