public void TryParse_Valid( )
        {
            SUTest ipAddress;
            bool   result;

            result = SUTest.TryParse("0.0.0.0", out ipAddress);
            Assert.AreEqual(true, result);
            CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 0 }, ipAddress.Bytes);

            result = SUTest.TryParse("255.255.255.255", out ipAddress);
            Assert.AreEqual(true, result);
            CollectionAssert.AreEqual(new byte[] { 255, 255, 255, 255 }, ipAddress.Bytes);

            result = SUTest.TryParse("0.128.64.32", out ipAddress);
            Assert.AreEqual(true, result);
            CollectionAssert.AreEqual(new byte[] { 0, 128, 64, 32 }, ipAddress.Bytes);

            result = SUTest.TryParse("255.128.64.32", out ipAddress);
            Assert.AreEqual(true, result);
            CollectionAssert.AreEqual(new byte[] { 255, 128, 64, 32 }, ipAddress.Bytes);

            result = SUTest.TryParse("255.0.64.32", out ipAddress);
            Assert.AreEqual(true, result);
            CollectionAssert.AreEqual(new byte[] { 255, 0, 64, 32 }, ipAddress.Bytes);

            result = SUTest.TryParse("255.128.0.32", out ipAddress);
            Assert.AreEqual(true, result);
            CollectionAssert.AreEqual(new byte[] { 255, 128, 0, 32 }, ipAddress.Bytes);

            result = SUTest.TryParse("255.128.64.0", out ipAddress);
            Assert.AreEqual(true, result);
            CollectionAssert.AreEqual(new byte[] { 255, 128, 64, 0 }, ipAddress.Bytes);
        }
Ejemplo n.º 2
0
        public static NetIpAddress ComputeNetworkAddress(NetIpAddress address, NetIpAddress netmask)
        {
            var addr = address.GetUintRepresentation( );
            var netm = netmask.GetUintRepresentation( );
            var neta = addr & netm;

            return(new NetIpAddress(neta));
        }
        public void Equals( )
        {
            var ipAddressByte = new SUTest(new byte[] { 0, 0, 0, 0 });
            var ipAddressUint = new SUTest(uint.MinValue);

            Assert.AreEqual(true, ipAddressByte.Equals(ipAddressByte));
            Assert.AreEqual(true, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(true, ipAddressUint.Equals(ipAddressByte));

            ipAddressByte = new SUTest(new byte[] { 255, 255, 255, 255 });
            Assert.AreEqual(false, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(false, ipAddressUint.Equals(ipAddressByte));

            ipAddressUint = new SUTest(uint.MaxValue);
            Assert.AreEqual(true, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(true, ipAddressUint.Equals(ipAddressByte));

            ipAddressByte = new SUTest(new byte[] { 255, 128, 64, 32 });
            Assert.AreEqual(false, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(false, ipAddressUint.Equals(ipAddressByte));

            ipAddressUint = new SUTest(4286595104u);
            Assert.AreEqual(true, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(true, ipAddressUint.Equals(ipAddressByte));

            ipAddressByte = new SUTest(new byte[] { 0, 128, 64, 32 });
            Assert.AreEqual(false, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(false, ipAddressUint.Equals(ipAddressByte));

            ipAddressUint = new SUTest(8405024u);
            Assert.AreEqual(true, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(true, ipAddressUint.Equals(ipAddressByte));

            ipAddressByte = new SUTest(new byte[] { 255, 0, 64, 32 });
            Assert.AreEqual(false, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(false, ipAddressUint.Equals(ipAddressByte));

            ipAddressUint = new SUTest(4278206496u);
            Assert.AreEqual(true, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(true, ipAddressUint.Equals(ipAddressByte));

            ipAddressByte = new SUTest(new byte[] { 255, 128, 0, 32 });
            Assert.AreEqual(false, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(false, ipAddressUint.Equals(ipAddressByte));

            ipAddressUint = new SUTest(4286578720u);
            Assert.AreEqual(true, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(true, ipAddressUint.Equals(ipAddressByte));

            ipAddressByte = new SUTest(new byte[] { 255, 128, 64, 0 });
            Assert.AreEqual(false, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(false, ipAddressUint.Equals(ipAddressByte));

            ipAddressUint = new SUTest(4286595072u);
            Assert.AreEqual(true, ipAddressByte.Equals(ipAddressUint));
            Assert.AreEqual(true, ipAddressUint.Equals(ipAddressByte));
        }
        public void TryParse_AdditionalByte( )
        {
            SUTest ipAddress;
            bool   result;

            result = SUTest.TryParse("121.64.147.21.231", out ipAddress);

            Assert.AreEqual(false, result);
            Assert.AreEqual(null, ipAddress.Bytes);
        }
        public void TryParse_InvalidByte( )
        {
            SUTest ipAddress;
            bool   result;

            result = SUTest.TryParse("121.1255.64.147", out ipAddress);

            Assert.AreEqual(false, result);
            Assert.AreEqual(null, ipAddress.Bytes);
        }
        public void TryParse_EmptyString( )
        {
            SUTest ipAddress;
            bool   result;

            result = SUTest.TryParse(string.Empty, out ipAddress);

            Assert.AreEqual(false, result);
            Assert.AreEqual(null, ipAddress.Bytes);
        }
        public void GetUintRepresentation_FromUint( )
        {
            var ipAddress = new SUTest(uint.MinValue);

            Assert.AreEqual(uint.MinValue, ipAddress.GetUintRepresentation( ));

            ipAddress = new SUTest(uint.MaxValue);
            Assert.AreEqual(uint.MaxValue, ipAddress.GetUintRepresentation( ));

            ipAddress = new SUTest(4286595104u);
            Assert.AreEqual(4286595104u, ipAddress.GetUintRepresentation( ));
        }
        public void Constructor_UintEmpty( )
        {
            var ipAddress = new SUTest(uint.MinValue);

            CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 0 }, ipAddress.Bytes);

            ipAddress = new SUTest(uint.MaxValue);
            CollectionAssert.AreEqual(new byte[] { 255, 255, 255, 255 }, ipAddress.Bytes);

            ipAddress = new SUTest(4286595104u);
            CollectionAssert.AreEqual(new byte[] { 255, 128, 64, 32 }, ipAddress.Bytes);
        }
Ejemplo n.º 9
0
 public static NetIpAddress ComputeBroadcast(NetIpAddress address, NetIpAddress netmask)
 {
     var addr = address.Bytes;
     var netm = netmask.Bytes;
     var n = Math.Min(addr.Length, netm.Length);
     var broad = new byte[n];
     for (int i = 0; i < n; i++) {
         broad[i] = addr[i];
         broad[i] &= netm[i];
         byte second = byte.MaxValue;
         second ^= netm[i];
         broad[i] += second;
     }
     return new NetIpAddress(broad);
 }
Ejemplo n.º 10
0
        public static NetIpAddress ComputeBroadcast(NetIpAddress address, NetIpAddress netmask)
        {
            var addr  = address.Bytes;
            var netm  = netmask.Bytes;
            var n     = Math.Min(addr.Length, netm.Length);
            var broad = new byte[n];

            for (int i = 0; i < n; i++)
            {
                broad[i]  = addr[i];
                broad[i] &= netm[i];
                byte second = byte.MaxValue;
                second   ^= netm[i];
                broad[i] += second;
            }
            return(new NetIpAddress(broad));
        }
Ejemplo n.º 11
0
        public bool Equals(NetIpAddress other)
        {
            var thisBytes  = Bytes;
            var otherBytes = other.Bytes;

            if (thisBytes == null)
            {
                thisBytes = Zero.Bytes;
            }
            if (otherBytes == null)
            {
                otherBytes = Zero.Bytes;
            }
            return(thisBytes[0] == otherBytes[0] &&
                   thisBytes[1] == otherBytes[1] &&
                   thisBytes[2] == otherBytes[2] &&
                   thisBytes[3] == otherBytes[3]);
        }
Ejemplo n.º 12
0
 public static bool NetmaskIsValid(NetIpAddress netmask)
 {
     if (netmask != null)
     {
         var  bitNetmask = netmask.GetUintRepresentation( );
         bool hostPart   = false;
         for (int i = 0; i < 32; i++)
         {
             if (bitNetmask % 2 == 1)
             {
                 hostPart = true;
             }
             else if (hostPart)
             {
                 return(false);
             }
             bitNetmask >>= 1;
         }
     }
     return(true);
 }
Ejemplo n.º 13
0
        public static bool TryParse(string address, out NetIpAddress validAddress)
        {
            validAddress = default(NetIpAddress);
            if (address == null)
            {
                return(false);
            }
            if (address == string.Empty)
            {
                return(false);
            }
            var strBytes = address.Split('.');

            byte[] numBytes = new byte[4];
            if (strBytes.Length != 4)
            {
                return(false);
            }
            for (int i = 0; i < 4; i++)
            {
                byte num;
                if (!byte.TryParse(strBytes[i], out num))
                {
                    return(false);
                }
                if (num < 0)
                {
                    return(false);
                }
                if (num > 255)
                {
                    return(false);
                }
                numBytes[i] = num;
            }
            validAddress = new NetIpAddress(numBytes);
            return(true);
        }
        public void ToString_ValidInput( )
        {
            var ipAddress = new SUTest(new byte[] { 0, 0, 0, 0 });

            Assert.AreEqual("0.0.0.0", ipAddress.ToString( ));

            ipAddress = new SUTest(new byte[] { 255, 255, 255, 255 });
            Assert.AreEqual("255.255.255.255", ipAddress.ToString( ));

            ipAddress = new SUTest(new byte[] { 255, 128, 64, 32 });
            Assert.AreEqual("255.128.64.32", ipAddress.ToString( ));

            ipAddress = new SUTest(new byte[] { 0, 128, 64, 32 });
            Assert.AreEqual("0.128.64.32", ipAddress.ToString( ));

            ipAddress = new SUTest(new byte[] { 255, 0, 64, 32 });
            Assert.AreEqual("255.0.64.32", ipAddress.ToString( ));

            ipAddress = new SUTest(new byte[] { 255, 128, 0, 32 });
            Assert.AreEqual("255.128.0.32", ipAddress.ToString( ));

            ipAddress = new SUTest(new byte[] { 255, 128, 64, 0 });
            Assert.AreEqual("255.128.64.0", ipAddress.ToString( ));
        }
Ejemplo n.º 15
0
 public static bool TryParse(string address, out NetIpAddress validAddress)
 {
     validAddress = default(NetIpAddress);
     if (address == null)
         return false;
     if (address == string.Empty)
         return false;
     var strBytes = address.Split('.');
     byte[] numBytes = new byte[4];
     if (strBytes.Length != 4)
         return false;
     for (int i = 0; i < 4; i++) {
         byte num;
         if (!byte.TryParse(strBytes[i], out num))
             return false;
         if (num < 0)
             return false;
         if (num > 255)
             return false;
         numBytes[i] = num;
     }
     validAddress = new NetIpAddress(numBytes);
     return true;
 }
        public void GetUintRepresentation_FromByteArray( )
        {
            var ipAddress = new SUTest(new byte[] { 0, 0, 0, 0 });

            Assert.AreEqual(uint.MinValue, ipAddress.GetUintRepresentation( ));

            ipAddress = new SUTest(new byte[] { 255, 255, 255, 255 });
            Assert.AreEqual(uint.MaxValue, ipAddress.GetUintRepresentation( ));

            ipAddress = new SUTest(new byte[] { 255, 128, 64, 32 });
            Assert.AreEqual(4286595104u, ipAddress.GetUintRepresentation( ));

            ipAddress = new SUTest(new byte[] { 0, 128, 64, 32 });
            Assert.AreEqual(8405024u, ipAddress.GetUintRepresentation( ));

            ipAddress = new SUTest(new byte[] { 255, 0, 64, 32 });
            Assert.AreEqual(4278206496u, ipAddress.GetUintRepresentation( ));

            ipAddress = new SUTest(new byte[] { 255, 128, 0, 32 });
            Assert.AreEqual(4286578720u, ipAddress.GetUintRepresentation( ));

            ipAddress = new SUTest(new byte[] { 255, 128, 64, 0 });
            Assert.AreEqual(4286595072u, ipAddress.GetUintRepresentation( ));
        }
Ejemplo n.º 17
0
 public NetAddress(NetIpAddress address, NetIpAddress netmask)
 {
     this.address   = address;
     this.netmask   = netmask;
     this.broadcast = ComputeBroadcast(this.address, this.netmask);
 }
Ejemplo n.º 18
0
 public NetAddress(NetIpAddress address)
 {
     this.address   = address;
     this.netmask   = NetIpAddress.MaxAddress;
     this.broadcast = ComputeBroadcast(this.address, this.netmask);
 }
        public void Constructor_ByteArray( )
        {
            var ipAddress = new SUTest(new byte[] { 247, 30, 63, 161 });

            CollectionAssert.AreEqual(new byte[] { 247, 30, 63, 161 }, ipAddress.Bytes);
        }
Ejemplo n.º 20
0
 public static NetIpAddress ComputeNetworkAddress(NetIpAddress address, NetIpAddress netmask)
 {
     var addr = address.GetUintRepresentation( );
     var netm = netmask.GetUintRepresentation( );
     var neta = addr & netm;
     return new NetIpAddress(neta);
 }
Ejemplo n.º 21
0
 public NetAddress(NetIpAddress address)
 {
     this.address = address;
     this.netmask = NetIpAddress.MaxAddress;
     this.broadcast = ComputeBroadcast(this.address, this.netmask);
 }
Ejemplo n.º 22
0
 public NetAddress(NetIpAddress address, NetIpAddress netmask)
 {
     this.address = address;
     this.netmask = netmask;
     this.broadcast = ComputeBroadcast(this.address, this.netmask);
 }
Ejemplo n.º 23
0
 public NetRoute(NetIpAddress address, NetIpAddress netmask, NetIpAddress target)
 {
     this.address = new NetAddress(address, netmask);
     this.target  = new NetAddress(target);
 }
Ejemplo n.º 24
0
 public static bool NetmaskIsValid(NetIpAddress netmask)
 {
     if (netmask != null) {
         var bitNetmask = netmask.GetUintRepresentation( );
         bool hostPart = false;
         for (int i = 0; i < 32; i++) {
             if (bitNetmask % 2 == 1) {
                 hostPart = true;
             } else if (hostPart) {
                 return false;
             }
             bitNetmask >>= 1;
         }
     }
     return true;
 }
Ejemplo n.º 25
0
 public NetRoute(NetIpAddress target)
 {
     this.address = new NetAddress(NetIpAddress.Zero, NetIpAddress.Zero);
     this.target  = new NetAddress(target);
 }
Ejemplo n.º 26
0
 public NetRoute(NetIpAddress target)
 {
     this.address = new NetAddress(NetIpAddress.Zero, NetIpAddress.Zero);
     this.target = new NetAddress(target);
 }
Ejemplo n.º 27
0
 public NetRoute(NetIpAddress address, NetIpAddress netmask, NetIpAddress target)
 {
     this.address = new NetAddress(address, netmask);
     this.target = new NetAddress(target);
 }
Ejemplo n.º 28
0
 public NetAddress(NetIpAddress address, NetIpAddress netmask, NetIpAddress broadcast)
 {
     this.address   = address;
     this.netmask   = netmask;
     this.broadcast = broadcast;
 }
Ejemplo n.º 29
0
 public NetAddress(NetIpAddress address, NetIpAddress netmask, NetIpAddress broadcast)
 {
     this.address = address;
     this.netmask = netmask;
     this.broadcast = broadcast;
 }
        public void Constructor_Empty( )
        {
            var ipAddress = new SUTest( );

            CollectionAssert.AreEqual(null, ipAddress.Bytes);
        }
Ejemplo n.º 31
0
 public bool Equals(NetIpAddress other)
 {
     var thisBytes = Bytes;
     var otherBytes = other.Bytes;
     if (thisBytes == null) thisBytes = Zero.Bytes;
     if (otherBytes == null) otherBytes = Zero.Bytes;
     return thisBytes[0] == otherBytes[0]
         && thisBytes[1] == otherBytes[1]
         && thisBytes[2] == otherBytes[2]
         && thisBytes[3] == otherBytes[3];
 }