Skip to content

Commit 99fd114

Browse files
shanselmanCopilot
andcommitted
test: keep testhost listeners on loopback
Co-authored-by: Copilot App <[email protected]>
1 parent 603886a commit 99fd114

6 files changed

Lines changed: 106 additions & 30 deletions

File tree

‎docs/TEST_COVERAGE.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ dotnet test --logger "console;verbosity=detailed"
118118
In a fresh worktree, run the project once without `--no-restore` or build it
119119
first so `dotnet test --no-restore` cannot no-op before `bin\` exists.
120120

121+
Test-owned TCP listeners must bind only to loopback addresses. A successful
122+
wildcard bind from `testhost.exe` can trigger a per-worktree Windows Defender
123+
Firewall consent dialog and block unattended validation. Tests for production
124+
LAN-bind conflict handling should occupy loopback first so the production
125+
wildcard bind fails without opening a network-reachable listener.
126+
121127
## Not fully covered by automated tests
122128

123129
- Real shell tray hover/click behavior against Explorer.

‎tests/OpenClaw.E2ETests/Setup/E2ESetupFixture.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ private void WritePortAllocationArtifact(MirroredWslPortLease lease)
386386
{
387387
"outside-windows-dynamic-range",
388388
"outside-windows-excluded-ranges",
389-
"exclusive-ipv4-ipv6-bind-probe",
389+
"exclusive-ipv4-ipv6-loopback-bind-probe",
390390
"cross-process-fixture-lease",
391391
},
392392
};

‎tests/OpenClaw.E2ETests/Setup/MirroredWslPortLease.cs‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ internal sealed class MirroredWslPortLease : IDisposable
210210
public const int CandidateRangeStart = 20_000;
211211
public const int CandidateRangeEnd = 39_999;
212212
private const int CandidateCount = CandidateRangeEnd - CandidateRangeStart + 1;
213+
internal static IReadOnlyList<IPAddress> BindProbeAddresses { get; } =
214+
Socket.OSSupportsIPv6
215+
? [IPAddress.Loopback, IPAddress.IPv6Loopback]
216+
: [IPAddress.Loopback];
213217

214218
private FileStream? _leaseStream;
215219

@@ -234,7 +238,7 @@ public static MirroredWslPortLease Acquire()
234238
return Acquire(
235239
CreateCandidateSequence(startOffset),
236240
windowsPortState,
237-
CanBindExclusively,
241+
CanBindLoopbackExclusively,
238242
leaseDirectory);
239243
}
240244

@@ -308,22 +312,17 @@ public void Dispose()
308312
Interlocked.Exchange(ref _leaseStream, null)?.Dispose();
309313
}
310314

311-
private static bool CanBindExclusively(int port)
315+
internal static bool CanBindLoopbackExclusively(int port)
312316
{
313-
TcpListener? ipv4 = null;
314-
TcpListener? ipv6 = null;
317+
var listeners = new List<TcpListener>(BindProbeAddresses.Count);
315318
try
316319
{
317-
ipv4 = new TcpListener(IPAddress.Any, port);
318-
ipv4.Server.ExclusiveAddressUse = true;
319-
ipv4.Start();
320-
321-
if (Socket.OSSupportsIPv6)
320+
foreach (var address in BindProbeAddresses)
322321
{
323-
ipv6 = new TcpListener(IPAddress.IPv6Any, port);
324-
ipv6.Server.DualMode = false;
325-
ipv6.Server.ExclusiveAddressUse = true;
326-
ipv6.Start();
322+
var listener = new TcpListener(address, port);
323+
listeners.Add(listener);
324+
listener.Server.ExclusiveAddressUse = true;
325+
listener.Start();
327326
}
328327

329328
return true;
@@ -334,8 +333,8 @@ private static bool CanBindExclusively(int port)
334333
}
335334
finally
336335
{
337-
ipv6?.Stop();
338-
ipv4?.Stop();
336+
foreach (var listener in listeners)
337+
listener.Stop();
339338
}
340339
}
341340
}

‎tests/OpenClaw.E2ETests/Setup/MirroredWslPortLeaseTests.cs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Diagnostics;
2+
using System.Net;
3+
using System.Net.Sockets;
24

35
namespace OpenClaw.E2ETests.Setup;
46

@@ -20,6 +22,53 @@ public void CandidateSequence_CoversOnlyConfiguredLowRangeWithoutDuplicates()
2022
Assert.Equal(MirroredWslPortLease.CandidateRangeStart + 137, candidates[0]);
2123
}
2224

25+
[Fact]
26+
public void BindProbe_UsesOnlyLoopbackAddressesAndReleasesThePort()
27+
{
28+
Assert.Equal(IPAddress.Loopback, MirroredWslPortLease.BindProbeAddresses[0]);
29+
Assert.All(MirroredWslPortLease.BindProbeAddresses, address =>
30+
{
31+
Assert.True(IPAddress.IsLoopback(address));
32+
Assert.NotEqual(IPAddress.Any, address);
33+
Assert.NotEqual(IPAddress.IPv6Any, address);
34+
});
35+
36+
var reservation = new TcpListener(IPAddress.Loopback, 0);
37+
reservation.Start();
38+
var port = ((IPEndPoint)reservation.LocalEndpoint).Port;
39+
reservation.Stop();
40+
41+
Assert.True(MirroredWslPortLease.CanBindLoopbackExclusively(port));
42+
43+
var rebound = new TcpListener(IPAddress.Loopback, port);
44+
rebound.Server.ExclusiveAddressUse = true;
45+
rebound.Start();
46+
rebound.Stop();
47+
}
48+
49+
[Fact]
50+
public void BindProbe_RejectsIpv6LoopbackCollision()
51+
{
52+
if (!Socket.OSSupportsIPv6)
53+
return;
54+
55+
var ipv6Reservation = new TcpListener(IPAddress.IPv6Loopback, 0);
56+
ipv6Reservation.Server.ExclusiveAddressUse = true;
57+
ipv6Reservation.Start();
58+
var port = ((IPEndPoint)ipv6Reservation.LocalEndpoint).Port;
59+
60+
try
61+
{
62+
Assert.False(MirroredWslPortLease.CanBindLoopbackExclusively(port));
63+
}
64+
finally
65+
{
66+
ipv6Reservation.Stop();
67+
}
68+
69+
Assert.True(MirroredWslPortLease.CanBindLoopbackExclusively(port));
70+
}
71+
2372
[Fact]
2473
public void WindowsPortState_ParsesDynamicAndExcludedNetshRanges()
2574
{

‎tests/OpenClaw.E2ETests/Setup/SshOwnershipAdversarialProofTests.cs‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -872,12 +872,12 @@ private static int AllocateFreeForwardPortPair()
872872
for (var attempt = 0; attempt < 100; attempt++)
873873
{
874874
var candidate = Random.Shared.Next(20_000, 40_000);
875-
TcpListener? gatewayForward = null;
876-
TcpListener? browserForward = null;
875+
IReadOnlyList<TcpListener>? gatewayForward = null;
876+
IReadOnlyList<TcpListener>? browserForward = null;
877877
try
878878
{
879-
gatewayForward = StartExclusiveDualStackListener(candidate);
880-
browserForward = StartExclusiveDualStackListener(candidate + 2);
879+
gatewayForward = StartExclusiveLoopbackListeners(candidate);
880+
browserForward = StartExclusiveLoopbackListeners(candidate + 2);
881881
return candidate;
882882
}
883883
catch (SocketException)
@@ -886,21 +886,43 @@ private static int AllocateFreeForwardPortPair()
886886
}
887887
finally
888888
{
889-
browserForward?.Stop();
890-
gatewayForward?.Stop();
889+
StopListeners(browserForward);
890+
StopListeners(gatewayForward);
891891
}
892892
}
893893

894894
throw new InvalidOperationException("Unable to allocate an SSH forward port pair.");
895895
}
896896

897-
private static TcpListener StartExclusiveDualStackListener(int port)
897+
private static IReadOnlyList<TcpListener> StartExclusiveLoopbackListeners(int port)
898898
{
899-
var listener = new TcpListener(IPAddress.IPv6Any, port);
900-
listener.Server.DualMode = true;
901-
listener.Server.ExclusiveAddressUse = true;
902-
listener.Start();
903-
return listener;
899+
var listeners = new List<TcpListener>(MirroredWslPortLease.BindProbeAddresses.Count);
900+
try
901+
{
902+
foreach (var address in MirroredWslPortLease.BindProbeAddresses)
903+
{
904+
var listener = new TcpListener(address, port);
905+
listeners.Add(listener);
906+
listener.Server.ExclusiveAddressUse = true;
907+
listener.Start();
908+
}
909+
910+
return listeners;
911+
}
912+
catch
913+
{
914+
StopListeners(listeners);
915+
throw;
916+
}
917+
}
918+
919+
private static void StopListeners(IReadOnlyList<TcpListener>? listeners)
920+
{
921+
if (listeners is null)
922+
return;
923+
924+
foreach (var listener in listeners)
925+
listener.Stop();
904926
}
905927

906928
private sealed record ProcessResult(int ExitCode, string Stdout, string Stderr);

‎tests/OpenClaw.SetupEngine.Tests/SetupStepsTests.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,9 @@ public async Task PreflightPort_Loopback_SucceedsForAvailablePort()
980980
}
981981

982982
[Fact]
983-
public async Task PreflightPort_Lan_FailsWhenAnyBindPortInUse()
983+
public async Task PreflightPort_Lan_FailsWhenLoopbackPortInUse()
984984
{
985-
var listener = new TcpListener(IPAddress.Any, 0)
985+
var listener = new TcpListener(IPAddress.Loopback, 0)
986986
{
987987
ExclusiveAddressUse = true
988988
};

0 commit comments

Comments
 (0)