예제 #1
0
        public MFTestResults NetTest2_IPAddressBasic()
        {
            /// <summary>
            /// 1. Creates 30 Random IPs between 0.0.0.0 and 255.255.255.127
            /// 2. Verifies that they can be constructed
            /// 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));

                    if (address == null)
                        throw new Exception("Address is null");

                    Type typeOfAddress = address.GetType();
                    if (typeOfAddress != Type.GetType("System.Net.IPAddress"))
                        throw new Exception("Type is incorrect");

                    byte[] targetBytes = { (byte)IPInts[0], (byte)IPInts[1], 
                        (byte)IPInts[2], (byte)IPInts[3] };
                    byte[] addressBytes = address.GetAddressBytes();
                    if (addressBytes.Length != 4)
                        throw new Exception("GetAddressBytes returns wrong size");

                    for (int j = 0; j < 4; j++)
                        if (addressBytes[j] != targetBytes[j])
                            throw new Exception("GetAddressBytes returns wrong bytes");
                    IPAddress address2 = new IPAddress((long)(
                            IPInts[0]
                            + IPInts[1] * 256
                            + IPInts[2] * 256 * 256
                            + IPInts[3] * 256 * 256 * 256));

                    if (address.ToString() != address2.ToString())
                        throw new Exception("ToString returns differently for same data");

                    if (address.GetHashCode() != address2.GetHashCode())
                        throw new Exception("GetHasCode returns differently for same data");

                    address2 = new IPAddress((long)(
                        (IPInts[0] % 2 + 1)
                        + (IPInts[1] % 2 + 1 )* 256
                        + (IPInts[2] % 2 + 1 )* 256 * 256
                        + (IPInts[3] % 2 + 1 )* 256 * 256 * 256));
                    if (address.GetHashCode() == address2.GetHashCode())
                        throw new Exception("GetHasCode returns same for " + address.ToString() 
                            + " as " + address2.ToString());
                }
            }
            catch (Exception e)
            {
                Log.Comment("Caught exception: " + e.Message);
                testResult = false;
            }
            return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }