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