2005-11-14 Atsushi Enomoto <atsushi@ximian.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
17 namespace MonoTests.System
18 {
19         [TestFixture]
20         public class UriTest : Assertion
21         {
22                 protected bool isWin32 = false;
23
24                 [SetUp]
25                 public void GetReady () 
26                 {
27                         isWin32 = (Path.DirectorySeparatorChar == '\\');
28                 }
29
30                 [Test]
31                 public void Constructors ()
32                 {
33                         Uri uri = null;
34                         
35                         /*
36                         uri = new Uri ("http://www.ximian.com/foo" + ((char) 0xa9) + "/bar/index.cgi?a=1&b=" + ((char) 0xa9) + "left#fragm?ent2");
37                         Print (uri);
38
39                         uri = new Uri ("http://www.ximian.com/foo/xxx\"()-._;<=>@{|}~-,.`_^]\\[xx/" + ((char) 0xa9) + "/bar/index.cgi#fra+\">=@[gg]~gment2");
40                         Print (uri);
41
42                         uri = new Uri("http://11.22.33.588:9090");
43                         Print (uri);
44
45                         uri = new Uri("http://[11:22:33::88]:9090");
46                         Print (uri);
47                         
48                         uri = new Uri("http://[::127.11.22.33]:8080");
49                         Print (uri);
50                         
51                         uri = new Uri("http://[abcde::127.11.22.33]:8080");
52                         Print (uri);                    
53                         */
54                         
55                         /*
56                         uri = new Uri ("http://www.contoso.com:1234/foo/bar/");
57                         Print (uri);
58
59                         uri = new Uri ("http://www.contoso.com:1234/foo/bar");
60                         Print (uri);
61                         
62                         uri = new Uri ("http://www.contoso.com:1234/");
63                         Print (uri);
64
65                         uri = new Uri ("http://www.contoso.com:1234");
66                         Print (uri);
67                         */
68                         
69                         uri = new Uri ("http://contoso.com?subject=uri");
70                         AssertEquals ("#k1", "/", uri.AbsolutePath);
71                         AssertEquals ("#k2", "http://contoso.com/?subject=uri", uri.AbsoluteUri);
72                         AssertEquals ("#k3", "contoso.com", uri.Authority);
73                         AssertEquals ("#k4", "", uri.Fragment);
74                         AssertEquals ("#k5", "contoso.com", uri.Host);
75                         AssertEquals ("#k6", UriHostNameType.Dns, uri.HostNameType);
76                         AssertEquals ("#k7", true, uri.IsDefaultPort);
77                         AssertEquals ("#k8", false, uri.IsFile);
78                         AssertEquals ("#k9", false, uri.IsLoopback);
79                         AssertEquals ("#k10", false, uri.IsUnc);
80                         AssertEquals ("#k11", "/", uri.LocalPath);
81                         AssertEquals ("#k12", "/?subject=uri", uri.PathAndQuery);
82                         AssertEquals ("#k13", 80, uri.Port);
83                         AssertEquals ("#k14", "?subject=uri", uri.Query);
84                         AssertEquals ("#k15", "http", uri.Scheme);
85                         AssertEquals ("#k16", false, uri.UserEscaped);
86                         AssertEquals ("#k17", "", uri.UserInfo);
87
88                         uri = new Uri ("mailto:user:pwd@contoso.com?subject=uri");
89                         AssertEquals ("#m1", "", uri.AbsolutePath);
90                         AssertEquals ("#m2", "mailto:user:pwd@contoso.com?subject=uri", uri.AbsoluteUri);
91                         AssertEquals ("#m3", "contoso.com", uri.Authority);
92                         AssertEquals ("#m4", "", uri.Fragment);
93                         AssertEquals ("#m5", "contoso.com", uri.Host);
94                         AssertEquals ("#m6", UriHostNameType.Dns, uri.HostNameType);
95                         AssertEquals ("#m7", true, uri.IsDefaultPort);
96                         AssertEquals ("#m8", false, uri.IsFile);
97                         AssertEquals ("#m9", false, uri.IsLoopback);
98                         AssertEquals ("#m10", false, uri.IsUnc);
99                         AssertEquals ("#m11", "", uri.LocalPath);
100                         AssertEquals ("#m12", "?subject=uri", uri.PathAndQuery);
101                         AssertEquals ("#m13", 25, uri.Port);
102                         AssertEquals ("#m14", "?subject=uri", uri.Query);
103                         AssertEquals ("#m15", "mailto", uri.Scheme);
104                         AssertEquals ("#m16", false, uri.UserEscaped);
105                         AssertEquals ("#m17", "user:pwd", uri.UserInfo);
106                         
107                         uri = new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
108                         AssertEquals ("#n1", "/mydir/mysubdir/myfile.ext", uri.AbsolutePath);
109                         AssertEquals ("#n2", "file://myserver/mydir/mysubdir/myfile.ext", uri.AbsoluteUri);
110                         AssertEquals ("#n3", "myserver", uri.Authority);
111                         AssertEquals ("#n4", "", uri.Fragment);
112                         AssertEquals ("#n5", "myserver", uri.Host);
113                         AssertEquals ("#n6", UriHostNameType.Dns, uri.HostNameType);
114                         AssertEquals ("#n7", true, uri.IsDefaultPort);
115                         AssertEquals ("#n8", true, uri.IsFile);
116                         AssertEquals ("#n9", false, uri.IsLoopback);
117                         AssertEquals ("#n10", true, uri.IsUnc);
118
119                         if (isWin32)
120                                 AssertEquals ("#n11", @"\\myserver\mydir\mysubdir\myfile.ext", uri.LocalPath);
121                         else
122                                 // myserver never could be the part of Unix path.
123                                 AssertEquals ("#n11", "/mydir/mysubdir/myfile.ext", uri.LocalPath);
124
125                         AssertEquals ("#n12", "/mydir/mysubdir/myfile.ext", uri.PathAndQuery);
126                         AssertEquals ("#n13", -1, uri.Port);
127                         AssertEquals ("#n14", "", uri.Query);
128                         AssertEquals ("#n15", "file", uri.Scheme);
129                         AssertEquals ("#n16", false, uri.UserEscaped);
130                         AssertEquals ("#n17", "", uri.UserInfo);
131                         
132                         uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
133                         AssertEquals ("#rel1a", "http://www.contoso.com/Hello World.htm", uri.AbsoluteUri);
134                         AssertEquals ("#rel1b", true, uri.UserEscaped);
135                         uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
136                         AssertEquals ("#rel2a", "http://www.contoso.com/Hello%20World.htm", uri.AbsoluteUri);
137                         AssertEquals ("#rel2b", false, uri.UserEscaped);
138                         uri = new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
139                         AssertEquals ("#rel3", "http://www.xxx.com/Hello%20World.htm", uri.AbsoluteUri);
140                         //uri = new Uri (new Uri("http://www.contoso.com"), "foo:8080/bar/Hello World.htm", false);
141                         //AssertEquals ("#rel4", "foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri);
142                         uri = new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
143                         AssertEquals ("#rel5", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
144                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "foo/bar/Hello World.htm?x=0:8", false);
145                         AssertEquals ("#rel6", "http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
146                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
147                         AssertEquals ("#rel7", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
148                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
149                         AssertEquals ("#rel8", "http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
150                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../../../foo/bar/Hello World.htm?x=0:8", false);
151                         AssertEquals ("#rel9", "http://www.contoso.com/../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 ("#rel10", "http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
154
155                         try {
156                                 uri = new Uri (null, "http://www.contoso.com/index.htm", false);
157                                 Fail ("#rel20");
158                         } catch (NullReferenceException) {
159                         }
160                         try {
161                                 uri = new Uri (new Uri("http://www.contoso.com"), null, false);
162                                 Fail ("#rel21");
163                         } catch (NullReferenceException) {
164                         }
165                         try {
166                                 uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
167                                 AssertEquals("#22", "http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString ());
168                         } catch (NullReferenceException) {
169                         }
170
171                         uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
172                         AssertEquals ("#rel30", "http://www.xxx.com/?x=0", uri.ToString());
173                         uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "?x=0");
174                         AssertEquals ("#rel31", "http://www.xxx.com/?x=0", uri.ToString());
175                         uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
176                         AssertEquals ("#rel32", "http://www.xxx.com/index.htm#here", uri.ToString());
177                 }
178
179                 // regression for bug #47573
180                 [Test]
181                 public void RelativeCtor ()
182                 {
183                         Uri b = new Uri ("http://a/b/c/d;p?q");
184 //                      AssertEquals ("g:h", "g:h", new Uri (b, "g:h").ToString ()); this causes crash under MS.NET 1.1
185                         AssertEquals ("#1", "http://a/g", new Uri (b, "/g").ToString ());
186                         AssertEquals ("#2", "http://g/", new Uri (b, "//g").ToString ());
187                         AssertEquals ("#3", "http://a/b/c/?y", new Uri (b, "?y").ToString ());
188                         Assert ("#4", new Uri (b, "#s").ToString ().EndsWith ("#s"));
189
190                         Uri u = new Uri (b, "/g?q=r");
191                         AssertEquals ("#5", "http://a/g?q=r", u.ToString ());
192                         AssertEquals ("#6", "?q=r", u.Query);
193
194                         u = new Uri (b, "/g?q=r;. a");
195                         AssertEquals ("#5", "http://a/g?q=r;. a", u.ToString ());
196                         AssertEquals ("#6", "?q=r;.%20a", u.Query);
197                 }
198
199                 [Test]
200                 // some tests from bug 75144
201                 public void LeadingSlashes ()
202                 {
203                         Uri u;
204                         AssertEquals ("#1", "file:///", new Uri ("file:///").ToString());
205                         AssertEquals ("#2", "file:///", new Uri ("file://").ToString());
206
207                         u = new Uri ("file:///foo/bar");
208                         AssertEquals ("#3", "file:///foo/bar", u.ToString());
209                         AssertEquals ("#4", false, u.IsUnc);
210
211                         u = new Uri ("file://foo/bar");
212                         AssertEquals ("#5", "file://foo/bar", u.ToString());
213                         AssertEquals ("#6", true, u.IsUnc);
214
215                         u = new Uri ("file:////foo/bar");
216                         AssertEquals ("#7", "file://foo/bar", u.ToString());
217                         AssertEquals ("#8", true, u.IsUnc); 
218
219                         AssertEquals ("#9", "file://foo/bar", new Uri ("file://///foo/bar").ToString());
220
221                         AssertEquals ("#10", "mailto:", new Uri ("mailto:").ToString());
222                         AssertEquals ("#11", "mailto:foo", new Uri ("mailto:foo").ToString());
223                         AssertEquals ("#12", "mailto:/", new Uri ("mailto:/").ToString());
224                         AssertEquals ("#13", "mailto:/foo", new Uri ("mailto:/foo").ToString());
225                         AssertEquals ("#14", "mailto://foo", new Uri ("mailto://foo").ToString());
226                         AssertEquals ("#15", "mailto:///foo", new Uri ("mailto:///foo").ToString());
227
228                         AssertEquals ("#16", "news:", new Uri ("news:").ToString());
229                         AssertEquals ("#17", "news:foo", new Uri ("news:foo").ToString());
230                         AssertEquals ("#18", "news:/", new Uri ("news:/").ToString());
231                         AssertEquals ("#19", "news:/foo", new Uri ("news:/foo").ToString());
232                         AssertEquals ("#20", "news://foo", new Uri ("news://foo").ToString());
233                         AssertEquals ("#21", "news:///foo", new Uri ("news:///foo").ToString());
234                 }
235
236                 [Test]
237                 [ExpectedException (typeof (UriFormatException))]
238                 public void HttpHostname1 ()
239                 {
240                         new Uri ("http:");
241                 }
242
243                 [Test]
244                 [ExpectedException (typeof (UriFormatException))]
245                 public void HttpHostname2 ()
246                 {
247                         new Uri ("http:a");
248                 }
249                 
250                 [Test]
251                 [ExpectedException (typeof (UriFormatException))]
252                 public void HttpHostname3 ()
253                 {
254                         new Uri ("http:/");
255                 }
256
257                 [Test]
258                 [ExpectedException (typeof (UriFormatException))]
259                 public void HttpHostname4 ()
260                 {
261                         new Uri ("http:/foo");
262                 }
263
264                 [Test]
265                 [ExpectedException (typeof (UriFormatException))]
266                 public void HttpHostname5 ()
267                 {
268                         new Uri ("http://");
269                 }
270
271                 [Test]
272                 [ExpectedException (typeof (UriFormatException))]
273                 public void HttpHostname6 ()
274                 {
275                         new Uri ("http:///");
276                 }
277
278                 [Test]
279                 [ExpectedException (typeof (UriFormatException))]
280                 public void HttpHostname7 ()
281                 {
282                         new Uri ("http:///foo");
283                 }
284
285                 [Test]
286                 [ExpectedException (typeof (UriFormatException))]
287                 public void InvalidFile1 ()
288                 {
289                         new Uri ("file:");
290                 }
291
292                 [Test]
293                 [ExpectedException (typeof (UriFormatException))]
294                 public void InvalidFile2 ()
295                 {
296                         new Uri ("file:/");
297                 }
298
299                 [Test]
300                 [ExpectedException (typeof (UriFormatException))]
301                 public void InvalidFile3 ()
302                 {
303                         new Uri ("file:/foo");
304                 }       
305
306                 [Test]
307                 [ExpectedException (typeof (UriFormatException))]
308                 public void InvalidScheme ()
309                 {
310                         new Uri ("_:/");
311                 }
312
313                 [Test]
314                 public void ConstructorsRejectRelativePath ()
315                 {
316                         string [] reluris = new string [] {
317                                 "readme.txt",
318                                 "thisdir/childdir/file",
319                                 "./testfile"
320                         };
321                         string [] winRelUris = new string [] {
322                                 "c:readme.txt"
323                         };
324
325                         for (int i = 0; i < reluris.Length; i++) {
326                                 try {
327                                         new Uri (reluris [i]);
328                                         Fail ("Should be failed: " + reluris [i]);
329                                 } catch (UriFormatException) {
330                                 }
331                         }
332
333                         if (isWin32) {
334                                 for (int i = 0; i < winRelUris.Length; i++) {
335                                         try {
336                                                 new Uri (winRelUris [i]);
337                                                 Fail ("Should be failed: " + winRelUris [i]);
338                                         } catch (UriFormatException) {
339                                         }
340                                 }
341                         }
342                 }
343
344
345                 [Test]
346                 public void LocalPath ()
347                 {
348                         Uri uri = new Uri ("c:\\tmp\\hello.txt");
349                         AssertEquals ("#1a", "file:///c:/tmp/hello.txt", uri.ToString ());
350                         AssertEquals ("#1b", "c:\\tmp\\hello.txt", uri.LocalPath);
351                         AssertEquals ("#1c", "file", uri.Scheme);
352                         AssertEquals ("#1d", "", uri.Host);
353                         AssertEquals ("#1e", "c:/tmp/hello.txt", uri.AbsolutePath);
354                                         
355                         uri = new Uri ("file:////////cygwin/tmp/hello.txt");
356                         AssertEquals ("#3a", "file://cygwin/tmp/hello.txt", uri.ToString ());
357                         if (isWin32)
358                                 AssertEquals ("#3b win32", "\\\\cygwin\\tmp\\hello.txt", uri.LocalPath);
359                         else
360                                 AssertEquals ("#3b *nix", "/tmp/hello.txt", uri.LocalPath);
361                         AssertEquals ("#3c", "file", uri.Scheme);
362                         AssertEquals ("#3d", "cygwin", uri.Host);
363                         AssertEquals ("#3e", "/tmp/hello.txt", uri.AbsolutePath);
364
365                         uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
366                         AssertEquals ("#4a", "file://mymachine/cygwin/tmp/hello.txt", uri.ToString ());
367                         if (isWin32)
368                                 AssertEquals ("#4b win32", "\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath);
369                         else
370                                 AssertEquals ("#4b *nix", "/cygwin/tmp/hello.txt", uri.LocalPath);
371                         AssertEquals ("#4c", "file", uri.Scheme);
372                         AssertEquals ("#4d", "mymachine", uri.Host);
373                         AssertEquals ("#4e", "/cygwin/tmp/hello.txt", uri.AbsolutePath);
374                         
375                         uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
376                         AssertEquals ("#5a", "file:///c:/cygwin/tmp/hello.txt", uri.ToString ());
377                         AssertEquals ("#5b", "c:\\cygwin\\tmp\\hello.txt", uri.LocalPath);
378                         AssertEquals ("#5c", "file", uri.Scheme);
379                         AssertEquals ("#5d", "", uri.Host);
380                         AssertEquals ("#5e", "c:/cygwin/tmp/hello.txt", uri.AbsolutePath);
381                         // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
382                         uri = new Uri("file://one_file.txt");
383                         AssertEquals("#6a", "file://one_file.txt", uri.ToString());
384                         if (isWin32)
385                                 AssertEquals("#6b", "\\\\one_file.txt", uri.LocalPath);
386                         else
387                                 AssertEquals("#6b", "/", uri.LocalPath);
388                         AssertEquals("#6c", "file", uri.Scheme);
389                         AssertEquals("#6d", "one_file.txt", uri.Host);
390                         AssertEquals("#6e", "", uri.AbsolutePath);
391
392                         // escape
393                         uri = new Uri ("file:///tmp/a%20a");
394                         if (isWin32)
395                                 // actually MS.NET treats /// as \\ thus it fails here.
396                                 Assert ("#7a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\tmp\\a a"));
397                         else
398                                 AssertEquals ("#7b", "/tmp/a a", uri.LocalPath);
399
400                         uri = new Uri ("file:///tmp/foo%25bar");
401                         if (isWin32) {
402                                 // actually MS.NET treats /// as \\ thus it fails here.
403                                 Assert ("#8a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\tmp\\foo%bar"));
404                                 // ditto, file://tmp/foo%25bar (bug in 1.x)
405                                 Assert ("#8c:" + uri.ToString (), uri.ToString ().EndsWith ("//tmp/foo%25bar"));
406                         }
407                         else {
408                                 AssertEquals ("#8b", "/tmp/foo%bar", uri.LocalPath);
409                                 AssertEquals ("#8d", "file:///tmp/foo%25bar", uri.ToString ());
410                         }
411                         // bug #76643
412                         uri = new Uri ("file:///foo%25bar");
413                         if (isWin32) {
414                                 // actually MS.NET treats /// as \\ thus it fails here.
415                                 Assert ("#9a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\foo%bar"));
416                                 // ditto, file://tmp/foo%25bar (bug in 1.x)
417                                 Assert ("#9c:" + uri.ToString (), uri.ToString ().EndsWith ("//foo%25bar"));
418                         }
419                         else {
420                                 AssertEquals ("#9b", "/foo%bar", uri.LocalPath);
421                                 AssertEquals ("#9d", "file:///foo%25bar", uri.ToString ());
422                         }
423                 }
424                 
425                 [Test]
426                 public void UnixPath () {
427                         if (!isWin32)
428                                 AssertEquals ("#6a", "file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString ());
429                 }
430                 
431                 [Test]
432                 public void Unc ()
433                 {
434                         Uri uri = new Uri ("http://www.contoso.com");
435                         Assert ("#1", !uri.IsUnc);
436                         
437                         uri = new Uri ("news:123456@contoso.com");
438                         Assert ("#2", !uri.IsUnc);
439
440                         uri = new Uri ("file://server/filename.ext");
441                         Assert ("#3", uri.IsUnc);
442
443                         uri = new Uri (@"\\server\share\filename.ext");                 
444                         Assert ("#6", uri.IsUnc);
445
446                         uri = new Uri (@"a:\dir\filename.ext");
447                         Assert ("#8", !uri.IsUnc);
448                 }
449
450                 [Test]
451                 [Category("NotDotNet")]
452                 public void UncFail ()
453                 {
454                         Uri uri = new Uri ("/home/user/dir/filename.ext");
455                         Assert ("#7", !uri.IsUnc);
456                 }
457
458                 [Test]
459                 public void FromHex () 
460                 {
461                         AssertEquals ("#1", 0, Uri.FromHex ('0'));
462                         AssertEquals ("#2", 9, Uri.FromHex ('9'));
463                         AssertEquals ("#3", 10, Uri.FromHex ('a'));
464                         AssertEquals ("#4", 15, Uri.FromHex ('f'));
465                         AssertEquals ("#5", 10, Uri.FromHex ('A'));
466                         AssertEquals ("#6", 15, Uri.FromHex ('F'));
467                         try {
468                                 Uri.FromHex ('G');
469                                 Fail ("#7");
470                         } catch (ArgumentException) {}
471                         try {
472                                 Uri.FromHex (' ');
473                                 Fail ("#8");
474                         } catch (ArgumentException) {}
475                         try {
476                                 Uri.FromHex ('%');
477                                 Fail ("#8");
478                         } catch (ArgumentException) {}
479                 }
480
481                 class UriEx : Uri
482                 {
483                         public UriEx (string s) : base (s)
484                         {
485                         }
486
487                         public string UnescapeString (string s)
488                         {
489                                 return Unescape (s);
490                         }
491
492                         public static string UnescapeString (string uri, string target)
493                         {
494                                 return new UriEx (uri).UnescapeString (target);
495                         }
496                 }
497
498                 [Test]
499                 public void Unescape ()
500                 {
501                         AssertEquals ("#1", "#", UriEx.UnescapeString ("file://localhost/c#", "%23"));
502                         AssertEquals ("#2", "c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"));
503                         AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"));
504                         AssertEquals ("#1", "#", UriEx.UnescapeString ("http://localhost/c#", "%23"));
505                         AssertEquals ("#2", "c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"));
506                         AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"));
507                 }
508
509                 [Test]
510                 public void HexEscape () 
511                 {
512                         AssertEquals ("#1","%20", Uri.HexEscape (' ')); 
513                         AssertEquals ("#2","%A9", Uri.HexEscape ((char) 0xa9)); 
514                         AssertEquals ("#3","%41", Uri.HexEscape ('A')); 
515                         try {
516                                 Uri.HexEscape ((char) 0x0369);
517                                 Fail ("#4");
518                         } catch (ArgumentOutOfRangeException) {}
519                 }
520
521                 [Test]
522                 public void HexUnescape () 
523                 {
524                         int i = 0;
525                         AssertEquals ("#1", ' ', Uri.HexUnescape ("%20", ref i));
526                         AssertEquals ("#2", 3, i);
527                         i = 4;
528                         AssertEquals ("#3", (char) 0xa9, Uri.HexUnescape ("test%a9test", ref i));
529                         AssertEquals ("#4", 7, i);
530                         AssertEquals ("#5", 't', Uri.HexUnescape ("test%a9test", ref i));
531                         AssertEquals ("#6", 8, i);
532                         i = 4;
533                         AssertEquals ("#5", '%', Uri.HexUnescape ("test%a", ref i));
534                         AssertEquals ("#6", 5, i);
535                         AssertEquals ("#7", '%', Uri.HexUnescape ("testx%xx", ref i));
536                         AssertEquals ("#8", 6, i);
537
538                         // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
539                         i = 1;
540                         AssertEquals ("#9", 227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i));
541                         AssertEquals ("#10", 4, i);
542                         i = 1;
543                         AssertEquals ("#11", 240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i));
544                         AssertEquals ("#12", 4, i);
545                 }
546
547 #if !NET_2_0
548                 // These won't pass exactly with MS.NET 1.x, due to differences in the
549                 // handling of backslashes/forwardslashes
550                 [Category ("NotDotNet")]
551 #endif
552                 [Test]
553                 public void HexUnescapeMultiByte () 
554                 {
555                         // Tests from bug 74872
556                         // Note: These won't pass exactly with MS.NET, due to differences in the
557                         // handling of backslashes/forwardslashes
558                         Uri uri;
559                         string path;
560
561                         // 3-byte character
562                         uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
563                         path = uri.LocalPath;
564                         AssertEquals ("#1", 8, path.Length);
565                         AssertEquals ("#2", 0x304B, path [6]);
566
567                         // 4-byte character which should be handled as a surrogate
568                         uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
569                         path = uri.LocalPath;
570                         AssertEquals ("#3", 9, path.Length);
571                         AssertEquals ("#4", 0xDB40, path [6]);
572                         AssertEquals ("#5", 0xDD00, path [7]);
573                         AssertEquals ("#6", 0x62, path [8]);
574                         
575                         // 2-byte escape sequence, 2 individual characters
576                         uri = new Uri ("file:///foo/a%C2%F8b", true);
577                         path = uri.LocalPath;
578                         AssertEquals ("#7", 9, path.Length);
579                         AssertEquals ("#8", 0xC2, path [6]);
580                         AssertEquals ("#9", 0xF8, path [7]);                    
581                 }
582
583                 [Test]
584                 public void IsHexDigit () 
585                 {
586                         Assert ("#1", Uri.IsHexDigit ('a'));    
587                         Assert ("#2", Uri.IsHexDigit ('f'));
588                         Assert ("#3", !Uri.IsHexDigit ('g'));
589                         Assert ("#4", Uri.IsHexDigit ('0'));
590                         Assert ("#5", Uri.IsHexDigit ('9'));
591                         Assert ("#6", Uri.IsHexDigit ('A'));
592                         Assert ("#7", Uri.IsHexDigit ('F'));
593                         Assert ("#8", !Uri.IsHexDigit ('G'));
594                 }
595
596                 [Test]
597                 public void IsHexEncoding () 
598                 {
599                         Assert ("#1", Uri.IsHexEncoding ("test%a9test", 4));
600                         Assert ("#2", !Uri.IsHexEncoding ("test%a9test", 3));
601                         Assert ("#3", Uri.IsHexEncoding ("test%a9", 4));
602                         Assert ("#4", !Uri.IsHexEncoding ("test%a", 4));
603                 }
604                 
605                 [Test]
606                 public void GetLeftPart ()
607                 {
608                         Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
609                         AssertEquals ("#1", "http://", uri.GetLeftPart (UriPartial.Scheme));
610                         AssertEquals ("#2", "http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority));
611                         AssertEquals ("#3", "http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path));
612                         
613                         uri = new Uri ("mailto:user@contoso.com?subject=uri");
614                         AssertEquals ("#4", "mailto:", uri.GetLeftPart (UriPartial.Scheme));
615                         AssertEquals ("#5", "", uri.GetLeftPart (UriPartial.Authority));
616                         AssertEquals ("#6", "mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path));
617
618                         uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
619                         AssertEquals ("#7", "nntp://", uri.GetLeftPart (UriPartial.Scheme));
620                         AssertEquals ("#8", "nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority));
621                         AssertEquals ("#9", "nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path));                   
622                         
623                         uri = new Uri ("news:123456@contoso.com");
624                         AssertEquals ("#10", "news:", uri.GetLeftPart (UriPartial.Scheme));
625                         AssertEquals ("#11", "", uri.GetLeftPart (UriPartial.Authority));
626                         AssertEquals ("#12", "news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path));                     
627
628                         uri = new Uri ("file://server/filename.ext");
629                         AssertEquals ("#13", "file://", uri.GetLeftPart (UriPartial.Scheme));
630                         AssertEquals ("#14", "file://server", uri.GetLeftPart (UriPartial.Authority));
631                         AssertEquals ("#15", "file://server/filename.ext", uri.GetLeftPart (UriPartial.Path));                  
632
633                         uri = new Uri (@"\\server\share\filename.ext");
634                         AssertEquals ("#20", "file://", uri.GetLeftPart (UriPartial.Scheme));
635                         AssertEquals ("#21", "file://server", uri.GetLeftPart (UriPartial.Authority));
636                         AssertEquals ("#22", "file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path));
637                         
638                         uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
639                         AssertEquals ("#23", "http://", uri.GetLeftPart (UriPartial.Scheme));
640                         AssertEquals ("#24", "http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority));
641                         AssertEquals ("#25", "http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path));
642                 }
643
644                 [Test]
645                 public void NewsDefaultPort ()
646                 {
647                         Uri uri = new Uri("news://localhost:119/");
648                         AssertEquals ("#1", uri.IsDefaultPort, true);
649                 }
650
651                 [Test]
652                 public void FragmentEscape ()
653                 {
654                         Uri u = new Uri("http://localhost/index.asp#main#start", false);
655                         AssertEquals ("#1", u.Fragment, "#main%23start");
656
657                         u = new Uri("http://localhost/index.asp#main#start", true);
658                         AssertEquals ("#2", u.Fragment, "#main#start");
659
660                         // The other code path uses a BaseUri
661
662                         Uri b = new Uri ("http://www.gnome.org");
663                         Uri n = new Uri (b, "blah#main#start");
664                         AssertEquals ("#3", n.Fragment, "#main%23start");
665                         
666                         n = new Uri (b, "blah#main#start", true);
667                         AssertEquals ("#3", n.Fragment, "#main#start");
668                 }
669                         
670                 [Test]
671                 [ExpectedException(typeof(UriFormatException))]
672                 public void IncompleteSchemeDelimiter ()
673                 {
674                         new Uri ("file:/filename.ext");
675                 }
676
677                 [Test]
678                 [Category("NotDotNet")]
679                 public void CheckHostName1 ()
680                 {
681                         // reported to MSDN Product Feedback Center (FDBK28671)
682                         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"));
683                 }
684
685                 [Test]
686                 public void CheckHostName2 ()
687                 {
688                         AssertEquals ("#1", UriHostNameType.Unknown, Uri.CheckHostName (null));
689                         AssertEquals ("#2", UriHostNameType.Unknown, Uri.CheckHostName (""));
690                         AssertEquals ("#3", UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"));
691                         AssertEquals ("#4", UriHostNameType.Dns, Uri.CheckHostName ("x"));
692                         AssertEquals ("#5", UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"));
693                         AssertEquals ("#6", UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"));
694                         AssertEquals ("#7", UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"));
695                         AssertEquals ("#8", UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"));
696                         AssertEquals ("#9", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"));
697                         AssertEquals ("#10", UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"));
698                         AssertEquals ("#11", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."));
699                         AssertEquals ("#12", UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"));      
700                         AssertEquals ("#13", UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"));      
701                         AssertEquals ("#14", UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"));  
702                         
703                         // test IPv6
704                         AssertEquals ("#15", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"));
705                         AssertEquals ("#16", UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"));
706                         AssertEquals ("#17", UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"));
707                         AssertEquals ("#18", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"));
708                         AssertEquals ("#19", UriHostNameType.IPv6, Uri.CheckHostName ("11::88"));
709                         AssertEquals ("#20", UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"));
710                         AssertEquals ("#21", UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"));
711                         AssertEquals ("#22", UriHostNameType.IPv6, Uri.CheckHostName ("11::"));
712                         AssertEquals ("#23", UriHostNameType.IPv6, Uri.CheckHostName ("::88"));
713                         AssertEquals ("#24", UriHostNameType.IPv6, Uri.CheckHostName ("::1"));
714                         AssertEquals ("#25", UriHostNameType.IPv6, Uri.CheckHostName ("::"));
715                         AssertEquals ("#26", UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"));
716                         AssertEquals ("#27", UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"));
717                         AssertEquals ("#28", UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"));
718                         AssertEquals ("#29", UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"));
719                         AssertEquals ("#30", UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"));
720                         AssertEquals ("#33", UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"));
721
722                         AssertEquals ("#31", UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"));
723                         AssertEquals ("#32", UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"));
724                         AssertEquals ("#34", UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"));
725                         AssertEquals ("#35", UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"));
726                         AssertEquals ("#37", UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"));
727                         AssertEquals ("#38", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"));
728                         AssertEquals ("#39", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"));
729                         AssertEquals ("#40", UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"));
730                         AssertEquals ("#41", UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"));
731                         AssertEquals ("#42", UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"));
732                         AssertEquals ("#43", UriHostNameType.Unknown, Uri.CheckHostName (":::"));
733                         AssertEquals ("#44", UriHostNameType.Unknown, Uri.CheckHostName (":"));
734                 }
735                 
736                 [Test]
737                 public void IsLoopback ()
738                 {
739                         Uri uri = new Uri("http://loopback:8080");
740                         AssertEquals ("#1", true, uri.IsLoopback);
741                         uri = new Uri("http://localhost:8080");
742                         AssertEquals ("#2", true, uri.IsLoopback);
743                         uri = new Uri("http://127.0.0.1:8080");
744                         AssertEquals ("#3", true, uri.IsLoopback);
745                         uri = new Uri("http://127.0.0.001:8080");
746                         AssertEquals ("#4", true, uri.IsLoopback);
747                         uri = new Uri("http://[::1]");
748                         AssertEquals ("#5", true, uri.IsLoopback);
749                         uri = new Uri("http://[::1]:8080");
750                         AssertEquals ("#6", true, uri.IsLoopback);
751                         uri = new Uri("http://[::0001]:8080");
752                         AssertEquals ("#7", true, uri.IsLoopback);
753                         uri = new Uri("http://[0:0:0:0::1]:8080");
754                         AssertEquals ("#8", true, uri.IsLoopback);
755                         uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
756                         AssertEquals ("#9", true, uri.IsLoopback);
757                         uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
758                         AssertEquals ("#10", false, uri.IsLoopback);
759                         uri = new Uri("http://[::ffff:127.11.22.33]:8080");
760                         AssertEquals ("#11", false, uri.IsLoopback);
761                         uri = new Uri("http://[::ff00:7f11:2233]:8080");
762                         AssertEquals ("#12", false, uri.IsLoopback);
763                         uri = new Uri("http://[1:0:0:0::1]:8080");
764                         AssertEquals ("#13", false, uri.IsLoopback);
765                 }
766                 
767                 [Test]
768                 public void Equals1 ()
769                 {
770                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
771                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
772                         Assert ("#1", uri1.Equals (uri2));
773                         Assert ("#3", !uri2.Equals ("http://www.contoso.com/index.html?x=1"));
774                         Assert ("#4", !uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"));
775                 }
776
777                 [Test]
778 #if !NET_2_0
779                 [Category("NotDotNet")]
780 #endif
781                 public void Equals2 ()
782                 {
783                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
784                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
785                         Assert ("#2 known to fail with ms.net 1.x", !uri1.Equals (uri2));
786                 }
787
788                 [Test]
789                 public void TestEquals2 ()
790                 {
791                         Uri a = new Uri ("http://www.go-mono.com");
792                         Uri b = new Uri ("http://www.go-mono.com");
793
794                         AssertEquals ("#1", a, b);
795
796                         a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
797                         b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
798
799                         AssertEquals ("#2", a, b);
800
801                         a = new Uri ("http://www.go-mono.com/ports/");
802                         b = new Uri ("http://www.go-mono.com/PORTS/");
803
804                         Assert ("#3", !a.Equals (b));
805                 }
806
807                 [Test]
808                 public void GetHashCodeTest () 
809                 {
810                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
811                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
812                         AssertEquals ("#1", uri1.GetHashCode (), uri2.GetHashCode ());
813                         uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
814                         Assert ("#2", uri1.GetHashCode () != uri2.GetHashCode ());
815                         uri2 = new Uri ("http://www.contoso.com:80/index.htm");
816                         AssertEquals ("#3", uri1.GetHashCode (), uri2.GetHashCode ());                  
817                         uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
818                         Assert ("#4", uri1.GetHashCode () != uri2.GetHashCode ());                      
819                 }
820                 
821                 [Test]
822                 public void MakeRelative ()
823                 {
824                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
825                         Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
826                         Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
827                         Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
828                         Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
829                         Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
830                         Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
831                         Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
832
833                         AssertEquals ("#1", "foo/bar/index.htm", uri1.MakeRelative (uri2));
834                         AssertEquals ("#2", "../../index.htm", uri2.MakeRelative (uri1));
835                         
836                         AssertEquals ("#3", "../../bar/foo/index.htm", uri2.MakeRelative (uri3));
837                         AssertEquals ("#4", "../../foo/bar/index.htm", uri3.MakeRelative (uri2));                       
838
839                         AssertEquals ("#5", "../foo2/index.htm", uri3.MakeRelative (uri4));
840                         AssertEquals ("#6", "../foo/index.htm", uri4.MakeRelative (uri3));
841                         
842                         AssertEquals ("#7", "https://www.contoso.com/bar/foo/index.htm?y=1", 
843                                             uri4.MakeRelative (uri5));
844
845                         AssertEquals ("#8", "http://www.contoso2.com/bar/foo/index.htm?x=0", 
846                                             uri4.MakeRelative (uri6));
847
848                         AssertEquals ("#9", "", uri6.MakeRelative (uri6));
849                         AssertEquals ("#10", "foobar.htm", uri6.MakeRelative (uri7));
850                         
851                         Uri uri10 = new Uri ("mailto:xxx@xxx.com");
852                         Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
853                         AssertEquals ("#11", "", uri10.MakeRelative (uri11));
854                         
855                         Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
856                         AssertEquals ("#12", "mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12));
857                                                 
858                         Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
859                         AssertEquals ("#13", "/foo/bar", uri10.MakeRelative (uri13));
860                         
861                         AssertEquals ("#14", "http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8));
862                 }
863
864                 [Test]
865                 public void RelativeUri ()
866                 {
867                         Uri u = new Uri("http://localhost/../../../a");
868 #if NET_2_0
869                         AssertEquals ("http://localhost/a", u.ToString ());
870 #else
871                         AssertEquals ("http://localhost/../../../a", u.ToString ());
872 #endif
873
874                         u = new Uri ("http://localhost/../c/b/../a");
875 #if NET_2_0
876                         AssertEquals ("http://localhost/c/a", u.ToString ());
877 #else
878                         AssertEquals ("http://localhost/../c/a", u.ToString ());
879 #endif
880                 }
881
882                 [Test]
883                 public void ToStringTest()
884                 {                       
885                         Uri uri = new Uri ("dummy://xxx");
886                         AssertEquals ("#1", "dummy://xxx/", uri.ToString ());
887                 }
888
889                 [Test]
890                 public void CheckSchemeName ()
891                 {
892                         AssertEquals ("#01", false, Uri.CheckSchemeName (null));
893                         AssertEquals ("#02", false, Uri.CheckSchemeName (""));
894                         AssertEquals ("#03", true, Uri.CheckSchemeName ("http"));
895                         AssertEquals ("#04", true, Uri.CheckSchemeName ("http-"));
896                         AssertEquals ("#05", false, Uri.CheckSchemeName ("6http-"));
897                         AssertEquals ("#06", true, Uri.CheckSchemeName ("http6-"));
898                         AssertEquals ("#07", false, Uri.CheckSchemeName ("http6,"));
899                         AssertEquals ("#08", true, Uri.CheckSchemeName ("http6."));
900                         AssertEquals ("#09", false, Uri.CheckSchemeName ("+http"));
901                         AssertEquals ("#10", true, Uri.CheckSchemeName ("htt+p6"));
902                         // 0x00E1 -> &atilde;
903                         AssertEquals ("#11", true, Uri.CheckSchemeName ("htt\u00E1+p6"));
904                 }
905
906                 [Test]
907                 [ExpectedException (typeof (UriFormatException))]
908                 public void NoHostname ()
909                 {
910                         Uri uri = new Uri ("http://");
911                 }
912
913                 [Test]
914 #if !NET_2_0
915                 // MS.NET 1.x throws an IndexOutOfRangeException
916                 [Category("NotDotNet")]
917 #endif
918                 [Ignore("Bug #74144")]
919                 public void NoHostname2 ()
920                 {
921                         Uri uri = new Uri ("file://");
922                         AssertEquals ("#1", true, uri.IsFile);
923                         AssertEquals ("#2", false, uri.IsUnc);
924                         AssertEquals ("#3", "file", uri.Scheme);
925                         AssertEquals ("#4", "/", uri.LocalPath);
926                         AssertEquals ("#5", string.Empty, uri.Query);
927                         AssertEquals ("#6", "/", uri.AbsolutePath);
928                         AssertEquals ("#7", "file:///", uri.AbsoluteUri);
929                         AssertEquals ("#8", string.Empty, uri.Authority);
930                         AssertEquals ("#9", string.Empty, uri.Host);
931                         AssertEquals ("#10", UriHostNameType.Basic, uri.HostNameType);
932                         AssertEquals ("#11", string.Empty, uri.Fragment);
933                         AssertEquals ("#12", true, uri.IsDefaultPort);
934                         AssertEquals ("#13", true, uri.IsLoopback);
935                         AssertEquals ("#14", "/", uri.PathAndQuery);
936                         AssertEquals ("#15", false, uri.UserEscaped);
937                         AssertEquals ("#16", string.Empty, uri.UserInfo);
938                         AssertEquals ("#17", "file://", uri.GetLeftPart (UriPartial.Authority));
939                         AssertEquals ("#18", "file:///", uri.GetLeftPart (UriPartial.Path));
940                         AssertEquals ("#19", "file://", uri.GetLeftPart (UriPartial.Scheme));
941                 }
942
943                 [Test]
944                 public void Segments1 ()
945                 {
946                         Uri uri = new Uri ("http://localhost/");
947                         string [] segments = uri.Segments;
948                         AssertEquals ("#01", 1, segments.Length);
949                         AssertEquals ("#02", "/", segments [0]);
950                         
951                 }
952
953                 [Test]
954                 public void Segments2 ()
955                 {
956                         Uri uri = new Uri ("http://localhost/dir/dummypage.html");
957                         string [] segments = uri.Segments;
958                         AssertEquals ("#01", 3, segments.Length);
959                         AssertEquals ("#02", "/", segments [0]);
960                         AssertEquals ("#03", "dir/", segments [1]);
961                         AssertEquals ("#04", "dummypage.html", segments [2]);
962                         
963                 }
964
965                 [Test]
966                 public void Segments3 ()
967                 {
968                         Uri uri = new Uri ("http://localhost/dir/dummypage/");
969                         string [] segments = uri.Segments;
970                         AssertEquals ("#01", 3, segments.Length);
971                         AssertEquals ("#02", "/", segments [0]);
972                         AssertEquals ("#03", "dir/", segments [1]);
973                         AssertEquals ("#04", "dummypage/", segments [2]);
974                         
975                 }
976
977                 [Test]
978                 public void Segments4 ()
979                 {
980                         Uri uri = new Uri ("file:///c:/hello");
981                         string [] segments = uri.Segments;
982                         AssertEquals ("#01", 3, segments.Length);
983                         AssertEquals ("#02", "c:", segments [0]);
984                         AssertEquals ("#03", "/", segments [1]);
985                         AssertEquals ("#04", "hello", segments [2]);
986                         
987                 }
988
989                 [Test]
990                 public void Segments5 ()
991                 {
992                         Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
993                         string [] segments = uri.Segments;
994                         AssertEquals ("#01", 4, segments.Length);
995                         AssertEquals ("#02", "/", segments [0]);
996                         AssertEquals ("#03", "bar/", segments [1]);
997                         AssertEquals ("#04", "foo/", segments [2]);
998                         AssertEquals ("#05", "foobar.htm", segments [3]);
999                 }
1000
1001                 [Test]
1002                 [ExpectedException (typeof (UriFormatException))]
1003                 public void UriStartingWithColon()
1004                 {
1005                         new Uri("://");
1006                 }
1007
1008                 [Test]
1009                 [ExpectedException (typeof (UriFormatException))]
1010                 public void EmptyScheme ()
1011                 {
1012                         new Uri ("hey");
1013                 }
1014
1015 #if NET_2_0
1016                 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1017                 [ExpectedException (typeof (UriFormatException))]
1018 #endif
1019                 [Test]
1020                 public void InvalidPort1 ()
1021                 {
1022                         Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1023                         AssertEquals (65536, uri.Port);
1024                 }
1025
1026 #if NET_2_0
1027                 [ExpectedException (typeof (UriFormatException))]
1028 #endif
1029                 [Test]
1030                 public void InvalidPort2 ()
1031                 {
1032                         // UInt32.MaxValue gives port == -1 !!!
1033                         Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1034                         AssertEquals (-1, uri.Port);
1035                 }
1036
1037 #if NET_2_0
1038                 [ExpectedException (typeof (UriFormatException))]
1039 #endif
1040                 [Test]
1041                 public void InvalidPort3 ()
1042                 {
1043                         // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1044                         Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1045                         AssertEquals (-2147483648, uri.Port);
1046                 }
1047
1048 #if NET_2_0
1049                 [Test]
1050                 public void PortMax ()
1051                 {
1052                         // on .NET 2.0 a port number is limited to UInt16.MaxValue
1053                         Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1054                         AssertEquals (65535, uri.Port);
1055                 }
1056 #endif
1057
1058                 class UriEx2 : Uri
1059                 {
1060                         public UriEx2 (string s) : base (s)
1061                         {
1062                         }
1063
1064                         protected override void Parse ()
1065                         {
1066                         }
1067                 }
1068
1069 #if NET_2_0
1070                 // Parse method is no longer used on .NET 2.0
1071                 [ExpectedException (typeof (UriFormatException))]
1072 #endif
1073                 [Test]
1074                 public void ParseOverride ()
1075                 {
1076                         // If this does not override base's Parse(), it will
1077                         // fail since this argument is not Absolute URI.
1078                         UriEx2 ex = new UriEx2 ("readme.txt");
1079                 }
1080
1081                 [Test]
1082                 public void UnixLocalPath ()
1083                 {
1084                         // This works--the location is not part of the absolute path
1085                         string path = "file://localhost/tmp/foo/bar";
1086                         Uri fileUri = new Uri( path );
1087                         AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1088
1089                         // Empty path == localhost, in theory
1090                         path = "file:///c:/tmp/foo/bar";
1091                         fileUri = new Uri( path );
1092                         AssertEquals (path, "c:/tmp/foo/bar", fileUri.AbsolutePath);
1093                 }
1094
1095                 // This test doesn't work on Linux, and arguably shouldn't work.
1096                 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar" 
1097                 // on Linux, as anyone sane would expect.  It *doesn't* under .NET 1.1
1098                 // Apparently "tmp" is supposed to be a hostname (!)...
1099                 // Since "correct" behavior would confuse all Linux developers, and having
1100                 // an expected failure is evil, we'll just ignore this for now...
1101                 //
1102                 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1103                 //
1104                 // You are surrounded by conditional-compilation code, all alike.
1105                 // You are likely to be eaten by a Grue...
1106                 [Test]
1107 #if !NET_2_0
1108                 [Category ("NotDotNet")]
1109 #endif
1110                 public void UnixLocalPath_WTF ()
1111                 {
1112                         // Empty path == localhost, in theory
1113                         string path = "file:///tmp/foo/bar";
1114                         Uri fileUri = new Uri( path );
1115 //#if NET_2_0
1116                         AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1117 //#else
1118 //                      AssertEquals (path, "/foo/bar", fileUri.AbsolutePath);
1119 //#endif
1120
1121                         // bug #76643
1122                         string path2 = "file:///foo%25bar";
1123                         fileUri = new Uri (path2);
1124                         AssertEquals (path2, "file:///foo%25bar", fileUri.ToString ());
1125                 }
1126
1127                 public static void Print (Uri uri)
1128                 {
1129                         Console.WriteLine ("ToString: " + uri.ToString ());     
1130
1131                         Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1132                         Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1133                         Console.WriteLine ("Authority: " + uri.Authority);
1134                         Console.WriteLine ("Fragment: " + uri.Fragment);
1135                         Console.WriteLine ("Host: " + uri.Host);
1136                         Console.WriteLine ("HostNameType: " + uri.HostNameType);
1137                         Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1138                         Console.WriteLine ("IsFile: " + uri.IsFile);
1139                         Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1140                         Console.WriteLine ("IsUnc: " + uri.IsUnc);
1141                         Console.WriteLine ("LocalPath: " + uri.LocalPath);
1142                         Console.WriteLine ("PathAndQuery: " + uri.PathAndQuery);
1143                         Console.WriteLine ("Port: " + uri.Port);
1144                         Console.WriteLine ("Query: " + uri.Query);
1145                         Console.WriteLine ("Scheme: " + uri.Scheme);
1146                         Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1147                         Console.WriteLine ("UserInfo: " + uri.UserInfo);
1148
1149                         Console.WriteLine ("Segments:");
1150                         string [] segments = uri.Segments;
1151                         if (segments == null) 
1152                                 Console.WriteLine ("\tNo Segments");
1153                         else 
1154                                 for (int i = 0; i < segments.Length; i++) 
1155                                         Console.WriteLine ("\t" + segments[i]);                                 
1156                         Console.WriteLine ("");
1157                 }
1158
1159         }
1160 }
1161