* IPAddress.cs: On 1.1, 4-byte addresses are not supported in the
authorGert Driesen <drieseng@users.sourceforge.net>
Thu, 28 Dec 2006 21:18:12 +0000 (21:18 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Thu, 28 Dec 2006 21:18:12 +0000 (21:18 -0000)
ctor. Added null checks for the address passed in the ctors.
* IPAddressTest.cs: Added 4-byte and null address tests.

svn path=/trunk/mcs/; revision=70191

mcs/class/System/System.Net/ChangeLog
mcs/class/System/System.Net/IPAddress.cs
mcs/class/System/Test/System.Net/ChangeLog
mcs/class/System/Test/System.Net/IPAddressTest.cs

index 9e8894f1530efacbbb29db49d47f0564052c22df..76358c9944f74cbf8e4aff48c769020bbfa28b25 100644 (file)
@@ -1,3 +1,8 @@
+2006-12-28  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * IPAddress.cs: On 1.1, 4-byte addresses are not supported in the 
+       ctor. Added null checks for the address passed in the ctors.
+
 2006-12-28  Miguel de Icaza  <miguel@novell.com>
 
        * IPAddress.cs: The condition in 1.1 was broken, it only allowed
index 1f639e279b88f68da0a1ec1c45a0c75d904964c9..a036153e27b889357fc83d363efe367d908b95e9 100644 (file)
@@ -136,10 +136,18 @@ namespace System.Net {
 
                public IPAddress (byte[] address)
                {
+                       if (address == null)
+                               throw new ArgumentNullException ("address");
+
                        int len = address.Length;
 
+#if NET_2_0
                        if (len != 16 && len != 4)
                                throw new ArgumentException ("address");
+#else
+                       if (len != 16)
+                               throw new ArgumentException ("address");
+#endif
 
                        if (len == 16) {
                                Buffer.BlockCopy(address, 0, m_Numbers, 0, 16);
@@ -153,6 +161,9 @@ namespace System.Net {
 
                public IPAddress(byte[] address, long scopeId)
                {
+                       if (address == null)
+                               throw new ArgumentNullException ("address");
+
                        if (address.Length != 16)
                                throw new ArgumentException("address");
 
@@ -201,7 +212,7 @@ namespace System.Net {
                                
                        int pos = ip.IndexOf (' ');
                        if (pos != -1)
-                               ip = ip.Substring (0, pos);                             
+                               ip = ip.Substring (0, pos);
 
                        if (ip.Length == 0 || ip [ip.Length - 1] == '.')
                                return null;
index 97261cc77b6d15dd73d452e88ee5aa82e5532353..d26855c764f400efcfccc1f662b99d00977a2d1b 100644 (file)
@@ -1,3 +1,7 @@
+2006-12-28  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * IPAddressTest.cs: Added 4-byte and null address tests.
+
 2006-11-20 Andrew Skiba <andrews@mainsoft.com>
 
        * WebHeaderCollectionTest.cs: fix Indexers test to compile on 2.0
index 08404cdf1b7ff0ebb85c8b2cdbdfa6c7965b3737..d582666bbb9d873b0cba2de25c2a96e9c011f034 100644 (file)
@@ -324,36 +324,57 @@ public class IPAddressTest
                Assertion.AssertEquals ("#03", true, new Uri("http://[0:0:0:0::0.0.0.1]/").IsLoopback);
        }
 
-#if NET_2_0
-       [Test]
-       public void FromBytes3 ()
+       [Test] // bug #76792
+       public void Constructor0_Address_4Byte ()
        {
-               // This one works in 2.0
-               new IPAddress (new byte [4]);
+               byte[] bytes = new byte[4] { 192, 202, 112, 37 };
+#if NET_2_0
+               IPAddress i = new IPAddress (bytes);
+               Assert.AreEqual (bytes, i.GetAddressBytes (), "#1");
+               Assert.AreEqual ("192.202.112.37", i.ToString (), "#2");
+#else
+               try {
+                       new IPAddress (bytes);
+                       Assert.Fail ("#1");
+               } catch (ArgumentException ex) {
+                       Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+                       Assert.IsNotNull (ex.Message, "#3");
+                       Assert.AreEqual ("address", ex.Message, "#4");
+                       Assert.IsNull (ex.ParamName, "#5");
+                       Assert.IsNull (ex.InnerException, "#6");
+               }
+#endif
        }
-       
+
        [Test]
-       [ExpectedException (typeof (ArgumentException))]
-       public void FromBytes4 ()
+       [ExpectedException (typeof (ArgumentNullException))]
+       public void Constructor0_Address_Null ()
        {
-               new IPAddress (new byte [4], 0);
+               new IPAddress ((byte []) null);
        }
-#else
+
        [Test]
-       [ExpectedException (typeof (ArgumentException))]
-       public void FromBytes1 ()
+       public void Constructor1_Address_4Byte ()
        {
-               new IPAddress (new byte [4]);
+               byte [] bytes = new byte [4] { 192, 202, 112, 37 };
+               try {
+                       new IPAddress (bytes, 0);
+                       Assert.Fail ("#1");
+               } catch (ArgumentException ex) {
+                       Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+                       Assert.IsNotNull (ex.Message, "#3");
+                       Assert.AreEqual ("address", ex.Message, "#4");
+                       Assert.IsNull (ex.ParamName, "#5");
+                       Assert.IsNull (ex.InnerException, "#6");
+               }
        }
-       
+
        [Test]
-       [ExpectedException (typeof (ArgumentException))]
-       public void FromBytes2 ()
+       [ExpectedException (typeof (ArgumentNullException))]
+       public void Constructor1_Address_Null ()
        {
-               new IPAddress (new byte [4], 0);
+               new IPAddress ((byte []) null, 5);
        }
-#endif
-
 
 #if NET_2_0
        [Test]