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
34 public class PathTest : Assertion
40 static char DSC = Path.DirectorySeparatorChar;
47 path1 = "/foo/test.txt";
50 } else if ('\\' == DSC) {
52 path1 = "c:\\foo\\test.txt";
53 path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
57 //FIXME: For Mac. figure this out when we need it
58 path1 = "foo:test.txt";
67 return OS == OsType.Windows;
74 return OS == OsType.Unix;
81 return OS == OsType.Mac;
85 public void TestChangeExtension ()
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 ("", ".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 [ExpectedException (typeof (ArgumentException))]
138 public void ChangeExtension_BadPath ()
140 if (!Windows) throw new ArgumentException ("Test Only On Windows");
141 Path.ChangeExtension ("<", ".extension");
145 public void ChangeExtension_BadExtension ()
148 string fn = Path.ChangeExtension ("file.ext", "<");
149 AssertEquals ("Invalid filename", "file.<", fn);
153 public void TestCombine ()
155 string [] files = new string [3];
156 files [(int) OsType.Unix] = "/etc/init.d";
157 files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
158 files [(int) OsType.Mac] = "foo:bar";
160 string testPath = Path.Combine (path2, path3);
161 AssertEquals ("Combine #01", files [(int) OS], testPath);
163 testPath = Path.Combine ("one", "");
164 AssertEquals ("Combine #02", "one", testPath);
166 testPath = Path.Combine ("", "one");
167 AssertEquals ("Combine #03", "one", testPath);
169 string current = Directory.GetCurrentDirectory ();
170 testPath = Path.Combine (current, "one");
172 string expected = current + DSC + "one";
173 AssertEquals ("Combine #04", expected, testPath);
175 testPath = Path.Combine ("one", current);
176 // LAMESPEC noted in Path.cs
177 AssertEquals ("Combine #05", current, testPath);
179 testPath = Path.Combine (current, expected);
180 AssertEquals ("Combine #06", expected, testPath);
182 testPath = DSC + "one";
183 testPath = Path.Combine (testPath, "two" + DSC);
184 expected = DSC + "one" + DSC + "two" + DSC;
185 AssertEquals ("Combine #06", expected, testPath);
187 testPath = "one" + DSC;
188 testPath = Path.Combine (testPath, DSC + "two");
189 expected = DSC + "two";
190 AssertEquals ("Combine #06", expected, testPath);
192 testPath = "one" + DSC;
193 testPath = Path.Combine (testPath, "two" + DSC);
194 expected = "one" + DSC + "two" + DSC;
195 AssertEquals ("Combine #07", expected, testPath);
197 //TODO: Tests for UNC names
199 testPath = Path.Combine ("one", null);
200 Fail ("Combine Fail #01");
201 } catch (Exception e) {
202 AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
206 testPath = Path.Combine (null, "one");
207 Fail ("Combine Fail #02");
208 } catch (Exception e) {
209 AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
214 testPath = Path.Combine ("a>", "one");
215 Fail ("Combine Fail #03");
216 } catch (Exception e) {
217 AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
221 testPath = Path.Combine ("one", "aaa<");
222 Fail ("Combine Fail #04");
223 } catch (Exception e) {
224 AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
230 [ExpectedException (typeof(ArgumentException))]
231 public void EmptyDirectoryName ()
233 string testDirName = Path.GetDirectoryName ("");
236 public void TestDirectoryName ()
238 string [] files = new string [3];
239 files [(int) OsType.Unix] = "/foo";
240 files [(int) OsType.Windows] = "c:\\foo";
241 files [(int) OsType.Mac] = "foo";
243 AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
244 string testDirName = Path.GetDirectoryName (path1);
245 AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
246 testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
247 AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
251 testDirName = Path.GetDirectoryName ("aaa>");
252 Fail ("GetDirectoryName Fail #02");
253 } catch (Exception e) {
254 AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
259 testDirName = Path.GetDirectoryName (" ");
260 Fail ("GetDirectoryName Fail #03");
261 } catch (Exception e) {
262 AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
266 AssertEquals ("GetDirectoryName #04", null, Path.GetDirectoryName ("C:"));
267 AssertEquals ("GetDirectoryName #05", null, Path.GetDirectoryName (@"C:\"));
268 AssertEquals ("GetDirectoryName #06", @"C:\", Path.GetDirectoryName (@"C:\dir"));
269 AssertEquals ("GetDirectoryName #07", @"C:\dir", Path.GetDirectoryName (@"C:\dir\"));
270 AssertEquals ("GetDirectoryName #08", @"C:\dir", Path.GetDirectoryName (@"C:\dir\dir"));
271 AssertEquals ("GetDirectoryName #09", @"C:\dir\dir", Path.GetDirectoryName (@"C:\dir\dir\"));
274 AssertEquals ("GetDirectoryName #10", null, Path.GetDirectoryName (@"\\"));
275 AssertEquals ("GetDirectoryName #11", null, Path.GetDirectoryName (@"\\server"));
276 AssertEquals ("GetDirectoryName #12", null, Path.GetDirectoryName (@"\\server\share"));
278 AssertEquals ("GetDirectoryName #13", @"\\server\share", Path.GetDirectoryName (@"\\server\share\"));
279 AssertEquals ("GetDirectoryName #14", @"\\server\share", Path.GetDirectoryName (@"\\server\share\dir"));
280 AssertEquals ("GetDirectoryName #15", @"\\server\share\dir", Path.GetDirectoryName (@"\\server\share\dir\subdir"));
285 public void TestGetExtension ()
287 string testExtn = Path.GetExtension (path1);
289 AssertEquals ("GetExtension #01", ".txt", testExtn);
291 testExtn = Path.GetExtension (path2);
292 AssertEquals ("GetExtension #02", String.Empty, testExtn);
294 testExtn = Path.GetExtension (String.Empty);
295 AssertEquals ("GetExtension #03", String.Empty, testExtn);
297 testExtn = Path.GetExtension (null);
298 AssertEquals ("GetExtension #04", null, testExtn);
300 testExtn = Path.GetExtension (" ");
301 AssertEquals ("GetExtension #05", String.Empty, testExtn);
303 testExtn = Path.GetExtension (path1 + ".doc");
304 AssertEquals ("GetExtension #06", ".doc", testExtn);
306 testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
307 AssertEquals ("GetExtension #07", ".txt", testExtn);
309 testExtn = Path.GetExtension (".");
310 AssertEquals ("GetExtension #08", String.Empty, testExtn);
312 testExtn = Path.GetExtension ("end.");
313 AssertEquals ("GetExtension #09", String.Empty, testExtn);
315 testExtn = Path.GetExtension (".start");
316 AssertEquals ("GetExtension #10", ".start", testExtn);
318 testExtn = Path.GetExtension (".a");
319 AssertEquals ("GetExtension #11", ".a", testExtn);
321 testExtn = Path.GetExtension ("a.");
322 AssertEquals ("GetExtension #12", String.Empty, testExtn);
324 testExtn = Path.GetExtension ("a");
325 AssertEquals ("GetExtension #13", String.Empty, testExtn);
327 testExtn = Path.GetExtension ("makefile");
328 AssertEquals ("GetExtension #14", String.Empty, testExtn);
332 testExtn = Path.GetExtension ("hi<there.txt");
333 Fail ("GetExtension Fail #01");
334 } catch (Exception e) {
335 AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
340 public void TestGetFileName ()
342 string testFileName = Path.GetFileName (path1);
344 AssertEquals ("GetFileName #01", "test.txt", testFileName);
345 testFileName = Path.GetFileName (null);
346 AssertEquals ("GetFileName #02", null, testFileName);
347 testFileName = Path.GetFileName (String.Empty);
348 AssertEquals ("GetFileName #03", String.Empty, testFileName);
349 testFileName = Path.GetFileName (" ");
350 AssertEquals ("GetFileName #04", " ", testFileName);
354 testFileName = Path.GetFileName ("hi<");
355 Fail ("GetFileName Fail #01");
356 } catch (Exception e) {
357 AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
362 public void TestGetFileNameWithoutExtension ()
364 string testFileName = Path.GetFileNameWithoutExtension (path1);
366 AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
368 testFileName = Path.GetFileNameWithoutExtension (null);
369 AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
371 testFileName = Path.GetFileNameWithoutExtension (String.Empty);
372 AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
375 [Ignore("This does not work under windows. See ERROR comments below.")]
376 public void TestGetFullPath ()
378 string current = Directory.GetCurrentDirectory ();
380 string testFullPath = Path.GetFullPath ("foo.txt");
381 string expected = current + DSC + "foo.txt";
382 AssertEquals ("GetFullPath #01", expected, testFullPath);
384 testFullPath = Path.GetFullPath ("a//./.././foo.txt");
385 AssertEquals ("GetFullPath #02", expected, testFullPath);
386 string root = Windows ? "C:\\" : "/";
387 string [,] test = new string [,] {
388 {"root////././././././../root/././../root", "root"},
390 {"root/./", "root/"},
391 {"root/./", "root/"},
395 {"root/.hiddenfile", "root/.hiddenfile"},
396 {"root/. /", "root/. /"},
397 {"root/.. /", "root/.. /"},
398 {"root/..weirdname", "root/..weirdname"},
400 {"root/../a/b/../../..", ""},
404 {"root//dir", "root/dir"},
405 {"root/. /", "root/. /"},
406 {"root/.. /", "root/.. /"},
407 {"root/ . /", "root/ . /"},
408 {"root/ .. /", "root/ .. /"},
409 {"root/./", "root/"},
410 //ERROR! Paths are trimmed
411 {"root/.. /", "root/.. /"},
415 //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
416 //... so only the first test was being done.
417 for (int i = 0; i < test.GetUpperBound (1); i++) {
418 AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
422 string uncroot = @"\\server\share\";
423 string [,] testunc = new string [,] {
424 {"root////././././././../root/././../root", "root"},
426 {"root/./", "root/"},
427 {"root/./", "root/"},
431 {"root/.hiddenfile", "root/.hiddenfile"},
432 {"root/. /", "root/. /"},
433 {"root/.. /", "root/.. /"},
434 {"root/..weirdname", "root/..weirdname"},
436 {"root/../a/b/../../..", ""},
440 {"root//dir", "root/dir"},
441 {"root/. /", "root/. /"},
442 {"root/.. /", "root/.. /"},
443 {"root/ . /", "root/ . /"},
444 {"root/ .. /", "root/ .. /"},
445 {"root/./", "root/"},
446 {"root/.. /", "root/.. /"},
449 for (int i = 0; i < test.GetUpperBound (1); i++) {
450 AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
455 testFullPath = Path.GetFullPath (null);
456 Fail ("GetFullPath Fail #01");
457 } catch (Exception e) {
458 AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
462 testFullPath = Path.GetFullPath (String.Empty);
463 Fail ("GetFullPath Fail #02");
464 } catch (Exception e) {
465 AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
469 public void TestGetFullPath2 ()
472 AssertEquals ("GetFullPath w#01", @"Z:\", Path.GetFullPath ("Z:"));
473 #if !TARGET_JVM // Java full (canonical) path always starts with caps drive letter
474 AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path.GetFullPath (@"c:\abc\def"));
476 Assert ("GetFullPath w#03", Path.GetFullPath (@"\").EndsWith (@"\"));
477 // "\\\\" is not allowed
478 Assert ("GetFullPath w#05", Path.GetFullPath ("/").EndsWith (@"\"));
479 // "//" is not allowed
480 Assert ("GetFullPath w#07", Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
481 Assert ("GetFullPath w#08", Path.GetFullPath ("c").EndsWith (@"\c"));
482 Assert ("GetFullPath w#09", Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
483 Assert ("GetFullPath w#10", Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
484 AssertEquals ("GetFullPath w#11", @"\\abc\def", Path.GetFullPath (@"\\abc\def"));
485 AssertEquals ("GetFullPath w#12", Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"));
486 AssertEquals ("GetFullPath w#13", Directory.GetCurrentDirectory ().Substring (0,2) + @"\abc\def", Path.GetFullPath ("/abc/def"));
487 AssertEquals ("GetFullPath w#14", @"\\abc\def", Path.GetFullPath ("//abc/def"));
491 public void TestGetPathRoot ()
496 current = Directory.GetCurrentDirectory ();
497 expected = current [0].ToString ();
499 current = @"J:\Some\Strange Directory\Name";
503 string pathRoot = Path.GetPathRoot (current);
504 AssertEquals ("GetPathRoot #01", expected, pathRoot);
508 public void TestGetPathRoot2 ()
510 // note: this method doesn't call Directory.GetCurrentDirectory so it can be
511 // reused for partial trust unit tests in PathCas.cs
513 string pathRoot = Path.GetPathRoot ("hola");
514 AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
516 pathRoot = Path.GetPathRoot (null);
517 AssertEquals ("GetPathRoot #03", null, pathRoot);
520 AssertEquals ("GetPathRoot w#01", "z:", Path.GetPathRoot ("z:"));
521 AssertEquals ("GetPathRoot w#02", "c:\\", Path.GetPathRoot ("c:\\abc\\def"));
522 AssertEquals ("GetPathRoot w#03", "\\", Path.GetPathRoot ("\\"));
523 AssertEquals ("GetPathRoot w#04", "\\\\", Path.GetPathRoot ("\\\\"));
524 AssertEquals ("GetPathRoot w#05", "\\", Path.GetPathRoot ("/"));
525 AssertEquals ("GetPathRoot w#06", "\\\\", Path.GetPathRoot ("//"));
526 AssertEquals ("GetPathRoot w#07", String.Empty, Path.GetPathRoot ("readme.txt"));
527 AssertEquals ("GetPathRoot w#08", String.Empty, Path.GetPathRoot ("c"));
528 AssertEquals ("GetPathRoot w#09", String.Empty, Path.GetPathRoot ("abc\\def"));
529 AssertEquals ("GetPathRoot w#10", "\\", Path.GetPathRoot ("\\abc\\def"));
530 AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"));
531 AssertEquals ("GetPathRoot w#12", String.Empty, Path.GetPathRoot ("abc//def"));
532 AssertEquals ("GetPathRoot w#13", "\\", Path.GetPathRoot ("/abc/def"));
533 AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path.GetPathRoot ("//abc/def"));
534 AssertEquals ("GetPathRoot w#15", @"C:\", Path.GetPathRoot (@"C:\"));
535 AssertEquals ("GetPathRoot w#16", @"C:\", Path.GetPathRoot (@"C:\\"));
536 AssertEquals ("GetPathRoot w#17", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def\\ghi"));
538 // TODO: Same tests for Unix.
542 public void TestGetTempPath ()
544 string getTempPath = Path.GetTempPath ();
545 Assert ("GetTempPath #01", getTempPath != String.Empty);
546 Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
547 AssertEquals ("GetTempPath #03", Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1]);
550 public void TestGetTempFileName ()
552 string getTempFileName = null;
554 getTempFileName = Path.GetTempFileName ();
555 Assert ("GetTempFileName #01", getTempFileName != String.Empty);
556 Assert ("GetTempFileName #02", File.Exists (getTempFileName));
558 if (getTempFileName != null && getTempFileName != String.Empty){
559 File.Delete (getTempFileName);
564 public void TestHasExtension ()
566 AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
567 AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
568 AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
569 AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
570 AssertEquals ("HasExtension #05", false, Path.HasExtension (null));
571 AssertEquals ("HasExtension #06", false, Path.HasExtension (String.Empty));
572 AssertEquals ("HasExtension #07", false, Path.HasExtension (" "));
573 AssertEquals ("HasExtension #08", false, Path.HasExtension ("."));
574 AssertEquals ("HasExtension #09", false, Path.HasExtension ("end."));
575 AssertEquals ("HasExtension #10", true, Path.HasExtension (".start"));
576 AssertEquals ("HasExtension #11", true, Path.HasExtension (".a"));
577 AssertEquals ("HasExtension #12", false, Path.HasExtension ("a."));
578 AssertEquals ("HasExtension #13", false, Path.HasExtension ("Makefile"));
581 public void TestRooted ()
583 Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
584 Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
585 Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
586 Assert ("IsPathRooted #04", !Path.IsPathRooted (String.Empty));
587 Assert ("IsPathRooted #05", !Path.IsPathRooted (" "));
588 Assert ("IsPathRooted #06", Path.IsPathRooted ("/"));
590 Assert ("IsPathRooted #07", Path.IsPathRooted ("\\"));
592 Assert ("IsPathRooted #07", !Path.IsPathRooted ("\\"));
593 Assert ("IsPathRooted #08", Path.IsPathRooted ("//"));
595 Assert ("IsPathRooted #09", Path.IsPathRooted ("\\\\"));
597 Assert ("IsPathRooted #09", !Path.IsPathRooted ("\\\\"));
598 Assert ("IsPathRooted #10", !Path.IsPathRooted (":"));
600 Assert ("IsPathRooted #11", Path.IsPathRooted ("z:"));
602 Assert ("IsPathRooted #11", !Path.IsPathRooted ("z:"));
605 Assert ("IsPathRooted #12", Path.IsPathRooted ("z:\\"));
606 Assert ("IsPathRooted #13", Path.IsPathRooted ("z:\\topdir"));
607 // This looks MS BUG. It is treated as absolute path
608 Assert ("IsPathRooted #14", Path.IsPathRooted ("z:curdir"));
609 Assert ("IsPathRooted #15", Path.IsPathRooted ("\\abc\\def"));
613 public void TestCanonicalizeDots ()
615 string current = Path.GetFullPath (".");
616 Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
617 string parent = Path.GetFullPath ("..");
618 Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
621 public void TestDirectoryNameBugs ()
624 AssertEquals ("Win #01", "C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"));
626 AssertEquals ("No win #01", "/etc", Path.GetDirectoryName ("/etc/hostname"));
630 public void TestGetFullPathUnix ()
635 AssertEquals ("#01", "/", Path.GetFullPath ("/"));
636 AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
637 AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
638 AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
639 Path.GetFullPath ("hey"));
643 public void GetFullPath_EndingSeparator ()
645 string fp = Path.GetFullPath ("something/");
646 char end = fp[fp.Length - 1];
647 Assert (end == Path.DirectorySeparatorChar);
651 public void WindowsSystem32_76191 ()
654 // check for Unix platforms - see FAQ for more details
655 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
656 int platform = (int) Environment.OSVersion.Platform;
657 if ((platform == 4) || (platform == 128))
661 string curdir = Directory.GetCurrentDirectory ();
664 string system = "C:\\WINDOWS\\system32\\";
666 string system = Environment.SystemDirectory;
668 Directory.SetCurrentDirectory (system);
669 string drive = system.Substring (0, 2);
670 AssertEquals ("current dir", system, Path.GetFullPath (drive));
673 Directory.SetCurrentDirectory (curdir);
678 public void WindowsSystem32_77007 ()
681 // check for Unix platforms - see FAQ for more details
682 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
683 int platform = (int) Environment.OSVersion.Platform;
684 if ((platform == 4) || (platform == 128))
688 string curdir = Directory.GetCurrentDirectory ();
691 string system = "C:\\WINDOWS\\system32\\";
693 string system = Environment.SystemDirectory;
695 Directory.SetCurrentDirectory (system);
696 // e.g. C:dir (no backslash) will return CurrentDirectory + dir
697 string dir = system.Substring (0, 2) + "dir";
698 AssertEquals ("current dir", Path.Combine (system, "dir"), Path.GetFullPath (dir));
701 Directory.SetCurrentDirectory (curdir);
707 [Ignore("Java full (canonical) path always returns windows dir in caps")]
709 public void WindowsDriveC14N_77058 ()
711 // check for Unix platforms - see FAQ for more details
712 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
713 int platform = (int) Environment.OSVersion.Platform;
714 if ((platform == 4) || (platform == 128))
717 AssertEquals ("1", @"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"));
718 AssertEquals ("2", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"));
719 AssertEquals ("3", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"));
720 AssertEquals ("4", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"));
721 AssertEquals ("5", @"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"));
725 public void InvalidPathChars_Values ()
727 char[] invalid = Path.InvalidPathChars;
730 AssertEquals ("Length", 36, invalid.Length);
732 AssertEquals ("Length", 15, invalid.Length);
734 foreach (char c in invalid) {
740 if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
743 // in both 1.1 SP1 and 2.0
744 if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
746 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
749 foreach (char c in invalid) {
753 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
759 public void InvalidPathChars_Modify ()
761 char[] expected = Path.InvalidPathChars;
762 char[] invalid = Path.InvalidPathChars;
763 char original = invalid[0];
767 Assert ("expected", expected[0] == 'a');
768 AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]);
771 invalid[0] = original;
776 public void GetInvalidFileNameChars_Values ()
778 char[] invalid = Path.GetInvalidFileNameChars ();
780 AssertEquals (41, invalid.Length);
781 foreach (char c in invalid) {
785 if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
787 // ':', '*', '?', '\', '/'
788 if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
790 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
793 foreach (char c in invalid) {
796 if ((i == 0) || (i == 47))
798 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
804 public void GetInvalidFileNameChars_Modify ()
806 char[] expected = Path.GetInvalidFileNameChars ();
807 char[] invalid = Path.GetInvalidFileNameChars ();
809 Assert ("expected", expected[0] != 'a');
810 AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]);
814 public void GetInvalidPathChars_Values ()
816 char[] invalid = Path.GetInvalidPathChars ();
818 AssertEquals (36, invalid.Length);
819 foreach (char c in invalid) {
823 if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
825 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
828 foreach (char c in invalid) {
832 Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
838 public void GetInvalidPathChars_Modify ()
840 char[] expected = Path.GetInvalidPathChars ();
841 char[] invalid = Path.GetInvalidPathChars ();
843 Assert ("expected", expected[0] != 'a');
844 AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]);
848 public void GetRandomFileName ()
850 string s = Path.GetRandomFileName ();
851 AssertEquals ("Length", 12, s.Length);
852 char[] invalid = Path.GetInvalidFileNameChars ();
853 for (int i=0; i < s.Length; i++) {
855 AssertEquals ("8", '.', s[i]);
857 Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1);