PathTest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. // Atsushi Enomoto ([email protected])
  10. // Sebastien Pouliot <[email protected]>
  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. using NUnit.Framework;
  19. using System.IO;
  20. using System;
  21. using System.Text;
  22. namespace MonoTests.System.IO
  23. {
  24. enum OsType {
  25. Windows,
  26. Unix,
  27. Mac
  28. }
  29. [TestFixture]
  30. public class PathTest : Assertion
  31. {
  32. static string path1;
  33. static string path2;
  34. static string path3;
  35. static OsType OS;
  36. static char DSC = Path.DirectorySeparatorChar;
  37. [SetUp]
  38. public void SetUp ()
  39. {
  40. if ('/' == DSC) {
  41. OS = OsType.Unix;
  42. path1 = "/foo/test.txt";
  43. path2 = "/etc";
  44. path3 = "init.d";
  45. } else if ('\\' == DSC) {
  46. OS = OsType.Windows;
  47. path1 = "c:\\foo\\test.txt";
  48. path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
  49. path3 = "system32";
  50. } else {
  51. OS = OsType.Mac;
  52. //FIXME: For Mac. figure this out when we need it
  53. path1 = "foo:test.txt";
  54. path2 = "foo";
  55. path3 = "bar";
  56. }
  57. }
  58. bool Windows
  59. {
  60. get {
  61. return OS == OsType.Windows;
  62. }
  63. }
  64. bool Unix
  65. {
  66. get {
  67. return OS == OsType.Unix;
  68. }
  69. }
  70. bool Mac
  71. {
  72. get {
  73. return OS == OsType.Mac;
  74. }
  75. }
  76. public void TestChangeExtension ()
  77. {
  78. string [] files = new string [3];
  79. files [(int) OsType.Unix] = "/foo/test.doc";
  80. files [(int) OsType.Windows] = "c:\\foo\\test.doc";
  81. files [(int) OsType.Mac] = "foo:test.doc";
  82. string testPath = Path.ChangeExtension (path1, "doc");
  83. AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
  84. testPath = Path.ChangeExtension ("", ".extension");
  85. AssertEquals ("ChangeExtension #02", String.Empty, testPath);
  86. testPath = Path.ChangeExtension (null, ".extension");
  87. AssertEquals ("ChangeExtension #03", null, testPath);
  88. testPath = Path.ChangeExtension ("path", null);
  89. AssertEquals ("ChangeExtension #04", "path", testPath);
  90. testPath = Path.ChangeExtension ("path.ext", "doc");
  91. AssertEquals ("ChangeExtension #05", "path.doc", testPath);
  92. testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
  93. AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
  94. testPath = Path.ChangeExtension ("hogehoge.xml", ".xsl");
  95. AssertEquals ("ChangeExtension #07", "hogehoge.xsl", testPath);
  96. testPath = Path.ChangeExtension ("hogehoge", ".xsl");
  97. AssertEquals ("ChangeExtension #08", "hogehoge.xsl", testPath);
  98. testPath = Path.ChangeExtension ("hogehoge.xml", "xsl");
  99. AssertEquals ("ChangeExtension #09", "hogehoge.xsl", testPath);
  100. testPath = Path.ChangeExtension ("hogehoge", "xsl");
  101. AssertEquals ("ChangeExtension #10", "hogehoge.xsl", testPath);
  102. testPath = Path.ChangeExtension ("hogehoge.xml", String.Empty);
  103. AssertEquals ("ChangeExtension #11", "hogehoge.", testPath);
  104. testPath = Path.ChangeExtension ("hogehoge", String.Empty);
  105. AssertEquals ("ChangeExtension #12", "hogehoge.", testPath);
  106. testPath = Path.ChangeExtension ("hogehoge.", null);
  107. AssertEquals ("ChangeExtension #13", "hogehoge", testPath);
  108. testPath = Path.ChangeExtension ("hogehoge", null);
  109. AssertEquals ("ChangeExtension #14", "hogehoge", testPath);
  110. testPath = Path.ChangeExtension (String.Empty, null);
  111. AssertEquals ("ChangeExtension #15", String.Empty, testPath);
  112. testPath = Path.ChangeExtension (String.Empty, "bashrc");
  113. AssertEquals ("ChangeExtension #16", String.Empty, testPath);
  114. testPath = Path.ChangeExtension (String.Empty, ".bashrc");
  115. AssertEquals ("ChangeExtension #17", String.Empty, testPath);
  116. testPath = Path.ChangeExtension (null, null);
  117. AssertNull ("ChangeExtension #18", testPath);
  118. }
  119. [Test]
  120. [ExpectedException (typeof (ArgumentException))]
  121. public void ChangeExtension_BadPath ()
  122. {
  123. if (!Windows) throw new ArgumentException ("Test Only On Windows");
  124. Path.ChangeExtension ("<", ".extension");
  125. }
  126. [Test]
  127. public void ChangeExtension_BadExtension ()
  128. {
  129. if (Windows) {
  130. string fn = Path.ChangeExtension ("file.ext", "<");
  131. AssertEquals ("Invalid filename", "file.<", fn);
  132. }
  133. }
  134. public void TestCombine ()
  135. {
  136. string [] files = new string [3];
  137. files [(int) OsType.Unix] = "/etc/init.d";
  138. files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
  139. files [(int) OsType.Mac] = "foo:bar";
  140. string testPath = Path.Combine (path2, path3);
  141. AssertEquals ("Combine #01", files [(int) OS], testPath);
  142. testPath = Path.Combine ("one", "");
  143. AssertEquals ("Combine #02", "one", testPath);
  144. testPath = Path.Combine ("", "one");
  145. AssertEquals ("Combine #03", "one", testPath);
  146. string current = Directory.GetCurrentDirectory ();
  147. testPath = Path.Combine (current, "one");
  148. string expected = current + DSC + "one";
  149. AssertEquals ("Combine #04", expected, testPath);
  150. testPath = Path.Combine ("one", current);
  151. // LAMESPEC noted in Path.cs
  152. AssertEquals ("Combine #05", current, testPath);
  153. testPath = Path.Combine (current, expected);
  154. AssertEquals ("Combine #06", expected, testPath);
  155. testPath = DSC + "one";
  156. testPath = Path.Combine (testPath, "two" + DSC);
  157. expected = DSC + "one" + DSC + "two" + DSC;
  158. AssertEquals ("Combine #06", expected, testPath);
  159. testPath = "one" + DSC;
  160. testPath = Path.Combine (testPath, DSC + "two");
  161. expected = DSC + "two";
  162. AssertEquals ("Combine #06", expected, testPath);
  163. testPath = "one" + DSC;
  164. testPath = Path.Combine (testPath, "two" + DSC);
  165. expected = "one" + DSC + "two" + DSC;
  166. AssertEquals ("Combine #07", expected, testPath);
  167. //TODO: Tests for UNC names
  168. try {
  169. testPath = Path.Combine ("one", null);
  170. Fail ("Combine Fail #01");
  171. } catch (Exception e) {
  172. AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
  173. }
  174. try {
  175. testPath = Path.Combine (null, "one");
  176. Fail ("Combine Fail #02");
  177. } catch (Exception e) {
  178. AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
  179. }
  180. if (Windows) {
  181. try {
  182. testPath = Path.Combine ("a>", "one");
  183. Fail ("Combine Fail #03");
  184. } catch (Exception e) {
  185. AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
  186. }
  187. try {
  188. testPath = Path.Combine ("one", "aaa<");
  189. Fail ("Combine Fail #04");
  190. } catch (Exception e) {
  191. AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
  192. }
  193. }
  194. }
  195. [Test]
  196. [ExpectedException (typeof(ArgumentException))]
  197. public void EmptyDirectoryName ()
  198. {
  199. string testDirName = Path.GetDirectoryName ("");
  200. }
  201. public void TestDirectoryName ()
  202. {
  203. string [] files = new string [3];
  204. files [(int) OsType.Unix] = "/foo";
  205. files [(int) OsType.Windows] = "c:\\foo";
  206. files [(int) OsType.Mac] = "foo";
  207. AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
  208. string testDirName = Path.GetDirectoryName (path1);
  209. AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
  210. testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
  211. AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
  212. if (Windows) {
  213. try {
  214. testDirName = Path.GetDirectoryName ("aaa>");
  215. Fail ("GetDirectoryName Fail #02");
  216. } catch (Exception e) {
  217. AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
  218. }
  219. }
  220. try {
  221. testDirName = Path.GetDirectoryName (" ");
  222. Fail ("GetDirectoryName Fail #03");
  223. } catch (Exception e) {
  224. AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
  225. }
  226. if (Windows) {
  227. AssertEquals ("GetDirectoryName #04", null, Path.GetDirectoryName ("C:"));
  228. AssertEquals ("GetDirectoryName #05", null, Path.GetDirectoryName (@"C:\"));
  229. AssertEquals ("GetDirectoryName #06", @"C:\", Path.GetDirectoryName (@"C:\dir"));
  230. AssertEquals ("GetDirectoryName #07", @"C:\dir", Path.GetDirectoryName (@"C:\dir\"));
  231. AssertEquals ("GetDirectoryName #08", @"C:\dir", Path.GetDirectoryName (@"C:\dir\dir"));
  232. AssertEquals ("GetDirectoryName #09", @"C:\dir\dir", Path.GetDirectoryName (@"C:\dir\dir\"));
  233. }
  234. }
  235. public void TestGetExtension ()
  236. {
  237. string testExtn = Path.GetExtension (path1);
  238. AssertEquals ("GetExtension #01", ".txt", testExtn);
  239. testExtn = Path.GetExtension (path2);
  240. AssertEquals ("GetExtension #02", String.Empty, testExtn);
  241. testExtn = Path.GetExtension (String.Empty);
  242. AssertEquals ("GetExtension #03", String.Empty, testExtn);
  243. testExtn = Path.GetExtension (null);
  244. AssertEquals ("GetExtension #04", null, testExtn);
  245. testExtn = Path.GetExtension (" ");
  246. AssertEquals ("GetExtension #05", String.Empty, testExtn);
  247. testExtn = Path.GetExtension (path1 + ".doc");
  248. AssertEquals ("GetExtension #06", ".doc", testExtn);
  249. testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
  250. AssertEquals ("GetExtension #07", ".txt", testExtn);
  251. testExtn = Path.GetExtension (".");
  252. AssertEquals ("GetExtension #08", String.Empty, testExtn);
  253. testExtn = Path.GetExtension ("end.");
  254. AssertEquals ("GetExtension #09", String.Empty, testExtn);
  255. testExtn = Path.GetExtension (".start");
  256. AssertEquals ("GetExtension #10", ".start", testExtn);
  257. testExtn = Path.GetExtension (".a");
  258. AssertEquals ("GetExtension #11", ".a", testExtn);
  259. testExtn = Path.GetExtension ("a.");
  260. AssertEquals ("GetExtension #12", String.Empty, testExtn);
  261. testExtn = Path.GetExtension ("a");
  262. AssertEquals ("GetExtension #13", String.Empty, testExtn);
  263. testExtn = Path.GetExtension ("makefile");
  264. AssertEquals ("GetExtension #14", String.Empty, testExtn);
  265. if (Windows) {
  266. try {
  267. testExtn = Path.GetExtension ("hi<there.txt");
  268. Fail ("GetExtension Fail #01");
  269. } catch (Exception e) {
  270. AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
  271. }
  272. }
  273. }
  274. public void TestGetFileName ()
  275. {
  276. string testFileName = Path.GetFileName (path1);
  277. AssertEquals ("GetFileName #01", "test.txt", testFileName);
  278. testFileName = Path.GetFileName (null);
  279. AssertEquals ("GetFileName #02", null, testFileName);
  280. testFileName = Path.GetFileName (String.Empty);
  281. AssertEquals ("GetFileName #03", String.Empty, testFileName);
  282. testFileName = Path.GetFileName (" ");
  283. AssertEquals ("GetFileName #04", " ", testFileName);
  284. if (Windows) {
  285. try {
  286. testFileName = Path.GetFileName ("hi<");
  287. Fail ("GetFileName Fail #01");
  288. } catch (Exception e) {
  289. AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
  290. }
  291. }
  292. }
  293. public void TestGetFileNameWithoutExtension ()
  294. {
  295. string testFileName = Path.GetFileNameWithoutExtension (path1);
  296. AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
  297. testFileName = Path.GetFileNameWithoutExtension (null);
  298. AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
  299. testFileName = Path.GetFileNameWithoutExtension (String.Empty);
  300. AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
  301. }
  302. [Ignore("This does not work under windows. See ERROR comments below.")]
  303. public void TestGetFullPath ()
  304. {
  305. string current = Directory.GetCurrentDirectory ();
  306. string testFullPath = Path.GetFullPath ("foo.txt");
  307. string expected = current + DSC + "foo.txt";
  308. AssertEquals ("GetFullPath #01", expected, testFullPath);
  309. testFullPath = Path.GetFullPath ("a//./.././foo.txt");
  310. AssertEquals ("GetFullPath #02", expected, testFullPath);
  311. string root = Windows ? "C:\\" : "/";
  312. string [,] test = new string [,] {
  313. {"root////././././././../root/././../root", "root"},
  314. {"root/", "root/"},
  315. {"root/./", "root/"},
  316. {"root/./", "root/"},
  317. {"root/../", ""},
  318. {"root/../", ""},
  319. {"root/../..", ""},
  320. {"root/.hiddenfile", "root/.hiddenfile"},
  321. {"root/. /", "root/. /"},
  322. {"root/.. /", "root/.. /"},
  323. {"root/..weirdname", "root/..weirdname"},
  324. {"root/..", ""},
  325. {"root/../a/b/../../..", ""},
  326. {"root/./..", ""},
  327. {"..", ""},
  328. {".", ""},
  329. {"root//dir", "root/dir"},
  330. {"root/. /", "root/. /"},
  331. {"root/.. /", "root/.. /"},
  332. {"root/ . /", "root/ . /"},
  333. {"root/ .. /", "root/ .. /"},
  334. {"root/./", "root/"},
  335. //ERROR! Paths are trimmed
  336. {"root/.. /", "root/.. /"},
  337. {".//", ""}
  338. };
  339. //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
  340. //... so only the first test was being done.
  341. for (int i = 0; i < test.GetUpperBound (1); i++) {
  342. AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
  343. }
  344. if (Windows) {
  345. string uncroot = @"\\server\share\";
  346. string [,] testunc = new string [,] {
  347. {"root////././././././../root/././../root", "root"},
  348. {"root/", "root/"},
  349. {"root/./", "root/"},
  350. {"root/./", "root/"},
  351. {"root/../", ""},
  352. {"root/../", ""},
  353. {"root/../..", ""},
  354. {"root/.hiddenfile", "root/.hiddenfile"},
  355. {"root/. /", "root/. /"},
  356. {"root/.. /", "root/.. /"},
  357. {"root/..weirdname", "root/..weirdname"},
  358. {"root/..", ""},
  359. {"root/../a/b/../../..", ""},
  360. {"root/./..", ""},
  361. {"..", ""},
  362. {".", ""},
  363. {"root//dir", "root/dir"},
  364. {"root/. /", "root/. /"},
  365. {"root/.. /", "root/.. /"},
  366. {"root/ . /", "root/ . /"},
  367. {"root/ .. /", "root/ .. /"},
  368. {"root/./", "root/"},
  369. {"root/.. /", "root/.. /"},
  370. {".//", ""}
  371. };
  372. for (int i = 0; i < test.GetUpperBound (1); i++) {
  373. AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
  374. }
  375. }
  376. try {
  377. testFullPath = Path.GetFullPath (null);
  378. Fail ("GetFullPath Fail #01");
  379. } catch (Exception e) {
  380. AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
  381. }
  382. try {
  383. testFullPath = Path.GetFullPath (String.Empty);
  384. Fail ("GetFullPath Fail #02");
  385. } catch (Exception e) {
  386. AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
  387. }
  388. }
  389. public void TestGetFullPath2 ()
  390. {
  391. if (Windows) {
  392. AssertEquals ("GetFullPath w#01", @"Z:\", Path.GetFullPath ("Z:"));
  393. AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path.GetFullPath (@"c:\abc\def"));
  394. Assert ("GetFullPath w#03", Path.GetFullPath (@"\").EndsWith (@"\"));
  395. // "\\\\" is not allowed
  396. Assert ("GetFullPath w#05", Path.GetFullPath ("/").EndsWith (@"\"));
  397. // "//" is not allowed
  398. Assert ("GetFullPath w#07", Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
  399. Assert ("GetFullPath w#08", Path.GetFullPath ("c").EndsWith (@"\c"));
  400. Assert ("GetFullPath w#09", Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
  401. Assert ("GetFullPath w#10", Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
  402. AssertEquals ("GetFullPath w#11", @"\\abc\def", Path.GetFullPath (@"\\abc\def"));
  403. AssertEquals ("GetFullPath w#12", Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"));
  404. AssertEquals ("GetFullPath w#13", Directory.GetCurrentDirectory ().Substring (0,2) + @"\abc\def", Path.GetFullPath ("/abc/def"));
  405. AssertEquals ("GetFullPath w#14", @"\\abc\def", Path.GetFullPath ("//abc/def"));
  406. }
  407. }
  408. public void TestGetPathRoot ()
  409. {
  410. string current;
  411. string expected;
  412. if (!Windows){
  413. current = Directory.GetCurrentDirectory ();
  414. expected = current [0].ToString ();
  415. } else {
  416. current = @"J:\Some\Strange Directory\Name";
  417. expected = "J:\\";
  418. }
  419. string pathRoot = Path.GetPathRoot (current);
  420. AssertEquals ("GetPathRoot #01", expected, pathRoot);
  421. }
  422. [Test]
  423. public void TestGetPathRoot2 ()
  424. {
  425. // note: this method doesn't call Directory.GetCurrentDirectory so it can be
  426. // reused for partial trust unit tests in PathCas.cs
  427. string pathRoot = Path.GetPathRoot ("hola");
  428. AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
  429. pathRoot = Path.GetPathRoot (null);
  430. AssertEquals ("GetPathRoot #03", null, pathRoot);
  431. if (Windows) {
  432. AssertEquals ("GetPathRoot w#01", "z:", Path.GetPathRoot ("z:"));
  433. AssertEquals ("GetPathRoot w#02", "c:\\", Path.GetPathRoot ("c:\\abc\\def"));
  434. AssertEquals ("GetPathRoot w#03", "\\", Path.GetPathRoot ("\\"));
  435. AssertEquals ("GetPathRoot w#04", "\\\\", Path.GetPathRoot ("\\\\"));
  436. AssertEquals ("GetPathRoot w#05", "\\", Path.GetPathRoot ("/"));
  437. AssertEquals ("GetPathRoot w#06", "\\\\", Path.GetPathRoot ("//"));
  438. AssertEquals ("GetPathRoot w#07", String.Empty, Path.GetPathRoot ("readme.txt"));
  439. AssertEquals ("GetPathRoot w#08", String.Empty, Path.GetPathRoot ("c"));
  440. AssertEquals ("GetPathRoot w#09", String.Empty, Path.GetPathRoot ("abc\\def"));
  441. AssertEquals ("GetPathRoot w#10", "\\", Path.GetPathRoot ("\\abc\\def"));
  442. AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"));
  443. AssertEquals ("GetPathRoot w#12", String.Empty, Path.GetPathRoot ("abc//def"));
  444. AssertEquals ("GetPathRoot w#13", "\\", Path.GetPathRoot ("/abc/def"));
  445. AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path.GetPathRoot ("//abc/def"));
  446. AssertEquals ("GetPathRoot w#15", @"C:\", Path.GetPathRoot (@"C:\"));
  447. AssertEquals ("GetPathRoot w#16", @"C:\", Path.GetPathRoot (@"C:\\"));
  448. } else {
  449. // TODO: Same tests for Unix.
  450. }
  451. }
  452. public void TestGetTempPath ()
  453. {
  454. string getTempPath = Path.GetTempPath ();
  455. Assert ("GetTempPath #01", getTempPath != String.Empty);
  456. Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
  457. AssertEquals ("GetTempPath #03", Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1]);
  458. }
  459. public void TestGetTempFileName ()
  460. {
  461. string getTempFileName = null;
  462. try {
  463. getTempFileName = Path.GetTempFileName ();
  464. Assert ("GetTempFileName #01", getTempFileName != String.Empty);
  465. Assert ("GetTempFileName #02", File.Exists (getTempFileName));
  466. } finally {
  467. if (getTempFileName != null && getTempFileName != String.Empty){
  468. File.Delete (getTempFileName);
  469. }
  470. }
  471. }
  472. public void TestHasExtension ()
  473. {
  474. AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
  475. AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
  476. AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
  477. AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
  478. AssertEquals ("HasExtension #05", false, Path.HasExtension (null));
  479. AssertEquals ("HasExtension #06", false, Path.HasExtension (String.Empty));
  480. AssertEquals ("HasExtension #07", false, Path.HasExtension (" "));
  481. AssertEquals ("HasExtension #08", false, Path.HasExtension ("."));
  482. AssertEquals ("HasExtension #09", false, Path.HasExtension ("end."));
  483. AssertEquals ("HasExtension #10", true, Path.HasExtension (".start"));
  484. AssertEquals ("HasExtension #11", true, Path.HasExtension (".a"));
  485. AssertEquals ("HasExtension #12", false, Path.HasExtension ("a."));
  486. AssertEquals ("HasExtension #13", false, Path.HasExtension ("Makefile"));
  487. }
  488. public void TestRooted ()
  489. {
  490. Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
  491. Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
  492. Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
  493. Assert ("IsPathRooted #04", !Path.IsPathRooted (String.Empty));
  494. Assert ("IsPathRooted #05", !Path.IsPathRooted (" "));
  495. Assert ("IsPathRooted #06", Path.IsPathRooted ("/"));
  496. if (Windows)
  497. Assert ("IsPathRooted #07", Path.IsPathRooted ("\\"));
  498. else
  499. Assert ("IsPathRooted #07", !Path.IsPathRooted ("\\"));
  500. Assert ("IsPathRooted #08", Path.IsPathRooted ("//"));
  501. if (Windows)
  502. Assert ("IsPathRooted #09", Path.IsPathRooted ("\\\\"));
  503. else
  504. Assert ("IsPathRooted #09", !Path.IsPathRooted ("\\\\"));
  505. Assert ("IsPathRooted #10", !Path.IsPathRooted (":"));
  506. if (Windows)
  507. Assert ("IsPathRooted #11", Path.IsPathRooted ("z:"));
  508. else
  509. Assert ("IsPathRooted #11", !Path.IsPathRooted ("z:"));
  510. if (Windows) {
  511. Assert ("IsPathRooted #12", Path.IsPathRooted ("z:\\"));
  512. Assert ("IsPathRooted #13", Path.IsPathRooted ("z:\\topdir"));
  513. // This looks MS BUG. It is treated as absolute path
  514. Assert ("IsPathRooted #14", Path.IsPathRooted ("z:curdir"));
  515. Assert ("IsPathRooted #15", Path.IsPathRooted ("\\abc\\def"));
  516. }
  517. }
  518. public void TestCanonicalizeDots ()
  519. {
  520. string current = Path.GetFullPath (".");
  521. Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
  522. string parent = Path.GetFullPath ("..");
  523. Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
  524. }
  525. public void TestDirectoryNameBugs ()
  526. {
  527. if (Windows) {
  528. AssertEquals ("Win #01", "C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"));
  529. } else {
  530. AssertEquals ("No win #01", "/etc", Path.GetDirectoryName ("/etc/hostname"));
  531. }
  532. }
  533. public void TestGetFullPathUnix ()
  534. {
  535. if (Windows)
  536. return;
  537. AssertEquals ("#01", "/", Path.GetFullPath ("/"));
  538. AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
  539. AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
  540. AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
  541. Path.GetFullPath ("hey"));
  542. }
  543. [Test]
  544. public void GetFullPath_EndingSeparator ()
  545. {
  546. string fp = Path.GetFullPath ("something/");
  547. char end = fp[fp.Length - 1];
  548. Assert (end == Path.DirectorySeparatorChar);
  549. }
  550. [Test]
  551. public void WindowsSystem32_76191 ()
  552. {
  553. // check for Unix platforms - see FAQ for more details
  554. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  555. int platform = (int) Environment.OSVersion.Platform;
  556. if ((platform == 4) || (platform == 128))
  557. return;
  558. string curdir = Directory.GetCurrentDirectory ();
  559. try {
  560. string system = Environment.SystemDirectory;
  561. Directory.SetCurrentDirectory (system);
  562. string drive = system.Substring (0, 2);
  563. AssertEquals ("current dir", system, Path.GetFullPath (drive));
  564. }
  565. finally {
  566. Directory.SetCurrentDirectory (curdir);
  567. }
  568. }
  569. [Test]
  570. public void WindowsSystem32_77007 ()
  571. {
  572. // check for Unix platforms - see FAQ for more details
  573. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  574. int platform = (int) Environment.OSVersion.Platform;
  575. if ((platform == 4) || (platform == 128))
  576. return;
  577. string curdir = Directory.GetCurrentDirectory ();
  578. try {
  579. string system = Environment.SystemDirectory;
  580. Directory.SetCurrentDirectory (system);
  581. // e.g. C:dir (no backslash) will return CurrentDirectory + dir
  582. string dir = system.Substring (0, 2) + "dir";
  583. AssertEquals ("current dir", Path.Combine (system, "dir"), Path.GetFullPath (dir));
  584. }
  585. finally {
  586. Directory.SetCurrentDirectory (curdir);
  587. }
  588. }
  589. [Test]
  590. public void WindowsDriveC14N_77058 ()
  591. {
  592. // check for Unix platforms - see FAQ for more details
  593. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  594. int platform = (int) Environment.OSVersion.Platform;
  595. if ((platform == 4) || (platform == 128))
  596. return;
  597. AssertEquals ("1", @"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"));
  598. AssertEquals ("2", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"));
  599. AssertEquals ("3", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"));
  600. AssertEquals ("4", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"));
  601. AssertEquals ("5", @"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"));
  602. }
  603. [Test]
  604. public void InvalidPathChars_Values ()
  605. {
  606. char[] invalid = Path.InvalidPathChars;
  607. if (Windows) {
  608. #if NET_2_0
  609. AssertEquals ("Length", 36, invalid.Length);
  610. #else
  611. AssertEquals ("Length", 15, invalid.Length);
  612. #endif
  613. foreach (char c in invalid) {
  614. int i = (int) c;
  615. #if NET_2_0
  616. if (i < 32)
  617. continue;
  618. #else
  619. if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
  620. continue;
  621. #endif
  622. // in both 1.1 SP1 and 2.0
  623. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  624. continue;
  625. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  626. }
  627. } else {
  628. foreach (char c in invalid) {
  629. int i = (int) c;
  630. if (i == 0)
  631. continue;
  632. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  633. }
  634. }
  635. }
  636. [Test]
  637. public void InvalidPathChars_Modify ()
  638. {
  639. char[] expected = Path.InvalidPathChars;
  640. char[] invalid = Path.InvalidPathChars;
  641. char original = invalid[0];
  642. try {
  643. invalid[0] = 'a';
  644. // kind of scary
  645. Assert ("expected", expected[0] == 'a');
  646. AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]);
  647. }
  648. finally {
  649. invalid[0] = original;
  650. }
  651. }
  652. #if NET_2_0
  653. [Test]
  654. public void GetInvalidFileNameChars_Values ()
  655. {
  656. char[] invalid = Path.GetInvalidFileNameChars ();
  657. if (Windows) {
  658. AssertEquals (41, invalid.Length);
  659. foreach (char c in invalid) {
  660. int i = (int) c;
  661. if (i < 32)
  662. continue;
  663. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  664. continue;
  665. // ':', '*', '?', '\', '/'
  666. if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
  667. continue;
  668. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  669. }
  670. } else {
  671. foreach (char c in invalid) {
  672. int i = (int) c;
  673. // null or '/'
  674. if ((i == 0) || (i == 47))
  675. continue;
  676. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  677. }
  678. }
  679. }
  680. [Test]
  681. public void GetInvalidFileNameChars_Modify ()
  682. {
  683. char[] expected = Path.GetInvalidFileNameChars ();
  684. char[] invalid = Path.GetInvalidFileNameChars ();
  685. invalid[0] = 'a';
  686. Assert ("expected", expected[0] != 'a');
  687. AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]);
  688. }
  689. [Test]
  690. public void GetInvalidPathChars_Values ()
  691. {
  692. char[] invalid = Path.GetInvalidPathChars ();
  693. if (Windows) {
  694. AssertEquals (36, invalid.Length);
  695. foreach (char c in invalid) {
  696. int i = (int) c;
  697. if (i < 32)
  698. continue;
  699. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  700. continue;
  701. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  702. }
  703. } else {
  704. foreach (char c in invalid) {
  705. int i = (int) c;
  706. if (i == 0)
  707. continue;
  708. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  709. }
  710. }
  711. }
  712. [Test]
  713. public void GetInvalidPathChars_Modify ()
  714. {
  715. char[] expected = Path.GetInvalidPathChars ();
  716. char[] invalid = Path.GetInvalidPathChars ();
  717. invalid[0] = 'a';
  718. Assert ("expected", expected[0] != 'a');
  719. AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]);
  720. }
  721. [Test]
  722. public void GetRandomFileName ()
  723. {
  724. string s = Path.GetRandomFileName ();
  725. AssertEquals ("Length", 12, s.Length);
  726. char[] invalid = Path.GetInvalidFileNameChars ();
  727. for (int i=0; i < s.Length; i++) {
  728. if (i == 8)
  729. AssertEquals ("8", '.', s[i]);
  730. else
  731. Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1);
  732. }
  733. }
  734. #endif
  735. }
  736. }