1
1
using System ;
2
2
using System . Collections ;
3
+ using System . Collections . Generic ;
3
4
using System . Text ;
4
5
using NUnit . Framework ;
5
6
using Unity . Netcode . TestHelpers . Runtime ;
6
- using UnityEngine ;
7
7
using UnityEngine . TestTools ;
8
8
9
9
namespace Unity . Netcode . RuntimeTests
10
10
{
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
12
16
{
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
+
13
37
private Guid m_ValidationToken ;
14
- private bool m_IsValidated ;
15
38
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 ( )
18
45
{
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 ( ) ;
21
49
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 ( ) ;
22
76
}
23
77
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 ( )
26
85
{
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 ] )
37
87
{
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 )
40
97
{
41
- timedOut = true ;
98
+ if ( ! m_Validated . ContainsKey ( client . LocalClientId ) || ! m_Validated [ client . LocalClientId ] )
99
+ {
100
+ return false ;
101
+ }
42
102
}
43
103
}
104
+ return true ;
105
+ }
44
106
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!" ) ;
48
112
}
49
113
50
114
private void NetworkManagerObject_ConnectionApprovalCallback ( NetworkManager . ConnectionApprovalRequest request , NetworkManager . ConnectionApprovalResponse response )
51
115
{
52
116
var stringGuid = Encoding . UTF8 . GetString ( request . Payload ) ;
117
+
53
118
if ( m_ValidationToken . ToString ( ) == stringGuid )
54
119
{
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!" ;
56
127
}
57
128
58
- response . Approved = m_IsValidated ;
59
- response . CreatePlayerObject = false ;
129
+ response . CreatePlayerObject = ShouldCheckForSpawnedPlayers ( ) ;
60
130
response . Position = null ;
61
131
response . Rotation = null ;
62
- response . PlayerPrefabHash = null ;
132
+ response . PlayerPrefabHash = m_PlayerCreation == PlayerCreation . PrefabHash ? m_PlayerPrefab . GetComponent < NetworkObject > ( ) . GlobalObjectIdHash : null ;
63
133
}
64
134
65
135
@@ -78,13 +148,5 @@ public void VerifyUniqueNetworkConfigPerRequest()
78
148
79
149
Assert . True ( currentHash != newHash , $ "Hashed { nameof ( NetworkConfig ) } values { currentHash } and { newHash } should not be the same!") ;
80
150
}
81
-
82
- [ TearDown ]
83
- public void TearDown ( )
84
- {
85
- // Stop, shutdown, and destroy
86
- NetworkManagerHelper . ShutdownNetworkManager ( ) ;
87
- }
88
-
89
151
}
90
152
}
0 commit comments