2 // System.IO.Path Test Cases
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>
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)
19 using NUnit.Framework;
24 namespace MonoTests.System.IO
33 public class PathTest : Assertion
39 static char DSC = Path.DirectorySeparatorChar;
46 path1 = "/foo/test.txt";
49 } else if ('\\' == DSC) {
51 path1 = "c:\\foo\\test.txt";
52 path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
56 //FIXME: For Mac. figure this out when we need it
57 path1 = "foo:test.txt";
66 return OS == OsType.Windows;
73 return OS == OsType.Unix;
80 return OS == OsType.Mac;
85 public void ChangeExtension ()
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";
92 string testPath = Path.ChangeExtension (path1, "doc");
93 AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
95 testPath = Path.ChangeExtension (String.Empty, ".extension");
96 AssertEquals ("ChangeExtension #02", String.Empty, testPath);
98 testPath = Path.ChangeExtension (null, ".extension");
99 AssertEquals ("ChangeExtension #03", null, testPath);
101 testPath = Path.ChangeExtension ("path", null);
102 AssertEquals ("ChangeExtension #04", "path", testPath);
104 testPath = Path.ChangeExtension ("path.ext", "doc");
105 AssertEquals ("ChangeExtension #05", "path.doc", testPath);
107 testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
108 AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
110 testPath = Path.ChangeExtension ("hogehoge.xml", ".xsl");
111 AssertEquals ("ChangeExtension #07", "hogehoge.xsl", testPath);
112 testPath = Path.ChangeExtension ("hogehoge", ".xsl");
113 AssertEquals ("ChangeExtension #08", "hogehoge.xsl", testPath);
114 testPath = Path.ChangeExtension ("hogehoge.xml", "xsl");
115 AssertEquals ("ChangeExtension #09", "hogehoge.xsl", testPath);
116 testPath = Path.ChangeExtension ("hogehoge", "xsl");
117 AssertEquals ("ChangeExtension #10", "hogehoge.xsl", testPath);
118 testPath = Path.ChangeExtension ("hogehoge.xml", String.Empty);
119 AssertEquals ("ChangeExtension #11", "hogehoge.", testPath);
120 testPath = Path.ChangeExtension ("hogehoge", String.Empty);
121 AssertEquals ("ChangeExtension #12", "hogehoge.", testPath);
122 testPath = Path.ChangeExtension ("hogehoge.", null);
123 AssertEquals ("ChangeExtension #13", "hogehoge", testPath);
124 testPath = Path.ChangeExtension ("hogehoge", null);
125 AssertEquals ("ChangeExtension #14", "hogehoge", testPath);
126 testPath = Path.ChangeExtension (String.Empty, null);
127 AssertEquals ("ChangeExtension #15", String.Empty, testPath);
128 testPath = Path.ChangeExtension (String.Empty, "bashrc");
129 AssertEquals ("ChangeExtension #16", String.Empty, testPath);
130 testPath = Path.ChangeExtension (String.Empty, ".bashrc");
131 AssertEquals ("ChangeExtension #17", String.Empty, testPath);
132 testPath = Path.ChangeExtension (null, null);
133 AssertNull ("ChangeExtension #18", testPath);
137 public void ChangeExtension_Extension_InvalidPathChars ()
139 string fn = Path.ChangeExtension ("file.ext", "<");
140 AssertEquals ("Invalid filename", "file.<", fn);
144 public void ChangeExtension_Path_InvalidPathChars ()
147 Path.ChangeExtension ("fi\0le.ext", ".extension");
149 } catch (ArgumentException ex) {
150 // Illegal characters in path
151 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
152 AssertNull ("#3", ex.InnerException);
153 AssertNotNull ("#4", ex.Message);
154 AssertNull ("#5", ex.ParamName);
159 public void Combine ()
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";
166 string testPath = Path.Combine (path2, path3);
167 AssertEquals ("Combine #01", files [(int) OS], testPath);
169 testPath = Path.Combine ("one", String.Empty);
170 AssertEquals ("Combine #02", "one", testPath);
172 testPath = Path.Combine (String.Empty, "one");
173 AssertEquals ("Combine #03", "one", testPath);
175 string current = Directory.GetCurrentDirectory ();
176 testPath = Path.Combine (current, "one");
178 string expected = current + DSC + "one";
179 AssertEquals ("Combine #04", expected, testPath);
181 testPath = Path.Combine ("one", current);
182 // LAMESPEC noted in Path.cs
183 AssertEquals ("Combine #05", current, testPath);
185 testPath = Path.Combine (current, expected);
186 AssertEquals ("Combine #06", expected, testPath);
188 testPath = DSC + "one";
189 testPath = Path.Combine (testPath, "two" + DSC);
190 expected = DSC + "one" + DSC + "two" + DSC;
191 AssertEquals ("Combine #06", expected, testPath);
193 testPath = "one" + DSC;
194 testPath = Path.Combine (testPath, DSC + "two");
195 expected = DSC + "two";
196 AssertEquals ("Combine #06", expected, testPath);
198 testPath = "one" + DSC;
199 testPath = Path.Combine (testPath, "two" + DSC);
200 expected = "one" + DSC + "two" + DSC;
201 AssertEquals ("Combine #07", expected, testPath);
205 public void Combine_Path1_InvalidPathChars ()
208 Path.Combine ("a\0", "one");
210 } catch (ArgumentException ex) {
211 // Illegal characters in path
212 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
213 AssertNull ("#3", ex.InnerException);
214 AssertNotNull ("#4", ex.Message);
215 AssertNull ("#5", ex.ParamName);
220 public void Combine_Path1_Null ()
223 Path.Combine (null, "one");
225 } catch (ArgumentNullException ex) {
226 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
227 AssertNull ("#3", ex.InnerException);
228 AssertNotNull ("#4", ex.Message);
229 AssertEquals ("#5", "path1", ex.ParamName);
234 public void Combine_Path2_InvalidPathChars ()
237 Path.Combine ("one", "a\0");
239 } catch (ArgumentException ex) {
240 // Illegal characters in path
241 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
242 AssertNull ("#3", ex.InnerException);
243 AssertNotNull ("#4", ex.Message);
244 AssertNull ("#5", ex.ParamName);
249 public void Combine_Path2_Null ()
252 Path.Combine ("one", null);
254 } catch (ArgumentNullException ex) {
255 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
256 AssertNull ("#3", ex.InnerException);
257 AssertNotNull ("#4", ex.Message);
258 AssertEquals ("#5", "path2", ex.ParamName);
263 public void GetDirectoryName ()
265 string [] files = new string [3];
266 files [(int) OsType.Unix] = "/foo";
267 files [(int) OsType.Windows] = "c:\\foo";
268 files [(int) OsType.Mac] = "foo";
270 string testDirName = Path.GetDirectoryName (path1);
271 AssertEquals ("#A1", files [(int) OS], testDirName);
272 testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
273 AssertEquals ("#A2", files [(int) OS], testDirName);
276 AssertEquals ("#B1", "C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"));
277 AssertEquals ("#B2", null, Path.GetDirectoryName ("C:"));
278 AssertEquals ("#B3", null, Path.GetDirectoryName (@"C:\"));
279 AssertEquals ("#B4", @"C:\", Path.GetDirectoryName (@"C:\dir"));
280 AssertEquals ("#B5", @"C:\dir", Path.GetDirectoryName (@"C:\dir\"));
281 AssertEquals ("#B6", @"C:\dir", Path.GetDirectoryName (@"C:\dir\dir"));
282 AssertEquals ("#B7", @"C:\dir\dir", Path.GetDirectoryName (@"C:\dir\dir\"));
284 AssertEquals ("#C1", "\\foo\\bar", Path.GetDirectoryName ("/foo//bar/dingus"));
285 AssertEquals ("#C2", "foo\\bar", Path.GetDirectoryName ("foo/bar/"));
286 AssertEquals ("#C3", "foo\\bar", Path.GetDirectoryName ("foo/bar\\xxx"));
287 AssertEquals ("#C4", "\\\\host\\dir\\dir2", Path.GetDirectoryName ("\\\\host\\dir\\\\dir2\\path"));
290 AssertEquals ("#D1", null, Path.GetDirectoryName (@"\\"));
291 AssertEquals ("#D2", null, Path.GetDirectoryName (@"\\server"));
292 AssertEquals ("#D3", null, Path.GetDirectoryName (@"\\server\share"));
293 AssertEquals ("#D4", @"\\server\share", Path.GetDirectoryName (@"\\server\share\"));
294 AssertEquals ("#D5", @"\\server\share", Path.GetDirectoryName (@"\\server\share\dir"));
295 AssertEquals ("#D6", @"\\server\share\dir", Path.GetDirectoryName (@"\\server\share\dir\subdir"));
297 AssertEquals ("#B1", "/etc", Path.GetDirectoryName ("/etc/hostname"));
298 AssertEquals ("#B2", "/foo/bar", Path.GetDirectoryName ("/foo//bar/dingus"));
299 AssertEquals ("#B3", "foo/bar", Path.GetDirectoryName ("foo/bar/"));
300 AssertEquals ("#B4", "/", Path.GetDirectoryName ("/tmp"));
301 AssertNull ("#B5", Path.GetDirectoryName ("/"));
302 AssertEquals ("#B6", "a", Path.GetDirectoryName ("a//b"));
307 public void GetDirectoryName_Path_Empty ()
310 Path.GetDirectoryName (String.Empty);
312 } catch (ArgumentException ex) {
313 // The path is not of a legal form
314 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
315 AssertNull ("#3", ex.InnerException);
316 AssertNotNull ("#4", ex.Message);
317 AssertNull ("#5", ex.ParamName);
322 public void GetDirectoryName_Path_InvalidPathChars ()
325 Path.GetDirectoryName ("hi\0world");
327 } catch (ArgumentException ex) {
328 // Illegal characters in path
329 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
330 AssertNull ("#3", ex.InnerException);
331 AssertNotNull ("#4", ex.Message);
332 AssertNull ("#5", ex.ParamName);
337 public void GetDirectoryName_Path_Null ()
339 AssertNull (Path.GetDirectoryName (null));
343 public void GetDirectoryName_Path_Whitespace ()
346 Path.GetDirectoryName (" ");
348 } catch (ArgumentException ex) {
349 // The path is not of a legal form
350 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
351 AssertNull ("#3", ex.InnerException);
352 AssertNotNull ("#4", ex.Message);
353 AssertNull ("#5", ex.ParamName);
358 public void GetExtension ()
360 string testExtn = Path.GetExtension (path1);
362 AssertEquals ("GetExtension #01", ".txt", testExtn);
364 testExtn = Path.GetExtension (path2);
365 AssertEquals ("GetExtension #02", String.Empty, testExtn);
367 testExtn = Path.GetExtension (String.Empty);
368 AssertEquals ("GetExtension #03", String.Empty, testExtn);
370 testExtn = Path.GetExtension (null);
371 AssertEquals ("GetExtension #04", null, testExtn);
373 testExtn = Path.GetExtension (" ");
374 AssertEquals ("GetExtension #05", String.Empty, testExtn);
376 testExtn = Path.GetExtension (path1 + ".doc");
377 AssertEquals ("GetExtension #06", ".doc", testExtn);
379 testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
380 AssertEquals ("GetExtension #07", ".txt", testExtn);
382 testExtn = Path.GetExtension (".");
383 AssertEquals ("GetExtension #08", String.Empty, testExtn);
385 testExtn = Path.GetExtension ("end.");
386 AssertEquals ("GetExtension #09", String.Empty, testExtn);
388 testExtn = Path.GetExtension (".start");
389 AssertEquals ("GetExtension #10", ".start", testExtn);
391 testExtn = Path.GetExtension (".a");
392 AssertEquals ("GetExtension #11", ".a", testExtn);
394 testExtn = Path.GetExtension ("a.");
395 AssertEquals ("GetExtension #12", String.Empty, testExtn);
397 testExtn = Path.GetExtension ("a");
398 AssertEquals ("GetExtension #13", String.Empty, testExtn);
400 testExtn = Path.GetExtension ("makefile");
401 AssertEquals ("GetExtension #14", String.Empty, testExtn);
405 public void GetExtension_Path_InvalidPathChars ()
408 Path.GetExtension ("hi\0world.txt");
410 } catch (ArgumentException ex) {
411 // Illegal characters in path.
412 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
413 AssertNull ("#3", ex.InnerException);
414 AssertNotNull ("#4", ex.Message);
415 AssertNull ("#5", ex.ParamName);
420 public void GetFileName ()
422 string testFileName = Path.GetFileName (path1);
424 AssertEquals ("#1", "test.txt", testFileName);
425 testFileName = Path.GetFileName (null);
426 AssertEquals ("#2", null, testFileName);
427 testFileName = Path.GetFileName (String.Empty);
428 AssertEquals ("#3", String.Empty, testFileName);
429 testFileName = Path.GetFileName (" ");
430 AssertEquals ("#4", " ", testFileName);
434 public void GetFileName_Path_InvalidPathChars ()
437 Path.GetFileName ("hi\0world");
439 } catch (ArgumentException ex) {
440 // Illegal characters in path
441 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
442 AssertNull ("#3", ex.InnerException);
443 AssertNotNull ("#4", ex.Message);
444 AssertNull ("#5", ex.ParamName);
449 public void GetFileNameWithoutExtension ()
451 string testFileName = Path.GetFileNameWithoutExtension (path1);
453 AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
455 testFileName = Path.GetFileNameWithoutExtension (null);
456 AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
458 testFileName = Path.GetFileNameWithoutExtension (String.Empty);
459 AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
463 public void GetFileNameWithoutExtension_Path_InvalidPathChars ()
466 Path.GetFileNameWithoutExtension ("hi\0world");
468 } catch (ArgumentException ex) {
469 // Illegal characters in path
470 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
471 AssertNull ("#3", ex.InnerException);
472 AssertNotNull ("#4", ex.Message);
473 AssertNull ("#5", ex.ParamName);
478 public void GetFullPath ()
480 string current = Directory.GetCurrentDirectory ();
482 string testFullPath = Path.GetFullPath ("foo.txt");
483 string expected = current + DSC + "foo.txt";
484 AssertEquals ("GetFullPath #01", expected, testFullPath);
486 testFullPath = Path.GetFullPath ("a//./.././foo.txt");
487 AssertEquals ("GetFullPath #02", expected, testFullPath);
491 public void GetFullPath_Unix ()
497 string [,] test = new string [,] {
498 {"root////././././././../root/././../root", "root"},
500 {"root/./", "root/"},
501 {"root/./", "root/"},
502 {"root/../", String.Empty},
503 {"root/../", String.Empty},
504 {"root/../..", String.Empty},
505 {"root/.hiddenfile", "root/.hiddenfile"},
506 {"root/. /", "root/. /"},
507 {"root/.. /", "root/.. /"},
508 {"root/..weirdname", "root/..weirdname"},
509 {"root/..", String.Empty},
510 {"root/../a/b/../../..", String.Empty},
511 {"root/./..", String.Empty},
512 {"..", String.Empty},
514 {"root//dir", "root/dir"},
515 {"root/. /", "root/. /"},
516 {"root/.. /", "root/.. /"},
517 {"root/ . /", "root/ . /"},
518 {"root/ .. /", "root/ .. /"},
519 {"root/./", "root/"},
520 //ERROR! Paths are trimmed
521 // I don't understand this comment^^.
522 // No trimming occurs but the paths are not equal. That's why the test fails. Commented out.
523 //{"root/.. /", "root/.. /"},
524 {".//", String.Empty}
527 for (int i = 0; i < test.GetUpperBound (0); i++) {
528 AssertEquals (String.Format ("GetFullPathUnix #{0}", i),
529 root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
532 AssertEquals ("#01", "/", Path.GetFullPath ("/"));
533 AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
534 AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
535 AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
536 Path.GetFullPath ("hey"));
540 public void GetFullPath_Windows ()
545 string root = "C:\\";
546 string [,] test = new string [,] {
547 {"root////././././././../root/././../root", "root"},
549 {"root/./", "root\\"},
550 {"root/./", "root\\"},
554 {"root/.hiddenfile", "root\\.hiddenfile"},
555 {"root/. /", "root\\"},
557 {"root/..weirdname", "root\\..weirdname"},
559 {"root/../a/b/../../..", ""},
563 {"root//dir", "root\\dir"},
564 {"root/. /", "root\\"},
566 {"root/ . /", "root\\"},
568 {"root/./", "root\\"},
573 for (int i = 0; i < test.GetUpperBound (0); i++) {
574 AssertEquals (String.Format ("GetFullPathWindows #{0}", i),
575 root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
579 string root2 = @"\\server\share";
580 root = @"\\server\share\";
581 test = new string [,] {
582 {"root////././././././../root/././../root", "root"},
584 {"root/./", "root\\"},
585 {"root/./", "root\\"},
588 {"root/../..", null},
589 {"root/.hiddenfile", "root\\.hiddenfile"},
590 {"root/. /", "root\\"},
592 {"root/..weirdname", "root\\..weirdname"},
594 {"root/../a/b/../../..", null},
598 {"root//dir", "root\\dir"},
599 {"root/. /", "root\\"},
601 {"root/ . /", "root\\"},
603 {"root/./", "root\\"},
608 for (int i = 0; i < test.GetUpperBound (0); i++) {
609 // "null" means we have to compare against "root2"
610 string res = test [i, 1] != null
613 AssertEquals (String.Format ("GetFullPathWindows UNC #{0}", i),
614 res, Path.GetFullPath (root + test [i, 0]));
619 public void GetFullPath_Path_Empty ()
622 Path.GetFullPath (String.Empty);
624 } catch (ArgumentException ex) {
625 // The path is not of a legal form
626 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
627 AssertNull ("#3", ex.InnerException);
628 AssertNotNull ("#4", ex.Message);
629 AssertNull ("#5", ex.ParamName);
634 public void GetFullPath_Path_EndingSeparator ()
636 string fp = Path.GetFullPath ("something/");
637 char end = fp [fp.Length - 1];
638 Assert (end == Path.DirectorySeparatorChar);
642 public void GetFullPath_Path_InvalidPathChars ()
645 Path.GetFullPath ("hi\0world");
647 } catch (ArgumentException ex) {
648 // Illegal characters in path
649 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
650 AssertNull ("#3", ex.InnerException);
651 AssertNotNull ("#4", ex.Message);
652 AssertNull ("#5", ex.ParamName);
657 public void GetFullPath_Path_Null ()
660 Path.GetFullPath (null);
662 } catch (ArgumentNullException ex) {
663 AssertEquals ("#2", typeof (ArgumentNullException), ex.GetType ());
664 AssertNull ("#3", ex.InnerException);
665 AssertNotNull ("#4", ex.Message);
666 AssertEquals ("#5", "path", ex.ParamName);
671 public void GetFullPath_Path_Whitespace ()
674 Path.GetFullPath (" ");
676 } catch (ArgumentException ex) {
677 // The path is not of a legal form
678 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
679 AssertNull ("#3", ex.InnerException);
680 AssertNotNull ("#4", ex.Message);
681 AssertNull ("#5", ex.ParamName);
686 public void GetFullPath2 ()
689 AssertEquals ("GetFullPath w#01", @"Z:\", Path.GetFullPath ("Z:"));
690 #if !TARGET_JVM // Java full (canonical) path always starts with caps drive letter
691 AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path.GetFullPath (@"c:\abc\def"));
693 Assert ("GetFullPath w#03", Path.GetFullPath (@"\").EndsWith (@"\"));
694 // "\\\\" is not allowed
695 Assert ("GetFullPath w#05", Path.GetFullPath ("/").EndsWith (@"\"));
696 // "//" is not allowed
697 Assert ("GetFullPath w#07", Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
698 Assert ("GetFullPath w#08", Path.GetFullPath ("c").EndsWith (@"\c"));
699 Assert ("GetFullPath w#09", Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
700 Assert ("GetFullPath w#10", Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
701 AssertEquals ("GetFullPath w#11", @"\\abc\def", Path.GetFullPath (@"\\abc\def"));
702 AssertEquals ("GetFullPath w#12", Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"));
703 AssertEquals ("GetFullPath w#13", Directory.GetCurrentDirectory ().Substring (0, 2) + @"\abc\def", Path.GetFullPath ("/abc/def"));
704 AssertEquals ("GetFullPath w#14", @"\\abc\def", Path.GetFullPath ("//abc/def"));
706 AssertEquals ("#01", "/", Path.GetFullPath ("/"));
707 AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
708 AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
709 AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
710 Path.GetFullPath ("hey"));
715 public void GetPathRoot ()
720 current = Directory.GetCurrentDirectory ();
721 expected = current [0].ToString ();
723 current = @"J:\Some\Strange Directory\Name";
727 string pathRoot = Path.GetPathRoot (current);
728 AssertEquals ("GetPathRoot #01", expected, pathRoot);
732 public void GetPathRoot2 ()
734 // note: this method doesn't call Directory.GetCurrentDirectory so it can be
735 // reused for partial trust unit tests in PathCas.cs
739 pathRoot = Path.GetPathRoot ("hola");
740 AssertEquals ("#A1", String.Empty, pathRoot);
741 pathRoot = Path.GetPathRoot (null);
742 AssertEquals ("#A2", null, pathRoot);
745 AssertEquals ("GetPathRoot w#01", "z:", Path.GetPathRoot ("z:"));
746 AssertEquals ("GetPathRoot w#02", "c:\\", Path.GetPathRoot ("c:\\abc\\def"));
747 AssertEquals ("GetPathRoot w#03", "\\", Path.GetPathRoot ("\\"));
748 AssertEquals ("GetPathRoot w#04", "\\\\", Path.GetPathRoot ("\\\\"));
749 AssertEquals ("GetPathRoot w#05", "\\", Path.GetPathRoot ("/"));
750 AssertEquals ("GetPathRoot w#06", "\\\\", Path.GetPathRoot ("//"));
751 AssertEquals ("GetPathRoot w#07", String.Empty, Path.GetPathRoot ("readme.txt"));
752 AssertEquals ("GetPathRoot w#08", String.Empty, Path.GetPathRoot ("c"));
753 AssertEquals ("GetPathRoot w#09", String.Empty, Path.GetPathRoot ("abc\\def"));
754 AssertEquals ("GetPathRoot w#10", "\\", Path.GetPathRoot ("\\abc\\def"));
755 AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"));
756 AssertEquals ("GetPathRoot w#12", String.Empty, Path.GetPathRoot ("abc//def"));
757 AssertEquals ("GetPathRoot w#13", "\\", Path.GetPathRoot ("/abc/def"));
758 AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path.GetPathRoot ("//abc/def"));
759 AssertEquals ("GetPathRoot w#15", @"C:\", Path.GetPathRoot (@"C:\"));
760 AssertEquals ("GetPathRoot w#16", @"C:\", Path.GetPathRoot (@"C:\\"));
761 AssertEquals ("GetPathRoot w#17", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def\\ghi"));
763 // TODO: Same tests for Unix.
768 public void GetPathRoot_Path_Empty ()
771 Path.GetPathRoot (String.Empty);
773 } catch (ArgumentException ex) {
774 // The path is not of a legal form
775 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
776 AssertNull ("#3", ex.InnerException);
777 AssertNotNull ("#4", ex.Message);
778 AssertNull ("#5", ex.ParamName);
784 [Category ("NotWorking")] // we also throw ArgumentException on 1.0 profile
786 public void GetPathRoot_Path_InvalidPathChars ()
790 Path.GetPathRoot ("hi\0world");
792 } catch (ArgumentException ex) {
793 // Illegal characters in path
794 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
795 AssertNull ("#3", ex.InnerException);
796 AssertNotNull ("#4", ex.Message);
797 AssertNull ("#5", ex.ParamName);
800 AssertEquals (String.Empty, Path.GetPathRoot ("hi\0world"));
805 public void GetPathRoot_Path_Whitespace ()
808 Path.GetPathRoot (" ");
810 } catch (ArgumentException ex) {
811 // The path is not of a legal form
812 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
813 AssertNull ("#3", ex.InnerException);
814 AssertNotNull ("#4", ex.Message);
815 AssertNull ("#5", ex.ParamName);
820 public void GetTempPath ()
822 string getTempPath = Path.GetTempPath ();
823 Assert ("GetTempPath #01", getTempPath != String.Empty);
824 Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
825 AssertEquals ("GetTempPath #03", Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1]);
829 public void GetTempFileName ()
831 string getTempFileName = null;
833 getTempFileName = Path.GetTempFileName ();
834 Assert ("GetTempFileName #01", getTempFileName != String.Empty);
835 Assert ("GetTempFileName #02", File.Exists (getTempFileName));
837 if (getTempFileName != null && getTempFileName != String.Empty){
838 File.Delete (getTempFileName);
844 public void HasExtension ()
846 AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
847 AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
848 AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
849 AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
850 AssertEquals ("HasExtension #05", false, Path.HasExtension (null));
851 AssertEquals ("HasExtension #06", false, Path.HasExtension (String.Empty));
852 AssertEquals ("HasExtension #07", false, Path.HasExtension (" "));
853 AssertEquals ("HasExtension #08", false, Path.HasExtension ("."));
854 AssertEquals ("HasExtension #09", false, Path.HasExtension ("end."));
855 AssertEquals ("HasExtension #10", true, Path.HasExtension (".start"));
856 AssertEquals ("HasExtension #11", true, Path.HasExtension (".a"));
857 AssertEquals ("HasExtension #12", false, Path.HasExtension ("a."));
858 AssertEquals ("HasExtension #13", false, Path.HasExtension ("Makefile"));
862 public void HasExtension_Path_InvalidPathChars ()
865 Path.HasExtension ("hi\0world.txt");
867 } catch (ArgumentException ex) {
868 // Illegal characters in path
869 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
870 AssertNull ("#3", ex.InnerException);
871 AssertNotNull ("#4", ex.Message);
872 AssertNull ("#5", ex.ParamName);
877 public void IsPathRooted ()
879 Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
880 Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
881 Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
882 Assert ("IsPathRooted #04", !Path.IsPathRooted (String.Empty));
883 Assert ("IsPathRooted #05", !Path.IsPathRooted (" "));
884 Assert ("IsPathRooted #06", Path.IsPathRooted ("/"));
885 Assert ("IsPathRooted #07", Path.IsPathRooted ("//"));
886 Assert ("IsPathRooted #08", !Path.IsPathRooted (":"));
889 Assert ("IsPathRooted #09", Path.IsPathRooted ("\\"));
890 Assert ("IsPathRooted #10", Path.IsPathRooted ("\\\\"));
891 Assert ("IsPathRooted #11", Path.IsPathRooted ("z:"));
892 Assert ("IsPathRooted #12", Path.IsPathRooted ("z:\\"));
893 Assert ("IsPathRooted #13", Path.IsPathRooted ("z:\\topdir"));
894 // This looks MS BUG. It is treated as absolute path
895 Assert ("IsPathRooted #14", Path.IsPathRooted ("z:curdir"));
896 Assert ("IsPathRooted #15", Path.IsPathRooted ("\\abc\\def"));
898 Assert ("IsPathRooted #09", !Path.IsPathRooted ("\\"));
899 Assert ("IsPathRooted #10", !Path.IsPathRooted ("\\\\"));
900 Assert ("IsPathRooted #11", !Path.IsPathRooted ("z:"));
905 public void IsPathRooted_Path_Empty ()
907 Assert (!Path.IsPathRooted (String.Empty));
911 public void IsPathRooted_Path_InvalidPathChars ()
914 Path.IsPathRooted ("hi\0world");
916 } catch (ArgumentException ex) {
917 // Illegal characters in path.
918 AssertEquals ("#2", typeof (ArgumentException), ex.GetType ());
919 AssertNull ("#3", ex.InnerException);
920 AssertNotNull ("#4", ex.Message);
921 AssertNull ("#5", ex.ParamName);
926 public void IsPathRooted_Path_Null ()
928 Assert (!Path.IsPathRooted (null));
932 public void IsPathRooted_Path_Whitespace ()
934 Assert (!Path.IsPathRooted (" "));
938 public void CanonicalizeDots ()
940 string current = Path.GetFullPath (".");
941 Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
942 string parent = Path.GetFullPath ("..");
943 Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
947 public void WindowsSystem32_76191 ()
949 // check for Unix platforms - see FAQ for more details
950 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
951 int platform = (int) Environment.OSVersion.Platform;
952 if ((platform == 4) || (platform == 128))
955 string curdir = Directory.GetCurrentDirectory ();
958 string system = "C:\\WINDOWS\\system32\\";
960 string system = Environment.SystemDirectory;
962 Directory.SetCurrentDirectory (system);
963 string drive = system.Substring (0, 2);
964 AssertEquals ("current dir", system, Path.GetFullPath (drive));
967 Directory.SetCurrentDirectory (curdir);
972 public void WindowsSystem32_77007 ()
974 // check for Unix platforms - see FAQ for more details
975 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
976 int platform = (int) Environment.OSVersion.Platform;
977 if ((platform == 4) || (platform == 128))
980 string curdir = Directory.GetCurrentDirectory ();
983 string system = "C:\\WINDOWS\\system32\\";
985 string system = Environment.SystemDirectory;
987 Directory.SetCurrentDirectory (system);
988 // e.g. C:dir (no backslash) will return CurrentDirectory + dir
989 string dir = system.Substring (0, 2) + "dir";
990 AssertEquals ("current dir", Path.Combine (system, "dir"), Path.GetFullPath (dir));
993 Directory.SetCurrentDirectory (curdir);
999 [Ignore("Java full (canonical) path always returns windows dir in caps")]
1001 public void WindowsDriveC14N_77058 ()
1003 // check for Unix platforms - see FAQ for more details
1004 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
1005 int platform = (int) Environment.OSVersion.Platform;
1006 if ((platform == 4) || (platform == 128))
1009 AssertEquals ("1", @"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"));
1010 AssertEquals ("2", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"));
1011 AssertEquals ("3", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"));
1012 AssertEquals ("4", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"));
1013 AssertEquals ("5", @"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"));
1017 public void InvalidPathChars_Values ()
1019 char[] invalid = Path.InvalidPathChars;
1022 AssertEquals ("Length", 36, invalid.Length);
1024 AssertEquals ("Length", 15, invalid.Length);
1026 foreach (char c in invalid) {
1032 if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
1035 // in both 1.1 SP1 and 2.0
1036 if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
1038 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1041 foreach (char c in invalid) {
1045 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1051 public void InvalidPathChars_Modify ()
1053 char[] expected = Path.InvalidPathChars;
1054 char[] invalid = Path.InvalidPathChars;
1055 char original = invalid[0];
1059 Assert ("expected", expected[0] == 'a');
1060 AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]);
1062 invalid[0] = original;
1068 public void GetInvalidFileNameChars_Values ()
1070 char[] invalid = Path.GetInvalidFileNameChars ();
1072 AssertEquals (41, invalid.Length);
1073 foreach (char c in invalid) {
1077 if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
1079 // ':', '*', '?', '\', '/'
1080 if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
1082 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1085 foreach (char c in invalid) {
1088 if ((i == 0) || (i == 47))
1090 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1096 public void GetInvalidFileNameChars_Modify ()
1098 char[] expected = Path.GetInvalidFileNameChars ();
1099 char[] invalid = Path.GetInvalidFileNameChars ();
1101 Assert ("expected", expected[0] != 'a');
1102 AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]);
1106 public void GetInvalidPathChars_Values ()
1108 char[] invalid = Path.GetInvalidPathChars ();
1110 AssertEquals (36, invalid.Length);
1111 foreach (char c in invalid) {
1115 if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
1117 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1120 foreach (char c in invalid) {
1124 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
1130 public void GetInvalidPathChars_Order ()
1133 char [] invalid = Path.GetInvalidPathChars ();
1134 char [] expected = new char [36] { '\x22', '\x3C', '\x3E', '\x7C', '\x00', '\x01', '\x02',
1135 '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D',
1136 '\x0E', '\x0F', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18',
1137 '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' };
1138 AssertEquals (expected.Length, invalid.Length);
1139 for (int i = 0; i < expected.Length; i++ ) {
1140 AssertEquals( "Character at position " + i,expected [i], invalid [i]);
1146 public void GetInvalidPathChars_Modify ()
1148 char[] expected = Path.GetInvalidPathChars ();
1149 char[] invalid = Path.GetInvalidPathChars ();
1151 Assert ("expected", expected[0] != 'a');
1152 AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]);
1156 public void GetRandomFileName ()
1158 string s = Path.GetRandomFileName ();
1159 AssertEquals ("Length", 12, s.Length);
1160 char[] invalid = Path.GetInvalidFileNameChars ();
1161 for (int i=0; i < s.Length; i++) {
1163 AssertEquals ("8", '.', s[i]);
1165 Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1);
1170 public void GetRandomFileNameIsAlphaNumerical ()
1172 string [] names = new string [1000];
1173 for (int i = 0; i < names.Length; i++)
1174 names [i] = Path.GetRandomFileName ();
1176 foreach (string name in names) {
1177 AssertEquals (12, name.Length);
1178 AssertEquals ('.', name [8]);
1180 for (int i = 0; i < 12; i++) {
1185 Assert (('a' <= c && c <= 'z') || ('0' <= c && c <= '9'));