Explorar o código

* WebProxy.cs: more rigorous testing, fixed bug + had to change
internal representation of bypasslist to ArrayList, different
implementation of checking regex's.

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

Lawrence Pit %!s(int64=23) %!d(string=hai) anos
pai
achega
7ba9248113

+ 3 - 1
mcs/class/System/System.Net/ChangeLog

@@ -1,7 +1,9 @@
 2002-05-21  Lawrence Pit <[email protected]>
 
 	* WebClient.cs: stubbed
-	* WebProxy.cs: fixed bug
+	* WebProxy.cs: fixed bug; had to change internal representation
+	of bypasslist to ArrayList, different implementation of checking
+	regex's.
 
 2002-05-20  Lawrence Pit <[email protected]>
 

+ 38 - 23
mcs/class/System/System.Net/WebProxy.cs

@@ -17,10 +17,8 @@ namespace System.Net
 	{		
 		private Uri address;
 		private bool bypassOnLocal;
-		private string [] bypassList;
+		private ArrayList bypassList;
 		private ICredentials credentials;
-		
-		private Regex [] bypassRegexList;
 	
 		// Constructors
 	
@@ -59,9 +57,9 @@ namespace System.Net
 			this.bypassOnLocal = bypassOnLocal;
 			if (bypassList == null)
 				bypassList = new string [] {};
-			this.bypassList = bypassList;
+			this.bypassList = new ArrayList (bypassList);
 			this.credentials = credentials;
-			CreateBypassRegexList ();
+			CheckBypassList ();
 		}
 		
 		[MonoTODO]
@@ -79,17 +77,17 @@ namespace System.Net
 		
 		public ArrayList BypassArrayList {
 			get { 
-				return new ArrayList (bypassList);
+				return bypassList;
 			}
 		}
 		
-		public string[] BypassList {
-			get { return bypassList; }
+		public string [] BypassList {
+			get { return (string []) bypassList.ToArray (typeof (string)); }
 			set { 
 				if (value == null)
 					throw new ArgumentNullException ();
-				bypassList = value; 
-				CreateBypassRegexList ();
+				bypassList = new ArrayList (value); 
+				CheckBypassList ();
 			}
 		}
 		
@@ -138,12 +136,31 @@ namespace System.Net
 			if (bypassOnLocal && host.Host.IndexOf ('.') == -1)
 				return true;
 				
-			string hostStr = host.Scheme + "://" + host.Authority;				
-			for (int i = 0; i < bypassRegexList.Length; i++) 
-				if (bypassRegexList [i].IsMatch (hostStr))
-					return true;
-			
-			return false;
+			try {				
+				string hostStr = host.Scheme + "://" + host.Authority;				
+				int i = 0;
+				for (; i < bypassList.Count; i++) {
+					Regex regex = new Regex ((string) bypassList [i], 
+						// TODO: RegexOptions.Compiled |  // not implemented yet by Regex
+						RegexOptions.IgnoreCase |
+						RegexOptions.Singleline);
+
+					if (regex.IsMatch (hostStr))
+						break;
+				}
+
+				if (i == bypassList.Count)
+					return false;
+
+				// continue checking correctness of regular expressions..
+				// will throw expression when an invalid one is found
+				for (; i < bypassList.Count; i++)
+					new Regex ((string) bypassList [i]);
+
+				return true;
+			} catch (ArgumentException) {
+				return false;
+			}
 		}
 
 		[MonoTODO]		
@@ -155,14 +172,12 @@ namespace System.Net
 		
 		// Private Methods
 		
-		private void CreateBypassRegexList ()
+		// this compiles the regular expressions, and will throw
+		// an exception when an invalid one is found.
+		private void CheckBypassList ()
 		{			
-			bypassRegexList	= new Regex [bypassList.Length];
-			for (int i = 0; i < bypassList.Length; i++)
-				bypassRegexList [i] = new Regex (bypassList [i], 
-							// TODO: RegexOptions.Compiled |  // not implemented yet by Regex
-							RegexOptions.IgnoreCase |
-							RegexOptions.Singleline);
+			for (int i = 0; i < bypassList.Count; i++)
+				new Regex ((string) bypassList [i]);
 		}
 		
 		private static Uri ToUri (string address)

+ 2 - 1
mcs/class/System/Test/System.Net/ChangeLog

@@ -1,6 +1,7 @@
 2002-05-21  Lawrence Pit <[email protected]>
 
-	* WebProxyTest.cs: added more ctor tests
+	* WebClientTest.cs: added
+	* WebProxyTest.cs: more tests
 
 2002-05-20  Lawrence Pit <[email protected]>
 

+ 71 - 0
mcs/class/System/Test/System.Net/WebProxyTest.cs

@@ -67,6 +67,73 @@ public class WebProxyTest : TestCase
 
 		p = new WebProxy ("file://webserver");
 		AssertEquals ("#10", new Uri ("file://webserver"), p.Address);		
+		
+		p = new WebProxy ("http://www.contoso.com", true, null, null);
+		AssertEquals ("#11", 0, p.BypassList.Length);
+		AssertEquals ("#12", 0, p.BypassArrayList.Count);
+		
+		try {
+			p = new WebProxy ("http://contoso.com", true, 
+				new string [] {"?^!@#$%^&}{]["}, null);
+			Fail ("#13: illegal regular expression");
+		} catch (ArgumentException) {
+		}
+	}
+	
+	public void TestBypassArrayList ()
+	{
+		Uri proxy1 = new Uri("http://proxy.contoso.com");
+		Uri proxy2 = new Uri ("http://proxy2.contoso.com");
+		
+		WebProxy p = new WebProxy (proxy1, true);
+		p.BypassArrayList.Add ("http://proxy2.contoso.com");
+		p.BypassArrayList.Add ("http://proxy2.contoso.com");		
+		AssertEquals ("#1", 2, p.BypassList.Length);
+		Assert ("#2", !p.IsBypassed (new Uri ("http://www.google.com")));
+		Assert ("#3", p.IsBypassed (proxy2));
+		AssertEquals ("#4", proxy2, p.GetProxy (proxy2));
+
+		p.BypassArrayList.Add ("?^!@#$%^&}{][");
+		AssertEquals ("#10", 3, p.BypassList.Length);
+		try {
+			Assert ("#11", !p.IsBypassed (proxy2));
+			Assert ("#12", !p.IsBypassed (new Uri ("http://www.x.com")));		
+			AssertEquals ("#13", proxy1, p.GetProxy (proxy2));
+			// hmm... although #11 and #13 succeeded before (#3 resp. #4), 
+			// it now fails to bypass, and the IsByPassed and GetProxy 
+			// methods do not fail.. so when an illegal regular 
+			// expression is added through this property it's ignored. 
+			// probably an ms.net bug?? :(
+		} catch (ArgumentException) {
+			Fail ("#15: illegal regular expression");
+		}		
+	}
+	
+	public void TestBypassList ()
+	{
+		Uri proxy1 = new Uri("http://proxy.contoso.com");
+		Uri proxy2 = new Uri ("http://proxy2.contoso.com");
+		
+		WebProxy p = new WebProxy (proxy1, true);
+		try {
+			p.BypassList = new string [] {"http://proxy2.contoso.com", "?^!@#$%^&}{]["};		
+			Fail ("#1");
+		} catch (ArgumentException) {
+			// weird, this way invalid regex's fail again..
+		}
+		
+		AssertEquals ("#2", 2, p.BypassList.Length);
+		// but it did apparenly store the regex's !
+
+		p.BypassList = new string [] {"http://www.x.com"};		
+		AssertEquals ("#3", 1, p.BypassList.Length);
+
+		try {
+			p.BypassList = null;
+			Fail ("#4");
+		} catch (ArgumentNullException) {}
+		
+		AssertEquals ("#4", 1, p.BypassList.Length);		
 	}
 	
 	public void TestGetProxy ()
@@ -102,6 +169,10 @@ public class WebProxyTest : TestCase
 		Assert ("#22", p.IsBypassed (new Uri ("http://www.contoso.com:8080/foo/bar/index.html")));
 		Assert ("#23", !p.IsBypassed (new Uri ("http://www.contoso2.com:8080/foo/bar/index.html")));
 		Assert ("#24", !p.IsBypassed (new Uri ("http://www.foo.com:8080/contoso.com.html")));
+		
+		p.BypassList = new string [] { "https" };		
+		Assert ("#30", !p.IsBypassed (new Uri ("http://www.google.com")));
+		Assert ("#31", p.IsBypassed (new Uri ("https://www.google.com")));
 	}
 }