Skip to content

Commit 86afb0a

Browse files
fix: some small fixes while testing webgl (#2291)
* better error handling. Adding IsNullOrEmpty checks Removing catching of the exception. With previous version, NGO still starts and IsClient is still set. With exception, StartHost fails now correctly when certificates are not set. * adding const strings for relay connection types (instead of having to guess) * adding test for null check * Address review comments Co-authored-by: Simon Lemay <[email protected]>
1 parent 4487baa commit 86afb0a

File tree

3 files changed

+58
-24
lines changed

3 files changed

+58
-24
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,36 +1471,30 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
14711471
}
14721472
else
14731473
{
1474-
try
1474+
if (NetworkManager.IsServer)
14751475
{
1476-
if (NetworkManager.IsServer)
1476+
if (String.IsNullOrEmpty(m_ServerCertificate) || String.IsNullOrEmpty(m_ServerPrivateKey))
14771477
{
1478-
if (m_ServerCertificate.Length == 0 || m_ServerPrivateKey.Length == 0)
1479-
{
1480-
throw new Exception("In order to use encrypted communications, when hosting, you must set the server certificate and key.");
1481-
}
1482-
m_NetworkSettings.WithSecureServerParameters(m_ServerCertificate, m_ServerPrivateKey);
1478+
throw new Exception("In order to use encrypted communications, when hosting, you must set the server certificate and key.");
1479+
}
1480+
1481+
m_NetworkSettings.WithSecureServerParameters(m_ServerCertificate, m_ServerPrivateKey);
1482+
}
1483+
else
1484+
{
1485+
if (String.IsNullOrEmpty(m_ServerCommonName))
1486+
{
1487+
throw new Exception("In order to use encrypted communications, clients must set the server common name.");
1488+
}
1489+
else if (String.IsNullOrEmpty(m_ClientCaCertificate))
1490+
{
1491+
m_NetworkSettings.WithSecureClientParameters(m_ServerCommonName);
14831492
}
14841493
else
14851494
{
1486-
if (m_ServerCommonName.Length == 0)
1487-
{
1488-
throw new Exception("In order to use encrypted communications, clients must set the server common name.");
1489-
}
1490-
else if (m_ClientCaCertificate == null)
1491-
{
1492-
m_NetworkSettings.WithSecureClientParameters(m_ServerCommonName);
1493-
}
1494-
else
1495-
{
1496-
m_NetworkSettings.WithSecureClientParameters(m_ClientCaCertificate, m_ServerCommonName);
1497-
}
1495+
m_NetworkSettings.WithSecureClientParameters(m_ClientCaCertificate, m_ServerCommonName);
14981496
}
14991497
}
1500-
catch(Exception e)
1501-
{
1502-
Debug.LogException(e, this);
1503-
}
15041498
}
15051499
}
15061500
#endif

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,40 @@ public void UnityTransport_RestartSucceedsAfterFailure()
126126

127127
transport.Shutdown();
128128
}
129+
130+
#if UTP_TRANSPORT_2_0_ABOVE
131+
[Test]
132+
public void UnityTransport_EmptySecurityStringsShouldThrow([Values("", null)] string cert, [Values("", null)] string secret)
133+
{
134+
var supportingGO = new GameObject();
135+
try
136+
{
137+
var networkManager = supportingGO.AddComponent<NetworkManager>(); // NM is required for UTP to work with certificates.
138+
networkManager.NetworkConfig = new NetworkConfig();
139+
UnityTransport transport = supportingGO.AddComponent<UnityTransport>();
140+
networkManager.NetworkConfig.NetworkTransport = transport;
141+
transport.Initialize();
142+
transport.SetServerSecrets(serverCertificate: cert, serverPrivateKey: secret);
143+
144+
// Use encryption, but don't set certificate and check for exception
145+
transport.UseEncryption = true;
146+
Assert.Throws<System.Exception>(() =>
147+
{
148+
networkManager.StartServer();
149+
});
150+
// Make sure StartServer failed
151+
Assert.False(transport.NetworkDriver.IsCreated);
152+
Assert.False(networkManager.IsServer);
153+
Assert.False(networkManager.IsListening);
154+
}
155+
finally
156+
{
157+
if (supportingGO != null)
158+
{
159+
Object.DestroyImmediate(supportingGO);
160+
}
161+
}
162+
}
163+
#endif
129164
}
130165
}

com.unity.netcode.gameobjects/Tests/Editor/com.unity.netcode.editortests.asmdef

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
"name": "Unity",
3333
"expression": "(0,2022.2.0a5)",
3434
"define": "UNITY_UNET_PRESENT"
35+
},
36+
{
37+
"name": "com.unity.transport",
38+
"expression": "2.0.0-exp",
39+
"define": "UTP_TRANSPORT_2_0_ABOVE"
3540
}
3641
]
37-
}
42+
}

0 commit comments

Comments
 (0)