2003-05-22 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System.IO / PathTest.cs
1 //
2 // System.IO.Path Test Cases
3 //
4 // Authors:
5 //      Marcin Szczepanski (marcins@zipworld.com.au)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Ben Maurer (bmaurer@users.sf.net)
8 //      Gilles Freart (gfr@skynet.be)
9 //
10 // (c) Marcin Szczepanski 
11 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
12 // (c) 2003 Ben Maurer
13 // (c) 2003 Gilles Freart
14 //
15
16 #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
17
18 #if NUNIT
19 using NUnit.Framework;
20 #else
21 using System.Reflection;
22 #endif
23
24 using System.IO;
25 using System;
26 using System.Text;
27
28 namespace MonoTests.System.IO
29 {
30
31         enum OsType {
32                 Windows,
33                 Unix,
34                 Mac
35         }
36
37 #if NUNIT
38         public class PathTest : TestCase
39         {
40 #else
41         public class PathTest
42         {
43 #endif
44                 static string path1;
45                 static string path2;
46                 static string path3;
47                 static OsType OS;
48                 static char DSC = Path.DirectorySeparatorChar;
49              
50 #if NUNIT
51                 protected override void SetUp ()
52                 {
53 #else
54                 static PathTest ()
55                 {
56 #endif
57                         if ('/' == DSC) {
58                                 OS = OsType.Unix;
59                                 path1 = "/foo/test.txt";
60                                 path2 = "/etc";
61                                 path3 = "init.d";
62                         } else if ('\\' == DSC) {
63                                 OS = OsType.Windows;
64                                 path1 = "c:\\foo\\test.txt";
65                                 path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
66                                 path3 = "system32";
67                         } else {
68                                 OS = OsType.Mac;
69                                 //FIXME: For Mac. figure this out when we need it
70                                 path1 = "foo:test.txt";
71                                 path2 = "foo";
72                                 path3 = "bar";
73                         }
74                 }
75
76                 bool Windows
77                 {
78                         get {
79                                 return OS == OsType.Windows;
80                         }
81                 }
82
83                 bool Unix
84                 {
85                         get {
86                                 return OS == OsType.Unix;
87                         }
88                 }
89
90                 bool Mac
91                 {
92                         get {
93                                 return OS == OsType.Mac;
94                         }
95                 }
96
97                 public void TestChangeExtension ()
98                 {
99                         string [] files = new string [3];
100                         files [(int) OsType.Unix] = "/foo/test.doc";
101                         files [(int) OsType.Windows] = "c:\\foo\\test.doc";
102                         files [(int) OsType.Mac] = "foo:test.doc";
103
104                         string testPath = Path.ChangeExtension (path1, "doc");
105                         AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
106
107                         testPath = Path.ChangeExtension ("", ".extension");
108                         AssertEquals ("ChangeExtension #02", String.Empty, testPath);
109
110                         testPath = Path.ChangeExtension (null, ".extension");
111                         AssertEquals ("ChangeExtension #03", null, testPath);
112
113                         testPath = Path.ChangeExtension ("path", null);
114                         AssertEquals ("ChangeExtension #04", "path", testPath);
115
116                         testPath = Path.ChangeExtension ("path.ext", "doc");
117                         AssertEquals ("ChangeExtension #05", "path.doc", testPath);
118
119                         testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
120                         AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
121
122                         if (Windows) {
123                                 try {
124                                         testPath = Path.ChangeExtension ("<", ".extension");
125                                         Fail ("ChangeException Fail #01");
126                                 } catch (Exception e) {
127                                         AssertEquals ("ChangeExtension Exc. #01", typeof (ArgumentException), e.GetType ());
128                                 }
129                         }
130                 }
131
132                 public void TestCombine ()
133                 {
134                         string [] files = new string [3];
135                         files [(int) OsType.Unix] = "/etc/init.d";
136                         files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
137                         files [(int) OsType.Mac] = "foo:bar";
138
139                         string testPath = Path.Combine (path2, path3);
140                         AssertEquals ("Combine #01", files [(int) OS], testPath);
141
142                         testPath = Path.Combine ("one", "");
143                         AssertEquals ("Combine #02", "one", testPath);
144
145                         testPath = Path.Combine ("", "one");
146                         AssertEquals ("Combine #03", "one", testPath);
147
148                         string current = Directory.GetCurrentDirectory ();
149                         testPath = Path.Combine (current, "one");
150
151                         string expected = current + DSC + "one";
152                         AssertEquals ("Combine #04", expected, testPath);
153
154                         testPath = Path.Combine ("one", current);
155                         // LAMESPEC noted in Path.cs
156                         AssertEquals ("Combine #05", current, testPath);
157
158                         testPath = Path.Combine (current, expected);
159                         AssertEquals ("Combine #06", expected, testPath);
160
161                         testPath = DSC + "one";
162                         testPath = Path.Combine (testPath, "two" + DSC);
163                         expected = DSC + "one" + DSC + "two" + DSC;
164                         AssertEquals ("Combine #06", expected, testPath);
165
166                         testPath = "one" + DSC;
167                         testPath = Path.Combine (testPath, DSC + "two");
168                         expected = DSC + "two";
169                         AssertEquals ("Combine #06", expected, testPath);
170
171                         testPath = "one" + DSC;
172                         testPath = Path.Combine (testPath, "two" + DSC);
173                         expected = "one" + DSC + "two" + DSC;
174                         AssertEquals ("Combine #07", expected, testPath);
175
176                         //TODO: Tests for UNC names
177                         try {
178                                 testPath = Path.Combine ("one", null);
179                                 Fail ("Combine Fail #01");
180                         } catch (Exception e) {
181                                 AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
182                         }
183
184                         try {
185                                 testPath = Path.Combine (null, "one");
186                                 Fail ("Combine Fail #02");
187                         } catch (Exception e) {
188                                 AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
189                         }
190
191                         if (Windows) {
192                                 try {
193                                         testPath = Path.Combine ("a>", "one");
194                                         Fail ("Combine Fail #03");
195                                 } catch (Exception e) {
196                                         AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
197                                 }
198
199                                 try {
200                                         testPath = Path.Combine ("one", "aaa<");
201                                         Fail ("Combine Fail #04");
202                                 } catch (Exception e) {
203                                         AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
204                                 }
205                         }
206                 }
207
208                 [Test]
209                 [ExpectedException (typeof(ArgumentException))]
210                 public void EmptyDirectoryName ()
211                 {
212                         string testDirName = Path.GetDirectoryName ("");
213                 }
214
215                 public void TestDirectoryName ()
216                 {
217                         string [] files = new string [3];
218                         files [(int) OsType.Unix] = "/foo";
219                         files [(int) OsType.Windows] = "c:\\foo";
220                         files [(int) OsType.Mac] = "foo";
221
222                         AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
223                         string testDirName = Path.GetDirectoryName (path1);
224                         AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
225                         testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
226                         AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
227
228                         if (Windows) {
229                                 try {
230                                         testDirName = Path.GetDirectoryName ("aaa>");
231                                         Fail ("GetDirectoryName Fail #02");
232                                 } catch (Exception e) {
233                                         AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
234                                 }
235                         }
236
237                         try {
238                                 testDirName = Path.GetDirectoryName ("   ");
239                                 Fail ("GetDirectoryName Fail #03");
240                         } catch (Exception e) {
241                                 AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
242                         }
243                 }
244
245                 public void TestGetExtension ()
246                 {
247                         string testExtn = Path.GetExtension (path1);
248
249                         AssertEquals ("GetExtension #01",  ".txt", testExtn);
250
251                         testExtn = Path.GetExtension (path2);
252                         AssertEquals ("GetExtension #02", String.Empty, testExtn);
253
254                         testExtn = Path.GetExtension (String.Empty);
255                         AssertEquals ("GetExtension #03", String.Empty, testExtn);
256
257                         testExtn = Path.GetExtension (null);
258                         AssertEquals ("GetExtension #04", null, testExtn);
259
260                         testExtn = Path.GetExtension (" ");
261                         AssertEquals ("GetExtension #05", String.Empty, testExtn);
262
263                         testExtn = Path.GetExtension (path1 + ".doc");
264                         AssertEquals ("GetExtension #06", ".doc", testExtn);
265
266                         testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
267                         AssertEquals ("GetExtension #07", ".txt", testExtn);
268
269                         if (Windows) {
270                                 try {
271                                         testExtn = Path.GetExtension ("hi<there.txt");
272                                         Fail ("GetExtension Fail #01");
273                                 } catch (Exception e) {
274                                         AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
275                                 }
276                         }
277                 }
278
279                 public void TestGetFileName ()
280                 {
281                         string testFileName = Path.GetFileName (path1);
282
283                         AssertEquals ("GetFileName #01", "test.txt", testFileName);
284                         testFileName = Path.GetFileName (null);
285                         AssertEquals ("GetFileName #02", null, testFileName);
286                         testFileName = Path.GetFileName (String.Empty);
287                         AssertEquals ("GetFileName #03", String.Empty, testFileName);
288                         testFileName = Path.GetFileName (" ");
289                         AssertEquals ("GetFileName #04", " ", testFileName);
290
291                         if (Windows) {
292                                 try {
293                                         testFileName = Path.GetFileName ("hi<");
294                                         Fail ("GetFileName Fail #01");
295                                 } catch (Exception e) {
296                                         AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
297                                 }
298                         }
299                 }
300
301                 public void TestGetFileNameWithoutExtension ()
302                 {
303                         string testFileName = Path.GetFileNameWithoutExtension (path1);
304
305                         AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
306
307                         testFileName = Path.GetFileNameWithoutExtension (null);
308                         AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
309
310                         testFileName = Path.GetFileNameWithoutExtension (String.Empty);
311                         AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
312                 }
313
314                 [Ignore("This does not work under windows. See ERROR comments below.")]
315                 public void TestGetFullPath ()
316                 {
317                         string current = Directory.GetCurrentDirectory ();
318
319                         string testFullPath = Path.GetFullPath ("foo.txt");
320                         string expected = current + DSC + "foo.txt";
321                         AssertEquals ("GetFullPath #01", expected, testFullPath);
322
323                         testFullPath = Path.GetFullPath ("a//./.././foo.txt");
324                         AssertEquals ("GetFullPath #02", expected, testFullPath);
325                         string root = Windows ? "C:\\" : "/";
326                         string [,] test = new string [,] {              
327                                 {"root////././././././../root/././../root", "root"},
328                                 {"root/", "root/"},
329                                 {"root/./", "root/"},
330                                 {"root/./", "root/"},
331                                 {"root/../", ""},
332                                 {"root/../", ""},
333                                 {"root/../..", ""},
334                                 {"root/.hiddenfile", "root/.hiddenfile"},
335                                 {"root/. /", "root/. /"},
336                                 {"root/.. /", "root/.. /"},
337                                 {"root/..weirdname", "root/..weirdname"},
338                                 {"root/..", ""},
339                                 {"root/../a/b/../../..", ""},
340                                 {"root/./..", ""},
341                                 {"..", ""},
342                                 {".", ""},
343                                 {"root//dir", "root/dir"},
344                                 {"root/.              /", "root/.              /"},
345                                 {"root/..             /", "root/..             /"},
346                                 {"root/      .              /", "root/      .              /"},
347                                 {"root/      ..             /", "root/      ..             /"},
348                                 {"root/./", "root/"},
349                                 //ERROR! Paths are trimmed
350                                 {"root/..                      /", "root/..                   /"},
351                                 {".//", ""}
352                         };
353
354                         //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
355                         //... so only the first test was being done.
356                         for (int i = 0; i < test.GetUpperBound (1); i++) {
357                                 AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
358                         }
359                         
360                         if (Windows) {
361                                 string uncroot = @"\\server\share\";
362                                 string [,] testunc = new string [,] {           
363                                         {"root////././././././../root/././../root", "root"},
364                                         {"root/", "root/"},
365                                         {"root/./", "root/"},
366                                         {"root/./", "root/"},
367                                         {"root/../", ""},
368                                         {"root/../", ""},
369                                         {"root/../..", ""},
370                                         {"root/.hiddenfile", "root/.hiddenfile"},
371                                         {"root/. /", "root/. /"},
372                                         {"root/.. /", "root/.. /"},
373                                         {"root/..weirdname", "root/..weirdname"},
374                                         {"root/..", ""},
375                                         {"root/../a/b/../../..", ""},
376                                         {"root/./..", ""},
377                                         {"..", ""},
378                                         {".", ""},
379                                         {"root//dir", "root/dir"},
380                                         {"root/.              /", "root/.              /"},
381                                         {"root/..             /", "root/..             /"},
382                                         {"root/      .              /", "root/      .              /"},
383                                         {"root/      ..             /", "root/      ..             /"},
384                                         {"root/./", "root/"},
385                                         {"root/..                      /", "root/..                   /"},
386                                         {".//", ""}
387                                 };
388                                 for (int i = 0; i < test.GetUpperBound (1); i++) {
389                                         AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
390                                 }       
391                         }
392                         
393                         try {
394                                 testFullPath = Path.GetFullPath (null);
395                                 Fail ("GetFullPath Fail #01");
396                         } catch (Exception e) {
397                                 AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
398                         }
399
400                         try {
401                                 testFullPath = Path.GetFullPath (String.Empty);
402                                 Fail ("GetFullPath Fail #02");
403                         } catch (Exception e) {
404                                 AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
405                         }
406                 }
407                 
408                 public void TestGetPathRoot ()
409                 {
410                         string current;
411                         string expected;
412                         if (!Windows){
413                                 current = Directory.GetCurrentDirectory ();
414                                 expected = current [0].ToString ();
415                         } else {
416                                 current = @"J:\Some\Strange Directory\Name";
417                                 expected = "J:\\";
418                         }
419
420                         string pathRoot = Path.GetPathRoot (current);
421                         AssertEquals ("GetPathRoot #01", expected, pathRoot);
422
423                         pathRoot = Path.GetPathRoot ("hola");
424                         AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
425
426                         pathRoot = Path.GetPathRoot (null);
427                         AssertEquals ("GetPathRoot #03", null, pathRoot);
428                 }
429
430                 public void TestGetTempPath ()
431                 {
432                         string getTempPath = Path.GetTempPath ();
433                         Assert ("GetTempPath #01",  getTempPath != String.Empty);
434                         Assert ("GetTempPath #02",  Path.IsPathRooted (getTempPath));
435                 }
436
437                 public void TestGetTempFileName ()
438                 {
439                         string getTempFileName = null;
440                         try {
441                                 getTempFileName = Path.GetTempFileName ();
442                                 Assert ("GetTempFileName #01", getTempFileName != String.Empty);
443                                 Assert ("GetTempFileName #02", File.Exists (getTempFileName));
444                         } finally {
445                                 if (getTempFileName != null && getTempFileName != String.Empty){
446                                         File.Delete (getTempFileName);
447                                 }
448                         }
449                 }
450
451                 public void TestHasExtension ()
452                 {
453                         AssertEquals ("HasExtension #01",  true, Path.HasExtension ("foo.txt"));
454                         AssertEquals ("HasExtension #02",  false, Path.HasExtension ("foo"));
455                         AssertEquals ("HasExtension #03",  true, Path.HasExtension (path1));
456                         AssertEquals ("HasExtension #04",  false, Path.HasExtension (path2));
457                 }
458
459                 public void TestRooted ()
460                 {
461                         Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
462                         Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
463                         Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
464                 }
465
466                 public void TestCanonicalizeDots ()
467                 {
468                         string current = Path.GetFullPath (".");
469                         Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
470                         string parent = Path.GetFullPath ("..");
471                         Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
472                 }
473 #if !NUNIT
474                 void Assert (string msg, bool result)
475                 {
476                         if (!result)
477                                 Console.WriteLine (msg);
478                 }
479
480                 void AssertEquals (string msg, object expected, object real)
481                 {
482                         if (expected == null && real == null)
483                                 return;
484
485                         if (expected != null && expected.Equals (real))
486                                 return;
487
488                         Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
489                 }
490
491                 void Fail (string msg)
492                 {
493                         Console.WriteLine ("Failed: {0}", msg);
494                 }
495
496                 static void Main ()
497                 {
498                         PathTest p = new PathTest ();
499                         Type t = p.GetType ();
500                         MethodInfo [] methods = t.GetMethods ();
501                         foreach (MethodInfo m in methods) {
502                                 if (m.Name.Substring (0, 4) == "Test") {
503                                         m.Invoke (p, null);
504                                 }
505                         }
506                 }
507 #endif
508         }
509 }
510