2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System / Test / System.Net / WebProxyTest.cs
1 //
2 // WebProxyTest.cs - NUnit Test Cases for System.Net.WebProxy
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Gert Driesen (drieseng@users.sourceforge.net)
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 //
11
12 using System;
13 using System.Collections;
14 using System.IO;
15 using System.Net;
16 using System.Runtime.Serialization;
17 using System.Runtime.Serialization.Formatters;
18 using System.Runtime.Serialization.Formatters.Binary;
19 using System.Threading;
20
21 using NUnit.Framework;
22
23 namespace MonoTests.System.Net
24 {
25         [TestFixture]
26         public class WebProxyTest
27         {
28                 [Test]
29                 public void Constructors ()
30                 {
31                         WebProxy p = new WebProxy ();
32                         Assertion.Assert ("#1", p.Address == null);
33                         Assertion.AssertEquals ("#2", 0, p.BypassArrayList.Count);
34                         Assertion.AssertEquals ("#3", 0, p.BypassList.Length);
35                         Assertion.AssertEquals ("#4", false, p.BypassProxyOnLocal);
36                         try {
37                                 p.BypassList = null;
38                                 Assertion.Fail ("#5 not spec'd, but should follow ms.net implementation");
39                         } catch (ArgumentNullException) { }
40
41                         p = new WebProxy ("webserver.com", 8080);
42                         Assertion.AssertEquals ("#6", new Uri ("http://webserver.com:8080/"), p.Address);
43
44                         p = new WebProxy ("webserver");
45                         Assertion.AssertEquals ("#7", new Uri ("http://webserver"), p.Address);
46
47                         p = new WebProxy ("webserver.com");
48                         Assertion.AssertEquals ("#8", new Uri ("http://webserver.com"), p.Address);
49
50                         p = new WebProxy ("http://webserver.com");
51                         Assertion.AssertEquals ("#9", new Uri ("http://webserver.com"), p.Address);
52
53                         p = new WebProxy ("file://webserver");
54                         Assertion.AssertEquals ("#10", new Uri ("file://webserver"), p.Address);
55
56                         p = new WebProxy ("http://www.contoso.com", true, null, null);
57                         Assertion.AssertEquals ("#11", 0, p.BypassList.Length);
58                         Assertion.AssertEquals ("#12", 0, p.BypassArrayList.Count);
59
60                         try {
61                                 p = new WebProxy ("http://contoso.com", true,
62                                         new string [] { "?^!@#$%^&}{][" }, null);
63                                 Assertion.Fail ("#13: illegal regular expression");
64                         } catch (ArgumentException) {
65                         }
66                 }
67
68                 [Test]
69                 public void BypassArrayList ()
70                 {
71                         Uri proxy1 = new Uri ("http://proxy.contoso.com");
72                         Uri proxy2 = new Uri ("http://proxy2.contoso.com");
73
74                         WebProxy p = new WebProxy (proxy1, true);
75                         p.BypassArrayList.Add ("http://proxy2.contoso.com");
76                         p.BypassArrayList.Add ("http://proxy2.contoso.com");
77                         Assertion.AssertEquals ("#1", 2, p.BypassList.Length);
78                         Assertion.Assert ("#2", !p.IsBypassed (new Uri ("http://www.google.com")));
79                         Assertion.Assert ("#3", p.IsBypassed (proxy2));
80                         Assertion.AssertEquals ("#4", proxy2, p.GetProxy (proxy2));
81
82                         p.BypassArrayList.Add ("?^!@#$%^&}{][");
83                         Assertion.AssertEquals ("#10", 3, p.BypassList.Length);
84                         try {
85                                 Assertion.Assert ("#11", !p.IsBypassed (proxy2));
86                                 Assertion.Assert ("#12", !p.IsBypassed (new Uri ("http://www.x.com")));
87                                 Assertion.AssertEquals ("#13", proxy1, p.GetProxy (proxy2));
88                                 // hmm... although #11 and #13 succeeded before (#3 resp. #4), 
89                                 // it now fails to bypass, and the IsByPassed and GetProxy 
90                                 // methods do not fail.. so when an illegal regular 
91                                 // expression is added through this property it's ignored. 
92                                 // probably an ms.net bug?? :(
93                         } catch (ArgumentException) {
94                                 Assertion.Fail ("#15: illegal regular expression");
95                         }
96                 }
97
98                 [Test]
99                 public void BypassList ()
100                 {
101                         Uri proxy1 = new Uri ("http://proxy.contoso.com");
102                         Uri proxy2 = new Uri ("http://proxy2.contoso.com");
103
104                         WebProxy p = new WebProxy (proxy1, true);
105                         try {
106                                 p.BypassList = new string [] { "http://proxy2.contoso.com", "?^!@#$%^&}{][" };
107                                 Assertion.Fail ("#1");
108                         } catch (ArgumentException) {
109                                 // weird, this way invalid regex's fail again..
110                         }
111
112                         Assertion.AssertEquals ("#2", 2, p.BypassList.Length);
113                         // but it did apparenly store the regex's !
114
115                         p.BypassList = new string [] { "http://www.x.com" };
116                         Assertion.AssertEquals ("#3", 1, p.BypassList.Length);
117
118                         try {
119                                 p.BypassList = null;
120                                 Assertion.Fail ("#4");
121                         } catch (ArgumentNullException) { }
122
123                         Assertion.AssertEquals ("#4", 1, p.BypassList.Length);
124                 }
125
126                 [Test]
127                 public void GetProxy ()
128                 {
129                 }
130
131                 [Test]
132                 public void IsByPassed ()
133                 {
134                         WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
135                         Assertion.Assert ("#1", !p.IsBypassed (new Uri ("http://www.google.com")));
136                         Assertion.Assert ("#2", p.IsBypassed (new Uri ("http://localhost/index.html")));
137                         Assertion.Assert ("#3", p.IsBypassed (new Uri ("http://localhost:8080/index.html")));
138                         Assertion.Assert ("#4", p.IsBypassed (new Uri ("http://loopback:8080/index.html")));
139                         Assertion.Assert ("#5", p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")));
140                         Assertion.Assert ("#6", p.IsBypassed (new Uri ("http://webserver/index.html")));
141                         Assertion.Assert ("#7", !p.IsBypassed (new Uri ("http://webserver.com/index.html")));
142
143                         p = new WebProxy ("http://proxy.contoso.com", false);
144                         Assertion.Assert ("#11", !p.IsBypassed (new Uri ("http://www.google.com")));
145                         Assertion.Assert ("#12: lamespec of ms.net", p.IsBypassed (new Uri ("http://localhost/index.html")));
146                         Assertion.Assert ("#13: lamespec of ms.net", p.IsBypassed (new Uri ("http://localhost:8080/index.html")));
147                         Assertion.Assert ("#14: lamespec of ms.net", p.IsBypassed (new Uri ("http://loopback:8080/index.html")));
148                         Assertion.Assert ("#15: lamespec of ms.net", p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")));
149                         Assertion.Assert ("#16", !p.IsBypassed (new Uri ("http://webserver/index.html")));
150
151                         p.BypassList = new string [] { "google.com", "contoso.com" };
152                         Assertion.Assert ("#20", p.IsBypassed (new Uri ("http://www.google.com")));
153                         Assertion.Assert ("#21", p.IsBypassed (new Uri ("http://www.GOOGLE.com")));
154                         Assertion.Assert ("#22", p.IsBypassed (new Uri ("http://www.contoso.com:8080/foo/bar/index.html")));
155                         Assertion.Assert ("#23", !p.IsBypassed (new Uri ("http://www.contoso2.com:8080/foo/bar/index.html")));
156                         Assertion.Assert ("#24", !p.IsBypassed (new Uri ("http://www.foo.com:8080/contoso.com.html")));
157
158                         p.BypassList = new string [] { "https" };
159                         Assertion.Assert ("#30", !p.IsBypassed (new Uri ("http://www.google.com")));
160                         Assertion.Assert ("#31", p.IsBypassed (new Uri ("https://www.google.com")));
161                 }
162
163                 [Test]
164                 public void IsByPassed_Address_Null ()
165                 {
166                         WebProxy p = new WebProxy ((Uri) null, false);
167                         Assertion.Assert ("#1", p.IsBypassed (new Uri ("http://www.google.com")));
168
169                         p = new WebProxy ((Uri) null, true);
170                         Assertion.Assert ("#2", p.IsBypassed (new Uri ("http://www.google.com")));
171                 }
172
173                 [Test]
174 #if TARGET_JVM
175                 [Ignore ("TD BUG ID: 7213")]
176 #endif          
177                 public void IsByPassed_Host_Null ()
178                 {
179                         WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
180                         try {
181                                 p.IsBypassed (null);
182                                 Assertion.Fail ("#A1");
183 #if NET_2_0
184                         } catch (ArgumentNullException ex) {
185                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
186                                 Assert.IsNotNull (ex.Message, "#A3");
187                                 Assert.IsNotNull (ex.ParamName, "#A4");
188                                 Assert.AreEqual ("host", ex.ParamName, "#A5");
189                                 Assert.IsNull (ex.InnerException, "#A6");
190                         }
191 #else
192                         } catch (NullReferenceException) {
193                         }
194 #endif
195
196                         p = new WebProxy ((Uri) null);
197                         try {
198                                 p.IsBypassed (null);
199                                 Assertion.Fail ("#B1");
200 #if NET_2_0
201                         } catch (ArgumentNullException ex) {
202                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
203                                 Assert.IsNotNull (ex.Message, "#B3");
204                                 Assert.IsNotNull (ex.ParamName, "#B4");
205                                 Assert.AreEqual ("host", ex.ParamName, "#B5");
206                                 Assert.IsNull (ex.InnerException, "#B6");
207                         }
208 #else
209                         } catch (NullReferenceException) {
210                         }
211 #endif
212
213                         p = new WebProxy ((Uri) null, true);
214                         try {
215                                 p.IsBypassed (null);
216                                 Assertion.Fail ("#C1");
217 #if NET_2_0
218                         } catch (ArgumentNullException ex) {
219                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
220                                 Assert.IsNotNull (ex.Message, "#C3");
221                                 Assert.IsNotNull (ex.ParamName, "#C4");
222                                 Assert.AreEqual ("host", ex.ParamName, "#C5");
223                                 Assert.IsNull (ex.InnerException, "#C6");
224                         }
225 #else
226                         } catch (NullReferenceException) {
227                         }
228 #endif
229                 }
230
231                 [Test]
232 #if TARGET_JVM
233                 [Ignore ("The MS compliant binary serialization is not supported")]
234 #endif          
235                 public void GetObjectData ()
236                 {
237                         SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
238                                 new FormatterConverter ());
239
240                         WebProxy proxy = new WebProxy ("proxy.ximian.com");
241                         ((ISerializable) proxy).GetObjectData (si, new StreamingContext ());
242 #if NET_2_0
243                         Assert.AreEqual (4, si.MemberCount, "#A1");
244 #else
245                         Assert.AreEqual (3, si.MemberCount, "#A1");
246 #endif
247                         int i = 0;
248                         foreach (SerializationEntry entry in si) {
249                                 Assert.IsNotNull (entry.Name, "#A2:" + i);
250                                 Assert.IsNotNull (entry.ObjectType, "#A3:" + i);
251
252                                 switch (i) {
253                                 case 0:
254                                         Assert.AreEqual ("_BypassOnLocal", entry.Name, "#A4:" + i);
255                                         Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
256                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
257                                         Assert.AreEqual (false, entry.Value, "#A7:" + i);
258                                         break;
259                                 case 1:
260                                         Assert.AreEqual ("_ProxyAddress", entry.Name, "#A4:" + i);
261                                         Assert.AreEqual (typeof (Uri), entry.ObjectType, "#A5:" + i);
262                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
263                                         break;
264                                 case 2:
265                                         Assert.AreEqual ("_BypassList", entry.Name, "#A4:" + i);
266                                         Assert.AreEqual (typeof (object), entry.ObjectType, "#A5:" + i);
267                                         Assert.IsNull (entry.Value, "#A6:" + i);
268                                         break;
269 #if NET_2_0
270                                 case 3:
271                                         Assert.AreEqual ("_UseDefaultCredentials", entry.Name, "#A4:" + i);
272                                         Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
273                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
274                                         Assert.AreEqual (false, entry.Value, "#A7:" + i);
275                                         break;
276 #endif
277                                 }
278                                 i++;
279                         }
280                 }
281         }
282 }