Пример #1
0
        static public void TestTunnelData()
        {
            // Test binary tunneling
            byte[] data = { 1, 2, 3 };
            using (var hs = new HttpSocket(
                       new EchoSocket(false, data).socket))
            {
                // We tunnel data... to ourselves!
                hs.TunnelDataTo(hs, 3);
                uint r = hs.ReadBinary();
                Assert.AreEqual(3, r);
                Assert.AreEqual(1, hs.Buffer[0]);
                Assert.AreEqual(2, hs.Buffer[1]);
                Assert.AreEqual(3, hs.Buffer[2]);
            }

            // Test the mixing of line-based and raw-based tunneling
            byte[] data2 = { (byte)'A', 13, 10, 42, 42, (byte)'B', 13, 10 };
            using (var hs = new HttpSocket(
                       new EchoSocket(false, data2).socket))
            {
                // Ditto
                hs.TunnelDataTo(hs, (uint)data2.Length);
                string msgA = hs.ReadAsciiLine();
                Assert.AreEqual("A", msgA);
                hs.TunnelDataTo(hs, 2);
                string msgB = hs.ReadAsciiLine();
                Assert.AreEqual("B", msgB);
            }
        }
Пример #2
0
        /// <summary>
        /// Send a message through the TCP channel, and expect it to
        /// return unchanged
        /// </summary>
        /// <remarks>
        /// Useful for testing I/O levels 1 and 2.
        /// This method must be called between Setup() and TearDown().
        /// </remarks>
        static public void DoMsgRoundTrip(string msg_send, string msg_expect)
        {
            byte[] to_send = System.Text.Encoding.ASCII.GetBytes(msg_send);

            using (var hs = new HttpSocket(
                       new EchoSocket(bUseIPv6, to_send).socket))
            {
                var msg_receive = hs.ReadAsciiLine();
                Assert.AreEqual(msg_expect, msg_receive);
                hs.CloseSocket();
            }
        }
Пример #3
0
        static public void TestHttpResponseLine()
        {
            // Test the mixing of structured and unstructured
            // line-based reads
            var msg      = "Captain Copyright";
            var http_msg = "HTTP/1.0 200 OK\r\n\r\n" + msg + "\r\n";

            using (var hs = new HttpSocket(
                       new EchoSocket(false, http_msg).socket))
            {
                var hsl = new HttpStatusLine(hs);
                Assert.AreEqual(200, hsl.StatusCode);
                /*var hh = */ new HttpHeaders(hs);
                var line = hs.ReadAsciiLine();
                Assert.AreEqual(msg, line);
            }
        }
Пример #4
0
        public static void TestHttpResponseLine()
        {
            // Test the mixing of structured and unstructured
            // line-based reads
            var msg = "Captain Copyright";
            var http_msg = "HTTP/1.0 200 OK\r\n\r\n" + msg + "\r\n";

            using (var hs = new HttpSocket(
                new EchoSocket(false, http_msg).socket))
            {
                var hsl = new HttpStatusLine(hs);
                Assert.AreEqual(200, hsl.StatusCode);
                /*var hh = */new HttpHeaders(hs);
                var line = hs.ReadAsciiLine();
                Assert.AreEqual(msg, line);
            }
        }
Пример #5
0
        public static void TestPortAlreadyAssigned()
        {
            int       port     = 20000;
            bool      bUseIPv6 = false;
            TcpServer s1       = new TcpServer(port, bUseIPv6);
            TcpServer s2       = new TcpServer(port, bUseIPv6);

            s1.Start(OnConnectionTalk);
            s1.InitListenFinished.WaitOne();
            if (s1.InitListenException != null)
            {
                throw s1.InitListenException;
            }
            Assert.IsTrue(s1.IsListening);

            // Starting a second server on the same port should fail
            Console.WriteLine("Note: SocketException expected");
            s2.Start(OnConnectionFail);
            s2.InitListenFinished.WaitOne();
            Assert.IsNotNull(s2.InitListenException);
            var e = s2.InitListenException as SocketException;

            Assert.IsNotNull(e);
            Assert.AreEqual(10048, e.ErrorCode);
            // 10048 means "Only one usage of each socket address ..."
            Assert.IsFalse(s2.IsListening);

            // Check that the first server still works
            using (var hs = new HttpSocket(
                       new EchoSocket(port, bUseIPv6).socket))
            {
                string s = hs.ReadAsciiLine();
                Assert.AreEqual(ss, s);
            }

            Assert.IsTrue(s1.IsListening);
            s1.Stop();                      // stop the server in case we want to re-run the test
            Assert.IsFalse(s1.IsListening); // while we are here...
        }
Пример #6
0
        public static void TestPortAlreadyAssigned()
        {
            int port = 20000;
            bool bUseIPv6 = false;
            TcpServer s1 = new TcpServer(port, bUseIPv6);
            TcpServer s2 = new TcpServer(port, bUseIPv6);

            s1.Start(OnConnectionTalk);
            s1.InitListenFinished.WaitOne();
            if (s1.InitListenException != null)
                throw s1.InitListenException;
            Assert.IsTrue(s1.IsListening);

            // Starting a second server on the same port should fail
            Console.WriteLine("Note: SocketException expected");
            s2.Start(OnConnectionFail);
            s2.InitListenFinished.WaitOne();
            Assert.IsNotNull(s2.InitListenException);
            var e = s2.InitListenException as SocketException;
            Assert.IsNotNull(e);
            Assert.AreEqual(10048, e.ErrorCode);
                // 10048 means "Only one usage of each socket address ..."
            Assert.IsFalse(s2.IsListening);

            // Check that the first server still works
            using (var hs = new HttpSocket(
                new EchoSocket(port, bUseIPv6).socket))
            {
                string s = hs.ReadAsciiLine();
                Assert.AreEqual(ss, s);
            }

            Assert.IsTrue(s1.IsListening);
            s1.Stop(); // stop the server in case we want to re-run the test
            Assert.IsFalse(s1.IsListening); // while we are here...
        }
Пример #7
0
        public static void TestTunnelData()
        {
            // Test binary tunneling
            byte[] data = { 1, 2, 3 };
            using (var hs = new HttpSocket(
                new EchoSocket(false, data).socket))
            {
                // We tunnel data... to ourselves!
                hs.TunnelDataTo(hs, 3);
                uint r = hs.ReadBinary();
                Assert.AreEqual(3, r);
                Assert.AreEqual(1, hs.Buffer[0]);
                Assert.AreEqual(2, hs.Buffer[1]);
                Assert.AreEqual(3, hs.Buffer[2]);
            }

            // Test the mixing of line-based and raw-based tunneling
            byte[] data2 = { (byte)'A', 13, 10, 42, 42, (byte)'B', 13, 10 };
            using (var hs = new HttpSocket(
                new EchoSocket(false, data2).socket))
            {
                // Ditto
                hs.TunnelDataTo(hs, (uint)data2.Length);
                string msgA = hs.ReadAsciiLine();
                Assert.AreEqual("A", msgA);
                hs.TunnelDataTo(hs, 2);
                string msgB = hs.ReadAsciiLine();
                Assert.AreEqual("B", msgB);
            }
        }