diff --git a/Tests/NetworkHelperTests/ConnectToEthernetTests.cs b/Tests/NetworkHelperTests/ConnectToEthernetTests.cs index 7a55754..20f13bf 100644 --- a/Tests/NetworkHelperTests/ConnectToEthernetTests.cs +++ b/Tests/NetworkHelperTests/ConnectToEthernetTests.cs @@ -94,8 +94,9 @@ public void TestSingleUsage() [TestMethod] public void TestRetryAfterTimeout() { - // First attempt: very short timeout so it expires - CancellationTokenSource cs1 = new(1000); + // First attempt: pre-cancelled token guarantees timeout regardless of network state + CancellationTokenSource cs1 = new(); + cs1.Cancel(); var firstResult = NetworkHelper.SetupAndConnectNetwork(token: cs1.Token); Assert.IsFalse(firstResult, "First call should have timed out"); diff --git a/Tests/SocketTests/SocketExceptionsTests.cs b/Tests/SocketTests/SocketExceptionsTests.cs index 76de8c1..a90ca4f 100644 --- a/Tests/SocketTests/SocketExceptionsTests.cs +++ b/Tests/SocketTests/SocketExceptionsTests.cs @@ -46,7 +46,7 @@ public void SocketExceptionTest3_Protocol_Address_FamilyNotSupported() } catch (SocketException e) { - Assert.IsFalse(e.ErrorCode != (int)SocketError.ProtocolFamilyNotSupported && e.ErrorCode != (int)SocketError.AddressFamilyNotSupported, "Incorrect ErrorCode in SocketException " + Assert.IsFalse(e.ErrorCode != (int)SocketError.ProtocolFamilyNotSupported && e.ErrorCode != (int)SocketError.AddressFamilyNotSupported && e.ErrorCode != (int)SocketError.ProtocolNotSupported, "Incorrect ErrorCode in SocketException " + e.ErrorCode); return; } @@ -57,7 +57,6 @@ public void SocketExceptionTest3_Protocol_Address_FamilyNotSupported() [TestMethod] public void SocketExceptionTest4_ProtocolNotSupported() { - try { Socket socketTest = new Socket(AddressFamily.InterNetwork, @@ -65,8 +64,7 @@ public void SocketExceptionTest4_ProtocolNotSupported() } catch (SocketException e) { - Assert.AreEqual((int)SocketError.ProtocolNotSupported, e.ErrorCode, "Incorrect ErrorCode in SocketException " - + e.ErrorCode); + Assert.AreEqual((int)SocketError.ProtocolNotSupported, e.ErrorCode, "Incorrect ErrorCode in SocketException " + e.ErrorCode); return; } throw new Exception("No SocketException thrown"); @@ -112,16 +110,18 @@ public void SocketExceptionTest11_AccessDenied() public void SocketExceptionTest12_NotConnected() { SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); + Socket socketTemp = null; Assert.ThrowsException(typeof(SocketException), () => { testSockets.Startup(0, 0); - Socket socketTemp = new Socket(AddressFamily.InterNetwork, + socketTemp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socketTemp.Bind(testSockets.socketServer.RemoteEndPoint); socketTemp.Send(new byte[2]); }); + socketTemp?.Close(); testSockets.TearDown(); } @@ -146,17 +146,24 @@ public void SocketExceptionTest13_InvalidArgument() [TestMethod] public void SocketExceptionTest14_AddressNotAvailable() { + // Bind to an IP not assigned to any local interface, then connect to force address validation. + // nanoFramework's LWIP stack defers the AddressNotAvailable error to connect time, not bind time. SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); + Socket freshSocket = null; + Assert.ThrowsException(typeof(SocketException), () => { - int clientPort = SocketTools.nextPort; int serverPort = SocketTools.nextPort; - int tempPort = clientPort; + int clientPort = SocketTools.nextPort; testSockets.Startup(clientPort, serverPort); + testSockets.socketServer.Listen(1); - testSockets.socketClient.Bind(new IPEndPoint(new IPAddress(SocketTools.DottedDecimalToIp((byte)192, (byte)168, (byte)192, (byte)168)), tempPort)); + freshSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + freshSocket.Bind(new IPEndPoint(new IPAddress(SocketTools.DottedDecimalToIp((byte)192, (byte)168, (byte)192, (byte)168)), SocketTools.nextPort)); + freshSocket.Connect(testSockets.epServer); }); + freshSocket?.Close(); testSockets.TearDown(); }