Browse Source

Cookie test units

svn path=/trunk/mcs/; revision=4074
Lawrence Pit 24 years ago
parent
commit
d75e0b9d00

+ 3 - 0
mcs/class/System/Test/System.Net/AllTests.cs

@@ -22,6 +22,9 @@ namespace MonoTests.System.Net {
                                 TestSuite suite = new TestSuite();
                                 suite.AddTest (IPAddressTest.Suite);
                                 suite.AddTest (IPEndPointTest.Suite);
+                                suite.AddTest (CookieTest.Suite);
+                                suite.AddTest (CookieCollectionTest.Suite);
+                                // suite.AddTest (CookieContainerTest.Suite);
 				return suite;
                         }
                 }

+ 5 - 0
mcs/class/System/Test/System.Net/ChangeLog

@@ -1,3 +1,8 @@
+2002-04-27  Lawrence Pit <[email protected]>
+	* CookieTest.cs
+	* CookieCollectionTest.cs
+	* AllTests.cs
+
 2002-04-24  Nick Drochak  <[email protected]>
 
 	* IPAddressTest.cs: Make test conform to MS behavior. Also, if wrong 

+ 123 - 0
mcs/class/System/Test/System.Net/CookieCollectionTest.cs

@@ -0,0 +1,123 @@
+//
+// CookieCollectionTest.cs - NUnit Test Cases for System.Net.CookieCollection
+//
+// Author:
+//   Lawrence Pit ([email protected])
+//
+
+using NUnit.Framework;
+using System;
+using System.Net;
+using System.Collections;
+
+namespace MonoTests.System.Net
+{
+
+public class CookieCollectionTest : TestCase
+{
+	CookieCollection col;
+	
+        public CookieCollectionTest () :
+                base ("[MonoTests.System.Net.CookieCollectionTest]") {}
+
+        public CookieCollectionTest (string name) : base (name) {}
+
+        protected override void SetUp () 
+        {
+		col = new CookieCollection ();	
+		col.Add (new Cookie ("name1", "value1"));
+		col.Add (new Cookie ("name2", "value2", "path2"));
+		col.Add (new Cookie ("name3", "value3", "path3", "domain3"));		
+	}
+
+        protected override void TearDown () {}
+
+        public static ITest Suite
+        {
+                get {
+                        return new TestSuite (typeof (CookieCollectionTest));
+                }
+        }
+        
+        public void TestCount ()
+        {
+		AssertEquals ("#1", col.Count, 3);
+	}
+
+        public void TestIndexer ()
+        {
+		Cookie c = null;
+		try {
+			c = col [-1];
+			Fail ("#1");
+		} catch (ArgumentOutOfRangeException) {
+		}
+		try {
+			c = col [col.Count];
+			Fail ("#2");
+		} catch (ArgumentOutOfRangeException) {
+		}
+		c = col ["name1"];
+		AssertEquals ("#3", c.Name, "name1");
+		c = col ["NAME2"];
+		AssertEquals ("#4", c.Name, "name2");
+	}
+	
+	public void TestAdd ()
+	{
+		try {
+			Cookie c = null;
+			col.Add (c);
+			Fail ("#1");
+		} catch (ArgumentNullException) {
+		}
+		
+		// in the microsoft implementation this will fail,
+		// so we'll have to fail to.
+		try {
+			col.Add (col);
+			Fail ("#2");
+		} catch (Exception) {
+		}
+		AssertEquals ("#3", col.Count, 3);
+		
+		col.Add (new Cookie("name1", "value1"));		
+		AssertEquals ("#4", col.Count, 3);
+		
+		CookieCollection col2 = new CookieCollection();
+		Cookie c4 = new Cookie("name4", "value4");
+		Cookie c5 = new Cookie("name5", "value5");
+		col2.Add (c4);
+		col2.Add (c5);
+		col.Add (col2);
+		AssertEquals ("#5", col.Count, 5);
+		AssertEquals ("#6", col ["NAME4"], c4);
+		AssertEquals ("#7", col [4], c5);
+	}
+	
+	public void TestCopyTo ()
+	{
+		Array a = Array.CreateInstance (typeof (Cookie), 3);
+		col.CopyTo (a, 0);
+		AssertEquals ("#1", a.GetValue (0), col [0]);
+		AssertEquals ("#2", a.GetValue (1), col [1]);
+		AssertEquals ("#3", a.GetValue (2), col [2]);
+	}
+	
+	public void TestEnumerator ()
+	{
+		IEnumerator enumerator = col.GetEnumerator ();
+		enumerator.MoveNext ();
+		Cookie c = (Cookie) enumerator.Current;
+		AssertEquals ("#1", c, col [0]);
+		col.Add (new Cookie ("name6", "value6"));
+		try {
+			enumerator.MoveNext ();
+			Fail ("#2");
+		} catch (InvalidOperationException) {
+		}
+	}
+}
+
+}
+

+ 163 - 0
mcs/class/System/Test/System.Net/CookieTest.cs

@@ -0,0 +1,163 @@
+//
+// CookieTest.cs - NUnit Test Cases for System.Net.Cookie
+//
+// Author:
+//   Lawrence Pit ([email protected])
+//
+
+using NUnit.Framework;
+using System;
+using System.Net;
+
+namespace MonoTests.System.Net
+{
+
+public class CookieTest : TestCase
+{
+        public CookieTest () :
+                base ("[MonoTests.System.Net.CookieTest]") {}
+
+        public CookieTest (string name) : base (name) {}
+
+        protected override void SetUp () {}
+
+        protected override void TearDown () {}
+
+        public static ITest Suite
+        {
+                get {
+                        return new TestSuite (typeof (CookieTest));
+                }
+        }
+
+        public void TestPublicFields ()
+        {
+        }
+
+        public void TestConstructors ()
+        {
+		Cookie c = new Cookie ("somename", null, null, null);
+		try {
+			c = new Cookie (null, null, null, null);
+			Fail ("#1: Name cannot be null");
+		} catch (CookieException) {
+		}
+        }
+        
+        public void TestName ()         
+        {
+		Cookie c = new Cookie ("SomeName", "SomeValue");
+		AssertEquals ("#1", c.Name, "SomeName");
+		try {
+			c.Name = null;
+			Fail ("#2a");
+		} catch (CookieException) {
+			AssertEquals ("#2b", "SomeName", c.Name);
+		}
+		try {
+			c.Name = "";
+			Fail ("#2c");
+		} catch (CookieException) {
+			AssertEquals ("#2d", "SomeName", c.Name);			
+		}
+		try {
+			c.Name = " ";
+			Fail ("#2e");			
+		} catch (CookieException) {
+			// bah! this fails, yet the name is changed.. 
+			// inconsistent with previous test
+			AssertEquals ("#2f", String.Empty, c.Name);			
+		}
+		try {
+			c.Name = "xxx\r\n";
+			Fail ("#2g");			
+		} catch (CookieException ttt) {
+			AssertEquals ("#2h", String.Empty, c.Name);			
+		}		
+		try {
+			c.Name = "xxx" + (char) 0x80;
+		} catch (CookieException) {
+			Fail ("#2i");			
+		}				
+		try {
+			c.Name = "$omeName";
+			Fail ("#3a: Name cannot start with '$' character");
+		} catch (CookieException) {
+			AssertEquals ("#3b", String.Empty, c.Name);
+		}
+		c.Name = "SomeName$";
+		AssertEquals ("#4", c.Name, "SomeName$");
+		try {
+			c.Name = "Some=Name";
+			Fail ("#5a: Name cannot contain '=' character");
+		} catch (CookieException) {
+			AssertEquals ("#5b", String.Empty, c.Name);
+		}		
+		c.Name = "domain";
+		AssertEquals ("#6", c.Name, "domain");
+	}
+	
+	public void TestValue ()
+	{
+		// LAMESPEC: According to .Net specs the Value property should not accept 
+		// the semicolon and comma characters, yet it does
+		Cookie c = new Cookie("SomeName", "SomeValue");
+		try {
+			c.Value = "Some;Value";
+			Fail ("#1: semicolon should not be accepted");
+		} catch (CookieException) {
+		}
+		try {
+			c.Value = "Some,Value";
+			Fail ("#2: comma should not be accepted");
+		} catch (CookieException) {
+		}
+		c.Value = "Some\tValue";
+		AssertEquals ("#3", c.Value, "Some\tValue");
+	}
+	
+	public void TestPort ()
+	{
+		Cookie c = new Cookie ("SomeName", "SomeValue");
+		try {
+			c.Port = "123";
+			Fail ("#1: port must start and end with double quotes");
+		} catch (CookieException) {			
+		}
+		try {
+			c.Port = "\"123\"";
+		} catch (CookieException) {			
+			Fail ("#2");
+		}
+		try {
+			c.Port = "\"123;124\"";
+			Fail ("#3");
+		} catch (CookieException) {					
+		}
+		try {
+			c.Port = "\"123,123,124\"";
+		} catch (CookieException) {			
+			Fail ("#4");
+		}
+		try {
+			c.Port = "\"123,124\"";
+		} catch (CookieException) {			
+			Fail ("#5");
+		}
+	}
+
+        public void TestEquals ()
+        {
+		Cookie c1 = new Cookie ("NAME", "VALUE", "PATH", "DOMAIN");
+		Cookie c2 = new Cookie ("name", "value", "path", "domain");
+		Assert("#1", !c1.Equals (c2));
+		c2.Value = "VALUE";
+		c2.Path = "PATH";
+		Assert("#2", c1.Equals (c2));
+		c2.Version = 1;
+		Assert("#3", !c1.Equals (c2));
+        }
+}
+
+}
+

+ 3 - 0
mcs/class/corlib/Test/System.Net/AllTests.cs

@@ -22,6 +22,9 @@ namespace MonoTests.System.Net {
                                 TestSuite suite = new TestSuite();
                                 suite.AddTest (IPAddressTest.Suite);
                                 suite.AddTest (IPEndPointTest.Suite);
+                                suite.AddTest (CookieTest.Suite);
+                                suite.AddTest (CookieCollectionTest.Suite);
+                                // suite.AddTest (CookieContainerTest.Suite);
 				return suite;
                         }
                 }

+ 5 - 0
mcs/class/corlib/Test/System.Net/ChangeLog

@@ -1,3 +1,8 @@
+2002-04-27  Lawrence Pit <[email protected]>
+	* CookieTest.cs
+	* CookieCollectionTest.cs
+	* AllTests.cs
+
 2002-04-24  Nick Drochak  <[email protected]>
 
 	* IPAddressTest.cs: Make test conform to MS behavior. Also, if wrong 

+ 123 - 0
mcs/class/corlib/Test/System.Net/CookieCollectionTest.cs

@@ -0,0 +1,123 @@
+//
+// CookieCollectionTest.cs - NUnit Test Cases for System.Net.CookieCollection
+//
+// Author:
+//   Lawrence Pit ([email protected])
+//
+
+using NUnit.Framework;
+using System;
+using System.Net;
+using System.Collections;
+
+namespace MonoTests.System.Net
+{
+
+public class CookieCollectionTest : TestCase
+{
+	CookieCollection col;
+	
+        public CookieCollectionTest () :
+                base ("[MonoTests.System.Net.CookieCollectionTest]") {}
+
+        public CookieCollectionTest (string name) : base (name) {}
+
+        protected override void SetUp () 
+        {
+		col = new CookieCollection ();	
+		col.Add (new Cookie ("name1", "value1"));
+		col.Add (new Cookie ("name2", "value2", "path2"));
+		col.Add (new Cookie ("name3", "value3", "path3", "domain3"));		
+	}
+
+        protected override void TearDown () {}
+
+        public static ITest Suite
+        {
+                get {
+                        return new TestSuite (typeof (CookieCollectionTest));
+                }
+        }
+        
+        public void TestCount ()
+        {
+		AssertEquals ("#1", col.Count, 3);
+	}
+
+        public void TestIndexer ()
+        {
+		Cookie c = null;
+		try {
+			c = col [-1];
+			Fail ("#1");
+		} catch (ArgumentOutOfRangeException) {
+		}
+		try {
+			c = col [col.Count];
+			Fail ("#2");
+		} catch (ArgumentOutOfRangeException) {
+		}
+		c = col ["name1"];
+		AssertEquals ("#3", c.Name, "name1");
+		c = col ["NAME2"];
+		AssertEquals ("#4", c.Name, "name2");
+	}
+	
+	public void TestAdd ()
+	{
+		try {
+			Cookie c = null;
+			col.Add (c);
+			Fail ("#1");
+		} catch (ArgumentNullException) {
+		}
+		
+		// in the microsoft implementation this will fail,
+		// so we'll have to fail to.
+		try {
+			col.Add (col);
+			Fail ("#2");
+		} catch (Exception) {
+		}
+		AssertEquals ("#3", col.Count, 3);
+		
+		col.Add (new Cookie("name1", "value1"));		
+		AssertEquals ("#4", col.Count, 3);
+		
+		CookieCollection col2 = new CookieCollection();
+		Cookie c4 = new Cookie("name4", "value4");
+		Cookie c5 = new Cookie("name5", "value5");
+		col2.Add (c4);
+		col2.Add (c5);
+		col.Add (col2);
+		AssertEquals ("#5", col.Count, 5);
+		AssertEquals ("#6", col ["NAME4"], c4);
+		AssertEquals ("#7", col [4], c5);
+	}
+	
+	public void TestCopyTo ()
+	{
+		Array a = Array.CreateInstance (typeof (Cookie), 3);
+		col.CopyTo (a, 0);
+		AssertEquals ("#1", a.GetValue (0), col [0]);
+		AssertEquals ("#2", a.GetValue (1), col [1]);
+		AssertEquals ("#3", a.GetValue (2), col [2]);
+	}
+	
+	public void TestEnumerator ()
+	{
+		IEnumerator enumerator = col.GetEnumerator ();
+		enumerator.MoveNext ();
+		Cookie c = (Cookie) enumerator.Current;
+		AssertEquals ("#1", c, col [0]);
+		col.Add (new Cookie ("name6", "value6"));
+		try {
+			enumerator.MoveNext ();
+			Fail ("#2");
+		} catch (InvalidOperationException) {
+		}
+	}
+}
+
+}
+

+ 163 - 0
mcs/class/corlib/Test/System.Net/CookieTest.cs

@@ -0,0 +1,163 @@
+//
+// CookieTest.cs - NUnit Test Cases for System.Net.Cookie
+//
+// Author:
+//   Lawrence Pit ([email protected])
+//
+
+using NUnit.Framework;
+using System;
+using System.Net;
+
+namespace MonoTests.System.Net
+{
+
+public class CookieTest : TestCase
+{
+        public CookieTest () :
+                base ("[MonoTests.System.Net.CookieTest]") {}
+
+        public CookieTest (string name) : base (name) {}
+
+        protected override void SetUp () {}
+
+        protected override void TearDown () {}
+
+        public static ITest Suite
+        {
+                get {
+                        return new TestSuite (typeof (CookieTest));
+                }
+        }
+
+        public void TestPublicFields ()
+        {
+        }
+
+        public void TestConstructors ()
+        {
+		Cookie c = new Cookie ("somename", null, null, null);
+		try {
+			c = new Cookie (null, null, null, null);
+			Fail ("#1: Name cannot be null");
+		} catch (CookieException) {
+		}
+        }
+        
+        public void TestName ()         
+        {
+		Cookie c = new Cookie ("SomeName", "SomeValue");
+		AssertEquals ("#1", c.Name, "SomeName");
+		try {
+			c.Name = null;
+			Fail ("#2a");
+		} catch (CookieException) {
+			AssertEquals ("#2b", "SomeName", c.Name);
+		}
+		try {
+			c.Name = "";
+			Fail ("#2c");
+		} catch (CookieException) {
+			AssertEquals ("#2d", "SomeName", c.Name);			
+		}
+		try {
+			c.Name = " ";
+			Fail ("#2e");			
+		} catch (CookieException) {
+			// bah! this fails, yet the name is changed.. 
+			// inconsistent with previous test
+			AssertEquals ("#2f", String.Empty, c.Name);			
+		}
+		try {
+			c.Name = "xxx\r\n";
+			Fail ("#2g");			
+		} catch (CookieException ttt) {
+			AssertEquals ("#2h", String.Empty, c.Name);			
+		}		
+		try {
+			c.Name = "xxx" + (char) 0x80;
+		} catch (CookieException) {
+			Fail ("#2i");			
+		}				
+		try {
+			c.Name = "$omeName";
+			Fail ("#3a: Name cannot start with '$' character");
+		} catch (CookieException) {
+			AssertEquals ("#3b", String.Empty, c.Name);
+		}
+		c.Name = "SomeName$";
+		AssertEquals ("#4", c.Name, "SomeName$");
+		try {
+			c.Name = "Some=Name";
+			Fail ("#5a: Name cannot contain '=' character");
+		} catch (CookieException) {
+			AssertEquals ("#5b", String.Empty, c.Name);
+		}		
+		c.Name = "domain";
+		AssertEquals ("#6", c.Name, "domain");
+	}
+	
+	public void TestValue ()
+	{
+		// LAMESPEC: According to .Net specs the Value property should not accept 
+		// the semicolon and comma characters, yet it does
+		Cookie c = new Cookie("SomeName", "SomeValue");
+		try {
+			c.Value = "Some;Value";
+			Fail ("#1: semicolon should not be accepted");
+		} catch (CookieException) {
+		}
+		try {
+			c.Value = "Some,Value";
+			Fail ("#2: comma should not be accepted");
+		} catch (CookieException) {
+		}
+		c.Value = "Some\tValue";
+		AssertEquals ("#3", c.Value, "Some\tValue");
+	}
+	
+	public void TestPort ()
+	{
+		Cookie c = new Cookie ("SomeName", "SomeValue");
+		try {
+			c.Port = "123";
+			Fail ("#1: port must start and end with double quotes");
+		} catch (CookieException) {			
+		}
+		try {
+			c.Port = "\"123\"";
+		} catch (CookieException) {			
+			Fail ("#2");
+		}
+		try {
+			c.Port = "\"123;124\"";
+			Fail ("#3");
+		} catch (CookieException) {					
+		}
+		try {
+			c.Port = "\"123,123,124\"";
+		} catch (CookieException) {			
+			Fail ("#4");
+		}
+		try {
+			c.Port = "\"123,124\"";
+		} catch (CookieException) {			
+			Fail ("#5");
+		}
+	}
+
+        public void TestEquals ()
+        {
+		Cookie c1 = new Cookie ("NAME", "VALUE", "PATH", "DOMAIN");
+		Cookie c2 = new Cookie ("name", "value", "path", "domain");
+		Assert("#1", !c1.Equals (c2));
+		c2.Value = "VALUE";
+		c2.Path = "PATH";
+		Assert("#2", c1.Equals (c2));
+		c2.Version = 1;
+		Assert("#3", !c1.Equals (c2));
+        }
+}
+
+}
+