Beispiel #1
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);
        }
Beispiel #2
0
        public void Ctor_WhenIpAddressIsV4_DeviceNetworkTypeEqualsV41()
        {
            var socketFactory       = new SocketFactory(IPAddress.Any.ToString());
            var communicationServer = new SsdpCommunicationsServer(socketFactory);

            Assert.AreEqual(DeviceNetworkType.IPv4, communicationServer.DeviceNetworkType);
        }
Beispiel #3
0
        public void Ctor_WhenIpAddressIsV6_DeviceNetworkTypeEqualsV62()
        {
            var socketFactory       = new SocketFactory("::1");
            var communicationServer = new SsdpCommunicationsServer(socketFactory);

            Assert.AreEqual(DeviceNetworkType.IPv6, communicationServer.DeviceNetworkType);
        }
Beispiel #4
0
        public void CommsServer_HttpResponseInvalidVersionIgnored()
        {
            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 = @"HTTP/A.1 200 OK

";

            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);
        }
Beispiel #5
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);
        }
Beispiel #6
0
        public void CommsServer_ListeningForBroadcastsCreatesMulticastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server        = new SsdpCommunicationsServer(socketFactory);

            Assert.IsNull(socketFactory.MulticastSocket);
            server.BeginListeningForBroadcasts();
            Assert.IsNotNull(socketFactory.MulticastSocket);
        }
Beispiel #7
0
        public void CommsServer_StopListeningForResponsesThrowsIfDisposed()
        {
            var socketFactory = new MockSocketFactory();
            var server        = new SsdpCommunicationsServer(socketFactory);

            server.Dispose();

            server.StopListeningForResponses();
        }
Beispiel #8
0
        public void CommsServer_BeginListeningForBroadcastsThrowsIfDisposed()
        {
            var socketFactory = new MockSocketFactory();
            var server        = new SsdpCommunicationsServer(socketFactory);

            server.Dispose();

            server.BeginListeningForBroadcasts();
        }
Beispiel #9
0
        public void CommsServer_StopListeningForBroadcastsDisposesMulticastSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server        = new SsdpCommunicationsServer(socketFactory);

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

            Assert.IsTrue(((DisposableManagedObjectBase)socketFactory.MulticastSocket).IsDisposed);
        }
Beispiel #10
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);
        }
Beispiel #11
0
        public void CommsServer_ReconnectsOnClosedSocket()
        {
            var socketFactory = new MockSocketFactory();
            var server        = new SsdpCommunicationsServer(socketFactory);

            server.BeginListeningForBroadcasts();

            var socket1 = socketFactory.MulticastSocket;

            System.Threading.Thread.Sleep(500);

            socketFactory.ThrowSocketClosedException();
            System.Threading.Thread.Sleep(2000);

            var requestReceived = 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();
                };

                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"
                                            );

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

                eventReceivedSignal.WaitOne(120000);
                Assert.AreNotEqual(socket1, socketFactory.MulticastSocket);
                Assert.IsTrue(requestReceived);
            }
        }
Beispiel #12
0
        public void CommsServer_SendNullMessageThrowsException()
        {
            var socketFactory = new MockSocketFactory();
            var server        = new SsdpCommunicationsServer(socketFactory);

            UdpEndPoint destination = new UdpEndPoint()
            {
                IPAddress = "192.168.1.100", Port = 1701
            };

            server.SendMessage(null, destination);
        }
Beispiel #13
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());
            }
        }
Beispiel #14
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);
        }
Beispiel #15
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);
        }
Beispiel #16
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);
        }
Beispiel #17
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);
        }
Beispiel #18
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);
        }
Beispiel #19
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);
        }
Beispiel #20
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);
        }
Beispiel #21
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);
        }
Beispiel #22
0
 public void CommsServer_PartialConstructor_CompleteSuccessfullyWithValidArguments()
 {
     var socketFactory = new MockSocketFactory();
     var server        = new SsdpCommunicationsServer(socketFactory, 1701);
 }
Beispiel #23
0
 public void CommsServer_FullConstructor_ZeroMulticastTtlThrowsException()
 {
     var socketFactory = new MockSocketFactory();
     var server        = new SsdpCommunicationsServer(socketFactory, 1701, 0);
 }
Beispiel #24
0
 public void CommsServer_MinimumConstructor_NullSocketFactoryThrowsExeption()
 {
     var server = new SsdpCommunicationsServer(null);
 }
Beispiel #25
0
 public void CommsServer_FullConstructor_NullSocketFactoryThrowsExeption()
 {
     var server = new SsdpCommunicationsServer(null, 1701, 4);
 }