Converted ChangeLogs to UTF-8 and recovered incorrectly encoded characters.
[mono.git] / mcs / class / System / Test / System / UriTest2.cs
1 //\r
2 // UriTest2.cs - More NUnit Test Cases for System.Uri\r
3 //
4 \r
5 using System;\r
6 using System.IO;
7 using System.Reflection;\r
8 using System.Text;\r
9 using NUnit.Framework;\r
10 \r
11 namespace MonoTests.System\r
12 {\r
13         [TestFixture]\r
14         public class UriTest2\r
15         {\r
16                 // Segments cannot be validated here...\r
17                 public void AssertUri (string relsrc, Uri uri,\r
18                         string toString,\r
19                         string absoluteUri,\r
20                         string scheme,\r
21                         string host,\r
22                         string localPath,\r
23                         string query,\r
24                         int port,\r
25                         bool isFile,\r
26                         bool isUnc,\r
27                         bool isLoopback,\r
28                         bool userEscaped,\r
29                         UriHostNameType hostNameType,\r
30                         string absolutePath,\r
31                         string pathAndQuery,\r
32                         string authority,\r
33                         string fragment,\r
34                         string userInfo)\r
35                 {\r
36                         Assert.AreEqual (absoluteUri, uri.AbsoluteUri, relsrc + " AbsoluteUri");\r
37                         Assert.AreEqual (scheme, uri.Scheme, relsrc + " Scheme");\r
38                         Assert.AreEqual (host, uri.Host, relsrc + " Host");\r
39                         Assert.AreEqual (port, uri.Port, relsrc + " Port");\r
40                         // Windows UNC path is not automatically testable on *nix environment,\r
41                         if (relsrc.StartsWith ("\\\\") && Path.DirectorySeparatorChar == '\\')\r
42                                 Assert.AreEqual (localPath, uri.LocalPath, relsrc + " LocalPath");\r
43                         Assert.AreEqual (query, uri.Query, relsrc + " Query");\r
44                         Assert.AreEqual (fragment, uri.Fragment, relsrc + " Fragment");\r
45                         Assert.AreEqual (isFile, uri.IsFile, relsrc + " IsFile");\r
46                         Assert.AreEqual (isUnc, uri.IsUnc, relsrc + " IsUnc");\r
47                         Assert.AreEqual (isLoopback, uri.IsLoopback, relsrc + " IsLoopback");\r
48                         Assert.AreEqual (authority, uri.Authority, relsrc + " Authority");\r
49                         Assert.AreEqual (userEscaped, uri.UserEscaped, relsrc + " UserEscaped");\r
50                         Assert.AreEqual (userInfo, uri.UserInfo, relsrc + " UserInfo");\r
51                         Assert.AreEqual (hostNameType, uri.HostNameType, relsrc + " HostNameType");\r
52                         Assert.AreEqual (absolutePath, uri.AbsolutePath, relsrc + " AbsolutePath");\r
53                         Assert.AreEqual (pathAndQuery, uri.PathAndQuery, relsrc + " PathAndQuery");\r
54                         Assert.AreEqual (toString, uri.ToString (), relsrc + " ToString()");\r
55                 }\r
56 \r
57                 [Test]\r
58 #if NET_2_0\r
59                 [Ignore ("Tests needs to be updated for 2.0")]\r
60 #endif\r
61                 public void AbsoluteUriFromFile ()\r
62                 {\r
63                         FromResource ("test-uri-props.txt", null);\r
64                 }\r
65                 \r
66                 [Test]\r
67                 [Category("NotDotNet")]\r
68 #if NET_2_0\r
69                 [Ignore ("Tests needs to be updated for 2.0")]\r
70 #endif\r
71                 public void AbsoluteUriFromFileManual ()\r
72                 {\r
73                         FromResource ("test-uri-props-manual.txt", null);\r
74                 }\r
75                 \r
76                 [Test]\r
77 #if NET_2_0\r
78                 [Ignore ("Tests needs to be updated for 2.0")]\r
79 #endif\r
80                 public void RelativeUriFromFile ()\r
81                 {\r
82                         FromResource ("test-uri-relative-props.txt", new Uri ("http://www.go-mono.com"));\r
83                 }\r
84                 \r
85                 private void FromResource (string res, Uri baseUri)\r
86                 {
87                         Assembly a = Assembly.GetExecutingAssembly ();
88                         Stream s = a.GetManifestResourceStream (res);\r
89                         StreamReader sr = new StreamReader (s, Encoding.UTF8);\r
90                         while (sr.Peek () > 0) {\r
91                                 sr.ReadLine (); // skip\r
92                                 string uriString = sr.ReadLine ();\r
93 /*\r
94 TextWriter sw = Console.Out;\r
95                                 sw.WriteLine ("-------------------------");\r
96                                 sw.WriteLine (uriString);\r
97 */\r
98                                 if (uriString == null || uriString.Length == 0)\r
99                                         break;\r
100 \r
101                                 try {\r
102                                         Uri uri = baseUri == null ? new Uri (uriString) : new Uri (baseUri, uriString);\r
103 /*\r
104                                 sw.WriteLine ("ToString(): " + uri.ToString ());\r
105                                 sw.WriteLine (uri.AbsoluteUri);\r
106                                 sw.WriteLine (uri.Scheme);\r
107                                 sw.WriteLine (uri.Host);\r
108                                 sw.WriteLine (uri.LocalPath);\r
109                                 sw.WriteLine (uri.Query);\r
110                                 sw.WriteLine ("Port: " + uri.Port);\r
111                                 sw.WriteLine (uri.IsFile);\r
112                                 sw.WriteLine (uri.IsUnc);\r
113                                 sw.WriteLine (uri.IsLoopback);\r
114                                 sw.WriteLine (uri.UserEscaped);\r
115                                 sw.WriteLine ("HostNameType: " + uri.HostNameType);\r
116                                 sw.WriteLine (uri.AbsolutePath);\r
117                                 sw.WriteLine ("PathAndQuery: " + uri.PathAndQuery);\r
118                                 sw.WriteLine (uri.Authority);\r
119                                 sw.WriteLine (uri.Fragment);\r
120                                 sw.WriteLine (uri.UserInfo);\r
121 */\r
122                                         AssertUri (uriString, uri,\r
123                                                 sr.ReadLine (),\r
124                                                 sr.ReadLine (),\r
125                                                 sr.ReadLine (),\r
126                                                 sr.ReadLine (),\r
127                                                 sr.ReadLine (),\r
128                                                 sr.ReadLine (),\r
129                                                 int.Parse (sr.ReadLine ()),\r
130                                                 bool.Parse (sr.ReadLine ()),\r
131                                                 bool.Parse (sr.ReadLine ()),\r
132                                                 bool.Parse (sr.ReadLine ()),\r
133                                                 bool.Parse (sr.ReadLine ()),\r
134                                                 (UriHostNameType) Enum.Parse (typeof (UriHostNameType), sr.ReadLine (), false),\r
135                                                 sr.ReadLine (),\r
136                                                 sr.ReadLine (),\r
137                                                 sr.ReadLine (),\r
138                                                 sr.ReadLine (),\r
139                                                 sr.ReadLine ());\r
140 //                              Console.WriteLine ("Passed: " + uriString);\r
141                                 } catch (UriFormatException ex) {\r
142                                         Assert.Fail (String.Format ("Construction failed: [{0}] {1}", uriString, ex.Message));\r
143                                 }\r
144                         }\r
145                 }\r
146
147                 [Test]
148                 public void MoreUnescape () // bug 733316
149                 {
150                         int index = 0;
151                         char unesc = Uri.HexUnescape ("%F6", ref index);\r
152                         Assert.AreEqual (3, index, "#01");\r
153                         Assert.AreEqual (0xf6, unesc, "#02");
154                 }\r
155 \r
156                 [Test]\r
157                 public void UriScheme ()\r
158                 {\r
159                         Assert.AreEqual ("://", Uri.SchemeDelimiter, "://");\r
160                         Assert.AreEqual ("file", Uri.UriSchemeFile, "file");\r
161                         Assert.AreEqual ("ftp", Uri.UriSchemeFtp, "ftp");\r
162                         Assert.AreEqual ("gopher", Uri.UriSchemeGopher, "gopher");\r
163                         Assert.AreEqual ("http", Uri.UriSchemeHttp, "http");\r
164                         Assert.AreEqual ("https", Uri.UriSchemeHttps, "https");\r
165                         Assert.AreEqual ("mailto", Uri.UriSchemeMailto, "mailto");\r
166                         Assert.AreEqual ("news", Uri.UriSchemeNews, "news");\r
167                         Assert.AreEqual ("nntp", Uri.UriSchemeNntp, "file");\r
168 #if NET_2_0\r
169                         Assert.AreEqual ("net.pipe", Uri.UriSchemeNetPipe, "net.pipe");\r
170                         Assert.AreEqual ("net.tcp", Uri.UriSchemeNetTcp, "net.tcp");\r
171 #endif\r
172                 }\r
173 \r
174                 [Test] // bug #71049\r
175                 [ExpectedException (typeof (UriFormatException))]\r
176                 public void StarsInHost ()\r
177                 {\r
178                         new Uri ("http://w*w*w.go-mono.com");\r
179                 }\r
180
181                 [Test]\r
182 #if ONLY_1_1\r
183                 [Category ("NotDotNet")] // 1.x throws an UriFormatException\r
184 #endif\r
185                 public void NoHostName1_Bug76146 ()\r
186                 {\r
187                         Uri u = new Uri ("foo:///?bar");\r
188                         Assert.AreEqual ("/", u.AbsolutePath, "AbsolutePath");\r
189                         Assert.AreEqual ("foo:///?bar", u.AbsoluteUri, "AbsoluteUri");\r
190                         Assert.AreEqual (String.Empty, u.Authority, "Authority");\r
191                         Assert.AreEqual (String.Empty, u.Fragment, "Fragment");\r
192                         Assert.AreEqual (String.Empty, u.Host, "Host");\r
193                         // FIXME (2.0) - Normally this is never Basic without an Host name :(
194                         // Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "HostNameType");\r
195                         Assert.IsTrue (u.IsDefaultPort, "IsDefaultPort");\r
196                         Assert.IsFalse (u.IsFile, "IsFile");\r
197                         // FIXME Assert.IsTrue (u.IsLoopback, "IsLoopback");\r
198                         Assert.IsFalse (u.IsUnc, "IsUnc");\r
199                         Assert.AreEqual ("/", u.LocalPath, "LocalPath");\r
200                         Assert.AreEqual ("/?bar", u.PathAndQuery, "PathAndQuery");\r
201                         Assert.AreEqual ("foo", u.Scheme, "Scheme");\r
202                         Assert.IsFalse (u.UserEscaped, "UserEscaped");\r
203                         Assert.AreEqual (String.Empty, u.UserInfo, "UserInfo");\r
204                 }\r
205 \r
206                 [Test]\r
207 #if ONLY_1_1\r
208                 [Category ("NotDotNet")] // 1.x throws an UriFormatException\r
209 #endif\r
210                 public void NoHostName2_Bug76146 ()\r
211                 {\r
212                         Uri u = new Uri ("foo:///bar");\r
213                         Assert.AreEqual ("/bar", u.AbsolutePath, "AbsolutePath");\r
214                         Assert.AreEqual ("foo:///bar", u.AbsoluteUri, "AbsoluteUri");\r
215                         Assert.AreEqual (String.Empty, u.Authority, "Authority");\r
216                         Assert.AreEqual (String.Empty, u.Fragment, "Fragment");\r
217                         Assert.AreEqual (String.Empty, u.Host, "Host");
218                         // FIXME (2.0) - Normally this is never Basic without an Host name :(
219                         // Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "HostNameType");\r
220                         Assert.IsTrue (u.IsDefaultPort, "IsDefaultPort");\r
221                         Assert.IsFalse (u.IsFile, "IsFile");\r
222                         // FIXME Assert.IsTrue (u.IsLoopback, "IsLoopback");\r
223                         Assert.IsFalse (u.IsUnc, "IsUnc");\r
224                         Assert.AreEqual ("/bar", u.LocalPath, "LocalPath");\r
225                         Assert.AreEqual ("/bar", u.PathAndQuery, "PathAndQuery");\r
226                         Assert.AreEqual ("foo", u.Scheme, "Scheme");\r
227                         Assert.IsFalse (u.UserEscaped, "UserEscaped");\r
228                         Assert.AreEqual (String.Empty, u.UserInfo, "UserInfo");\r
229                 }\r
230 \r
231                 [Test]\r
232                 [ExpectedException (typeof (UriFormatException))]\r
233                 public void InvalidIPAddress_Bug76659 ()\r
234                 {\r
235                         new Uri ("http://127.0.0.1::::/");\r
236                 }\r
237         }\r
238 }\r