11using System ;
22using System . Collections ;
3+ using System . Collections . Generic ;
34using System . Text ;
45using NUnit . Framework ;
56using Unity . Netcode . TestHelpers . Runtime ;
6- using UnityEngine ;
77using UnityEngine . TestTools ;
88
99namespace Unity . Netcode . RuntimeTests
1010{
11- public class ConnectionApprovalTests
11+ [ TestFixture ( PlayerCreation . Prefab ) ]
12+ [ TestFixture ( PlayerCreation . PrefabHash ) ]
13+ [ TestFixture ( PlayerCreation . NoPlayer ) ]
14+ [ TestFixture ( PlayerCreation . FailValidation ) ]
15+ internal class ConnectionApprovalTests : NetcodeIntegrationTest
1216 {
17+ private const string k_InvalidToken = "Invalid validation token!" ;
18+ public enum PlayerCreation
19+ {
20+ Prefab ,
21+ PrefabHash ,
22+ NoPlayer ,
23+ FailValidation
24+ }
25+ private PlayerCreation m_PlayerCreation ;
26+ private bool m_ClientDisconnectReasonValidated ;
27+
28+ private Dictionary < ulong , bool > m_Validated = new Dictionary < ulong , bool > ( ) ;
29+
30+ public ConnectionApprovalTests ( PlayerCreation playerCreation )
31+ {
32+ m_PlayerCreation = playerCreation ;
33+ }
34+
35+ protected override int NumberOfClients => 1 ;
36+
1337 private Guid m_ValidationToken ;
14- private bool m_IsValidated ;
1538
16- [ SetUp ]
17- public void Setup ( )
39+ protected override bool ShouldCheckForSpawnedPlayers ( )
40+ {
41+ return m_PlayerCreation != PlayerCreation . NoPlayer ;
42+ }
43+
44+ protected override void OnServerAndClientsCreated ( )
1845 {
19- // Create, instantiate, and host
20- Assert . IsTrue ( NetworkManagerHelper . StartNetworkManager ( out _ , NetworkManagerHelper . NetworkManagerOperatingMode . None ) ) ;
46+ m_ClientDisconnectReasonValidated = false ;
47+ m_BypassConnectionTimeout = m_PlayerCreation == PlayerCreation . FailValidation ;
48+ m_Validated . Clear ( ) ;
2149 m_ValidationToken = Guid . NewGuid ( ) ;
50+ var validationToken = Encoding . UTF8 . GetBytes ( m_ValidationToken . ToString ( ) ) ;
51+ m_ServerNetworkManager . ConnectionApprovalCallback = NetworkManagerObject_ConnectionApprovalCallback ;
52+ m_ServerNetworkManager . NetworkConfig . PlayerPrefab = m_PlayerCreation == PlayerCreation . Prefab ? m_PlayerPrefab : null ;
53+ if ( m_PlayerCreation == PlayerCreation . PrefabHash )
54+ {
55+ m_ServerNetworkManager . NetworkConfig . Prefabs . Add ( new NetworkPrefab ( ) { Prefab = m_PlayerPrefab } ) ;
56+ }
57+ m_ServerNetworkManager . NetworkConfig . ConnectionApproval = true ;
58+ m_ServerNetworkManager . NetworkConfig . ConnectionData = validationToken ;
59+
60+ foreach ( var client in m_ClientNetworkManagers )
61+ {
62+ client . NetworkConfig . PlayerPrefab = m_PlayerCreation == PlayerCreation . Prefab ? m_PlayerPrefab : null ;
63+ if ( m_PlayerCreation == PlayerCreation . PrefabHash )
64+ {
65+ client . NetworkConfig . Prefabs . Add ( new NetworkPrefab ( ) { Prefab = m_PlayerPrefab } ) ;
66+ }
67+ client . NetworkConfig . ConnectionApproval = true ;
68+ client . NetworkConfig . ConnectionData = m_PlayerCreation == PlayerCreation . FailValidation ? Encoding . UTF8 . GetBytes ( Guid . NewGuid ( ) . ToString ( ) ) : validationToken ;
69+ if ( m_PlayerCreation == PlayerCreation . FailValidation )
70+ {
71+ client . OnClientDisconnectCallback += Client_OnClientDisconnectCallback ;
72+ }
73+ }
74+
75+ base . OnServerAndClientsCreated ( ) ;
2276 }
2377
24- [ UnityTest ]
25- public IEnumerator ConnectionApproval ( )
78+ private void Client_OnClientDisconnectCallback ( ulong clientId )
79+ {
80+ m_ClientNetworkManagers [ 0 ] . OnClientDisconnectCallback -= Client_OnClientDisconnectCallback ;
81+ m_ClientDisconnectReasonValidated = m_ClientNetworkManagers [ 0 ] . LocalClientId == clientId && m_ClientNetworkManagers [ 0 ] . DisconnectReason == k_InvalidToken ;
82+ }
83+
84+ private bool ClientAndHostValidated ( )
2685 {
27- NetworkManagerHelper . NetworkManagerObject . ConnectionApprovalCallback = NetworkManagerObject_ConnectionApprovalCallback ;
28- NetworkManagerHelper . NetworkManagerObject . NetworkConfig . ConnectionApproval = true ;
29- NetworkManagerHelper . NetworkManagerObject . NetworkConfig . PlayerPrefab = null ;
30- NetworkManagerHelper . NetworkManagerObject . NetworkConfig . ConnectionData = Encoding . UTF8 . GetBytes ( m_ValidationToken . ToString ( ) ) ;
31- m_IsValidated = false ;
32- NetworkManagerHelper . NetworkManagerObject . StartHost ( ) ;
33-
34- var timeOut = Time . realtimeSinceStartup + 3.0f ;
35- var timedOut = false ;
36- while ( ! m_IsValidated )
86+ if ( ! m_Validated . ContainsKey ( m_ServerNetworkManager . LocalClientId ) || ! m_Validated [ m_ServerNetworkManager . LocalClientId ] )
3787 {
38- yield return new WaitForSeconds ( 0.01f ) ;
39- if ( timeOut < Time . realtimeSinceStartup )
88+ return false ;
89+ }
90+ if ( m_PlayerCreation == PlayerCreation . FailValidation )
91+ {
92+ return m_ClientDisconnectReasonValidated ;
93+ }
94+ else
95+ {
96+ foreach ( var client in m_ClientNetworkManagers )
4097 {
41- timedOut = true ;
98+ if ( ! m_Validated . ContainsKey ( client . LocalClientId ) || ! m_Validated [ client . LocalClientId ] )
99+ {
100+ return false ;
101+ }
42102 }
43103 }
104+ return true ;
105+ }
44106
45- //Make sure we didn't time out
46- Assert . False ( timedOut ) ;
47- Assert . True ( m_IsValidated ) ;
107+ [ UnityTest ]
108+ public IEnumerator ConnectionApproval ( )
109+ {
110+ yield return WaitForConditionOrTimeOut ( ClientAndHostValidated ) ;
111+ AssertOnTimeout ( "Timed out waiting for all clients to be approved!" ) ;
48112 }
49113
50114 private void NetworkManagerObject_ConnectionApprovalCallback ( NetworkManager . ConnectionApprovalRequest request , NetworkManager . ConnectionApprovalResponse response )
51115 {
52116 var stringGuid = Encoding . UTF8 . GetString ( request . Payload ) ;
117+
53118 if ( m_ValidationToken . ToString ( ) == stringGuid )
54119 {
55- m_IsValidated = true ;
120+ m_Validated . Add ( request . ClientNetworkId , true ) ;
121+ response . Approved = true ;
122+ }
123+ else
124+ {
125+ response . Approved = false ;
126+ response . Reason = "Invalid validation token!" ;
56127 }
57128
58- response . Approved = m_IsValidated ;
59- response . CreatePlayerObject = false ;
129+ response . CreatePlayerObject = ShouldCheckForSpawnedPlayers ( ) ;
60130 response . Position = null ;
61131 response . Rotation = null ;
62- response . PlayerPrefabHash = null ;
132+ response . PlayerPrefabHash = m_PlayerCreation == PlayerCreation . PrefabHash ? m_PlayerPrefab . GetComponent < NetworkObject > ( ) . GlobalObjectIdHash : null ;
63133 }
64134
65135
@@ -78,13 +148,5 @@ public void VerifyUniqueNetworkConfigPerRequest()
78148
79149 Assert . True ( currentHash != newHash , $ "Hashed { nameof ( NetworkConfig ) } values { currentHash } and { newHash } should not be the same!") ;
80150 }
81-
82- [ TearDown ]
83- public void TearDown ( )
84- {
85- // Stop, shutdown, and destroy
86- NetworkManagerHelper . ShutdownNetworkManager ( ) ;
87- }
88-
89151 }
90152}
0 commit comments