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