New test.
[mono.git] / mcs / class / System / Test / System / UriTest.cs
1 //
2 // UriTest.cs - NUnit Test Cases for System.Uri
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Ben Maurer (bmaurer@users.sourceforge.net)
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Ben Maurer
11 //
12
13 using NUnit.Framework;
14 using System;
15 using System.IO;
16
17 namespace MonoTests.System
18 {
19         [TestFixture]
20         public class UriTest : Assertion
21         {
22                 protected bool isWin32 = false;
23
24                 [TestFixtureSetUp]
25                 public void GetReady () 
26                 {
27                         isWin32 = (Path.DirectorySeparatorChar == '\\');
28                 }
29
30                 [Test]
31                 public void Constructors ()
32                 {
33                         Uri uri = null;
34                         
35                         /*
36                         uri = new Uri ("http://www.ximian.com/foo" + ((char) 0xa9) + "/bar/index.cgi?a=1&b=" + ((char) 0xa9) + "left#fragm?ent2");
37                         Print (uri);
38
39                         uri = new Uri ("http://www.ximian.com/foo/xxx\"()-._;<=>@{|}~-,.`_^]\\[xx/" + ((char) 0xa9) + "/bar/index.cgi#fra+\">=@[gg]~gment2");
40                         Print (uri);
41
42                         uri = new Uri("http://11.22.33.588:9090");
43                         Print (uri);
44
45                         uri = new Uri("http://[11:22:33::88]:9090");
46                         Print (uri);
47                         
48                         uri = new Uri("http://[::127.11.22.33]:8080");
49                         Print (uri);
50                         
51                         uri = new Uri("http://[abcde::127.11.22.33]:8080");
52                         Print (uri);                    
53                         */
54                         
55                         /*
56                         uri = new Uri ("http://www.contoso.com:1234/foo/bar/");
57                         Print (uri);
58
59                         uri = new Uri ("http://www.contoso.com:1234/foo/bar");
60                         Print (uri);
61                         
62                         uri = new Uri ("http://www.contoso.com:1234/");
63                         Print (uri);
64
65                         uri = new Uri ("http://www.contoso.com:1234");
66                         Print (uri);
67                         */
68                         
69                         uri = new Uri ("http://contoso.com?subject=uri");
70                         AssertEquals ("#k1", "/", uri.AbsolutePath);
71                         AssertEquals ("#k2", "http://contoso.com/?subject=uri", uri.AbsoluteUri);
72                         AssertEquals ("#k3", "contoso.com", uri.Authority);
73                         AssertEquals ("#k4", "", uri.Fragment);
74                         AssertEquals ("#k5", "contoso.com", uri.Host);
75                         AssertEquals ("#k6", UriHostNameType.Dns, uri.HostNameType);
76                         AssertEquals ("#k7", true, uri.IsDefaultPort);
77                         AssertEquals ("#k8", false, uri.IsFile);
78                         AssertEquals ("#k9", false, uri.IsLoopback);
79                         AssertEquals ("#k10", false, uri.IsUnc);
80                         AssertEquals ("#k11", "/", uri.LocalPath);
81                         AssertEquals ("#k12", "/?subject=uri", uri.PathAndQuery);
82                         AssertEquals ("#k13", 80, uri.Port);
83                         AssertEquals ("#k14", "?subject=uri", uri.Query);
84                         AssertEquals ("#k15", "http", uri.Scheme);
85                         AssertEquals ("#k16", false, uri.UserEscaped);
86                         AssertEquals ("#k17", "", uri.UserInfo);
87
88                         uri = new Uri ("mailto:user:pwd@contoso.com?subject=uri");
89                         AssertEquals ("#m1", "", uri.AbsolutePath);
90                         AssertEquals ("#m2", "mailto:user:pwd@contoso.com?subject=uri", uri.AbsoluteUri);
91                         AssertEquals ("#m3", "contoso.com", uri.Authority);
92                         AssertEquals ("#m4", "", uri.Fragment);
93                         AssertEquals ("#m5", "contoso.com", uri.Host);
94                         AssertEquals ("#m6", UriHostNameType.Dns, uri.HostNameType);
95                         AssertEquals ("#m7", true, uri.IsDefaultPort);
96                         AssertEquals ("#m8", false, uri.IsFile);
97                         AssertEquals ("#m9", false, uri.IsLoopback);
98                         AssertEquals ("#m10", false, uri.IsUnc);
99                         AssertEquals ("#m11", "", uri.LocalPath);
100                         AssertEquals ("#m12", "?subject=uri", uri.PathAndQuery);
101                         AssertEquals ("#m13", 25, uri.Port);
102                         AssertEquals ("#m14", "?subject=uri", uri.Query);
103                         AssertEquals ("#m15", "mailto", uri.Scheme);
104                         AssertEquals ("#m16", false, uri.UserEscaped);
105                         AssertEquals ("#m17", "user:pwd", uri.UserInfo);
106                         
107                         uri = new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
108                         AssertEquals ("#n1", "/mydir/mysubdir/myfile.ext", uri.AbsolutePath);
109                         AssertEquals ("#n2", "file://myserver/mydir/mysubdir/myfile.ext", uri.AbsoluteUri);
110                         AssertEquals ("#n3", "myserver", uri.Authority);
111                         AssertEquals ("#n4", "", uri.Fragment);
112                         AssertEquals ("#n5", "myserver", uri.Host);
113                         AssertEquals ("#n6", UriHostNameType.Dns, uri.HostNameType);
114                         AssertEquals ("#n7", true, uri.IsDefaultPort);
115                         AssertEquals ("#n8", true, uri.IsFile);
116                         AssertEquals ("#n9", false, uri.IsLoopback);
117                         AssertEquals ("#n10", true, uri.IsUnc);
118
119                         if (isWin32)
120                                 AssertEquals ("#n11", @"\\myserver\mydir\mysubdir\myfile.ext", uri.LocalPath);
121                         else
122                                 // myserver never could be the part of Unix path.
123                                 AssertEquals ("#n11", "/mydir/mysubdir/myfile.ext", uri.LocalPath);
124
125                         AssertEquals ("#n12", "/mydir/mysubdir/myfile.ext", uri.PathAndQuery);
126                         AssertEquals ("#n13", -1, uri.Port);
127                         AssertEquals ("#n14", "", uri.Query);
128                         AssertEquals ("#n15", "file", uri.Scheme);
129                         AssertEquals ("#n16", false, uri.UserEscaped);
130                         AssertEquals ("#n17", "", uri.UserInfo);
131                         
132                         uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
133                         AssertEquals ("#rel1a", "http://www.contoso.com/Hello World.htm", uri.AbsoluteUri);
134                         AssertEquals ("#rel1b", true, uri.UserEscaped);
135                         uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
136                         AssertEquals ("#rel2a", "http://www.contoso.com/Hello%20World.htm", uri.AbsoluteUri);
137                         AssertEquals ("#rel2b", false, uri.UserEscaped);
138                         uri = new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
139                         AssertEquals ("#rel3", "http://www.xxx.com/Hello%20World.htm", uri.AbsoluteUri);
140
141                         uri = new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
142                         AssertEquals ("#rel5", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
143                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "foo/bar/Hello World.htm?x=0:8", false);
144                         AssertEquals ("#rel6", "http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
145                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
146                         AssertEquals ("#rel7", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
147                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
148                         AssertEquals ("#rel8", "http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
149                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../../../foo/bar/Hello World.htm?x=0:8", false);
150 #if NET_2_0
151                         AssertEquals ("#rel9", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
152 #else
153                         AssertEquals ("#rel9", "http://www.contoso.com/../foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
154 #endif\r
155                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
156                         AssertEquals ("#rel10", "http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
157
158                         uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
159                         AssertEquals("#22", "http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString ());
160
161                         uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
162                         AssertEquals ("#rel30", "http://www.xxx.com/?x=0", uri.ToString());
163                         uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "?x=0");
164                         AssertEquals ("#rel31", "http://www.xxx.com/?x=0", uri.ToString());
165                         uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
166                         AssertEquals ("#rel32", "http://www.xxx.com/index.htm#here", uri.ToString());
167 #if NET_2_0
168                         uri = new Uri ("relative", UriKind.Relative);
169                         uri = new Uri ("relative/abc", UriKind.Relative);
170                         uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
171
172                         Assert ("#rel33", !uri.IsAbsoluteUri);
173                         AssertEquals ("#rel34", uri.OriginalString, "relative");
174                         Assert ("#rel35", !uri.UserEscaped);
175 #endif
176                 }\r
177 \r
178                 [Test]\r
179                 public void Constructor_DualHostPort ()\r
180                 {\r
181                         string relative = "foo:8080/bar/Hello World.htm";\r
182                         Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);\r
183                         AssertEquals ("AbsolutePath", "8080/bar/Hello%20World.htm", uri.AbsolutePath);\r
184                         AssertEquals ("AbsoluteUri", "foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri);\r
185                         AssertEquals ("Authority", String.Empty, uri.Authority);\r
186                         AssertEquals ("Fragment", String.Empty, uri.Fragment);\r
187                         AssertEquals ("Host", String.Empty, uri.Host);\r
188                         AssertEquals ("PathAndQuery", "8080/bar/Hello%20World.htm", uri.PathAndQuery);\r
189                         AssertEquals ("Port", -1, uri.Port);\r
190                         AssertEquals ("Query", String.Empty, uri.Query);\r
191                         AssertEquals ("Scheme", "foo", uri.Scheme);\r
192                         AssertEquals ("Query", String.Empty, uri.UserInfo);\r
193 \r
194                         AssertEquals ("Segments[0]", "8080/", uri.Segments[0]);\r
195                         AssertEquals ("Segments[1]", "bar/", uri.Segments[1]);\r
196                         AssertEquals ("Segments[2]", "Hello%20World.htm", uri.Segments[2]);\r
197 \r
198                         Assert ("IsDefaultPort", uri.IsDefaultPort);\r
199                         Assert ("IsFile", !uri.IsFile);\r
200                         Assert ("IsLoopback", !uri.IsLoopback);\r
201                         Assert ("IsUnc", !uri.IsUnc);\r
202                         Assert ("UserEscaped", !uri.UserEscaped);\r
203 #if NET_2_0\r
204                         AssertEquals ("HostNameType", UriHostNameType.Unknown, uri.HostNameType);\r
205                         Assert ("IsAbsoluteUri", uri.IsAbsoluteUri);\r
206                         AssertEquals ("OriginalString", relative, uri.OriginalString);
207 #else
208                         AssertEquals ("HostNameType", UriHostNameType.Basic, uri.HostNameType);\r
209 #endif\r
210                 }\r
211 \r
212                 [Test]\r
213 #if NET_2_0\r
214                 [ExpectedException (typeof (ArgumentNullException))]\r
215 #else\r
216                 [ExpectedException (typeof (NullReferenceException))]\r
217 #endif\r
218                 public void Constructor_NullStringBool ()\r
219                 {\r
220                         new Uri (null, "http://www.contoso.com/index.htm", false);\r
221                 }\r
222 \r
223                 [Test]\r
224 #if ONLY_1_1\r
225                 [ExpectedException (typeof (NullReferenceException))]\r
226 #endif\r
227                 public void Constructor_UriNullBool ()\r
228                 {\r
229                         new Uri (new Uri ("http://www.contoso.com"), null, false);\r
230                 }
231
232                 // regression for bug #47573
233                 [Test]
234                 public void RelativeCtor ()
235                 {
236                         Uri b = new Uri ("http://a/b/c/d;p?q");
237                         AssertEquals ("#1", "http://a/g", new Uri (b, "/g").ToString ());
238                         AssertEquals ("#2", "http://g/", new Uri (b, "//g").ToString ());
239                         AssertEquals ("#3", "http://a/b/c/?y", new Uri (b, "?y").ToString ());
240                         Assert ("#4", new Uri (b, "#s").ToString ().EndsWith ("#s"));
241
242                         Uri u = new Uri (b, "/g?q=r");
243                         AssertEquals ("#5", "http://a/g?q=r", u.ToString ());
244                         AssertEquals ("#6", "?q=r", u.Query);
245
246                         u = new Uri (b, "/g?q=r;. a");
247                         AssertEquals ("#5", "http://a/g?q=r;. a", u.ToString ());
248                         AssertEquals ("#6", "?q=r;.%20a", u.Query);
249                 }
250
251 #if NET_2_0\r
252                 [Test]\r
253                 [ExpectedException (typeof (UriFormatException))]\r
254                 public void RelativeCtor_11_Crasher ()
255                 {\r
256                         Uri b = new Uri ("http://a/b/c/d;p?q");\r
257                         // this causes crash under MS.NET 1.1\r
258                         AssertEquals ("g:h", "g:h", new Uri (b, "g:h").ToString ());\r
259                 }
260 #endif\r
261 \r
262                 [Test]\r
263                 [ExpectedException (typeof (UriFormatException))]\r
264                 public void Bad_IPv6 ()\r
265                 {\r
266                         new Uri ("http://0:0:0:0::127.1.2.3]/");\r
267                 }
268
269                 [Test]
270 #if ONLY_1_1
271                 [Category ("NotDotNet")]
272 #endif
273                 public void LeadingSlashes_ShouldFailOn1x ()
274                 {
275                         // doesn't (but should) fail under 1.x
276                         AssertEquals ("#1", "file:///", new Uri ("file:///").ToString ());
277                         AssertEquals ("#2", "file:///", new Uri ("file://").ToString ());
278                 }\r
279 \r
280                 [Test]\r
281 #if ONLY_1_1
282                 [Category ("NotWorking")]
283 #endif\r
284                 public void LeadingSlashes_BadResultsOn1x ()\r
285                 {\r
286                         // strange behaviours of 1.x - it's probably not worth to fix it\r
287                         // on Mono as 2.0 has been fixed\r
288                         Uri u = new Uri ("file:///foo/bar");\r
289 #if NET_2_0\r
290                         AssertEquals ("#3a", String.Empty, u.Host);\r
291                         AssertEquals ("#3b", UriHostNameType.Basic, u.HostNameType);\r
292                         AssertEquals ("#3c", "file:///foo/bar", u.ToString ());\r
293                         AssertEquals ("#3d", false, u.IsUnc);\r
294 #else
295                         // 1.x misinterpret the first path element as the host name
296                         AssertEquals ("#3a", "foo", u.Host);\r
297                         AssertEquals ("#3b", UriHostNameType.Dns, u.HostNameType);\r
298                         AssertEquals ("#3c", "file://foo/bar", u.ToString ());
299                         AssertEquals ("#3d", true, u.IsUnc);
300 #endif\r
301                         u = new Uri ("mailto:/foo");\r
302 #if NET_2_0\r
303                         AssertEquals ("#13a", String.Empty, u.Host);\r
304                         AssertEquals ("#13b", UriHostNameType.Basic, u.HostNameType);\r
305                         AssertEquals ("#13c", "mailto:/foo", u.ToString ());\r
306 #else
307                         // 1.x misinterpret the first path element as the host name
308                         AssertEquals ("#13a", "foo", u.Host);\r
309                         AssertEquals ("#13b", UriHostNameType.Dns, u.HostNameType);\r
310                         AssertEquals ("#13c", "mailto:foo", u.ToString ());\r
311 #endif\r
312                         u = new Uri ("mailto://foo");\r
313 #if NET_2_0\r
314                         AssertEquals ("#14a", String.Empty, u.Host);\r
315                         AssertEquals ("#14b", UriHostNameType.Basic, u.HostNameType);\r
316                         AssertEquals ("#14c", "mailto://foo", u.ToString ());\r
317 #else
318                         // 1.x misinterpret the first path element as the host name
319                         AssertEquals ("#14a", "foo", u.Host);\r
320                         AssertEquals ("#14b", UriHostNameType.Dns, u.HostNameType);\r
321                         AssertEquals ("#14c", "mailto://foo/", u.ToString ());\r
322 #endif\r
323                         u = new Uri ("news:/");\r
324                         AssertEquals ("#18a", String.Empty, u.Host);\r
325 #if NET_2_0\r
326                         AssertEquals ("#18b", UriHostNameType.Unknown, u.HostNameType);\r
327                         AssertEquals ("#18c", "news:/", u.ToString ());\r
328                         AssertEquals ("#18d", "/", u.AbsolutePath);\r
329                         AssertEquals ("#18e", "news:/", u.AbsoluteUri);\r
330 #else
331                         AssertEquals ("#18b", UriHostNameType.Basic, u.HostNameType);
332                         AssertEquals ("#18c", "news:", u.ToString ());\r
333                         AssertEquals ("#18d", String.Empty, u.AbsolutePath);\r
334                         AssertEquals ("#18e", "news:", u.AbsoluteUri);\r
335 #endif\r
336                         u = new Uri ("news:/foo");\r
337                         AssertEquals ("#19a", String.Empty, u.Host);\r
338 #if NET_2_0\r
339                         AssertEquals ("#19b", UriHostNameType.Unknown, u.HostNameType);\r
340                         AssertEquals ("#19c", "news:/foo", u.ToString ());\r
341                         AssertEquals ("#19d", "/foo", u.AbsolutePath);\r
342                         AssertEquals ("#19e", "news:/foo", u.AbsoluteUri);\r
343 #else
344                         AssertEquals ("#19b", UriHostNameType.Basic, u.HostNameType);
345                         AssertEquals ("#19c", "news:foo", u.ToString ());\r
346                         AssertEquals ("#19d", "foo", u.AbsolutePath);\r
347                         AssertEquals ("#19e", "news:foo", u.AbsoluteUri);\r
348 #endif\r
349                         u = new Uri ("news://foo");\r
350 #if NET_2_0\r
351                         AssertEquals ("#20a", String.Empty, u.Host);\r
352                         AssertEquals ("#20b", UriHostNameType.Unknown, u.HostNameType);\r
353                         AssertEquals ("#20c", "news://foo", u.ToString ());\r
354                         AssertEquals ("#20d", "//foo", u.AbsolutePath);\r
355                         AssertEquals ("#20e", "news://foo", u.AbsoluteUri);\r
356 #else
357                         AssertEquals ("#20a", "foo", u.Host);\r
358                         AssertEquals ("#20b", UriHostNameType.Dns, u.HostNameType);
359                         AssertEquals ("#20c", "news://foo/", u.ToString ());\r
360                         AssertEquals ("#20d", "/", u.AbsolutePath);\r
361                         AssertEquals ("#20e", "news://foo/", u.AbsoluteUri);\r
362 #endif\r
363                         u = new Uri ("news://foo/bar");\r
364 #if NET_2_0\r
365                         AssertEquals ("#22a", String.Empty, u.Host);\r
366                         AssertEquals ("#22b", UriHostNameType.Unknown, u.HostNameType);\r
367                         AssertEquals ("#22c", "news://foo/bar", u.ToString ());\r
368                         AssertEquals ("#22d", "//foo/bar", u.AbsolutePath);\r
369                         AssertEquals ("#22e", "news://foo/bar", u.AbsoluteUri);\r
370 #else
371                         AssertEquals ("#22a", "foo", u.Host);\r
372                         AssertEquals ("#22b", UriHostNameType.Dns, u.HostNameType);\r
373                         AssertEquals ("#22c", "news://foo/bar", u.ToString ());\r
374                         AssertEquals ("#22d", "/bar", u.AbsolutePath);\r
375                         AssertEquals ("#22e", "news://foo/bar", u.AbsoluteUri);
376 #endif\r
377                 }\r
378 \r
379                 [Test]\r
380 #if ONLY_1_1
381                 [Category ("NotDotNet")] // does (but shouldn't) fail under 1.x
382 #endif\r
383                 public void LeadingSlashes_FailOn1x ()\r
384                 {\r
385                         // 1.x throws an UriFormatException because it can't decode the host name\r
386                         Uri u = new Uri ("mailto:");\r
387                         AssertEquals ("#10a", String.Empty, u.Host);\r
388                         AssertEquals ("#10b", UriHostNameType.Basic, u.HostNameType);\r
389                         AssertEquals ("#10c", "mailto:", u.ToString ());\r
390 \r
391                         // 1.x throws an UriFormatException because it can't decode the host name\r
392                         u = new Uri ("mailto:/");\r
393                         AssertEquals ("#12a", String.Empty, u.Host);\r
394                         AssertEquals ("#12b", UriHostNameType.Basic, u.HostNameType);\r
395                         AssertEquals ("#12c", "mailto:/", u.ToString ());\r
396 \r
397                         // 1.x throws an UriFormatException because it cannot detect the format\r
398                         u = new Uri ("mailto:///foo");\r
399                         AssertEquals ("#15a", String.Empty, u.Host);\r
400                         AssertEquals ("#15b", UriHostNameType.Basic, u.HostNameType);\r
401                         AssertEquals ("#15c", "mailto:///foo", u.ToString ());\r
402 \r
403                         // 1.x throws an UriFormatException because it cannot detect the format\r
404                         u = new Uri ("news:///foo");\r
405                         AssertEquals ("#21a", String.Empty, u.Host);\r
406 #if NET_2_0\r
407                         AssertEquals ("#21b", UriHostNameType.Unknown, u.HostNameType);\r
408 #else\r
409                         AssertEquals ("#21b", UriHostNameType.Basic, u.HostNameType);\r
410 #endif\r
411                         AssertEquals ("#21c", "news:///foo", u.ToString ());\r
412                         AssertEquals ("#21d", "///foo", u.AbsolutePath);\r
413                         AssertEquals ("#21e", "news:///foo", u.AbsoluteUri);\r
414                 }\r
415
416                 [Test]
417                 // some tests from bug 75144
418                 public void LeadingSlashes ()
419                 {
420                         Uri u = new Uri ("file://foo/bar");\r
421                         AssertEquals ("#5a", "foo", u.Host);\r
422                         AssertEquals ("#5b", UriHostNameType.Dns, u.HostNameType);\r
423                         AssertEquals ("#5c", "file://foo/bar", u.ToString ());
424                         AssertEquals ("#5d", true, u.IsUnc);
425
426                         u = new Uri ("file:////foo/bar");\r
427                         AssertEquals ("#7a", "foo", u.Host);\r
428                         AssertEquals ("#7b", UriHostNameType.Dns, u.HostNameType);\r
429                         AssertEquals ("#7c", "file://foo/bar", u.ToString ());
430                         AssertEquals ("#7d", true, u.IsUnc); 
431
432                         AssertEquals ("#9", "file://foo/bar", new Uri ("file://///foo/bar").ToString());
433 \r
434                         u = new Uri ("mailto:foo");\r
435                         AssertEquals ("#11a", "foo", u.Host);\r
436                         AssertEquals ("#11b", UriHostNameType.Dns, u.HostNameType);\r
437                         AssertEquals ("#11c", "mailto:foo", u.ToString ());\r
438 \r
439                         u = new Uri ("news:");
440                         AssertEquals ("#16a", String.Empty, u.Host);
441 #if NET_2_0\r
442                         AssertEquals ("#16b", UriHostNameType.Unknown, u.HostNameType);
443 #else
444                         AssertEquals ("#16b", UriHostNameType.Basic, u.HostNameType);
445 #endif\r
446                         AssertEquals ("#16c", "news:", u.ToString ());\r
447
448                         u = new Uri ("news:foo");\r
449                         AssertEquals ("#17a", String.Empty, u.Host);\r
450 #if NET_2_0\r
451                         AssertEquals ("#17b", UriHostNameType.Unknown, u.HostNameType);
452 #else
453                         AssertEquals ("#17b", UriHostNameType.Basic, u.HostNameType);
454 #endif\r
455                         AssertEquals ("#17c", "news:foo", u.ToString ());\r
456                         AssertEquals ("#17d", "foo", u.AbsolutePath);\r
457                         AssertEquals ("#17e", "news:foo", u.AbsoluteUri);\r
458                 }
459
460                 [Test]
461                 [ExpectedException (typeof (UriFormatException))]
462                 public void HttpHostname1 ()
463                 {
464                         new Uri ("http:");
465                 }
466
467                 [Test]
468                 [ExpectedException (typeof (UriFormatException))]
469 #if ONLY_1_1
470                 [Category ("NotDotNet")] // doesn't fail under 1.x
471 #endif
472                 public void HttpHostname2 ()
473                 {
474                         new Uri ("http:a");
475                 }
476                 
477                 [Test]
478                 [ExpectedException (typeof (UriFormatException))]
479                 public void HttpHostname3 ()
480                 {
481                         new Uri ("http:/");
482                 }
483
484                 [Test]
485                 [ExpectedException (typeof (UriFormatException))]
486 #if ONLY_1_1
487                 [Category ("NotDotNet")] // doesn't fail under 1.x
488 #endif
489                 public void HttpHostname4 ()
490                 {
491                         new Uri ("http:/foo");
492                 }
493
494                 [Test]
495                 [ExpectedException (typeof (UriFormatException))]
496                 public void HttpHostname5 ()
497                 {
498                         new Uri ("http://");
499                 }
500
501                 [Test]
502                 [ExpectedException (typeof (UriFormatException))]
503                 public void HttpHostname6 ()
504                 {
505                         new Uri ("http:///");
506                 }
507
508                 [Test]
509                 [ExpectedException (typeof (UriFormatException))]
510                 public void HttpHostname7 ()
511                 {
512                         new Uri ("http:///foo");
513                 }
514
515                 [Test]
516                 [ExpectedException (typeof (UriFormatException))]
517                 public void InvalidFile1 ()
518                 {
519                         new Uri ("file:");
520                 }
521
522                 [Test]
523                 [ExpectedException (typeof (UriFormatException))]
524                 public void InvalidFile2 ()
525                 {
526                         new Uri ("file:/");
527                 }
528
529                 [Test]
530                 [ExpectedException (typeof (UriFormatException))]
531                 public void InvalidFile3 ()
532                 {
533                         new Uri ("file:/foo");
534                 }       
535
536                 [Test]
537                 [ExpectedException (typeof (UriFormatException))]
538                 public void InvalidScheme ()
539                 {
540                         new Uri ("_:/");
541                 }
542
543                 [Test]
544                 public void ConstructorsRejectRelativePath ()
545                 {
546                         string [] reluris = new string [] {
547                                 "readme.txt",
548                                 "thisdir/childdir/file",
549                                 "./testfile"
550                         };
551                         string [] winRelUris = new string [] {
552                                 "c:readme.txt"
553                         };
554
555                         for (int i = 0; i < reluris.Length; i++) {
556                                 try {
557                                         new Uri (reluris [i]);
558                                         Fail ("Should be failed: " + reluris [i]);
559                                 } catch (UriFormatException) {
560                                 }
561                         }
562
563                         if (isWin32) {
564                                 for (int i = 0; i < winRelUris.Length; i++) {
565                                         try {
566                                                 new Uri (winRelUris [i]);
567                                                 Fail ("Should be failed: " + winRelUris [i]);
568                                         } catch (UriFormatException) {
569                                         }
570                                 }
571                         }
572                 }
573
574                 [Test]
575                 public void LocalPath ()
576                 {
577                         Uri uri = new Uri ("c:\\tmp\\hello.txt");
578                         AssertEquals ("#1a", "file:///c:/tmp/hello.txt", uri.ToString ());
579                         AssertEquals ("#1b", "c:\\tmp\\hello.txt", uri.LocalPath);
580                         AssertEquals ("#1c", "file", uri.Scheme);
581                         AssertEquals ("#1d", "", uri.Host);
582                         AssertEquals ("#1e", "c:/tmp/hello.txt", uri.AbsolutePath);
583                                         
584                         uri = new Uri ("file:////////cygwin/tmp/hello.txt");
585                         AssertEquals ("#3a", "file://cygwin/tmp/hello.txt", uri.ToString ());
586                         if (isWin32)
587                                 AssertEquals ("#3b win32", "\\\\cygwin\\tmp\\hello.txt", uri.LocalPath);
588                         else
589                                 AssertEquals ("#3b *nix", "/tmp/hello.txt", uri.LocalPath);
590                         AssertEquals ("#3c", "file", uri.Scheme);
591                         AssertEquals ("#3d", "cygwin", uri.Host);
592                         AssertEquals ("#3e", "/tmp/hello.txt", uri.AbsolutePath);
593
594                         uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
595                         AssertEquals ("#4a", "file://mymachine/cygwin/tmp/hello.txt", uri.ToString ());
596                         if (isWin32)
597                                 AssertEquals ("#4b win32", "\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath);
598                         else
599                                 AssertEquals ("#4b *nix", "/cygwin/tmp/hello.txt", uri.LocalPath);
600                         AssertEquals ("#4c", "file", uri.Scheme);
601                         AssertEquals ("#4d", "mymachine", uri.Host);
602                         AssertEquals ("#4e", "/cygwin/tmp/hello.txt", uri.AbsolutePath);
603                         
604                         uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
605                         AssertEquals ("#5a", "file:///c:/cygwin/tmp/hello.txt", uri.ToString ());
606                         AssertEquals ("#5b", "c:\\cygwin\\tmp\\hello.txt", uri.LocalPath);
607                         AssertEquals ("#5c", "file", uri.Scheme);
608                         AssertEquals ("#5d", "", uri.Host);
609                         AssertEquals ("#5e", "c:/cygwin/tmp/hello.txt", uri.AbsolutePath);
610                 }
611
612                 [Test]
613                 public void LocalPath_FileHost ()
614                 {
615                         // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
616                         Uri uri = new Uri("file://one_file.txt");
617 #if NET_2_0
618                         AssertEquals("#6a", "file://one_file.txt/", uri.ToString());\r
619                         AssertEquals ("#6e", "/", uri.AbsolutePath);\r
620                         AssertEquals ("#6f", "/", uri.PathAndQuery);\r
621                         AssertEquals ("#6g", "file://one_file.txt/", uri.GetLeftPart (UriPartial.Path));\r
622 #else
623                         AssertEquals("#6a", "file://one_file.txt", uri.ToString());
624                         AssertEquals("#6e", "", uri.AbsolutePath);
625                         AssertEquals ("#6f", "", uri.PathAndQuery);\r
626                         AssertEquals ("#6g", "file://one_file.txt", uri.GetLeftPart (UriPartial.Path));\r
627 #endif\r
628                         if (isWin32)
629                                 AssertEquals("#6b", "\\\\one_file.txt", uri.LocalPath);
630                         else
631                                 AssertEquals("#6b", "/", uri.LocalPath);
632                         AssertEquals("#6c", "file", uri.Scheme);
633                         AssertEquals("#6d", "one_file.txt", uri.Host);\r
634 \r
635                         // same tests - but original Uri is now ending with a '/'\r
636 \r
637                         uri = new Uri ("file://one_file.txt/");\r
638                         AssertEquals ("#7a", "file://one_file.txt/", uri.ToString ());\r
639                         AssertEquals ("#7e", "/", uri.AbsolutePath);\r
640                         AssertEquals ("#7f", "/", uri.PathAndQuery);\r
641                         AssertEquals ("#7g", "file://one_file.txt/", uri.GetLeftPart (UriPartial.Path));\r
642                         if (isWin32)\r
643                                 AssertEquals ("#7b", "\\\\one_file.txt\\", uri.LocalPath);\r
644                         else\r
645                                 AssertEquals ("#7b", "/", uri.LocalPath);\r
646                         AssertEquals ("#7c", "file", uri.Scheme);\r
647                         AssertEquals ("#7d", "one_file.txt", uri.Host);\r
648                 }
649
650                 [Test]
651                 public void LocalPath_Escape ()
652                 {
653                         // escape
654                         Uri uri = new Uri ("file:///tmp/a%20a");\r
655                         if (isWin32) {\r
656 #if NET_2_0\r
657                                 Assert ("#7a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/tmp/a a"));\r
658 #else\r
659                                 // actually MS.NET treats /// as \\ thus it fails here.\r
660                                 Assert ("#7a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\tmp\\a a"));\r
661 #endif\r
662                         } else\r
663                                 AssertEquals ("#7b", "/tmp/a a", uri.LocalPath);
664
665                         uri = new Uri ("file:///tmp/foo%25bar");
666                         if (isWin32) {
667 #if NET_2_0\r
668                                 Assert ("#8a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/tmp/foo%bar"));\r
669                                 Assert ("#8c:" + uri.ToString (), uri.ToString ().EndsWith ("//tmp/foo%25bar"));\r
670 #else
671                                 // actually MS.NET treats /// as \\ thus it fails here.
672                                 Assert ("#8a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\tmp\\foo%bar"));
673                                 // ditto, file://tmp/foo%25bar (bug in 1.x)\r
674                                 Assert ("#8c:" + uri.ToString (), uri.ToString ().EndsWith ("//tmp/foo%bar"));\r
675 #endif\r
676                         } else {
677                                 AssertEquals ("#8b", "/tmp/foo%bar", uri.LocalPath);
678                                 AssertEquals ("#8d", "file:///tmp/foo%25bar", uri.ToString ());
679                         }
680                         // bug #76643
681                         uri = new Uri ("file:///foo%25bar");
682                         if (isWin32) {
683 #if NET_2_0\r
684                                 Assert ("#9a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/foo%bar"));\r
685 #else
686                                 // actually MS.NET treats /// as \\ thus it fails here.
687                                 Assert ("#9a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\foo%25bar"));
688 #endif\r
689                                 // ditto, file://tmp/foo%25bar (bug in 1.x)\r
690                                 Assert ("#9c:" + uri.ToString (), uri.ToString ().EndsWith ("//foo%25bar"));\r
691                         } else {
692                                 AssertEquals ("#9b", "/foo%bar", uri.LocalPath);
693                                 AssertEquals ("#9d", "file:///foo%25bar", uri.ToString ());
694                         }
695                 }
696                 
697                 [Test]
698                 public void UnixPath () {
699                         if (!isWin32)
700                                 AssertEquals ("#6a", "file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString ());
701                 }
702                 
703                 [Test]
704                 public void Unc ()
705                 {
706                         Uri uri = new Uri ("http://www.contoso.com");
707                         Assert ("#1", !uri.IsUnc);
708                         
709                         uri = new Uri ("news:123456@contoso.com");
710                         Assert ("#2", !uri.IsUnc);
711
712                         uri = new Uri ("file://server/filename.ext");
713                         Assert ("#3", uri.IsUnc);
714
715                         uri = new Uri (@"\\server\share\filename.ext");                 
716                         Assert ("#6", uri.IsUnc);
717
718                         uri = new Uri (@"a:\dir\filename.ext");
719                         Assert ("#8", !uri.IsUnc);
720                 }
721
722                 [Test]
723                 [Category("NotDotNet")]
724                 public void UncFail ()
725                 {
726                         Uri uri = new Uri ("/home/user/dir/filename.ext");
727                         Assert ("#7", !uri.IsUnc);
728                 }
729
730                 [Test]
731                 public void FromHex () 
732                 {
733                         AssertEquals ("#1", 0, Uri.FromHex ('0'));
734                         AssertEquals ("#2", 9, Uri.FromHex ('9'));
735                         AssertEquals ("#3", 10, Uri.FromHex ('a'));
736                         AssertEquals ("#4", 15, Uri.FromHex ('f'));
737                         AssertEquals ("#5", 10, Uri.FromHex ('A'));
738                         AssertEquals ("#6", 15, Uri.FromHex ('F'));
739                         try {
740                                 Uri.FromHex ('G');
741                                 Fail ("#7");
742                         } catch (ArgumentException) {}
743                         try {
744                                 Uri.FromHex (' ');
745                                 Fail ("#8");
746                         } catch (ArgumentException) {}
747                         try {
748                                 Uri.FromHex ('%');
749                                 Fail ("#8");
750                         } catch (ArgumentException) {}
751                 }
752
753                 class UriEx : Uri
754                 {
755                         public UriEx (string s) : base (s)
756                         {
757                         }
758
759                         public string UnescapeString (string s)
760                         {
761                                 return Unescape (s);
762                         }
763
764                         public static string UnescapeString (string uri, string target)
765                         {
766                                 return new UriEx (uri).UnescapeString (target);
767                         }
768                 }
769
770                 [Test]
771                 public void Unescape ()
772                 {
773                         AssertEquals ("#1", "#", UriEx.UnescapeString ("file://localhost/c#", "%23"));
774                         AssertEquals ("#2", "c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"));
775                         AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"));
776                         AssertEquals ("#1", "#", UriEx.UnescapeString ("http://localhost/c#", "%23"));
777                         AssertEquals ("#2", "c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"));
778                         AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"));
779                 }
780
781                 [Test]
782                 public void HexEscape () 
783                 {
784                         AssertEquals ("#1","%20", Uri.HexEscape (' ')); 
785                         AssertEquals ("#2","%A9", Uri.HexEscape ((char) 0xa9)); 
786                         AssertEquals ("#3","%41", Uri.HexEscape ('A')); 
787                         try {
788                                 Uri.HexEscape ((char) 0x0369);
789                                 Fail ("#4");
790                         } catch (ArgumentOutOfRangeException) {}
791                 }
792
793                 [Test]
794                 public void HexUnescape () 
795                 {
796                         int i = 0;
797                         AssertEquals ("#1", ' ', Uri.HexUnescape ("%20", ref i));
798                         AssertEquals ("#2", 3, i);
799                         i = 4;
800                         AssertEquals ("#3", (char) 0xa9, Uri.HexUnescape ("test%a9test", ref i));
801                         AssertEquals ("#4", 7, i);
802                         AssertEquals ("#5", 't', Uri.HexUnescape ("test%a9test", ref i));
803                         AssertEquals ("#6", 8, i);
804                         i = 4;
805                         AssertEquals ("#5", '%', Uri.HexUnescape ("test%a", ref i));
806                         AssertEquals ("#6", 5, i);
807                         AssertEquals ("#7", '%', Uri.HexUnescape ("testx%xx", ref i));
808                         AssertEquals ("#8", 6, i);
809
810                         // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
811                         i = 1;
812                         AssertEquals ("#9", 227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i));
813                         AssertEquals ("#10", 4, i);
814                         i = 1;
815                         AssertEquals ("#11", 240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i));
816                         AssertEquals ("#12", 4, i);
817                 }
818
819 #if !NET_2_0
820                 // These won't pass exactly with MS.NET 1.x, due to differences in the
821                 // handling of backslashes/forwardslashes
822                 [Category ("NotDotNet")]
823 #endif
824                 [Test]
825                 public void HexUnescapeMultiByte () 
826                 {
827                         // Tests from bug 74872
828                         // Note: These won't pass exactly with MS.NET, due to differences in the
829                         // handling of backslashes/forwardslashes
830                         Uri uri;
831                         string path;
832
833                         // 3-byte character
834                         uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
835                         path = uri.LocalPath;
836                         AssertEquals ("#1", 8, path.Length);
837                         AssertEquals ("#2", 0x304B, path [6]);
838
839                         // 4-byte character which should be handled as a surrogate
840                         uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
841                         path = uri.LocalPath;
842                         AssertEquals ("#3", 9, path.Length);
843                         AssertEquals ("#4", 0xDB40, path [6]);
844                         AssertEquals ("#5", 0xDD00, path [7]);
845                         AssertEquals ("#6", 0x62, path [8]);
846                         
847                         // 2-byte escape sequence, 2 individual characters
848                         uri = new Uri ("file:///foo/a%C2%F8b", true);
849                         path = uri.LocalPath;
850                         AssertEquals ("#7", 9, path.Length);
851                         AssertEquals ("#8", 0xC2, path [6]);
852                         AssertEquals ("#9", 0xF8, path [7]);                    
853                 }
854
855                 [Test]
856                 public void IsHexDigit () 
857                 {
858                         Assert ("#1", Uri.IsHexDigit ('a'));    
859                         Assert ("#2", Uri.IsHexDigit ('f'));
860                         Assert ("#3", !Uri.IsHexDigit ('g'));
861                         Assert ("#4", Uri.IsHexDigit ('0'));
862                         Assert ("#5", Uri.IsHexDigit ('9'));
863                         Assert ("#6", Uri.IsHexDigit ('A'));
864                         Assert ("#7", Uri.IsHexDigit ('F'));
865                         Assert ("#8", !Uri.IsHexDigit ('G'));
866                 }
867
868                 [Test]
869                 public void IsHexEncoding () 
870                 {
871                         Assert ("#1", Uri.IsHexEncoding ("test%a9test", 4));
872                         Assert ("#2", !Uri.IsHexEncoding ("test%a9test", 3));
873                         Assert ("#3", Uri.IsHexEncoding ("test%a9", 4));
874                         Assert ("#4", !Uri.IsHexEncoding ("test%a", 4));
875                 }
876                 
877                 [Test]
878                 public void GetLeftPart ()
879                 {
880                         Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
881                         AssertEquals ("#1", "http://", uri.GetLeftPart (UriPartial.Scheme));
882                         AssertEquals ("#2", "http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority));
883                         AssertEquals ("#3", "http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path));
884                         
885                         uri = new Uri ("mailto:user@contoso.com?subject=uri");
886                         AssertEquals ("#4", "mailto:", uri.GetLeftPart (UriPartial.Scheme));
887                         AssertEquals ("#5", "", uri.GetLeftPart (UriPartial.Authority));
888                         AssertEquals ("#6", "mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path));
889
890                         uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
891                         AssertEquals ("#7", "nntp://", uri.GetLeftPart (UriPartial.Scheme));
892                         AssertEquals ("#8", "nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority));
893                         AssertEquals ("#9", "nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path));                   
894                         
895                         uri = new Uri ("news:123456@contoso.com");
896                         AssertEquals ("#10", "news:", uri.GetLeftPart (UriPartial.Scheme));
897                         AssertEquals ("#11", "", uri.GetLeftPart (UriPartial.Authority));
898                         AssertEquals ("#12", "news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path));                     
899
900                         uri = new Uri ("file://server/filename.ext");
901                         AssertEquals ("#13", "file://", uri.GetLeftPart (UriPartial.Scheme));
902                         AssertEquals ("#14", "file://server", uri.GetLeftPart (UriPartial.Authority));
903                         AssertEquals ("#15", "file://server/filename.ext", uri.GetLeftPart (UriPartial.Path));                  
904
905                         uri = new Uri (@"\\server\share\filename.ext");
906                         AssertEquals ("#20", "file://", uri.GetLeftPart (UriPartial.Scheme));
907                         AssertEquals ("#21", "file://server", uri.GetLeftPart (UriPartial.Authority));
908                         AssertEquals ("#22", "file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path));
909                         
910                         uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
911                         AssertEquals ("#23", "http://", uri.GetLeftPart (UriPartial.Scheme));
912                         AssertEquals ("#24", "http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority));
913                         AssertEquals ("#25", "http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path));
914                 }
915
916                 [Test]
917                 public void NewsDefaultPort ()
918                 {
919                         Uri uri = new Uri("news://localhost:119/");
920                         AssertEquals ("#1", uri.IsDefaultPort, true);
921                 }
922
923                 [Test]
924                 public void FragmentEscape ()
925                 {
926                         Uri u = new Uri("http://localhost/index.asp#main#start", false);
927                         AssertEquals ("#1", u.Fragment, "#main%23start");
928
929                         u = new Uri("http://localhost/index.asp#main#start", true);
930                         AssertEquals ("#2", u.Fragment, "#main#start");
931
932                         // The other code path uses a BaseUri
933
934                         Uri b = new Uri ("http://www.gnome.org");
935                         Uri n = new Uri (b, "blah#main#start");
936                         AssertEquals ("#3", n.Fragment, "#main%23start");
937                         
938                         n = new Uri (b, "blah#main#start", true);
939                         AssertEquals ("#4", n.Fragment, "#main#start");
940                 }
941                         
942                 [Test]
943                 [ExpectedException(typeof(UriFormatException))]
944                 public void IncompleteSchemeDelimiter ()
945                 {
946                         new Uri ("file:/filename.ext");
947                 }
948
949                 [Test]
950                 [Category("NotDotNet")]
951                 public void CheckHostName1 ()
952                 {
953                         // reported to MSDN Product Feedback Center (FDBK28671)
954                         AssertEquals ("#36 known to fail with ms.net: this is not a valid IPv6 address.", UriHostNameType.Unknown, Uri.CheckHostName (":11:22:33:44:55:66:77:88"));
955                 }
956
957                 [Test]
958                 public void CheckHostName2 ()
959                 {
960                         AssertEquals ("#1", UriHostNameType.Unknown, Uri.CheckHostName (null));
961                         AssertEquals ("#2", UriHostNameType.Unknown, Uri.CheckHostName (""));
962                         AssertEquals ("#3", UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"));
963                         AssertEquals ("#4", UriHostNameType.Dns, Uri.CheckHostName ("x"));
964                         AssertEquals ("#5", UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"));
965                         AssertEquals ("#6", UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"));
966                         AssertEquals ("#7", UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"));
967                         AssertEquals ("#8", UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"));
968                         AssertEquals ("#9", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"));
969                         AssertEquals ("#10", UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"));
970                         AssertEquals ("#11", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."));
971                         AssertEquals ("#12", UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"));      
972                         AssertEquals ("#13", UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"));      
973                         AssertEquals ("#14", UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"));  
974                         
975                         // test IPv6
976                         AssertEquals ("#15", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"));
977                         AssertEquals ("#16", UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"));
978                         AssertEquals ("#17", UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"));
979                         AssertEquals ("#18", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"));
980                         AssertEquals ("#19", UriHostNameType.IPv6, Uri.CheckHostName ("11::88"));
981                         AssertEquals ("#20", UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"));
982                         AssertEquals ("#21", UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"));
983                         AssertEquals ("#22", UriHostNameType.IPv6, Uri.CheckHostName ("11::"));
984                         AssertEquals ("#23", UriHostNameType.IPv6, Uri.CheckHostName ("::88"));
985                         AssertEquals ("#24", UriHostNameType.IPv6, Uri.CheckHostName ("::1"));
986                         AssertEquals ("#25", UriHostNameType.IPv6, Uri.CheckHostName ("::"));
987                         AssertEquals ("#26", UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"));
988                         AssertEquals ("#27", UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"));
989                         AssertEquals ("#28", UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"));
990                         AssertEquals ("#29", UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"));
991                         AssertEquals ("#30", UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"));
992                         AssertEquals ("#33", UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"));
993
994                         AssertEquals ("#31", UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"));
995                         AssertEquals ("#32", UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"));
996                         AssertEquals ("#34", UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"));
997                         AssertEquals ("#35", UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"));
998                         AssertEquals ("#37", UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"));
999                         AssertEquals ("#38", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"));
1000                         AssertEquals ("#39", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"));
1001                         AssertEquals ("#40", UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"));
1002                         AssertEquals ("#41", UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"));
1003                         AssertEquals ("#42", UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"));
1004                         AssertEquals ("#43", UriHostNameType.Unknown, Uri.CheckHostName (":::"));
1005                         AssertEquals ("#44", UriHostNameType.Unknown, Uri.CheckHostName (":"));\r
1006 \r
1007                         AssertEquals ("#45", UriHostNameType.Unknown, Uri.CheckHostName ("*"));\r
1008                         AssertEquals ("#46", UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"));\r
1009                         AssertEquals ("#47", UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"));\r
1010                 }
1011                 
1012                 [Test]
1013                 public void IsLoopback ()
1014                 {
1015                         Uri uri = new Uri("http://loopback:8080");
1016                         AssertEquals ("#1", true, uri.IsLoopback);
1017                         uri = new Uri("http://localhost:8080");
1018                         AssertEquals ("#2", true, uri.IsLoopback);
1019                         uri = new Uri("http://127.0.0.1:8080");
1020                         AssertEquals ("#3", true, uri.IsLoopback);
1021                         uri = new Uri("http://127.0.0.001:8080");
1022                         AssertEquals ("#4", true, uri.IsLoopback);
1023                         uri = new Uri("http://[::1]");
1024                         AssertEquals ("#5", true, uri.IsLoopback);
1025                         uri = new Uri("http://[::1]:8080");
1026                         AssertEquals ("#6", true, uri.IsLoopback);
1027                         uri = new Uri("http://[::0001]:8080");
1028                         AssertEquals ("#7", true, uri.IsLoopback);
1029                         uri = new Uri("http://[0:0:0:0::1]:8080");
1030                         AssertEquals ("#8", true, uri.IsLoopback);
1031                         uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
1032                         AssertEquals ("#9", true, uri.IsLoopback);
1033                         uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
1034                         AssertEquals ("#10", false, uri.IsLoopback);
1035                         uri = new Uri("http://[::ffff:127.11.22.33]:8080");
1036                         AssertEquals ("#11", false, uri.IsLoopback);
1037                         uri = new Uri("http://[::ff00:7f11:2233]:8080");
1038                         AssertEquals ("#12", false, uri.IsLoopback);
1039                         uri = new Uri("http://[1:0:0:0::1]:8080");
1040                         AssertEquals ("#13", false, uri.IsLoopback);\r
1041                 }\r
1042 \r
1043                 [Test]\r
1044                 public void IsLoopback_File ()\r
1045                 {\r
1046                         Uri uri = new Uri ("file:///index.html");
1047 #if NET_2_0\r
1048                         Assert ("file", uri.IsLoopback);
1049 #else
1050                         Assert ("file", !uri.IsLoopback);
1051 #endif\r
1052                 }\r
1053 \r
1054                 [Test]\r
1055                 public void IsLoopback_Relative_Http ()\r
1056                 {\r
1057                         string relative = "http:8080/bar/Hello World.htm";\r
1058                         Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);\r
1059                         Assert ("http", !uri.IsLoopback);\r
1060                 }\r
1061 \r
1062                 [Test]\r
1063                 public void IsLoopback_Relative_Unknown ()\r
1064                 {\r
1065                         string relative = "foo:8080/bar/Hello World.htm";\r
1066                         Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);\r
1067                         Assert ("foo", !uri.IsLoopback);\r
1068                 }
1069
1070                 [Test]
1071                 public void Equals1 ()
1072                 {
1073                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1074                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1075                         Assert ("#1", uri1.Equals (uri2));
1076                         Assert ("#3", !uri2.Equals ("http://www.contoso.com/index.html?x=1"));
1077                         Assert ("#4", !uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"));
1078                 }
1079
1080                 [Test]
1081 #if !NET_2_0
1082                 [Category("NotDotNet")]
1083 #endif
1084                 public void Equals2 ()
1085                 {
1086                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1087                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1088                         Assert ("#2 known to fail with ms.net 1.x", !uri1.Equals (uri2));
1089                 }
1090
1091                 [Test]
1092                 public void Equals3 ()
1093                 {
1094                         Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1095                         Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1096                         Assert (uri1.Equals (uri2));
1097                 }
1098
1099                 [Test]
1100                 public void TestEquals2 ()
1101                 {
1102                         Uri a = new Uri ("http://www.go-mono.com");
1103                         Uri b = new Uri ("http://www.go-mono.com");
1104
1105                         AssertEquals ("#1", a, b);
1106
1107                         a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1108                         b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1109 #if NET_2_0\r
1110                         Assert ("#2", a != b);\r
1111                         AssertEquals ("#2a", "mailto:user:pwd@go-mono.com?subject=uri", a.ToString ());\r
1112                         AssertEquals ("#2b", "mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString ());
1113 #else
1114                         AssertEquals ("#2", a, b);
1115 #endif
1116                         a = new Uri ("http://www.go-mono.com/ports/");
1117                         b = new Uri ("http://www.go-mono.com/PORTS/");
1118
1119                         Assert ("#3", !a.Equals (b));
1120                 }\r
1121 \r
1122                 [Test]\r
1123                 public void CaseSensitivity ()\r
1124                 {\r
1125                         Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");\r
1126                         AssertEquals ("#1", "mailto", mailto.Scheme);\r
1127                         AssertEquals ("#2", "go-mono.com", mailto.Host);\r
1128                         AssertEquals ("#3", "mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString ());\r
1129 \r
1130                         Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");\r
1131                         AssertEquals ("#4", "http", http.Scheme);\r
1132                         AssertEquals ("#5", "go-mono.com", http.Host);\r
1133                         AssertEquals ("#6", "http://go-mono.com/INDEX.HTML", http.ToString ());\r
1134 \r
1135                         // IPv6 Address\r
1136                         Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");\r
1137                         AssertEquals ("#7", "ftp", ftp.Scheme);\r
1138                         AssertEquals ("#8", "[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host);\r
1139                         AssertEquals ("#9", "ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString ());\r
1140                 }
1141
1142                 [Test]
1143                 public void GetHashCodeTest () 
1144                 {
1145                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1146                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1147                         AssertEquals ("#1", uri1.GetHashCode (), uri2.GetHashCode ());
1148                         uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1149                         Assert ("#2", uri1.GetHashCode () != uri2.GetHashCode ());
1150                         uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1151                         AssertEquals ("#3", uri1.GetHashCode (), uri2.GetHashCode ());                  
1152                         uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1153                         Assert ("#4", uri1.GetHashCode () != uri2.GetHashCode ());                      
1154                 }
1155                 
1156                 [Test]
1157                 public void MakeRelative ()
1158                 {
1159                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1160                         Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1161                         Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1162                         Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1163                         Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1164                         Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1165                         Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1166                         Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1167
1168                         AssertEquals ("#1", "foo/bar/index.htm", uri1.MakeRelative (uri2));
1169                         AssertEquals ("#2", "../../index.htm", uri2.MakeRelative (uri1));
1170                         
1171                         AssertEquals ("#3", "../../bar/foo/index.htm", uri2.MakeRelative (uri3));
1172                         AssertEquals ("#4", "../../foo/bar/index.htm", uri3.MakeRelative (uri2));                       
1173
1174                         AssertEquals ("#5", "../foo2/index.htm", uri3.MakeRelative (uri4));
1175                         AssertEquals ("#6", "../foo/index.htm", uri4.MakeRelative (uri3));
1176                         
1177                         AssertEquals ("#7", "https://www.contoso.com/bar/foo/index.htm?y=1", 
1178                                             uri4.MakeRelative (uri5));
1179
1180                         AssertEquals ("#8", "http://www.contoso2.com/bar/foo/index.htm?x=0", 
1181                                             uri4.MakeRelative (uri6));
1182
1183                         AssertEquals ("#9", "", uri6.MakeRelative (uri6));
1184                         AssertEquals ("#10", "foobar.htm", uri6.MakeRelative (uri7));
1185                         
1186                         Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1187                         Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1188                         AssertEquals ("#11", "", uri10.MakeRelative (uri11));
1189                         
1190                         Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1191                         AssertEquals ("#12", "mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12));
1192                                                 
1193                         Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1194                         AssertEquals ("#13", "/foo/bar", uri10.MakeRelative (uri13));
1195                         
1196                         AssertEquals ("#14", "http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8));
1197                 }
1198
1199                 [Test]
1200                 public void RelativeUri ()
1201                 {
1202                         Uri u = new Uri("http://localhost/../../../a");
1203 #if NET_2_0
1204                         AssertEquals ("http://localhost/a", u.ToString ());
1205 #else
1206                         AssertEquals ("http://localhost/../../../a", u.ToString ());
1207 #endif
1208
1209                         u = new Uri ("http://localhost/../c/b/../a");
1210 #if NET_2_0
1211                         AssertEquals ("http://localhost/c/a", u.ToString ());
1212 #else
1213                         AssertEquals ("http://localhost/../c/a", u.ToString ());
1214 #endif
1215                 }
1216
1217                 [Test]
1218                 public void ToStringTest()
1219                 {                       
1220                         Uri uri = new Uri ("dummy://xxx");
1221                         AssertEquals ("#1", "dummy://xxx/", uri.ToString ());
1222                 }
1223
1224                 [Test]
1225                 public void CheckSchemeName ()
1226                 {
1227                         AssertEquals ("#01", false, Uri.CheckSchemeName (null));
1228                         AssertEquals ("#02", false, Uri.CheckSchemeName (""));
1229                         AssertEquals ("#03", true, Uri.CheckSchemeName ("http"));
1230                         AssertEquals ("#04", true, Uri.CheckSchemeName ("http-"));
1231                         AssertEquals ("#05", false, Uri.CheckSchemeName ("6http-"));
1232                         AssertEquals ("#06", true, Uri.CheckSchemeName ("http6-"));
1233                         AssertEquals ("#07", false, Uri.CheckSchemeName ("http6,"));
1234                         AssertEquals ("#08", true, Uri.CheckSchemeName ("http6."));
1235                         AssertEquals ("#09", false, Uri.CheckSchemeName ("+http"));
1236                         AssertEquals ("#10", true, Uri.CheckSchemeName ("htt+p6"));\r
1237                         // 0x00E1 -> &atilde;\r
1238 #if NET_2_0
1239                         Assert ("#11", !Uri.CheckSchemeName ("htt\u00E1+p6"));
1240 #else
1241                         Assert ("#11", Uri.CheckSchemeName ("htt\u00E1+p6"));
1242 #endif\r
1243                 }\r
1244 \r
1245                 [Test]\r
1246                 public void CheckSchemeName_FirstChar ()\r
1247                 {\r
1248                         for (int i = 0; i < 256; i++) {\r
1249                                 string s = String.Format ("#{0}", i);\r
1250                                 char c = (char) i;\r
1251                                 bool b = Uri.CheckSchemeName (c.ToString ());\r
1252 #if NET_2_0\r
1253                                 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));\r
1254                                 AssertEquals (s, valid, b);\r
1255 #else\r
1256                                 AssertEquals (s, Char.IsLetter (c), b);\r
1257 #endif\r
1258                         }\r
1259                 }\r
1260 \r
1261                 [Test]\r
1262                 public void CheckSchemeName_AnyOtherChar ()\r
1263                 {\r
1264                         for (int i = 0; i < 256; i++) {\r
1265                                 string s = String.Format ("#{0}", i);\r
1266                                 char c = (char) i;\r
1267                                 string scheme = String.Format ("a+b-c.d{0}", c);\r
1268                                 bool b = Uri.CheckSchemeName (scheme);\r
1269                                 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');\r
1270 #if NET_2_0\r
1271                                 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));\r
1272                                 AssertEquals (s, valid, b);\r
1273 #else\r
1274                                 AssertEquals (s, (Char.IsLetter (c) || common), b);\r
1275 #endif\r
1276                         }\r
1277                 }\r
1278 \r
1279                 [Test]\r
1280                 [ExpectedException (typeof (UriFormatException))]\r
1281                 public void NoHostname ()
1282                 {
1283                         Uri uri = new Uri ("http://");
1284                 }
1285
1286                 [Test]
1287 #if !NET_2_0
1288                 // MS.NET 1.x throws an IndexOutOfRangeException
1289                 [Category("NotDotNet")]
1290 #endif
1291                 public void NoHostname2 ()
1292                 {
1293                         // bug 75144
1294                         Uri uri = new Uri ("file://");
1295                         AssertEquals ("#1", true, uri.IsFile);
1296                         AssertEquals ("#2", false, uri.IsUnc);
1297                         AssertEquals ("#3", "file", uri.Scheme);
1298                         AssertEquals ("#4", "/", uri.LocalPath);
1299                         AssertEquals ("#5", string.Empty, uri.Query);
1300                         AssertEquals ("#6", "/", uri.AbsolutePath);
1301                         AssertEquals ("#7", "file:///", uri.AbsoluteUri);
1302                         AssertEquals ("#8", string.Empty, uri.Authority);
1303                         AssertEquals ("#9", string.Empty, uri.Host);
1304                         AssertEquals ("#10", UriHostNameType.Basic, uri.HostNameType);
1305                         AssertEquals ("#11", string.Empty, uri.Fragment);
1306                         AssertEquals ("#12", true, uri.IsDefaultPort);
1307 #if NET_2_0
1308                         Assert ("#13", uri.IsLoopback);
1309 #else
1310                         Assert ("#13", !uri.IsLoopback);
1311 #endif
1312                         AssertEquals ("#14", "/", uri.PathAndQuery);
1313                         AssertEquals ("#15", false, uri.UserEscaped);
1314                         AssertEquals ("#16", string.Empty, uri.UserInfo);
1315                         AssertEquals ("#17", "file://", uri.GetLeftPart (UriPartial.Authority));
1316                         AssertEquals ("#18", "file:///", uri.GetLeftPart (UriPartial.Path));
1317                         AssertEquals ("#19", "file://", uri.GetLeftPart (UriPartial.Scheme));
1318                 }
1319
1320                 [Test]
1321                 public void Segments1 ()
1322                 {
1323                         Uri uri = new Uri ("http://localhost/");
1324                         string [] segments = uri.Segments;
1325                         AssertEquals ("#01", 1, segments.Length);
1326                         AssertEquals ("#02", "/", segments [0]);
1327                 }
1328
1329                 [Test]
1330                 public void Segments2 ()
1331                 {
1332                         Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1333                         string [] segments = uri.Segments;
1334                         AssertEquals ("#01", 3, segments.Length);
1335                         AssertEquals ("#02", "/", segments [0]);
1336                         AssertEquals ("#03", "dir/", segments [1]);
1337                         AssertEquals ("#04", "dummypage.html", segments [2]);
1338                 }
1339
1340                 [Test]
1341                 public void Segments3 ()
1342                 {
1343                         Uri uri = new Uri ("http://localhost/dir/dummypage/");
1344                         string [] segments = uri.Segments;
1345                         AssertEquals ("#01", 3, segments.Length);
1346                         AssertEquals ("#02", "/", segments [0]);
1347                         AssertEquals ("#03", "dir/", segments [1]);
1348                         AssertEquals ("#04", "dummypage/", segments [2]);
1349                 }
1350
1351                 [Test]
1352 #if NET_2_0
1353                 [Category ("NotWorking")]
1354 #endif
1355                 public void Segments4 ()
1356                 {
1357                         Uri uri = new Uri ("file:///c:/hello");
1358                         string [] segments = uri.Segments;
1359                         AssertEquals ("#01", 3, segments.Length);
1360 #if NET_2_0
1361                         AssertEquals ("#02", "/", segments [0]);\r
1362                         AssertEquals ("#03", "c:/", segments[1]);\r
1363 #else
1364                         AssertEquals ("#02", "c:", segments [0]);
1365                         AssertEquals ("#03", "/", segments [1]);
1366 #endif\r
1367                         AssertEquals ("#04", "hello", segments [2]);
1368                 }
1369
1370                 [Test]
1371                 public void Segments5 ()
1372                 {
1373                         Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1374                         string [] segments = uri.Segments;
1375                         AssertEquals ("#01", 4, segments.Length);
1376                         AssertEquals ("#02", "/", segments [0]);
1377                         AssertEquals ("#03", "bar/", segments [1]);
1378                         AssertEquals ("#04", "foo/", segments [2]);
1379                         AssertEquals ("#05", "foobar.htm", segments [3]);
1380                 }
1381
1382                 [Test]
1383                 [ExpectedException (typeof (UriFormatException))]
1384                 public void UriStartingWithColon()
1385                 {
1386                         new Uri("://");
1387                 }
1388
1389                 [Test]
1390                 [ExpectedException (typeof (UriFormatException))]
1391                 public void EmptyScheme ()
1392                 {
1393                         new Uri ("hey");
1394                 }
1395
1396 #if NET_2_0
1397                 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1398                 [ExpectedException (typeof (UriFormatException))]
1399 #endif
1400                 [Test]
1401                 public void InvalidPort1 ()
1402                 {
1403                         Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1404                         AssertEquals (65536, uri.Port);
1405                 }
1406
1407 #if NET_2_0
1408                 [ExpectedException (typeof (UriFormatException))]
1409 #endif
1410                 [Test]
1411                 public void InvalidPort2 ()
1412                 {
1413                         // UInt32.MaxValue gives port == -1 !!!
1414                         Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1415                         AssertEquals (-1, uri.Port);
1416                 }
1417
1418 #if NET_2_0
1419                 [ExpectedException (typeof (UriFormatException))]
1420 #endif
1421                 [Test]
1422                 public void InvalidPort3 ()
1423                 {
1424                         // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1425                         Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1426                         AssertEquals (-2147483648, uri.Port);
1427                 }
1428
1429 #if NET_2_0
1430                 [Test]
1431                 public void PortMax ()
1432                 {
1433                         // on .NET 2.0 a port number is limited to UInt16.MaxValue
1434                         Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1435                         AssertEquals (65535, uri.Port);
1436                 }
1437 #endif
1438
1439                 class UriEx2 : Uri
1440                 {
1441                         public UriEx2 (string s) : base (s)
1442                         {
1443                         }
1444
1445                         protected override void Parse ()
1446                         {
1447                         }
1448                 }
1449
1450 #if NET_2_0
1451                 // Parse method is no longer used on .NET 2.0
1452                 [ExpectedException (typeof (UriFormatException))]
1453 #endif
1454                 [Test]
1455                 public void ParseOverride ()
1456                 {
1457                         // If this does not override base's Parse(), it will
1458                         // fail since this argument is not Absolute URI.
1459                         UriEx2 ex = new UriEx2 ("readme.txt");
1460                 }
1461
1462                 [Test]
1463                 public void UnixLocalPath ()
1464                 {
1465                         // This works--the location is not part of the absolute path
1466                         string path = "file://localhost/tmp/foo/bar";
1467                         Uri fileUri = new Uri( path );
1468                         AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1469
1470                         // Empty path == localhost, in theory
1471                         path = "file:///c:/tmp/foo/bar";
1472                         fileUri = new Uri( path );
1473                         AssertEquals (path, "c:/tmp/foo/bar", fileUri.AbsolutePath);
1474                 }
1475
1476                 // This test doesn't work on Linux, and arguably shouldn't work.
1477                 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar" 
1478                 // on Linux, as anyone sane would expect.  It *doesn't* under .NET 1.1
1479                 // Apparently "tmp" is supposed to be a hostname (!)...
1480                 // Since "correct" behavior would confuse all Linux developers, and having
1481                 // an expected failure is evil, we'll just ignore this for now...
1482                 //
1483                 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1484                 //
1485                 // You are surrounded by conditional-compilation code, all alike.
1486                 // You are likely to be eaten by a Grue...
1487                 [Test]
1488 #if !NET_2_0
1489                 [Category ("NotDotNet")]
1490 #endif
1491                 public void UnixLocalPath_WTF ()
1492                 {
1493                         // Empty path == localhost, in theory
1494                         string path = "file:///tmp/foo/bar";
1495                         Uri fileUri = new Uri( path );
1496 //#if NET_2_0
1497                         AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1498 //#else
1499 //                      AssertEquals (path, "/foo/bar", fileUri.AbsolutePath);
1500 //#endif
1501
1502                         // bug #76643
1503                         string path2 = "file:///foo%25bar";
1504                         fileUri = new Uri (path2);
1505                         AssertEquals (path2, "file:///foo%25bar", fileUri.ToString ());
1506                 }
1507
1508                 public static void Print (Uri uri)
1509                 {
1510                         Console.WriteLine ("ToString: " + uri.ToString ());     
1511
1512                         Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1513                         Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1514                         Console.WriteLine ("Authority: " + uri.Authority);
1515                         Console.WriteLine ("Fragment: " + uri.Fragment);
1516                         Console.WriteLine ("Host: " + uri.Host);
1517                         Console.WriteLine ("HostNameType: " + uri.HostNameType);
1518                         Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1519                         Console.WriteLine ("IsFile: " + uri.IsFile);
1520                         Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1521                         Console.WriteLine ("IsUnc: " + uri.IsUnc);
1522                         Console.WriteLine ("LocalPath: " + uri.LocalPath);
1523                         Console.WriteLine ("PathAndQuery: " + uri.PathAndQuery);
1524                         Console.WriteLine ("Port: " + uri.Port);
1525                         Console.WriteLine ("Query: " + uri.Query);
1526                         Console.WriteLine ("Scheme: " + uri.Scheme);
1527                         Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1528                         Console.WriteLine ("UserInfo: " + uri.UserInfo);
1529
1530                         Console.WriteLine ("Segments:");
1531                         string [] segments = uri.Segments;
1532                         if (segments == null) 
1533                                 Console.WriteLine ("\tNo Segments");
1534                         else 
1535                                 for (int i = 0; i < segments.Length; i++) 
1536                                         Console.WriteLine ("\t" + segments[i]);                                 
1537                         Console.WriteLine ("");
1538                 }
1539         }
1540 }
1541