Exemplo n.º 1
0
        public void CommsServer_BeginListeningForBroadcastsThrowsIfDisposed()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);
            server.Dispose();

            server.BeginListeningForBroadcasts();
        }
Exemplo n.º 2
0
        public void CommsServer_DisposeStopsListeningWithoutError()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            server.BeginListeningForBroadcasts();
            server.Dispose();

            Assert.IsTrue(((DisposableManagedObjectBase)socketFactory.MulticastSocket).IsDisposed);

            var mockSocket = socketFactory.MulticastSocket as MockSocket;
            Assert.IsTrue(mockSocket.IsDisposed);
        }
Exemplo n.º 3
0
        public void CommsServer_StopListeningForBroadcastsDisposesMulticastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            server.BeginListeningForBroadcasts();
            server.StopListeningForBroadcasts();

            Assert.IsTrue(((DisposableManagedObjectBase)socketFactory.MulticastSocket).IsDisposed);
        }
Exemplo n.º 4
0
        public void CommsServer_NonHttpPacketsIgnored()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            var requestReceived = false;
            var responseReceived = false;

            server.RequestReceived += (sender, e) =>
                {
                    requestReceived = true;
                };

            server.ResponseReceived += (sender, e) =>
                {
                    responseReceived = true;
                };

            server.BeginListeningForBroadcasts();

            var mockSocket = socketFactory.MulticastSocket as MockSocket;
            mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes("Not an HTTP message"), new
                UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort });

            Assert.IsFalse(requestReceived);
            Assert.IsFalse(responseReceived);
        }
Exemplo n.º 5
0
        public void CommsServer_ListeningForBroadcastsCreatesMulticastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            Assert.IsNull(socketFactory.MulticastSocket);
            server.BeginListeningForBroadcasts();
            Assert.IsNotNull(socketFactory.MulticastSocket);
        }
Exemplo n.º 6
0
        public void CommsServer_InvalidHttpRequestIgnored()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            var requestReceived = false;
            var responseReceived = false;

            server.RequestReceived += (sender, e) =>
            {
                requestReceived = true;
            };

            server.ResponseReceived += (sender, e) =>
            {
                responseReceived = true;
            };

            var message = String.Format(@"M-SEARCH * HTTP/1.1
            ",
            "",
            SsdpConstants.MulticastLocalAdminAddress,
            SsdpConstants.MulticastPort,
            "test search target",
            "1"
             );

            server.BeginListeningForBroadcasts();

            var mockSocket = socketFactory.MulticastSocket as MockSocket;
            mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new
                UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort });

            Assert.IsFalse(requestReceived);
            Assert.IsFalse(responseReceived);
        }
Exemplo n.º 7
0
        public void CommsServer_HttpResponseRaisesResponseReceived()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            var requestReceived = false;
            var responseReceived = false;

            var eventReceivedSignal = new System.Threading.ManualResetEvent(false);

            System.Net.Http.HttpResponseMessage receivedMessage = null;
            UdpEndPoint receivedFrom = null;
            server.RequestReceived += (sender, e) =>
            {
                requestReceived = true;
            };

            server.ResponseReceived += (sender, e) =>
            {
                receivedMessage = e.Message;
                receivedFrom = e.ReceivedFrom;
                responseReceived = true;
                eventReceivedSignal.Set();
            };

            server.BeginListeningForBroadcasts();

            var message = String.Format(@"HTTP/1.1 200 OK
            EXT:
            DATE: {0}
            CACHE-CONTROL: public, max-age=1800
            ST:test search target
            SERVER: TestOS/1.0 UPnP/1.0 RSSDP/1.0
            USN:testusn
            LOCATION:http://somedevice:1700

            ",
            DateTime.UtcNow.ToString("r")
             );

            var mockSocket = socketFactory.MulticastSocket as MockSocket;
            mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new
                UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort });

            eventReceivedSignal.WaitOne(10000);

            Assert.IsFalse(requestReceived);
            Assert.IsTrue(responseReceived);
            Assert.IsTrue(receivedMessage.IsSuccessStatusCode);
            Assert.AreEqual(System.Net.HttpStatusCode.OK, receivedMessage.StatusCode);
            Assert.AreEqual(SsdpConstants.MulticastLocalAdminAddress, receivedFrom.IPAddress);
            Assert.AreEqual(SsdpConstants.MulticastPort, receivedFrom.Port);
        }
Exemplo n.º 8
0
        public void CommsServer_HttpRequestWithNonAsterixUriIgnored()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            var requestReceived = false;
            var responseReceived = false;

            var eventReceivedSignal = new System.Threading.ManualResetEvent(false);
            server.RequestReceived += (sender, e) =>
            {
                requestReceived = true;
                eventReceivedSignal.Set();
            };

            server.ResponseReceived += (sender, e) =>
            {
                responseReceived = true;
                eventReceivedSignal.Set();
            };

            server.BeginListeningForBroadcasts();

            var message = String.Format(@"M-SEARCH http://someuri HTTP/1.1
            HOST: {0}:{1}
            MAN: ""ssdp:discover""
            MX: {3}
            ST: {2}

            ",
            "",
            SsdpConstants.MulticastLocalAdminAddress,
            SsdpConstants.MulticastPort,
            "test search target",
            "1"
             );

            var mockSocket = socketFactory.MulticastSocket as MockSocket;
            mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new
                UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort });

            eventReceivedSignal.WaitOne(1000);

            Assert.IsFalse(requestReceived);
            Assert.IsFalse(responseReceived);
        }
Exemplo n.º 9
0
        public void CommsServer_HttpRequestRaisesRequestReceived()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            var requestReceived = false;
            var responseReceived = false;

            var eventReceivedSignal = new System.Threading.ManualResetEvent(false);

            System.Net.Http.HttpRequestMessage receivedMessage = null;
            UdpEndPoint receivedFrom = null;
            server.RequestReceived += (sender, e) =>
            {
                receivedMessage = e.Message;
                receivedFrom = e.ReceivedFrom;
                requestReceived = true;
                eventReceivedSignal.Set();
            };

            server.ResponseReceived += (sender, e) =>
            {
                responseReceived = true;
            };

            server.BeginListeningForBroadcasts();

            var message = String.Format(@"M-SEARCH * HTTP/1.1
            HOST: {0}:{1}
            MAN: ""ssdp:discover""
            MX: {3}
            ST: {2}

            ",
            "",
            SsdpConstants.MulticastLocalAdminAddress,
            SsdpConstants.MulticastPort,
            "test search target",
            "1"
             );

            var mockSocket = socketFactory.MulticastSocket as MockSocket;
            mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new
                UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort });

            eventReceivedSignal.WaitOne(10000);

            Assert.IsTrue(requestReceived);
            Assert.IsFalse(responseReceived);
            Assert.AreEqual("M-SEARCH", receivedMessage.Method.Method);
            Assert.AreEqual("*", receivedMessage.RequestUri.ToString());
            Assert.AreEqual(SsdpConstants.MulticastLocalAdminAddress, receivedFrom.IPAddress);
            Assert.AreEqual(SsdpConstants.MulticastPort, receivedFrom.Port);
        }
Exemplo n.º 10
0
        public void CommsServer_HandlesRequestContentHeadersOk()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            var requestReceived = false;
            var responseReceived = false;

            using (var eventReceivedSignal = new System.Threading.ManualResetEvent(false))
            {

                System.Net.Http.HttpRequestMessage receivedMessage = null;
                server.RequestReceived += (sender, e) =>
            {
                receivedMessage = e.Message;
                requestReceived = true;
                eventReceivedSignal.Set();
            };

                server.ResponseReceived += (sender, e) =>
                {
                    responseReceived = true;
                };

                var message = String.Format(@"M-SEARCH * HTTP/1.1
            HOST: {0}:{1}
            MAN: ""ssdp:discover""
            MX: {3}
            ST: {2}
            CONTENT-ENCODING:UTF8

            some content here
            ",
            SsdpConstants.MulticastLocalAdminAddress,
            SsdpConstants.MulticastPort,
            "test search target",
            "1"
             );

                server.BeginListeningForBroadcasts();

                var mockSocket = socketFactory.MulticastSocket as MockSocket;
                mockSocket.MockReceive(System.Text.UTF8Encoding.UTF8.GetBytes(message), new
                    UdpEndPoint() { IPAddress = SsdpConstants.MulticastLocalAdminAddress, Port = SsdpConstants.MulticastPort });

                eventReceivedSignal.WaitOne(10000);
                Assert.IsTrue(requestReceived);
                Assert.IsFalse(responseReceived);
                Assert.AreEqual("UTF8", receivedMessage.Content.Headers.ContentEncoding.FirstOrDefault());
            }
        }