Files

24 lines
1.2 KiB
C#

using System.Net;
using System.Net.Sockets;
using Ads.Desktop;
using Xunit;
namespace Ads.Desktop.Tests;
public class ConnectionPortsTests
{
[Fact] public void AllocationSkipsReservedAndForeignListenerWithoutClosingIt()
{
var occupied = new TcpListener(IPAddress.Loopback, 0); occupied.Start();
try {
var first = ((IPEndPoint)occupied.LocalEndpoint).Port;
var a = ConnectionPorts.Allocate(first, [first + 1]);
var b = ConnectionPorts.Allocate(first, [first + 1, a]);
Assert.NotEqual(first, a); Assert.NotEqual(first + 1, a); Assert.NotEqual(a, b);
Assert.True(occupied.Server.IsBound);
} finally { occupied.Stop(); }
}
[Fact] public void AdbServerPortIsNeverAllocated() => Assert.NotEqual(5037, ConnectionPorts.Allocate(5037, []));
[Fact] public void ExhaustedRangeFailsWithoutReusingReservedPort() => Assert.Throws<InvalidOperationException>(() => ConnectionPorts.Allocate(65535, [65535]));
[Theory] [InlineData(0)] [InlineData(65536)]
public void InvalidBasePortFails(int port) => Assert.Throws<InvalidOperationException>(() => ConnectionPorts.Allocate(port, []));
}