// // System.IO.Path Test Cases // // Authors: // Marcin Szczepanski (marcins@zipworld.com.au) // Gonzalo Paniagua Javier (gonzalo@ximian.com) // Ben Maurer (bmaurer@users.sf.net) // Gilles Freart (gfr@skynet.be) // Atsushi Enomoto (atsushi@ximian.com) // Sebastien Pouliot // // (c) Marcin Szczepanski // (c) 2002 Ximian, Inc. (http://www.ximian.com) // (c) 2003 Ben Maurer // (c) 2003 Gilles Freart // Copyright (C) 2005 Novell, Inc (http://www.novell.com) // using NUnit.Framework; using System.IO; using System; using System.Text; namespace MonoTests.System.IO { enum OsType { Windows, Unix, Mac } [TestFixture] public class PathTest : Assertion { static string path1; static string path2; static string path3; static OsType OS; static char DSC = Path.DirectorySeparatorChar; [SetUp] public void SetUp () { if ('/' == DSC) { OS = OsType.Unix; path1 = "/foo/test.txt"; path2 = "/etc"; path3 = "init.d"; } else if ('\\' == DSC) { OS = OsType.Windows; path1 = "c:\\foo\\test.txt"; path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT"); path3 = "system32"; } else { OS = OsType.Mac; //FIXME: For Mac. figure this out when we need it path1 = "foo:test.txt"; path2 = "foo"; path3 = "bar"; } } bool Windows { get { return OS == OsType.Windows; } } bool Unix { get { return OS == OsType.Unix; } } bool Mac { get { return OS == OsType.Mac; } } public void TestChangeExtension () { string [] files = new string [3]; files [(int) OsType.Unix] = "/foo/test.doc"; files [(int) OsType.Windows] = "c:\\foo\\test.doc"; files [(int) OsType.Mac] = "foo:test.doc"; string testPath = Path.ChangeExtension (path1, "doc"); AssertEquals ("ChangeExtension #01", files [(int) OS], testPath); testPath = Path.ChangeExtension ("", ".extension"); AssertEquals ("ChangeExtension #02", String.Empty, testPath); testPath = Path.ChangeExtension (null, ".extension"); AssertEquals ("ChangeExtension #03", null, testPath); testPath = Path.ChangeExtension ("path", null); AssertEquals ("ChangeExtension #04", "path", testPath); testPath = Path.ChangeExtension ("path.ext", "doc"); AssertEquals ("ChangeExtension #05", "path.doc", testPath); testPath = Path.ChangeExtension ("path.ext1.ext2", "doc"); AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath); testPath = Path.ChangeExtension ("hogehoge.xml", ".xsl"); AssertEquals ("ChangeExtension #07", "hogehoge.xsl", testPath); testPath = Path.ChangeExtension ("hogehoge", ".xsl"); AssertEquals ("ChangeExtension #08", "hogehoge.xsl", testPath); testPath = Path.ChangeExtension ("hogehoge.xml", "xsl"); AssertEquals ("ChangeExtension #09", "hogehoge.xsl", testPath); testPath = Path.ChangeExtension ("hogehoge", "xsl"); AssertEquals ("ChangeExtension #10", "hogehoge.xsl", testPath); testPath = Path.ChangeExtension ("hogehoge.xml", String.Empty); AssertEquals ("ChangeExtension #11", "hogehoge.", testPath); testPath = Path.ChangeExtension ("hogehoge", String.Empty); AssertEquals ("ChangeExtension #12", "hogehoge.", testPath); testPath = Path.ChangeExtension ("hogehoge.", null); AssertEquals ("ChangeExtension #13", "hogehoge", testPath); testPath = Path.ChangeExtension ("hogehoge", null); AssertEquals ("ChangeExtension #14", "hogehoge", testPath); testPath = Path.ChangeExtension (String.Empty, null); AssertEquals ("ChangeExtension #15", String.Empty, testPath); testPath = Path.ChangeExtension (String.Empty, "bashrc"); AssertEquals ("ChangeExtension #16", String.Empty, testPath); testPath = Path.ChangeExtension (String.Empty, ".bashrc"); AssertEquals ("ChangeExtension #17", String.Empty, testPath); testPath = Path.ChangeExtension (null, null); AssertNull ("ChangeExtension #18", testPath); } [Test] [ExpectedException (typeof (ArgumentException))] public void ChangeExtension_BadPath () { if (!Windows) throw new ArgumentException ("Test Only On Windows"); Path.ChangeExtension ("<", ".extension"); } [Test] public void ChangeExtension_BadExtension () { if (Windows) { string fn = Path.ChangeExtension ("file.ext", "<"); AssertEquals ("Invalid filename", "file.<", fn); } } public void TestCombine () { string [] files = new string [3]; files [(int) OsType.Unix] = "/etc/init.d"; files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32"; files [(int) OsType.Mac] = "foo:bar"; string testPath = Path.Combine (path2, path3); AssertEquals ("Combine #01", files [(int) OS], testPath); testPath = Path.Combine ("one", ""); AssertEquals ("Combine #02", "one", testPath); testPath = Path.Combine ("", "one"); AssertEquals ("Combine #03", "one", testPath); string current = Directory.GetCurrentDirectory (); testPath = Path.Combine (current, "one"); string expected = current + DSC + "one"; AssertEquals ("Combine #04", expected, testPath); testPath = Path.Combine ("one", current); // LAMESPEC noted in Path.cs AssertEquals ("Combine #05", current, testPath); testPath = Path.Combine (current, expected); AssertEquals ("Combine #06", expected, testPath); testPath = DSC + "one"; testPath = Path.Combine (testPath, "two" + DSC); expected = DSC + "one" + DSC + "two" + DSC; AssertEquals ("Combine #06", expected, testPath); testPath = "one" + DSC; testPath = Path.Combine (testPath, DSC + "two"); expected = DSC + "two"; AssertEquals ("Combine #06", expected, testPath); testPath = "one" + DSC; testPath = Path.Combine (testPath, "two" + DSC); expected = "one" + DSC + "two" + DSC; AssertEquals ("Combine #07", expected, testPath); //TODO: Tests for UNC names try { testPath = Path.Combine ("one", null); Fail ("Combine Fail #01"); } catch (Exception e) { AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ()); } try { testPath = Path.Combine (null, "one"); Fail ("Combine Fail #02"); } catch (Exception e) { AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ()); } if (Windows) { try { testPath = Path.Combine ("a>", "one"); Fail ("Combine Fail #03"); } catch (Exception e) { AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ()); } try { testPath = Path.Combine ("one", "aaa<"); Fail ("Combine Fail #04"); } catch (Exception e) { AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ()); } } } [Test] [ExpectedException (typeof(ArgumentException))] public void EmptyDirectoryName () { string testDirName = Path.GetDirectoryName (""); } public void TestDirectoryName () { string [] files = new string [3]; files [(int) OsType.Unix] = "/foo"; files [(int) OsType.Windows] = "c:\\foo"; files [(int) OsType.Mac] = "foo"; AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null)); string testDirName = Path.GetDirectoryName (path1); AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName); testDirName = Path.GetDirectoryName (files [(int) OS] + DSC); AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName); if (Windows) { try { testDirName = Path.GetDirectoryName ("aaa>"); Fail ("GetDirectoryName Fail #02"); } catch (Exception e) { AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ()); } } try { testDirName = Path.GetDirectoryName (" "); Fail ("GetDirectoryName Fail #03"); } catch (Exception e) { AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ()); } if (Windows) { AssertEquals ("GetDirectoryName #04", null, Path.GetDirectoryName ("C:")); AssertEquals ("GetDirectoryName #05", null, Path.GetDirectoryName (@"C:\")); AssertEquals ("GetDirectoryName #06", @"C:\", Path.GetDirectoryName (@"C:\dir")); AssertEquals ("GetDirectoryName #07", @"C:\dir", Path.GetDirectoryName (@"C:\dir\")); AssertEquals ("GetDirectoryName #08", @"C:\dir", Path.GetDirectoryName (@"C:\dir\dir")); AssertEquals ("GetDirectoryName #09", @"C:\dir\dir", Path.GetDirectoryName (@"C:\dir\dir\")); // UNC tests AssertEquals ("GetDirectoryName #10", null, Path.GetDirectoryName (@"\\")); AssertEquals ("GetDirectoryName #11", null, Path.GetDirectoryName (@"\\server")); AssertEquals ("GetDirectoryName #12", null, Path.GetDirectoryName (@"\\server\share")); AssertEquals ("GetDirectoryName #13", @"\\server\share", Path.GetDirectoryName (@"\\server\share\")); AssertEquals ("GetDirectoryName #14", @"\\server\share", Path.GetDirectoryName (@"\\server\share\dir")); AssertEquals ("GetDirectoryName #15", @"\\server\share\dir", Path.GetDirectoryName (@"\\server\share\dir\subdir")); } } public void TestGetExtension () { string testExtn = Path.GetExtension (path1); AssertEquals ("GetExtension #01", ".txt", testExtn); testExtn = Path.GetExtension (path2); AssertEquals ("GetExtension #02", String.Empty, testExtn); testExtn = Path.GetExtension (String.Empty); AssertEquals ("GetExtension #03", String.Empty, testExtn); testExtn = Path.GetExtension (null); AssertEquals ("GetExtension #04", null, testExtn); testExtn = Path.GetExtension (" "); AssertEquals ("GetExtension #05", String.Empty, testExtn); testExtn = Path.GetExtension (path1 + ".doc"); AssertEquals ("GetExtension #06", ".doc", testExtn); testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt"); AssertEquals ("GetExtension #07", ".txt", testExtn); testExtn = Path.GetExtension ("."); AssertEquals ("GetExtension #08", String.Empty, testExtn); testExtn = Path.GetExtension ("end."); AssertEquals ("GetExtension #09", String.Empty, testExtn); testExtn = Path.GetExtension (".start"); AssertEquals ("GetExtension #10", ".start", testExtn); testExtn = Path.GetExtension (".a"); AssertEquals ("GetExtension #11", ".a", testExtn); testExtn = Path.GetExtension ("a."); AssertEquals ("GetExtension #12", String.Empty, testExtn); testExtn = Path.GetExtension ("a"); AssertEquals ("GetExtension #13", String.Empty, testExtn); testExtn = Path.GetExtension ("makefile"); AssertEquals ("GetExtension #14", String.Empty, testExtn); if (Windows) { try { testExtn = Path.GetExtension ("hi 15) && (i < 19)) || ((i > 19) && (i < 26))) continue; #endif // in both 1.1 SP1 and 2.0 if ((i == 34) || (i == 60) || (i == 62) || (i == 124)) continue; Fail (String.Format ("'{0}' (#{1}) is invalid", c, i)); } } else { foreach (char c in invalid) { int i = (int) c; if (i == 0) continue; Fail (String.Format ("'{0}' (#{1}) is invalid", c, i)); } } } [Test] public void InvalidPathChars_Modify () { char[] expected = Path.InvalidPathChars; char[] invalid = Path.InvalidPathChars; char original = invalid[0]; try { invalid[0] = 'a'; // kind of scary Assert ("expected", expected[0] == 'a'); AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]); } finally { invalid[0] = original; } } #if NET_2_0 [Test] public void GetInvalidFileNameChars_Values () { char[] invalid = Path.GetInvalidFileNameChars (); if (Windows) { AssertEquals (41, invalid.Length); foreach (char c in invalid) { int i = (int) c; if (i < 32) continue; if ((i == 34) || (i == 60) || (i == 62) || (i == 124)) continue; // ':', '*', '?', '\', '/' if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47)) continue; Fail (String.Format ("'{0}' (#{1}) is invalid", c, i)); } } else { foreach (char c in invalid) { int i = (int) c; // null or '/' if ((i == 0) || (i == 47)) continue; Fail (String.Format ("'{0}' (#{1}) is invalid", c, i)); } } } [Test] public void GetInvalidFileNameChars_Modify () { char[] expected = Path.GetInvalidFileNameChars (); char[] invalid = Path.GetInvalidFileNameChars (); invalid[0] = 'a'; Assert ("expected", expected[0] != 'a'); AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]); } [Test] public void GetInvalidPathChars_Values () { char[] invalid = Path.GetInvalidPathChars (); if (Windows) { AssertEquals (36, invalid.Length); foreach (char c in invalid) { int i = (int) c; if (i < 32) continue; if ((i == 34) || (i == 60) || (i == 62) || (i == 124)) continue; Fail (String.Format ("'{0}' (#{1}) is invalid", c, i)); } } else { foreach (char c in invalid) { int i = (int) c; if (i == 0) continue; Fail (String.Format ("'{0}' (#{1}) is invalid", c, i)); } } } [Test] public void GetInvalidPathChars_Modify () { char[] expected = Path.GetInvalidPathChars (); char[] invalid = Path.GetInvalidPathChars (); invalid[0] = 'a'; Assert ("expected", expected[0] != 'a'); AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]); } [Test] public void GetRandomFileName () { string s = Path.GetRandomFileName (); AssertEquals ("Length", 12, s.Length); char[] invalid = Path.GetInvalidFileNameChars (); for (int i=0; i < s.Length; i++) { if (i == 8) AssertEquals ("8", '.', s[i]); else Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1); } } #endif } }