public void NetTest6_SocketAddressBasic() { Random random = new(); for (int i = 0; i <= 30; i++) { int[] IPInts = { random.Next(256), random.Next(256), random.Next(256), random.Next(128) }; Debug.WriteLine($"Random IP {IPInts[0]}.{IPInts[1]}.{IPInts[2]}.{IPInts[3]}"); IPAddress address = new( IPInts[0] + IPInts[1] * 256 + IPInts[2] * 256 * 256 + IPInts[3] * 256 * 256 * 256); int portInt = random.Next(65536); IPEndPoint ipEndpoint1 = new(address, portInt); SocketAddress socketAddress1 = ipEndpoint1.Serialize(); SocketAddress socketAddress2 = ipEndpoint1.Serialize(); Assert.NotNull(socketAddress1, "socketAddress1 is null"); Assert.NotNull(socketAddress2, "socketAddress2 is null"); Type typeOfSocketAddress = socketAddress1.GetType(); Assert.IsType(typeOfSocketAddress, Type.GetType("System.Net.SocketAddress"), "socketAddress1 Type is incorrect"); typeOfSocketAddress = socketAddress2.GetType(); Assert.IsType(typeOfSocketAddress, Type.GetType("System.Net.SocketAddress"), "socketAddress2 Type is incorrect"); Assert.Equal(socketAddress1.ToString(), socketAddress2.ToString(), "ToString returns differently for same data"); Assert.Equal(socketAddress1.GetHashCode(), socketAddress2.GetHashCode(), $"GetHashCode returns differently for same data"); Assert.True(socketAddress1.Family == AddressFamily.InterNetwork, "socketAddress1 Family is incorrect"); Assert.True(socketAddress2.Family == AddressFamily.InterNetwork, "socketAddress2 Family is incorrect"); // Recreate a different Socket socketAddress2 = new SocketAddress(AddressFamily.Chaos, 8); Assert.NotEqual(socketAddress1.GetHashCode(), socketAddress2.GetHashCode(), "GetHashCode returns same for " + socketAddress1.ToString() + " " + socketAddress1.GetHashCode() + " as " + socketAddress2.ToString() + " " + socketAddress2.GetHashCode()); } }
public MFTestResults NetTest6_SocketAddressBasic() { /// <summary> /// 1. Creates 30 Random IPs between 0.0.0.0 and 255.255.255.127 /// 2. Verifies that they can be constructed as SocketAddress /// 3. Verifies that they have the correct data (GetAddressBytes) /// 4. Verifies ToString and GetHashcode /// </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) }; Log.Comment("Random IP " + IPInts[0] + "." + IPInts[1] + "." + IPInts[2] + "." + IPInts[3]); IPAddress address = new IPAddress((long)( IPInts[0] + IPInts[1] * 256 + IPInts[2] * 256 * 256 + IPInts[3] * 256 * 256 * 256)); int portInt = random.Next(65536); IPEndPoint ipEndpoint1 = new IPEndPoint(address, portInt); SocketAddress socketAddress1 = ipEndpoint1.Serialize(); SocketAddress socketAddress2 = ipEndpoint1.Serialize(); if (socketAddress1 == null) { throw new Exception("socketAddress1 is null"); } if (socketAddress2 == null) { throw new Exception("socketAddress2 is null"); } Type typeOfSocketAddress = socketAddress1.GetType(); if (typeOfSocketAddress != Type.GetType("System.Net.SocketAddress")) { throw new Exception("socketAddress1 Type is incorrect"); } typeOfSocketAddress = socketAddress2.GetType(); if (typeOfSocketAddress != Type.GetType("System.Net.SocketAddress")) { throw new Exception("socketAddress2 Type is incorrect"); } if (socketAddress1.ToString() != socketAddress2.ToString()) { throw new Exception("ToString returns differently for same data"); } //21295 GetHashCode returns differently for cloned data if (socketAddress1.GetHashCode() != socketAddress2.GetHashCode()) { throw new Exception("GetHashCode returns differently for same data"); } if (socketAddress1.Family != AddressFamily.InterNetwork) { throw new Exception("socketAddress1 Family is incorrect"); } if (socketAddress2.Family != AddressFamily.InterNetwork) { throw new Exception("socketAddress2 Family is incorrect"); } /* * Pending Resolution of 17428 * * Log.Comment("Recreating socketAddress2 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); + + IPEndPoint ipEndpoint2 = new IPEndPoint(addressLong2, portInt2); + socketAddress2 = ipEndpoint2.Serialize(); + socketAddress2.Family = AddressFamily.Chaos; */ socketAddress2 = new SocketAddress(AddressFamily.Chaos, 8); if (socketAddress1.GetHashCode() == socketAddress2.GetHashCode()) { throw new Exception("GetHashCode returns same for " + socketAddress1.ToString() + " as " + socketAddress2.ToString()); } if (socketAddress1.ToString() == socketAddress2.ToString()) { throw new Exception("ToString returns same for different data"); } } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); testResult = false; } return(testResult ? MFTestResults.Pass : MFTestResults.KnownFailure); }