PathTest.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. // UNC tests
  234. AssertEquals ("GetDirectoryName #10", null, Path.GetDirectoryName (@"\\"));
  235. AssertEquals ("GetDirectoryName #11", null, Path.GetDirectoryName (@"\\server"));
  236. AssertEquals ("GetDirectoryName #12", null, Path.GetDirectoryName (@"\\server\share"));
  237. AssertEquals ("GetDirectoryName #13", @"\\server\share", Path.GetDirectoryName (@"\\server\share\"));
  238. AssertEquals ("GetDirectoryName #14", @"\\server\share", Path.GetDirectoryName (@"\\server\share\dir"));
  239. AssertEquals ("GetDirectoryName #15", @"\\server\share\dir", Path.GetDirectoryName (@"\\server\share\dir\subdir"));
  240. }
  241. }
  242. public void TestGetExtension ()
  243. {
  244. string testExtn = Path.GetExtension (path1);
  245. AssertEquals ("GetExtension #01", ".txt", testExtn);
  246. testExtn = Path.GetExtension (path2);
  247. AssertEquals ("GetExtension #02", String.Empty, testExtn);
  248. testExtn = Path.GetExtension (String.Empty);
  249. AssertEquals ("GetExtension #03", String.Empty, testExtn);
  250. testExtn = Path.GetExtension (null);
  251. AssertEquals ("GetExtension #04", null, testExtn);
  252. testExtn = Path.GetExtension (" ");
  253. AssertEquals ("GetExtension #05", String.Empty, testExtn);
  254. testExtn = Path.GetExtension (path1 + ".doc");
  255. AssertEquals ("GetExtension #06", ".doc", testExtn);
  256. testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
  257. AssertEquals ("GetExtension #07", ".txt", testExtn);
  258. testExtn = Path.GetExtension (".");
  259. AssertEquals ("GetExtension #08", String.Empty, testExtn);
  260. testExtn = Path.GetExtension ("end.");
  261. AssertEquals ("GetExtension #09", String.Empty, testExtn);
  262. testExtn = Path.GetExtension (".start");
  263. AssertEquals ("GetExtension #10", ".start", testExtn);
  264. testExtn = Path.GetExtension (".a");
  265. AssertEquals ("GetExtension #11", ".a", testExtn);
  266. testExtn = Path.GetExtension ("a.");
  267. AssertEquals ("GetExtension #12", String.Empty, testExtn);
  268. testExtn = Path.GetExtension ("a");
  269. AssertEquals ("GetExtension #13", String.Empty, testExtn);
  270. testExtn = Path.GetExtension ("makefile");
  271. AssertEquals ("GetExtension #14", String.Empty, testExtn);
  272. if (Windows) {
  273. try {
  274. testExtn = Path.GetExtension ("hi<there.txt");
  275. Fail ("GetExtension Fail #01");
  276. } catch (Exception e) {
  277. AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
  278. }
  279. }
  280. }
  281. public void TestGetFileName ()
  282. {
  283. string testFileName = Path.GetFileName (path1);
  284. AssertEquals ("GetFileName #01", "test.txt", testFileName);
  285. testFileName = Path.GetFileName (null);
  286. AssertEquals ("GetFileName #02", null, testFileName);
  287. testFileName = Path.GetFileName (String.Empty);
  288. AssertEquals ("GetFileName #03", String.Empty, testFileName);
  289. testFileName = Path.GetFileName (" ");
  290. AssertEquals ("GetFileName #04", " ", testFileName);
  291. if (Windows) {
  292. try {
  293. testFileName = Path.GetFileName ("hi<");
  294. Fail ("GetFileName Fail #01");
  295. } catch (Exception e) {
  296. AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
  297. }
  298. }
  299. }
  300. public void TestGetFileNameWithoutExtension ()
  301. {
  302. string testFileName = Path.GetFileNameWithoutExtension (path1);
  303. AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
  304. testFileName = Path.GetFileNameWithoutExtension (null);
  305. AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
  306. testFileName = Path.GetFileNameWithoutExtension (String.Empty);
  307. AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
  308. }
  309. [Ignore("This does not work under windows. See ERROR comments below.")]
  310. public void TestGetFullPath ()
  311. {
  312. string current = Directory.GetCurrentDirectory ();
  313. string testFullPath = Path.GetFullPath ("foo.txt");
  314. string expected = current + DSC + "foo.txt";
  315. AssertEquals ("GetFullPath #01", expected, testFullPath);
  316. testFullPath = Path.GetFullPath ("a//./.././foo.txt");
  317. AssertEquals ("GetFullPath #02", expected, testFullPath);
  318. string root = Windows ? "C:\\" : "/";
  319. string [,] test = new string [,] {
  320. {"root////././././././../root/././../root", "root"},
  321. {"root/", "root/"},
  322. {"root/./", "root/"},
  323. {"root/./", "root/"},
  324. {"root/../", ""},
  325. {"root/../", ""},
  326. {"root/../..", ""},
  327. {"root/.hiddenfile", "root/.hiddenfile"},
  328. {"root/. /", "root/. /"},
  329. {"root/.. /", "root/.. /"},
  330. {"root/..weirdname", "root/..weirdname"},
  331. {"root/..", ""},
  332. {"root/../a/b/../../..", ""},
  333. {"root/./..", ""},
  334. {"..", ""},
  335. {".", ""},
  336. {"root//dir", "root/dir"},
  337. {"root/. /", "root/. /"},
  338. {"root/.. /", "root/.. /"},
  339. {"root/ . /", "root/ . /"},
  340. {"root/ .. /", "root/ .. /"},
  341. {"root/./", "root/"},
  342. //ERROR! Paths are trimmed
  343. {"root/.. /", "root/.. /"},
  344. {".//", ""}
  345. };
  346. //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
  347. //... so only the first test was being done.
  348. for (int i = 0; i < test.GetUpperBound (1); i++) {
  349. AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
  350. }
  351. if (Windows) {
  352. string uncroot = @"\\server\share\";
  353. string [,] testunc = new string [,] {
  354. {"root////././././././../root/././../root", "root"},
  355. {"root/", "root/"},
  356. {"root/./", "root/"},
  357. {"root/./", "root/"},
  358. {"root/../", ""},
  359. {"root/../", ""},
  360. {"root/../..", ""},
  361. {"root/.hiddenfile", "root/.hiddenfile"},
  362. {"root/. /", "root/. /"},
  363. {"root/.. /", "root/.. /"},
  364. {"root/..weirdname", "root/..weirdname"},
  365. {"root/..", ""},
  366. {"root/../a/b/../../..", ""},
  367. {"root/./..", ""},
  368. {"..", ""},
  369. {".", ""},
  370. {"root//dir", "root/dir"},
  371. {"root/. /", "root/. /"},
  372. {"root/.. /", "root/.. /"},
  373. {"root/ . /", "root/ . /"},
  374. {"root/ .. /", "root/ .. /"},
  375. {"root/./", "root/"},
  376. {"root/.. /", "root/.. /"},
  377. {".//", ""}
  378. };
  379. for (int i = 0; i < test.GetUpperBound (1); i++) {
  380. AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
  381. }
  382. }
  383. try {
  384. testFullPath = Path.GetFullPath (null);
  385. Fail ("GetFullPath Fail #01");
  386. } catch (Exception e) {
  387. AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
  388. }
  389. try {
  390. testFullPath = Path.GetFullPath (String.Empty);
  391. Fail ("GetFullPath Fail #02");
  392. } catch (Exception e) {
  393. AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
  394. }
  395. }
  396. public void TestGetFullPath2 ()
  397. {
  398. if (Windows) {
  399. AssertEquals ("GetFullPath w#01", @"Z:\", Path.GetFullPath ("Z:"));
  400. AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path.GetFullPath (@"c:\abc\def"));
  401. Assert ("GetFullPath w#03", Path.GetFullPath (@"\").EndsWith (@"\"));
  402. // "\\\\" is not allowed
  403. Assert ("GetFullPath w#05", Path.GetFullPath ("/").EndsWith (@"\"));
  404. // "//" is not allowed
  405. Assert ("GetFullPath w#07", Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
  406. Assert ("GetFullPath w#08", Path.GetFullPath ("c").EndsWith (@"\c"));
  407. Assert ("GetFullPath w#09", Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
  408. Assert ("GetFullPath w#10", Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
  409. AssertEquals ("GetFullPath w#11", @"\\abc\def", Path.GetFullPath (@"\\abc\def"));
  410. AssertEquals ("GetFullPath w#12", Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"));
  411. AssertEquals ("GetFullPath w#13", Directory.GetCurrentDirectory ().Substring (0,2) + @"\abc\def", Path.GetFullPath ("/abc/def"));
  412. AssertEquals ("GetFullPath w#14", @"\\abc\def", Path.GetFullPath ("//abc/def"));
  413. }
  414. }
  415. public void TestGetPathRoot ()
  416. {
  417. string current;
  418. string expected;
  419. if (!Windows){
  420. current = Directory.GetCurrentDirectory ();
  421. expected = current [0].ToString ();
  422. } else {
  423. current = @"J:\Some\Strange Directory\Name";
  424. expected = "J:\\";
  425. }
  426. string pathRoot = Path.GetPathRoot (current);
  427. AssertEquals ("GetPathRoot #01", expected, pathRoot);
  428. }
  429. [Test]
  430. public void TestGetPathRoot2 ()
  431. {
  432. // note: this method doesn't call Directory.GetCurrentDirectory so it can be
  433. // reused for partial trust unit tests in PathCas.cs
  434. string pathRoot = Path.GetPathRoot ("hola");
  435. AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
  436. pathRoot = Path.GetPathRoot (null);
  437. AssertEquals ("GetPathRoot #03", null, pathRoot);
  438. if (Windows) {
  439. AssertEquals ("GetPathRoot w#01", "z:", Path.GetPathRoot ("z:"));
  440. AssertEquals ("GetPathRoot w#02", "c:\\", Path.GetPathRoot ("c:\\abc\\def"));
  441. AssertEquals ("GetPathRoot w#03", "\\", Path.GetPathRoot ("\\"));
  442. AssertEquals ("GetPathRoot w#04", "\\\\", Path.GetPathRoot ("\\\\"));
  443. AssertEquals ("GetPathRoot w#05", "\\", Path.GetPathRoot ("/"));
  444. AssertEquals ("GetPathRoot w#06", "\\\\", Path.GetPathRoot ("//"));
  445. AssertEquals ("GetPathRoot w#07", String.Empty, Path.GetPathRoot ("readme.txt"));
  446. AssertEquals ("GetPathRoot w#08", String.Empty, Path.GetPathRoot ("c"));
  447. AssertEquals ("GetPathRoot w#09", String.Empty, Path.GetPathRoot ("abc\\def"));
  448. AssertEquals ("GetPathRoot w#10", "\\", Path.GetPathRoot ("\\abc\\def"));
  449. AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"));
  450. AssertEquals ("GetPathRoot w#12", String.Empty, Path.GetPathRoot ("abc//def"));
  451. AssertEquals ("GetPathRoot w#13", "\\", Path.GetPathRoot ("/abc/def"));
  452. AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path.GetPathRoot ("//abc/def"));
  453. AssertEquals ("GetPathRoot w#15", @"C:\", Path.GetPathRoot (@"C:\"));
  454. AssertEquals ("GetPathRoot w#16", @"C:\", Path.GetPathRoot (@"C:\\"));
  455. AssertEquals ("GetPathRoot w#17", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def\\ghi"));
  456. } else {
  457. // TODO: Same tests for Unix.
  458. }
  459. }
  460. public void TestGetTempPath ()
  461. {
  462. string getTempPath = Path.GetTempPath ();
  463. Assert ("GetTempPath #01", getTempPath != String.Empty);
  464. Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
  465. AssertEquals ("GetTempPath #03", Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1]);
  466. }
  467. public void TestGetTempFileName ()
  468. {
  469. string getTempFileName = null;
  470. try {
  471. getTempFileName = Path.GetTempFileName ();
  472. Assert ("GetTempFileName #01", getTempFileName != String.Empty);
  473. Assert ("GetTempFileName #02", File.Exists (getTempFileName));
  474. } finally {
  475. if (getTempFileName != null && getTempFileName != String.Empty){
  476. File.Delete (getTempFileName);
  477. }
  478. }
  479. }
  480. public void TestHasExtension ()
  481. {
  482. AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
  483. AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
  484. AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
  485. AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
  486. AssertEquals ("HasExtension #05", false, Path.HasExtension (null));
  487. AssertEquals ("HasExtension #06", false, Path.HasExtension (String.Empty));
  488. AssertEquals ("HasExtension #07", false, Path.HasExtension (" "));
  489. AssertEquals ("HasExtension #08", false, Path.HasExtension ("."));
  490. AssertEquals ("HasExtension #09", false, Path.HasExtension ("end."));
  491. AssertEquals ("HasExtension #10", true, Path.HasExtension (".start"));
  492. AssertEquals ("HasExtension #11", true, Path.HasExtension (".a"));
  493. AssertEquals ("HasExtension #12", false, Path.HasExtension ("a."));
  494. AssertEquals ("HasExtension #13", false, Path.HasExtension ("Makefile"));
  495. }
  496. public void TestRooted ()
  497. {
  498. Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
  499. Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
  500. Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
  501. Assert ("IsPathRooted #04", !Path.IsPathRooted (String.Empty));
  502. Assert ("IsPathRooted #05", !Path.IsPathRooted (" "));
  503. Assert ("IsPathRooted #06", Path.IsPathRooted ("/"));
  504. if (Windows)
  505. Assert ("IsPathRooted #07", Path.IsPathRooted ("\\"));
  506. else
  507. Assert ("IsPathRooted #07", !Path.IsPathRooted ("\\"));
  508. Assert ("IsPathRooted #08", Path.IsPathRooted ("//"));
  509. if (Windows)
  510. Assert ("IsPathRooted #09", Path.IsPathRooted ("\\\\"));
  511. else
  512. Assert ("IsPathRooted #09", !Path.IsPathRooted ("\\\\"));
  513. Assert ("IsPathRooted #10", !Path.IsPathRooted (":"));
  514. if (Windows)
  515. Assert ("IsPathRooted #11", Path.IsPathRooted ("z:"));
  516. else
  517. Assert ("IsPathRooted #11", !Path.IsPathRooted ("z:"));
  518. if (Windows) {
  519. Assert ("IsPathRooted #12", Path.IsPathRooted ("z:\\"));
  520. Assert ("IsPathRooted #13", Path.IsPathRooted ("z:\\topdir"));
  521. // This looks MS BUG. It is treated as absolute path
  522. Assert ("IsPathRooted #14", Path.IsPathRooted ("z:curdir"));
  523. Assert ("IsPathRooted #15", Path.IsPathRooted ("\\abc\\def"));
  524. }
  525. }
  526. public void TestCanonicalizeDots ()
  527. {
  528. string current = Path.GetFullPath (".");
  529. Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
  530. string parent = Path.GetFullPath ("..");
  531. Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
  532. }
  533. public void TestDirectoryNameBugs ()
  534. {
  535. if (Windows) {
  536. AssertEquals ("Win #01", "C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"));
  537. } else {
  538. AssertEquals ("No win #01", "/etc", Path.GetDirectoryName ("/etc/hostname"));
  539. }
  540. }
  541. public void TestGetFullPathUnix ()
  542. {
  543. if (Windows)
  544. return;
  545. AssertEquals ("#01", "/", Path.GetFullPath ("/"));
  546. AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
  547. AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
  548. AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
  549. Path.GetFullPath ("hey"));
  550. }
  551. [Test]
  552. public void GetFullPath_EndingSeparator ()
  553. {
  554. string fp = Path.GetFullPath ("something/");
  555. char end = fp[fp.Length - 1];
  556. Assert (end == Path.DirectorySeparatorChar);
  557. }
  558. [Test]
  559. public void WindowsSystem32_76191 ()
  560. {
  561. // check for Unix platforms - see FAQ for more details
  562. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  563. int platform = (int) Environment.OSVersion.Platform;
  564. if ((platform == 4) || (platform == 128))
  565. return;
  566. string curdir = Directory.GetCurrentDirectory ();
  567. try {
  568. string system = Environment.SystemDirectory;
  569. Directory.SetCurrentDirectory (system);
  570. string drive = system.Substring (0, 2);
  571. AssertEquals ("current dir", system, Path.GetFullPath (drive));
  572. }
  573. finally {
  574. Directory.SetCurrentDirectory (curdir);
  575. }
  576. }
  577. [Test]
  578. public void WindowsSystem32_77007 ()
  579. {
  580. // check for Unix platforms - see FAQ for more details
  581. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  582. int platform = (int) Environment.OSVersion.Platform;
  583. if ((platform == 4) || (platform == 128))
  584. return;
  585. string curdir = Directory.GetCurrentDirectory ();
  586. try {
  587. string system = Environment.SystemDirectory;
  588. Directory.SetCurrentDirectory (system);
  589. // e.g. C:dir (no backslash) will return CurrentDirectory + dir
  590. string dir = system.Substring (0, 2) + "dir";
  591. AssertEquals ("current dir", Path.Combine (system, "dir"), Path.GetFullPath (dir));
  592. }
  593. finally {
  594. Directory.SetCurrentDirectory (curdir);
  595. }
  596. }
  597. [Test]
  598. public void WindowsDriveC14N_77058 ()
  599. {
  600. // check for Unix platforms - see FAQ for more details
  601. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  602. int platform = (int) Environment.OSVersion.Platform;
  603. if ((platform == 4) || (platform == 128))
  604. return;
  605. AssertEquals ("1", @"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"));
  606. AssertEquals ("2", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"));
  607. AssertEquals ("3", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"));
  608. AssertEquals ("4", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"));
  609. AssertEquals ("5", @"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"));
  610. }
  611. [Test]
  612. public void InvalidPathChars_Values ()
  613. {
  614. char[] invalid = Path.InvalidPathChars;
  615. if (Windows) {
  616. #if NET_2_0
  617. AssertEquals ("Length", 36, invalid.Length);
  618. #else
  619. AssertEquals ("Length", 15, invalid.Length);
  620. #endif
  621. foreach (char c in invalid) {
  622. int i = (int) c;
  623. #if NET_2_0
  624. if (i < 32)
  625. continue;
  626. #else
  627. if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
  628. continue;
  629. #endif
  630. // in both 1.1 SP1 and 2.0
  631. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  632. continue;
  633. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  634. }
  635. } else {
  636. foreach (char c in invalid) {
  637. int i = (int) c;
  638. if (i == 0)
  639. continue;
  640. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  641. }
  642. }
  643. }
  644. [Test]
  645. public void InvalidPathChars_Modify ()
  646. {
  647. char[] expected = Path.InvalidPathChars;
  648. char[] invalid = Path.InvalidPathChars;
  649. char original = invalid[0];
  650. try {
  651. invalid[0] = 'a';
  652. // kind of scary
  653. Assert ("expected", expected[0] == 'a');
  654. AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]);
  655. }
  656. finally {
  657. invalid[0] = original;
  658. }
  659. }
  660. #if NET_2_0
  661. [Test]
  662. public void GetInvalidFileNameChars_Values ()
  663. {
  664. char[] invalid = Path.GetInvalidFileNameChars ();
  665. if (Windows) {
  666. AssertEquals (41, invalid.Length);
  667. foreach (char c in invalid) {
  668. int i = (int) c;
  669. if (i < 32)
  670. continue;
  671. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  672. continue;
  673. // ':', '*', '?', '\', '/'
  674. if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
  675. continue;
  676. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  677. }
  678. } else {
  679. foreach (char c in invalid) {
  680. int i = (int) c;
  681. // null or '/'
  682. if ((i == 0) || (i == 47))
  683. continue;
  684. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  685. }
  686. }
  687. }
  688. [Test]
  689. public void GetInvalidFileNameChars_Modify ()
  690. {
  691. char[] expected = Path.GetInvalidFileNameChars ();
  692. char[] invalid = Path.GetInvalidFileNameChars ();
  693. invalid[0] = 'a';
  694. Assert ("expected", expected[0] != 'a');
  695. AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]);
  696. }
  697. [Test]
  698. public void GetInvalidPathChars_Values ()
  699. {
  700. char[] invalid = Path.GetInvalidPathChars ();
  701. if (Windows) {
  702. AssertEquals (36, invalid.Length);
  703. foreach (char c in invalid) {
  704. int i = (int) c;
  705. if (i < 32)
  706. continue;
  707. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  708. continue;
  709. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  710. }
  711. } else {
  712. foreach (char c in invalid) {
  713. int i = (int) c;
  714. if (i == 0)
  715. continue;
  716. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  717. }
  718. }
  719. }
  720. [Test]
  721. public void GetInvalidPathChars_Modify ()
  722. {
  723. char[] expected = Path.GetInvalidPathChars ();
  724. char[] invalid = Path.GetInvalidPathChars ();
  725. invalid[0] = 'a';
  726. Assert ("expected", expected[0] != 'a');
  727. AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]);
  728. }
  729. [Test]
  730. public void GetRandomFileName ()
  731. {
  732. string s = Path.GetRandomFileName ();
  733. AssertEquals ("Length", 12, s.Length);
  734. char[] invalid = Path.GetInvalidFileNameChars ();
  735. for (int i=0; i < s.Length; i++) {
  736. if (i == 8)
  737. AssertEquals ("8", '.', s[i]);
  738. else
  739. Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1);
  740. }
  741. }
  742. #endif
  743. }
  744. }