Skip to content

Commit 4abf5a1

Browse files
fix: NGO UnityTransport not detecting invalid endpoint [MTT-3937] (#2496)
* fix detect and return early if trying to bind to an invalid endpoint. * test Adding a unit test to validate the fix. Minor tweaks to NetcodeLogAssert. Updating test to detect invalid endpoint. Fixing the editor test expecting a error log that no longer will happen with an invalid endpoint address.
1 parent 509a619 commit 4abf5a1

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ Additional documentation and release notes are available at [Multiplayer Documen
88

99
## [Unreleased]
1010

11+
### Added
12+
1113
### Fixed
1214

15+
- Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496)
1316
- Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495)
1417

18+
## Changed
19+
1520
## [1.4.0]
1621

1722
### Added

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,13 @@ private bool ClientBindAndConnect()
536536
serverEndpoint = ConnectionData.ServerEndPoint;
537537
}
538538

539+
// Verify the endpoint is valid before proceeding
540+
if (serverEndpoint.Family == NetworkFamily.Invalid)
541+
{
542+
Debug.LogError($"Target server network address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!");
543+
return false;
544+
}
545+
539546
InitDriver();
540547

541548
var bindEndpoint = serverEndpoint.Family == NetworkFamily.Ipv6 ? NetworkEndpoint.AnyIpv6 : NetworkEndpoint.AnyIpv4;
@@ -554,6 +561,13 @@ private bool ClientBindAndConnect()
554561

555562
private bool ServerBindAndListen(NetworkEndpoint endPoint)
556563
{
564+
// Verify the endpoint is valid before proceeding
565+
if (endPoint.Family == NetworkFamily.Invalid)
566+
{
567+
Debug.LogError($"Network listen address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!");
568+
return false;
569+
}
570+
557571
InitDriver();
558572

559573
int result = m_Driver.Bind(endPoint);

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeLogAssert.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
using System.Text.RegularExpressions;
44
using NUnit.Framework;
55
using UnityEngine;
6+
using UnityEngine.TestTools;
67

78
namespace Unity.Netcode.RuntimeTests
89
{
9-
public class NetcodeLogAssert
10+
public class NetcodeLogAssert : IDisposable
1011
{
1112
private struct LogData
1213
{
@@ -20,8 +21,11 @@ private struct LogData
2021

2122
private List<LogData> AllLogs { get; }
2223

23-
public NetcodeLogAssert()
24+
private bool m_ResetIgnoreFailingMessagesOnTearDown;
25+
public NetcodeLogAssert(bool ignorFailingMessages = false, bool resetOnTearDown = true)
2426
{
27+
LogAssert.ignoreFailingMessages = ignorFailingMessages;
28+
m_ResetIgnoreFailingMessagesOnTearDown = resetOnTearDown;
2529
AllLogs = new List<LogData>();
2630
Activate();
2731
}
@@ -51,6 +55,16 @@ public void AddLog(string message, string stacktrace, LogType type)
5155
}
5256
}
5357

58+
[UnityTearDown]
59+
public void OnTearDown()
60+
{
61+
// Defaults to true and will reset LogAssert.ignoreFailingMessages during tear down
62+
if (m_ResetIgnoreFailingMessagesOnTearDown)
63+
{
64+
LogAssert.ignoreFailingMessages = false;
65+
}
66+
}
67+
5468
public void Dispose()
5569
{
5670
Dispose(true);
@@ -59,6 +73,8 @@ public void Dispose()
5973

6074
private void Dispose(bool disposing)
6175
{
76+
// Always reset when disposing
77+
LogAssert.ignoreFailingMessages = false;
6278
if (m_Disposed)
6379
{
6480
return;

com.unity.netcode.gameobjects/Tests/Editor/Transports/UnityTransportTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ public void UnityTransport_RestartSucceedsAfterFailure()
116116
transport.Initialize();
117117

118118
transport.SetConnectionData("127.0.0.", 4242, "127.0.0.");
119+
119120
Assert.False(transport.StartServer());
120121

121122
LogAssert.Expect(LogType.Error, "Invalid network endpoint: 127.0.0.:4242.");
123+
LogAssert.Expect(LogType.Error, $"Network listen address (127.0.0.) is Invalid!");
122124
#if UTP_TRANSPORT_2_0_ABOVE
123125
LogAssert.Expect(LogType.Error, "Socket creation failed (error Unity.Baselib.LowLevel.Binding+Baselib_ErrorState: Invalid argument (0x01000003) <argument name stripped>");
124126
#endif
125-
LogAssert.Expect(LogType.Error, "Server failed to bind. This is usually caused by another process being bound to the same port.");
126-
127127
transport.SetConnectionData("127.0.0.1", 4242, "127.0.0.1");
128128
Assert.True(transport.StartServer());
129129

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportConnectionTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public IEnumerator Cleanup()
4545
yield return null;
4646
}
4747

48+
// Check that invalid endpoint addresses are detected and return false if detected
49+
[Test]
50+
public void DetectInvalidEndpoint()
51+
{
52+
using var netcodeLogAssert = new NetcodeLogAssert(true);
53+
InitializeTransport(out m_Server, out m_ServerEvents);
54+
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
55+
m_Server.ConnectionData.Address = "Fubar";
56+
m_Server.ConnectionData.ServerListenAddress = "Fubar";
57+
m_Clients[0].ConnectionData.Address = "MoreFubar";
58+
Assert.False(m_Server.StartServer(), "Server failed to detect invalid endpoint!");
59+
Assert.False(m_Clients[0].StartClient(), "Client failed to detect invalid endpoint!");
60+
netcodeLogAssert.LogWasReceived(LogType.Error, $"Network listen address ({m_Server.ConnectionData.Address}) is Invalid!");
61+
netcodeLogAssert.LogWasReceived(LogType.Error, $"Target server network address ({m_Clients[0].ConnectionData.Address}) is Invalid!");
62+
}
63+
4864
// Check connection with a single client.
4965
[UnityTest]
5066
public IEnumerator ConnectSingleClient()

0 commit comments

Comments
 (0)