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