コード例 #1
0
        public void TestEventLoop(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState, 2119);

            int calledCount = 0;

            // This OnNetworkAction asks for more data, creating an event loop
            testLocalSocketState.OnNetworkAction = (x) => {
                if (x.ErrorOccured)
                {
                    return;
                }
                calledCount++;
                Networking.GetData(x);
            };

            Networking.Send(testRemoteSocketState.TheSocket, "a");
            Networking.GetData(testLocalSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => calledCount == 1, NetworkTestHelper.timeout);

            Networking.Send(testRemoteSocketState.TheSocket, "a");
            NetworkTestHelper.WaitForOrTimeout(() => calledCount == 2, NetworkTestHelper.timeout);

            Assert.AreEqual(2, calledCount);
        }
コード例 #2
0
        public void TestMultipleClientConnections()
        {
            SocketState state = new SocketState(null, null);
            int         i     = 0;
            int         j     = 0;

            void toServerCall(SocketState s)
            {
                i++;
            }

            void toClientCall(SocketState s)
            {
                j++;
                NetworkTestHelper.WaitForOrTimeout(() => false, 2000);
                Assert.IsTrue(i == j);
            }

            TcpListener listener = Networking.StartServer(toServerCall, 2112);

            Networking.ConnectToServer(toClientCall, "localhost", 2112);
            Networking.ConnectToServer(toClientCall, "localhost", 2112);
            Networking.ConnectToServer(toClientCall, "localhost", 2112);
            Networking.ConnectToServer(toClientCall, "localhost", 2112);
            Networking.ConnectToServer(toClientCall, "localhost", 2112);
        }
コード例 #3
0
        public void TestReceiveHugeMessage(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);

            testLocalSocketState.OnNetworkAction = (x) =>
            {
                if (x.ErrorOccured)
                {
                    return;
                }
                Networking.GetData(x);
            };

            Networking.GetData(testLocalSocketState);

            StringBuilder message = new StringBuilder();

            message.Append('a', (int)(SocketState.BufferSize * 7.5));

            Networking.Send(testRemoteSocketState.TheSocket, message.ToString());

            NetworkTestHelper.WaitForOrTimeout(() => testLocalSocketState.GetData().Length == message.Length, NetworkTestHelper.timeout);

            Assert.AreEqual(message.ToString(), testLocalSocketState.GetData());
        }
コード例 #4
0
 public void TestSendViaClosedSocket()
 {
     NetworkTestHelper.SetupSingleConnectionTest(out testListener, out testLocalSocketState, out testRemoteSocketState);
     testRemoteSocketState.TheSocket.Close();
     //Sending null value, should causes a Error in the RemoteState
     Assert.IsFalse(Networking.Send(testRemoteSocketState.TheSocket, null));
 }
コード例 #5
0
        public void testMultipleSends(bool clientSide)
        {
            SetupTestConnections(
                clientSide,
                out testListener,
                out testRemoteSocketState,
                out testLocalSocketState, 2380);

            string data = "";

            void ProcessMessages(SocketState s)
            {
                data += s.GetData() + " ";
                s.RemoveData(0, s.GetData().Length);
                if (!s.ErrorOccured)
                {
                    Networking.GetData(testRemoteSocketState);
                }
            }

            testRemoteSocketState.OnNetworkAction = ProcessMessages;
            bool firstSent;
            bool secondSent;

            firstSent  = Networking.Send(testLocalSocketState.TheSocket, "HelloThere");
            secondSent = Networking.Send(testLocalSocketState.TheSocket, "Pan");

            Networking.GetData(testRemoteSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => false, 3000);
            Assert.IsTrue(firstSent && secondSent);
            Assert.AreEqual("HelloThere Pan ", data);
        }
コード例 #6
0
        public void TestConnectCallsDelegate()
        {
            bool serverActionCalled = false;
            bool clientActionCalled = false;

            void saveServerState(SocketState x)
            {
                testLocalSocketState = x;
                serverActionCalled   = true;
            }

            void saveClientState(SocketState x)
            {
                testRemoteSocketState = x;
                clientActionCalled    = true;
            }

            testListener = Networking.StartServer(saveServerState, 2112);
            Networking.ConnectToServer(saveClientState, "localhost", 2112);
            NetworkTestHelper.WaitForOrTimeout(() => serverActionCalled, NetworkTestHelper.timeout);
            NetworkTestHelper.WaitForOrTimeout(() => clientActionCalled, NetworkTestHelper.timeout);

            Assert.IsTrue(serverActionCalled);
            Assert.IsTrue(clientActionCalled);
        }
コード例 #7
0
        public void TestConnect()
        {
            NetworkTestHelper.SetupSingleConnectionTest(out testListener, out testLocalSocketState, out testRemoteSocketState, 2112);

            Assert.IsTrue(testRemoteSocketState.TheSocket.Connected);
            Assert.IsTrue(testLocalSocketState.TheSocket.Connected);

            Assert.AreEqual("127.0.0.1:2112", testLocalSocketState.TheSocket.RemoteEndPoint.ToString());
        }
コード例 #8
0
        public void TestConnectToServerErrorNoServerExists()
        {
            void toCall(SocketState s)
            {
                testLocalSocketState = s;
            }

            Networking.ConnectToServer(toCall, "localhost", 2134);

            NetworkTestHelper.WaitForOrTimeout(() => false, 3000);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
        }
コード例 #9
0
        public void TestReceiveRemovesPartial(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState, 2122);

            const string toSend = "abcdefghijklmnopqrstuvwxyz";

            // Use a static seed for reproducibility
            Random rand = new Random(0);

            StringBuilder localCopy = new StringBuilder();

            void removeMessage(SocketState state)
            {
                if (state.ErrorOccured)
                {
                    return;
                }
                int numToRemove = rand.Next(state.GetData().Length);

                localCopy.Append(state.GetData().Substring(0, numToRemove));
                state.RemoveData(0, numToRemove);
                Networking.GetData(state);
            }

            testLocalSocketState.OnNetworkAction = removeMessage;

            // Start a receive loop
            Networking.GetData(testLocalSocketState);

            for (int i = 0; i < 1000; i++)
            {
                Networking.Send(testRemoteSocketState.TheSocket, toSend);
            }

            // Wait a while
            NetworkTestHelper.WaitForOrTimeout(() => false, NetworkTestHelper.timeout);

            localCopy.Append(testLocalSocketState.GetData());

            // Reconstruct the original message outside the send loop
            // to (in theory) make the send operations happen more rapidly.
            StringBuilder message = new StringBuilder();

            for (int i = 0; i < 1000; i++)
            {
                message.Append(toSend);
            }

            Assert.AreEqual(message.ToString(), localCopy.ToString());
        }
コード例 #10
0
        public void SendAndClose_SendFails_ShouldCloseSocket(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);
            testLocalSocketState.OnNetworkAction  = x => { };
            testRemoteSocketState.OnNetworkAction = x => { };

            string nullInjection     = null;
            bool   sendWasSuccessful = Networking.SendAndClose(testLocalSocketState.TheSocket, nullInjection);

            Networking.GetData(testRemoteSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => testRemoteSocketState.GetData().Length > 0, NetworkTestHelper.timeout);

            Assert.IsFalse(sendWasSuccessful);
            Assert.AreEqual("", testRemoteSocketState.GetData());
            Assert.IsFalse(testLocalSocketState.TheSocket.Connected);
        }
コード例 #11
0
        public void TestClose()
        {
            NetworkTestHelper.SetupSingleConnectionTest(out testListener, out testLocalSocketState, out testRemoteSocketState);
            Networking.StopServer(testListener);

            SocketState client = null;

            void SaveClient(SocketState s)
            {
                client = s;
            }

            //Using port 2112 because it is the same port being used in SetupSingleConnectionTest method use
            //Should produce a ErrorOccured SocketState because we have closed hearing any incoming connections
            Networking.ConnectToServer(SaveClient, "localhost", 2112);
            Assert.IsTrue(client.ErrorOccured);
        }
コード例 #12
0
        public void TestConnectNoServer()
        {
            bool isCalled = false;

            void saveClientState(SocketState x)
            {
                isCalled             = true;
                testLocalSocketState = x;
            }

            // Try to connect without setting up a server first.
            Networking.ConnectToServer(saveClientState, "localhost", 2126);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);

            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
        }
コード例 #13
0
        public void TestStopServer()
        {
            NetworkTestHelper.SetupSingleConnectionTest(
                out testListener,
                out testRemoteSocketState,
                out testLocalSocketState, 2200);

            Networking.StopServer(testListener);

            SocketState newState = new SocketState(null, null);

            Networking.ConnectToServer(s => newState = s, "localhost", 2200);

            NetworkTestHelper.WaitForOrTimeout(() => false, 3000);

            Assert.IsTrue(newState.ErrorOccured);
        }
コード例 #14
0
        public void TestSendAndReceiveHugeMessage()
        {
            //Set up
            int sendTimes = 5;

            StringBuilder ans = new StringBuilder();

            for (int i = 0; i < sendTimes; i++)
            {
                ans.Append((char)i, (int)(SocketState.BufferSize * 1.5));
            }
            //establish connection
            NetworkTestHelper.SetupSingleConnectionTest(out TcpListener listener, out SocketState clt, out SocketState svr);
            // Receive loop in clt
            clt.OnNetworkAction = x =>
            {
                //for error checking
                if (x.ErrorOccured)
                {
                    return;
                }

                Networking.GetData(x);
            };

            //clt start listening messages
            Networking.GetData(clt);
            //Start sending
            for (int i = 0; i < sendTimes; i++)
            {
                StringBuilder message = new StringBuilder();
                message.Append((char)i, (int)(SocketState.BufferSize * 1.5));
                Networking.Send(svr.TheSocket, message.ToString());
            }
            //Wait here
            NetworkTestHelper.WaitForOrTimeout(() => clt.GetData().Length == SocketState.BufferSize * 1.5 * 5, NetworkTestHelper.timeout);
            //Check answer
            Assert.AreEqual(ans.ToString(), clt.GetData());

            //clean up
            StopTestServer(listener, clt, svr);
            //wait
            Thread.Sleep(1);
            Assert.IsTrue(clt.ErrorOccured);
        }
コード例 #15
0
        public void ConnectToServer_HostNameIsInvalidIPAddress_ShouldReturnErrorSocketStateAndInvokeToCallDelegateOnce()
        {
            bool isCalled       = false;
            int  numTimesCalled = 0;

            void saveClientState(SocketState x)
            {
                isCalled = true;
                numTimesCalled++;
                testLocalSocketState = x;
            }

            Networking.ConnectToServer(saveClientState, "ybxiciwjwlpdooyqwwesxnvlezxiqe.com", 2112);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);
            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
            Assert.AreEqual(1, numTimesCalled);
        }
コード例 #16
0
        public void ConnectToServer_ErrorOccursDuringSocketBeginConnect_ShouldReturnErrorSocketStateAndInvokeToCallDelegateOnce()
        {
            bool isCalled       = false;
            int  numTimesCalled = 0;

            void saveClientState(SocketState x)
            {
                isCalled = true;
                numTimesCalled++;
                testLocalSocketState = x;
            }

            Networking.ConnectToServer(saveClientState, "localhost", 99999999);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);
            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
            Assert.AreEqual(1, numTimesCalled);
        }
コード例 #17
0
        public void ConnectToServer_BeginConnectWillStartButThenTimeout_ShouldInvokeToCallDelegateOnce()
        {
            bool isCalled       = false;
            int  numTimesCalled = 0;

            void saveClientState(SocketState x)
            {
                isCalled = true;
                numTimesCalled++;
                testLocalSocketState = x;
            }

            Networking.ConnectToServer(saveClientState, "google.com", 2112);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);
            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
            Assert.AreEqual(1, numTimesCalled);
        }
コード例 #18
0
        public void TestConnectTimeout()
        {
            bool isCalled = false;

            void saveClientState(SocketState x)
            {
                isCalled             = true;
                testLocalSocketState = x;
            }

            Networking.ConnectToServer(saveClientState, "google.com", 2112);

            // The connection should timeout after 3 seconds. NetworkTestHelper.timeout is 5 seconds.
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);

            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
        }
コード例 #19
0
        public void TestConnectToServerIPV4Address()
        {
            bool isCalled = false;

            void saveClientState(SocketState x)
            {
                isCalled             = true;
                testLocalSocketState = x;
            }

            NetworkTestHelper.SetupSingleConnectionTest(out testListener, out testLocalSocketState, out testRemoteSocketState);

            Networking.ConnectToServer(saveClientState, "192.168.0.19", 2112);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, 1000);

            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
        }
コード例 #20
0
        public void TestConnectToServerinvaildIPadress()
        {
            bool isCalled = false;

            void saveClientState(SocketState x)
            {
                isCalled             = true;
                testLocalSocketState = x;
            }

            NetworkTestHelper.SetupSingleConnectionTest(out testListener, out testLocalSocketState, out testRemoteSocketState);

            Networking.ConnectToServer(saveClientState, "3731:54:65fe:2:  :a7/64", 2112);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);

            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
        }
コード例 #21
0
        public void SendAndClose_SendTinyMessage_ShouldPutDataInSocketStateAndCloseSocket(bool clientSide)
        {
            // assemble
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);
            testLocalSocketState.OnNetworkAction  = x => { };
            testRemoteSocketState.OnNetworkAction = x => { };
            bool sendWasSuccessful;

            // act
            sendWasSuccessful = Networking.SendAndClose(testLocalSocketState.TheSocket, "abc");
            Networking.GetData(testRemoteSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => testRemoteSocketState.GetData().Length > 0, NetworkTestHelper.timeout);

            // assert
            Assert.IsTrue(sendWasSuccessful);
            Assert.AreEqual("abc", testRemoteSocketState.GetData());
            Assert.IsFalse(testLocalSocketState.TheSocket.Connected);
        }
コード例 #22
0
        public void ConnectToServer_CouldNotFindIPV4Address_ShouldReturnErrorSocketStateAndInvokeToCallDelegateOnce()
        {
            bool isCalled       = false;
            int  numTimesCalled = 0;

            void saveClientState(SocketState x)
            {
                isCalled = true;
                numTimesCalled++;
                testLocalSocketState = x;
            }

            Networking.ConnectToServer(saveClientState, "ipv6.google.com", 2112);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);
            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
            Assert.AreEqual(1, numTimesCalled);
        }
コード例 #23
0
        public void TestConnectStartAndStopNoServer()
        {
            bool isCalled = false;

            void saveClientState(SocketState x)
            {
                isCalled             = true;
                testLocalSocketState = x;
            }

            NetworkTestHelper.SetupSingleConnectionTest(out testListener, out testLocalSocketState, out testRemoteSocketState);
            Networking.StopServer(testListener);
            // Try to connect without setting up a server first.
            Networking.ConnectToServer(saveClientState, "localhost", 2112);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);

            Assert.IsTrue(isCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
        }
コード例 #24
0
        public void TestReceiveRemovesAll(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);

            StringBuilder localCopy = new StringBuilder();

            void removeMessage(SocketState state)
            {
                if (state.ErrorOccured)
                {
                    return;
                }
                localCopy.Append(state.GetData());
                state.RemoveData(0, state.GetData().Length);
                Networking.GetData(state);
            }

            testLocalSocketState.OnNetworkAction = removeMessage;

            // Start a receive loop
            Networking.GetData(testLocalSocketState);


            for (int i = 0; i < 10000; i++)
            {
                char c = (char)('a' + (i % 26));
                Networking.Send(testRemoteSocketState.TheSocket, "" + c);
            }

            NetworkTestHelper.WaitForOrTimeout(() => localCopy.Length == 10000, NetworkTestHelper.timeout);

            // Reconstruct the original message outside the send loop
            // to (in theory) make the send operations happen more rapidly.
            StringBuilder message = new StringBuilder();

            for (int i = 0; i < 10000; i++)
            {
                char c = (char)('a' + (i % 26));
                message.Append(c);
            }

            Assert.AreEqual(message.ToString(), localCopy.ToString());
        }
コード例 #25
0
        public void SendAndClose_SocketIsAlreadyClosed_ShouldNotAttemptToSend(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);
            testLocalSocketState.OnNetworkAction  = x => { };
            testRemoteSocketState.OnNetworkAction = x => { };
            bool sendWasSuccessful = true;

            testLocalSocketState.TheSocket.Shutdown(SocketShutdown.Both);
            testLocalSocketState.TheSocket.Close();
            sendWasSuccessful = Networking.SendAndClose(testLocalSocketState.TheSocket, "abc");
            Networking.GetData(testRemoteSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => testRemoteSocketState.GetData().Length > 0, NetworkTestHelper.timeout);

            Assert.IsFalse(sendWasSuccessful);
            Assert.AreEqual("", testRemoteSocketState.GetData());
            Assert.IsFalse(testLocalSocketState.TheSocket.Connected);
            // TODO is this check helpful?
            Assert.ThrowsException <ObjectDisposedException>(() => testLocalSocketState.TheSocket.Available);
        }
コード例 #26
0
        public void TestSendTinyMessage(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState, 2117);

            // Set the action to do nothing
            testLocalSocketState.OnNetworkAction  = x => { };
            testRemoteSocketState.OnNetworkAction = x => { };

            Networking.Send(testLocalSocketState.TheSocket, "a");

            Networking.GetData(testRemoteSocketState);

            // Note that waiting for data like this is *NOT* how the networking library is
            // intended to be used. This is only for testing purposes.
            // Normally, you would provide an OnNetworkAction that handles the data.
            NetworkTestHelper.WaitForOrTimeout(() => testRemoteSocketState.GetData().Length > 0, NetworkTestHelper.timeout);

            Assert.AreEqual("a", testRemoteSocketState.GetData());
        }
コード例 #27
0
        public void TestStartServer()
        {
            SocketState state = new SocketState(null, null);

            void toCall(SocketState s)
            {
            }

            bool passed = false;

            try {
                TcpListener listener = Networking.StartServer(toCall, 10000000);
                NetworkTestHelper.WaitForOrTimeout(() => false, 2000);
            }
            catch (Exception e) {
                passed = true;
            }

            Assert.IsTrue(passed);
        }
コード例 #28
0
 public void SetupTestConnections(bool clientSide,
                                  out TcpListener listener, out SocketState local, out SocketState remote)
 {
     if (clientSide)
     {
         NetworkTestHelper.SetupSingleConnectionTest(
             out listener,
             out local,   // local becomes client
             out remote); // remote becomes server
     }
     else
     {
         NetworkTestHelper.SetupSingleConnectionTest(
             out listener,
             out remote, // remote becomes client
             out local); // local becomes server
     }
     Assert.IsNotNull(local);
     Assert.IsNotNull(remote);
 }
コード例 #29
0
        public void GetData_SocketIsAlreadyClosedSoBeginReceiveWillFail_ShouldSetErrorSocketStateAndInvokeToCallDelegate(bool clientSide)
        {
            bool isCalled       = false;
            int  numTimesCalled = 0;

            void saveClientState(SocketState x)
            {
                isCalled = true;
                numTimesCalled++;
                testLocalSocketState = x;
            }

            testLocalSocketState = new SocketState(saveClientState, null);

            Networking.GetData(testLocalSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => isCalled, NetworkTestHelper.timeout);

            Assert.AreEqual(1, numTimesCalled);
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
            Assert.AreEqual("", testLocalSocketState.GetData());
        }
コード例 #30
0
        public void TestShutDownSocketState()
        {
            NetworkTestHelper.SetupSingleConnectionTest(out testListener, out testLocalSocketState, out testRemoteSocketState);

            testLocalSocketState.OnNetworkAction = x =>
            {
                if (x.ErrorOccured)
                {
                    return;
                }
                Networking.GetData(x);
            };
            //Server Ready to receive data
            Networking.GetData(testLocalSocketState);
            //Shutting down RemoteSocket (sending), this should cause LocalSocket to receive 0 byte in EndReceive call, which will result in Error
            testRemoteSocketState.TheSocket.Shutdown(SocketShutdown.Both);

            NetworkTestHelper.WaitForOrTimeout(() => false, NetworkTestHelper.timeout);
            //Since remote is shut down this should receive 0 bytes, should causes a Error in the LocalState
            Assert.IsTrue(testLocalSocketState.ErrorOccured);
        }