PathTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //
  2. // System.IO.Path Test Cases
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Ben Maurer ([email protected])
  8. // Gilles Freart ([email protected])
  9. //
  10. // (c) Marcin Szczepanski
  11. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  12. // (c) 2003 Ben Maurer
  13. // (c) 2003 Gilles Freart
  14. //
  15. #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
  16. #if NUNIT
  17. using NUnit.Framework;
  18. #else
  19. using System.Reflection;
  20. #endif
  21. using System.IO;
  22. using System;
  23. using System.Text;
  24. namespace MonoTests.System.IO
  25. {
  26. enum OsType {
  27. Windows,
  28. Unix,
  29. Mac
  30. }
  31. #if NUNIT
  32. public class PathTest : TestCase
  33. {
  34. #else
  35. public class PathTest
  36. {
  37. #endif
  38. static string path1;
  39. static string path2;
  40. static string path3;
  41. static OsType OS;
  42. static char DSC = Path.DirectorySeparatorChar;
  43. #if NUNIT
  44. protected override void SetUp ()
  45. {
  46. #else
  47. static PathTest ()
  48. {
  49. #endif
  50. if ('/' == DSC) {
  51. OS = OsType.Unix;
  52. path1 = "/foo/test.txt";
  53. path2 = "/etc";
  54. path3 = "init.d";
  55. } else if ('\\' == DSC) {
  56. OS = OsType.Windows;
  57. path1 = "c:\\foo\\test.txt";
  58. path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
  59. path3 = "system32";
  60. } else {
  61. OS = OsType.Mac;
  62. //FIXME: For Mac. figure this out when we need it
  63. path1 = "foo:test.txt";
  64. path2 = "foo";
  65. path3 = "bar";
  66. }
  67. }
  68. bool Windows
  69. {
  70. get {
  71. return OS == OsType.Windows;
  72. }
  73. }
  74. bool Unix
  75. {
  76. get {
  77. return OS == OsType.Unix;
  78. }
  79. }
  80. bool Mac
  81. {
  82. get {
  83. return OS == OsType.Mac;
  84. }
  85. }
  86. public void TestChangeExtension ()
  87. {
  88. string [] files = new string [3];
  89. files [(int) OsType.Unix] = "/foo/test.doc";
  90. files [(int) OsType.Windows] = "c:\\foo\\test.doc";
  91. files [(int) OsType.Mac] = "foo:test.doc";
  92. string testPath = Path.ChangeExtension (path1, "doc");
  93. AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
  94. testPath = Path.ChangeExtension ("", ".extension");
  95. AssertEquals ("ChangeExtension #02", String.Empty, testPath);
  96. testPath = Path.ChangeExtension (null, ".extension");
  97. AssertEquals ("ChangeExtension #03", null, testPath);
  98. testPath = Path.ChangeExtension ("path", null);
  99. AssertEquals ("ChangeExtension #04", "path", testPath);
  100. testPath = Path.ChangeExtension ("path.ext", "doc");
  101. AssertEquals ("ChangeExtension #05", "path.doc", testPath);
  102. testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
  103. AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
  104. if (Windows) {
  105. try {
  106. testPath = Path.ChangeExtension ("<", ".extension");
  107. Fail ("ChangeException Fail #01");
  108. } catch (Exception e) {
  109. AssertEquals ("ChangeExtension Exc. #01", typeof (ArgumentException), e.GetType ());
  110. }
  111. }
  112. }
  113. public void TestCombine ()
  114. {
  115. string [] files = new string [3];
  116. files [(int) OsType.Unix] = "/etc/init.d";
  117. files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
  118. files [(int) OsType.Mac] = "foo:bar";
  119. string testPath = Path.Combine (path2, path3);
  120. AssertEquals ("Combine #01", files [(int) OS], testPath);
  121. testPath = Path.Combine ("one", "");
  122. AssertEquals ("Combine #02", "one", testPath);
  123. testPath = Path.Combine ("", "one");
  124. AssertEquals ("Combine #03", "one", testPath);
  125. string current = Directory.GetCurrentDirectory ();
  126. testPath = Path.Combine (current, "one");
  127. string expected = current + DSC + "one";
  128. AssertEquals ("Combine #04", expected, testPath);
  129. testPath = Path.Combine ("one", current);
  130. // LAMESPEC noted in Path.cs
  131. AssertEquals ("Combine #05", current, testPath);
  132. testPath = Path.Combine (current, expected);
  133. AssertEquals ("Combine #06", expected, testPath);
  134. testPath = DSC + "one";
  135. testPath = Path.Combine (testPath, "two" + DSC);
  136. expected = DSC + "one" + DSC + "two" + DSC;
  137. AssertEquals ("Combine #06", expected, testPath);
  138. testPath = "one" + DSC;
  139. testPath = Path.Combine (testPath, DSC + "two");
  140. expected = DSC + "two";
  141. AssertEquals ("Combine #06", expected, testPath);
  142. testPath = "one" + DSC;
  143. testPath = Path.Combine (testPath, "two" + DSC);
  144. expected = "one" + DSC + "two" + DSC;
  145. AssertEquals ("Combine #07", expected, testPath);
  146. //TODO: Tests for UNC names
  147. try {
  148. testPath = Path.Combine ("one", null);
  149. Fail ("Combine Fail #01");
  150. } catch (Exception e) {
  151. AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
  152. }
  153. try {
  154. testPath = Path.Combine (null, "one");
  155. Fail ("Combine Fail #02");
  156. } catch (Exception e) {
  157. AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
  158. }
  159. if (Windows) {
  160. try {
  161. testPath = Path.Combine ("a>", "one");
  162. Fail ("Combine Fail #03");
  163. } catch (Exception e) {
  164. AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
  165. }
  166. try {
  167. testPath = Path.Combine ("one", "aaa<");
  168. Fail ("Combine Fail #04");
  169. } catch (Exception e) {
  170. AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
  171. }
  172. }
  173. }
  174. [Test]
  175. [ExpectedException (typeof(ArgumentException))]
  176. public void EmptyDirectoryName ()
  177. {
  178. string testDirName = Path.GetDirectoryName ("");
  179. }
  180. public void TestDirectoryName ()
  181. {
  182. string [] files = new string [3];
  183. files [(int) OsType.Unix] = "/foo";
  184. files [(int) OsType.Windows] = "c:\\foo";
  185. files [(int) OsType.Mac] = "foo";
  186. AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
  187. string testDirName = Path.GetDirectoryName (path1);
  188. AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
  189. testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
  190. AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
  191. if (Windows) {
  192. try {
  193. testDirName = Path.GetDirectoryName ("aaa>");
  194. Fail ("GetDirectoryName Fail #02");
  195. } catch (Exception e) {
  196. AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
  197. }
  198. }
  199. try {
  200. testDirName = Path.GetDirectoryName (" ");
  201. Fail ("GetDirectoryName Fail #03");
  202. } catch (Exception e) {
  203. AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
  204. }
  205. }
  206. public void TestGetExtension ()
  207. {
  208. string testExtn = Path.GetExtension (path1);
  209. AssertEquals ("GetExtension #01", ".txt", testExtn);
  210. testExtn = Path.GetExtension (path2);
  211. AssertEquals ("GetExtension #02", String.Empty, testExtn);
  212. testExtn = Path.GetExtension (String.Empty);
  213. AssertEquals ("GetExtension #03", String.Empty, testExtn);
  214. testExtn = Path.GetExtension (null);
  215. AssertEquals ("GetExtension #04", null, testExtn);
  216. testExtn = Path.GetExtension (" ");
  217. AssertEquals ("GetExtension #05", String.Empty, testExtn);
  218. testExtn = Path.GetExtension (path1 + ".doc");
  219. AssertEquals ("GetExtension #06", ".doc", testExtn);
  220. testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
  221. AssertEquals ("GetExtension #07", ".txt", testExtn);
  222. if (Windows) {
  223. try {
  224. testExtn = Path.GetExtension ("hi<there.txt");
  225. Fail ("GetExtension Fail #01");
  226. } catch (Exception e) {
  227. AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
  228. }
  229. }
  230. }
  231. public void TestGetFileName ()
  232. {
  233. string testFileName = Path.GetFileName (path1);
  234. AssertEquals ("GetFileName #01", "test.txt", testFileName);
  235. testFileName = Path.GetFileName (null);
  236. AssertEquals ("GetFileName #02", null, testFileName);
  237. testFileName = Path.GetFileName (String.Empty);
  238. AssertEquals ("GetFileName #03", String.Empty, testFileName);
  239. testFileName = Path.GetFileName (" ");
  240. AssertEquals ("GetFileName #04", " ", testFileName);
  241. if (Windows) {
  242. try {
  243. testFileName = Path.GetFileName ("hi<");
  244. Fail ("GetFileName Fail #01");
  245. } catch (Exception e) {
  246. AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
  247. }
  248. }
  249. }
  250. public void TestGetFileNameWithoutExtension ()
  251. {
  252. string testFileName = Path.GetFileNameWithoutExtension (path1);
  253. AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
  254. testFileName = Path.GetFileNameWithoutExtension (null);
  255. AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
  256. testFileName = Path.GetFileNameWithoutExtension (String.Empty);
  257. AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
  258. }
  259. [Ignore("This does not work under windows. See ERROR comments below.")]
  260. public void TestGetFullPath ()
  261. {
  262. string current = Directory.GetCurrentDirectory ();
  263. string testFullPath = Path.GetFullPath ("foo.txt");
  264. string expected = current + DSC + "foo.txt";
  265. AssertEquals ("GetFullPath #01", expected, testFullPath);
  266. testFullPath = Path.GetFullPath ("a//./.././foo.txt");
  267. AssertEquals ("GetFullPath #02", expected, testFullPath);
  268. string root = Windows ? "C:\\" : "/";
  269. string [,] test = new string [,] {
  270. {"root////././././././../root/././../root", "root"},
  271. {"root/", "root/"},
  272. {"root/./", "root/"},
  273. {"root/./", "root/"},
  274. {"root/../", ""},
  275. {"root/../", ""},
  276. {"root/../..", ""},
  277. {"root/.hiddenfile", "root/.hiddenfile"},
  278. {"root/. /", "root/. /"},
  279. {"root/.. /", "root/.. /"},
  280. {"root/..weirdname", "root/..weirdname"},
  281. {"root/..", ""},
  282. {"root/../a/b/../../..", ""},
  283. {"root/./..", ""},
  284. {"..", ""},
  285. {".", ""},
  286. {"root//dir", "root/dir"},
  287. {"root/. /", "root/. /"},
  288. {"root/.. /", "root/.. /"},
  289. {"root/ . /", "root/ . /"},
  290. {"root/ .. /", "root/ .. /"},
  291. {"root/./", "root/"},
  292. //ERROR! Paths are trimmed
  293. {"root/.. /", "root/.. /"},
  294. {".//", ""}
  295. };
  296. //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
  297. //... so only the first test was being done.
  298. for (int i = 0; i < test.GetUpperBound (1); i++) {
  299. AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
  300. }
  301. if (Windows) {
  302. string uncroot = @"\\server\share\";
  303. string [,] testunc = new string [,] {
  304. {"root////././././././../root/././../root", "root"},
  305. {"root/", "root/"},
  306. {"root/./", "root/"},
  307. {"root/./", "root/"},
  308. {"root/../", ""},
  309. {"root/../", ""},
  310. {"root/../..", ""},
  311. {"root/.hiddenfile", "root/.hiddenfile"},
  312. {"root/. /", "root/. /"},
  313. {"root/.. /", "root/.. /"},
  314. {"root/..weirdname", "root/..weirdname"},
  315. {"root/..", ""},
  316. {"root/../a/b/../../..", ""},
  317. {"root/./..", ""},
  318. {"..", ""},
  319. {".", ""},
  320. {"root//dir", "root/dir"},
  321. {"root/. /", "root/. /"},
  322. {"root/.. /", "root/.. /"},
  323. {"root/ . /", "root/ . /"},
  324. {"root/ .. /", "root/ .. /"},
  325. {"root/./", "root/"},
  326. {"root/.. /", "root/.. /"},
  327. {".//", ""}
  328. };
  329. for (int i = 0; i < test.GetUpperBound (1); i++) {
  330. AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
  331. }
  332. }
  333. try {
  334. testFullPath = Path.GetFullPath (null);
  335. Fail ("GetFullPath Fail #01");
  336. } catch (Exception e) {
  337. AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
  338. }
  339. try {
  340. testFullPath = Path.GetFullPath (String.Empty);
  341. Fail ("GetFullPath Fail #02");
  342. } catch (Exception e) {
  343. AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
  344. }
  345. }
  346. public void TestGetPathRoot ()
  347. {
  348. string current;
  349. string expected;
  350. if (!Windows){
  351. current = Directory.GetCurrentDirectory ();
  352. expected = current [0].ToString ();
  353. } else {
  354. current = @"J:\Some\Strange Directory\Name";
  355. expected = "J:\\";
  356. }
  357. string pathRoot = Path.GetPathRoot (current);
  358. AssertEquals ("GetPathRoot #01", expected, pathRoot);
  359. pathRoot = Path.GetPathRoot ("hola");
  360. AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
  361. pathRoot = Path.GetPathRoot (null);
  362. AssertEquals ("GetPathRoot #03", null, pathRoot);
  363. }
  364. public void TestGetTempPath ()
  365. {
  366. string getTempPath = Path.GetTempPath ();
  367. Assert ("GetTempPath #01", getTempPath != String.Empty);
  368. Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
  369. }
  370. public void TestGetTempFileName ()
  371. {
  372. string getTempFileName = null;
  373. try {
  374. getTempFileName = Path.GetTempFileName ();
  375. Assert ("GetTempFileName #01", getTempFileName != String.Empty);
  376. Assert ("GetTempFileName #02", File.Exists (getTempFileName));
  377. } finally {
  378. if (getTempFileName != null && getTempFileName != String.Empty){
  379. File.Delete (getTempFileName);
  380. }
  381. }
  382. }
  383. public void TestHasExtension ()
  384. {
  385. AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
  386. AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
  387. AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
  388. AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
  389. }
  390. public void TestRooted ()
  391. {
  392. Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
  393. Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
  394. Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
  395. }
  396. public void TestCanonicalizeDots ()
  397. {
  398. string current = Path.GetFullPath (".");
  399. Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
  400. string parent = Path.GetFullPath ("..");
  401. Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
  402. }
  403. #if !NUNIT
  404. void Assert (string msg, bool result)
  405. {
  406. if (!result)
  407. Console.WriteLine (msg);
  408. }
  409. void AssertEquals (string msg, object expected, object real)
  410. {
  411. if (expected == null && real == null)
  412. return;
  413. if (expected != null && expected.Equals (real))
  414. return;
  415. Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
  416. }
  417. void Fail (string msg)
  418. {
  419. Console.WriteLine ("Failed: {0}", msg);
  420. }
  421. static void Main ()
  422. {
  423. PathTest p = new PathTest ();
  424. Type t = p.GetType ();
  425. MethodInfo [] methods = t.GetMethods ();
  426. foreach (MethodInfo m in methods) {
  427. if (m.Name.Substring (0, 4) == "Test") {
  428. m.Invoke (p, null);
  429. }
  430. }
  431. }
  432. #endif
  433. }
  434. }