diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index b61bd2b682..1e3c937a14 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -8,10 +8,15 @@ Additional documentation and release notes are available at [Multiplayer Documen ## [Unreleased] +### Added + ### Fixed +- Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496) - Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495) +## Changed + ## [1.4.0] ### Added diff --git a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs index 25878cd7eb..12f9843f67 100644 --- a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs +++ b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs @@ -536,6 +536,13 @@ private bool ClientBindAndConnect() serverEndpoint = ConnectionData.ServerEndPoint; } + // Verify the endpoint is valid before proceeding + if (serverEndpoint.Family == NetworkFamily.Invalid) + { + Debug.LogError($"Target server network address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!"); + return false; + } + InitDriver(); var bindEndpoint = serverEndpoint.Family == NetworkFamily.Ipv6 ? NetworkEndpoint.AnyIpv6 : NetworkEndpoint.AnyIpv4; @@ -554,6 +561,13 @@ private bool ClientBindAndConnect() private bool ServerBindAndListen(NetworkEndpoint endPoint) { + // Verify the endpoint is valid before proceeding + if (endPoint.Family == NetworkFamily.Invalid) + { + Debug.LogError($"Network listen address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!"); + return false; + } + InitDriver(); int result = m_Driver.Bind(endPoint); diff --git a/com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeLogAssert.cs b/com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeLogAssert.cs index d6e4136bc9..3fab026dd0 100644 --- a/com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeLogAssert.cs +++ b/com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeLogAssert.cs @@ -3,10 +3,11 @@ using System.Text.RegularExpressions; using NUnit.Framework; using UnityEngine; +using UnityEngine.TestTools; namespace Unity.Netcode.RuntimeTests { - public class NetcodeLogAssert + public class NetcodeLogAssert : IDisposable { private struct LogData { @@ -20,8 +21,11 @@ private struct LogData private List AllLogs { get; } - public NetcodeLogAssert() + private bool m_ResetIgnoreFailingMessagesOnTearDown; + public NetcodeLogAssert(bool ignorFailingMessages = false, bool resetOnTearDown = true) { + LogAssert.ignoreFailingMessages = ignorFailingMessages; + m_ResetIgnoreFailingMessagesOnTearDown = resetOnTearDown; AllLogs = new List(); Activate(); } @@ -51,6 +55,16 @@ public void AddLog(string message, string stacktrace, LogType type) } } + [UnityTearDown] + public void OnTearDown() + { + // Defaults to true and will reset LogAssert.ignoreFailingMessages during tear down + if (m_ResetIgnoreFailingMessagesOnTearDown) + { + LogAssert.ignoreFailingMessages = false; + } + } + public void Dispose() { Dispose(true); @@ -59,6 +73,8 @@ public void Dispose() private void Dispose(bool disposing) { + // Always reset when disposing + LogAssert.ignoreFailingMessages = false; if (m_Disposed) { return; diff --git a/com.unity.netcode.gameobjects/Tests/Editor/Transports/UnityTransportTests.cs b/com.unity.netcode.gameobjects/Tests/Editor/Transports/UnityTransportTests.cs index 660b05b770..f746316edf 100644 --- a/com.unity.netcode.gameobjects/Tests/Editor/Transports/UnityTransportTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Editor/Transports/UnityTransportTests.cs @@ -116,14 +116,14 @@ public void UnityTransport_RestartSucceedsAfterFailure() transport.Initialize(); transport.SetConnectionData("127.0.0.", 4242, "127.0.0."); + Assert.False(transport.StartServer()); LogAssert.Expect(LogType.Error, "Invalid network endpoint: 127.0.0.:4242."); + LogAssert.Expect(LogType.Error, $"Network listen address (127.0.0.) is Invalid!"); #if UTP_TRANSPORT_2_0_ABOVE LogAssert.Expect(LogType.Error, "Socket creation failed (error Unity.Baselib.LowLevel.Binding+Baselib_ErrorState: Invalid argument (0x01000003) "); #endif - LogAssert.Expect(LogType.Error, "Server failed to bind. This is usually caused by another process being bound to the same port."); - transport.SetConnectionData("127.0.0.1", 4242, "127.0.0.1"); Assert.True(transport.StartServer()); diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportConnectionTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportConnectionTests.cs index 671515d27c..8effb7096c 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportConnectionTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportConnectionTests.cs @@ -45,6 +45,22 @@ public IEnumerator Cleanup() yield return null; } + // Check that invalid endpoint addresses are detected and return false if detected + [Test] + public void DetectInvalidEndpoint() + { + using var netcodeLogAssert = new NetcodeLogAssert(true); + InitializeTransport(out m_Server, out m_ServerEvents); + InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]); + m_Server.ConnectionData.Address = "Fubar"; + m_Server.ConnectionData.ServerListenAddress = "Fubar"; + m_Clients[0].ConnectionData.Address = "MoreFubar"; + Assert.False(m_Server.StartServer(), "Server failed to detect invalid endpoint!"); + Assert.False(m_Clients[0].StartClient(), "Client failed to detect invalid endpoint!"); + netcodeLogAssert.LogWasReceived(LogType.Error, $"Network listen address ({m_Server.ConnectionData.Address}) is Invalid!"); + netcodeLogAssert.LogWasReceived(LogType.Error, $"Target server network address ({m_Clients[0].ConnectionData.Address}) is Invalid!"); + } + // Check connection with a single client. [UnityTest] public IEnumerator ConnectSingleClient()