Test fixes for TARGET_JVM.
[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 #if !TARGET_JVM
654                         // check for Unix platforms - see FAQ for more details
655                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
656                         int platform = (int) Environment.OSVersion.Platform;
657                         if ((platform == 4) || (platform == 128))
658                                 return;
659 #endif
660
661                         string curdir = Directory.GetCurrentDirectory ();
662                         try {
663 #if TARGET_JVM
664                 string system = "C:\\WINDOWS\\system32\\";
665 #else
666                                 string system = Environment.SystemDirectory;
667 #endif
668                                 Directory.SetCurrentDirectory (system);
669                                 string drive = system.Substring (0, 2);
670                                 AssertEquals ("current dir", system, Path.GetFullPath (drive));
671                         }
672                         finally {
673                                 Directory.SetCurrentDirectory (curdir);
674                         }
675                 }
676
677                 [Test]
678                 public void WindowsSystem32_77007 ()
679                 {
680 #if !TARGET_JVM
681                         // check for Unix platforms - see FAQ for more details
682                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
683                         int platform = (int) Environment.OSVersion.Platform;
684                         if ((platform == 4) || (platform == 128))
685                                 return;
686 #endif
687
688                         string curdir = Directory.GetCurrentDirectory ();
689                         try {
690 #if TARGET_JVM
691                 string system = "C:\\WINDOWS\\system32\\";
692 #else
693                                 string system = Environment.SystemDirectory;
694 #endif
695                                 Directory.SetCurrentDirectory (system);
696                                 // e.g. C:dir (no backslash) will return CurrentDirectory + dir
697                                 string dir = system.Substring (0, 2) + "dir";
698                                 AssertEquals ("current dir", Path.Combine (system, "dir"), Path.GetFullPath (dir));
699                         }
700                         finally {
701                                 Directory.SetCurrentDirectory (curdir);
702                         }
703                 }
704
705                 [Test]
706 #if TARGET_JVM
707         [Ignore("Java full (canonical) path always returns windows dir in caps")]
708 #endif
709                 public void WindowsDriveC14N_77058 ()
710                 {
711                         // check for Unix platforms - see FAQ for more details
712                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
713                         int platform = (int) Environment.OSVersion.Platform;
714                         if ((platform == 4) || (platform == 128))
715                                 return;
716
717                         AssertEquals ("1", @"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"));
718                         AssertEquals ("2", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"));
719                         AssertEquals ("3", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"));
720                         AssertEquals ("4", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"));
721                         AssertEquals ("5", @"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"));
722                 }
723
724                 [Test]
725                 public void InvalidPathChars_Values ()
726                 {
727                         char[] invalid = Path.InvalidPathChars;
728                         if (Windows) {
729 #if NET_2_0
730                                 AssertEquals ("Length", 36, invalid.Length);
731 #else
732                                 AssertEquals ("Length", 15, invalid.Length);
733 #endif
734                                 foreach (char c in invalid) {
735                                         int i = (int) c;
736 #if NET_2_0
737                                         if (i < 32)
738                                                 continue;
739 #else
740                                         if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
741                                                 continue;
742 #endif
743                                         // in both 1.1 SP1 and 2.0
744                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
745                                                 continue;
746                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
747                                 }
748                         } else {
749                                 foreach (char c in invalid) {
750                                         int i = (int) c;
751                                         if (i == 0)
752                                                 continue;
753                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
754                                 }
755                         }
756                 }
757
758                 [Test]
759                 public void InvalidPathChars_Modify ()
760                 {
761                         char[] expected = Path.InvalidPathChars;
762                         char[] invalid = Path.InvalidPathChars;
763                         char original = invalid[0];
764                         try {
765                                 invalid[0] = 'a';
766                                 // kind of scary
767                                 Assert ("expected", expected[0] == 'a');
768                                 AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]);
769                         }
770                         finally {
771                                 invalid[0] = original;
772                         }
773                 }
774 #if NET_2_0
775                 [Test]
776                 public void GetInvalidFileNameChars_Values ()
777                 {
778                         char[] invalid = Path.GetInvalidFileNameChars ();
779                         if (Windows) {
780                                 AssertEquals (41, invalid.Length);
781                                 foreach (char c in invalid) {
782                                         int i = (int) c;
783                                         if (i < 32)
784                                                 continue;
785                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
786                                                 continue;
787                                         // ':', '*', '?', '\', '/'
788                                         if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
789                                                 continue;
790                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
791                                 }
792                         } else {
793                                 foreach (char c in invalid) {
794                                         int i = (int) c;
795                                         // null or '/'
796                                         if ((i == 0) || (i == 47))
797                                                 continue;
798                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
799                                 }
800                         }
801                 }
802
803                 [Test]
804                 public void GetInvalidFileNameChars_Modify ()
805                 {
806                         char[] expected = Path.GetInvalidFileNameChars ();
807                         char[] invalid = Path.GetInvalidFileNameChars ();
808                         invalid[0] = 'a';
809                         Assert ("expected", expected[0] != 'a');
810                         AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]);
811                 }
812
813                 [Test]
814                 public void GetInvalidPathChars_Values ()
815                 {
816                         char[] invalid = Path.GetInvalidPathChars ();
817                         if (Windows) {
818                                 AssertEquals (36, invalid.Length);
819                                 foreach (char c in invalid) {
820                                         int i = (int) c;
821                                         if (i < 32)
822                                                 continue;
823                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
824                                                 continue;
825                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
826                                 }
827                         } else {
828                                 foreach (char c in invalid) {
829                                         int i = (int) c;
830                                         if (i == 0)
831                                                 continue;
832                                         Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
833                                 }
834                         }
835                 }
836
837                 [Test]
838                 public void GetInvalidPathChars_Modify ()
839                 {
840                         char[] expected = Path.GetInvalidPathChars ();
841                         char[] invalid = Path.GetInvalidPathChars ();
842                         invalid[0] = 'a';
843                         Assert ("expected", expected[0] != 'a');
844                         AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]);
845                 }
846
847                 [Test]
848                 public void GetRandomFileName ()
849                 {
850                         string s = Path.GetRandomFileName ();
851                         AssertEquals ("Length", 12, s.Length);
852                         char[] invalid = Path.GetInvalidFileNameChars ();
853                         for (int i=0; i < s.Length; i++) {
854                                 if (i == 8)
855                                         AssertEquals ("8", '.', s[i]);
856                                 else
857                                         Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1);
858                         }
859                 }
860 #endif
861         }
862 }
863