PathTest.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. #if !TARGET_JVM // Java full (canonical) path always starts with caps drive letter
  401. AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path.GetFullPath (@"c:\abc\def"));
  402. #endif
  403. Assert ("GetFullPath w#03", Path.GetFullPath (@"\").EndsWith (@"\"));
  404. // "\\\\" is not allowed
  405. Assert ("GetFullPath w#05", Path.GetFullPath ("/").EndsWith (@"\"));
  406. // "//" is not allowed
  407. Assert ("GetFullPath w#07", Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
  408. Assert ("GetFullPath w#08", Path.GetFullPath ("c").EndsWith (@"\c"));
  409. Assert ("GetFullPath w#09", Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
  410. Assert ("GetFullPath w#10", Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
  411. AssertEquals ("GetFullPath w#11", @"\\abc\def", Path.GetFullPath (@"\\abc\def"));
  412. AssertEquals ("GetFullPath w#12", Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"));
  413. AssertEquals ("GetFullPath w#13", Directory.GetCurrentDirectory ().Substring (0,2) + @"\abc\def", Path.GetFullPath ("/abc/def"));
  414. AssertEquals ("GetFullPath w#14", @"\\abc\def", Path.GetFullPath ("//abc/def"));
  415. }
  416. }
  417. public void TestGetPathRoot ()
  418. {
  419. string current;
  420. string expected;
  421. if (!Windows){
  422. current = Directory.GetCurrentDirectory ();
  423. expected = current [0].ToString ();
  424. } else {
  425. current = @"J:\Some\Strange Directory\Name";
  426. expected = "J:\\";
  427. }
  428. string pathRoot = Path.GetPathRoot (current);
  429. AssertEquals ("GetPathRoot #01", expected, pathRoot);
  430. }
  431. [Test]
  432. public void TestGetPathRoot2 ()
  433. {
  434. // note: this method doesn't call Directory.GetCurrentDirectory so it can be
  435. // reused for partial trust unit tests in PathCas.cs
  436. string pathRoot = Path.GetPathRoot ("hola");
  437. AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
  438. pathRoot = Path.GetPathRoot (null);
  439. AssertEquals ("GetPathRoot #03", null, pathRoot);
  440. if (Windows) {
  441. AssertEquals ("GetPathRoot w#01", "z:", Path.GetPathRoot ("z:"));
  442. AssertEquals ("GetPathRoot w#02", "c:\\", Path.GetPathRoot ("c:\\abc\\def"));
  443. AssertEquals ("GetPathRoot w#03", "\\", Path.GetPathRoot ("\\"));
  444. AssertEquals ("GetPathRoot w#04", "\\\\", Path.GetPathRoot ("\\\\"));
  445. AssertEquals ("GetPathRoot w#05", "\\", Path.GetPathRoot ("/"));
  446. AssertEquals ("GetPathRoot w#06", "\\\\", Path.GetPathRoot ("//"));
  447. AssertEquals ("GetPathRoot w#07", String.Empty, Path.GetPathRoot ("readme.txt"));
  448. AssertEquals ("GetPathRoot w#08", String.Empty, Path.GetPathRoot ("c"));
  449. AssertEquals ("GetPathRoot w#09", String.Empty, Path.GetPathRoot ("abc\\def"));
  450. AssertEquals ("GetPathRoot w#10", "\\", Path.GetPathRoot ("\\abc\\def"));
  451. AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"));
  452. AssertEquals ("GetPathRoot w#12", String.Empty, Path.GetPathRoot ("abc//def"));
  453. AssertEquals ("GetPathRoot w#13", "\\", Path.GetPathRoot ("/abc/def"));
  454. AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path.GetPathRoot ("//abc/def"));
  455. AssertEquals ("GetPathRoot w#15", @"C:\", Path.GetPathRoot (@"C:\"));
  456. AssertEquals ("GetPathRoot w#16", @"C:\", Path.GetPathRoot (@"C:\\"));
  457. AssertEquals ("GetPathRoot w#17", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def\\ghi"));
  458. } else {
  459. // TODO: Same tests for Unix.
  460. }
  461. }
  462. public void TestGetTempPath ()
  463. {
  464. string getTempPath = Path.GetTempPath ();
  465. Assert ("GetTempPath #01", getTempPath != String.Empty);
  466. Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
  467. AssertEquals ("GetTempPath #03", Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1]);
  468. }
  469. public void TestGetTempFileName ()
  470. {
  471. string getTempFileName = null;
  472. try {
  473. getTempFileName = Path.GetTempFileName ();
  474. Assert ("GetTempFileName #01", getTempFileName != String.Empty);
  475. Assert ("GetTempFileName #02", File.Exists (getTempFileName));
  476. } finally {
  477. if (getTempFileName != null && getTempFileName != String.Empty){
  478. File.Delete (getTempFileName);
  479. }
  480. }
  481. }
  482. public void TestHasExtension ()
  483. {
  484. AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
  485. AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
  486. AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
  487. AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
  488. AssertEquals ("HasExtension #05", false, Path.HasExtension (null));
  489. AssertEquals ("HasExtension #06", false, Path.HasExtension (String.Empty));
  490. AssertEquals ("HasExtension #07", false, Path.HasExtension (" "));
  491. AssertEquals ("HasExtension #08", false, Path.HasExtension ("."));
  492. AssertEquals ("HasExtension #09", false, Path.HasExtension ("end."));
  493. AssertEquals ("HasExtension #10", true, Path.HasExtension (".start"));
  494. AssertEquals ("HasExtension #11", true, Path.HasExtension (".a"));
  495. AssertEquals ("HasExtension #12", false, Path.HasExtension ("a."));
  496. AssertEquals ("HasExtension #13", false, Path.HasExtension ("Makefile"));
  497. }
  498. public void TestRooted ()
  499. {
  500. Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
  501. Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
  502. Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
  503. Assert ("IsPathRooted #04", !Path.IsPathRooted (String.Empty));
  504. Assert ("IsPathRooted #05", !Path.IsPathRooted (" "));
  505. Assert ("IsPathRooted #06", Path.IsPathRooted ("/"));
  506. if (Windows)
  507. Assert ("IsPathRooted #07", Path.IsPathRooted ("\\"));
  508. else
  509. Assert ("IsPathRooted #07", !Path.IsPathRooted ("\\"));
  510. Assert ("IsPathRooted #08", Path.IsPathRooted ("//"));
  511. if (Windows)
  512. Assert ("IsPathRooted #09", Path.IsPathRooted ("\\\\"));
  513. else
  514. Assert ("IsPathRooted #09", !Path.IsPathRooted ("\\\\"));
  515. Assert ("IsPathRooted #10", !Path.IsPathRooted (":"));
  516. if (Windows)
  517. Assert ("IsPathRooted #11", Path.IsPathRooted ("z:"));
  518. else
  519. Assert ("IsPathRooted #11", !Path.IsPathRooted ("z:"));
  520. if (Windows) {
  521. Assert ("IsPathRooted #12", Path.IsPathRooted ("z:\\"));
  522. Assert ("IsPathRooted #13", Path.IsPathRooted ("z:\\topdir"));
  523. // This looks MS BUG. It is treated as absolute path
  524. Assert ("IsPathRooted #14", Path.IsPathRooted ("z:curdir"));
  525. Assert ("IsPathRooted #15", Path.IsPathRooted ("\\abc\\def"));
  526. }
  527. }
  528. public void TestCanonicalizeDots ()
  529. {
  530. string current = Path.GetFullPath (".");
  531. Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
  532. string parent = Path.GetFullPath ("..");
  533. Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
  534. }
  535. public void TestDirectoryNameBugs ()
  536. {
  537. if (Windows) {
  538. AssertEquals ("Win #01", "C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"));
  539. } else {
  540. AssertEquals ("No win #01", "/etc", Path.GetDirectoryName ("/etc/hostname"));
  541. }
  542. }
  543. public void TestGetFullPathUnix ()
  544. {
  545. if (Windows)
  546. return;
  547. AssertEquals ("#01", "/", Path.GetFullPath ("/"));
  548. AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
  549. AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
  550. AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
  551. Path.GetFullPath ("hey"));
  552. }
  553. [Test]
  554. public void GetFullPath_EndingSeparator ()
  555. {
  556. string fp = Path.GetFullPath ("something/");
  557. char end = fp[fp.Length - 1];
  558. Assert (end == Path.DirectorySeparatorChar);
  559. }
  560. [Test]
  561. public void WindowsSystem32_76191 ()
  562. {
  563. #if !TARGET_JVM
  564. // check for Unix platforms - see FAQ for more details
  565. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  566. int platform = (int) Environment.OSVersion.Platform;
  567. if ((platform == 4) || (platform == 128))
  568. return;
  569. #endif
  570. string curdir = Directory.GetCurrentDirectory ();
  571. try {
  572. #if TARGET_JVM
  573. string system = "C:\\WINDOWS\\system32\\";
  574. #else
  575. string system = Environment.SystemDirectory;
  576. #endif
  577. Directory.SetCurrentDirectory (system);
  578. string drive = system.Substring (0, 2);
  579. AssertEquals ("current dir", system, Path.GetFullPath (drive));
  580. }
  581. finally {
  582. Directory.SetCurrentDirectory (curdir);
  583. }
  584. }
  585. [Test]
  586. public void WindowsSystem32_77007 ()
  587. {
  588. #if !TARGET_JVM
  589. // check for Unix platforms - see FAQ for more details
  590. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  591. int platform = (int) Environment.OSVersion.Platform;
  592. if ((platform == 4) || (platform == 128))
  593. return;
  594. #endif
  595. string curdir = Directory.GetCurrentDirectory ();
  596. try {
  597. #if TARGET_JVM
  598. string system = "C:\\WINDOWS\\system32\\";
  599. #else
  600. string system = Environment.SystemDirectory;
  601. #endif
  602. Directory.SetCurrentDirectory (system);
  603. // e.g. C:dir (no backslash) will return CurrentDirectory + dir
  604. string dir = system.Substring (0, 2) + "dir";
  605. AssertEquals ("current dir", Path.Combine (system, "dir"), Path.GetFullPath (dir));
  606. }
  607. finally {
  608. Directory.SetCurrentDirectory (curdir);
  609. }
  610. }
  611. [Test]
  612. #if TARGET_JVM
  613. [Ignore("Java full (canonical) path always returns windows dir in caps")]
  614. #endif
  615. public void WindowsDriveC14N_77058 ()
  616. {
  617. // check for Unix platforms - see FAQ for more details
  618. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  619. int platform = (int) Environment.OSVersion.Platform;
  620. if ((platform == 4) || (platform == 128))
  621. return;
  622. AssertEquals ("1", @"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"));
  623. AssertEquals ("2", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"));
  624. AssertEquals ("3", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"));
  625. AssertEquals ("4", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"));
  626. AssertEquals ("5", @"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"));
  627. }
  628. [Test]
  629. public void InvalidPathChars_Values ()
  630. {
  631. char[] invalid = Path.InvalidPathChars;
  632. if (Windows) {
  633. #if NET_2_0
  634. AssertEquals ("Length", 36, invalid.Length);
  635. #else
  636. AssertEquals ("Length", 15, invalid.Length);
  637. #endif
  638. foreach (char c in invalid) {
  639. int i = (int) c;
  640. #if NET_2_0
  641. if (i < 32)
  642. continue;
  643. #else
  644. if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
  645. continue;
  646. #endif
  647. // in both 1.1 SP1 and 2.0
  648. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  649. continue;
  650. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  651. }
  652. } else {
  653. foreach (char c in invalid) {
  654. int i = (int) c;
  655. if (i == 0)
  656. continue;
  657. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  658. }
  659. }
  660. }
  661. [Test]
  662. public void InvalidPathChars_Modify ()
  663. {
  664. char[] expected = Path.InvalidPathChars;
  665. char[] invalid = Path.InvalidPathChars;
  666. char original = invalid[0];
  667. try {
  668. invalid[0] = 'a';
  669. // kind of scary
  670. Assert ("expected", expected[0] == 'a');
  671. AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]);
  672. }
  673. finally {
  674. invalid[0] = original;
  675. }
  676. }
  677. #if NET_2_0
  678. [Test]
  679. public void GetInvalidFileNameChars_Values ()
  680. {
  681. char[] invalid = Path.GetInvalidFileNameChars ();
  682. if (Windows) {
  683. AssertEquals (41, invalid.Length);
  684. foreach (char c in invalid) {
  685. int i = (int) c;
  686. if (i < 32)
  687. continue;
  688. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  689. continue;
  690. // ':', '*', '?', '\', '/'
  691. if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
  692. continue;
  693. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  694. }
  695. } else {
  696. foreach (char c in invalid) {
  697. int i = (int) c;
  698. // null or '/'
  699. if ((i == 0) || (i == 47))
  700. continue;
  701. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  702. }
  703. }
  704. }
  705. [Test]
  706. public void GetInvalidFileNameChars_Modify ()
  707. {
  708. char[] expected = Path.GetInvalidFileNameChars ();
  709. char[] invalid = Path.GetInvalidFileNameChars ();
  710. invalid[0] = 'a';
  711. Assert ("expected", expected[0] != 'a');
  712. AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]);
  713. }
  714. [Test]
  715. public void GetInvalidPathChars_Values ()
  716. {
  717. char[] invalid = Path.GetInvalidPathChars ();
  718. if (Windows) {
  719. AssertEquals (36, invalid.Length);
  720. foreach (char c in invalid) {
  721. int i = (int) c;
  722. if (i < 32)
  723. continue;
  724. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  725. continue;
  726. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  727. }
  728. } else {
  729. foreach (char c in invalid) {
  730. int i = (int) c;
  731. if (i == 0)
  732. continue;
  733. Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  734. }
  735. }
  736. }
  737. [Test]
  738. public void GetInvalidPathChars_Order()
  739. {
  740. if (Windows) {
  741. char [] invalid = Path.GetInvalidPathChars ();
  742. char [] expected = new char [36] { '\x22', '\x3C', '\x3E', '\x7C', '\x00', '\x01', '\x02',
  743. '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D',
  744. '\x0E', '\x0F', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18',
  745. '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' };
  746. AssertEquals (expected.Length, invalid.Length);
  747. for (int i = 0; i < expected.Length; i++ ) {
  748. AssertEquals( "Character at position " + i,expected [i], invalid [i]);
  749. }
  750. }
  751. }
  752. [Test]
  753. public void GetInvalidPathChars_Modify ()
  754. {
  755. char[] expected = Path.GetInvalidPathChars ();
  756. char[] invalid = Path.GetInvalidPathChars ();
  757. invalid[0] = 'a';
  758. Assert ("expected", expected[0] != 'a');
  759. AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]);
  760. }
  761. [Test]
  762. public void GetRandomFileName ()
  763. {
  764. string s = Path.GetRandomFileName ();
  765. AssertEquals ("Length", 12, s.Length);
  766. char[] invalid = Path.GetInvalidFileNameChars ();
  767. for (int i=0; i < s.Length; i++) {
  768. if (i == 8)
  769. AssertEquals ("8", '.', s[i]);
  770. else
  771. Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1);
  772. }
  773. }
  774. #endif
  775. }
  776. }