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