示例#1
0
        private void BeginReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connectTo, bool expectedToTimeout = false)
        {
            using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                int port = serverSocket.BindToAnonymousPort(listenOn);

                EndPoint receivedFrom = new IPEndPoint(connectTo, port);
                SocketFlags socketFlags = SocketFlags.None;
                IPPacketInformation ipPacketInformation;
                IAsyncResult async = serverSocket.BeginReceiveMessageFrom(new byte[1], 0, 1, socketFlags, ref receivedFrom, null, null);

                // Behavior difference from Desktop: receivedFrom will _not_ change during the synchronous phase.

                // IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                // Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                // Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
                bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout);
                if (!success)
                {
                    throw new TimeoutException();
                }

                receivedFrom = new IPEndPoint(connectTo, port);
                int received = serverSocket.EndReceiveMessageFrom(async, ref socketFlags, ref receivedFrom, out ipPacketInformation);

                Assert.Equal(1, received);
                Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));

                IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                Assert.Equal(SocketFlags.None, socketFlags);
                Assert.NotNull(ipPacketInformation);
                Assert.Equal(connectTo, ipPacketInformation.Address);

                // TODO: Move to NetworkInformation tests.
                //Assert.Equal(NetworkInterface.IPv6LoopbackInterfaceIndex, ipPacketInformation.Interface);
            }
        }
示例#2
0
        private void ReceiveFrom_Helper(IPAddress listenOn, IPAddress connectTo)
        {
            using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                serverSocket.ReceiveTimeout = 500;
                int port = serverSocket.BindToAnonymousPort(listenOn);

                SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);

                EndPoint receivedFrom = new IPEndPoint(connectTo, port);
                int received = serverSocket.ReceiveFrom(new byte[1], ref receivedFrom);

                Assert.Equal(1, received);
                Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));

                IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
            }
        }
示例#3
0
        private void ReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connectTo)
        {
            using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                serverSocket.ReceiveTimeout = 500;
                int port = serverSocket.BindToAnonymousPort(listenOn);

                EndPoint receivedFrom = new IPEndPoint(connectTo, port);
                SocketFlags socketFlags = SocketFlags.None;
                IPPacketInformation ipPacketInformation;
                int received = 0;

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Assert.Throws<SocketException>(() =>
                    {
                        // This is a false start.
                        // http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receivemessagefrom.aspx
                        // "...the returned IPPacketInformation object will only be valid for packets which arrive at the
                        // local computer after the socket option has been set. If a socket is sent packets between when
                        // it is bound to a local endpoint (explicitly by the Bind method or implicitly by one of the Connect,
                        // ConnectAsync, SendTo, or SendToAsync methods) and its first call to the ReceiveMessageFrom method,
                        // calls to ReceiveMessageFrom method will return invalid IPPacketInformation objects for these packets."
                        received = serverSocket.ReceiveMessageFrom(new byte[1], 0, 1, ref socketFlags, ref receivedFrom, out ipPacketInformation);
                    });
                }
                else
                {
                    // *nix may throw either a SocketException or ArgumentException in this case, depending on how the IP stack
                    // behaves w.r.t. dual-mode sockets bound to IPv6-specific addresses.
                    Assert.ThrowsAny<Exception>(() =>
                    {
                        received = serverSocket.ReceiveMessageFrom(new byte[1], 0, 1, ref socketFlags, ref receivedFrom, out ipPacketInformation);
                    });
                }

                SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);

                receivedFrom = new IPEndPoint(connectTo, port);
                socketFlags = SocketFlags.None;
                received = serverSocket.ReceiveMessageFrom(new byte[1], 0, 1, ref socketFlags, ref receivedFrom, out ipPacketInformation);

                Assert.Equal(1, received);
                Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));

                IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                Assert.Equal(SocketFlags.None, socketFlags);
                Assert.NotNull(ipPacketInformation);

                Assert.Equal(connectTo, ipPacketInformation.Address);

                // TODO: Move to NetworkInformation tests.
                // Assert.Equal(NetworkInterface.IPv6LoopbackInterfaceIndex, ipPacketInformation.Interface);
            }
        }
        private void BeginReceiveFrom_Helper(IPAddress listenOn, IPAddress connectTo, bool expectedToTimeout = false)
        {
            using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                serverSocket.ReceiveTimeout = 500;
                int port = serverSocket.BindToAnonymousPort(listenOn);

                EndPoint receivedFrom = new IPEndPoint(connectTo, port);
                IAsyncResult async = serverSocket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);

                IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                SocketUdpClient client = new SocketUdpClient(serverSocket, connectTo, port);
                bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout);
                if (!success)
                {
                    throw new TimeoutException();
                }

                receivedFrom = new IPEndPoint(connectTo, port);
                int received = serverSocket.EndReceiveFrom(async, ref receivedFrom);

                Assert.Equal(1, received);
                Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));

                remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
            }
        }
示例#5
0
        private void BeginReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connectTo)
        {
            using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                int port = serverSocket.BindToAnonymousPort(listenOn);

                EndPoint receivedFrom = new IPEndPoint(connectTo, port);
                SocketFlags socketFlags = SocketFlags.None;
                IPPacketInformation ipPacketInformation;
                IAsyncResult async = serverSocket.BeginReceiveMessageFrom(new byte[1], 0, 1, socketFlags, ref receivedFrom, null, null);

                IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                SocketUdpClient client = new SocketUdpClient(serverSocket, connectTo, port);
                bool success = async.AsyncWaitHandle.WaitOne(500);
                if (!success)
                {
                    throw new TimeoutException();
                }

                receivedFrom = new IPEndPoint(connectTo, port);
                int received = serverSocket.EndReceiveMessageFrom(async, ref socketFlags, ref receivedFrom, out ipPacketInformation);

                Assert.Equal(1, received);
                Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));

                remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                Assert.Equal(SocketFlags.None, socketFlags);
                Assert.NotNull(ipPacketInformation);
                Assert.Equal(connectTo, ipPacketInformation.Address);

                // TODO: Move to NetworkInformation tests.
                //Assert.Equal(NetworkInterface.IPv6LoopbackInterfaceIndex, ipPacketInformation.Interface);
            }
        }
示例#6
0
        private void BeginReceiveFrom_Helper(IPAddress listenOn, IPAddress connectTo, bool expectedToTimeout = false)
        {
            using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
            {
                serverSocket.ReceiveTimeout = 500;
                int port = serverSocket.BindToAnonymousPort(listenOn);

                EndPoint receivedFrom = new IPEndPoint(connectTo, port);
                IAsyncResult async = serverSocket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);

                // Behavior difference from Desktop: receivedFrom will _not_ change during the synchronous phase.

                // IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                // Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                // Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

                SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port, redundant: !expectedToTimeout);
                bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout);
                if (!success)
                {
                    throw new TimeoutException();
                }

                receivedFrom = new IPEndPoint(connectTo, port);
                int received = serverSocket.EndReceiveFrom(async, ref receivedFrom);

                Assert.Equal(1, received);
                Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));

                IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
                Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
                Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
            }
        }
示例#7
0
 public void Check_Convertibility_Must_Succeed()
 {
     _ipEndPointConverter.CanConvert(_ipEndpoint.GetType()).Should().BeTrue();
 }
        public MFTestResults NetTest5_IPEndPointBasic()
        {
            /// <summary>
            /// 1. Creates 30 Random IPs between 0.0.0.0 and 255.255.255.127
            /// 2. Verifies that they can be constructed as IPEndPoints with both ctors
            /// 3. Verifies that their data, ToString and GetHashCode funcs return normally
            /// 4. Clones one with Create and verifies the above funcs again
            /// </summary>
            ///
            bool testResult = true;

            try
            {
                Random random = new Random();
                for (int i = 0; i <= 30; i++)
                {
                    int[] IPInts = { random.Next(256), random.Next(256),
                                     random.Next(256), random.Next(128) };
                    int   portInt     = random.Next(65535) + 1;
                    long  addressLong = (long)(
                        IPInts[0]
                        + IPInts[1] * 256
                        + IPInts[2] * 256 * 256
                        + IPInts[3] * 256 * 256 * 256);
                    Log.Comment("Random IP " + IPInts[0] + "." + IPInts[1]
                                + "." + IPInts[2] + "." + IPInts[3] + ":" + portInt);
                    IPAddress address = new IPAddress(addressLong);

                    Log.Comment("EndPoint1 created with IPAddress and int");
                    IPEndPoint endPoint1 = new IPEndPoint(address, portInt);
                    Log.Comment("EndPoint2 created with long and int");
                    IPEndPoint endPoint2 = new IPEndPoint(addressLong, portInt);
                    if (endPoint1 == null)
                    {
                        throw new Exception("EndPoint1 is null");
                    }
                    if (endPoint2 == null)
                    {
                        throw new Exception("EndPoint2 is null");
                    }

                    Type typeOfEndPoint = endPoint1.GetType();
                    if (typeOfEndPoint != Type.GetType("System.Net.IPEndPoint"))
                    {
                        throw new Exception("EndPoint1 Type is incorrect");
                    }
                    typeOfEndPoint = endPoint2.GetType();
                    if (typeOfEndPoint != Type.GetType("System.Net.IPEndPoint"))
                    {
                        throw new Exception("EndPoint2 Type is incorrect");
                    }

                    if (endPoint1.ToString() != endPoint2.ToString())
                    {
                        throw new Exception("ToString returns differently for same data");
                    }

                    if (!endPoint1.Equals(endPoint2))
                    {
                        throw new Exception("Equals returns false for same data");
                    }


                    int hashCode1 = endPoint1.GetHashCode();
                    int hashCode2 = endPoint2.GetHashCode();


                    if (hashCode1 != hashCode2)
                    {
                        throw new Exception("GetHasCode returns differently for same data");
                    }

                    if (endPoint1.Address.ToString() != endPoint2.Address.ToString() ||
                        endPoint1.Address.ToString() != address.ToString() ||
                        endPoint2.Address.ToString() != address.ToString())
                    {
                        throw new Exception("Address returns wrong data");
                    }

                    if (endPoint1.Port != endPoint2.Port ||
                        endPoint1.Port != portInt ||
                        endPoint2.Port != portInt)
                    {
                        throw new Exception("Port returns wrong data");
                    }

                    Log.Comment("Cloning Enpoint1 into EndPoint2");
                    endPoint2      = (IPEndPoint)endPoint2.Create(endPoint1.Serialize());
                    typeOfEndPoint = endPoint2.GetType();
                    if (typeOfEndPoint != Type.GetType("System.Net.IPEndPoint"))
                    {
                        throw new Exception("EndPoint2 Type is incorrect after clone");
                    }

                    if (endPoint1.ToString() != endPoint2.ToString())
                    {
                        throw new Exception("ToString returns differently for cloned data");
                    }


                    //21295	GetHashCode returns differently for cloned data
                    if (endPoint1.GetHashCode() != endPoint2.GetHashCode())
                    {
                        throw new Exception("GetHashCode returns differently for cloned data");
                    }

                    if (endPoint1.Address.ToString() != endPoint2.Address.ToString() ||
                        endPoint1.Address.ToString() != address.ToString() ||
                        endPoint2.Address.ToString() != address.ToString())
                    {
                        throw new Exception("Address returns wrong data after clone");
                    }

                    if (endPoint1.Port != endPoint2.Port ||
                        endPoint1.Port != portInt ||
                        endPoint2.Port != portInt)
                    {
                        throw new Exception("Port returns wrong data after clone");
                    }

                    Log.Comment("Recreating EndPoint2 with new data");
                    int  portInt2     = portInt % 2 + 1;
                    long addressLong2 = (long)(
                        (IPInts[0] % 2 + 1)
                        + (IPInts[1] % 2 + 1) * 256
                        + (IPInts[2] % 2 + 1) * 256 * 256
                        + (IPInts[3] % 2 + 1) * 256 * 256 * 256);
                    endPoint2 = new IPEndPoint(addressLong2, portInt2);

                    if (endPoint1.GetHashCode() == endPoint2.GetHashCode())
                    {
                        throw new Exception("GetHashCode returns same for "
                                            + endPoint1.ToString()
                                            + " as " + endPoint2.ToString());
                    }

                    if (endPoint1.Address == endPoint2.Address ||
                        endPoint2.Address == address)
                    {
                        throw new Exception("Address returns wrong data after change");
                    }

                    if (endPoint1.Port == endPoint2.Port ||
                        endPoint2.Port == portInt)
                    {
                        throw new Exception("Port returns wrong data after change");
                    }
                }
            }
            catch (Exception e)
            {
                Log.Comment("Caught exception: " + e.Message);
                testResult = false;
            }
            return(testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }