2005-08-17 Daniel Drake <dsd@gentoo.org>
[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                 
393                 [Test]
394                 public void UnixPath () {
395                         if (!isWin32)
396                                 AssertEquals ("#6a", "file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString ());
397                 }
398                 
399                 [Test]
400                 public void Unc ()
401                 {
402                         Uri uri = new Uri ("http://www.contoso.com");
403                         Assert ("#1", !uri.IsUnc);
404                         
405                         uri = new Uri ("news:123456@contoso.com");
406                         Assert ("#2", !uri.IsUnc);
407
408                         uri = new Uri ("file://server/filename.ext");
409                         Assert ("#3", uri.IsUnc);
410
411                         uri = new Uri (@"\\server\share\filename.ext");                 
412                         Assert ("#6", uri.IsUnc);
413
414                         uri = new Uri (@"a:\dir\filename.ext");
415                         Assert ("#8", !uri.IsUnc);
416                 }
417
418                 [Test]
419                 [Category("NotDotNet")]
420                 public void UncFail ()
421                 {
422                         Uri uri = new Uri ("/home/user/dir/filename.ext");
423                         Assert ("#7", !uri.IsUnc);
424                 }
425
426                 [Test]
427                 public void FromHex () 
428                 {
429                         AssertEquals ("#1", 0, Uri.FromHex ('0'));
430                         AssertEquals ("#2", 9, Uri.FromHex ('9'));
431                         AssertEquals ("#3", 10, Uri.FromHex ('a'));
432                         AssertEquals ("#4", 15, Uri.FromHex ('f'));
433                         AssertEquals ("#5", 10, Uri.FromHex ('A'));
434                         AssertEquals ("#6", 15, Uri.FromHex ('F'));
435                         try {
436                                 Uri.FromHex ('G');
437                                 Fail ("#7");
438                         } catch (ArgumentException) {}
439                         try {
440                                 Uri.FromHex (' ');
441                                 Fail ("#8");
442                         } catch (ArgumentException) {}
443                         try {
444                                 Uri.FromHex ('%');
445                                 Fail ("#8");
446                         } catch (ArgumentException) {}
447                 }
448
449                 class UriEx : Uri
450                 {
451                         public UriEx (string s) : base (s)
452                         {
453                         }
454
455                         public string UnescapeString (string s)
456                         {
457                                 return Unescape (s);
458                         }
459
460                         public static string UnescapeString (string uri, string target)
461                         {
462                                 return new UriEx (uri).UnescapeString (target);
463                         }
464                 }
465
466                 [Test]
467                 public void Unescape ()
468                 {
469                         AssertEquals ("#1", "#", UriEx.UnescapeString ("file://localhost/c#", "%23"));
470                         AssertEquals ("#2", "c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"));
471                         AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"));
472                         AssertEquals ("#1", "#", UriEx.UnescapeString ("http://localhost/c#", "%23"));
473                         AssertEquals ("#2", "c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"));
474                         AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"));
475                 }
476
477                 [Test]
478                 public void HexEscape () 
479                 {
480                         AssertEquals ("#1","%20", Uri.HexEscape (' ')); 
481                         AssertEquals ("#2","%A9", Uri.HexEscape ((char) 0xa9)); 
482                         AssertEquals ("#3","%41", Uri.HexEscape ('A')); 
483                         try {
484                                 Uri.HexEscape ((char) 0x0369);
485                                 Fail ("#4");
486                         } catch (ArgumentOutOfRangeException) {}
487                 }
488
489                 [Test]
490                 public void HexUnescape () 
491                 {
492                         int i = 0;
493                         AssertEquals ("#1", ' ', Uri.HexUnescape ("%20", ref i));
494                         AssertEquals ("#2", 3, i);
495                         i = 4;
496                         AssertEquals ("#3", (char) 0xa9, Uri.HexUnescape ("test%a9test", ref i));
497                         AssertEquals ("#4", 7, i);
498                         AssertEquals ("#5", 't', Uri.HexUnescape ("test%a9test", ref i));
499                         AssertEquals ("#6", 8, i);
500                         i = 4;
501                         AssertEquals ("#5", '%', Uri.HexUnescape ("test%a", ref i));
502                         AssertEquals ("#6", 5, i);
503                         AssertEquals ("#7", '%', Uri.HexUnescape ("testx%xx", ref i));
504                         AssertEquals ("#8", 6, i);
505
506                         // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
507                         i = 1;
508                         AssertEquals ("#9", 227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i));
509                         AssertEquals ("#10", 4, i);
510                         i = 1;
511                         AssertEquals ("#11", 240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i));
512                         AssertEquals ("#12", 4, i);
513                 }
514
515 #if !NET_2_0
516                 // These won't pass exactly with MS.NET 1.x, due to differences in the
517                 // handling of backslashes/forwardslashes
518                 [Category ("NotDotNet")]
519 #endif
520                 [Test]
521                 public void HexUnescapeMultiByte () 
522                 {
523                         // Tests from bug 74872
524                         // Note: These won't pass exactly with MS.NET, due to differences in the
525                         // handling of backslashes/forwardslashes
526                         Uri uri;
527                         string path;
528
529                         // 3-byte character
530                         uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
531                         path = uri.LocalPath;
532                         AssertEquals ("#1", 8, path.Length);
533                         AssertEquals ("#2", 0x304B, path [6]);
534
535                         // 4-byte character which should be handled as a surrogate
536                         uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
537                         path = uri.LocalPath;
538                         AssertEquals ("#3", 9, path.Length);
539                         AssertEquals ("#4", 0xDB40, path [6]);
540                         AssertEquals ("#5", 0xDD00, path [7]);
541                         AssertEquals ("#6", 0x62, path [8]);
542                         
543                         // 2-byte escape sequence, 2 individual characters
544                         uri = new Uri ("file:///foo/a%C2%F8b", true);
545                         path = uri.LocalPath;
546                         AssertEquals ("#7", 9, path.Length);
547                         AssertEquals ("#8", 0xC2, path [6]);
548                         AssertEquals ("#9", 0xF8, path [7]);                    
549                 }
550
551                 [Test]
552                 public void IsHexDigit () 
553                 {
554                         Assert ("#1", Uri.IsHexDigit ('a'));    
555                         Assert ("#2", Uri.IsHexDigit ('f'));
556                         Assert ("#3", !Uri.IsHexDigit ('g'));
557                         Assert ("#4", Uri.IsHexDigit ('0'));
558                         Assert ("#5", Uri.IsHexDigit ('9'));
559                         Assert ("#6", Uri.IsHexDigit ('A'));
560                         Assert ("#7", Uri.IsHexDigit ('F'));
561                         Assert ("#8", !Uri.IsHexDigit ('G'));
562                 }
563
564                 [Test]
565                 public void IsHexEncoding () 
566                 {
567                         Assert ("#1", Uri.IsHexEncoding ("test%a9test", 4));
568                         Assert ("#2", !Uri.IsHexEncoding ("test%a9test", 3));
569                         Assert ("#3", Uri.IsHexEncoding ("test%a9", 4));
570                         Assert ("#4", !Uri.IsHexEncoding ("test%a", 4));
571                 }
572                 
573                 [Test]
574                 public void GetLeftPart ()
575                 {
576                         Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
577                         AssertEquals ("#1", "http://", uri.GetLeftPart (UriPartial.Scheme));
578                         AssertEquals ("#2", "http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority));
579                         AssertEquals ("#3", "http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path));
580                         
581                         uri = new Uri ("mailto:user@contoso.com?subject=uri");
582                         AssertEquals ("#4", "mailto:", uri.GetLeftPart (UriPartial.Scheme));
583                         AssertEquals ("#5", "", uri.GetLeftPart (UriPartial.Authority));
584                         AssertEquals ("#6", "mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path));
585
586                         uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
587                         AssertEquals ("#7", "nntp://", uri.GetLeftPart (UriPartial.Scheme));
588                         AssertEquals ("#8", "nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority));
589                         AssertEquals ("#9", "nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path));                   
590                         
591                         uri = new Uri ("news:123456@contoso.com");
592                         AssertEquals ("#10", "news:", uri.GetLeftPart (UriPartial.Scheme));
593                         AssertEquals ("#11", "", uri.GetLeftPart (UriPartial.Authority));
594                         AssertEquals ("#12", "news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path));                     
595
596                         uri = new Uri ("file://server/filename.ext");
597                         AssertEquals ("#13", "file://", uri.GetLeftPart (UriPartial.Scheme));
598                         AssertEquals ("#14", "file://server", uri.GetLeftPart (UriPartial.Authority));
599                         AssertEquals ("#15", "file://server/filename.ext", uri.GetLeftPart (UriPartial.Path));                  
600
601                         uri = new Uri (@"\\server\share\filename.ext");
602                         AssertEquals ("#20", "file://", uri.GetLeftPart (UriPartial.Scheme));
603                         AssertEquals ("#21", "file://server", uri.GetLeftPart (UriPartial.Authority));
604                         AssertEquals ("#22", "file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path));
605                         
606                         uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
607                         AssertEquals ("#23", "http://", uri.GetLeftPart (UriPartial.Scheme));
608                         AssertEquals ("#24", "http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority));
609                         AssertEquals ("#25", "http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path));
610                 }
611
612                 [Test]
613                 public void NewsDefaultPort ()
614                 {
615                         Uri uri = new Uri("news://localhost:119/");
616                         AssertEquals ("#1", uri.IsDefaultPort, true);
617                 }
618
619                 [Test]
620                 public void FragmentEscape ()
621                 {
622                         Uri u = new Uri("http://localhost/index.asp#main#start", false);
623                         AssertEquals ("#1", u.Fragment, "#main%23start");
624
625                         u = new Uri("http://localhost/index.asp#main#start", true);
626                         AssertEquals ("#2", u.Fragment, "#main#start");
627
628                         // The other code path uses a BaseUri
629
630                         Uri b = new Uri ("http://www.gnome.org");
631                         Uri n = new Uri (b, "blah#main#start");
632                         AssertEquals ("#3", n.Fragment, "#main%23start");
633                         
634                         n = new Uri (b, "blah#main#start", true);
635                         AssertEquals ("#3", n.Fragment, "#main#start");
636                 }
637                         
638                 [Test]
639                 [ExpectedException(typeof(UriFormatException))]
640                 public void IncompleteSchemeDelimiter ()
641                 {
642                         new Uri ("file:/filename.ext");
643                 }
644
645                 [Test]
646                 [Category("NotDotNet")]
647                 public void CheckHostName1 ()
648                 {
649                         // reported to MSDN Product Feedback Center (FDBK28671)
650                         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"));
651                 }
652
653                 [Test]
654                 public void CheckHostName2 ()
655                 {
656                         AssertEquals ("#1", UriHostNameType.Unknown, Uri.CheckHostName (null));
657                         AssertEquals ("#2", UriHostNameType.Unknown, Uri.CheckHostName (""));
658                         AssertEquals ("#3", UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"));
659                         AssertEquals ("#4", UriHostNameType.Dns, Uri.CheckHostName ("x"));
660                         AssertEquals ("#5", UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"));
661                         AssertEquals ("#6", UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"));
662                         AssertEquals ("#7", UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"));
663                         AssertEquals ("#8", UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"));
664                         AssertEquals ("#9", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"));
665                         AssertEquals ("#10", UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"));
666                         AssertEquals ("#11", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."));
667                         AssertEquals ("#12", UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"));      
668                         AssertEquals ("#13", UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"));      
669                         AssertEquals ("#14", UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"));  
670                         
671                         // test IPv6
672                         AssertEquals ("#15", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"));
673                         AssertEquals ("#16", UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"));
674                         AssertEquals ("#17", UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"));
675                         AssertEquals ("#18", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"));
676                         AssertEquals ("#19", UriHostNameType.IPv6, Uri.CheckHostName ("11::88"));
677                         AssertEquals ("#20", UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"));
678                         AssertEquals ("#21", UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"));
679                         AssertEquals ("#22", UriHostNameType.IPv6, Uri.CheckHostName ("11::"));
680                         AssertEquals ("#23", UriHostNameType.IPv6, Uri.CheckHostName ("::88"));
681                         AssertEquals ("#24", UriHostNameType.IPv6, Uri.CheckHostName ("::1"));
682                         AssertEquals ("#25", UriHostNameType.IPv6, Uri.CheckHostName ("::"));
683                         AssertEquals ("#26", UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"));
684                         AssertEquals ("#27", UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"));
685                         AssertEquals ("#28", UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"));
686                         AssertEquals ("#29", UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"));
687                         AssertEquals ("#30", UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"));
688                         AssertEquals ("#33", UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"));
689
690                         AssertEquals ("#31", UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"));
691                         AssertEquals ("#32", UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"));
692                         AssertEquals ("#34", UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"));
693                         AssertEquals ("#35", UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"));
694                         AssertEquals ("#37", UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"));
695                         AssertEquals ("#38", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"));
696                         AssertEquals ("#39", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"));
697                         AssertEquals ("#40", UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"));
698                         AssertEquals ("#41", UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"));
699                         AssertEquals ("#42", UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"));
700                         AssertEquals ("#43", UriHostNameType.Unknown, Uri.CheckHostName (":::"));
701                         AssertEquals ("#44", UriHostNameType.Unknown, Uri.CheckHostName (":"));
702                 }
703                 
704                 [Test]
705                 public void IsLoopback ()
706                 {
707                         Uri uri = new Uri("http://loopback:8080");
708                         AssertEquals ("#1", true, uri.IsLoopback);
709                         uri = new Uri("http://localhost:8080");
710                         AssertEquals ("#2", true, uri.IsLoopback);
711                         uri = new Uri("http://127.0.0.1:8080");
712                         AssertEquals ("#3", true, uri.IsLoopback);
713                         uri = new Uri("http://127.0.0.001:8080");
714                         AssertEquals ("#4", true, uri.IsLoopback);
715                         uri = new Uri("http://[::1]");
716                         AssertEquals ("#5", true, uri.IsLoopback);
717                         uri = new Uri("http://[::1]:8080");
718                         AssertEquals ("#6", true, uri.IsLoopback);
719                         uri = new Uri("http://[::0001]:8080");
720                         AssertEquals ("#7", true, uri.IsLoopback);
721                         uri = new Uri("http://[0:0:0:0::1]:8080");
722                         AssertEquals ("#8", true, uri.IsLoopback);
723                         uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
724                         AssertEquals ("#9", true, uri.IsLoopback);
725                         uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
726                         AssertEquals ("#10", false, uri.IsLoopback);
727                         uri = new Uri("http://[::ffff:127.11.22.33]:8080");
728                         AssertEquals ("#11", false, uri.IsLoopback);
729                         uri = new Uri("http://[::ff00:7f11:2233]:8080");
730                         AssertEquals ("#12", false, uri.IsLoopback);
731                         uri = new Uri("http://[1:0:0:0::1]:8080");
732                         AssertEquals ("#13", false, uri.IsLoopback);
733                 }
734                 
735                 [Test]
736                 public void Equals1 ()
737                 {
738                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
739                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
740                         Assert ("#1", uri1.Equals (uri2));
741                         Assert ("#3", !uri2.Equals ("http://www.contoso.com/index.html?x=1"));
742                         Assert ("#4", !uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"));
743                 }
744
745                 [Test]
746 #if !NET_2_0
747                 [Category("NotDotNet")]
748 #endif
749                 public void Equals2 ()
750                 {
751                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
752                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
753                         Assert ("#2 known to fail with ms.net 1.x", !uri1.Equals (uri2));
754                 }
755
756                 [Test]
757                 public void TestEquals2 ()
758                 {
759                         Uri a = new Uri ("http://www.go-mono.com");
760                         Uri b = new Uri ("http://www.go-mono.com");
761
762                         AssertEquals ("#1", a, b);
763
764                         a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
765                         b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
766
767                         AssertEquals ("#2", a, b);
768
769                         a = new Uri ("http://www.go-mono.com/ports/");
770                         b = new Uri ("http://www.go-mono.com/PORTS/");
771
772                         Assert ("#3", !a.Equals (b));
773                 }
774
775                 [Test]
776                 public void GetHashCodeTest () 
777                 {
778                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
779                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
780                         AssertEquals ("#1", uri1.GetHashCode (), uri2.GetHashCode ());
781                         uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
782                         Assert ("#2", uri1.GetHashCode () != uri2.GetHashCode ());
783                         uri2 = new Uri ("http://www.contoso.com:80/index.htm");
784                         AssertEquals ("#3", uri1.GetHashCode (), uri2.GetHashCode ());                  
785                         uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
786                         Assert ("#4", uri1.GetHashCode () != uri2.GetHashCode ());                      
787                 }
788                 
789                 [Test]
790                 public void MakeRelative ()
791                 {
792                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
793                         Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
794                         Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
795                         Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
796                         Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
797                         Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
798                         Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
799                         Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
800
801                         AssertEquals ("#1", "foo/bar/index.htm", uri1.MakeRelative (uri2));
802                         AssertEquals ("#2", "../../index.htm", uri2.MakeRelative (uri1));
803                         
804                         AssertEquals ("#3", "../../bar/foo/index.htm", uri2.MakeRelative (uri3));
805                         AssertEquals ("#4", "../../foo/bar/index.htm", uri3.MakeRelative (uri2));                       
806
807                         AssertEquals ("#5", "../foo2/index.htm", uri3.MakeRelative (uri4));
808                         AssertEquals ("#6", "../foo/index.htm", uri4.MakeRelative (uri3));
809                         
810                         AssertEquals ("#7", "https://www.contoso.com/bar/foo/index.htm?y=1", 
811                                             uri4.MakeRelative (uri5));
812
813                         AssertEquals ("#8", "http://www.contoso2.com/bar/foo/index.htm?x=0", 
814                                             uri4.MakeRelative (uri6));
815
816                         AssertEquals ("#9", "", uri6.MakeRelative (uri6));
817                         AssertEquals ("#10", "foobar.htm", uri6.MakeRelative (uri7));
818                         
819                         Uri uri10 = new Uri ("mailto:xxx@xxx.com");
820                         Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
821                         AssertEquals ("#11", "", uri10.MakeRelative (uri11));
822                         
823                         Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
824                         AssertEquals ("#12", "mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12));
825                                                 
826                         Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
827                         AssertEquals ("#13", "/foo/bar", uri10.MakeRelative (uri13));
828                         
829                         AssertEquals ("#14", "http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8));
830                 }
831
832                 [Test]
833                 public void RelativeUri ()
834                 {
835                         Uri u = new Uri("http://localhost/../../../a");
836 #if NET_2_0
837                         AssertEquals ("http://localhost/a", u.ToString ());
838 #else
839                         AssertEquals ("http://localhost/../../../a", u.ToString ());
840 #endif
841
842                         u = new Uri ("http://localhost/../c/b/../a");
843 #if NET_2_0
844                         AssertEquals ("http://localhost/c/a", u.ToString ());
845 #else
846                         AssertEquals ("http://localhost/../c/a", u.ToString ());
847 #endif
848                 }
849
850                 [Test]
851                 public void ToStringTest()
852                 {                       
853                         Uri uri = new Uri ("dummy://xxx");
854                         AssertEquals ("#1", "dummy://xxx/", uri.ToString ());
855                 }
856
857                 [Test]
858                 public void CheckSchemeName ()
859                 {
860                         AssertEquals ("#01", false, Uri.CheckSchemeName (null));
861                         AssertEquals ("#02", false, Uri.CheckSchemeName (""));
862                         AssertEquals ("#03", true, Uri.CheckSchemeName ("http"));
863                         AssertEquals ("#04", true, Uri.CheckSchemeName ("http-"));
864                         AssertEquals ("#05", false, Uri.CheckSchemeName ("6http-"));
865                         AssertEquals ("#06", true, Uri.CheckSchemeName ("http6-"));
866                         AssertEquals ("#07", false, Uri.CheckSchemeName ("http6,"));
867                         AssertEquals ("#08", true, Uri.CheckSchemeName ("http6."));
868                         AssertEquals ("#09", false, Uri.CheckSchemeName ("+http"));
869                         AssertEquals ("#10", true, Uri.CheckSchemeName ("htt+p6"));
870                         // 0x00E1 -> &atilde;
871                         AssertEquals ("#11", true, Uri.CheckSchemeName ("htt\u00E1+p6"));
872                 }
873
874                 [Test]
875                 [ExpectedException (typeof (UriFormatException))]
876                 public void NoHostname ()
877                 {
878                         Uri uri = new Uri ("http://");
879                 }
880
881                 [Test]
882 #if !NET_2_0
883                 // MS.NET 1.x throws an IndexOutOfRangeException
884                 [Category("NotDotNet")]
885 #endif
886                 [Ignore("Bug #74144")]
887                 public void NoHostname2 ()
888                 {
889                         Uri uri = new Uri ("file://");
890                         AssertEquals ("#1", true, uri.IsFile);
891                         AssertEquals ("#2", false, uri.IsUnc);
892                         AssertEquals ("#3", "file", uri.Scheme);
893                         AssertEquals ("#4", "/", uri.LocalPath);
894                         AssertEquals ("#5", string.Empty, uri.Query);
895                         AssertEquals ("#6", "/", uri.AbsolutePath);
896                         AssertEquals ("#7", "file:///", uri.AbsoluteUri);
897                         AssertEquals ("#8", string.Empty, uri.Authority);
898                         AssertEquals ("#9", string.Empty, uri.Host);
899                         AssertEquals ("#10", UriHostNameType.Basic, uri.HostNameType);
900                         AssertEquals ("#11", string.Empty, uri.Fragment);
901                         AssertEquals ("#12", true, uri.IsDefaultPort);
902                         AssertEquals ("#13", true, uri.IsLoopback);
903                         AssertEquals ("#14", "/", uri.PathAndQuery);
904                         AssertEquals ("#15", false, uri.UserEscaped);
905                         AssertEquals ("#16", string.Empty, uri.UserInfo);
906                         AssertEquals ("#17", "file://", uri.GetLeftPart (UriPartial.Authority));
907                         AssertEquals ("#18", "file:///", uri.GetLeftPart (UriPartial.Path));
908                         AssertEquals ("#19", "file://", uri.GetLeftPart (UriPartial.Scheme));
909                 }
910
911                 [Test]
912                 public void Segments1 ()
913                 {
914                         Uri uri = new Uri ("http://localhost/");
915                         string [] segments = uri.Segments;
916                         AssertEquals ("#01", 1, segments.Length);
917                         AssertEquals ("#02", "/", segments [0]);
918                         
919                 }
920
921                 [Test]
922                 public void Segments2 ()
923                 {
924                         Uri uri = new Uri ("http://localhost/dir/dummypage.html");
925                         string [] segments = uri.Segments;
926                         AssertEquals ("#01", 3, segments.Length);
927                         AssertEquals ("#02", "/", segments [0]);
928                         AssertEquals ("#03", "dir/", segments [1]);
929                         AssertEquals ("#04", "dummypage.html", segments [2]);
930                         
931                 }
932
933                 [Test]
934                 public void Segments3 ()
935                 {
936                         Uri uri = new Uri ("http://localhost/dir/dummypage/");
937                         string [] segments = uri.Segments;
938                         AssertEquals ("#01", 3, segments.Length);
939                         AssertEquals ("#02", "/", segments [0]);
940                         AssertEquals ("#03", "dir/", segments [1]);
941                         AssertEquals ("#04", "dummypage/", segments [2]);
942                         
943                 }
944
945                 [Test]
946                 public void Segments4 ()
947                 {
948                         Uri uri = new Uri ("file:///c:/hello");
949                         string [] segments = uri.Segments;
950                         AssertEquals ("#01", 3, segments.Length);
951                         AssertEquals ("#02", "c:", segments [0]);
952                         AssertEquals ("#03", "/", segments [1]);
953                         AssertEquals ("#04", "hello", segments [2]);
954                         
955                 }
956
957                 [Test]
958                 public void Segments5 ()
959                 {
960                         Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
961                         string [] segments = uri.Segments;
962                         AssertEquals ("#01", 4, segments.Length);
963                         AssertEquals ("#02", "/", segments [0]);
964                         AssertEquals ("#03", "bar/", segments [1]);
965                         AssertEquals ("#04", "foo/", segments [2]);
966                         AssertEquals ("#05", "foobar.htm", segments [3]);
967                 }
968
969                 [Test]
970                 [ExpectedException (typeof (UriFormatException))]
971                 public void UriStartingWithColon()
972                 {
973                         new Uri("://");
974                 }
975
976                 [Test]
977                 [ExpectedException (typeof (UriFormatException))]
978                 public void EmptyScheme ()
979                 {
980                         new Uri ("hey");
981                 }
982
983 #if NET_2_0
984                 // on .NET 2.0 a port number is limited to UInt16.MaxValue
985                 [ExpectedException (typeof (UriFormatException))]
986 #endif
987                 [Test]
988                 public void InvalidPort1 ()
989                 {
990                         Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
991                         AssertEquals (65536, uri.Port);
992                 }
993
994 #if NET_2_0
995                 [ExpectedException (typeof (UriFormatException))]
996 #endif
997                 [Test]
998                 public void InvalidPort2 ()
999                 {
1000                         // UInt32.MaxValue gives port == -1 !!!
1001                         Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1002                         AssertEquals (-1, uri.Port);
1003                 }
1004
1005 #if NET_2_0
1006                 [ExpectedException (typeof (UriFormatException))]
1007 #endif
1008                 [Test]
1009                 public void InvalidPort3 ()
1010                 {
1011                         // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1012                         Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1013                         AssertEquals (-2147483648, uri.Port);
1014                 }
1015
1016 #if NET_2_0
1017                 [Test]
1018                 public void PortMax ()
1019                 {
1020                         // on .NET 2.0 a port number is limited to UInt16.MaxValue
1021                         Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1022                         AssertEquals (65535, uri.Port);
1023                 }
1024 #endif
1025
1026                 class UriEx2 : Uri
1027                 {
1028                         public UriEx2 (string s) : base (s)
1029                         {
1030                         }
1031
1032                         protected override void Parse ()
1033                         {
1034                         }
1035                 }
1036
1037 #if NET_2_0
1038                 // Parse method is no longer used on .NET 2.0
1039                 [ExpectedException (typeof (UriFormatException))]
1040 #endif
1041                 [Test]
1042                 public void ParseOverride ()
1043                 {
1044                         // If this does not override base's Parse(), it will
1045                         // fail since this argument is not Absolute URI.
1046                         UriEx2 ex = new UriEx2 ("readme.txt");
1047                 }
1048
1049                 [Test]
1050                 public void UnixLocalPath ()
1051                 {
1052                         // This works--the location is not part of the absolute path
1053                         string path = "file://localhost/tmp/foo/bar";
1054                         Uri fileUri = new Uri( path );
1055                         AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1056
1057                         // Empty path == localhost, in theory
1058                         path = "file:///c:/tmp/foo/bar";
1059                         fileUri = new Uri( path );
1060                         AssertEquals (path, "c:/tmp/foo/bar", fileUri.AbsolutePath);
1061                 }
1062
1063                 // This test doesn't work on Linux, and arguably shouldn't work.
1064                 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar" 
1065                 // on Linux, as anyone sane would expect.  It *doesn't* under .NET 1.1
1066                 // Apparently "tmp" is supposed to be a hostname (!)...
1067                 // Since "correct" behavior would confuse all Linux developers, and having
1068                 // an expected failure is evil, we'll just ignore this for now...
1069                 //
1070                 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1071                 //
1072                 // You are surrounded by conditional-compilation code, all alike.
1073                 // You are likely to be eaten by a Grue...
1074                 [Test]
1075 #if ONLY_1_1
1076                 [Category ("NotWorking")]
1077 #endif
1078                 public void UnixLocalPath_WTF ()
1079                 {
1080                         // Empty path == localhost, in theory
1081                         string path = "file:///tmp/foo/bar";
1082                         Uri fileUri = new Uri( path );
1083 #if NET_2_0
1084                         AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1085 #else
1086                         AssertEquals (path, "/foo/bar", fileUri.AbsolutePath);
1087 #endif
1088                 }
1089
1090                 public static void Print (Uri uri)
1091                 {
1092                         Console.WriteLine ("ToString: " + uri.ToString ());     
1093
1094                         Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1095                         Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1096                         Console.WriteLine ("Authority: " + uri.Authority);
1097                         Console.WriteLine ("Fragment: " + uri.Fragment);
1098                         Console.WriteLine ("Host: " + uri.Host);
1099                         Console.WriteLine ("HostNameType: " + uri.HostNameType);
1100                         Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1101                         Console.WriteLine ("IsFile: " + uri.IsFile);
1102                         Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1103                         Console.WriteLine ("IsUnc: " + uri.IsUnc);
1104                         Console.WriteLine ("LocalPath: " + uri.LocalPath);
1105                         Console.WriteLine ("PathAndQuery: " + uri.PathAndQuery);
1106                         Console.WriteLine ("Port: " + uri.Port);
1107                         Console.WriteLine ("Query: " + uri.Query);
1108                         Console.WriteLine ("Scheme: " + uri.Scheme);
1109                         Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1110                         Console.WriteLine ("UserInfo: " + uri.UserInfo);
1111
1112                         Console.WriteLine ("Segments:");
1113                         string [] segments = uri.Segments;
1114                         if (segments == null) 
1115                                 Console.WriteLine ("\tNo Segments");
1116                         else 
1117                                 for (int i = 0; i < segments.Length; i++) 
1118                                         Console.WriteLine ("\t" + segments[i]);                                 
1119                         Console.WriteLine ("");
1120                 }
1121
1122         }
1123 }
1124