public void OnDataReceivedTest() { // add one custom message handler bool wasReceived = false; NetworkConnection connectionReceived = null; TestMessage1 messageReceived = new TestMessage1(); NetworkServer.RegisterHandler <TestMessage1>((conn, msg) => { wasReceived = true; connectionReceived = conn; messageReceived = msg; }, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add a connection NetworkConnectionToClient connection = new NetworkConnectionToClient(42, false, 0); NetworkServer.AddConnection(connection); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); // serialize a test message into an arraysegment TestMessage1 testMessage = new TestMessage1 { IntValue = 13, DoubleValue = 14, StringValue = "15" }; NetworkWriter writer = new NetworkWriter(); MessagePacker.Pack(testMessage, writer); ArraySegment <byte> segment = writer.ToArraySegment(); // call transport.OnDataReceived // -> should call NetworkServer.OnDataReceived // -> conn.TransportReceive // -> Handler(CommandMessage) Transport.activeTransport.OnServerDataReceived.Invoke(42, segment, 0); // was our message handler called now? Assert.That(wasReceived, Is.True); Assert.That(connectionReceived, Is.EqualTo(connection)); Assert.That(messageReceived, Is.EqualTo(testMessage)); }
public IEnumerator SetClientReadyAndNotReadyCheckObserversOfNetworkIdentityTest() { // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); NetworkServer.AddConnection(connection); Assert.That(connection.isReady, Is.False); connection.isAuthenticated = true; // set client ready NetworkServer.SetClientReady(connection); Assert.That(connection.isReady, Is.True); // spawn network identity on the server GameObject gameObject = new GameObject(); NetworkIdentity networkIdentity = gameObject.AddComponent <NetworkIdentity>(); NetworkServer.Spawn(gameObject); // allow 1 frame to spawn object yield return(null); // connection should now automatically be observing the spawned identity Assert.That(connection.observing.Contains(networkIdentity), Is.True); Assert.That(networkIdentity.observers.ContainsKey(connection.connectionId), Is.True); Assert.That(networkIdentity.observers[connection.connectionId], Is.EqualTo(connection)); // setting client to not ready // note: we need runtime test instead of edit mode test // because otherwise gameobject.scene.buildIndex is not valid, // and buildIndex is needed to check if observer should be removed NetworkServer.SetClientNotReady(connection); Assert.That(connection.isReady, Is.False); // client that is not ready should not observe the spawned identity Assert.That(connection.observing.Contains(networkIdentity), Is.False); Assert.That(networkIdentity.observers.ContainsKey(connection.connectionId), Is.False); // clean up NetworkServer.Destroy(gameObject); NetworkIdentity.spawned.Clear(); NetworkServer.Shutdown(); }
public void SendToClientOfPlayer() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <int, NetworkMessageDelegate>() { { MessagePacker.GetId <TestMessage1>(), ((conn, reader, channelId) => ++ called) } }); NetworkServer.AddConnection(connection); // create a message TestMessage1 message = new TestMessage1 { IntValue = 1, DoubleValue = 2, StringValue = "3" }; // create a gameobject and networkidentity NetworkIdentity identity = new GameObject().AddComponent <NetworkIdentity>(); identity.connectionToClient = connection; // send it to that player NetworkServer.SendToClientOfPlayer(identity, message); // update local connection once so that the incoming queue is processed connection.connectionToServer.Update(); // was it send to and handled by the connection? Assert.That(called, Is.EqualTo(1)); // clean up NetworkServer.Shutdown(); // destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call // GameObject.Destroy (but we need DestroyImmediate in Editor) GameObject.DestroyImmediate(identity.gameObject); }
public void SendToAllTest() { // message handlers NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => { }, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <int, NetworkMessageDelegate>() { { MessagePacker.GetId <TestMessage>(), ((conn, reader, channelId) => ++ called) } }); NetworkServer.AddConnection(connection); // create a message TestMessage message = new TestMessage { IntValue = 1, DoubleValue = 2, StringValue = "3" }; // send it to all bool result = NetworkServer.SendToAll(message); Assert.That(result, Is.True); // update local connection once so that the incoming queue is processed connection.connectionToServer.Update(); // was it send to and handled by the connection? Assert.That(called, Is.EqualTo(1)); // clean up NetworkServer.Shutdown(); }
public void AddConnection_PreventsDuplicates() { // listen NetworkServer.Listen(1); // add a connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); Assert.That(NetworkServer.AddConnection(conn42), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); // add duplicate connectionId NetworkConnectionToClient connDup = new NetworkConnectionToClient(42); Assert.That(NetworkServer.AddConnection(connDup), Is.False); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); }
public void AddConnectionTest() { // message handlers NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => { }, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add first connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); bool result42 = NetworkServer.AddConnection(conn42); Assert.That(result42, Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections.ContainsKey(42), Is.True); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); // add second connection NetworkConnectionToClient conn43 = new NetworkConnectionToClient(43); bool result43 = NetworkServer.AddConnection(conn43); Assert.That(result43, Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(2)); Assert.That(NetworkServer.connections.ContainsKey(42), Is.True); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); Assert.That(NetworkServer.connections.ContainsKey(43), Is.True); Assert.That(NetworkServer.connections[43], Is.EqualTo(conn43)); // add duplicate connectionId NetworkConnectionToClient connDup = new NetworkConnectionToClient(42); bool resultDup = NetworkServer.AddConnection(connDup); Assert.That(resultDup, Is.False); Assert.That(NetworkServer.connections.Count, Is.EqualTo(2)); Assert.That(NetworkServer.connections.ContainsKey(42), Is.True); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); Assert.That(NetworkServer.connections.ContainsKey(43), Is.True); Assert.That(NetworkServer.connections[43], Is.EqualTo(conn43)); }
public void DisconnectAllConnectionsTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, false, 0); NetworkServer.AddConnection(conn42); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); // disconnect all connections NetworkServer.DisconnectAllExternalConnections(); // update transports. OnTransportDisconnected should be fired and // clear all connections. Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); }
public void AddConnection() { // listen NetworkServer.Listen(1); // add first connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); Assert.That(NetworkServer.AddConnection(conn42), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); // add second connection NetworkConnectionToClient conn43 = new NetworkConnectionToClient(43); Assert.That(NetworkServer.AddConnection(conn43), Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(2)); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); Assert.That(NetworkServer.connections[43], Is.EqualTo(conn43)); }
public void HideForConnection() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); // required for ShowForConnection connection.isReady = true; connection.connectionToServer = new LocalConnectionToServer(); // set a client handler int called = 0; connection.connectionToServer.SetHandlers(new Dictionary <ushort, NetworkMessageDelegate>() { { MessagePacking.GetId <ObjectHideMessage>(), ((conn, reader, channelId) => ++ called) } }); NetworkServer.AddConnection(connection); // create a gameobject and networkidentity NetworkIdentity identity = new GameObject().AddComponent <NetworkIdentity>(); identity.connectionToClient = connection; // call HideForConnection NetworkServer.HideForConnection(identity, connection); // update local connection once so that the incoming queue is processed connection.connectionToServer.Update(); // was it sent to and handled by the connection? Assert.That(called, Is.EqualTo(1)); // clean up NetworkServer.Shutdown(); // destroy GO after shutdown, otherwise isServer is true in OnDestroy and it tries to call // GameObject.Destroy (but we need DestroyImmediate in Editor) GameObject.DestroyImmediate(identity.gameObject); }
public void HideForConnection() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // setup connections LocalConnectionToClient connectionToClient = new LocalConnectionToClient(); LocalConnectionToServer connectionToServer = new LocalConnectionToServer(); connectionToClient.connectionToServer = new LocalConnectionToServer(); // setup NetworkServer/Client connections so messages are handled NetworkClient.connection = connectionToServer; NetworkServer.connections[connectionToClient.connectionId] = connectionToClient; // required for ShowForConnection connectionToClient.isReady = true; connectionToClient.connectionToServer = new LocalConnectionToServer(); // set a client handler int called = 0; void Handler(ObjectHideMessage _) => ++ called; NetworkClient.RegisterHandler <ObjectHideMessage>(Handler, false); NetworkServer.AddConnection(connectionToClient); // create a gameobject and networkidentity CreateNetworked(out GameObject _, out NetworkIdentity identity); identity.connectionToClient = connectionToClient; // call HideForConnection NetworkServer.HideForConnection(identity, connectionToClient); // update local connection once so that the incoming queue is processed connectionToClient.connectionToServer.Update(); // was it sent to and handled by the connection? Assert.That(called, Is.EqualTo(1)); }
public void DisconnectAllConnectionsTest() { // message handlers NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => { }, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42); NetworkServer.AddConnection(conn42); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); // disconnect all connections NetworkServer.DisconnectAllConnections(); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); }
public void RemoveConnectionTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, false, 0); bool result42 = NetworkServer.AddConnection(conn42); Assert.That(result42, Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections.ContainsKey(42), Is.True); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); // remove connection bool resultRemove = NetworkServer.RemoveConnection(42); Assert.That(resultRemove, Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); }
public void SendToAll() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // setup connections LocalConnectionToClient connectionToClient = new LocalConnectionToClient(); LocalConnectionToServer connectionToServer = new LocalConnectionToServer(); connectionToClient.connectionToServer = new LocalConnectionToServer(); // setup NetworkServer/Client connections so messages are handled NetworkClient.connection = connectionToServer; NetworkServer.connections[connectionToClient.connectionId] = connectionToClient; // set a client handler int called = 0; void Handler(TestMessage1 _) => ++ called; NetworkClient.RegisterHandler <TestMessage1>(Handler, false); NetworkServer.AddConnection(connectionToClient); // create a message TestMessage1 message = new TestMessage1 { IntValue = 1, DoubleValue = 2, StringValue = "3" }; // send it to all NetworkServer.SendToAll(message); // update local connection once so that the incoming queue is processed connectionToClient.connectionToServer.Update(); // was it send to and handled by the connection? Assert.That(called, Is.EqualTo(1)); }
public void DisconnectAllTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // set local connection LocalConnectionToClient localConnection = new LocalConnectionToClient(); NetworkServer.SetLocalConnection(localConnection); Assert.That(NetworkServer.localConnection, Is.EqualTo(localConnection)); // add connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, false, 0); NetworkServer.AddConnection(conn42); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); // disconnect all connections and local connection NetworkServer.DisconnectAll(); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); Assert.That(NetworkServer.localConnection, Is.Null); }
public void AddConnectionTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add first connection NetworkConnectionToClient conn42 = new NetworkConnectionToClient(42, false, 0); bool result42 = NetworkServer.AddConnection(conn42); Assert.That(result42, Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); Assert.That(NetworkServer.connections.ContainsKey(42), Is.True); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); // add second connection NetworkConnectionToClient conn43 = new NetworkConnectionToClient(43, false, 0); bool result43 = NetworkServer.AddConnection(conn43); Assert.That(result43, Is.True); Assert.That(NetworkServer.connections.Count, Is.EqualTo(2)); Assert.That(NetworkServer.connections.ContainsKey(42), Is.True); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); Assert.That(NetworkServer.connections.ContainsKey(43), Is.True); Assert.That(NetworkServer.connections[43], Is.EqualTo(conn43)); // add duplicate connectionId NetworkConnectionToClient connDup = new NetworkConnectionToClient(42, false, 0); bool resultDup = NetworkServer.AddConnection(connDup); Assert.That(resultDup, Is.False); Assert.That(NetworkServer.connections.Count, Is.EqualTo(2)); Assert.That(NetworkServer.connections.ContainsKey(42), Is.True); Assert.That(NetworkServer.connections[42], Is.EqualTo(conn42)); Assert.That(NetworkServer.connections.ContainsKey(43), Is.True); Assert.That(NetworkServer.connections[43], Is.EqualTo(conn43)); }
private IEnumerator CardsSpawned() { var conn = new NetworkConnectionToClient(2); NetworkServer.AddConnection(conn); gameServer.OnServerAddPlayer(conn); yield return(new WaitForSeconds(1f)); foreach ((var player, var idx) in Utils.GetPlayersIndexed()) { if (player.PlayerName == null || player.PlayerName == "") { player.PlayerName = "Mustermann" + idx; player.CmdSendName("Mustermann" + idx); } } yield return(new WaitForSeconds(1f)); Assert.NotNull(GameObject.FindGameObjectWithTag("QuestionCard"), "No QuestionCard spawned"); Assert.NotNull(GameObject.FindGameObjectWithTag("InputCard"), "No InputCard spawned"); yield return(null); }
public void CommandMessageCallsCommandTest() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection ULocalConnectionToClient connection = new ULocalConnectionToClient(); connection.connectionToServer = new ULocalConnectionToServer(); NetworkServer.AddConnection(connection); // set as authenticated, otherwise removeplayer is rejected connection.isAuthenticated = true; // add an identity with two networkbehaviour components GameObject go = new GameObject(); NetworkIdentity identity = go.AddComponent <NetworkIdentity>(); identity.netId = 42; // for authority check identity.connectionToClient = connection; CommandTestNetworkBehaviour comp0 = go.AddComponent <CommandTestNetworkBehaviour>(); Assert.That(comp0.called, Is.EqualTo(0)); CommandTestNetworkBehaviour comp1 = go.AddComponent <CommandTestNetworkBehaviour>(); Assert.That(comp1.called, Is.EqualTo(0)); connection.identity = identity; // register the command delegate, otherwise it's not found NetworkBehaviour.RegisterCommandDelegate(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated), CommandTestNetworkBehaviour.CommandGenerated); // identity needs to be in spawned dict, otherwise command handler // won't find it NetworkIdentity.spawned[identity.netId] = identity; // serialize a removeplayer message into an arraysegment CommandMessage message = new CommandMessage { componentIndex = 0, functionHash = NetworkBehaviour.GetMethodHash(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated)), netId = identity.netId, payload = new ArraySegment <byte>(new byte[0]) }; NetworkWriter writer = new NetworkWriter(); MessagePacker.Pack(message, writer); ArraySegment <byte> segment = writer.ToArraySegment(); // call transport.OnDataReceived with the message // -> calls NetworkServer.OnRemovePlayerMessage // -> destroys conn.identity and sets it to null Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); // was the command called in the first component, not in the second one? Assert.That(comp0.called, Is.EqualTo(1)); Assert.That(comp1.called, Is.EqualTo(0)); // send another command for the second component comp0.called = 0; message.componentIndex = 1; writer = new NetworkWriter(); MessagePacker.Pack(message, writer); segment = writer.ToArraySegment(); Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); // was the command called in the second component, not in the first one? Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(1)); // sending a command without authority should fail // (= if connectionToClient is not what we received the data on) // set wrong authority identity.connectionToClient = new ULocalConnectionToClient(); comp0.called = 0; comp1.called = 0; Transport.activeTransport.OnServerDataReceived.Invoke(0, segment, 0); Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(0)); // restore authority identity.connectionToClient = connection; // sending a component with wrong netId should fail // wrong netid message.netId += 1; writer = new NetworkWriter(); // need to serialize the message again with wrong netid MessagePacker.Pack(message, writer); ArraySegment <byte> segmentWrongNetId = writer.ToArraySegment(); comp0.called = 0; comp1.called = 0; Transport.activeTransport.OnServerDataReceived.Invoke(0, segmentWrongNetId, 0); Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(0)); // clean up NetworkBehaviour.ClearDelegates(); NetworkIdentity.spawned.Clear(); NetworkBehaviour.ClearDelegates(); NetworkServer.Shutdown(); // destroy the test gameobject AFTER server was stopped. // otherwise isServer is true in OnDestroy, which means it would try // to call Destroy(go). but we need to use DestroyImmediate in // Editor GameObject.DestroyImmediate(go); }
public void RegisterUnregisterClearHandlerTest() { // message handlers that are needed for the test NetworkServer.RegisterHandler <ConnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false); NetworkServer.RegisterHandler <ErrorMessage>((conn, msg) => { }, false); // RegisterHandler(conn, msg) variant int variant1Called = 0; NetworkServer.RegisterHandler <TestMessage>((conn, msg) => { ++variant1Called; }, false); // RegisterHandler(msg) variant int variant2Called = 0; NetworkServer.RegisterHandler <WovenTestMessage>(msg => { ++variant2Called; }, false); // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add a connection NetworkConnectionToClient connection = new NetworkConnectionToClient(42); NetworkServer.AddConnection(connection); Assert.That(NetworkServer.connections.Count, Is.EqualTo(1)); // serialize first message, send it to server, check if it was handled NetworkWriter writer = new NetworkWriter(); MessagePacker.Pack(new TestMessage(), writer); Transport.activeTransport.OnServerDataReceived.Invoke(42, writer.ToArraySegment(), 0); Assert.That(variant1Called, Is.EqualTo(1)); // serialize second message, send it to server, check if it was handled writer = new NetworkWriter(); MessagePacker.Pack(new WovenTestMessage(), writer); Transport.activeTransport.OnServerDataReceived.Invoke(42, writer.ToArraySegment(), 0); Assert.That(variant2Called, Is.EqualTo(1)); // unregister first handler, send, should fail NetworkServer.UnregisterHandler <TestMessage>(); writer = new NetworkWriter(); MessagePacker.Pack(new TestMessage(), writer); // log error messages are expected LogAssert.ignoreFailingMessages = true; Transport.activeTransport.OnServerDataReceived.Invoke(42, writer.ToArraySegment(), 0); LogAssert.ignoreFailingMessages = false; // still 1, not 2 Assert.That(variant1Called, Is.EqualTo(1)); // unregister second handler via ClearHandlers to test that one too. send, should fail NetworkServer.ClearHandlers(); // (only add this one to avoid disconnect error) NetworkServer.RegisterHandler <DisconnectMessage>((conn, msg) => { }, false); writer = new NetworkWriter(); MessagePacker.Pack(new TestMessage(), writer); // log error messages are expected LogAssert.ignoreFailingMessages = true; Transport.activeTransport.OnServerDataReceived.Invoke(42, writer.ToArraySegment(), 0); LogAssert.ignoreFailingMessages = false; // still 1, not 2 Assert.That(variant2Called, Is.EqualTo(1)); // clean up NetworkServer.Shutdown(); }
public void CommandMessageCallsCommand() { // listen NetworkServer.Listen(1); Assert.That(NetworkServer.connections.Count, Is.EqualTo(0)); // add connection LocalConnectionToClient connection = new LocalConnectionToClient(); connection.connectionToServer = new LocalConnectionToServer(); NetworkServer.AddConnection(connection); // set as authenticated, otherwise removeplayer is rejected connection.isAuthenticated = true; // add an identity with two networkbehaviour components CreateNetworked(out GameObject go, out NetworkIdentity identity, out CommandTestNetworkBehaviour comp0, out CommandTestNetworkBehaviour comp1); identity.netId = 42; // for authority check identity.connectionToClient = connection; connection.identity = identity; Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(0)); // register the command delegate, otherwise it's not found int registeredHash = RemoteCallHelper.RegisterDelegate(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated), MirrorInvokeType.Command, CommandTestNetworkBehaviour.CommandGenerated, true); // identity needs to be in spawned dict, otherwise command handler // won't find it NetworkIdentity.spawned[identity.netId] = identity; // serialize a removeplayer message into an arraysegment CommandMessage message = new CommandMessage { componentIndex = 0, functionHash = RemoteCallHelper.GetMethodHash(typeof(CommandTestNetworkBehaviour), nameof(CommandTestNetworkBehaviour.CommandGenerated)), netId = identity.netId, payload = new ArraySegment <byte>(new byte[0]) }; NetworkWriter writer = new NetworkWriter(); MessagePacking.Pack(message, writer); ArraySegment <byte> segment = writer.ToArraySegment(); // call transport.OnDataReceived with the message // -> calls NetworkServer.OnRemovePlayerMessage // -> destroys conn.identity and sets it to null transport.OnServerDataReceived.Invoke(0, segment, 0); // was the command called in the first component, not in the second one? Assert.That(comp0.called, Is.EqualTo(1)); Assert.That(comp1.called, Is.EqualTo(0)); // send another command for the second component comp0.called = 0; message.componentIndex = 1; writer = new NetworkWriter(); MessagePacking.Pack(message, writer); segment = writer.ToArraySegment(); transport.OnServerDataReceived.Invoke(0, segment, 0); // was the command called in the second component, not in the first one? Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(1)); // sending a command without authority should fail // (= if connectionToClient is not what we received the data on) // set wrong authority identity.connectionToClient = new LocalConnectionToClient(); comp0.called = 0; comp1.called = 0; transport.OnServerDataReceived.Invoke(0, segment, 0); Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(0)); // restore authority identity.connectionToClient = connection; // sending a component with wrong netId should fail // wrong netid message.netId += 1; writer = new NetworkWriter(); // need to serialize the message again with wrong netid MessagePacking.Pack(message, writer); ArraySegment <byte> segmentWrongNetId = writer.ToArraySegment(); comp0.called = 0; comp1.called = 0; transport.OnServerDataReceived.Invoke(0, segmentWrongNetId, 0); Assert.That(comp0.called, Is.EqualTo(0)); Assert.That(comp1.called, Is.EqualTo(0)); // clean up NetworkIdentity.spawned.Clear(); RemoteCallHelper.RemoveDelegate(registeredHash); }