2007-11-02 Robert Jordan <robertj@gmx.net>
[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 //      Atsushi Enomoto (atsushi@ximian.com)
10 //      Sebastien Pouliot  <sebastien@ximian.com>
11 //
12 // (c) Marcin Szczepanski 
13 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
14 // (c) 2003 Ben Maurer
15 // (c) 2003 Gilles Freart
16 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
17 //
18
19 using NUnit.Framework;
20 using System.IO;
21 using System;
22 using System.Text;
23
24 namespace MonoTests.System.IO
25 {
26
27         enum OsType {
28                 Windows,
29                 Unix,
30                 Mac
31         }
32
33         [TestFixture]
34         public class PathTest : Assertion
35         {
36                 static string path1;
37                 static string path2;
38                 static string path3;
39                 static OsType OS;
40                 static char DSC = Path.DirectorySeparatorChar;
41              
42                 [SetUp]
43                 public void SetUp ()
44                 {
45                         if ('/' == DSC) {
46                                 OS = OsType.Unix;
47                                 path1 = "/foo/test.txt";
48                                 path2 = "/etc";
49                                 path3 = "init.d";
50                         } else if ('\\' == DSC) {
51                                 OS = OsType.Windows;
52                                 path1 = "c:\\foo\\test.txt";
53                                 path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
54                                 path3 = "system32";
55                         } else {
56                                 OS = OsType.Mac;
57                                 //FIXME: For Mac. figure this out when we need it
58                                 path1 = "foo:test.txt";
59                                 path2 = "foo";
60                                 path3 = "bar";
61                         }
62                 }
63
64                 bool Windows
65                 {
66                         get {
67                                 return OS == OsType.Windows;
68                         }
69                 }
70
71                 bool Unix
72                 {
73                         get {
74                                 return OS == OsType.Unix;
75                         }
76                 }
77
78                 bool Mac
79                 {
80                         get {
81                                 return OS == OsType.Mac;
82                         }
83                 }
84
85                 public void TestChangeExtension ()
86                 {
87                         string [] files = new string [3];
88                         files [(int) OsType.Unix] = "/foo/test.doc";
89                         files [(int) OsType.Windows] = "c:\\foo\\test.doc";
90                         files [(int) OsType.Mac] = "foo:test.doc";
91
92                         string testPath = Path.ChangeExtension (path1, "doc");
93                         AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
94
95                         testPath = Path.ChangeExtension ("", ".extension");
96                         AssertEquals ("ChangeExtension #02", String.Empty, testPath);
97
98                         testPath = Path.ChangeExtension (null, ".extension");
99                         AssertEquals ("ChangeExtension #03", null, testPath);
100
101                         testPath = Path.ChangeExtension ("path", null);
102                         AssertEquals ("ChangeExtension #04", "path", testPath);
103
104                         testPath = Path.ChangeExtension ("path.ext", "doc");
105                         AssertEquals ("ChangeExtension #05", "path.doc", testPath);
106
107                         testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
108                         AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
109
110                         testPath = Path.ChangeExtension ("hogehoge.xml", ".xsl");
111                         AssertEquals ("ChangeExtension #07", "hogehoge.xsl", testPath);
112                         testPath = Path.ChangeExtension ("hogehoge", ".xsl");
113                         AssertEquals ("ChangeExtension #08", "hogehoge.xsl", testPath);
114                         testPath = Path.ChangeExtension ("hogehoge.xml", "xsl");
115                         AssertEquals ("ChangeExtension #09", "hogehoge.xsl", testPath);
116                         testPath = Path.ChangeExtension ("hogehoge", "xsl");
117                         AssertEquals ("ChangeExtension #10", "hogehoge.xsl", testPath);
118                         testPath = Path.ChangeExtension ("hogehoge.xml", String.Empty);
119                         AssertEquals ("ChangeExtension #11", "hogehoge.", testPath);
120                         testPath = Path.ChangeExtension ("hogehoge", String.Empty);
121                         AssertEquals ("ChangeExtension #12", "hogehoge.", testPath);
122                         testPath = Path.ChangeExtension ("hogehoge.", null);
123                         AssertEquals ("ChangeExtension #13", "hogehoge", testPath);
124                         testPath = Path.ChangeExtension ("hogehoge", null);
125                         AssertEquals ("ChangeExtension #14", "hogehoge", testPath);
126                         testPath = Path.ChangeExtension (String.Empty, null);
127                         AssertEquals ("ChangeExtension #15", String.Empty, testPath);
128                         testPath = Path.ChangeExtension (String.Empty, "bashrc");
129                         AssertEquals ("ChangeExtension #16", String.Empty, testPath);
130                         testPath = Path.ChangeExtension (String.Empty, ".bashrc");
131                         AssertEquals ("ChangeExtension #17", String.Empty, testPath);
132                         testPath = Path.ChangeExtension (null, null);
133                         AssertNull ("ChangeExtension #18", testPath);
134                 }
135
136                 [Test]
137                 [ExpectedException (typeof (ArgumentException))]
138                 public void ChangeExtension_BadPath () 
139                 {
140                         if (!Windows) throw new ArgumentException ("Test Only On Windows");
141                         Path.ChangeExtension ("<", ".extension");
142                 }
143
144                 [Test]
145                 public void ChangeExtension_BadExtension () 
146                 {
147                         if (Windows) {
148                                 string fn = Path.ChangeExtension ("file.ext", "<");
149                                 AssertEquals ("Invalid filename", "file.<", fn);
150                         }
151                 }
152
153                 public void TestCombine ()
154                 {
155                         string [] files = new string [3];
156                         files [(int) OsType.Unix] = "/etc/init.d";
157                         files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
158                         files [(int) OsType.Mac] = "foo:bar";
159
160                         string testPath = Path.Combine (path2, path3);
161                         AssertEquals ("Combine #01", files [(int) OS], testPath);
162
163                         testPath = Path.Combine ("one", "");
164                         AssertEquals ("Combine #02", "one", testPath);
165
166                         testPath = Path.Combine ("", "one");
167                         AssertEquals ("Combine #03", "one", testPath);
168
169                         string current = Directory.GetCurrentDirectory ();
170                         testPath = Path.Combine (current, "one");
171
172                         string expected = current + DSC + "one";
173                         AssertEquals ("Combine #04", expected, testPath);
174
175                         testPath = Path.Combine ("one", current);
176                         // LAMESPEC noted in Path.cs
177                         AssertEquals ("Combine #05", current, testPath);
178
179                         testPath = Path.Combine (current, expected);
180                         AssertEquals ("Combine #06", expected, testPath);
181
182                         testPath = DSC + "one";
183                         testPath = Path.Combine (testPath, "two" + DSC);
184                         expected = DSC + "one" + DSC + "two" + DSC;
185                         AssertEquals ("Combine #06", expected, testPath);
186
187                         testPath = "one" + DSC;
188                         testPath = Path.Combine (testPath, DSC + "two");
189                         expected = DSC + "two";
190                         AssertEquals ("Combine #06", expected, testPath);
191
192                         testPath = "one" + DSC;
193                         testPath = Path.Combine (testPath, "two" + DSC);
194                         expected = "one" + DSC + "two" + DSC;
195                         AssertEquals ("Combine #07", expected, testPath);
196
197                         //TODO: Tests for UNC names
198                         try {
199                                 testPath = Path.Combine ("one", null);
200                                 Fail ("Combine Fail #01");
201                         } catch (Exception e) {
202                                 AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
203                         }
204
205                         try {
206                                 testPath = Path.Combine (null, "one");
207                                 Fail ("Combine Fail #02");
208                         } catch (Exception e) {
209                                 AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
210                         }
211
212                         if (Windows) {
213                                 try {
214                                         testPath = Path.Combine ("a>", "one");
215                                         Fail ("Combine Fail #03");
216                                 } catch (Exception e) {
217                                         AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
218                                 }
219
220                                 try {
221                                         testPath = Path.Combine ("one", "aaa<");
222                                         Fail ("Combine Fail #04");
223                                 } catch (Exception e) {
224                                         AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
225                                 }
226                         }
227                 }
228
229                 [Test]
230                 [ExpectedException (typeof(ArgumentException))]
231                 public void EmptyDirectoryName ()
232                 {
233                         string testDirName = Path.GetDirectoryName ("");
234                 }
235
236                 public void TestDirectoryName ()
237                 {
238                         string [] files = new string [3];
239                         files [(int) OsType.Unix] = "/foo";
240                         files [(int) OsType.Windows] = "c:\\foo";
241                         files [(int) OsType.Mac] = "foo";
242
243                         AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
244                         string testDirName = Path.GetDirectoryName (path1);
245                         AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
246                         testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
247                         AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
248
249                         if (Windows) {
250                                 try {
251                                         testDirName = Path.GetDirectoryName ("aaa>");
252                                         Fail ("GetDirectoryName Fail #02");
253                                 } catch (Exception e) {
254                                         AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
255                                 }
256                         }
257
258                         try {
259                                 testDirName = Path.GetDirectoryName ("   ");
260                                 Fail ("GetDirectoryName Fail #03");
261                         } catch (Exception e) {
262                                 AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
263                         }
264
265                         if (Windows) {
266                                 AssertEquals ("GetDirectoryName #04", null, Path.GetDirectoryName ("C:"));
267                                 AssertEquals ("GetDirectoryName #05", null, Path.GetDirectoryName (@"C:\"));
268                                 AssertEquals ("GetDirectoryName #06", @"C:\", Path.GetDirectoryName (@"C:\dir"));
269                                 AssertEquals ("GetDirectoryName #07", @"C:\dir", Path.GetDirectoryName (@"C:\dir\"));
270                                 AssertEquals ("GetDirectoryName #08", @"C:\dir", Path.GetDirectoryName (@"C:\dir\dir"));
271                                 AssertEquals ("GetDirectoryName #09", @"C:\dir\dir", Path.GetDirectoryName (@"C:\dir\dir\"));
272
273                                 // UNC tests
274                                 AssertEquals ("GetDirectoryName #10", null, Path.GetDirectoryName (@"\\"));
275                                 AssertEquals ("GetDirectoryName #11", null, Path.GetDirectoryName (@"\\server"));
276                                 AssertEquals ("GetDirectoryName #12", null, Path.GetDirectoryName (@"\\server\share"));
277
278                                 AssertEquals ("GetDirectoryName #13", @"\\server\share", Path.GetDirectoryName (@"\\server\share\"));
279                                 AssertEquals ("GetDirectoryName #14", @"\\server\share", Path.GetDirectoryName (@"\\server\share\dir"));
280                                 AssertEquals ("GetDirectoryName #15", @"\\server\share\dir", Path.GetDirectoryName (@"\\server\share\dir\subdir"));
281                         }
282                                         
283                 }
284
285                 public void TestGetExtension ()
286                 {
287                         string testExtn = Path.GetExtension (path1);
288
289                         AssertEquals ("GetExtension #01",  ".txt", testExtn);
290
291                         testExtn = Path.GetExtension (path2);
292                         AssertEquals ("GetExtension #02", String.Empty, testExtn);
293
294                         testExtn = Path.GetExtension (String.Empty);
295                         AssertEquals ("GetExtension #03", String.Empty, testExtn);
296
297                         testExtn = Path.GetExtension (null);
298                         AssertEquals ("GetExtension #04", null, testExtn);
299
300                         testExtn = Path.GetExtension (" ");
301                         AssertEquals ("GetExtension #05", String.Empty, testExtn);
302
303                         testExtn = Path.GetExtension (path1 + ".doc");
304                         AssertEquals ("GetExtension #06", ".doc", testExtn);
305
306                         testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
307                         AssertEquals ("GetExtension #07", ".txt", testExtn);
308
309                         testExtn = Path.GetExtension (".");
310                         AssertEquals ("GetExtension #08", String.Empty, testExtn);
311
312                         testExtn = Path.GetExtension ("end.");
313                         AssertEquals ("GetExtension #09", String.Empty, testExtn);
314
315                         testExtn = Path.GetExtension (".start");
316                         AssertEquals ("GetExtension #10", ".start", testExtn);
317
318                         testExtn = Path.GetExtension (".a");
319                         AssertEquals ("GetExtension #11", ".a", testExtn);
320
321                         testExtn = Path.GetExtension ("a.");
322                         AssertEquals ("GetExtension #12", String.Empty, testExtn);
323
324                         testExtn = Path.GetExtension ("a");
325                         AssertEquals ("GetExtension #13", String.Empty, testExtn);
326
327                         testExtn = Path.GetExtension ("makefile");
328                         AssertEquals ("GetExtension #14", String.Empty, testExtn);
329
330                         if (Windows) {
331                                 try {
332                                         testExtn = Path.GetExtension ("hi<there.txt");
333                                         Fail ("GetExtension Fail #01");
334                                 } catch (Exception e) {
335                                         AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
336                                 }
337                         }
338                 }
339
340                 public void TestGetFileName ()
341                 {
342                         string testFileName = Path.GetFileName (path1);
343
344                         AssertEquals ("GetFileName #01", "test.txt", testFileName);
345                         testFileName = Path.GetFileName (null);
346                         AssertEquals ("GetFileName #02", null, testFileName);
347                         testFileName = Path.GetFileName (String.Empty);
348                         AssertEquals ("GetFileName #03", String.Empty, testFileName);
349                         testFileName = Path.GetFileName (" ");
350                         AssertEquals ("GetFileName #04", " ", testFileName);
351
352                         if (Windows) {
353                                 try {
354                                         testFileName = Path.GetFileName ("hi<");
355                                         Fail ("GetFileName Fail #01");
356                                 } catch (Exception e) {
357                                         AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
358                                 }
359                         }
360                 }
361
362                 public void TestGetFileNameWithoutExtension ()
363                 {
364                         string testFileName = Path.GetFileNameWithoutExtension (path1);
365
366                         AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
367
368                         testFileName = Path.GetFileNameWithoutExtension (null);
369                         AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
370
371                         testFileName = Path.GetFileNameWithoutExtension (String.Empty);
372                         AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
373                 }
374
375                 [Ignore("This does not work under windows. See ERROR comments below.")]
376                 public void TestGetFullPath ()
377                 {
378                         string current = Directory.GetCurrentDirectory ();
379
380                         string testFullPath = Path.GetFullPath ("foo.txt");
381                         string expected = current + DSC + "foo.txt";
382                         AssertEquals ("GetFullPath #01", expected, testFullPath);
383
384                         testFullPath = Path.GetFullPath ("a//./.././foo.txt");
385                         AssertEquals ("GetFullPath #02", expected, testFullPath);
386                         string root = Windows ? "C:\\" : "/";
387                         string [,] test = new string [,] {              
388                                 {"root////././././././../root/././../root", "root"},
389                                 {"root/", "root/"},
390                                 {"root/./", "root/"},
391                                 {"root/./", "root/"},
392                                 {"root/../", ""},
393                                 {"root/../", ""},
394                                 {"root/../..", ""},
395                                 {"root/.hiddenfile", "root/.hiddenfile"},
396                                 {"root/. /", "root/. /"},
397                                 {"root/.. /", "root/.. /"},
398                                 {"root/..weirdname", "root/..weirdname"},
399                                 {"root/..", ""},
400                                 {"root/../a/b/../../..", ""},
401                                 {"root/./..", ""},
402                                 {"..", ""},
403                                 {".", ""},
404                                 {"root//dir", "root/dir"},
405                                 {"root/.              /", "root/.              /"},
406                                 {"root/..             /", "root/..             /"},
407                                 {"root/      .              /", "root/      .              /"},
408                                 {"root/      ..             /", "root/      ..             /"},
409                                 {"root/./", "root/"},
410                                 //ERROR! Paths are trimmed
411                                 {"root/..                      /", "root/..                   /"},
412                                 {".//", ""}
413                         };
414
415                         //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
416                         //... so only the first test was being done.
417                         for (int i = 0; i < test.GetUpperBound (1); i++) {
418                                 AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
419                         }
420                         
421                         if (Windows) {
422                                 string uncroot = @"\\server\share\";
423                                 string [,] testunc = new string [,] {           
424                                         {"root////././././././../root/././../root", "root"},
425                                         {"root/", "root/"},
426                                         {"root/./", "root/"},
427                                         {"root/./", "root/"},
428                                         {"root/../", ""},
429                                         {"root/../", ""},
430                                         {"root/../..", ""},
431                                         {"root/.hiddenfile", "root/.hiddenfile"},
432                                         {"root/. /", "root/. /"},
433                                         {"root/.. /", "root/.. /"},
434                                         {"root/..weirdname", "root/..weirdname"},
435                                         {"root/..", ""},
436                                         {"root/../a/b/../../..", ""},
437                                         {"root/./..", ""},
438                                         {"..", ""},
439                                         {".", ""},
440                                         {"root//dir", "root/dir"},
441                                         {"root/.              /", "root/.              /"},
442                                         {"root/..             /", "root/..             /"},
443                                         {"root/      .              /", "root/      .              /"},
444                                         {"root/      ..             /", "root/      ..             /"},
445                                         {"root/./", "root/"},
446                                         {"root/..                      /", "root/..                   /"},
447                                         {".//", ""}
448                                 };
449                                 for (int i = 0; i < test.GetUpperBound (1); i++) {
450                                         AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
451                                 }       
452                         }
453                         
454                         try {
455                                 testFullPath = Path.GetFullPath (null);
456                                 Fail ("GetFullPath Fail #01");
457                         } catch (Exception e) {
458                                 AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
459                         }
460
461                         try {
462                                 testFullPath = Path.GetFullPath (String.Empty);
463                                 Fail ("GetFullPath Fail #02");
464                         } catch (Exception e) {
465                                 AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
466                         }
467                 }
468
469                 public void TestGetFullPath2 ()
470                 {
471                         if (Windows) {
472                                 AssertEquals ("GetFullPath w#01", @"Z:\", Path.GetFullPath ("Z:"));
473 #if !TARGET_JVM // Java full (canonical) path always starts with caps drive letter
474                                 AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path.GetFullPath (@"c:\abc\def"));
475 #endif
476                                 Assert ("GetFullPath w#03", Path.GetFullPath (@"\").EndsWith (@"\"));
477                                 // "\\\\" is not allowed
478                                 Assert ("GetFullPath w#05", Path.GetFullPath ("/").EndsWith (@"\"));
479                                 // "//" is not allowed
480                                 Assert ("GetFullPath w#07", Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
481                                 Assert ("GetFullPath w#08", Path.GetFullPath ("c").EndsWith (@"\c"));
482                                 Assert ("GetFullPath w#09", Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
483                                 Assert ("GetFullPath w#10", Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
484                                 AssertEquals ("GetFullPath w#11", @"\\abc\def", Path.GetFullPath (@"\\abc\def"));
485                                 AssertEquals ("GetFullPath w#12", Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"));
486                                 AssertEquals ("GetFullPath w#13", Directory.GetCurrentDirectory ().Substring (0,2) + @"\abc\def", Path.GetFullPath ("/abc/def"));
487                                 AssertEquals ("GetFullPath w#14", @"\\abc\def", Path.GetFullPath ("//abc/def"));
488                         }
489                 }
490
491                 public void TestGetPathRoot ()
492                 {
493                         string current;
494                         string expected;
495                         if (!Windows){
496                                 current = Directory.GetCurrentDirectory ();
497                                 expected = current [0].ToString ();
498                         } else {
499                                 current = @"J:\Some\Strange Directory\Name";
500                                 expected = "J:\\";
501                         }
502
503                         string pathRoot = Path.GetPathRoot (current);
504                         AssertEquals ("GetPathRoot #01", expected, pathRoot);
505                 }
506
507                 [Test]
508                 public void TestGetPathRoot2 ()
509                 {
510                         // note: this method doesn't call Directory.GetCurrentDirectory so it can be
511                         // reused for partial trust unit tests in PathCas.cs
512
513                         string pathRoot = Path.GetPathRoot ("hola");
514                         AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
515
516                         pathRoot = Path.GetPathRoot (null);
517                         AssertEquals ("GetPathRoot #03", null, pathRoot);
518
519                         if (Windows) {
520                                 AssertEquals ("GetPathRoot w#01", "z:", Path.GetPathRoot ("z:"));
521                                 AssertEquals ("GetPathRoot w#02", "c:\\", Path.GetPathRoot ("c:\\abc\\def"));
522                                 AssertEquals ("GetPathRoot w#03", "\\", Path.GetPathRoot ("\\"));
523                                 AssertEquals ("GetPathRoot w#04", "\\\\", Path.GetPathRoot ("\\\\"));
524                                 AssertEquals ("GetPathRoot w#05", "\\", Path.GetPathRoot ("/"));
525                                 AssertEquals ("GetPathRoot w#06", "\\\\", Path.GetPathRoot ("//"));
526                                 AssertEquals ("GetPathRoot w#07", String.Empty, Path.GetPathRoot ("readme.txt"));
527                                 AssertEquals ("GetPathRoot w#08", String.Empty, Path.GetPathRoot ("c"));
528                                 AssertEquals ("GetPathRoot w#09", String.Empty, Path.GetPathRoot ("abc\\def"));
529                                 AssertEquals ("GetPathRoot w#10", "\\", Path.GetPathRoot ("\\abc\\def"));
530                                 AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"));
531                                 AssertEquals ("GetPathRoot w#12", String.Empty, Path.GetPathRoot ("abc//def"));
532                                 AssertEquals ("GetPathRoot w#13", "\\", Path.GetPathRoot ("/abc/def"));
533                                 AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path.GetPathRoot ("//abc/def"));
534                                 AssertEquals ("GetPathRoot w#15", @"C:\", Path.GetPathRoot (@"C:\"));
535                                 AssertEquals ("GetPathRoot w#16", @"C:\", Path.GetPathRoot (@"C:\\"));
536                                 AssertEquals ("GetPathRoot w#17", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def\\ghi"));
537                         } else {
538                                 // TODO: Same tests for Unix.
539                         }
540                 }
541
542                 public void TestGetTempPath ()
543                 {
544                         string getTempPath = Path.GetTempPath ();
545                         Assert ("GetTempPath #01",  getTempPath != String.Empty);
546                         Assert ("GetTempPath #02",  Path.IsPathRooted (getTempPath));
547                         AssertEquals ("GetTempPath #03", Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1]);
548                 }
549
550                 public void TestGetTempFileName ()
551                 {
552                         string getTempFileName = null;
553                         try {
554                                 getTempFileName = Path.GetTempFileName ();
555                                 Assert ("GetTempFileName #01", getTempFileName != String.Empty);
556                                 Assert ("GetTempFileName #02", File.Exists (getTempFileName));
557                         } finally {
558                                 if (getTempFileName != null && getTempFileName != String.Empty){
559                                         File.Delete (getTempFileName);
560                                 }
561                         }
562                 }
563
564                 public void TestHasExtension ()
565                 {
566                         AssertEquals ("HasExtension #01",  true, Path.HasExtension ("foo.txt"));
567                         AssertEquals ("HasExtension #02",  false, Path.HasExtension ("foo"));
568                         AssertEquals ("HasExtension #03",  true, Path.HasExtension (path1));
569                         AssertEquals ("HasExtension #04",  false, Path.HasExtension (path2));
570                         AssertEquals ("HasExtension #05",  false, Path.HasExtension (null));
571                         AssertEquals ("HasExtension #06",  false, Path.HasExtension (String.Empty));
572                         AssertEquals ("HasExtension #07",  false, Path.HasExtension (" "));
573                         AssertEquals ("HasExtension #08",  false, Path.HasExtension ("."));
574                         AssertEquals ("HasExtension #09",  false, Path.HasExtension ("end."));
575                         AssertEquals ("HasExtension #10",  true, Path.HasExtension (".start"));
576                         AssertEquals ("HasExtension #11",  true, Path.HasExtension (".a"));
577                         AssertEquals ("HasExtension #12",  false, Path.HasExtension ("a."));
578                         AssertEquals ("HasExtension #13",  false, Path.HasExtension ("Makefile"));
579                 }
580
581                 public void TestRooted ()
582                 {
583                         Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
584                         Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
585                         Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
586                         Assert ("IsPathRooted #04", !Path.IsPathRooted (String.Empty));
587                         Assert ("IsPathRooted #05", !Path.IsPathRooted (" "));
588                         Assert ("IsPathRooted #06", Path.IsPathRooted ("/"));
589                         if (Windows)
590                                 Assert ("IsPathRooted #07", Path.IsPathRooted ("\\"));
591                         else
592                                 Assert ("IsPathRooted #07", !Path.IsPathRooted ("\\"));
593                         Assert ("IsPathRooted #08", Path.IsPathRooted ("//"));
594                         if (Windows)
595                                 Assert ("IsPathRooted #09", Path.IsPathRooted ("\\\\"));
596                         else
597                                 Assert ("IsPathRooted #09", !Path.IsPathRooted ("\\\\"));
598                         Assert ("IsPathRooted #10", !Path.IsPathRooted (":"));
599                         if (Windows)
600                                 Assert ("IsPathRooted #11", Path.IsPathRooted ("z:"));
601                         else
602                                 Assert ("IsPathRooted #11", !Path.IsPathRooted ("z:"));
603
604                         if (Windows) {
605                                 Assert ("IsPathRooted #12", Path.IsPathRooted ("z:\\"));
606                                 Assert ("IsPathRooted #13", Path.IsPathRooted ("z:\\topdir"));
607                                 // This looks MS BUG. It is treated as absolute path
608                                 Assert ("IsPathRooted #14", Path.IsPathRooted ("z:curdir"));
609                                 Assert ("IsPathRooted #15", Path.IsPathRooted ("\\abc\\def"));
610                         }
611                 }
612
613                 public void TestCanonicalizeDots ()
614                 {
615                         string current = Path.GetFullPath (".");
616                         Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
617                         string parent = Path.GetFullPath ("..");
618                         Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
619                 }
620
621                 public void TestDirectoryNameBugs ()
622                 {
623                         if (Windows) {
624                                 AssertEquals ("Win #01", "C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"));
625                         } else {
626                                 AssertEquals ("No win #01", "/etc", Path.GetDirectoryName ("/etc/hostname"));
627                         }
628                 }
629
630                 public void TestGetFullPathUnix ()
631                 {
632                         if (Windows)
633                                 return;
634
635                         AssertEquals ("#01", "/", Path.GetFullPath ("/"));
636                         AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
637                         AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
638                         AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
639                                              Path.GetFullPath ("hey"));
640                 }
641
642                 [Test]
643                 public void GetFullPath_EndingSeparator ()
644                 {
645                         string fp = Path.GetFullPath ("something/");
646                         char end = fp[fp.Length - 1];
647                         Assert (end == Path.DirectorySeparatorChar);
648                 }
649
650                 [Test]
651                 public void WindowsSystem32_76191 ()
652                 {
653                         // check for Unix platforms - see FAQ for more details
654                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
655                         int platform = (int) Environment.OSVersion.Platform;
656                         if ((platform == 4) || (platform == 128))
657                                 return;
658
659                         string curdir = Directory.GetCurrentDirectory ();
660                         try {
661 #if TARGET_JVM
662                 string system = "C:\\WINDOWS\\system32\\";
663 #else
664                                 string system = Environment.SystemDirectory;
665 #endif
666                                 Directory.SetCurrentDirectory (system);
667                                 string drive = system.Substring (0, 2);
668                                 AssertEquals ("current dir", system, Path.GetFullPath (drive));
669                         }
670                         finally {
671                                 Directory.SetCurrentDirectory (curdir);
672                         }
673                 }
674
675                 [Test]
676                 public void WindowsSystem32_77007 ()
677                 {
678                         // check for Unix platforms - see FAQ for more details
679                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
680                         int platform = (int) Environment.OSVersion.Platform;
681                         if ((platform == 4) || (platform == 128))
682                                 return;
683
684                         string curdir = Directory.GetCurrentDirectory ();
685                         try {
686 #if TARGET_JVM
687                 string system = "C:\\WINDOWS\\system32\\";
688 #else
689                                 string system = Environment.SystemDirectory;
690 #endif
691                                 Directory.SetCurrentDirectory (system);
692                                 // e.g. C:dir (no backslash) will return CurrentDirectory + dir
693                                 string dir = system.Substring (0, 2) + "dir";
694                                 AssertEquals ("current dir", Path.Combine (system, "dir"), Path.GetFullPath (dir));
695                         }
696                         finally {
697                                 Directory.SetCurrentDirectory (curdir);
698                         }
699                 }
700
701                 [Test]
702 #if TARGET_JVM
703         [Ignore("Java full (canonical) path always returns windows dir in caps")]
704 #endif
705                 public void WindowsDriveC14N_77058 ()
706                 {
707                         // check for Unix platforms - see FAQ for more details
708                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
709                         int platform = (int) Environment.OSVersion.Platform;
710                         if ((platform == 4) || (platform == 128))
711                                 return;
712
713                         AssertEquals ("1", @"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"));
714                         AssertEquals ("2", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"));
715                         AssertEquals ("3", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"));
716                         AssertEquals ("4", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"));
717                         AssertEquals ("5", @"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"));
718                 }
719
720                 [Test]
721                 public void InvalidPathChars_Values ()
722                 {
723                         char[] invalid = Path.InvalidPathChars;
724                         if (Windows) {
725 #if NET_2_0
726                                 AssertEquals ("Length", 36, invalid.Length);
727 #else
728                                 AssertEquals ("Length", 15, invalid.Length);
729 #endif
730                                 foreach (char c in invalid) {
731                                         int i = (int) c;
732 #if NET_2_0
733                                         if (i < 32)
734                                                 continue;
735 #else
736                                         if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
737                                                 continue;
738 #endif
739                                         // in both 1.1 SP1 and 2.0
740                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
741                                                 continue;
742                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
743                                 }
744                         } else {
745                                 foreach (char c in invalid) {
746                                         int i = (int) c;
747                                         if (i == 0)
748                                                 continue;
749                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
750                                 }
751                         }
752                 }
753
754                 [Test]
755                 public void InvalidPathChars_Modify ()
756                 {
757                         char[] expected = Path.InvalidPathChars;
758                         char[] invalid = Path.InvalidPathChars;
759                         char original = invalid[0];
760                         try {
761                                 invalid[0] = 'a';
762                                 // kind of scary
763                                 Assert ("expected", expected[0] == 'a');
764                                 AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]);
765                         }
766                         finally {
767                                 invalid[0] = original;
768                         }
769                 }
770 #if NET_2_0
771                 [Test]
772                 public void GetInvalidFileNameChars_Values ()
773                 {
774                         char[] invalid = Path.GetInvalidFileNameChars ();
775                         if (Windows) {
776                                 AssertEquals (41, invalid.Length);
777                                 foreach (char c in invalid) {
778                                         int i = (int) c;
779                                         if (i < 32)
780                                                 continue;
781                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
782                                                 continue;
783                                         // ':', '*', '?', '\', '/'
784                                         if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
785                                                 continue;
786                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
787                                 }
788                         } else {
789                                 foreach (char c in invalid) {
790                                         int i = (int) c;
791                                         // null or '/'
792                                         if ((i == 0) || (i == 47))
793                                                 continue;
794                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
795                                 }
796                         }
797                 }
798
799                 [Test]
800                 public void GetInvalidFileNameChars_Modify ()
801                 {
802                         char[] expected = Path.GetInvalidFileNameChars ();
803                         char[] invalid = Path.GetInvalidFileNameChars ();
804                         invalid[0] = 'a';
805                         Assert ("expected", expected[0] != 'a');
806                         AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]);
807                 }
808
809                 [Test]
810                 public void GetInvalidPathChars_Values ()
811                 {
812                         char[] invalid = Path.GetInvalidPathChars ();
813                         if (Windows) {
814                                 AssertEquals (36, invalid.Length);
815                                 foreach (char c in invalid) {
816                                         int i = (int) c;
817                                         if (i < 32)
818                                                 continue;
819                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
820                                                 continue;
821                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
822                                 }
823                         } else {
824                                 foreach (char c in invalid) {
825                                         int i = (int) c;
826                                         if (i == 0)
827                                                 continue;
828                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
829                                 }
830                         }
831                 }
832
833                 [Test]
834                 public void GetInvalidPathChars_Order()
835                 {
836                         if (Windows) {
837                                 char [] invalid = Path.GetInvalidPathChars ();
838                                 char [] expected = new char [36] { '\x22', '\x3C', '\x3E', '\x7C', '\x00', '\x01', '\x02',
839                                         '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D',
840                                         '\x0E', '\x0F', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18',
841                                         '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' };
842                                 AssertEquals (expected.Length, invalid.Length);
843                                 for (int i = 0; i < expected.Length; i++ ) {
844                                         AssertEquals( "Character at position " + i,expected [i], invalid [i]);
845                                 }
846                         }
847                 }
848
849                 [Test]
850                 public void GetInvalidPathChars_Modify ()
851                 {
852                         char[] expected = Path.GetInvalidPathChars ();
853                         char[] invalid = Path.GetInvalidPathChars ();
854                         invalid[0] = 'a';
855                         Assert ("expected", expected[0] != 'a');
856                         AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]);
857                 }
858
859                 [Test]
860                 public void GetRandomFileName ()
861                 {
862                         string s = Path.GetRandomFileName ();
863                         AssertEquals ("Length", 12, s.Length);
864                         char[] invalid = Path.GetInvalidFileNameChars ();
865                         for (int i=0; i < s.Length; i++) {
866                                 if (i == 8)
867                                         AssertEquals ("8", '.', s[i]);
868                                 else
869                                         Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1);
870                         }
871                 }
872
873                 [Test]
874                 public void TestGetDirectoryName ()
875                 {
876                         if (Unix){
877                                 AssertEquals ("GetDirectoryName#1", Path.GetDirectoryName ("/foo//bar/dingus"), "/foo/bar");
878                                 AssertEquals ("GDN#3", Path.GetDirectoryName ("foo/bar/"), "foo/bar");
879                                 AssertEquals ("GDN#6", Path.GetDirectoryName ("/tmp"), "/");
880                                 AssertEquals ("GDN#6", Path.GetDirectoryName ("/"), "");
881                         } else {
882                                 AssertEquals ("GetDirectoryName#1", Path.GetDirectoryName ("/foo//bar/dingus"), "\\foo\\bar");
883                                 AssertEquals ("GDN#4", Path.GetDirectoryName ("foo/bar/"), "foo\\bar");
884                                 AssertEquals ("GDN#5", Path.GetDirectoryName ("foo/bar\\xxx"), "foo\\bar");
885                                 AssertEquals ("GDN#2", Path.GetDirectoryName ("\\\\host\\dir\\\\dir2\\path"), "\\\\host\\dir\\dir2");
886                         }
887                 }
888 #endif
889         }
890 }
891