From 6d2ed0d1ec8543b4944b96b0eb350d8a41abe4b8 Mon Sep 17 00:00:00 2001 From: Jon Petersson Date: Tue, 26 Sep 2023 16:11:46 +0200 Subject: [PATCH] Add tests for PacketTunnelActor --- ios/MullvadVPN.xcodeproj/project.pbxproj | 92 ++--- ...ketTunnelActor+ConnectionMonitoring.swift} | 0 ...ift => PacketTunnelActor+ErrorState.swift} | 0 ...ift => PacketTunnelActor+Extensions.swift} | 0 ...wift => PacketTunnelActor+KeyPolicy.swift} | 0 ...cketTunnelActor+NetworkReachability.swift} | 0 ...c.swift => PacketTunnelActor+Public.swift} | 2 +- ...ift => PacketTunnelActor+SleepCycle.swift} | 0 .../{Actor.swift => PacketTunnelActor.swift} | 2 +- ios/PacketTunnelCore/Actor/State.swift | 6 +- ios/PacketTunnelCoreTests/ActorTests.swift | 102 ------ .../Mocks/DefaultPathObserverFake.swift | 2 +- .../PacketTunnelActorTests.swift | 340 ++++++++++++++++++ 13 files changed, 394 insertions(+), 152 deletions(-) rename ios/PacketTunnelCore/Actor/{Actor+ConnectionMonitoring.swift => PacketTunnelActor+ConnectionMonitoring.swift} (100%) rename ios/PacketTunnelCore/Actor/{Actor+ErrorState.swift => PacketTunnelActor+ErrorState.swift} (100%) rename ios/PacketTunnelCore/Actor/{Actor+Extensions.swift => PacketTunnelActor+Extensions.swift} (100%) rename ios/PacketTunnelCore/Actor/{Actor+KeyPolicy.swift => PacketTunnelActor+KeyPolicy.swift} (100%) rename ios/PacketTunnelCore/Actor/{Actor+NetworkReachability.swift => PacketTunnelActor+NetworkReachability.swift} (100%) rename ios/PacketTunnelCore/Actor/{Actor+Public.swift => PacketTunnelActor+Public.swift} (97%) rename ios/PacketTunnelCore/Actor/{Actor+SleepCycle.swift => PacketTunnelActor+SleepCycle.swift} (100%) rename ios/PacketTunnelCore/Actor/{Actor.swift => PacketTunnelActor.swift} (99%) delete mode 100644 ios/PacketTunnelCoreTests/ActorTests.swift create mode 100644 ios/PacketTunnelCoreTests/PacketTunnelActorTests.swift diff --git a/ios/MullvadVPN.xcodeproj/project.pbxproj b/ios/MullvadVPN.xcodeproj/project.pbxproj index 7a8db6966edd..c2000d755b68 100644 --- a/ios/MullvadVPN.xcodeproj/project.pbxproj +++ b/ios/MullvadVPN.xcodeproj/project.pbxproj @@ -89,11 +89,11 @@ 5835B7CC233B76CB0096D79F /* TunnelManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5835B7CB233B76CB0096D79F /* TunnelManager.swift */; }; 5838321B2AC1B18400EA2071 /* PacketTunnelActor+Mocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5838321A2AC1B18400EA2071 /* PacketTunnelActor+Mocks.swift */; }; 5838321D2AC1C54600EA2071 /* TaskSleepTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5838321C2AC1C54600EA2071 /* TaskSleepTests.swift */; }; - 5838321F2AC3160A00EA2071 /* Actor+KeyPolicy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5838321E2AC3160A00EA2071 /* Actor+KeyPolicy.swift */; }; - 583832212AC3174700EA2071 /* Actor+NetworkReachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832202AC3174700EA2071 /* Actor+NetworkReachability.swift */; }; - 583832232AC3181400EA2071 /* Actor+ErrorState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832222AC3181400EA2071 /* Actor+ErrorState.swift */; }; - 583832252AC318A100EA2071 /* Actor+ConnectionMonitoring.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832242AC318A100EA2071 /* Actor+ConnectionMonitoring.swift */; }; - 583832272AC3193600EA2071 /* Actor+SleepCycle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832262AC3193600EA2071 /* Actor+SleepCycle.swift */; }; + 5838321F2AC3160A00EA2071 /* PacketTunnelActor+KeyPolicy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5838321E2AC3160A00EA2071 /* PacketTunnelActor+KeyPolicy.swift */; }; + 583832212AC3174700EA2071 /* PacketTunnelActor+NetworkReachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832202AC3174700EA2071 /* PacketTunnelActor+NetworkReachability.swift */; }; + 583832232AC3181400EA2071 /* PacketTunnelActor+ErrorState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832222AC3181400EA2071 /* PacketTunnelActor+ErrorState.swift */; }; + 583832252AC318A100EA2071 /* PacketTunnelActor+ConnectionMonitoring.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832242AC318A100EA2071 /* PacketTunnelActor+ConnectionMonitoring.swift */; }; + 583832272AC3193600EA2071 /* PacketTunnelActor+SleepCycle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832262AC3193600EA2071 /* PacketTunnelActor+SleepCycle.swift */; }; 583832292AC3DF1300EA2071 /* Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583832282AC3DF1300EA2071 /* Command.swift */; }; 5838322B2AC3EF9600EA2071 /* CommandChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5838322A2AC3EF9600EA2071 /* CommandChannel.swift */; }; 583D86482A2678DC0060D63B /* DeviceStateAccessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583D86472A2678DC0060D63B /* DeviceStateAccessor.swift */; }; @@ -146,7 +146,7 @@ 586A950E290125F3007BAF2B /* ProductsRequestOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5846226426E0D9630035F7C2 /* ProductsRequestOperation.swift */; }; 586A950F29012BEE007BAF2B /* AddressCacheTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06AC114028F841390037AF9A /* AddressCacheTracker.swift */; }; 586C14582AC463BB00245C01 /* CommandChannelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 586C14572AC463BB00245C01 /* CommandChannelTests.swift */; }; - 586C145A2AC4735F00245C01 /* Actor+Public.swift in Sources */ = {isa = PBXBuildFile; fileRef = 586C14592AC4735F00245C01 /* Actor+Public.swift */; }; + 586C145A2AC4735F00245C01 /* PacketTunnelActor+Public.swift in Sources */ = {isa = PBXBuildFile; fileRef = 586C14592AC4735F00245C01 /* PacketTunnelActor+Public.swift */; }; 586E54FB27A2DF6D0029B88B /* SendTunnelProviderMessageOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 586E54FA27A2DF6D0029B88B /* SendTunnelProviderMessageOperation.swift */; }; 586E8DB82AAF4AC4007BF3DA /* Task+Duration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 586E8DB72AAF4AC4007BF3DA /* Task+Duration.swift */; }; 5871167F2910035700D41AAC /* PreferencesInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5871167E2910035700D41AAC /* PreferencesInteractor.swift */; }; @@ -416,11 +416,11 @@ 58FE25C62AA72779003D1918 /* PacketTunnelCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 58C7A4362A863F440060C66F /* PacketTunnelCore.framework */; }; 58FE25CB2AA727A9003D1918 /* libRelaySelector.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5898D29829017DAC00EB5EBA /* libRelaySelector.a */; }; 58FE25CE2AA72802003D1918 /* MullvadSettings.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 58B2FDD32AA71D2A003EB5C6 /* MullvadSettings.framework */; }; - 58FE25D42AA729B5003D1918 /* ActorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FE25D32AA729B5003D1918 /* ActorTests.swift */; }; + 58FE25D42AA729B5003D1918 /* PacketTunnelActorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FE25D32AA729B5003D1918 /* PacketTunnelActorTests.swift */; }; 58FE25D72AA72A8F003D1918 /* State.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5824030C2A811B0000163DE8 /* State.swift */; }; 58FE25D82AA72A8F003D1918 /* ConfigurationBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 583E60952A9F6D0800DC61EF /* ConfigurationBuilder.swift */; }; 58FE25D92AA72A8F003D1918 /* AutoCancellingTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58F3F3652AA086A400D3B0A4 /* AutoCancellingTask.swift */; }; - 58FE25DA2AA72A8F003D1918 /* Actor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58E9C3852A4EF1CB00CFDEAC /* Actor.swift */; }; + 58FE25DA2AA72A8F003D1918 /* PacketTunnelActor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58E9C3852A4EF1CB00CFDEAC /* PacketTunnelActor.swift */; }; 58FE25DB2AA72A8F003D1918 /* StartOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58ED3A132A7C199C0085CE65 /* StartOptions.swift */; }; 58FE25DC2AA72A8F003D1918 /* AnyTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58BDEBA02A9CA14B00F578F2 /* AnyTask.swift */; }; 58FE25DF2AA72A9B003D1918 /* RelaySelectorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5824037F2A827DF300163DE8 /* RelaySelectorProtocol.swift */; }; @@ -430,7 +430,7 @@ 58FE25EE2AA7764E003D1918 /* TunnelAdapterDummy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FE25ED2AA7764E003D1918 /* TunnelAdapterDummy.swift */; }; 58FE25F02AA77664003D1918 /* RelaySelectorStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FE25EF2AA77664003D1918 /* RelaySelectorStub.swift */; }; 58FE25F22AA77674003D1918 /* SettingsReaderStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FE25F12AA77674003D1918 /* SettingsReaderStub.swift */; }; - 58FE25F42AA9D730003D1918 /* Actor+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FE25F32AA9D730003D1918 /* Actor+Extensions.swift */; }; + 58FE25F42AA9D730003D1918 /* PacketTunnelActor+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FE25F32AA9D730003D1918 /* PacketTunnelActor+Extensions.swift */; }; 58FE65952AB1D90600E53CB5 /* MullvadTypes.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 58D223D5294C8E5E0029F5F8 /* MullvadTypes.framework */; }; 58FEEB58260B662E00A621A8 /* AutomaticKeyboardResponder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FEEB57260B662E00A621A8 /* AutomaticKeyboardResponder.swift */; }; 58FF23A32AB09BEE003A2AF2 /* DeviceChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF23A22AB09BEE003A2AF2 /* DeviceChecker.swift */; }; @@ -1131,11 +1131,11 @@ 5838318A27C40A3900000571 /* Pinger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pinger.swift; sourceTree = ""; }; 5838321A2AC1B18400EA2071 /* PacketTunnelActor+Mocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelActor+Mocks.swift"; sourceTree = ""; }; 5838321C2AC1C54600EA2071 /* TaskSleepTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskSleepTests.swift; sourceTree = ""; }; - 5838321E2AC3160A00EA2071 /* Actor+KeyPolicy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Actor+KeyPolicy.swift"; sourceTree = ""; }; - 583832202AC3174700EA2071 /* Actor+NetworkReachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Actor+NetworkReachability.swift"; sourceTree = ""; }; - 583832222AC3181400EA2071 /* Actor+ErrorState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Actor+ErrorState.swift"; sourceTree = ""; }; - 583832242AC318A100EA2071 /* Actor+ConnectionMonitoring.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Actor+ConnectionMonitoring.swift"; sourceTree = ""; }; - 583832262AC3193600EA2071 /* Actor+SleepCycle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Actor+SleepCycle.swift"; sourceTree = ""; }; + 5838321E2AC3160A00EA2071 /* PacketTunnelActor+KeyPolicy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelActor+KeyPolicy.swift"; sourceTree = ""; }; + 583832202AC3174700EA2071 /* PacketTunnelActor+NetworkReachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelActor+NetworkReachability.swift"; sourceTree = ""; }; + 583832222AC3181400EA2071 /* PacketTunnelActor+ErrorState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelActor+ErrorState.swift"; sourceTree = ""; }; + 583832242AC318A100EA2071 /* PacketTunnelActor+ConnectionMonitoring.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelActor+ConnectionMonitoring.swift"; sourceTree = ""; }; + 583832262AC3193600EA2071 /* PacketTunnelActor+SleepCycle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelActor+SleepCycle.swift"; sourceTree = ""; }; 583832282AC3DF1300EA2071 /* Command.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Command.swift; sourceTree = ""; }; 5838322A2AC3EF9600EA2071 /* CommandChannel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandChannel.swift; sourceTree = ""; }; 583D86472A2678DC0060D63B /* DeviceStateAccessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceStateAccessor.swift; sourceTree = ""; }; @@ -1198,7 +1198,7 @@ 586A95112901321B007BAF2B /* IPv6Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPv6Endpoint.swift; sourceTree = ""; }; 586A951329013235007BAF2B /* AnyIPEndpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyIPEndpoint.swift; sourceTree = ""; }; 586C14572AC463BB00245C01 /* CommandChannelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandChannelTests.swift; sourceTree = ""; }; - 586C14592AC4735F00245C01 /* Actor+Public.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Actor+Public.swift"; sourceTree = ""; }; + 586C14592AC4735F00245C01 /* PacketTunnelActor+Public.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelActor+Public.swift"; sourceTree = ""; }; 586E54FA27A2DF6D0029B88B /* SendTunnelProviderMessageOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendTunnelProviderMessageOperation.swift; sourceTree = ""; }; 586E7A2C2A987689006DAB1B /* SettingsReaderProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsReaderProtocol.swift; sourceTree = ""; }; 586E8DB72AAF4AC4007BF3DA /* Task+Duration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Task+Duration.swift"; sourceTree = ""; }; @@ -1370,8 +1370,8 @@ 58E7BA182A975DF70068EC3A /* RESTTransportProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RESTTransportProvider.swift; sourceTree = ""; }; 58E973DD24850EB600096F90 /* AsyncOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncOperation.swift; sourceTree = ""; }; 58E9C3832A4EF15300CFDEAC /* WireGuardAdapter+Async.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WireGuardAdapter+Async.swift"; sourceTree = ""; }; - 58E9C3852A4EF1CB00CFDEAC /* Actor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Actor.swift; sourceTree = ""; }; 58EC06792A8D208D00BEB973 /* TunnelDeviceInfoStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelDeviceInfoStub.swift; sourceTree = ""; }; + 58E9C3852A4EF1CB00CFDEAC /* PacketTunnelActor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PacketTunnelActor.swift; sourceTree = ""; }; 58EC067B2A8D2A0B00BEB973 /* NetworkCounters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkCounters.swift; sourceTree = ""; }; 58ECD29123F178FD004298B6 /* Screenshots.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Screenshots.xcconfig; sourceTree = ""; }; 58ED3A132A7C199C0085CE65 /* StartOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartOptions.swift; sourceTree = ""; }; @@ -1402,12 +1402,12 @@ 58FD5BEF24238EB300112C88 /* SKProduct+Formatting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SKProduct+Formatting.swift"; sourceTree = ""; }; 58FD5BF32428C67600112C88 /* InAppPurchaseButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InAppPurchaseButton.swift; sourceTree = ""; }; 58FDF2D82A0BA11900C2B061 /* DeviceCheckOperation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeviceCheckOperation.swift; sourceTree = ""; }; - 58FE25D32AA729B5003D1918 /* ActorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActorTests.swift; sourceTree = ""; }; + 58FE25D32AA729B5003D1918 /* PacketTunnelActorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PacketTunnelActorTests.swift; sourceTree = ""; }; 58FE25EB2AA77638003D1918 /* TunnelMonitorStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelMonitorStub.swift; sourceTree = ""; }; 58FE25ED2AA7764E003D1918 /* TunnelAdapterDummy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelAdapterDummy.swift; sourceTree = ""; }; 58FE25EF2AA77664003D1918 /* RelaySelectorStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelaySelectorStub.swift; sourceTree = ""; }; 58FE25F12AA77674003D1918 /* SettingsReaderStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsReaderStub.swift; sourceTree = ""; }; - 58FE25F32AA9D730003D1918 /* Actor+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Actor+Extensions.swift"; sourceTree = ""; }; + 58FE25F32AA9D730003D1918 /* PacketTunnelActor+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelActor+Extensions.swift"; sourceTree = ""; }; 58FE65922AB1CDE000E53CB5 /* DeviceCheck.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceCheck.swift; sourceTree = ""; }; 58FEEB57260B662E00A621A8 /* AutomaticKeyboardResponder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutomaticKeyboardResponder.swift; sourceTree = ""; }; 58FF23A22AB09BEE003A2AF2 /* DeviceChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceChecker.swift; sourceTree = ""; }; @@ -2202,26 +2202,26 @@ 5864AF802A9F52E3008BC928 /* Actor */ = { isa = PBXGroup; children = ( + 58BDEBA02A9CA14B00F578F2 /* AnyTask.swift */, + 58F3F3652AA086A400D3B0A4 /* AutoCancellingTask.swift */, 583832282AC3DF1300EA2071 /* Command.swift */, 5838322A2AC3EF9600EA2071 /* CommandChannel.swift */, - 58E9C3852A4EF1CB00CFDEAC /* Actor.swift */, - 586C14592AC4735F00245C01 /* Actor+Public.swift */, - 583832222AC3181400EA2071 /* Actor+ErrorState.swift */, - 5838321E2AC3160A00EA2071 /* Actor+KeyPolicy.swift */, - 583832262AC3193600EA2071 /* Actor+SleepCycle.swift */, - 583832242AC318A100EA2071 /* Actor+ConnectionMonitoring.swift */, - 583832202AC3174700EA2071 /* Actor+NetworkReachability.swift */, - 58FE25F32AA9D730003D1918 /* Actor+Extensions.swift */, - 58DDA18E2ABC32380039C360 /* Timings.swift */, - 5824030C2A811B0000163DE8 /* State.swift */, - 58342C032AAB61FB003BA12D /* State+Extensions.swift */, 583E60952A9F6D0800DC61EF /* ConfigurationBuilder.swift */, - 58BDEBA02A9CA14B00F578F2 /* AnyTask.swift */, - 586E8DB72AAF4AC4007BF3DA /* Task+Duration.swift */, - 58F3F3652AA086A400D3B0A4 /* AutoCancellingTask.swift */, - 58ED3A132A7C199C0085CE65 /* StartOptions.swift */, 580D6B892AB31AB400B2D6E0 /* NetworkPath+NetworkReachability.swift */, + 58E9C3852A4EF1CB00CFDEAC /* PacketTunnelActor.swift */, + 583832242AC318A100EA2071 /* PacketTunnelActor+ConnectionMonitoring.swift */, + 583832222AC3181400EA2071 /* PacketTunnelActor+ErrorState.swift */, + 58FE25F32AA9D730003D1918 /* PacketTunnelActor+Extensions.swift */, + 5838321E2AC3160A00EA2071 /* PacketTunnelActor+KeyPolicy.swift */, + 583832202AC3174700EA2071 /* PacketTunnelActor+NetworkReachability.swift */, + 586C14592AC4735F00245C01 /* PacketTunnelActor+Public.swift */, + 583832262AC3193600EA2071 /* PacketTunnelActor+SleepCycle.swift */, 58E7A0312AA0715100C57861 /* Protocols */, + 58ED3A132A7C199C0085CE65 /* StartOptions.swift */, + 5824030C2A811B0000163DE8 /* State.swift */, + 58342C032AAB61FB003BA12D /* State+Extensions.swift */, + 586E8DB72AAF4AC4007BF3DA /* Task+Duration.swift */, + 58DDA18E2ABC32380039C360 /* Timings.swift */, ); path = Actor; sourceTree = ""; @@ -2438,12 +2438,12 @@ 58C7A4432A863F490060C66F /* PacketTunnelCoreTests */ = { isa = PBXGroup; children = ( - 58FE25D32AA729B5003D1918 /* ActorTests.swift */, - 5838321C2AC1C54600EA2071 /* TaskSleepTests.swift */, - 58C7A46F2A8649ED0060C66F /* PingerTests.swift */, - 58092E532A8B832E00C3CC72 /* TunnelMonitorTests.swift */, 586C14572AC463BB00245C01 /* CommandChannelTests.swift */, 58EC067D2A8D2B0700BEB973 /* Mocks */, + 58FE25D32AA729B5003D1918 /* PacketTunnelActorTests.swift */, + 58C7A46F2A8649ED0060C66F /* PingerTests.swift */, + 5838321C2AC1C54600EA2071 /* TaskSleepTests.swift */, + 58092E532A8B832E00C3CC72 /* TunnelMonitorTests.swift */, ); path = PacketTunnelCoreTests; sourceTree = ""; @@ -3928,25 +3928,25 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 58FE25F42AA9D730003D1918 /* Actor+Extensions.swift in Sources */, + 58FE25F42AA9D730003D1918 /* PacketTunnelActor+Extensions.swift in Sources */, 58DDA18F2ABC32380039C360 /* Timings.swift in Sources */, 58FE25DF2AA72A9B003D1918 /* RelaySelectorProtocol.swift in Sources */, 58C7A4522A863FB50060C66F /* Pinger.swift in Sources */, 580D6B8C2AB3369300B2D6E0 /* BlockedStateErrorMapperProtocol.swift in Sources */, 58C7AF172ABD84AA007EDD7A /* ProxyURLRequest.swift in Sources */, 58C7AF142ABD8480007EDD7A /* RelaySelectorResult+PacketTunnelRelay.swift in Sources */, - 5838321F2AC3160A00EA2071 /* Actor+KeyPolicy.swift in Sources */, + 5838321F2AC3160A00EA2071 /* PacketTunnelActor+KeyPolicy.swift in Sources */, 58C7AF122ABD8480007EDD7A /* TunnelProviderReply.swift in Sources */, 58C7A4572A863FB90060C66F /* TunnelDeviceInfoProtocol.swift in Sources */, 58C7A4562A863FB90060C66F /* DefaultPathObserverProtocol.swift in Sources */, - 58FE25DA2AA72A8F003D1918 /* Actor.swift in Sources */, + 58FE25DA2AA72A8F003D1918 /* PacketTunnelActor.swift in Sources */, 58FE25E62AA738E8003D1918 /* TunnelAdapterProtocol.swift in Sources */, - 583832252AC318A100EA2071 /* Actor+ConnectionMonitoring.swift in Sources */, + 583832252AC318A100EA2071 /* PacketTunnelActor+ConnectionMonitoring.swift in Sources */, 58C7A4552A863FB90060C66F /* TunnelMonitor.swift in Sources */, 58C7AF182ABD84AB007EDD7A /* ProxyURLResponse.swift in Sources */, 58C7A4512A863FB50060C66F /* PingerProtocol.swift in Sources */, 583832292AC3DF1300EA2071 /* Command.swift in Sources */, - 583832232AC3181400EA2071 /* Actor+ErrorState.swift in Sources */, + 583832232AC3181400EA2071 /* PacketTunnelActor+ErrorState.swift in Sources */, 58C7AF112ABD8480007EDD7A /* TunnelProviderMessage.swift in Sources */, 58C7AF162ABD84A8007EDD7A /* URLRequestProxy.swift in Sources */, 58FE25D72AA72A8F003D1918 /* State.swift in Sources */, @@ -3954,15 +3954,15 @@ 58C7A4592A863FB90060C66F /* WgStats.swift in Sources */, 7A6B4F592AB8412E00123853 /* TunnelMonitorTimings.swift in Sources */, 58FE25DB2AA72A8F003D1918 /* StartOptions.swift in Sources */, - 583832212AC3174700EA2071 /* Actor+NetworkReachability.swift in Sources */, + 583832212AC3174700EA2071 /* PacketTunnelActor+NetworkReachability.swift in Sources */, 58FE25D82AA72A8F003D1918 /* ConfigurationBuilder.swift in Sources */, 580D6B8A2AB31AB400B2D6E0 /* NetworkPath+NetworkReachability.swift in Sources */, 5826B6CB2ABD83E200B1CA13 /* PacketTunnelOptions.swift in Sources */, 586E8DB82AAF4AC4007BF3DA /* Task+Duration.swift in Sources */, 5838322B2AC3EF9600EA2071 /* CommandChannel.swift in Sources */, - 586C145A2AC4735F00245C01 /* Actor+Public.swift in Sources */, + 586C145A2AC4735F00245C01 /* PacketTunnelActor+Public.swift in Sources */, 58342C042AAB61FB003BA12D /* State+Extensions.swift in Sources */, - 583832272AC3193600EA2071 /* Actor+SleepCycle.swift in Sources */, + 583832272AC3193600EA2071 /* PacketTunnelActor+SleepCycle.swift in Sources */, 58FE25DC2AA72A8F003D1918 /* AnyTask.swift in Sources */, 58C7AF152ABD8480007EDD7A /* PacketTunnelRelay.swift in Sources */, 58FE25D92AA72A8F003D1918 /* AutoCancellingTask.swift in Sources */, @@ -3988,7 +3988,7 @@ 581F23AF2A8CF94D00788AB6 /* PingerMock.swift in Sources */, 58FE25F22AA77674003D1918 /* SettingsReaderStub.swift in Sources */, 58F7753D2AB8473200425B47 /* BlockedStateErrorMapperStub.swift in Sources */, - 58FE25D42AA729B5003D1918 /* ActorTests.swift in Sources */, + 58FE25D42AA729B5003D1918 /* PacketTunnelActorTests.swift in Sources */, 58C7A4702A8649ED0060C66F /* PingerTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/ios/PacketTunnelCore/Actor/Actor+ConnectionMonitoring.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+ConnectionMonitoring.swift similarity index 100% rename from ios/PacketTunnelCore/Actor/Actor+ConnectionMonitoring.swift rename to ios/PacketTunnelCore/Actor/PacketTunnelActor+ConnectionMonitoring.swift diff --git a/ios/PacketTunnelCore/Actor/Actor+ErrorState.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift similarity index 100% rename from ios/PacketTunnelCore/Actor/Actor+ErrorState.swift rename to ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift diff --git a/ios/PacketTunnelCore/Actor/Actor+Extensions.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+Extensions.swift similarity index 100% rename from ios/PacketTunnelCore/Actor/Actor+Extensions.swift rename to ios/PacketTunnelCore/Actor/PacketTunnelActor+Extensions.swift diff --git a/ios/PacketTunnelCore/Actor/Actor+KeyPolicy.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift similarity index 100% rename from ios/PacketTunnelCore/Actor/Actor+KeyPolicy.swift rename to ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift diff --git a/ios/PacketTunnelCore/Actor/Actor+NetworkReachability.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+NetworkReachability.swift similarity index 100% rename from ios/PacketTunnelCore/Actor/Actor+NetworkReachability.swift rename to ios/PacketTunnelCore/Actor/PacketTunnelActor+NetworkReachability.swift diff --git a/ios/PacketTunnelCore/Actor/Actor+Public.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+Public.swift similarity index 97% rename from ios/PacketTunnelCore/Actor/Actor+Public.swift rename to ios/PacketTunnelCore/Actor/PacketTunnelActor+Public.swift index 7a50477c29d5..b54d397ca47d 100644 --- a/ios/PacketTunnelCore/Actor/Actor+Public.swift +++ b/ios/PacketTunnelCore/Actor/PacketTunnelActor+Public.swift @@ -1,5 +1,5 @@ // -// Actor+Public.swift +// PacketTunnelActor+Public.swift // PacketTunnelCore // // Created by pronebird on 27/09/2023. diff --git a/ios/PacketTunnelCore/Actor/Actor+SleepCycle.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor+SleepCycle.swift similarity index 100% rename from ios/PacketTunnelCore/Actor/Actor+SleepCycle.swift rename to ios/PacketTunnelCore/Actor/PacketTunnelActor+SleepCycle.swift diff --git a/ios/PacketTunnelCore/Actor/Actor.swift b/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift similarity index 99% rename from ios/PacketTunnelCore/Actor/Actor.swift rename to ios/PacketTunnelCore/Actor/PacketTunnelActor.swift index 18be2e2d409e..45f805eafd8b 100644 --- a/ios/PacketTunnelCore/Actor/Actor.swift +++ b/ios/PacketTunnelCore/Actor/PacketTunnelActor.swift @@ -1,5 +1,5 @@ // -// Actor.swift +// PacketTunnelActor.swift // PacketTunnel // // Created by pronebird on 30/06/2023. diff --git a/ios/PacketTunnelCore/Actor/State.swift b/ios/PacketTunnelCore/Actor/State.swift index 2c5cfd13b81f..836e742594ac 100644 --- a/ios/PacketTunnelCore/Actor/State.swift +++ b/ios/PacketTunnelCore/Actor/State.swift @@ -55,7 +55,11 @@ import class WireGuardKitTypes.PrivateKey `.connecting`, `.reconnecting`, `.error` can be interrupted if the tunnel is requested to stop, which should segue actor towards `.disconnected` state. */ -public enum State { +public enum State: Equatable { + public static func == (lhs: State, rhs: State) -> Bool { + lhs.name == rhs.name + } + /// Initial state at the time when actor is initialized but before the first connection attempt. case initial diff --git a/ios/PacketTunnelCoreTests/ActorTests.swift b/ios/PacketTunnelCoreTests/ActorTests.swift deleted file mode 100644 index 7b8cd6953875..000000000000 --- a/ios/PacketTunnelCoreTests/ActorTests.swift +++ /dev/null @@ -1,102 +0,0 @@ -// -// ActorTests.swift -// PacketTunnelCoreTests -// -// Created by pronebird on 05/09/2023. -// Copyright © 2023 Mullvad VPN AB. All rights reserved. -// - -import Combine -import MullvadSettings -import MullvadTypes -import Network -@testable import PacketTunnelCore -@testable import RelaySelector -import struct WireGuardKitTypes.IPAddressRange -import class WireGuardKitTypes.PrivateKey -import XCTest - -final class ActorTests: XCTestCase { - private var actor: PacketTunnelActor? - private var stateSink: Combine.Cancellable? - - override func tearDown() async throws { - stateSink?.cancel() - actor?.stop() - await actor?.waitUntilDisconnected() - } - - /** - Test a happy path start sequence. - - As actor should transition through the following states: .initial → .connecting → .connected - */ - func testStart() async throws { - let actor = PacketTunnelActor.mock() - let initialStateExpectation = expectation(description: "Expect initial state") - let connectingExpectation = expectation(description: "Expect connecting state") - let connectedStateExpectation = expectation(description: "Expect connected state") - - let allExpectations = [initialStateExpectation, connectingExpectation, connectedStateExpectation] - - stateSink = await actor.$state - .receive(on: DispatchQueue.main) - .sink { newState in - switch newState { - case .initial: - initialStateExpectation.fulfill() - case .connecting: - connectingExpectation.fulfill() - case .connected: - connectedStateExpectation.fulfill() - default: - break - } - } - - self.actor = actor - - actor.start(options: StartOptions(launchSource: .app)) - - await fulfillment(of: allExpectations, timeout: 1, enforceOrder: true) - } - - /** - Test stopping connected tunnel. - - As actor should transition through the following states: .connected → .disconnecting → .disconnected - */ - func testStopConnectedTunnel() async throws { - let actor = PacketTunnelActor.mock() - let connectedStateExpectation = expectation(description: "Expect connected state") - let disconnectingStateExpectation = expectation(description: "Expect disconnecting state") - let disconnectedStateExpectation = expectation(description: "Expect disconnected state") - - let allExpectations = [connectedStateExpectation, disconnectingStateExpectation, disconnectedStateExpectation] - - stateSink = await actor.$state - .receive(on: DispatchQueue.main) - .sink { newState in - switch newState { - case .connected: - connectedStateExpectation.fulfill() - actor.stop() - - case .disconnecting: - disconnectingStateExpectation.fulfill() - - case .disconnected: - disconnectedStateExpectation.fulfill() - - default: - break - } - } - - self.actor = actor - - actor.start(options: StartOptions(launchSource: .app)) - - await fulfillment(of: allExpectations, timeout: 1, enforceOrder: true) - } -} diff --git a/ios/PacketTunnelCoreTests/Mocks/DefaultPathObserverFake.swift b/ios/PacketTunnelCoreTests/Mocks/DefaultPathObserverFake.swift index 8031ea888c18..b237647b30f0 100644 --- a/ios/PacketTunnelCoreTests/Mocks/DefaultPathObserverFake.swift +++ b/ios/PacketTunnelCoreTests/Mocks/DefaultPathObserverFake.swift @@ -23,7 +23,7 @@ class DefaultPathObserverFake: DefaultPathObserverProtocol { private var innerPath: NetworkPath = NetworkPathStub() private var stateLock = NSLock() - private var defaultPathHandler: ((NetworkPath) -> Void)? + var defaultPathHandler: ((NetworkPath) -> Void)? func start(_ body: @escaping (NetworkPath) -> Void) { stateLock.withLock { diff --git a/ios/PacketTunnelCoreTests/PacketTunnelActorTests.swift b/ios/PacketTunnelCoreTests/PacketTunnelActorTests.swift new file mode 100644 index 000000000000..2b4861d494bc --- /dev/null +++ b/ios/PacketTunnelCoreTests/PacketTunnelActorTests.swift @@ -0,0 +1,340 @@ +// +// PacketTunnelActorTests.swift +// PacketTunnelCoreTests +// +// Created by pronebird on 05/09/2023. +// Copyright © 2023 Mullvad VPN AB. All rights reserved. +// + +import Combine +import MullvadSettings +import MullvadTypes +import Network +@testable import PacketTunnelCore +@testable import RelaySelector +import struct WireGuardKitTypes.IPAddressRange +import class WireGuardKitTypes.PrivateKey +import XCTest + +final class PacketTunnelActorTests: XCTestCase { + private var stateSink: Combine.Cancellable? + + override func tearDown() async throws { + stateSink?.cancel() + } + + /** + Test a happy path start sequence. + + As actor should transition through the following states: .initial → .connecting → .connected + */ + func testStartGoesToConnectedInSequence() async throws { + let actor = PacketTunnelActor.mock() + + // As actor starts it should transition through the following states based on simulation: + // .initial → .connecting → .connected + let initialStateExpectation = expectation(description: "Expect initial state") + let connectingExpectation = expectation(description: "Expect connecting state") + let connectedStateExpectation = expectation(description: "Expect connected state") + + let allExpectations = [initialStateExpectation, connectingExpectation, connectedStateExpectation] + + stateSink = await actor.$state + .receive(on: DispatchQueue.main) + .sink { newState in + switch newState { + case .initial: + initialStateExpectation.fulfill() + case .connecting: + connectingExpectation.fulfill() + case .connected: + connectedStateExpectation.fulfill() + default: + break + } + } + + actor.start(options: StartOptions(launchSource: .app)) + + await fulfillment(of: allExpectations, timeout: 1, enforceOrder: true) + } + + func testStartIgnoresSubsequentStarts() async throws { + let actor = PacketTunnelActor.mock() + + // As actor starts it should transition through the following states based on simulation: + // .initial → .connecting → .connected + let initialStateExpectation = expectation(description: "Expect initial state") + let connectingExpectation = expectation(description: "Expect connecting state") + let connectedStateExpectation = expectation(description: "Expect connected state") + + let allExpectations = [initialStateExpectation, connectingExpectation, connectedStateExpectation] + + stateSink = await actor.$state + .receive(on: DispatchQueue.main) + .sink { newState in + switch newState { + case .initial: + initialStateExpectation.fulfill() + case .connecting: + connectingExpectation.fulfill() + case .connected: + connectedStateExpectation.fulfill() + default: + break + } + } + + actor.start(options: StartOptions(launchSource: .app)) + actor.start(options: StartOptions(launchSource: .app)) + + await fulfillment(of: allExpectations, timeout: 1, enforceOrder: true) + } + + /** + Test start sequence when reading settings yields an error indicating that device is locked. + This is common when network extenesion starts on boot with iOS. + + 1. The first attempt to read settings yields an error indicating that device is locked. + 2. An actor should set up a task to reconnect the tunnel periodically. + 3. The issue goes away on the second attempt to read settings. + 4. An actor should transition through `.connecting` towards`.connected` state. + */ + func testLockedDeviceErrorOnBoot() async throws { + let initialStateExpectation = expectation(description: "Expect initial state") + let errorStateExpectation = expectation(description: "Expect error state") + let connectingStateExpectation = expectation(description: "Expect connecting state") + let connectedStateExpectation = expectation(description: "Expect connected state") + let allExpectations = [ + initialStateExpectation, + errorStateExpectation, + connectingStateExpectation, + connectedStateExpectation, + ] + + let blockedStateMapper = BlockedStateErrorMapperStub { error in + if let error = error as? POSIXError, error.code == .EPERM { + return .deviceLocked + } else { + return .unknown + } + } + + var isFirstReadAttempt = true + let settingsReader = SettingsReaderStub { + if isFirstReadAttempt { + isFirstReadAttempt = false + throw POSIXError(.EPERM) + } else { + return Settings( + privateKey: PrivateKey(), + interfaceAddresses: [IPAddressRange(from: "127.0.0.1/32")!], + relayConstraints: RelayConstraints(), + dnsServers: .gateway + ) + } + } + + let actor = PacketTunnelActor.mock(blockedStateErrorMapper: blockedStateMapper, settingsReader: settingsReader) + + stateSink = await actor.$state + .receive(on: DispatchQueue.main) + .sink { newState in + switch newState { + case .initial: + initialStateExpectation.fulfill() + case .error: + errorStateExpectation.fulfill() + case .connecting: + connectingStateExpectation.fulfill() + case .connected: + connectedStateExpectation.fulfill() + default: + break + } + } + + actor.start(options: StartOptions(launchSource: .app)) + + await fulfillment(of: allExpectations, timeout: 1, enforceOrder: true) + } + + func testStopGoesToDisconnected() async throws { + let actor = PacketTunnelActor.mock() + let disconnectedStateExpectation = expectation(description: "Expect disconnected state") + let connectedStateExpectation = expectation(description: "Expect connected state") + + let expression: (State) -> Bool = { if case .connected = $0 { true } else { false } } + + await expect(expression, on: actor) { + connectedStateExpectation.fulfill() + } + + // Wait for the connected state to happen so it doesn't get coalesced immediately after the call to `actor.stop` + actor.start(options: StartOptions(launchSource: .app)) + await fulfillment(of: [connectedStateExpectation], timeout: 1) + + await expect(.disconnected, on: actor) { + disconnectedStateExpectation.fulfill() + } + actor.stop() + await fulfillment(of: [disconnectedStateExpectation], timeout: 1) + } + + func testStopIsNoopBeforeStart() async throws { + let actor = PacketTunnelActor.mock() + + actor.stop() + actor.stop() + actor.stop() + + switch await actor.state { + case .initial: break + default: XCTFail("Actor did not start, should be in .initial state") + } + } + + func testStopCancelsDefaultPathObserver() async throws { + let pathObserver = DefaultPathObserverFake() + let actor = PacketTunnelActor.mock(defaultPathObserver: pathObserver) + let connectedStateExpectation = expectation(description: "Connected state") + + let expression: (State) -> Bool = { if case .connected = $0 { true } else { false } } + + await expect(expression, on: actor) { + connectedStateExpectation.fulfill() + } + + actor.start(options: StartOptions(launchSource: .app)) + await fulfillment(of: [connectedStateExpectation], timeout: 1) + + let disconnectedStateExpectation = expectation(description: "Disconnected state") + + await expect(.disconnected, on: actor) { + disconnectedStateExpectation.fulfill() + } + actor.stop() + await fulfillment(of: [disconnectedStateExpectation], timeout: 1) + + XCTAssertNil(pathObserver.defaultPathHandler) + } + + func testSetErrorStateGetsCancelled() async throws { + let actor = PacketTunnelActor.mock() + let connectingStateExpectation = expectation(description: "Connecting state") + let disconnectedStateExpectation = expectation(description: "Disconnected state") + + stateSink = await actor.$state + .receive(on: DispatchQueue.main) + .sink { newState in + switch newState { + case .connecting: + // Guarantee that the task doesn't set the actor to error state before it's cancelled + let task = Task.detached { + try await Task.sleep(duration: .seconds(1)) + actor.setErrorState(reason: .readSettings) + } + task.cancel() + connectingStateExpectation.fulfill() + case .error: + XCTFail("Should not go to error state") + case .disconnected: + disconnectedStateExpectation.fulfill() + default: + break + } + } + + actor.start(options: StartOptions(launchSource: .app)) + await fulfillment(of: [connectingStateExpectation], timeout: 1) + actor.stop() + await fulfillment(of: [disconnectedStateExpectation], timeout: 1) + } + + func testReconnectIsNoopBeforeConnecting() async throws { + let actor = PacketTunnelActor.mock() + let initialStateExpectation = expectation(description: "Expect initial state") + + stateSink = await actor.$state.receive(on: DispatchQueue.main).sink { newState in + if case .initial = newState { + initialStateExpectation.fulfill() + return + } + XCTFail("Should not change states before starting the actor") + } + + actor.reconnect(to: .random) + + await fulfillment(of: [initialStateExpectation], timeout: 1) + } + + func testCannotReconnectAfterStopping() async throws { + let actor = PacketTunnelActor.mock() + + let disconnectedStateExpectation = expectation(description: "Expect disconnected state") + + await expect(.disconnected, on: actor) { + disconnectedStateExpectation.fulfill() + } + + actor.start(options: StartOptions(launchSource: .app)) + actor.stop() + + await fulfillment(of: [disconnectedStateExpectation], timeout: 1) + + await expect(.initial, on: actor) { + XCTFail("Should not be trying to reconnect after stopping") + } + actor.reconnect(to: .random) + } + + func testReconnectionStopsTunnelMonitor() async throws { + let stopMonitorExpectation = expectation(description: "Tunnel monitor stop") + + let tunnelMonitor = TunnelMonitorStub { command, dispatcher in + switch command { + case .start: + dispatcher.send(.connectionEstablished, after: .milliseconds(10)) + case .stop: + stopMonitorExpectation.fulfill() + } + } + let actor = PacketTunnelActor.mock(tunnelMonitor: tunnelMonitor) + let connectedExpectation = expectation(description: "Expect connected state") + + let expression: (State) -> Bool = { if case .connected = $0 { return true } else { return false } } + await expect(expression, on: actor) { + connectedExpectation.fulfill() + } + actor.start(options: StartOptions(launchSource: .app)) + await fulfillment(of: [connectedExpectation], timeout: 1) + + // Cancel the state sink to avoid overfulfilling the connected expectation + stateSink?.cancel() + + actor.reconnect(to: .random) + await fulfillment(of: [stopMonitorExpectation], timeout: 1) + } +} + +extension PacketTunnelActorTests { + func expect(_ state: State, on actor: PacketTunnelActor, _ action: @escaping () -> Void) async { + stateSink = await actor.$state.receive(on: DispatchQueue.main).sink { newState in + if state == newState { + action() + } + } + } + + func expect( + _ expression: @escaping (State) -> Bool, + on actor: PacketTunnelActor, + _ action: @escaping () -> Void + ) async { + stateSink = await actor.$state.receive(on: DispatchQueue.main).sink { newState in + if expression(newState) { + action() + } + } + } +}