Added patch for #82403 fixing several issues with PhysicalAddress
authorAlan McGovern <alan.mcgovern@gmail.com>
Mon, 13 Aug 2007 22:45:19 +0000 (22:45 -0000)
committerAlan McGovern <alan.mcgovern@gmail.com>
Mon, 13 Aug 2007 22:45:19 +0000 (22:45 -0000)
svn path=/trunk/mcs/; revision=84018

mcs/class/System/System.Net.NetworkInformation/ChangeLog
mcs/class/System/System.Net.NetworkInformation/PhysicalAddress.cs
mcs/class/System/System_test.dll.sources
mcs/class/System/Test/System.Net.NetworkInformation/ChangeLog [new file with mode: 0644]
mcs/class/System/Test/System.Net.NetworkInformation/PhysicalAddressTest.cs [new file with mode: 0644]

index 453ba09e84f68bfd90068b1bbffe402430531d77..6f14725505530d83a59cb56825f7b2a6242b3dae 100644 (file)
@@ -1,3 +1,8 @@
+2007-08-13  Alan McGovern  <amcgovern@novell.com>
+
+       * PhysicalAddress.cs: Added patch for #82403 to fix some parsing
+       errors and a bug in .Equals()
+
 2006-03-11  Miguel de Icaza  <miguel@novell.com>
 
        * NetworkChange.cs: Use pragmas to eliminate warnings from events
index d20b801a41da5cae862be36cc7db8fb0c3389d73..b1f52409ad3630e4c32bae4a8e00ea582d66179c 100644 (file)
@@ -32,6 +32,7 @@ using System.Text;
 namespace System.Net.NetworkInformation {
        public class PhysicalAddress {
                public static readonly PhysicalAddress None = new PhysicalAddress (new byte [0]);
+               private const int numberOfBytes = 6;
                byte [] bytes;
                
                public PhysicalAddress (byte [] address)
@@ -44,28 +45,36 @@ namespace System.Net.NetworkInformation {
                        if (address == null)
                                return None;
 
-                       if (address == "")
-                               throw new FormatException ("Invalid physical address.");
+                       if (address == string.Empty)
+                               throw new FormatException("An invalid physical address was specified.");
 
-                       // MS fails with IndexOutOfRange for something like: "00-0"
-                       int ndashes = 0;
-                       foreach (char c in address) {
-                               if (c == '-')
-                                       ndashes++;
+                       string[] addrSplit = address.Split('-');
+
+                       if (addrSplit.Length == 1) {
+                               if (address.Length != numberOfBytes * 2)
+                                       throw new FormatException("An invalid physical address was specified.");
+
+                               addrSplit = new string[numberOfBytes];
+                               for (int index = 0; index < addrSplit.Length; index++) {
+                                       addrSplit[index] = address.Substring(index * 2, 2);
+                               }
                        }
 
-                       int len = address.Length;
-                       if (((len - 2) / 3) != ndashes)
-                               throw new FormatException ("Invalid physical address.");
-
-                       byte [] data = new byte [ndashes + 1];
-                       int idx = 0;
-                       for (int i = 0; i < len; i++) {
-                               byte b = (byte) (GetValue (address [i++]) << 8);
-                               b += GetValue (address [i++]);
-                               if (address [i] != '-')
-                                       throw new FormatException ("Invalid physical address.");
-                               data [idx++] = b;
+                       if (addrSplit.Length == numberOfBytes) {
+                               foreach (string str in addrSplit)
+                                       if (str.Length > 2)
+                                               throw new FormatException("An invalid physical address was specified.");
+                                       else if (str.Length < 2)
+                                               throw new IndexOutOfRangeException("An invalid physical address was specified.");
+                       }
+                       else
+                               throw new FormatException("An invalid physical address was specified.");
+
+                       byte[] data = new byte[numberOfBytes];
+                       for (int i = 0; i < numberOfBytes; i++) {
+                               byte b = (byte)(GetValue(addrSplit[i][0]) << 4);
+                               b += GetValue(addrSplit[i][1]);
+                               data[i] = b;
                        }
 
                        return new PhysicalAddress (data);
@@ -73,7 +82,7 @@ namespace System.Net.NetworkInformation {
 
                static byte GetValue (char c)
                {
-                       if (c >= 0 && c <= 9)
+                       if (c >= '0' && c <= '9')
                                return (byte) (c - '0');
 
                        if (c >= 'a' && c <= 'f')
@@ -91,20 +100,19 @@ namespace System.Net.NetworkInformation {
                        if (other == null)
                                return false;
 
-                       // new byte [0] != new byte [0]
-                       return (bytes == other.bytes);
+                       if (bytes.Length != other.bytes.Length)
+                               return false;
+
+                       for (int index = 0; index < bytes.Length; index++)
+                               if (bytes[index] != other.bytes[index])
+                                       return false;
+
+                       return true;
                }
 
                public override int GetHashCode ()
                {
-                       if (bytes == null)
-                               return 0;
-
-                       int a = 5;
-                       foreach (byte b in bytes)
-                               a  = (a << 3)  + b;
-
-                       return a;
+                       return (bytes[5] << 8) ^ (bytes[4]) ^ (bytes[3] << 24) ^ (bytes[2] << 16) ^ (bytes[1] << 8) ^ (bytes[0]);
                }
 
                public byte [] GetAddressBytes ()
@@ -119,10 +127,9 @@ namespace System.Net.NetworkInformation {
 
                        StringBuilder sb = new StringBuilder ();
                        foreach (byte b in bytes)
-                               sb.AppendFormat ("{0:2X}", (uint) b);
+                               sb.AppendFormat("{0:X2}", b);
                        return sb.ToString ();
                }
        }
 }
 #endif
-
index aa18f51adbf34eaca100650833d758ecfb7871c4..a8cc9fe1c56e130afb704de0ec38968cbaa14141 100644 (file)
@@ -216,6 +216,7 @@ System.Net.Mail/SmtpPermissionTest.cs
 System.Net.Mail/SmtpPermissionAttributeTest.cs
 System.Net.Mime/ContentDispositionTest.cs
 System.Net.Mime/ContentTypeTest.cs
+System.Net.NetworkInformation/PhysicalAddressTest.cs
 System.Security.Cryptography/AsnEncodedDataTest.cs
 System.Security.Cryptography/OidCollectionTest.cs
 System.Security.Cryptography/OidEnumeratorTest.cs
diff --git a/mcs/class/System/Test/System.Net.NetworkInformation/ChangeLog b/mcs/class/System/Test/System.Net.NetworkInformation/ChangeLog
new file mode 100644 (file)
index 0000000..c5ef703
--- /dev/null
@@ -0,0 +1,3 @@
+2007-08-13  Alan McGovern  <amcgovern@novell.com>
+
+       * PhysicalAddress.cs : Added tests for #82403
diff --git a/mcs/class/System/Test/System.Net.NetworkInformation/PhysicalAddressTest.cs b/mcs/class/System/Test/System.Net.NetworkInformation/PhysicalAddressTest.cs
new file mode 100644 (file)
index 0000000..4dfd358
--- /dev/null
@@ -0,0 +1,133 @@
+using NUnit.Framework;
+using System;
+using System.Net.NetworkInformation;
+
+namespace MonoTests.System.Net.NetworkInformation
+{
+       [TestFixture]
+       public class PhysicalAddressTest
+       {
+               [Test] 
+               public void CreateNormal()
+               {
+                       // Normal case, creation of physical address
+                       PhysicalAddress phys = new PhysicalAddress(new byte [] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+               }
+
+               [Test]
+               public void CreateWithLarger()
+               {
+                       // MS.NET 2.0 Allows Physical Address to be created if array larger than normal
+                       PhysicalAddress phys = new PhysicalAddress(new byte [] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+               }
+
+               [Test]
+               public void CreateWithSmaller()
+               {
+                       // MS.NET 2.0 Allows Physical Address to be created if array smaller than normal
+                       PhysicalAddress phys = new PhysicalAddress(new byte [] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+               }
+
+               [Test]
+               public void ParseNull()
+               {
+                       // MS.NET 2.0 returns empty PhysicalAddress when passed null
+                       PhysicalAddress phys = PhysicalAddress.Parse(null);
+                       Assert.AreEqual(0, phys.GetAddressBytes().Length);
+               }
+
+               [Test]
+               [ExpectedException(typeof(FormatException))]
+               public void ParseEmpty()
+               {
+                       // MS.NET 2.0 Fails parse when empty
+                       PhysicalAddress phys = PhysicalAddress.Parse("");
+               }
+
+               [Test]
+               public void ParseWithoutDash()
+               {
+                       // MS.NET 2.0 Parses without the dash separator
+                       PhysicalAddress phys = PhysicalAddress.Parse("010203040506");
+               }
+
+               [Test]
+               [ExpectedException(typeof(FormatException))]
+               public void ParseWithoutDashToFewChars()
+               {
+                       // MS.NET 2.0 Fails parse when too few characters
+                       PhysicalAddress phys = PhysicalAddress.Parse("01020304050");
+               }
+
+               [Test]
+               [ExpectedException(typeof(FormatException))]
+               public void ParseWithoutDashToManyChars()
+               {
+                       // MS.NET 2.0 Fails parse when too many characters
+                       PhysicalAddress phys = PhysicalAddress.Parse("0102030405060");
+               }
+
+               [Test]
+               public void ParseWithDash()
+               {
+                       PhysicalAddress phys = PhysicalAddress.Parse("01-02-03-04-05-06");
+               }
+
+               [Test]
+               [ExpectedException(typeof(IndexOutOfRangeException))]
+               public void ParseWithDashToFewChars()
+               {
+                       PhysicalAddress phys = PhysicalAddress.Parse("01-02-03-04-05-0");
+               }
+
+               [Test]
+               [ExpectedException(typeof(FormatException))]
+               public void ParseWithDashToManyChars()
+               {
+                       PhysicalAddress phys = PhysicalAddress.Parse("01-02-03-04-05-060");
+               }
+
+               [Test]
+               public void GetHashCodeEqual()
+               {
+                       PhysicalAddress phys1 = new PhysicalAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+                       PhysicalAddress phys2 = new PhysicalAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+
+                       Assert.AreEqual(phys1.GetHashCode(), phys2.GetHashCode());
+               }
+
+               [Test]
+               public void GetHashCodeNotEqual()
+               {
+                       PhysicalAddress phys1 = new PhysicalAddress(new byte[] { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 });
+                       PhysicalAddress phys2 = new PhysicalAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+
+                       Assert.IsFalse(phys1.GetHashCode().Equals(phys2.GetHashCode()));
+               }
+
+               [Test]
+               public void ToString()
+               {
+                       PhysicalAddress phys1 = new PhysicalAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+                       Assert.AreEqual("010203040506", phys1.ToString());
+               }
+
+               [Test]
+               public void EqualsNormal()
+               {
+                       PhysicalAddress phys1 = new PhysicalAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+                       PhysicalAddress phys2 = new PhysicalAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+
+                       Assert.IsTrue(phys1.Equals(phys2));
+               }
+
+               [Test]
+               public void EqualsNot()
+               {
+                       PhysicalAddress phys1 = new PhysicalAddress(new byte[] { 0x06, 0x5, 0x04, 0x03, 0x02, 0x01 });
+                       PhysicalAddress phys2 = new PhysicalAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });
+
+                       Assert.IsTrue(!phys1.Equals(phys2));
+               }
+       }
+}