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

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

            server.SendMessage(System.Text.UTF8Encoding.UTF8.GetBytes("Hello!"), new UdpEndPoint() { IPAddress = "192.168.1.100", Port = 1701 });
            server.Dispose();

            var mockSocket = socketFactory.UnicastSocket as MockSocket;
            Assert.IsTrue(mockSocket.IsDisposed);
        }
Ejemplo n.º 3
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);
        }
    public ServerEmulator(Func <string, JObject, JObject> onEventReceived, Action <Uri> onClientConnected = null, Action <Uri> onClientDisconnected = null)
    {
        this.ssdpServer = new Rssdp.Infrastructure.SsdpCommunicationsServer(new Rssdp.SocketFactory(IPAddress.Any.ToString()), 1900, 1);
        this.httpBridge = new HttpBridge(this.OnHttpServerRequested, 44642);

        this.OnClientDisconnected = onClientDisconnected;
        this.OnClientConnected    = onClientConnected;
        this.OnEventReceived      = onEventReceived;

        // Starts to listen
        this.ssdpServer.Request​Received += OnSsdpServerRequested;
        this.ssdpServer.BeginListeningForBroadcasts();
    }
Ejemplo n.º 5
0
        public void CommsServer_SendMulticastMessageSendsToSsdpMulticastGroupOnUnicastSoket()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            string message = "Hello Everyone!";
            server.SendMulticastMessage(System.Text.UTF8Encoding.UTF8.GetBytes(message));

            Assert.IsNotNull(socketFactory.UnicastSocket);

            var mockSocket = socketFactory.UnicastSocket as MockSocket;
            Assert.AreEqual(message, System.Text.UTF8Encoding.UTF8.GetString(mockSocket.LastMessage));
            Assert.AreEqual(SsdpConstants.MulticastLocalAdminAddress, mockSocket.LastSentTo.IPAddress);
            Assert.AreEqual(SsdpConstants.MulticastPort, mockSocket.LastSentTo.Port);
        }
Ejemplo n.º 6
0
        public void CommsServer_SendMessageSendsOnUnicastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            string message = "Hello!";
            UdpEndPoint destination = new UdpEndPoint() { IPAddress = "192.168.1.100", Port = 1701 };
            server.SendMessage(System.Text.UTF8Encoding.UTF8.GetBytes(message), destination);

            Assert.IsNotNull(socketFactory.UnicastSocket);

            var mockSocket = socketFactory.UnicastSocket as MockSocket;
            Assert.AreEqual(message, System.Text.UTF8Encoding.UTF8.GetString(mockSocket.LastMessage));
            Assert.AreEqual(destination.IPAddress, mockSocket.LastSentTo.IPAddress);
            Assert.AreEqual(destination.Port, mockSocket.LastSentTo.Port);
        }
Ejemplo n.º 7
0
 public void CommsServer_PartialConstructor_CompleteSuccessfullyWithValidArguments()
 {
     var socketFactory = new MockSocketFactory();
     var server = new SsdpCommunicationsServer(socketFactory, 1701);
 }
Ejemplo n.º 8
0
 public void CommsServer_FullConstructor_ZeroMulticastTtlThrowsException()
 {
     var socketFactory = new MockSocketFactory();
     var server = new SsdpCommunicationsServer(socketFactory, 1701, 0);
 }
Ejemplo n.º 9
0
 public void CommsServer_FullConstructor_NullSocketFactoryThrowsExeption()
 {
     var server = new SsdpCommunicationsServer(null, 1701, 4);
 }
Ejemplo n.º 10
0
        public void CommsServer_StopListeningForResponsesDisposesUnicastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            string message = "Hello!";
            UdpEndPoint destination = new UdpEndPoint() { IPAddress = "192.168.1.100", Port = 1701 };
            server.SendMessage(System.Text.UTF8Encoding.UTF8.GetBytes(message), destination);

            var mockSocket = socketFactory.UnicastSocket as MockSocket;

            Assert.IsFalse(mockSocket.IsDisposed);

            server.StopListeningForResponses();

            Assert.IsTrue(mockSocket.IsDisposed);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        public void CommsServer_SendNullMulticastMessageThrowsException()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            UdpEndPoint destination = new UdpEndPoint() { IPAddress = "192.168.1.100", Port = 1701 };
            server.SendMulticastMessage(null);
        }
Ejemplo n.º 16
0
        public void CommsServer_StopListeningForBroadcastsDisposesMulticastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

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

            Assert.IsTrue(((DisposableManagedObjectBase)socketFactory.MulticastSocket).IsDisposed);
        }
Ejemplo n.º 17
0
 public void CommsServer_IsSharedPropertyReturnsSetValue()
 {
     var socketFactory = new MockSocketFactory();
     var server = new SsdpCommunicationsServer(socketFactory, 1701, 4);
     server.IsShared = true;
     Assert.IsTrue(server.IsShared);
     server.IsShared = false;
     Assert.IsFalse(server.IsShared);
 }
Ejemplo n.º 18
0
        public void CommsServer_StopListeningForResponsesThrowsIfDisposed()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);
            server.Dispose();

            server.StopListeningForResponses();
        }
Ejemplo n.º 19
0
        public void CommsServer_ListeningForBroadcastsCreatesMulticastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory);

            Assert.IsNull(socketFactory.MulticastSocket);
            server.BeginListeningForBroadcasts();
            Assert.IsNotNull(socketFactory.MulticastSocket);
        }
Ejemplo n.º 20
0
        public void CommsServer_FullConstructor_SpecifiedPortUsedForUnicastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server = new SsdpCommunicationsServer(socketFactory, 1702, 4);
            server.SendMessage(System.Text.UTF8Encoding.UTF8.GetBytes("Hello!"), new UdpEndPoint() { IPAddress = "192.168.1.100", Port = 1900 });
            var mockSocket = socketFactory.UnicastSocket as MockSocket;

            Assert.AreEqual(1702, mockSocket.LocalPort);
        }
Ejemplo n.º 21
0
 public void CommsServer_MinimumConstructor_NullSocketFactoryThrowsExeption()
 {
     var server = new SsdpCommunicationsServer(null);
 }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
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());
            }
        }