[tests] Fix for when CWD is /
[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         enum OsType {
27                 Windows,
28                 Unix,
29                 Mac
30         }
31
32         [TestFixture]
33         public class PathTest
34         {
35                 static string path1;
36                 static string path2;
37                 static string path3;
38                 static OsType OS;
39                 static char DSC = Path.DirectorySeparatorChar;
40
41                 [SetUp]
42                 public void SetUp ()
43                 {
44                         if ('/' == DSC) {
45                                 OS = OsType.Unix;
46                                 path1 = "/foo/test.txt";
47                                 path2 = "/etc";
48                                 path3 = "init.d";
49                         } else if ('\\' == DSC) {
50                                 OS = OsType.Windows;
51                                 path1 = "c:\\foo\\test.txt";
52                                 path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
53                                 path3 = "system32";
54                         } else {
55                                 OS = OsType.Mac;
56                                 //FIXME: For Mac. figure this out when we need it
57                                 path1 = "foo:test.txt";
58                                 path2 = "foo";
59                                 path3 = "bar";
60                         }
61                 }
62
63                 bool Windows
64                 {
65                         get {
66                                 return OS == OsType.Windows;
67                         }
68                 }
69
70                 bool Unix
71                 {
72                         get {
73                                 return OS == OsType.Unix;
74                         }
75                 }
76
77                 bool Mac
78                 {
79                         get {
80                                 return OS == OsType.Mac;
81                         }
82                 }
83
84                 [Test]
85                 public void ChangeExtension ()
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                         Assert.AreEqual (files [(int) OS], testPath, "ChangeExtension #01");
94
95                         testPath = Path.ChangeExtension (String.Empty, ".extension");
96                         Assert.AreEqual (String.Empty, testPath, "ChangeExtension #02");
97
98                         testPath = Path.ChangeExtension (null, ".extension");
99                         Assert.AreEqual (null, testPath, "ChangeExtension #03");
100
101                         testPath = Path.ChangeExtension ("path", null);
102                         Assert.AreEqual ("path", testPath, "ChangeExtension #04");
103
104                         testPath = Path.ChangeExtension ("path.ext", "doc");
105                         Assert.AreEqual ("path.doc", testPath, "ChangeExtension #05");
106
107                         testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
108                         Assert.AreEqual ("path.ext1.doc", testPath, "ChangeExtension #06");
109
110                         testPath = Path.ChangeExtension ("hogehoge.xml", ".xsl");
111                         Assert.AreEqual ("hogehoge.xsl", testPath, "ChangeExtension #07");
112                         testPath = Path.ChangeExtension ("hogehoge", ".xsl");
113                         Assert.AreEqual ("hogehoge.xsl", testPath, "ChangeExtension #08");
114                         testPath = Path.ChangeExtension ("hogehoge.xml", "xsl");
115                         Assert.AreEqual ("hogehoge.xsl", testPath, "ChangeExtension #09");
116                         testPath = Path.ChangeExtension ("hogehoge", "xsl");
117                         Assert.AreEqual ("hogehoge.xsl", testPath, "ChangeExtension #10");
118                         testPath = Path.ChangeExtension ("hogehoge.xml", String.Empty);
119                         Assert.AreEqual ("hogehoge.", testPath, "ChangeExtension #11");
120                         testPath = Path.ChangeExtension ("hogehoge", String.Empty);
121                         Assert.AreEqual ("hogehoge.", testPath, "ChangeExtension #12");
122                         testPath = Path.ChangeExtension ("hogehoge.", null);
123                         Assert.AreEqual ("hogehoge", testPath, "ChangeExtension #13");
124                         testPath = Path.ChangeExtension ("hogehoge", null);
125                         Assert.AreEqual ("hogehoge", testPath, "ChangeExtension #14");
126                         testPath = Path.ChangeExtension (String.Empty, null);
127                         Assert.AreEqual (String.Empty, testPath, "ChangeExtension #15");
128                         testPath = Path.ChangeExtension (String.Empty, "bashrc");
129                         Assert.AreEqual (String.Empty, testPath, "ChangeExtension #16");
130                         testPath = Path.ChangeExtension (String.Empty, ".bashrc");
131                         Assert.AreEqual (String.Empty, testPath, "ChangeExtension #17");
132                         testPath = Path.ChangeExtension (null, null);
133                         Assert.IsNull (testPath, "ChangeExtension #18");
134                 }
135
136                 [Test]
137                 public void ChangeExtension_Extension_InvalidPathChars () 
138                 {
139                         string fn = Path.ChangeExtension ("file.ext", "<");
140                         Assert.AreEqual ("file.<", fn, "Invalid filename");
141                 }
142
143                 [Test]
144                 public void ChangeExtension_Path_InvalidPathChars ()
145                 {
146                         try {
147                                 Path.ChangeExtension ("fi\0le.ext", ".extension");
148                                 Assert.Fail ("#1");
149                         } catch (ArgumentException ex) {
150                                 // Illegal characters in path
151                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
152                                 Assert.IsNull (ex.InnerException, "#3");
153                                 Assert.IsNotNull (ex.Message, "#4");
154                                 Assert.IsNull (ex.ParamName, "#5");
155                         }
156                 }
157
158                 [Test]
159                 public void Combine ()
160                 {
161                         string [] files = new string [3];
162                         files [(int) OsType.Unix] = "/etc/init.d";
163                         files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
164                         files [(int) OsType.Mac] = "foo:bar";
165
166                         string testPath = Path.Combine (path2, path3);
167                         Assert.AreEqual (files [(int) OS], testPath, "Combine #01");
168
169                         testPath = Path.Combine ("one", String.Empty);
170                         Assert.AreEqual ("one", testPath, "Combine #02");
171
172                         testPath = Path.Combine (String.Empty, "one");
173                         Assert.AreEqual ("one", testPath, "Combine #03");
174
175                         string current = Directory.GetCurrentDirectory ();
176                         bool currentIsDSC = current.Length == 1 && current [0] == DSC;
177                         testPath = Path.Combine (current, "one");
178
179                         string expected = (currentIsDSC ? String.Empty : current) + DSC + "one";
180                         Assert.AreEqual (expected, testPath, "Combine #04");
181
182                         testPath = Path.Combine ("one", current);
183                         // LAMESPEC noted in Path.cs
184                         Assert.AreEqual (current, testPath, "Combine #05");
185
186                         testPath = Path.Combine (current, expected);
187                         Assert.AreEqual (expected, testPath, "Combine #06");
188
189                         testPath = DSC + "one";
190                         testPath = Path.Combine (testPath, "two" + DSC);
191                         expected = DSC + "one" + DSC + "two" + DSC;
192                         Assert.AreEqual (expected, testPath, "Combine #06");
193
194                         testPath = "one" + DSC;
195                         testPath = Path.Combine (testPath, DSC + "two");
196                         expected = DSC + "two";
197                         Assert.AreEqual (expected, testPath, "Combine #06");
198
199                         testPath = "one" + DSC;
200                         testPath = Path.Combine (testPath, "two" + DSC);
201                         expected = "one" + DSC + "two" + DSC;
202                         Assert.AreEqual (expected, testPath, "Combine #07");
203
204 #if NET_4_0
205                         Assert.AreEqual ("a", Path.Combine (new [] { "a", "" }), "Combine #08");
206 #endif
207                 }
208
209                 [Test]
210                 public void Combine_Path1_InvalidPathChars ()
211                 {
212                         try {
213                                 Path.Combine ("a\0", "one");
214                                 Assert.Fail ("#1");
215                         } catch (ArgumentException ex) {
216                                 // Illegal characters in path
217                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
218                                 Assert.IsNull (ex.InnerException, "#3");
219                                 Assert.IsNotNull (ex.Message, "#4");
220                                 Assert.IsNull (ex.ParamName, "#5");
221                         }
222                 }
223
224                 [Test]
225                 public void Combine_Path1_Null ()
226                 {
227                         try {
228                                 Path.Combine (null, "one");
229                                 Assert.Fail ("#1");
230                         } catch (ArgumentNullException ex) {
231                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
232                                 Assert.IsNull (ex.InnerException, "#3");
233                                 Assert.IsNotNull (ex.Message, "#4");
234                                 Assert.AreEqual ("path1", ex.ParamName, "#5");
235                         }
236                 }
237
238                 [Test]
239                 public void Combine_Path2_InvalidPathChars ()
240                 {
241                         try {
242                                 Path.Combine ("one", "a\0");
243                                 Assert.Fail ("#1");
244                         } catch (ArgumentException ex) {
245                                 // Illegal characters in path
246                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
247                                 Assert.IsNull (ex.InnerException, "#3");
248                                 Assert.IsNotNull (ex.Message, "#4");
249                                 Assert.IsNull (ex.ParamName, "#5");
250                         }
251                 }
252
253                 [Test]
254                 public void Combine_Path2_Null ()
255                 {
256                         try {
257                                 Path.Combine ("one", null);
258                                 Assert.Fail ("#1");
259                         } catch (ArgumentNullException ex) {
260                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
261                                 Assert.IsNull (ex.InnerException, "#3");
262                                 Assert.IsNotNull (ex.Message, "#4");
263                                 Assert.AreEqual ("path2", ex.ParamName, "#5");
264                         }
265                 }
266
267                 [Test]
268                 public void GetDirectoryName ()
269                 {
270                         string [] files = new string [3];
271                         files [(int) OsType.Unix] = "/foo";
272                         files [(int) OsType.Windows] = "c:\\foo";
273                         files [(int) OsType.Mac] = "foo";
274
275                         string testDirName = Path.GetDirectoryName (path1);
276                         Assert.AreEqual (files [(int) OS], testDirName, "#A1");
277                         testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
278                         Assert.AreEqual (files [(int) OS], testDirName, "#A2");
279
280                         if (Windows) {
281                                 Assert.AreEqual ("C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"), "#B1");
282                                 Assert.AreEqual (null, Path.GetDirectoryName ("C:"), "#B2");
283                                 Assert.AreEqual (null, Path.GetDirectoryName (@"C:\"), "#B3");
284                                 Assert.AreEqual (@"C:\", Path.GetDirectoryName (@"C:\dir"), "#B4");
285                                 Assert.AreEqual (@"C:\dir", Path.GetDirectoryName (@"C:\dir\"), "#B5");
286                                 Assert.AreEqual (@"C:\dir", Path.GetDirectoryName (@"C:\dir\dir"), "#B6");
287                                 Assert.AreEqual (@"C:\dir\dir", Path.GetDirectoryName (@"C:\dir\dir\"), "#B7");
288                                 Assert.AreEqual (@"C:", Path.GetDirectoryName (@"C:foo.txt"), "#B8");
289                                 Assert.AreEqual (@"C:dir", Path.GetDirectoryName (@"C:dir\"), "#B9");
290
291                                 Assert.AreEqual ("\\foo\\bar", Path.GetDirectoryName ("/foo//bar/dingus"), "#C1");
292                                 Assert.AreEqual ("foo\\bar", Path.GetDirectoryName ("foo/bar/"), "#C2");
293                                 Assert.AreEqual ("foo\\bar", Path.GetDirectoryName ("foo/bar\\xxx"), "#C3");
294                                 Assert.AreEqual ("\\\\host\\dir\\dir2", Path.GetDirectoryName ("\\\\host\\dir\\\\dir2\\path"), "#C4");
295
296                                 // UNC tests
297                                 Assert.AreEqual (null, Path.GetDirectoryName (@"\\"), "#D1");
298                                 Assert.AreEqual (null, Path.GetDirectoryName (@"\\server"), "#D2");
299                                 Assert.AreEqual (null, Path.GetDirectoryName (@"\\server\share"), "#D3");
300                                 Assert.AreEqual (@"\\server\share", Path.GetDirectoryName (@"\\server\share\"), "#D4");
301                                 Assert.AreEqual (@"\\server\share", Path.GetDirectoryName (@"\\server\share\dir"), "#D5");
302                                 Assert.AreEqual (@"\\server\share\dir", Path.GetDirectoryName (@"\\server\share\dir\subdir"), "#D6");
303                         } else {
304                                 Assert.AreEqual ("/etc", Path.GetDirectoryName ("/etc/hostname"), "#B1");
305                                 Assert.AreEqual ("/foo/bar", Path.GetDirectoryName ("/foo//bar/dingus"), "#B2");
306                                 Assert.AreEqual ("foo/bar", Path.GetDirectoryName ("foo/bar/"), "#B3");
307                                 Assert.AreEqual ("/", Path.GetDirectoryName ("/tmp"), "#B4");
308                                 Assert.IsNull (Path.GetDirectoryName ("/"), "#B5");
309                                 Assert.AreEqual ("a", Path.GetDirectoryName ("a//b"), "#B6");
310                         }
311                 }
312
313                 [Test]
314                 public void GetDirectoryName_Path_Empty ()
315                 {
316                         try {
317                                 Path.GetDirectoryName (String.Empty);
318                                 Assert.Fail ("#1");
319                         } catch (ArgumentException ex) {
320                                 // The path is not of a legal form
321                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
322                                 Assert.IsNull (ex.InnerException, "#3");
323                                 Assert.IsNotNull (ex.Message, "#4");
324                                 Assert.IsNull (ex.ParamName, "#5");
325                         }
326                 }
327
328                 [Test]
329                 public void GetDirectoryName_Path_InvalidPathChars ()
330                 {
331                         try {
332                                 Path.GetDirectoryName ("hi\0world");
333                                 Assert.Fail ("#1");
334                         } catch (ArgumentException ex) {
335                                 // Illegal characters in path
336                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
337                                 Assert.IsNull (ex.InnerException, "#3");
338                                 Assert.IsNotNull (ex.Message, "#4");
339                                 Assert.IsNull (ex.ParamName, "#5");
340                         }
341                 }
342
343                 [Test]
344                 public void GetDirectoryName_Path_Null ()
345                 {
346                         Assert.IsNull (Path.GetDirectoryName (null));
347                 }
348
349                 [Test]
350                 public void GetDirectoryName_Path_Whitespace ()
351                 {
352                         try {
353                                 Path.GetDirectoryName ("   ");
354                                 Assert.Fail ("#1");
355                         } catch (ArgumentException ex) {
356                                 // The path is not of a legal form
357                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
358                                 Assert.IsNull (ex.InnerException, "#3");
359                                 Assert.IsNotNull (ex.Message, "#4");
360                                 Assert.IsNull (ex.ParamName, "#5");
361                         }
362                 }
363
364                 [Test]
365                 public void GetExtension ()
366                 {
367                         string testExtn = Path.GetExtension (path1);
368
369                         Assert.AreEqual (".txt", testExtn, "GetExtension #01");
370
371                         testExtn = Path.GetExtension (path2);
372                         Assert.AreEqual (String.Empty, testExtn, "GetExtension #02");
373
374                         testExtn = Path.GetExtension (String.Empty);
375                         Assert.AreEqual (String.Empty, testExtn, "GetExtension #03");
376
377                         testExtn = Path.GetExtension (null);
378                         Assert.AreEqual (null, testExtn, "GetExtension #04");
379
380                         testExtn = Path.GetExtension (" ");
381                         Assert.AreEqual (String.Empty, testExtn, "GetExtension #05");
382
383                         testExtn = Path.GetExtension (path1 + ".doc");
384                         Assert.AreEqual (".doc", testExtn, "GetExtension #06");
385
386                         testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
387                         Assert.AreEqual (".txt", testExtn, "GetExtension #07");
388
389                         testExtn = Path.GetExtension (".");
390                         Assert.AreEqual (String.Empty, testExtn, "GetExtension #08");
391
392                         testExtn = Path.GetExtension ("end.");
393                         Assert.AreEqual (String.Empty, testExtn, "GetExtension #09");
394
395                         testExtn = Path.GetExtension (".start");
396                         Assert.AreEqual (".start", testExtn, "GetExtension #10");
397
398                         testExtn = Path.GetExtension (".a");
399                         Assert.AreEqual (".a", testExtn, "GetExtension #11");
400
401                         testExtn = Path.GetExtension ("a.");
402                         Assert.AreEqual (String.Empty, testExtn, "GetExtension #12");
403
404                         testExtn = Path.GetExtension ("a");
405                         Assert.AreEqual (String.Empty, testExtn, "GetExtension #13");
406
407                         testExtn = Path.GetExtension ("makefile");
408                         Assert.AreEqual (String.Empty, testExtn, "GetExtension #14");
409                 }
410
411                 [Test]
412                 public void GetExtension_Path_InvalidPathChars ()
413                 {
414                         try {
415                                 Path.GetExtension ("hi\0world.txt");
416                                 Assert.Fail ("#1");
417                         } catch (ArgumentException ex) {
418                                 // Illegal characters in path.
419                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
420                                 Assert.IsNull (ex.InnerException, "#3");
421                                 Assert.IsNotNull (ex.Message, "#4");
422                                 Assert.IsNull (ex.ParamName, "#5");
423                         }
424                 }
425
426                 [Test]
427                 public void GetFileName ()
428                 {
429                         string testFileName = Path.GetFileName (path1);
430
431                         Assert.AreEqual ("test.txt", testFileName, "#1");
432                         testFileName = Path.GetFileName (null);
433                         Assert.AreEqual (null, testFileName, "#2");
434                         testFileName = Path.GetFileName (String.Empty);
435                         Assert.AreEqual (String.Empty, testFileName, "#3");
436                         testFileName = Path.GetFileName (" ");
437                         Assert.AreEqual (" ", testFileName, "#4");
438                 }
439
440                 [Test]
441                 public void GetFileName_Path_InvalidPathChars ()
442                 {
443                         try {
444                                 Path.GetFileName ("hi\0world");
445                                 Assert.Fail ("#1");
446                         } catch (ArgumentException ex) {
447                                 // Illegal characters in path
448                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
449                                 Assert.IsNull (ex.InnerException, "#3");
450                                 Assert.IsNotNull (ex.Message, "#4");
451                                 Assert.IsNull (ex.ParamName, "#5");
452                         }
453                 }
454
455                 [Test]
456                 public void GetFileNameWithoutExtension ()
457                 {
458                         string testFileName = Path.GetFileNameWithoutExtension (path1);
459
460                         Assert.AreEqual ("test", testFileName, "GetFileNameWithoutExtension #01");
461
462                         testFileName = Path.GetFileNameWithoutExtension (null);
463                         Assert.AreEqual (null, testFileName, "GetFileNameWithoutExtension #02");
464
465                         testFileName = Path.GetFileNameWithoutExtension (String.Empty);
466                         Assert.AreEqual (String.Empty, testFileName, "GetFileNameWithoutExtension #03");
467                 }
468
469                 [Test]
470                 public void GetFileNameWithoutExtension_Path_InvalidPathChars ()
471                 {
472                         try {
473                                 Path.GetFileNameWithoutExtension ("hi\0world");
474                                 Assert.Fail ("#1");
475                         } catch (ArgumentException ex) {
476                                 // Illegal characters in path
477                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
478                                 Assert.IsNull (ex.InnerException, "#3");
479                                 Assert.IsNotNull (ex.Message, "#4");
480                                 Assert.IsNull (ex.ParamName, "#5");
481                         }
482                 }
483
484                 [Test]
485                 public void GetFullPath ()
486                 {
487                         string current = Directory.GetCurrentDirectory ();
488                         bool currentIsDSC = current.Length == 1 && current [0] == DSC;
489                         string testFullPath = Path.GetFullPath ("foo.txt");
490                         string expected = (currentIsDSC ? String.Empty : current) + DSC + "foo.txt";
491                         Assert.AreEqual (expected, testFullPath, "GetFullPath #01");
492
493                         testFullPath = Path.GetFullPath ("a//./.././foo.txt");
494                         Assert.AreEqual (expected, testFullPath, "GetFullPath #02");
495
496                         if (!Windows){
497                                 Assert.AreEqual ("/bin/bash", Path.GetFullPath ("/../bin/bash"));
498                         }
499                                 
500                 }
501
502                 [Test]
503                 public void GetFullPath_Unix ()
504                 {
505                         if (Windows)
506                                 Assert.Ignore ("Running on Windows.");
507
508                         string root =  "/";
509                         string [,] test = new string [,] {
510                                 {"root////././././././../root/././../root", "root"},
511                                 {"root/", "root/"},
512                                 {"root/./", "root/"},
513                                 {"root/./", "root/"},
514                                 {"root/../", String.Empty},
515                                 {"root/../", String.Empty},
516                                 {"root/../..", String.Empty},
517                                 {"root/.hiddenfile", "root/.hiddenfile"},
518                                 {"root/. /", "root/. /"},
519                                 {"root/.. /", "root/.. /"},
520                                 {"root/..weirdname", "root/..weirdname"},
521                                 {"root/..", String.Empty},
522                                 {"root/../a/b/../../..", String.Empty},
523                                 {"root/./..", String.Empty},
524                                 {"..", String.Empty},
525                                 {".", String.Empty},
526                                 {"root//dir", "root/dir"},
527                                 {"root/.              /", "root/.              /"},
528                                 {"root/..             /", "root/..             /"},
529                                 {"root/      .              /", "root/      .              /"},
530                                 {"root/      ..             /", "root/      ..             /"},
531                                 {"root/./", "root/"},
532                                 //ERROR! Paths are trimmed
533                                 // I don't understand this comment^^.
534                                 // No trimming occurs but the paths are not equal. That's why the test fails. Commented out.
535                                 //{"root/..                      /", "root/..                   /"},
536                                 {".//", String.Empty}
537                         };
538
539                         for (int i = 0; i < test.GetUpperBound (0); i++) {
540                                 Assert.AreEqual (root + test [i, 1], Path.GetFullPath (root + test [i, 0]),
541                                                  String.Format ("GetFullPathUnix #{0}", i));
542                         }
543
544                         Assert.AreEqual ("/", Path.GetFullPath ("/"), "#01");
545                         Assert.AreEqual ("/hey", Path.GetFullPath ("/hey"), "#02");
546                         Assert.AreEqual (Environment.CurrentDirectory, Path.GetFullPath ("."), "#03");
547                         Assert.AreEqual (Path.Combine (Environment.CurrentDirectory, "hey"),
548                                              Path.GetFullPath ("hey"), "#04");
549                         Assert.AreEqual ("/", Path.GetFullPath ("/"), "#01");
550
551                         string curdir = Directory.GetCurrentDirectory ();
552                         try {
553                                 Directory.SetCurrentDirectory ("/");
554                                 Assert.AreEqual ("/test.txt", Path.GetFullPath ("test.txt"), "xambug #833");
555                         }
556                         finally {
557                                 Directory.SetCurrentDirectory (curdir);
558                         }
559                 }
560
561                 [Test]
562                 public void GetFullPath_Windows ()
563                 {
564                         if (!Windows)
565                                 Assert.Ignore ("Not running on Windows.");
566
567                         string root =  "C:\\";
568                         string [,] test = new string [,] {
569                                 {"root////././././././../root/././../root", "root"},
570                                 {"root/", "root\\"},
571                                 {"root/./", "root\\"},
572                                 {"root/./", "root\\"},
573                                 {"root/../", ""},
574                                 {"root/../", ""},
575                                 {"root/../..", ""},
576                                 {"root/.hiddenfile", "root\\.hiddenfile"},
577                                 {"root/. /", "root\\"},
578                                 {"root/.. /", ""},
579                                 {"root/..weirdname", "root\\..weirdname"},
580                                 {"root/..", ""},
581                                 {"root/../a/b/../../..", ""},
582                                 {"root/./..", ""},
583                                 {"..", ""},
584                                 {".", ""},
585                                 {"root//dir", "root\\dir"},
586                                 {"root/.              /", "root\\"},
587                                 {"root/..             /", ""},
588                                 {"root/./", "root\\"},
589                                 {"root/..                      /", ""},
590                                 {".//", ""}
591                         };
592
593                         for (int i = 0; i < test.GetUpperBound (0); i++) {
594                                 try {
595                                         Assert.AreEqual (root + test [i, 1], Path.GetFullPath (root + test [i, 0]),
596                                                          String.Format ("GetFullPathWindows #{0}", i));
597                                 } catch (Exception ex) { 
598                                         Assert.Fail (String.Format ("GetFullPathWindows #{0} (\"{1}\") failed: {2}", 
599                                                 i, root + test [i, 0], ex.GetType ()));
600                                 }
601                         }
602
603                         // UNC tests
604                         string root2 = @"\\server\share";
605                         root = @"\\server\share\";
606                         test = new string [,] {         
607                                 {"root////././././././../root/././../root", "root"},
608                                 {"root/", "root\\"},
609                                 {"root/./", "root\\"},
610                                 {"root/./", "root\\"},
611                                 {"root/../", ""},
612                                 {"root/../", ""},
613                                 {"root/../..", null},
614                                 {"root/.hiddenfile", "root\\.hiddenfile"},
615                                 {"root/. /", "root\\"},
616                                 {"root/.. /", ""},
617                                 {"root/..weirdname", "root\\..weirdname"},
618                                 {"root/..", null},
619                                 {"root/../a/b/../../..", null},
620                                 {"root/./..", null},
621                                 {"..", null},
622                                 {".", null},
623                                 {"root//dir", "root\\dir"},
624                                 {"root/.              /", "root\\"},
625                                 {"root/..             /", ""},
626                                 {"root/./", "root\\"},
627                                 {"root/..                      /", ""},
628                                 {".//", ""}
629                         };
630
631                         for (int i = 0; i < test.GetUpperBound (0); i++) {
632                                 // "null" means we have to compare against "root2"
633                                 string res = test [i, 1] != null
634                                         ? root + test [i, 1]
635                                         : root2;
636                                 try {
637                                         Assert.AreEqual (res, Path.GetFullPath (root + test [i, 0]),
638                                                          String.Format ("GetFullPathWindows UNC #{0}", i));
639                                 } catch (AssertionException) {
640                                         throw;
641                                 } catch (Exception ex) {
642                                         Assert.Fail (String.Format ("GetFullPathWindows UNC #{0} (\"{1}\") failed: {2}",
643                                                 i, root + test [i, 0], ex.GetType ()));
644                                 }
645                         }
646
647                         test = new string [,] {         
648                                 {"root////././././././../root/././../root", "root"},
649                                 {"root/", "root\\"},
650                                 {"root/./", "root\\"},
651                                 {"root/./", "root\\"},
652                                 {"root/../", ""},
653                                 {"root/../", ""},
654                                 {"root/../..", null},
655                                 {"root/.hiddenfile", "root\\.hiddenfile"},
656                                 {"root/. /", "root\\"},
657                                 {"root/.. /", ""},
658                                 {"root/..weirdname", "root\\..weirdname"},
659                                 {"root/..", null},
660                                 {"root/../a/b/../../..", null},
661                                 {"root/./..", null},
662                                 {"..", null},
663                                 {".", null},
664                                 {"root//dir", "root\\dir"},
665                                 {"root/.              /", "root\\"},
666                                 {"root/..             /", ""},
667                                 {"root/./", "root\\"},
668                                 {"root/..                      /", ""},
669                                 {".//", ""}
670                         };
671
672                         string root3 = @"//server/share";
673                         root = @"//server/share/";
674                         bool needSlashConvert = Path.DirectorySeparatorChar != '/';
675
676                         for (int i = 0; i < test.GetUpperBound (0); i++) {
677                                 // "null" means we have to compare against "root2"
678                                 string res = test [i, 1] != null
679                                         ? root + test [i, 1]
680                                         : root3;
681                                 if (needSlashConvert)
682                                         res = res.Replace ('/', Path.DirectorySeparatorChar);
683                                 try {
684                                         Assert.AreEqual (res, Path.GetFullPath (root + test [i, 0]),
685                                                          String.Format ("GetFullPathWindows UNC[2] #{0}", i));
686                                 } catch (AssertionException) {
687                                         throw;
688                                 } catch (Exception ex) {
689                                         Assert.Fail (String.Format ("GetFullPathWindows UNC[2] #{0} (\"{1}\") failed: {2}",
690                                                 i, root + test [i, 0], ex.GetType ()));
691                                 }
692                         }
693                 }
694
695                 [Test]
696                 public void GetFullPath_Path_Empty ()
697                 {
698                         try {
699                                 Path.GetFullPath (String.Empty);
700                                 Assert.Fail ("#1");
701                         } catch (ArgumentException ex) {
702                                 // The path is not of a legal form
703                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
704                                 Assert.IsNull (ex.InnerException, "#3");
705                                 Assert.IsNotNull (ex.Message, "#4");
706                                 Assert.IsNull (ex.ParamName, "#5");
707                         }
708                 }
709
710                 [Test]
711                 public void GetFullPath_Path_EndingSeparator ()
712                 {
713                         string fp = Path.GetFullPath ("something/");
714                         char end = fp [fp.Length - 1];
715                         Assert.IsTrue (end == Path.DirectorySeparatorChar);
716                 }
717
718                 [Test]
719                 public void GetFullPath_Path_InvalidPathChars ()
720                 {
721                         try {
722                                 Path.GetFullPath ("hi\0world");
723                                 Assert.Fail ("#1");
724                         } catch (ArgumentException ex) {
725                                 // Illegal characters in path
726                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
727                                 Assert.IsNull (ex.InnerException, "#3");
728                                 Assert.IsNotNull (ex.Message, "#4");
729                                 Assert.IsNull (ex.ParamName, "#5");
730                         }
731                 }
732
733                 [Test]
734                 public void GetFullPath_Path_Null ()
735                 {
736                         try {
737                                 Path.GetFullPath (null);
738                                 Assert.Fail ("#1");
739                         } catch (ArgumentNullException ex) {
740                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
741                                 Assert.IsNull (ex.InnerException, "#3");
742                                 Assert.IsNotNull (ex.Message, "#4");
743                                 Assert.AreEqual ("path", ex.ParamName, "#5");
744                         }
745                 }
746
747                 [Test]
748                 public void GetFullPath_Path_Whitespace ()
749                 {
750                         try {
751                                 Path.GetFullPath ("  ");
752                                 Assert.Fail ("#1");
753                         } catch (ArgumentException ex) {
754                                 // The path is not of a legal form
755                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
756                                 Assert.IsNull (ex.InnerException, "#3");
757                                 Assert.IsNotNull (ex.Message, "#4");
758                                 Assert.IsNull (ex.ParamName, "#5");
759                         }
760                 }
761
762                 [Test]
763                 public void GetFullPath2 ()
764                 {
765                         if (Windows) {
766                                 Assert.AreEqual (@"Z:\", Path.GetFullPath ("Z:"), "GetFullPath w#01");
767                                 Assert.AreEqual (@"c:\abc\def", Path.GetFullPath (@"c:\abc\def"), "GetFullPath w#02");
768                                 Assert.IsTrue (Path.GetFullPath (@"\").EndsWith (@"\"), "GetFullPath w#03");
769                                 // "\\\\" is not allowed
770                                 Assert.IsTrue (Path.GetFullPath ("/").EndsWith (@"\"), "GetFullPath w#05");
771                                 // "//" is not allowed
772                                 Assert.IsTrue (Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"), "GetFullPath w#07");
773                                 Assert.IsTrue (Path.GetFullPath ("c").EndsWith (@"\c"), "GetFullPath w#08");
774                                 Assert.IsTrue (Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"), "GetFullPath w#09");
775                                 Assert.IsTrue (Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"), "GetFullPath w#10");
776                                 Assert.AreEqual (@"\\abc\def", Path.GetFullPath (@"\\abc\def"), "GetFullPath w#11");
777                                 Assert.AreEqual (Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"), "GetFullPath w#12");
778                                 Assert.AreEqual (Directory.GetCurrentDirectory ().Substring (0, 2) + @"\abc\def", Path.GetFullPath ("/abc/def"), "GetFullPath w#13");
779                                 Assert.AreEqual (@"\\abc\def", Path.GetFullPath ("//abc/def"), "GetFullPath w#14");
780                         } else {
781                                 Assert.AreEqual ("/", Path.GetFullPath ("/"), "#01");
782                                 Assert.AreEqual ("/hey", Path.GetFullPath ("/hey"), "#02");
783                                 Assert.AreEqual (Environment.CurrentDirectory, Path.GetFullPath ("."), "#03");
784                                 Assert.AreEqual (Path.Combine (Environment.CurrentDirectory, "hey"),
785                                                      Path.GetFullPath ("hey"), "#04");
786                         }
787                 }
788
789                 [Test]
790                 public void GetPathRoot ()
791                 {
792                         string current;
793                         string expected;
794                         if (!Windows){
795                                 current = Directory.GetCurrentDirectory ();
796                                 expected = current [0].ToString ();
797                         } else {
798                                 current = @"J:\Some\Strange Directory\Name";
799                                 expected = "J:\\";
800                         }
801
802                         string pathRoot = Path.GetPathRoot (current);
803                         Assert.AreEqual (expected, pathRoot, "GetPathRoot #01");
804                 }
805
806                 [Test]
807                 public void GetPathRoot2 ()
808                 {
809                         // note: this method doesn't call Directory.GetCurrentDirectory so it can be
810                         // reused for partial trust unit tests in PathCas.cs
811
812                         string pathRoot;
813                         
814                         pathRoot = Path.GetPathRoot ("hola");
815                         Assert.AreEqual (String.Empty, pathRoot, "#A1");
816                         pathRoot = Path.GetPathRoot (null);
817                         Assert.AreEqual (null, pathRoot, "#A2");
818
819                         if (Windows) {
820                                 Assert.AreEqual ("z:", Path.GetPathRoot ("z:"), "GetPathRoot w#01");
821                                 Assert.AreEqual ("c:\\", Path.GetPathRoot ("c:\\abc\\def"), "GetPathRoot w#02");
822                                 Assert.AreEqual ("\\", Path.GetPathRoot ("\\"), "GetPathRoot w#03");
823                                 Assert.AreEqual ("\\\\", Path.GetPathRoot ("\\\\"), "GetPathRoot w#04");
824                                 Assert.AreEqual ("\\", Path.GetPathRoot ("/"), "GetPathRoot w#05");
825                                 Assert.AreEqual ("\\\\", Path.GetPathRoot ("//"), "GetPathRoot w#06");
826                                 Assert.AreEqual (String.Empty, Path.GetPathRoot ("readme.txt"), "GetPathRoot w#07");
827                                 Assert.AreEqual (String.Empty, Path.GetPathRoot ("c"), "GetPathRoot w#08");
828                                 Assert.AreEqual (String.Empty, Path.GetPathRoot ("abc\\def"), "GetPathRoot w#09");
829                                 Assert.AreEqual ("\\", Path.GetPathRoot ("\\abc\\def"), "GetPathRoot w#10");
830                                 Assert.AreEqual ("\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"), "GetPathRoot w#11");
831                                 Assert.AreEqual (String.Empty, Path.GetPathRoot ("abc//def"), "GetPathRoot w#12");
832                                 Assert.AreEqual ("\\", Path.GetPathRoot ("/abc/def"), "GetPathRoot w#13");
833                                 Assert.AreEqual ("\\\\abc\\def", Path.GetPathRoot ("//abc/def"), "GetPathRoot w#14");
834                                 Assert.AreEqual (@"C:\", Path.GetPathRoot (@"C:\"), "GetPathRoot w#15");
835                                 Assert.AreEqual (@"C:\", Path.GetPathRoot (@"C:\\"), "GetPathRoot w#16");
836                                 Assert.AreEqual ("\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def\\ghi"), "GetPathRoot w#17");
837                         } else {
838                                 // TODO: Same tests for Unix.
839                         }
840                 }
841
842                 [Test]
843                 public void GetPathRoot_Path_Empty ()
844                 {
845                         try {
846                                 Path.GetPathRoot (String.Empty);
847                                 Assert.Fail ("#1");
848                         } catch (ArgumentException ex) {
849                                 // The path is not of a legal form
850                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
851                                 Assert.IsNull (ex.InnerException, "#3");
852                                 Assert.IsNotNull (ex.Message, "#4");
853                                 Assert.IsNull (ex.ParamName, "#5");
854                         }
855                 }
856
857                 [Test]
858                 public void GetPathRoot_Path_InvalidPathChars ()
859                 {
860                         try {
861                                 Path.GetPathRoot ("hi\0world");
862                                 Assert.Fail ("#1");
863                         } catch (ArgumentException ex) {
864                                 // Illegal characters in path
865                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
866                                 Assert.IsNull (ex.InnerException, "#3");
867                                 Assert.IsNotNull (ex.Message, "#4");
868                                 Assert.IsNull (ex.ParamName, "#5");
869                         }
870                 }
871
872                 [Test]
873                 public void GetPathRoot_Path_Whitespace ()
874                 {
875                         try {
876                                 Path.GetPathRoot ("  ");
877                                 Assert.Fail ("#1");
878                         } catch (ArgumentException ex) {
879                                 // The path is not of a legal form
880                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
881                                 Assert.IsNull (ex.InnerException, "#3");
882                                 Assert.IsNotNull (ex.Message, "#4");
883                                 Assert.IsNull (ex.ParamName, "#5");
884                         }
885                 }
886
887                 [Test]
888                 public void GetTempPath ()
889                 {
890                         string getTempPath = Path.GetTempPath ();
891                         Assert.IsTrue (getTempPath != String.Empty, "GetTempPath #01");
892                         Assert.IsTrue (Path.IsPathRooted (getTempPath), "GetTempPath #02");
893                         Assert.AreEqual (Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1], "GetTempPath #03");
894                 }
895
896                 [Test]
897                 public void GetTempFileName ()
898                 {
899                         string getTempFileName = null;
900                         try {
901                                 getTempFileName = Path.GetTempFileName ();
902                                 Assert.IsTrue (getTempFileName != String.Empty, "GetTempFileName #01");
903                                 Assert.IsTrue (File.Exists (getTempFileName), "GetTempFileName #02");
904                         } finally {
905                                 if (getTempFileName != null && getTempFileName != String.Empty){
906                                         File.Delete (getTempFileName);
907                                 }
908                         }
909                 }
910
911                 [Test]
912                 public void HasExtension ()
913                 {
914                         Assert.AreEqual (true, Path.HasExtension ("foo.txt"), "HasExtension #01");
915                         Assert.AreEqual (false, Path.HasExtension ("foo"), "HasExtension #02");
916                         Assert.AreEqual (true, Path.HasExtension (path1), "HasExtension #03");
917                         Assert.AreEqual (false, Path.HasExtension (path2), "HasExtension #04");
918                         Assert.AreEqual (false, Path.HasExtension (null), "HasExtension #05");
919                         Assert.AreEqual (false, Path.HasExtension (String.Empty), "HasExtension #06");
920                         Assert.AreEqual (false, Path.HasExtension (" "), "HasExtension #07");
921                         Assert.AreEqual (false, Path.HasExtension ("."), "HasExtension #08");
922                         Assert.AreEqual (false, Path.HasExtension ("end."), "HasExtension #09");
923                         Assert.AreEqual (true, Path.HasExtension (".start"), "HasExtension #10");
924                         Assert.AreEqual (true, Path.HasExtension (".a"), "HasExtension #11");
925                         Assert.AreEqual (false, Path.HasExtension ("a."), "HasExtension #12");
926                         Assert.AreEqual (false, Path.HasExtension ("Makefile"), "HasExtension #13");
927                 }
928
929                 [Test]
930                 public void HasExtension_Path_InvalidPathChars ()
931                 {
932                         try {
933                                 Path.HasExtension ("hi\0world.txt");
934                                 Assert.Fail ("#1");
935                         } catch (ArgumentException ex) {
936                                 // Illegal characters in path
937                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
938                                 Assert.IsNull (ex.InnerException, "#3");
939                                 Assert.IsNotNull (ex.Message, "#4");
940                                 Assert.IsNull (ex.ParamName, "#5");
941                         }
942                 }
943
944                 [Test]
945                 public void IsPathRooted ()
946                 {
947                         Assert.IsTrue (Path.IsPathRooted (path2), "IsPathRooted #01");
948                         Assert.IsTrue (!Path.IsPathRooted (path3), "IsPathRooted #02");
949                         Assert.IsTrue (!Path.IsPathRooted (null), "IsPathRooted #03");
950                         Assert.IsTrue (!Path.IsPathRooted (String.Empty), "IsPathRooted #04");
951                         Assert.IsTrue (!Path.IsPathRooted (" "), "IsPathRooted #05");
952                         Assert.IsTrue (Path.IsPathRooted ("/"), "IsPathRooted #06");
953                         Assert.IsTrue (Path.IsPathRooted ("//"), "IsPathRooted #07");
954                         Assert.IsTrue (!Path.IsPathRooted (":"), "IsPathRooted #08");
955
956                         if (Windows) {
957                                 Assert.IsTrue (Path.IsPathRooted ("\\"), "IsPathRooted #09");
958                                 Assert.IsTrue (Path.IsPathRooted ("\\\\"), "IsPathRooted #10");
959                                 Assert.IsTrue (Path.IsPathRooted ("z:"), "IsPathRooted #11");
960                                 Assert.IsTrue (Path.IsPathRooted ("z:\\"), "IsPathRooted #12");
961                                 Assert.IsTrue (Path.IsPathRooted ("z:\\topdir"), "IsPathRooted #13");
962                                 // This looks MS BUG. It is treated as absolute path
963                                 Assert.IsTrue (Path.IsPathRooted ("z:curdir"), "IsPathRooted #14");
964                                 Assert.IsTrue (Path.IsPathRooted ("\\abc\\def"), "IsPathRooted #15");
965                         } else {
966                                 if (Environment.GetEnvironmentVariable ("MONO_IOMAP") == "all"){
967                                         Assert.IsTrue (Path.IsPathRooted ("\\"), "IsPathRooted #16");
968                                         Assert.IsTrue (Path.IsPathRooted ("\\\\"), "IsPathRooted #17");
969                                 } else {
970                                         Assert.IsTrue (!Path.IsPathRooted ("\\"), "IsPathRooted #09");
971                                         Assert.IsTrue (!Path.IsPathRooted ("\\\\"), "IsPathRooted #10");
972                                         Assert.IsTrue (!Path.IsPathRooted ("z:"), "IsPathRooted #11");
973                                 }
974                         }
975                 }
976
977                 [Test]
978                 public void IsPathRooted_Path_Empty ()
979                 {
980                         Assert.IsTrue (!Path.IsPathRooted (String.Empty));
981                 }
982
983                 [Test]
984                 public void IsPathRooted_Path_InvalidPathChars ()
985                 {
986                         try {
987                                 Path.IsPathRooted ("hi\0world");
988                                 Assert.Fail ("#1");
989                         } catch (ArgumentException ex) {
990                                 // Illegal characters in path.
991                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
992                                 Assert.IsNull (ex.InnerException, "#3");
993                                 Assert.IsNotNull (ex.Message, "#4");
994                                 Assert.IsNull (ex.ParamName, "#5");
995                         }
996                 }
997
998                 [Test]
999                 public void IsPathRooted_Path_Null ()
1000                 {
1001                         Assert.IsTrue (!Path.IsPathRooted (null));
1002                 }
1003
1004                 [Test]
1005                 public void IsPathRooted_Path_Whitespace ()
1006                 {
1007                         Assert.IsTrue (!Path.IsPathRooted ("  "));
1008                 }
1009
1010                 [Test]
1011                 public void CanonicalizeDots ()
1012                 {
1013                         string current = Path.GetFullPath (".");
1014                         Assert.IsTrue (!current.EndsWith ("."), "TestCanonicalizeDotst #01");
1015                         string parent = Path.GetFullPath ("..");
1016                         Assert.IsTrue (!current.EndsWith (".."), "TestCanonicalizeDotst #02");
1017                 }
1018 #if !MOBILE
1019                 [Test]
1020                 public void WindowsSystem32_76191 ()
1021                 {
1022                         // check for Unix platforms - see FAQ for more details
1023                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
1024                         int platform = (int) Environment.OSVersion.Platform;
1025                         if ((platform == 4) || (platform == 128) || (platform == 6))
1026                                 Assert.Ignore ("Running on Unix.");
1027
1028                         string curdir = Directory.GetCurrentDirectory ();
1029                         try {
1030                                 string system = Environment.SystemDirectory;
1031                                 Directory.SetCurrentDirectory (system);
1032                                 string drive = system.Substring (0, 2);
1033                                 Assert.AreEqual (system, Path.GetFullPath (drive), "current dir");
1034                         }
1035                         finally {
1036                                 Directory.SetCurrentDirectory (curdir);
1037                         }
1038                 }
1039
1040                 [Test]
1041                 public void WindowsSystem32_77007 ()
1042                 {
1043                         // check for Unix platforms - see FAQ for more details
1044                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
1045                         int platform = (int) Environment.OSVersion.Platform;
1046                         if ((platform == 4) || (platform == 128) || (platform == 6))
1047                                 Assert.Ignore ("Running on Unix.");
1048
1049                         string curdir = Directory.GetCurrentDirectory ();
1050                         try {
1051                                 string system = Environment.SystemDirectory;
1052                                 Directory.SetCurrentDirectory (system);
1053                                 // e.g. C:dir (no backslash) will return CurrentDirectory + dir
1054                                 string dir = system.Substring (0, 2) + "dir";
1055                                 Assert.AreEqual (Path.Combine (system, "dir"), Path.GetFullPath (dir), "current dir");
1056                         }
1057                         finally {
1058                                 Directory.SetCurrentDirectory (curdir);
1059                         }
1060                 }
1061 #endif
1062                 [Test]
1063                 public void WindowsDriveC14N_77058 ()
1064                 {
1065                         // check for Unix platforms - see FAQ for more details
1066                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
1067                         int platform = (int) Environment.OSVersion.Platform;
1068                         if ((platform == 4) || (platform == 128) || (platform == 6))
1069                                 Assert.Ignore ("Running on Unix.");
1070
1071                         Assert.AreEqual (@"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"), "1");
1072                         Assert.AreEqual (@"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"), "2");
1073                         Assert.AreEqual (@"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"), "3");
1074                         Assert.AreEqual (@"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"), "4");
1075                         Assert.AreEqual (@"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"), "5");
1076                 }
1077
1078                 [Test]
1079                 public void InvalidPathChars_Values ()
1080                 {
1081                         char[] invalid = Path.InvalidPathChars;
1082                         if (Windows) {
1083                                 Assert.AreEqual (36, invalid.Length, "Length");
1084
1085                                 foreach (char c in invalid) {
1086                                         int i = (int) c;
1087
1088                                         if (i < 32)
1089                                                 continue;
1090
1091                                         // in both 1.1 SP1 and 2.0
1092                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
1093                                                 continue;
1094                                         Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1095                                 }
1096                         } else {
1097                                 foreach (char c in invalid) {
1098                                         int i = (int) c;
1099                                         if (i == 0)
1100                                                 continue;
1101                                         Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1102                                 }
1103                         }
1104                 }
1105
1106                 [Test]
1107                 public void InvalidPathChars_Modify ()
1108                 {
1109                         char[] expected = Path.InvalidPathChars;
1110                         char[] invalid = Path.InvalidPathChars;
1111                         char original = invalid[0];
1112                         try {
1113                                 invalid[0] = 'a';
1114                                 // kind of scary
1115                                 Assert.IsTrue (expected[0] == 'a', "expected");
1116                                 Assert.AreEqual (expected[0], Path.InvalidPathChars[0], "readonly");
1117                         } finally {
1118                                 invalid[0] = original;
1119                         }
1120                 }
1121
1122                 [Test]
1123                 public void GetInvalidFileNameChars_Values ()
1124                 {
1125                         char[] invalid = Path.GetInvalidFileNameChars ();
1126                         if (Windows) {
1127                                 Assert.AreEqual (41, invalid.Length);
1128                                 foreach (char c in invalid) {
1129                                         int i = (int) c;
1130                                         if (i < 32)
1131                                                 continue;
1132                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
1133                                                 continue;
1134                                         // ':', '*', '?', '\', '/'
1135                                         if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
1136                                                 continue;
1137                                         Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1138                                 }
1139                         } else {
1140                                 foreach (char c in invalid) {
1141                                         int i = (int) c;
1142                                         // null or '/'
1143                                         if ((i == 0) || (i == 47))
1144                                                 continue;
1145                                         Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1146                                 }
1147                         }
1148                 }
1149
1150                 [Test]
1151                 public void GetInvalidFileNameChars_Modify ()
1152                 {
1153                         char[] expected = Path.GetInvalidFileNameChars ();
1154                         char[] invalid = Path.GetInvalidFileNameChars ();
1155                         invalid[0] = 'a';
1156                         Assert.IsTrue (expected[0] != 'a', "expected");
1157                         Assert.AreEqual (expected[0], Path.GetInvalidFileNameChars ()[0], "readonly");
1158                 }
1159
1160                 [Test]
1161                 public void GetInvalidPathChars_Values ()
1162                 {
1163                         char[] invalid = Path.GetInvalidPathChars ();
1164                         if (Windows) {
1165                                 Assert.AreEqual (36, invalid.Length);
1166                                 foreach (char c in invalid) {
1167                                         int i = (int) c;
1168                                         if (i < 32)
1169                                                 continue;
1170                                         if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
1171                                                 continue;
1172                                         Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1173                                 }
1174                         } else {
1175                                 foreach (char c in invalid) {
1176                                         int i = (int) c;
1177                                         if (i == 0)
1178                                                 continue;
1179                                         Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1180                                 }
1181                         }
1182                 }
1183
1184                 [Test]
1185                 public void GetInvalidPathChars_Order ()
1186                 {
1187                         if (Windows) {
1188                                 char [] invalid = Path.GetInvalidPathChars ();
1189                                 char [] expected = new char [36] { '\x22', '\x3C', '\x3E', '\x7C', '\x00', '\x01', '\x02',
1190                                         '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D',
1191                                         '\x0E', '\x0F', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18',
1192                                         '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' };
1193                                 Assert.AreEqual (expected.Length, invalid.Length);
1194                                 for (int i = 0; i < expected.Length; i++ ) {
1195                                         Assert.AreEqual (expected [i], invalid [i], "Character at position " + i);
1196                                 }
1197                         }
1198                 }
1199
1200                 [Test]
1201                 public void GetInvalidPathChars_Modify ()
1202                 {
1203                         char[] expected = Path.GetInvalidPathChars ();
1204                         char[] invalid = Path.GetInvalidPathChars ();
1205                         invalid[0] = 'a';
1206                         Assert.IsTrue (expected[0] != 'a', "expected");
1207                         Assert.AreEqual (expected[0], Path.GetInvalidPathChars ()[0], "readonly");
1208                 }
1209
1210                 [Test]
1211                 public void GetRandomFileName ()
1212                 {
1213                         string s = Path.GetRandomFileName ();
1214                         Assert.AreEqual (12, s.Length, "Length");
1215                         char[] invalid = Path.GetInvalidFileNameChars ();
1216                         for (int i=0; i < s.Length; i++) {
1217                                 if (i == 8)
1218                                         Assert.AreEqual ('.', s[i], "8");
1219                                 else
1220                                         Assert.IsTrue (Array.IndexOf (invalid, s[i]) == -1, i.ToString ());
1221                         }
1222                 }
1223
1224                 [Test]
1225                 public void GetRandomFileNameIsAlphaNumerical ()
1226                 {
1227                         string [] names = new string [1000];
1228                         for (int i = 0; i < names.Length; i++)
1229                                 names [i] = Path.GetRandomFileName ();
1230
1231                         foreach (string name in names) {
1232                                 Assert.AreEqual (12, name.Length);
1233                                 Assert.AreEqual ('.', name [8]);
1234
1235                                 for (int i = 0; i < 12; i++) {
1236                                         if (i == 8)
1237                                                 continue;
1238
1239                                         char c = name [i];
1240                                         Assert.IsTrue (('a' <= c && c <= 'z') || ('0' <= c && c <= '9'));
1241                                 }
1242                         }
1243                 }
1244
1245 #if NET_4_0
1246                 string Concat (string sep, params string [] parms)
1247                 {
1248                         return String.Join (sep, parms);
1249                 }
1250
1251                 [Test]
1252                 public void Combine_3Params ()
1253                 {
1254                         string sep = Path.DirectorySeparatorChar.ToString ();
1255
1256                         try {
1257                                 Path.Combine (null, "two", "three");
1258                                 Assert.Fail ("#A1-1");
1259                         } catch {
1260                                 // success
1261                         }
1262
1263                         try {
1264                                 Path.Combine ("one", null, "three");
1265                                 Assert.Fail ("#A1-2");
1266                         } catch {
1267                                 // success
1268                         }
1269
1270                         try {
1271                                 Path.Combine ("one", "two", null);
1272                                 Assert.Fail ("#A1-3");
1273                         } catch {
1274                                 // success
1275                         }
1276                         
1277                         Assert.AreEqual (Concat (sep, "one", "two", "three"), Path.Combine ("one", "two", "three"), "#A2-1");
1278                         Assert.AreEqual (Concat (sep, sep + "one", "two", "three"), Path.Combine (sep + "one", "two", "three"), "#A2-2");
1279                         Assert.AreEqual (Concat (sep, sep + "one", "two", "three"), Path.Combine (sep + "one" + sep, "two", "three"), "#A2-3");
1280                         Assert.AreEqual (Concat (sep, sep + "two", "three"), Path.Combine (sep + "one" + sep, sep + "two", "three"), "#A2-4");
1281                         Assert.AreEqual (Concat (sep, sep + "three"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three"), "#A2-5");
1282
1283                         Assert.AreEqual (Concat (sep, sep + "one" + sep, "two", "three"), Path.Combine (sep + "one" + sep + sep, "two", "three"), "#A3");
1284
1285                         Assert.AreEqual ("", Path.Combine ("", "", ""), "#A4");
1286                 }
1287
1288                 [Test]
1289                 public void Combine_4Params ()
1290                 {
1291                         string sep = Path.DirectorySeparatorChar.ToString ();
1292
1293                         try {
1294                                 Path.Combine (null, "two", "three", "four");
1295                                 Assert.Fail ("#A1-1");
1296                         } catch {
1297                                 // success
1298                         }
1299
1300                         try {
1301                                 Path.Combine ("one", null, "three", "four");
1302                                 Assert.Fail ("#A1-2");
1303                         } catch {
1304                                 // success
1305                         }
1306
1307                         try {
1308                                 Path.Combine ("one", "two", null, "four");
1309                                 Assert.Fail ("#A1-3");
1310                         } catch {
1311                                 // success
1312                         }
1313
1314                         try {
1315                                 Path.Combine ("one", "two", "three", null);
1316                                 Assert.Fail ("#A1-4");
1317                         } catch {
1318                                 // success
1319                         }
1320
1321                         Assert.AreEqual (Concat (sep, "one", "two", "three", "four"), Path.Combine ("one", "two", "three", "four"), "#A2-1");
1322                         Assert.AreEqual (Concat (sep, sep + "one", "two", "three", "four"), Path.Combine (sep + "one", "two", "three", "four"), "#A2-2");
1323                         Assert.AreEqual (Concat (sep, sep + "one", "two", "three", "four"), Path.Combine (sep + "one" + sep, "two", "three", "four"), "#A2-3");
1324                         Assert.AreEqual (Concat (sep, sep + "two", "three", "four"), Path.Combine (sep + "one" + sep, sep + "two", "three", "four"), "#A2-4");
1325                         Assert.AreEqual (Concat (sep, sep + "three", "four"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", "four"), "#A2-5");
1326                         Assert.AreEqual (Concat (sep, sep + "four"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", sep + "four"), "#A2-6");
1327
1328                         Assert.AreEqual (Concat (sep, sep + "one" + sep, "two", "three", "four"), Path.Combine (sep + "one" + sep + sep, "two", "three", "four"), "#A3");
1329
1330                         Assert.AreEqual ("", Path.Combine ("", "", "", ""), "#A4");
1331                 }
1332
1333                 [Test]
1334                 public void Combine_ManyParams ()
1335                 {
1336                         string sep = Path.DirectorySeparatorChar.ToString ();
1337
1338                         try {
1339                                 Path.Combine (null, "two", "three", "four", "five");
1340                                 Assert.Fail ("#A1-1");
1341                         } catch {
1342                                 // success
1343                         }
1344
1345                         try {
1346                                 Path.Combine ("one", null, "three", "four", "five");
1347                                 Assert.Fail ("#A1-2");
1348                         } catch {
1349                                 // success
1350                         }
1351
1352                         try {
1353                                 Path.Combine ("one", "two", null, "four", "five");
1354                                 Assert.Fail ("#A1-3");
1355                         } catch {
1356                                 // success
1357                         }
1358
1359                         try {
1360                                 Path.Combine ("one", "two", "three", null, "five");
1361                                 Assert.Fail ("#A1-4");
1362                         } catch {
1363                                 // success
1364                         }
1365
1366                         try {
1367                                 Path.Combine ("one", "two", "three", "four", null);
1368                                 Assert.Fail ("#A1-5");
1369                         } catch {
1370                                 // success
1371                         }
1372
1373                         Assert.AreEqual (Concat (sep, "one", "two", "three", "four", "five"), Path.Combine ("one", "two", "three", "four", "five"), "#A2-1");
1374                         Assert.AreEqual (Concat (sep, sep + "one", "two", "three", "four", "five"), Path.Combine (sep + "one", "two", "three", "four", "five"), "#A2-2");
1375                         Assert.AreEqual (Concat (sep, sep + "one", "two", "three", "four", "five"), Path.Combine (sep + "one" + sep, "two", "three", "four", "five"), "#A2-3");
1376                         Assert.AreEqual (Concat (sep, sep + "two", "three", "four", "five"), Path.Combine (sep + "one" + sep, sep + "two", "three", "four", "five"), "#A2-4");
1377                         Assert.AreEqual (Concat (sep, sep + "three", "four", "five"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", "four", "five"), "#A2-5");
1378                         Assert.AreEqual (Concat (sep, sep + "four", "five"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", sep + "four", "five"), "#A2-6");
1379                         Assert.AreEqual (Concat (sep, sep + "five"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", sep + "four", sep + "five"), "#A2-6");
1380
1381                         Assert.AreEqual (Concat (sep, sep + "one" + sep, "two", "three", "four", "five"), Path.Combine (sep + "one" + sep + sep, "two", "three", "four", "five"), "#A3");
1382
1383                         Assert.AreEqual ("", Path.Combine ("", "", "", "", ""), "#A4");
1384                 }
1385 #endif
1386         }
1387 }
1388