* Uri.cs: Allow domain address with trailing period to indicate that the
[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                 [ExpectedException (typeof (UriFormatException))]
201                 public void InvalidScheme ()
202                 {
203                         new Uri ("_:/");
204                 }
205
206                 [Test]
207                 public void ConstructorsRejectRelativePath ()
208                 {
209                         string [] reluris = new string [] {
210                                 "readme.txt",
211                                 "thisdir/childdir/file",
212                                 "./testfile"
213                         };
214                         string [] winRelUris = new string [] {
215                                 "c:readme.txt"
216                         };
217
218                         for (int i = 0; i < reluris.Length; i++) {
219                                 try {
220                                         new Uri (reluris [i]);
221                                         Fail ("Should be failed: " + reluris [i]);
222                                 } catch (UriFormatException) {
223                                 }
224                         }
225
226                         if (isWin32) {
227                                 for (int i = 0; i < winRelUris.Length; i++) {
228                                         try {
229                                                 new Uri (winRelUris [i]);
230                                                 Fail ("Should be failed: " + winRelUris [i]);
231                                         } catch (UriFormatException) {
232                                         }
233                                 }
234                         }
235                 }
236
237
238                 [Test]
239                 public void LocalPath ()
240                 {
241                         Uri uri = new Uri ("c:\\tmp\\hello.txt");
242                         AssertEquals ("#1a", "file:///c:/tmp/hello.txt", uri.ToString ());
243                         AssertEquals ("#1b", "c:\\tmp\\hello.txt", uri.LocalPath);
244                         AssertEquals ("#1c", "file", uri.Scheme);
245                         AssertEquals ("#1d", "", uri.Host);
246                         AssertEquals ("#1e", "c:/tmp/hello.txt", uri.AbsolutePath);
247                                         
248                         uri = new Uri ("file:////////cygwin/tmp/hello.txt");
249                         AssertEquals ("#3a", "file://cygwin/tmp/hello.txt", uri.ToString ());
250                         if (isWin32)
251                                 AssertEquals ("#3b win32", "\\\\cygwin\\tmp\\hello.txt", uri.LocalPath);
252                         else
253                                 AssertEquals ("#3b *nix", "/tmp/hello.txt", uri.LocalPath);
254                         AssertEquals ("#3c", "file", uri.Scheme);
255                         AssertEquals ("#3d", "cygwin", uri.Host);
256                         AssertEquals ("#3e", "/tmp/hello.txt", uri.AbsolutePath);
257
258                         uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
259                         AssertEquals ("#4a", "file://mymachine/cygwin/tmp/hello.txt", uri.ToString ());
260                         if (isWin32)
261                                 AssertEquals ("#4b win32", "\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath);
262                         else
263                                 AssertEquals ("#4b *nix", "/cygwin/tmp/hello.txt", uri.LocalPath);
264                         AssertEquals ("#4c", "file", uri.Scheme);
265                         AssertEquals ("#4d", "mymachine", uri.Host);
266                         AssertEquals ("#4e", "/cygwin/tmp/hello.txt", uri.AbsolutePath);
267                         
268                         uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
269                         AssertEquals ("#5a", "file:///c:/cygwin/tmp/hello.txt", uri.ToString ());
270                         AssertEquals ("#5b", "c:\\cygwin\\tmp\\hello.txt", uri.LocalPath);
271                         AssertEquals ("#5c", "file", uri.Scheme);
272                         AssertEquals ("#5d", "", uri.Host);
273                         AssertEquals ("#5e", "c:/cygwin/tmp/hello.txt", uri.AbsolutePath);
274                         // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
275                         uri = new Uri("file://one_file.txt");
276                         AssertEquals("#6a", "file://one_file.txt", uri.ToString());
277                         if (isWin32)
278                                 AssertEquals("#6b", "\\\\one_file.txt", uri.LocalPath);
279                         else
280                                 AssertEquals("#6b", "/", uri.LocalPath);
281                         AssertEquals("#6c", "file", uri.Scheme);
282                         AssertEquals("#6d", "one_file.txt", uri.Host);
283                         AssertEquals("#6e", "", uri.AbsolutePath);
284                 }
285                 
286                 [Test]
287                 public void UnixPath () {
288                         if (!isWin32)
289                                 AssertEquals ("#6a", "file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString ());
290                 }
291                 
292                 [Test]
293                 public void Unc ()
294                 {
295                         Uri uri = new Uri ("http://www.contoso.com");
296                         Assert ("#1", !uri.IsUnc);
297                         
298                         uri = new Uri ("news:123456@contoso.com");
299                         Assert ("#2", !uri.IsUnc);
300
301                         uri = new Uri ("file://server/filename.ext");
302                         Assert ("#3", uri.IsUnc);
303
304                         uri = new Uri (@"\\server\share\filename.ext");                 
305                         Assert ("#6", uri.IsUnc);
306
307                         uri = new Uri (@"a:\dir\filename.ext");
308                         Assert ("#8", !uri.IsUnc);
309                 }
310
311                 [Test]
312                 [Category("NotDotNet")]
313                 public void UncFail ()
314                 {
315                         Uri uri = new Uri ("/home/user/dir/filename.ext");
316                         Assert ("#7", !uri.IsUnc);
317                 }
318
319                 [Test]
320                 public void FromHex () 
321                 {
322                         AssertEquals ("#1", 0, Uri.FromHex ('0'));
323                         AssertEquals ("#2", 9, Uri.FromHex ('9'));
324                         AssertEquals ("#3", 10, Uri.FromHex ('a'));
325                         AssertEquals ("#4", 15, Uri.FromHex ('f'));
326                         AssertEquals ("#5", 10, Uri.FromHex ('A'));
327                         AssertEquals ("#6", 15, Uri.FromHex ('F'));
328                         try {
329                                 Uri.FromHex ('G');
330                                 Fail ("#7");
331                         } catch (ArgumentException) {}
332                         try {
333                                 Uri.FromHex (' ');
334                                 Fail ("#8");
335                         } catch (ArgumentException) {}
336                         try {
337                                 Uri.FromHex ('%');
338                                 Fail ("#8");
339                         } catch (ArgumentException) {}
340                 }
341
342                 class UriEx : Uri
343                 {
344                         public UriEx (string s) : base (s)
345                         {
346                         }
347
348                         public string UnescapeString (string s)
349                         {
350                                 return Unescape (s);
351                         }
352
353                         public static string UnescapeString (string uri, string target)
354                         {
355                                 return new UriEx (uri).UnescapeString (target);
356                         }
357                 }
358
359                 [Test]
360                 public void Unescape ()
361                 {
362                         AssertEquals ("#1", "#", UriEx.UnescapeString ("file://localhost/c#", "%23"));
363                         AssertEquals ("#2", "c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"));
364                         AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"));
365                         AssertEquals ("#1", "#", UriEx.UnescapeString ("http://localhost/c#", "%23"));
366                         AssertEquals ("#2", "c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"));
367                         AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"));
368                 }
369
370                 [Test]
371                 public void HexEscape () 
372                 {
373                         AssertEquals ("#1","%20", Uri.HexEscape (' ')); 
374                         AssertEquals ("#2","%A9", Uri.HexEscape ((char) 0xa9)); 
375                         AssertEquals ("#3","%41", Uri.HexEscape ('A')); 
376                         try {
377                                 Uri.HexEscape ((char) 0x0369);
378                                 Fail ("#4");
379                         } catch (ArgumentOutOfRangeException) {}
380                 }
381
382                 [Test]
383                 public void HexUnescape () 
384                 {
385                         int i = 0;
386                         AssertEquals ("#1", ' ', Uri.HexUnescape ("%20", ref i));
387                         AssertEquals ("#2", 3, i);
388                         i = 4;
389                         AssertEquals ("#3", (char) 0xa9, Uri.HexUnescape ("test%a9test", ref i));
390                         AssertEquals ("#4", 7, i);
391                         AssertEquals ("#5", 't', Uri.HexUnescape ("test%a9test", ref i));
392                         AssertEquals ("#6", 8, i);
393                         i = 4;
394                         AssertEquals ("#5", '%', Uri.HexUnescape ("test%a", ref i));
395                         AssertEquals ("#6", 5, i);
396                         AssertEquals ("#7", '%', Uri.HexUnescape ("testx%xx", ref i));
397                         AssertEquals ("#8", 6, i);
398                 }
399
400                 [Test]
401                 public void IsHexDigit () 
402                 {
403                         Assert ("#1", Uri.IsHexDigit ('a'));    
404                         Assert ("#2", Uri.IsHexDigit ('f'));
405                         Assert ("#3", !Uri.IsHexDigit ('g'));
406                         Assert ("#4", Uri.IsHexDigit ('0'));
407                         Assert ("#5", Uri.IsHexDigit ('9'));
408                         Assert ("#6", Uri.IsHexDigit ('A'));
409                         Assert ("#7", Uri.IsHexDigit ('F'));
410                         Assert ("#8", !Uri.IsHexDigit ('G'));
411                 }
412
413                 [Test]
414                 public void IsHexEncoding () 
415                 {
416                         Assert ("#1", Uri.IsHexEncoding ("test%a9test", 4));
417                         Assert ("#2", !Uri.IsHexEncoding ("test%a9test", 3));
418                         Assert ("#3", Uri.IsHexEncoding ("test%a9", 4));
419                         Assert ("#4", !Uri.IsHexEncoding ("test%a", 4));
420                 }
421                 
422                 [Test]
423                 public void GetLeftPart ()
424                 {
425                         Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
426                         AssertEquals ("#1", "http://", uri.GetLeftPart (UriPartial.Scheme));
427                         AssertEquals ("#2", "http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority));
428                         AssertEquals ("#3", "http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path));
429                         
430                         uri = new Uri ("mailto:user@contoso.com?subject=uri");
431                         AssertEquals ("#4", "mailto:", uri.GetLeftPart (UriPartial.Scheme));
432                         AssertEquals ("#5", "", uri.GetLeftPart (UriPartial.Authority));
433                         AssertEquals ("#6", "mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path));
434
435                         uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
436                         AssertEquals ("#7", "nntp://", uri.GetLeftPart (UriPartial.Scheme));
437                         AssertEquals ("#8", "nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority));
438                         AssertEquals ("#9", "nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path));                   
439                         
440                         uri = new Uri ("news:123456@contoso.com");
441                         AssertEquals ("#10", "news:", uri.GetLeftPart (UriPartial.Scheme));
442                         AssertEquals ("#11", "", uri.GetLeftPart (UriPartial.Authority));
443                         AssertEquals ("#12", "news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path));                     
444
445                         uri = new Uri ("file://server/filename.ext");
446                         AssertEquals ("#13", "file://", uri.GetLeftPart (UriPartial.Scheme));
447                         AssertEquals ("#14", "file://server", uri.GetLeftPart (UriPartial.Authority));
448                         AssertEquals ("#15", "file://server/filename.ext", uri.GetLeftPart (UriPartial.Path));                  
449
450                         uri = new Uri (@"\\server\share\filename.ext");
451                         AssertEquals ("#20", "file://", uri.GetLeftPart (UriPartial.Scheme));
452                         AssertEquals ("#21", "file://server", uri.GetLeftPart (UriPartial.Authority));
453                         AssertEquals ("#22", "file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path));
454                         
455                         uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
456                         AssertEquals ("#23", "http://", uri.GetLeftPart (UriPartial.Scheme));
457                         AssertEquals ("#24", "http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority));
458                         AssertEquals ("#25", "http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path));
459                 }
460
461                 [Test]
462                 public void NewsDefaultPort ()
463                 {
464                         Uri uri = new Uri("news://localhost:119/");
465                         AssertEquals ("#1", uri.IsDefaultPort, true);
466                 }
467
468                 [Test]
469                 public void FragmentEscape ()
470                 {
471                         Uri u = new Uri("http://localhost/index.asp#main#start", false);
472                         AssertEquals ("#1", u.Fragment, "#main%23start");
473
474                         u = new Uri("http://localhost/index.asp#main#start", true);
475                         AssertEquals ("#2", u.Fragment, "#main#start");
476
477                         // The other code path uses a BaseUri
478
479                         Uri b = new Uri ("http://www.gnome.org");
480                         Uri n = new Uri (b, "blah#main#start");
481                         AssertEquals ("#3", n.Fragment, "#main%23start");
482                         
483                         n = new Uri (b, "blah#main#start", true);
484                         AssertEquals ("#3", n.Fragment, "#main#start");
485                 }
486                         
487                 [Test]
488                 [ExpectedException(typeof(UriFormatException))]
489                 public void IncompleteSchemeDelimiter ()
490                 {
491                         new Uri ("file:/filename.ext");
492                 }
493
494                 [Test]
495                 [Category("NotDotNet")]
496                 public void CheckHostName1 ()
497                 {
498                         // reported to MSDN Product Feedback Center (FDBK28671)
499                         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"));
500                 }
501
502                 [Test]
503                 public void CheckHostName2 ()
504                 {
505                         AssertEquals ("#1", UriHostNameType.Unknown, Uri.CheckHostName (null));
506                         AssertEquals ("#2", UriHostNameType.Unknown, Uri.CheckHostName (""));
507                         AssertEquals ("#3", UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"));
508                         AssertEquals ("#4", UriHostNameType.Dns, Uri.CheckHostName ("x"));
509                         AssertEquals ("#5", UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"));
510                         AssertEquals ("#6", UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"));
511                         AssertEquals ("#7", UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"));
512                         AssertEquals ("#8", UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"));
513                         AssertEquals ("#9", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"));
514                         AssertEquals ("#10", UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"));
515                         AssertEquals ("#11", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."));
516                         AssertEquals ("#12", UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"));      
517                         AssertEquals ("#13", UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"));      
518                         AssertEquals ("#14", UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"));  
519                         
520                         // test IPv6
521                         AssertEquals ("#15", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"));
522                         AssertEquals ("#16", UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"));
523                         AssertEquals ("#17", UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"));
524                         AssertEquals ("#18", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"));
525                         AssertEquals ("#19", UriHostNameType.IPv6, Uri.CheckHostName ("11::88"));
526                         AssertEquals ("#20", UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"));
527                         AssertEquals ("#21", UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"));
528                         AssertEquals ("#22", UriHostNameType.IPv6, Uri.CheckHostName ("11::"));
529                         AssertEquals ("#23", UriHostNameType.IPv6, Uri.CheckHostName ("::88"));
530                         AssertEquals ("#24", UriHostNameType.IPv6, Uri.CheckHostName ("::1"));
531                         AssertEquals ("#25", UriHostNameType.IPv6, Uri.CheckHostName ("::"));
532                         AssertEquals ("#26", UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"));
533                         AssertEquals ("#27", UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"));
534                         AssertEquals ("#28", UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"));
535                         AssertEquals ("#29", UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"));
536                         AssertEquals ("#30", UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"));
537                         AssertEquals ("#33", UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"));
538
539                         AssertEquals ("#31", UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"));
540                         AssertEquals ("#32", UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"));
541                         AssertEquals ("#34", UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"));
542                         AssertEquals ("#35", UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"));
543                         AssertEquals ("#37", UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"));
544                         AssertEquals ("#38", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"));
545                         AssertEquals ("#39", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"));
546                         AssertEquals ("#40", UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"));
547                         AssertEquals ("#41", UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"));
548                         AssertEquals ("#42", UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"));
549                         AssertEquals ("#43", UriHostNameType.Unknown, Uri.CheckHostName (":::"));
550                         AssertEquals ("#44", UriHostNameType.Unknown, Uri.CheckHostName (":"));
551                 }
552                 
553                 [Test]
554                 public void IsLoopback ()
555                 {
556                         Uri uri = new Uri("http://loopback:8080");
557                         AssertEquals ("#1", true, uri.IsLoopback);
558                         uri = new Uri("http://localhost:8080");
559                         AssertEquals ("#2", true, uri.IsLoopback);
560                         uri = new Uri("http://127.0.0.1:8080");
561                         AssertEquals ("#3", true, uri.IsLoopback);
562                         uri = new Uri("http://127.0.0.001:8080");
563                         AssertEquals ("#4", true, uri.IsLoopback);
564                         uri = new Uri("http://[::1]");
565                         AssertEquals ("#5", true, uri.IsLoopback);
566                         uri = new Uri("http://[::1]:8080");
567                         AssertEquals ("#6", true, uri.IsLoopback);
568                         uri = new Uri("http://[::0001]:8080");
569                         AssertEquals ("#7", true, uri.IsLoopback);
570                         uri = new Uri("http://[0:0:0:0::1]:8080");
571                         AssertEquals ("#8", true, uri.IsLoopback);
572                         uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
573                         AssertEquals ("#9", true, uri.IsLoopback);
574                         uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
575                         AssertEquals ("#10", false, uri.IsLoopback);
576                         uri = new Uri("http://[::ffff:127.11.22.33]:8080");
577                         AssertEquals ("#11", false, uri.IsLoopback);
578                         uri = new Uri("http://[::ff00:7f11:2233]:8080");
579                         AssertEquals ("#12", false, uri.IsLoopback);
580                         uri = new Uri("http://[1:0:0:0::1]:8080");
581                         AssertEquals ("#13", false, uri.IsLoopback);
582                 }
583                 
584                 [Test]
585                 public void Equals1 ()
586                 {
587                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
588                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
589                         Assert ("#1", uri1.Equals (uri2));
590                         Assert ("#3", !uri2.Equals ("http://www.contoso.com/index.html?x=1"));
591                         Assert ("#4", !uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"));
592                 }
593
594                 [Test]
595 #if !NET_2_0
596                 [Category("NotDotNet")]
597 #endif
598                 public void Equals2 ()
599                 {
600                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
601                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
602                         Assert ("#2 known to fail with ms.net 1.x", !uri1.Equals (uri2));
603                 }
604
605                 [Test]
606                 public void TestEquals2 ()
607                 {
608                         Uri a = new Uri ("http://www.go-mono.com");
609                         Uri b = new Uri ("http://www.go-mono.com");
610
611                         AssertEquals ("#1", a, b);
612
613                         a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
614                         b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
615
616                         AssertEquals ("#2", a, b);
617
618                         a = new Uri ("http://www.go-mono.com/ports/");
619                         b = new Uri ("http://www.go-mono.com/PORTS/");
620
621                         Assert ("#3", !a.Equals (b));
622                 }
623
624                 [Test]
625                 public void GetHashCodeTest () 
626                 {
627                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
628                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
629                         AssertEquals ("#1", uri1.GetHashCode (), uri2.GetHashCode ());
630                         uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
631                         Assert ("#2", uri1.GetHashCode () != uri2.GetHashCode ());
632                         uri2 = new Uri ("http://www.contoso.com:80/index.htm");
633                         AssertEquals ("#3", uri1.GetHashCode (), uri2.GetHashCode ());                  
634                         uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
635                         Assert ("#4", uri1.GetHashCode () != uri2.GetHashCode ());                      
636                 }
637                 
638                 [Test]
639                 public void MakeRelative ()
640                 {
641                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
642                         Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
643                         Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
644                         Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
645                         Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
646                         Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
647                         Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
648                         Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
649
650                         AssertEquals ("#1", "foo/bar/index.htm", uri1.MakeRelative (uri2));
651                         AssertEquals ("#2", "../../index.htm", uri2.MakeRelative (uri1));
652                         
653                         AssertEquals ("#3", "../../bar/foo/index.htm", uri2.MakeRelative (uri3));
654                         AssertEquals ("#4", "../../foo/bar/index.htm", uri3.MakeRelative (uri2));                       
655
656                         AssertEquals ("#5", "../foo2/index.htm", uri3.MakeRelative (uri4));
657                         AssertEquals ("#6", "../foo/index.htm", uri4.MakeRelative (uri3));
658                         
659                         AssertEquals ("#7", "https://www.contoso.com/bar/foo/index.htm?y=1", 
660                                             uri4.MakeRelative (uri5));
661
662                         AssertEquals ("#8", "http://www.contoso2.com/bar/foo/index.htm?x=0", 
663                                             uri4.MakeRelative (uri6));
664
665                         AssertEquals ("#9", "", uri6.MakeRelative (uri6));
666                         AssertEquals ("#10", "foobar.htm", uri6.MakeRelative (uri7));
667                         
668                         Uri uri10 = new Uri ("mailto:xxx@xxx.com");
669                         Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
670                         AssertEquals ("#11", "", uri10.MakeRelative (uri11));
671                         
672                         Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
673                         AssertEquals ("#12", "mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12));
674                                                 
675                         Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
676                         AssertEquals ("#13", "/foo/bar", uri10.MakeRelative (uri13));
677                         
678                         AssertEquals ("#14", "http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8));
679                 }
680
681                 [Test]
682                 public void ToStringTest()
683                 {                       
684                         Uri uri = new Uri ("dummy://xxx");
685                         AssertEquals ("#1", "dummy://xxx/", uri.ToString ());
686                 }
687
688                 [Test]
689                 public void CheckSchemeName ()
690                 {
691                         AssertEquals ("#01", false, Uri.CheckSchemeName (null));
692                         AssertEquals ("#02", false, Uri.CheckSchemeName (""));
693                         AssertEquals ("#03", true, Uri.CheckSchemeName ("http"));
694                         AssertEquals ("#04", true, Uri.CheckSchemeName ("http-"));
695                         AssertEquals ("#05", false, Uri.CheckSchemeName ("6http-"));
696                         AssertEquals ("#06", true, Uri.CheckSchemeName ("http6-"));
697                         AssertEquals ("#07", false, Uri.CheckSchemeName ("http6,"));
698                         AssertEquals ("#08", true, Uri.CheckSchemeName ("http6."));
699                         AssertEquals ("#09", false, Uri.CheckSchemeName ("+http"));
700                         AssertEquals ("#10", true, Uri.CheckSchemeName ("htt+p6"));
701                         // 0x00E1 -> &atilde;
702                         AssertEquals ("#11", true, Uri.CheckSchemeName ("htt\u00E1+p6"));
703                 }
704
705                 [Test]
706                 [ExpectedException (typeof (UriFormatException))]
707                 public void NoHostname ()
708                 {
709                         Uri uri = new Uri ("http://");
710                 }
711
712                 [Test]
713 #if !NET_2_0
714                 // MS.NET 1.x throws an IndexOutOfRangeException
715                 [Category("NotDotNet")]
716 #endif
717                 [Ignore("Bug #74144")]
718                 public void NoHostname2 ()
719                 {
720                         Uri uri = new Uri ("file://");
721                         AssertEquals ("#1", true, uri.IsFile);
722                         AssertEquals ("#2", false, uri.IsUnc);
723                         AssertEquals ("#3", "file", uri.Scheme);
724                         AssertEquals ("#4", "/", uri.LocalPath);
725                         AssertEquals ("#5", string.Empty, uri.Query);
726                         AssertEquals ("#6", "/", uri.AbsolutePath);
727                         AssertEquals ("#7", "file:///", uri.AbsoluteUri);
728                         AssertEquals ("#8", string.Empty, uri.Authority);
729                         AssertEquals ("#9", string.Empty, uri.Host);
730                         AssertEquals ("#10", UriHostNameType.Basic, uri.HostNameType);
731                         AssertEquals ("#11", string.Empty, uri.Fragment);
732                         AssertEquals ("#12", true, uri.IsDefaultPort);
733                         AssertEquals ("#13", true, uri.IsLoopback);
734                         AssertEquals ("#14", "/", uri.PathAndQuery);
735                         AssertEquals ("#15", false, uri.UserEscaped);
736                         AssertEquals ("#16", string.Empty, uri.UserInfo);
737                         AssertEquals ("#17", "file://", uri.GetLeftPart (UriPartial.Authority));
738                         AssertEquals ("#18", "file:///", uri.GetLeftPart (UriPartial.Path));
739                         AssertEquals ("#19", "file://", uri.GetLeftPart (UriPartial.Scheme));
740                 }
741
742                 [Test]
743                 public void Segments1 ()
744                 {
745                         Uri uri = new Uri ("http://localhost/");
746                         string [] segments = uri.Segments;
747                         AssertEquals ("#01", 1, segments.Length);
748                         AssertEquals ("#02", "/", segments [0]);
749                         
750                 }
751
752                 [Test]
753                 public void Segments2 ()
754                 {
755                         Uri uri = new Uri ("http://localhost/dir/dummypage.html");
756                         string [] segments = uri.Segments;
757                         AssertEquals ("#01", 3, segments.Length);
758                         AssertEquals ("#02", "/", segments [0]);
759                         AssertEquals ("#03", "dir/", segments [1]);
760                         AssertEquals ("#04", "dummypage.html", segments [2]);
761                         
762                 }
763
764                 [Test]
765                 public void Segments3 ()
766                 {
767                         Uri uri = new Uri ("http://localhost/dir/dummypage/");
768                         string [] segments = uri.Segments;
769                         AssertEquals ("#01", 3, segments.Length);
770                         AssertEquals ("#02", "/", segments [0]);
771                         AssertEquals ("#03", "dir/", segments [1]);
772                         AssertEquals ("#04", "dummypage/", segments [2]);
773                         
774                 }
775
776                 [Test]
777                 public void Segments4 ()
778                 {
779                         Uri uri = new Uri ("file:///c:/hello");
780                         string [] segments = uri.Segments;
781                         AssertEquals ("#01", 3, segments.Length);
782                         AssertEquals ("#02", "c:", segments [0]);
783                         AssertEquals ("#03", "/", segments [1]);
784                         AssertEquals ("#04", "hello", segments [2]);
785                         
786                 }
787
788                 [Test]
789                 public void Segments5 ()
790                 {
791                         Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
792                         string [] segments = uri.Segments;
793                         AssertEquals ("#01", 4, segments.Length);
794                         AssertEquals ("#02", "/", segments [0]);
795                         AssertEquals ("#03", "bar/", segments [1]);
796                         AssertEquals ("#04", "foo/", segments [2]);
797                         AssertEquals ("#05", "foobar.htm", segments [3]);
798                 }
799
800                 [Test]
801                 [ExpectedException (typeof (UriFormatException))]
802                 public void UriStartingWithColon()
803                 {
804                         new Uri("://");
805                 }
806
807                 [Test]
808                 [ExpectedException (typeof (UriFormatException))]
809                 public void EmptyScheme ()
810                 {
811                         new Uri ("hey");
812                 }
813
814                 [Test]
815                 public void InvalidPortsThatWorkWithMS ()
816                 {
817                         new Uri ("http://www.contoso.com:12345678/foo/bar/");
818                         // UInt32.MaxValue gives port == -1 !!!
819                         new Uri ("http://www.contoso.com:4294967295/foo/bar/");
820                         // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
821                         new Uri ("http://www.contoso.com:2147483648/foo/bar/");
822                 }
823
824                 class UriEx2 : Uri
825                 {
826                         public UriEx2 (string s) : base (s)
827                         {
828                         }
829
830                         protected override void Parse ()
831                         {
832                         }
833                 }
834
835                 [Test]
836                 public void ParseOverride ()
837                 {
838                         // If this does not override base's Parse(), it will
839                         // fail since this argument is not Absolute URI.
840                         UriEx2 ex = new UriEx2 ("readme.txt");
841                 }
842
843                 [Test]
844                 public void UnixLocalPath ()
845                 {
846                         // This works--the location is not part of the absolute path
847                         string path = "file://localhost/tmp/foo/bar";
848                         Uri fileUri = new Uri( path );
849                         AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
850
851                         // Empty path == localhost, in theory
852                         path = "file:///c:/tmp/foo/bar";
853                         fileUri = new Uri( path );
854                         AssertEquals (path, "c:/tmp/foo/bar", fileUri.AbsolutePath);
855                 }
856
857                 // This test doesn't work on Linux, and arguably shouldn't work.
858                 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar" 
859                 // on Linux, as anyone sane would expect.  It *doesn't* under .NET 1.1
860                 // Apparently "tmp" is supposed to be a hostname (!)...
861                 // Since "correct" behavior would confuse all Linux developers, and having
862                 // an expected failure is evil, we'll just ignore this for now...
863                 //
864                 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
865                 //
866                 // You are surrounded by conditional-compilation code, all alike.
867                 // You are likely to be eaten by a Grue...
868                 [Test]
869 #if ONLY_1_1
870                 [Category ("NotWorking")]
871 #endif
872                 public void UnixLocalPath_WTF ()
873                 {
874                         // Empty path == localhost, in theory
875                         string path = "file:///tmp/foo/bar";
876                         Uri fileUri = new Uri( path );
877 #if NET_2_0
878                         AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
879 #else
880                         AssertEquals (path, "/foo/bar", fileUri.AbsolutePath);
881 #endif
882                 }
883
884                 public static void Print (Uri uri)
885                 {
886                         Console.WriteLine ("ToString: " + uri.ToString ());     
887
888                         Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
889                         Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
890                         Console.WriteLine ("Authority: " + uri.Authority);
891                         Console.WriteLine ("Fragment: " + uri.Fragment);
892                         Console.WriteLine ("Host: " + uri.Host);
893                         Console.WriteLine ("HostNameType: " + uri.HostNameType);
894                         Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
895                         Console.WriteLine ("IsFile: " + uri.IsFile);
896                         Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
897                         Console.WriteLine ("IsUnc: " + uri.IsUnc);
898                         Console.WriteLine ("LocalPath: " + uri.LocalPath);
899                         Console.WriteLine ("PathAndQuery: " + uri.PathAndQuery);
900                         Console.WriteLine ("Port: " + uri.Port);
901                         Console.WriteLine ("Query: " + uri.Query);
902                         Console.WriteLine ("Scheme: " + uri.Scheme);
903                         Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
904                         Console.WriteLine ("UserInfo: " + uri.UserInfo);
905
906                         Console.WriteLine ("Segments:");
907                         string [] segments = uri.Segments;
908                         if (segments == null) 
909                                 Console.WriteLine ("\tNo Segments");
910                         else 
911                                 for (int i = 0; i < segments.Length; i++) 
912                                         Console.WriteLine ("\t" + segments[i]);                                 
913                         Console.WriteLine ("");
914                 }
915
916         }
917 }
918