merge -r 58784:58785
[mono.git] / mcs / class / System / Test / System / UriParserTest.cs
1 //
2 // UriParserTest.cs - Unit tests for System.UriParser
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using NUnit.Framework;
32
33 using System;
34 using System.Text;
35
36 namespace MonoTests.System {
37
38         public class UnitTestUriParser : UriParser {
39
40                 private string scheme_name;
41                 private int default_port;
42                 private bool throw_on_register;
43                 private bool on_new_uri_called;
44
45                 public UnitTestUriParser ()
46                 {
47                 }
48
49                 public UnitTestUriParser (bool throwOnRegister)
50                 {
51                         throw_on_register = throwOnRegister;
52                 }
53
54                 public string SchemeName {
55                         get { return scheme_name; }
56                 }
57
58                 public int DefaultPort {
59                         get { return default_port; }
60                 }
61
62                 public bool OnNewUriCalled {
63                         get { return on_new_uri_called; }
64                 }
65
66                 public string _GetComponents (Uri uri, UriComponents components, UriFormat format)
67                 {
68                         return base.GetComponents (uri, components, format);
69                 }
70
71                 public void _InitializeAndValidate (Uri uri, out UriFormatException parserError)
72                 {
73                         base.InitializeAndValidate (uri, out parserError);
74                 }
75
76                 public bool _IsBaseOf (Uri baseUri, Uri relativeUri)
77                 {
78                         return base.IsBaseOf (baseUri, relativeUri);
79                 }
80
81                 public bool _IsWellFormedOriginalString (Uri uri)
82                 {
83                         return base.IsWellFormedOriginalString (uri);
84                 }
85
86                 public UriParser _OnNewUri ()
87                 {
88                         return base.OnNewUri ();
89                 }
90
91                 public void _OnRegister (string schemeName, int defaultPort)
92                 {
93                         base.OnRegister (schemeName, defaultPort);
94                 }
95
96                 public string _Resolve (Uri baseUri, Uri relativeUri, out UriFormatException parserError)
97                 {
98                         return base.Resolve (baseUri, relativeUri, out parserError);
99                 }
100
101                 protected override UriParser OnNewUri ()
102                 {
103                         on_new_uri_called = true;
104                         return base.OnNewUri ();
105                 }
106
107                 protected override void OnRegister (string schemeName, int defaultPort)
108                 {
109                         if (throw_on_register)
110                                 throw new NotSupportedException ();
111                         scheme_name = schemeName;
112                         default_port = defaultPort;
113                         base.OnRegister (schemeName, defaultPort);
114                 }
115         }
116
117         [TestFixture]
118         public class UriParserTest {
119
120                 private string prefix;
121                 private Uri http;
122
123                 [TestFixtureSetUp]
124                 public void FixtureSetUp ()
125                 {
126                         prefix = "unit.test.";
127                         http = new Uri ("http://www.mono-project.com");
128                 }
129
130                 public string Prefix {
131                         get { return prefix; }
132                         set { prefix = value; }
133                 }
134
135                 [Test]
136                 [Category ("NotWorking")]
137                 public void GetComponents ()
138                 {
139                         UnitTestUriParser p = new UnitTestUriParser ();
140                         Assert.AreEqual ("www.mono-project.com", p._GetComponents (http, UriComponents.Host, UriFormat.SafeUnescaped), "http");
141                         Assert.AreSame (p, p._OnNewUri (), "OnNewUri");
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (NullReferenceException))]
146                 [Category ("NotWorking")]
147                 public void GetComponents_Null ()
148                 {
149                         UnitTestUriParser p = new UnitTestUriParser ();
150                         p._GetComponents (null, UriComponents.Host, UriFormat.SafeUnescaped);
151                 }
152
153                 [Test]
154                 [Category ("NotWorking")]
155                 public void GetComponents_BadUriComponents ()
156                 {
157                         UnitTestUriParser p = new UnitTestUriParser ();
158                         Assert.AreEqual ("http://www.mono-project.com/", p._GetComponents (http, (UriComponents) Int32.MinValue, UriFormat.SafeUnescaped), "http");
159                 }
160
161                 [Test]
162                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
163                 public void GetComponents_BadUriFormat ()
164                 {
165                         UnitTestUriParser p = new UnitTestUriParser ();
166                         p._GetComponents (http, UriComponents.Host, (UriFormat)Int32.MinValue);
167                 }
168
169                 [Test]
170                 [Category ("NotWorking")]
171                 public void InitializeAndValidate ()
172                 {
173                         UriFormatException error = null;
174                         UnitTestUriParser p = new UnitTestUriParser ();
175                         p._InitializeAndValidate (http, out error);
176                         Assert.IsNotNull (error, "out"); // authority/host couldn't be parsed ?!?!
177                 }
178
179                 [Test]
180                 [ExpectedException (typeof (NullReferenceException))]
181                 [Category ("NotWorking")]
182                 public void InitializeAndValidate_Null ()
183                 {
184                         UriFormatException error = null;
185                         UnitTestUriParser p = new UnitTestUriParser ();
186                         p._InitializeAndValidate (null, out error);
187                 }
188
189                 [Test]
190                 [Category ("NotWorking")]
191                 public void IsBaseOf ()
192                 {
193                         UnitTestUriParser p = new UnitTestUriParser ();
194                         Assert.IsTrue (p._IsBaseOf (http, http), "http-http");
195                 }
196
197                 [Test]
198                 [ExpectedException (typeof (NullReferenceException))]
199                 [Category ("NotWorking")]
200                 public void IsBaseOf_UriNull ()
201                 {
202                         UnitTestUriParser p = new UnitTestUriParser ();
203                         p._IsBaseOf (http, null);
204                 }
205
206                 [Test]
207                 [ExpectedException (typeof (NullReferenceException))]
208                 [Category ("NotWorking")]
209                 public void IsBaseOf_NullUri ()
210                 {
211                         UnitTestUriParser p = new UnitTestUriParser ();
212                         p._IsBaseOf (null, http);
213                 }
214
215                 [Test]
216                 [Category ("NotWorking")]
217                 public void IsWellFormedOriginalString ()
218                 {
219                         UnitTestUriParser p = new UnitTestUriParser ();
220                         Assert.IsTrue (p._IsWellFormedOriginalString (http), "http");
221                 }
222
223                 [Test]
224                 [ExpectedException (typeof (NullReferenceException))]
225                 [Category ("NotWorking")]
226                 public void IsWellFormedOriginalString_Null ()
227                 {
228                         UnitTestUriParser p = new UnitTestUriParser ();
229                         p._IsWellFormedOriginalString (null);
230                 }
231
232                 [Test]
233                 [Category ("NotWorking")]
234                 public void OnNewUri ()
235                 {
236                         string scheme = prefix + "on.new.uri";
237                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
238
239                         UnitTestUriParser p = new UnitTestUriParser ();
240                         UriParser.Register (p, scheme, 1999);
241
242                         Assert.IsFalse (p.OnNewUriCalled, "!Called");
243                         Uri uri = new Uri (scheme + "://www.mono-project.com");
244                         Assert.IsTrue (p.OnNewUriCalled, "Called");
245                 }
246
247                 [Test]
248                 public void OnRegister ()
249                 {
250                         string scheme = prefix + "onregister";
251                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
252                         UnitTestUriParser p = new UnitTestUriParser ();
253                         try {
254                                 UriParser.Register (p, scheme, 2005);
255                         }
256                         catch (NotSupportedException) {
257                                 // special case / ordering
258                         }
259                         // if true then the registration is done before calling OnRegister
260                         Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
261                 }
262
263                 [Test]
264                 [Category ("NotWorking")]
265                 public void Resolve ()
266                 {
267                         UriFormatException error = null;
268                         UnitTestUriParser p = new UnitTestUriParser ();
269                         Assert.AreEqual ("http://www.mono-project.com", p._Resolve (http, http, out error), "http-http");
270                 }
271
272                 [Test]
273                 [Category ("NotWorking")]
274                 public void Resolve_UriNull ()
275                 {
276                         UriFormatException error = null;
277                         UnitTestUriParser p = new UnitTestUriParser ();
278                         Assert.AreEqual ("http://www.mono-project.com", p._Resolve (http, null, out error), "http-http");
279                 }
280
281                 [Test]
282                 [ExpectedException (typeof (NullReferenceException))]
283                 [Category ("NotWorking")]
284                 public void Resolve_NullUri ()
285                 {
286                         UriFormatException error = null;
287                         UnitTestUriParser p = new UnitTestUriParser ();
288                         p._Resolve (null, http, out error);
289                         p._Resolve (http, null, out error);
290                 }
291
292                 [Test]
293                 public void IsKnownScheme_WellKnown ()
294                 {
295                         // from Uri.UriScheme* fields
296                         Assert.IsTrue (UriParser.IsKnownScheme ("file"), "file");
297                         Assert.IsTrue (UriParser.IsKnownScheme ("ftp"), "ftp");
298                         Assert.IsTrue (UriParser.IsKnownScheme ("gopher"), "gopher");
299                         Assert.IsTrue (UriParser.IsKnownScheme ("http"), "http");
300                         Assert.IsTrue (UriParser.IsKnownScheme ("https"), "https");
301                         Assert.IsTrue (UriParser.IsKnownScheme ("mailto"), "mailto");
302                         Assert.IsTrue (UriParser.IsKnownScheme ("net.pipe"), "net.pipe");
303                         Assert.IsTrue (UriParser.IsKnownScheme ("net.tcp"), "net.tcp");
304                         Assert.IsTrue (UriParser.IsKnownScheme ("news"), "news");
305                         Assert.IsTrue (UriParser.IsKnownScheme ("nntp"), "nntp");
306                         // infered from class library
307                         Assert.IsTrue (UriParser.IsKnownScheme ("ldap"), "ldap");
308                         Assert.IsFalse (UriParser.IsKnownScheme ("ldaps"), "ldaps");
309                         // well known for not existing
310                         Assert.IsFalse (UriParser.IsKnownScheme ("unknown"), "unknown");
311
312                         // variations - mixed and upper case
313                         Assert.IsTrue (UriParser.IsKnownScheme ("FiLe"), "FiLe");
314                         Assert.IsTrue (UriParser.IsKnownScheme ("FTP"), "ftp");
315                 }
316
317                 [Test]
318                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
319                 [Category ("NotWorking")]
320                 public void IsKnownScheme_ExtraSpace ()
321                 {
322                         // same result for space before, inside or after the scheme
323                         UriParser.IsKnownScheme ("ht tp");
324                         // this is undocumented (and I hate exceptions in a boolean method)
325                 }
326
327                 [Test]
328                 [ExpectedException (typeof (ArgumentNullException))]
329                 public void IsKnownScheme_Null ()
330                 {
331                         UriParser.IsKnownScheme (null);
332                 }
333
334                 [Test]
335                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
336                 public void IsKnownScheme_Empty ()
337                 {
338                         UriParser.IsKnownScheme (String.Empty);
339                 }
340
341                 [Test]
342                 public void Register ()
343                 {
344                         string scheme = prefix + "register.mono";
345                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
346
347                         UnitTestUriParser p = new UnitTestUriParser ();
348                         UriParser.Register (p, scheme, 2005);
349                         Assert.AreEqual (scheme, p.SchemeName, "SchemeName");
350                         Assert.AreEqual (2005, p.DefaultPort, "DefaultPort");
351
352                         Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
353                 }
354
355                 [Test]
356                 [ExpectedException (typeof (ArgumentNullException))]
357                 public void Register_NullParser ()
358                 {
359                         UriParser.Register (null, prefix + "null.parser", 2006);
360                 }
361
362                 [Test]
363                 [ExpectedException (typeof (ArgumentNullException))]
364                 public void Register_NullScheme ()
365                 {
366                         UnitTestUriParser p = new UnitTestUriParser ();
367                         UriParser.Register (p, null, 2006);
368                 }
369
370                 [Test]
371                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
372                 public void Register_NegativePort ()
373                 {
374                         UnitTestUriParser p = new UnitTestUriParser ();
375                         UriParser.Register (p, prefix + "negative.port", -2);
376                 }
377
378                 [Test]
379                 public void Register_Minus1Port ()
380                 {
381                         UnitTestUriParser p = new UnitTestUriParser ();
382                         UriParser.Register (p, prefix + "minus1.port", -1);
383                 }
384
385                 [Test]
386                 public void Register_UInt16PortMinus1 ()
387                 {
388                         UnitTestUriParser p = new UnitTestUriParser ();
389                         UriParser.Register (p, prefix + "uint16.minus.1.port", UInt16.MaxValue - 1);
390                 }
391
392                 [Test]
393                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
394                 public void Register_TooBigPort ()
395                 {
396                         UnitTestUriParser p = new UnitTestUriParser ();
397                         UriParser.Register (p, prefix + "too.big.port", UInt16.MaxValue);
398                 }
399
400                 [Test]
401                 [ExpectedException (typeof (InvalidOperationException))]
402                 public void ReRegister ()
403                 {
404                         string scheme = prefix + "re.register.mono";
405                         Assert.IsFalse (UriParser.IsKnownScheme (scheme), "IsKnownScheme-false");
406                         UnitTestUriParser p = new UnitTestUriParser ();
407                         UriParser.Register (p, scheme, 2005);
408                         Assert.IsTrue (UriParser.IsKnownScheme (scheme), "IsKnownScheme-true");
409                         UriParser.Register (p, scheme, 2006);
410                 }
411         }
412 }
413
414 #endif