[sgen] Don't register pinned objects if pin stats are disabled
[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                         Assert.IsTrue (p.Address == null, "#1");
33                         Assert.AreEqual (0, p.BypassArrayList.Count, "#2");
34                         Assert.AreEqual (0, p.BypassList.Length, "#3");
35                         Assert.AreEqual (false, p.BypassProxyOnLocal, "#4");
36                         try {
37                                 p.BypassList = null;
38                                 Assert.Fail ("#5 not spec'd, but should follow ms.net implementation");
39                         } catch (ArgumentNullException) { }
40
41                         p = new WebProxy ("webserver.com", 8080);
42                         Assert.AreEqual (new Uri ("http://webserver.com:8080/"), p.Address, "#6");
43
44                         p = new WebProxy ("webserver");
45                         Assert.AreEqual (new Uri ("http://webserver"), p.Address, "#7");
46
47                         p = new WebProxy ("webserver.com");
48                         Assert.AreEqual (new Uri ("http://webserver.com"), p.Address, "#8");
49
50                         p = new WebProxy ("http://webserver.com");
51                         Assert.AreEqual (new Uri ("http://webserver.com"), p.Address, "#9");
52
53                         p = new WebProxy ("file://webserver");
54                         Assert.AreEqual (new Uri ("file://webserver"), p.Address, "#10");
55
56                         p = new WebProxy ("http://www.contoso.com", true, null, null);
57                         Assert.AreEqual (0, p.BypassList.Length, "#11");
58                         Assert.AreEqual (0, p.BypassArrayList.Count, "#12");
59
60                         try {
61                                 p = new WebProxy ("http://contoso.com", true,
62                                         new string [] { "?^!@#$%^&}{][" }, null);
63                                 Assert.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                         Assert.AreEqual (2, p.BypassList.Length, "#1");
78                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.google.com")), "#2");
79                         Assert.IsTrue (p.IsBypassed (proxy2), "#3");
80                         Assert.AreEqual (proxy2, p.GetProxy (proxy2), "#4");
81
82                         p.BypassArrayList.Add ("?^!@#$%^&}{][");
83                         Assert.AreEqual (3, p.BypassList.Length, "#10");
84                         try {
85                                 Assert.IsTrue (!p.IsBypassed (proxy2), "#11");
86                                 Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.x.com")), "#12");
87                                 Assert.AreEqual (proxy1, p.GetProxy (proxy2), "#13");
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                                 Assert.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                                 Assert.Fail ("#1");
108                         } catch (ArgumentException) {
109                                 // weird, this way invalid regex's fail again..
110                         }
111
112                         Assert.AreEqual (2, p.BypassList.Length, "#2");
113                         // but it did apparenly store the regex's !
114
115                         p.BypassList = new string [] { "http://www.x.com" };
116                         Assert.AreEqual (1, p.BypassList.Length, "#3");
117
118                         try {
119                                 p.BypassList = null;
120                                 Assert.Fail ("#4");
121                         } catch (ArgumentNullException) { }
122
123                         Assert.AreEqual (1, p.BypassList.Length, "#4");
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                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.google.com")), "#1");
136                         Assert.IsTrue (p.IsBypassed (new Uri ("http://localhost/index.html")), "#2");
137                         Assert.IsTrue (p.IsBypassed (new Uri ("http://localhost:8080/index.html")), "#3");
138                         Assert.IsTrue (p.IsBypassed (new Uri ("http://loopback:8080/index.html")), "#4");
139                         Assert.IsTrue (p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")), "#5");
140                         Assert.IsTrue (p.IsBypassed (new Uri ("http://webserver/index.html")), "#6");
141                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://webserver.com/index.html")), "#7");
142
143                         p = new WebProxy ("http://proxy.contoso.com", false);
144                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.google.com")), "#11");
145                         Assert.IsTrue (p.IsBypassed (new Uri ("http://localhost/index.html")), "#12: lamespec of ms.net");
146                         Assert.IsTrue (p.IsBypassed (new Uri ("http://localhost:8080/index.html")), "#13: lamespec of ms.net");
147                         Assert.IsTrue (p.IsBypassed (new Uri ("http://loopback:8080/index.html")), "#14: lamespec of ms.net");
148                         Assert.IsTrue (p.IsBypassed (new Uri ("http://127.0.0.01:8080/index.html")), "#15: lamespec of ms.net");
149                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://webserver/index.html")), "#16");
150
151                         p.BypassList = new string [] { "google.com", "contoso.com" };
152                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.google.com")), "#20");
153                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.GOOGLE.com")), "#21");
154                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.contoso.com:8080/foo/bar/index.html")), "#22");
155                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.contoso2.com:8080/foo/bar/index.html")), "#23");
156                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.foo.com:8080/contoso.com.html")), "#24");
157
158                         p.BypassList = new string [] { "https" };
159                         Assert.IsTrue (!p.IsBypassed (new Uri ("http://www.google.com")), "#30");
160                         Assert.IsTrue (p.IsBypassed (new Uri ("https://www.google.com")), "#31");
161                 }
162
163                 [Test]
164                 public void IsByPassed_Address_Null ()
165                 {
166                         WebProxy p = new WebProxy ((Uri) null, false);
167                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.google.com")), "#1");
168
169                         p = new WebProxy ((Uri) null, true);
170                         Assert.IsTrue (p.IsBypassed (new Uri ("http://www.google.com")), "#2");
171                 }
172
173                 [Test]
174                 public void IsByPassed_Host_Null ()
175                 {
176                         WebProxy p = new WebProxy ("http://proxy.contoso.com", true);
177                         try {
178                                 p.IsBypassed (null);
179                                 Assert.Fail ("#A1");
180                         } catch (ArgumentNullException ex) {
181                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
182                                 Assert.IsNotNull (ex.Message, "#A3");
183                                 Assert.IsNotNull (ex.ParamName, "#A4");
184                                 Assert.AreEqual ("host", ex.ParamName, "#A5");
185                                 Assert.IsNull (ex.InnerException, "#A6");
186                         }
187
188                         p = new WebProxy ((Uri) null);
189                         try {
190                                 p.IsBypassed (null);
191                                 Assert.Fail ("#B1");
192                         } catch (ArgumentNullException ex) {
193                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
194                                 Assert.IsNotNull (ex.Message, "#B3");
195                                 Assert.IsNotNull (ex.ParamName, "#B4");
196                                 Assert.AreEqual ("host", ex.ParamName, "#B5");
197                                 Assert.IsNull (ex.InnerException, "#B6");
198                         }
199
200                         p = new WebProxy ((Uri) null, true);
201                         try {
202                                 p.IsBypassed (null);
203                                 Assert.Fail ("#C1");
204                         } catch (ArgumentNullException ex) {
205                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
206                                 Assert.IsNotNull (ex.Message, "#C3");
207                                 Assert.IsNotNull (ex.ParamName, "#C4");
208                                 Assert.AreEqual ("host", ex.ParamName, "#C5");
209                                 Assert.IsNull (ex.InnerException, "#C6");
210                         }
211                 }
212
213                 [Test]
214                 public void GetObjectData ()
215                 {
216                         SerializationInfo si = new SerializationInfo (typeof (WebHeaderCollection),
217                                 new FormatterConverter ());
218
219                         WebProxy proxy = new WebProxy ("proxy.ximian.com");
220                         ((ISerializable) proxy).GetObjectData (si, new StreamingContext ());
221                         Assert.AreEqual (4, si.MemberCount, "#A1");
222                         int i = 0;
223                         foreach (SerializationEntry entry in si) {
224                                 Assert.IsNotNull (entry.Name, "#A2:" + i);
225                                 Assert.IsNotNull (entry.ObjectType, "#A3:" + i);
226
227                                 switch (i) {
228                                 case 0:
229                                         Assert.AreEqual ("_BypassOnLocal", entry.Name, "#A4:" + i);
230                                         Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
231                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
232                                         Assert.AreEqual (false, entry.Value, "#A7:" + i);
233                                         break;
234                                 case 1:
235                                         Assert.AreEqual ("_ProxyAddress", entry.Name, "#A4:" + i);
236                                         Assert.AreEqual (typeof (Uri), entry.ObjectType, "#A5:" + i);
237                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
238                                         break;
239                                 case 2:
240                                         Assert.AreEqual ("_BypassList", entry.Name, "#A4:" + i);
241                                         Assert.AreEqual (typeof (object), entry.ObjectType, "#A5:" + i);
242                                         Assert.IsNull (entry.Value, "#A6:" + i);
243                                         break;
244                                 case 3:
245                                         Assert.AreEqual ("_UseDefaultCredentials", entry.Name, "#A4:" + i);
246                                         Assert.AreEqual (typeof (bool), entry.ObjectType, "#A5:" + i);
247                                         Assert.IsNotNull (entry.Value, "#A6:" + i);
248                                         Assert.AreEqual (false, entry.Value, "#A7:" + i);
249                                         break;
250                                 }
251                                 i++;
252                         }
253                 }
254         }
255 }