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