PathTest.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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
  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. [Test]
  77. public void ChangeExtension ()
  78. {
  79. string [] files = new string [3];
  80. files [(int) OsType.Unix] = "/foo/test.doc";
  81. files [(int) OsType.Windows] = "c:\\foo\\test.doc";
  82. files [(int) OsType.Mac] = "foo:test.doc";
  83. string testPath = Path.ChangeExtension (path1, "doc");
  84. Assert.AreEqual (files [(int) OS], testPath, "ChangeExtension #01");
  85. testPath = Path.ChangeExtension (String.Empty, ".extension");
  86. Assert.AreEqual (String.Empty, testPath, "ChangeExtension #02");
  87. testPath = Path.ChangeExtension (null, ".extension");
  88. Assert.AreEqual (null, testPath, "ChangeExtension #03");
  89. testPath = Path.ChangeExtension ("path", null);
  90. Assert.AreEqual ("path", testPath, "ChangeExtension #04");
  91. testPath = Path.ChangeExtension ("path.ext", "doc");
  92. Assert.AreEqual ("path.doc", testPath, "ChangeExtension #05");
  93. testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
  94. Assert.AreEqual ("path.ext1.doc", testPath, "ChangeExtension #06");
  95. testPath = Path.ChangeExtension ("hogehoge.xml", ".xsl");
  96. Assert.AreEqual ("hogehoge.xsl", testPath, "ChangeExtension #07");
  97. testPath = Path.ChangeExtension ("hogehoge", ".xsl");
  98. Assert.AreEqual ("hogehoge.xsl", testPath, "ChangeExtension #08");
  99. testPath = Path.ChangeExtension ("hogehoge.xml", "xsl");
  100. Assert.AreEqual ("hogehoge.xsl", testPath, "ChangeExtension #09");
  101. testPath = Path.ChangeExtension ("hogehoge", "xsl");
  102. Assert.AreEqual ("hogehoge.xsl", testPath, "ChangeExtension #10");
  103. testPath = Path.ChangeExtension ("hogehoge.xml", String.Empty);
  104. Assert.AreEqual ("hogehoge.", testPath, "ChangeExtension #11");
  105. testPath = Path.ChangeExtension ("hogehoge", String.Empty);
  106. Assert.AreEqual ("hogehoge.", testPath, "ChangeExtension #12");
  107. testPath = Path.ChangeExtension ("hogehoge.", null);
  108. Assert.AreEqual ("hogehoge", testPath, "ChangeExtension #13");
  109. testPath = Path.ChangeExtension ("hogehoge", null);
  110. Assert.AreEqual ("hogehoge", testPath, "ChangeExtension #14");
  111. testPath = Path.ChangeExtension (String.Empty, null);
  112. Assert.AreEqual (String.Empty, testPath, "ChangeExtension #15");
  113. testPath = Path.ChangeExtension (String.Empty, "bashrc");
  114. Assert.AreEqual (String.Empty, testPath, "ChangeExtension #16");
  115. testPath = Path.ChangeExtension (String.Empty, ".bashrc");
  116. Assert.AreEqual (String.Empty, testPath, "ChangeExtension #17");
  117. testPath = Path.ChangeExtension (null, null);
  118. Assert.IsNull (testPath, "ChangeExtension #18");
  119. }
  120. [Test]
  121. public void ChangeExtension_Extension_InvalidPathChars ()
  122. {
  123. string fn = Path.ChangeExtension ("file.ext", "<");
  124. Assert.AreEqual ("file.<", fn, "Invalid filename");
  125. }
  126. [Test]
  127. public void ChangeExtension_Path_InvalidPathChars ()
  128. {
  129. try {
  130. Path.ChangeExtension ("fi\0le.ext", ".extension");
  131. Assert.Fail ("#1");
  132. } catch (ArgumentException ex) {
  133. // Illegal characters in path
  134. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  135. Assert.IsNull (ex.InnerException, "#3");
  136. Assert.IsNotNull (ex.Message, "#4");
  137. Assert.IsNull (ex.ParamName, "#5");
  138. }
  139. }
  140. [Test]
  141. public void Combine ()
  142. {
  143. string [] files = new string [3];
  144. files [(int) OsType.Unix] = "/etc/init.d";
  145. files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
  146. files [(int) OsType.Mac] = "foo:bar";
  147. string testPath = Path.Combine (path2, path3);
  148. Assert.AreEqual (files [(int) OS], testPath, "Combine #01");
  149. testPath = Path.Combine ("one", String.Empty);
  150. Assert.AreEqual ("one", testPath, "Combine #02");
  151. testPath = Path.Combine (String.Empty, "one");
  152. Assert.AreEqual ("one", testPath, "Combine #03");
  153. string current = Directory.GetCurrentDirectory ();
  154. testPath = Path.Combine (current, "one");
  155. string expected = current + DSC + "one";
  156. Assert.AreEqual (expected, testPath, "Combine #04");
  157. testPath = Path.Combine ("one", current);
  158. // LAMESPEC noted in Path.cs
  159. Assert.AreEqual (current, testPath, "Combine #05");
  160. testPath = Path.Combine (current, expected);
  161. Assert.AreEqual (expected, testPath, "Combine #06");
  162. testPath = DSC + "one";
  163. testPath = Path.Combine (testPath, "two" + DSC);
  164. expected = DSC + "one" + DSC + "two" + DSC;
  165. Assert.AreEqual (expected, testPath, "Combine #06");
  166. testPath = "one" + DSC;
  167. testPath = Path.Combine (testPath, DSC + "two");
  168. expected = DSC + "two";
  169. Assert.AreEqual (expected, testPath, "Combine #06");
  170. testPath = "one" + DSC;
  171. testPath = Path.Combine (testPath, "two" + DSC);
  172. expected = "one" + DSC + "two" + DSC;
  173. Assert.AreEqual (expected, testPath, "Combine #07");
  174. }
  175. [Test]
  176. public void Combine_Path1_InvalidPathChars ()
  177. {
  178. try {
  179. Path.Combine ("a\0", "one");
  180. Assert.Fail ("#1");
  181. } catch (ArgumentException ex) {
  182. // Illegal characters in path
  183. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  184. Assert.IsNull (ex.InnerException, "#3");
  185. Assert.IsNotNull (ex.Message, "#4");
  186. Assert.IsNull (ex.ParamName, "#5");
  187. }
  188. }
  189. [Test]
  190. public void Combine_Path1_Null ()
  191. {
  192. try {
  193. Path.Combine (null, "one");
  194. Assert.Fail ("#1");
  195. } catch (ArgumentNullException ex) {
  196. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  197. Assert.IsNull (ex.InnerException, "#3");
  198. Assert.IsNotNull (ex.Message, "#4");
  199. Assert.AreEqual ("path1", ex.ParamName, "#5");
  200. }
  201. }
  202. [Test]
  203. public void Combine_Path2_InvalidPathChars ()
  204. {
  205. try {
  206. Path.Combine ("one", "a\0");
  207. Assert.Fail ("#1");
  208. } catch (ArgumentException ex) {
  209. // Illegal characters in path
  210. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  211. Assert.IsNull (ex.InnerException, "#3");
  212. Assert.IsNotNull (ex.Message, "#4");
  213. Assert.IsNull (ex.ParamName, "#5");
  214. }
  215. }
  216. [Test]
  217. public void Combine_Path2_Null ()
  218. {
  219. try {
  220. Path.Combine ("one", null);
  221. Assert.Fail ("#1");
  222. } catch (ArgumentNullException ex) {
  223. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  224. Assert.IsNull (ex.InnerException, "#3");
  225. Assert.IsNotNull (ex.Message, "#4");
  226. Assert.AreEqual ("path2", ex.ParamName, "#5");
  227. }
  228. }
  229. [Test]
  230. public void GetDirectoryName ()
  231. {
  232. string [] files = new string [3];
  233. files [(int) OsType.Unix] = "/foo";
  234. files [(int) OsType.Windows] = "c:\\foo";
  235. files [(int) OsType.Mac] = "foo";
  236. string testDirName = Path.GetDirectoryName (path1);
  237. Assert.AreEqual (files [(int) OS], testDirName, "#A1");
  238. testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
  239. Assert.AreEqual (files [(int) OS], testDirName, "#A2");
  240. if (Windows) {
  241. Assert.AreEqual ("C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"), "#B1");
  242. Assert.AreEqual (null, Path.GetDirectoryName ("C:"), "#B2");
  243. Assert.AreEqual (null, Path.GetDirectoryName (@"C:\"), "#B3");
  244. Assert.AreEqual (@"C:\", Path.GetDirectoryName (@"C:\dir"), "#B4");
  245. Assert.AreEqual (@"C:\dir", Path.GetDirectoryName (@"C:\dir\"), "#B5");
  246. Assert.AreEqual (@"C:\dir", Path.GetDirectoryName (@"C:\dir\dir"), "#B6");
  247. Assert.AreEqual (@"C:\dir\dir", Path.GetDirectoryName (@"C:\dir\dir\"), "#B7");
  248. Assert.AreEqual (@"C:", Path.GetDirectoryName (@"C:foo.txt"), "#B8");
  249. Assert.AreEqual (@"C:dir", Path.GetDirectoryName (@"C:dir\"), "#B9");
  250. Assert.AreEqual ("\\foo\\bar", Path.GetDirectoryName ("/foo//bar/dingus"), "#C1");
  251. Assert.AreEqual ("foo\\bar", Path.GetDirectoryName ("foo/bar/"), "#C2");
  252. Assert.AreEqual ("foo\\bar", Path.GetDirectoryName ("foo/bar\\xxx"), "#C3");
  253. Assert.AreEqual ("\\\\host\\dir\\dir2", Path.GetDirectoryName ("\\\\host\\dir\\\\dir2\\path"), "#C4");
  254. // UNC tests
  255. Assert.AreEqual (null, Path.GetDirectoryName (@"\\"), "#D1");
  256. Assert.AreEqual (null, Path.GetDirectoryName (@"\\server"), "#D2");
  257. Assert.AreEqual (null, Path.GetDirectoryName (@"\\server\share"), "#D3");
  258. Assert.AreEqual (@"\\server\share", Path.GetDirectoryName (@"\\server\share\"), "#D4");
  259. Assert.AreEqual (@"\\server\share", Path.GetDirectoryName (@"\\server\share\dir"), "#D5");
  260. Assert.AreEqual (@"\\server\share\dir", Path.GetDirectoryName (@"\\server\share\dir\subdir"), "#D6");
  261. } else {
  262. Assert.AreEqual ("/etc", Path.GetDirectoryName ("/etc/hostname"), "#B1");
  263. Assert.AreEqual ("/foo/bar", Path.GetDirectoryName ("/foo//bar/dingus"), "#B2");
  264. Assert.AreEqual ("foo/bar", Path.GetDirectoryName ("foo/bar/"), "#B3");
  265. Assert.AreEqual ("/", Path.GetDirectoryName ("/tmp"), "#B4");
  266. Assert.IsNull (Path.GetDirectoryName ("/"), "#B5");
  267. Assert.AreEqual ("a", Path.GetDirectoryName ("a//b"), "#B6");
  268. }
  269. }
  270. [Test]
  271. public void GetDirectoryName_Path_Empty ()
  272. {
  273. try {
  274. Path.GetDirectoryName (String.Empty);
  275. Assert.Fail ("#1");
  276. } catch (ArgumentException ex) {
  277. // The path is not of a legal form
  278. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  279. Assert.IsNull (ex.InnerException, "#3");
  280. Assert.IsNotNull (ex.Message, "#4");
  281. Assert.IsNull (ex.ParamName, "#5");
  282. }
  283. }
  284. [Test]
  285. public void GetDirectoryName_Path_InvalidPathChars ()
  286. {
  287. try {
  288. Path.GetDirectoryName ("hi\0world");
  289. Assert.Fail ("#1");
  290. } catch (ArgumentException ex) {
  291. // Illegal characters in path
  292. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  293. Assert.IsNull (ex.InnerException, "#3");
  294. Assert.IsNotNull (ex.Message, "#4");
  295. Assert.IsNull (ex.ParamName, "#5");
  296. }
  297. }
  298. [Test]
  299. public void GetDirectoryName_Path_Null ()
  300. {
  301. Assert.IsNull (Path.GetDirectoryName (null));
  302. }
  303. [Test]
  304. public void GetDirectoryName_Path_Whitespace ()
  305. {
  306. try {
  307. Path.GetDirectoryName (" ");
  308. Assert.Fail ("#1");
  309. } catch (ArgumentException ex) {
  310. // The path is not of a legal form
  311. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  312. Assert.IsNull (ex.InnerException, "#3");
  313. Assert.IsNotNull (ex.Message, "#4");
  314. Assert.IsNull (ex.ParamName, "#5");
  315. }
  316. }
  317. [Test]
  318. public void GetExtension ()
  319. {
  320. string testExtn = Path.GetExtension (path1);
  321. Assert.AreEqual (".txt", testExtn, "GetExtension #01");
  322. testExtn = Path.GetExtension (path2);
  323. Assert.AreEqual (String.Empty, testExtn, "GetExtension #02");
  324. testExtn = Path.GetExtension (String.Empty);
  325. Assert.AreEqual (String.Empty, testExtn, "GetExtension #03");
  326. testExtn = Path.GetExtension (null);
  327. Assert.AreEqual (null, testExtn, "GetExtension #04");
  328. testExtn = Path.GetExtension (" ");
  329. Assert.AreEqual (String.Empty, testExtn, "GetExtension #05");
  330. testExtn = Path.GetExtension (path1 + ".doc");
  331. Assert.AreEqual (".doc", testExtn, "GetExtension #06");
  332. testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
  333. Assert.AreEqual (".txt", testExtn, "GetExtension #07");
  334. testExtn = Path.GetExtension (".");
  335. Assert.AreEqual (String.Empty, testExtn, "GetExtension #08");
  336. testExtn = Path.GetExtension ("end.");
  337. Assert.AreEqual (String.Empty, testExtn, "GetExtension #09");
  338. testExtn = Path.GetExtension (".start");
  339. Assert.AreEqual (".start", testExtn, "GetExtension #10");
  340. testExtn = Path.GetExtension (".a");
  341. Assert.AreEqual (".a", testExtn, "GetExtension #11");
  342. testExtn = Path.GetExtension ("a.");
  343. Assert.AreEqual (String.Empty, testExtn, "GetExtension #12");
  344. testExtn = Path.GetExtension ("a");
  345. Assert.AreEqual (String.Empty, testExtn, "GetExtension #13");
  346. testExtn = Path.GetExtension ("makefile");
  347. Assert.AreEqual (String.Empty, testExtn, "GetExtension #14");
  348. }
  349. [Test]
  350. public void GetExtension_Path_InvalidPathChars ()
  351. {
  352. try {
  353. Path.GetExtension ("hi\0world.txt");
  354. Assert.Fail ("#1");
  355. } catch (ArgumentException ex) {
  356. // Illegal characters in path.
  357. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  358. Assert.IsNull (ex.InnerException, "#3");
  359. Assert.IsNotNull (ex.Message, "#4");
  360. Assert.IsNull (ex.ParamName, "#5");
  361. }
  362. }
  363. [Test]
  364. public void GetFileName ()
  365. {
  366. string testFileName = Path.GetFileName (path1);
  367. Assert.AreEqual ("test.txt", testFileName, "#1");
  368. testFileName = Path.GetFileName (null);
  369. Assert.AreEqual (null, testFileName, "#2");
  370. testFileName = Path.GetFileName (String.Empty);
  371. Assert.AreEqual (String.Empty, testFileName, "#3");
  372. testFileName = Path.GetFileName (" ");
  373. Assert.AreEqual (" ", testFileName, "#4");
  374. }
  375. [Test]
  376. public void GetFileName_Path_InvalidPathChars ()
  377. {
  378. try {
  379. Path.GetFileName ("hi\0world");
  380. Assert.Fail ("#1");
  381. } catch (ArgumentException ex) {
  382. // Illegal characters in path
  383. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  384. Assert.IsNull (ex.InnerException, "#3");
  385. Assert.IsNotNull (ex.Message, "#4");
  386. Assert.IsNull (ex.ParamName, "#5");
  387. }
  388. }
  389. [Test]
  390. public void GetFileNameWithoutExtension ()
  391. {
  392. string testFileName = Path.GetFileNameWithoutExtension (path1);
  393. Assert.AreEqual ("test", testFileName, "GetFileNameWithoutExtension #01");
  394. testFileName = Path.GetFileNameWithoutExtension (null);
  395. Assert.AreEqual (null, testFileName, "GetFileNameWithoutExtension #02");
  396. testFileName = Path.GetFileNameWithoutExtension (String.Empty);
  397. Assert.AreEqual (String.Empty, testFileName, "GetFileNameWithoutExtension #03");
  398. }
  399. [Test]
  400. public void GetFileNameWithoutExtension_Path_InvalidPathChars ()
  401. {
  402. try {
  403. Path.GetFileNameWithoutExtension ("hi\0world");
  404. Assert.Fail ("#1");
  405. } catch (ArgumentException ex) {
  406. // Illegal characters in path
  407. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  408. Assert.IsNull (ex.InnerException, "#3");
  409. Assert.IsNotNull (ex.Message, "#4");
  410. Assert.IsNull (ex.ParamName, "#5");
  411. }
  412. }
  413. [Test]
  414. public void GetFullPath ()
  415. {
  416. string current = Directory.GetCurrentDirectory ();
  417. string testFullPath = Path.GetFullPath ("foo.txt");
  418. string expected = current + DSC + "foo.txt";
  419. Assert.AreEqual (expected, testFullPath, "GetFullPath #01");
  420. testFullPath = Path.GetFullPath ("a//./.././foo.txt");
  421. Assert.AreEqual (expected, testFullPath, "GetFullPath #02");
  422. }
  423. [Test]
  424. public void GetFullPath_Unix ()
  425. {
  426. if (Windows)
  427. return;
  428. string root = "/";
  429. string [,] test = new string [,] {
  430. {"root////././././././../root/././../root", "root"},
  431. {"root/", "root/"},
  432. {"root/./", "root/"},
  433. {"root/./", "root/"},
  434. {"root/../", String.Empty},
  435. {"root/../", String.Empty},
  436. {"root/../..", String.Empty},
  437. {"root/.hiddenfile", "root/.hiddenfile"},
  438. {"root/. /", "root/. /"},
  439. {"root/.. /", "root/.. /"},
  440. {"root/..weirdname", "root/..weirdname"},
  441. {"root/..", String.Empty},
  442. {"root/../a/b/../../..", String.Empty},
  443. {"root/./..", String.Empty},
  444. {"..", String.Empty},
  445. {".", String.Empty},
  446. {"root//dir", "root/dir"},
  447. {"root/. /", "root/. /"},
  448. {"root/.. /", "root/.. /"},
  449. {"root/ . /", "root/ . /"},
  450. {"root/ .. /", "root/ .. /"},
  451. {"root/./", "root/"},
  452. //ERROR! Paths are trimmed
  453. // I don't understand this comment^^.
  454. // No trimming occurs but the paths are not equal. That's why the test fails. Commented out.
  455. //{"root/.. /", "root/.. /"},
  456. {".//", String.Empty}
  457. };
  458. for (int i = 0; i < test.GetUpperBound (0); i++) {
  459. Assert.AreEqual (root + test [i, 1], Path.GetFullPath (root + test [i, 0]),
  460. String.Format ("GetFullPathUnix #{0}", i));
  461. }
  462. Assert.AreEqual ("/", Path.GetFullPath ("/"), "#01");
  463. Assert.AreEqual ("/hey", Path.GetFullPath ("/hey"), "#02");
  464. Assert.AreEqual (Environment.CurrentDirectory, Path.GetFullPath ("."), "#03");
  465. Assert.AreEqual (Path.Combine (Environment.CurrentDirectory, "hey"),
  466. Path.GetFullPath ("hey"), "#04");
  467. }
  468. [Test]
  469. public void GetFullPath_Windows ()
  470. {
  471. if (!Windows)
  472. return;
  473. string root = "C:\\";
  474. string [,] test = new string [,] {
  475. {"root////././././././../root/././../root", "root"},
  476. {"root/", "root\\"},
  477. {"root/./", "root\\"},
  478. {"root/./", "root\\"},
  479. {"root/../", ""},
  480. {"root/../", ""},
  481. {"root/../..", ""},
  482. {"root/.hiddenfile", "root\\.hiddenfile"},
  483. {"root/. /", "root\\"},
  484. {"root/.. /", ""},
  485. {"root/..weirdname", "root\\..weirdname"},
  486. {"root/..", ""},
  487. {"root/../a/b/../../..", ""},
  488. {"root/./..", ""},
  489. {"..", ""},
  490. {".", ""},
  491. {"root//dir", "root\\dir"},
  492. {"root/. /", "root\\"},
  493. {"root/.. /", ""},
  494. #if !NET_2_0
  495. {"root/ . /", "root\\"},
  496. {"root/ .. /", ""},
  497. #endif
  498. {"root/./", "root\\"},
  499. {"root/.. /", ""},
  500. {".//", ""}
  501. };
  502. for (int i = 0; i < test.GetUpperBound (0); i++) {
  503. try {
  504. Assert.AreEqual (root + test [i, 1], Path.GetFullPath (root + test [i, 0]),
  505. String.Format ("GetFullPathWindows #{0}", i));
  506. } catch (Exception ex) {
  507. Assert.Fail (String.Format ("GetFullPathWindows #{0} (\"{1}\") failed: {2}",
  508. i, root + test [i, 0], ex.GetType ()));
  509. }
  510. }
  511. // UNC tests
  512. string root2 = @"\\server\share";
  513. root = @"\\server\share\";
  514. test = new string [,] {
  515. {"root////././././././../root/././../root", "root"},
  516. {"root/", "root\\"},
  517. {"root/./", "root\\"},
  518. {"root/./", "root\\"},
  519. {"root/../", ""},
  520. {"root/../", ""},
  521. {"root/../..", null},
  522. {"root/.hiddenfile", "root\\.hiddenfile"},
  523. {"root/. /", "root\\"},
  524. {"root/.. /", ""},
  525. {"root/..weirdname", "root\\..weirdname"},
  526. {"root/..", null},
  527. {"root/../a/b/../../..", null},
  528. {"root/./..", null},
  529. {"..", null},
  530. {".", null},
  531. {"root//dir", "root\\dir"},
  532. {"root/. /", "root\\"},
  533. {"root/.. /", ""},
  534. #if !NET_2_0
  535. {"root/ . /", "root\\"},
  536. {"root/ .. /", ""},
  537. #endif
  538. {"root/./", "root\\"},
  539. {"root/.. /", ""},
  540. {".//", ""}
  541. };
  542. for (int i = 0; i < test.GetUpperBound (0); i++) {
  543. // "null" means we have to compare against "root2"
  544. string res = test [i, 1] != null
  545. ? root + test [i, 1]
  546. : root2;
  547. try {
  548. Assert.AreEqual (res, Path.GetFullPath (root + test [i, 0]),
  549. String.Format ("GetFullPathWindows UNC #{0}", i));
  550. } catch (AssertionException) {
  551. throw;
  552. } catch (Exception ex) {
  553. Assert.Fail (String.Format ("GetFullPathWindows UNC #{0} (\"{1}\") failed: {2}",
  554. i, root + test [i, 0], ex.GetType ()));
  555. }
  556. }
  557. test = new string [,] {
  558. {"root////././././././../root/././../root", "root"},
  559. {"root/", "root\\"},
  560. {"root/./", "root\\"},
  561. {"root/./", "root\\"},
  562. {"root/../", ""},
  563. {"root/../", ""},
  564. {"root/../..", null},
  565. {"root/.hiddenfile", "root\\.hiddenfile"},
  566. {"root/. /", "root\\"},
  567. {"root/.. /", ""},
  568. {"root/..weirdname", "root\\..weirdname"},
  569. {"root/..", null},
  570. {"root/../a/b/../../..", null},
  571. {"root/./..", null},
  572. {"..", null},
  573. {".", null},
  574. {"root//dir", "root\\dir"},
  575. {"root/. /", "root\\"},
  576. {"root/.. /", ""},
  577. #if !NET_2_0
  578. {"root/ . /", "root\\"},
  579. {"root/ .. /", ""},
  580. #endif
  581. {"root/./", "root\\"},
  582. {"root/.. /", ""},
  583. {".//", ""}
  584. };
  585. string root3 = @"//server/share";
  586. root = @"//server/share/";
  587. bool needSlashConvert = Path.DirectorySeparatorChar != '/';
  588. for (int i = 0; i < test.GetUpperBound (0); i++) {
  589. // "null" means we have to compare against "root2"
  590. string res = test [i, 1] != null
  591. ? root + test [i, 1]
  592. : root3;
  593. if (needSlashConvert)
  594. res = res.Replace ('/', Path.DirectorySeparatorChar);
  595. try {
  596. Assert.AreEqual (res, Path.GetFullPath (root + test [i, 0]),
  597. String.Format ("GetFullPathWindows UNC[2] #{0}", i));
  598. } catch (AssertionException) {
  599. throw;
  600. } catch (Exception ex) {
  601. Assert.Fail (String.Format ("GetFullPathWindows UNC[2] #{0} (\"{1}\") failed: {2}",
  602. i, root + test [i, 0], ex.GetType ()));
  603. }
  604. }
  605. }
  606. [Test]
  607. public void GetFullPath_Path_Empty ()
  608. {
  609. try {
  610. Path.GetFullPath (String.Empty);
  611. Assert.Fail ("#1");
  612. } catch (ArgumentException ex) {
  613. // The path is not of a legal form
  614. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  615. Assert.IsNull (ex.InnerException, "#3");
  616. Assert.IsNotNull (ex.Message, "#4");
  617. Assert.IsNull (ex.ParamName, "#5");
  618. }
  619. }
  620. [Test]
  621. public void GetFullPath_Path_EndingSeparator ()
  622. {
  623. string fp = Path.GetFullPath ("something/");
  624. char end = fp [fp.Length - 1];
  625. Assert.IsTrue (end == Path.DirectorySeparatorChar);
  626. }
  627. [Test]
  628. public void GetFullPath_Path_InvalidPathChars ()
  629. {
  630. try {
  631. Path.GetFullPath ("hi\0world");
  632. Assert.Fail ("#1");
  633. } catch (ArgumentException ex) {
  634. // Illegal characters in path
  635. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  636. Assert.IsNull (ex.InnerException, "#3");
  637. Assert.IsNotNull (ex.Message, "#4");
  638. Assert.IsNull (ex.ParamName, "#5");
  639. }
  640. }
  641. [Test]
  642. public void GetFullPath_Path_Null ()
  643. {
  644. try {
  645. Path.GetFullPath (null);
  646. Assert.Fail ("#1");
  647. } catch (ArgumentNullException ex) {
  648. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  649. Assert.IsNull (ex.InnerException, "#3");
  650. Assert.IsNotNull (ex.Message, "#4");
  651. Assert.AreEqual ("path", ex.ParamName, "#5");
  652. }
  653. }
  654. [Test]
  655. public void GetFullPath_Path_Whitespace ()
  656. {
  657. try {
  658. Path.GetFullPath (" ");
  659. Assert.Fail ("#1");
  660. } catch (ArgumentException ex) {
  661. // The path is not of a legal form
  662. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  663. Assert.IsNull (ex.InnerException, "#3");
  664. Assert.IsNotNull (ex.Message, "#4");
  665. Assert.IsNull (ex.ParamName, "#5");
  666. }
  667. }
  668. [Test]
  669. public void GetFullPath2 ()
  670. {
  671. if (Windows) {
  672. Assert.AreEqual (@"Z:\", Path.GetFullPath ("Z:"), "GetFullPath w#01");
  673. #if !TARGET_JVM // Java full (canonical) path always starts with caps drive letter
  674. Assert.AreEqual (@"c:\abc\def", Path.GetFullPath (@"c:\abc\def"), "GetFullPath w#02");
  675. #endif
  676. Assert.IsTrue (Path.GetFullPath (@"\").EndsWith (@"\"), "GetFullPath w#03");
  677. // "\\\\" is not allowed
  678. Assert.IsTrue (Path.GetFullPath ("/").EndsWith (@"\"), "GetFullPath w#05");
  679. // "//" is not allowed
  680. Assert.IsTrue (Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"), "GetFullPath w#07");
  681. Assert.IsTrue (Path.GetFullPath ("c").EndsWith (@"\c"), "GetFullPath w#08");
  682. Assert.IsTrue (Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"), "GetFullPath w#09");
  683. Assert.IsTrue (Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"), "GetFullPath w#10");
  684. Assert.AreEqual (@"\\abc\def", Path.GetFullPath (@"\\abc\def"), "GetFullPath w#11");
  685. Assert.AreEqual (Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"), "GetFullPath w#12");
  686. Assert.AreEqual (Directory.GetCurrentDirectory ().Substring (0, 2) + @"\abc\def", Path.GetFullPath ("/abc/def"), "GetFullPath w#13");
  687. Assert.AreEqual (@"\\abc\def", Path.GetFullPath ("//abc/def"), "GetFullPath w#14");
  688. } else {
  689. Assert.AreEqual ("/", Path.GetFullPath ("/"), "#01");
  690. Assert.AreEqual ("/hey", Path.GetFullPath ("/hey"), "#02");
  691. Assert.AreEqual (Environment.CurrentDirectory, Path.GetFullPath ("."), "#03");
  692. Assert.AreEqual (Path.Combine (Environment.CurrentDirectory, "hey"),
  693. Path.GetFullPath ("hey"), "#04");
  694. }
  695. }
  696. [Test]
  697. public void GetPathRoot ()
  698. {
  699. string current;
  700. string expected;
  701. if (!Windows){
  702. current = Directory.GetCurrentDirectory ();
  703. expected = current [0].ToString ();
  704. } else {
  705. current = @"J:\Some\Strange Directory\Name";
  706. expected = "J:\\";
  707. }
  708. string pathRoot = Path.GetPathRoot (current);
  709. Assert.AreEqual (expected, pathRoot, "GetPathRoot #01");
  710. }
  711. [Test]
  712. public void GetPathRoot2 ()
  713. {
  714. // note: this method doesn't call Directory.GetCurrentDirectory so it can be
  715. // reused for partial trust unit tests in PathCas.cs
  716. string pathRoot;
  717. pathRoot = Path.GetPathRoot ("hola");
  718. Assert.AreEqual (String.Empty, pathRoot, "#A1");
  719. pathRoot = Path.GetPathRoot (null);
  720. Assert.AreEqual (null, pathRoot, "#A2");
  721. if (Windows) {
  722. Assert.AreEqual ("z:", Path.GetPathRoot ("z:"), "GetPathRoot w#01");
  723. Assert.AreEqual ("c:\\", Path.GetPathRoot ("c:\\abc\\def"), "GetPathRoot w#02");
  724. Assert.AreEqual ("\\", Path.GetPathRoot ("\\"), "GetPathRoot w#03");
  725. Assert.AreEqual ("\\\\", Path.GetPathRoot ("\\\\"), "GetPathRoot w#04");
  726. Assert.AreEqual ("\\", Path.GetPathRoot ("/"), "GetPathRoot w#05");
  727. Assert.AreEqual ("\\\\", Path.GetPathRoot ("//"), "GetPathRoot w#06");
  728. Assert.AreEqual (String.Empty, Path.GetPathRoot ("readme.txt"), "GetPathRoot w#07");
  729. Assert.AreEqual (String.Empty, Path.GetPathRoot ("c"), "GetPathRoot w#08");
  730. Assert.AreEqual (String.Empty, Path.GetPathRoot ("abc\\def"), "GetPathRoot w#09");
  731. Assert.AreEqual ("\\", Path.GetPathRoot ("\\abc\\def"), "GetPathRoot w#10");
  732. Assert.AreEqual ("\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"), "GetPathRoot w#11");
  733. Assert.AreEqual (String.Empty, Path.GetPathRoot ("abc//def"), "GetPathRoot w#12");
  734. Assert.AreEqual ("\\", Path.GetPathRoot ("/abc/def"), "GetPathRoot w#13");
  735. Assert.AreEqual ("\\\\abc\\def", Path.GetPathRoot ("//abc/def"), "GetPathRoot w#14");
  736. Assert.AreEqual (@"C:\", Path.GetPathRoot (@"C:\"), "GetPathRoot w#15");
  737. Assert.AreEqual (@"C:\", Path.GetPathRoot (@"C:\\"), "GetPathRoot w#16");
  738. Assert.AreEqual ("\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def\\ghi"), "GetPathRoot w#17");
  739. } else {
  740. // TODO: Same tests for Unix.
  741. }
  742. }
  743. [Test]
  744. public void GetPathRoot_Path_Empty ()
  745. {
  746. try {
  747. Path.GetPathRoot (String.Empty);
  748. Assert.Fail ("#1");
  749. } catch (ArgumentException ex) {
  750. // The path is not of a legal form
  751. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  752. Assert.IsNull (ex.InnerException, "#3");
  753. Assert.IsNotNull (ex.Message, "#4");
  754. Assert.IsNull (ex.ParamName, "#5");
  755. }
  756. }
  757. [Test]
  758. #if ONLY_1_1
  759. [Category ("NotWorking")] // we also throw ArgumentException on 1.0 profile
  760. #endif
  761. public void GetPathRoot_Path_InvalidPathChars ()
  762. {
  763. #if NET_2_0
  764. try {
  765. Path.GetPathRoot ("hi\0world");
  766. Assert.Fail ("#1");
  767. } catch (ArgumentException ex) {
  768. // Illegal characters in path
  769. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  770. Assert.IsNull (ex.InnerException, "#3");
  771. Assert.IsNotNull (ex.Message, "#4");
  772. Assert.IsNull (ex.ParamName, "#5");
  773. }
  774. #else
  775. Assert.AreEqual (String.Empty, Path.GetPathRoot ("hi\0world"));
  776. #endif
  777. }
  778. [Test]
  779. public void GetPathRoot_Path_Whitespace ()
  780. {
  781. try {
  782. Path.GetPathRoot (" ");
  783. Assert.Fail ("#1");
  784. } catch (ArgumentException ex) {
  785. // The path is not of a legal form
  786. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  787. Assert.IsNull (ex.InnerException, "#3");
  788. Assert.IsNotNull (ex.Message, "#4");
  789. Assert.IsNull (ex.ParamName, "#5");
  790. }
  791. }
  792. [Test]
  793. public void GetTempPath ()
  794. {
  795. string getTempPath = Path.GetTempPath ();
  796. Assert.IsTrue (getTempPath != String.Empty, "GetTempPath #01");
  797. Assert.IsTrue (Path.IsPathRooted (getTempPath), "GetTempPath #02");
  798. Assert.AreEqual (Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1], "GetTempPath #03");
  799. }
  800. [Test]
  801. public void GetTempFileName ()
  802. {
  803. string getTempFileName = null;
  804. try {
  805. getTempFileName = Path.GetTempFileName ();
  806. Assert.IsTrue (getTempFileName != String.Empty, "GetTempFileName #01");
  807. Assert.IsTrue (File.Exists (getTempFileName), "GetTempFileName #02");
  808. } finally {
  809. if (getTempFileName != null && getTempFileName != String.Empty){
  810. File.Delete (getTempFileName);
  811. }
  812. }
  813. }
  814. [Test]
  815. public void HasExtension ()
  816. {
  817. Assert.AreEqual (true, Path.HasExtension ("foo.txt"), "HasExtension #01");
  818. Assert.AreEqual (false, Path.HasExtension ("foo"), "HasExtension #02");
  819. Assert.AreEqual (true, Path.HasExtension (path1), "HasExtension #03");
  820. Assert.AreEqual (false, Path.HasExtension (path2), "HasExtension #04");
  821. Assert.AreEqual (false, Path.HasExtension (null), "HasExtension #05");
  822. Assert.AreEqual (false, Path.HasExtension (String.Empty), "HasExtension #06");
  823. Assert.AreEqual (false, Path.HasExtension (" "), "HasExtension #07");
  824. Assert.AreEqual (false, Path.HasExtension ("."), "HasExtension #08");
  825. Assert.AreEqual (false, Path.HasExtension ("end."), "HasExtension #09");
  826. Assert.AreEqual (true, Path.HasExtension (".start"), "HasExtension #10");
  827. Assert.AreEqual (true, Path.HasExtension (".a"), "HasExtension #11");
  828. Assert.AreEqual (false, Path.HasExtension ("a."), "HasExtension #12");
  829. Assert.AreEqual (false, Path.HasExtension ("Makefile"), "HasExtension #13");
  830. }
  831. [Test]
  832. public void HasExtension_Path_InvalidPathChars ()
  833. {
  834. try {
  835. Path.HasExtension ("hi\0world.txt");
  836. Assert.Fail ("#1");
  837. } catch (ArgumentException ex) {
  838. // Illegal characters in path
  839. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  840. Assert.IsNull (ex.InnerException, "#3");
  841. Assert.IsNotNull (ex.Message, "#4");
  842. Assert.IsNull (ex.ParamName, "#5");
  843. }
  844. }
  845. [Test]
  846. public void IsPathRooted ()
  847. {
  848. Assert.IsTrue (Path.IsPathRooted (path2), "IsPathRooted #01");
  849. Assert.IsTrue (!Path.IsPathRooted (path3), "IsPathRooted #02");
  850. Assert.IsTrue (!Path.IsPathRooted (null), "IsPathRooted #03");
  851. Assert.IsTrue (!Path.IsPathRooted (String.Empty), "IsPathRooted #04");
  852. Assert.IsTrue (!Path.IsPathRooted (" "), "IsPathRooted #05");
  853. Assert.IsTrue (Path.IsPathRooted ("/"), "IsPathRooted #06");
  854. Assert.IsTrue (Path.IsPathRooted ("//"), "IsPathRooted #07");
  855. Assert.IsTrue (!Path.IsPathRooted (":"), "IsPathRooted #08");
  856. if (Windows) {
  857. Assert.IsTrue (Path.IsPathRooted ("\\"), "IsPathRooted #09");
  858. Assert.IsTrue (Path.IsPathRooted ("\\\\"), "IsPathRooted #10");
  859. Assert.IsTrue (Path.IsPathRooted ("z:"), "IsPathRooted #11");
  860. Assert.IsTrue (Path.IsPathRooted ("z:\\"), "IsPathRooted #12");
  861. Assert.IsTrue (Path.IsPathRooted ("z:\\topdir"), "IsPathRooted #13");
  862. // This looks MS BUG. It is treated as absolute path
  863. Assert.IsTrue (Path.IsPathRooted ("z:curdir"), "IsPathRooted #14");
  864. Assert.IsTrue (Path.IsPathRooted ("\\abc\\def"), "IsPathRooted #15");
  865. } else {
  866. if (Environment.GetEnvironmentVariable ("MONO_IOMAP") == "all"){
  867. Assert.IsTrue (Path.IsPathRooted ("\\"), "IsPathRooted #16");
  868. Assert.IsTrue (Path.IsPathRooted ("\\\\"), "IsPathRooted #17");
  869. } else {
  870. Assert.IsTrue (!Path.IsPathRooted ("\\"), "IsPathRooted #09");
  871. Assert.IsTrue (!Path.IsPathRooted ("\\\\"), "IsPathRooted #10");
  872. Assert.IsTrue (!Path.IsPathRooted ("z:"), "IsPathRooted #11");
  873. }
  874. }
  875. }
  876. [Test]
  877. public void IsPathRooted_Path_Empty ()
  878. {
  879. Assert.IsTrue (!Path.IsPathRooted (String.Empty));
  880. }
  881. [Test]
  882. public void IsPathRooted_Path_InvalidPathChars ()
  883. {
  884. try {
  885. Path.IsPathRooted ("hi\0world");
  886. Assert.Fail ("#1");
  887. } catch (ArgumentException ex) {
  888. // Illegal characters in path.
  889. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  890. Assert.IsNull (ex.InnerException, "#3");
  891. Assert.IsNotNull (ex.Message, "#4");
  892. Assert.IsNull (ex.ParamName, "#5");
  893. }
  894. }
  895. [Test]
  896. public void IsPathRooted_Path_Null ()
  897. {
  898. Assert.IsTrue (!Path.IsPathRooted (null));
  899. }
  900. [Test]
  901. public void IsPathRooted_Path_Whitespace ()
  902. {
  903. Assert.IsTrue (!Path.IsPathRooted (" "));
  904. }
  905. [Test]
  906. public void CanonicalizeDots ()
  907. {
  908. string current = Path.GetFullPath (".");
  909. Assert.IsTrue (!current.EndsWith ("."), "TestCanonicalizeDotst #01");
  910. string parent = Path.GetFullPath ("..");
  911. Assert.IsTrue (!current.EndsWith (".."), "TestCanonicalizeDotst #02");
  912. }
  913. #if !MOBILE
  914. [Test]
  915. public void WindowsSystem32_76191 ()
  916. {
  917. // check for Unix platforms - see FAQ for more details
  918. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  919. int platform = (int) Environment.OSVersion.Platform;
  920. if ((platform == 4) || (platform == 128) || (platform == 6))
  921. return;
  922. string curdir = Directory.GetCurrentDirectory ();
  923. try {
  924. #if TARGET_JVM
  925. string system = "C:\\WINDOWS\\system32\\";
  926. #else
  927. string system = Environment.SystemDirectory;
  928. #endif
  929. Directory.SetCurrentDirectory (system);
  930. string drive = system.Substring (0, 2);
  931. Assert.AreEqual (system, Path.GetFullPath (drive), "current dir");
  932. }
  933. finally {
  934. Directory.SetCurrentDirectory (curdir);
  935. }
  936. }
  937. [Test]
  938. public void WindowsSystem32_77007 ()
  939. {
  940. // check for Unix platforms - see FAQ for more details
  941. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  942. int platform = (int) Environment.OSVersion.Platform;
  943. if ((platform == 4) || (platform == 128) || (platform == 6))
  944. return;
  945. string curdir = Directory.GetCurrentDirectory ();
  946. try {
  947. #if TARGET_JVM
  948. string system = "C:\\WINDOWS\\system32\\";
  949. #else
  950. string system = Environment.SystemDirectory;
  951. #endif
  952. Directory.SetCurrentDirectory (system);
  953. // e.g. C:dir (no backslash) will return CurrentDirectory + dir
  954. string dir = system.Substring (0, 2) + "dir";
  955. Assert.AreEqual (Path.Combine (system, "dir"), Path.GetFullPath (dir), "current dir");
  956. }
  957. finally {
  958. Directory.SetCurrentDirectory (curdir);
  959. }
  960. }
  961. #endif
  962. [Test]
  963. #if TARGET_JVM
  964. [Ignore("Java full (canonical) path always returns windows dir in caps")]
  965. #endif
  966. public void WindowsDriveC14N_77058 ()
  967. {
  968. // check for Unix platforms - see FAQ for more details
  969. // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
  970. int platform = (int) Environment.OSVersion.Platform;
  971. if ((platform == 4) || (platform == 128) || (platform == 6))
  972. return;
  973. Assert.AreEqual (@"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"), "1");
  974. Assert.AreEqual (@"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"), "2");
  975. Assert.AreEqual (@"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"), "3");
  976. Assert.AreEqual (@"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"), "4");
  977. Assert.AreEqual (@"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"), "5");
  978. }
  979. [Test]
  980. public void InvalidPathChars_Values ()
  981. {
  982. char[] invalid = Path.InvalidPathChars;
  983. if (Windows) {
  984. #if NET_2_0
  985. Assert.AreEqual (36, invalid.Length, "Length");
  986. #else
  987. Assert.AreEqual (15, invalid.Length, "Length");
  988. #endif
  989. foreach (char c in invalid) {
  990. int i = (int) c;
  991. #if NET_2_0
  992. if (i < 32)
  993. continue;
  994. #else
  995. if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
  996. continue;
  997. #endif
  998. // in both 1.1 SP1 and 2.0
  999. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  1000. continue;
  1001. Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  1002. }
  1003. } else {
  1004. foreach (char c in invalid) {
  1005. int i = (int) c;
  1006. if (i == 0)
  1007. continue;
  1008. Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  1009. }
  1010. }
  1011. }
  1012. [Test]
  1013. public void InvalidPathChars_Modify ()
  1014. {
  1015. char[] expected = Path.InvalidPathChars;
  1016. char[] invalid = Path.InvalidPathChars;
  1017. char original = invalid[0];
  1018. try {
  1019. invalid[0] = 'a';
  1020. // kind of scary
  1021. Assert.IsTrue (expected[0] == 'a', "expected");
  1022. Assert.AreEqual (expected[0], Path.InvalidPathChars[0], "readonly");
  1023. } finally {
  1024. invalid[0] = original;
  1025. }
  1026. }
  1027. #if NET_2_0
  1028. [Test]
  1029. public void GetInvalidFileNameChars_Values ()
  1030. {
  1031. char[] invalid = Path.GetInvalidFileNameChars ();
  1032. if (Windows) {
  1033. Assert.AreEqual (41, invalid.Length);
  1034. foreach (char c in invalid) {
  1035. int i = (int) c;
  1036. if (i < 32)
  1037. continue;
  1038. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  1039. continue;
  1040. // ':', '*', '?', '\', '/'
  1041. if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
  1042. continue;
  1043. Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  1044. }
  1045. } else {
  1046. foreach (char c in invalid) {
  1047. int i = (int) c;
  1048. // null or '/'
  1049. if ((i == 0) || (i == 47))
  1050. continue;
  1051. Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  1052. }
  1053. }
  1054. }
  1055. [Test]
  1056. public void GetInvalidFileNameChars_Modify ()
  1057. {
  1058. char[] expected = Path.GetInvalidFileNameChars ();
  1059. char[] invalid = Path.GetInvalidFileNameChars ();
  1060. invalid[0] = 'a';
  1061. Assert.IsTrue (expected[0] != 'a', "expected");
  1062. Assert.AreEqual (expected[0], Path.GetInvalidFileNameChars ()[0], "readonly");
  1063. }
  1064. [Test]
  1065. public void GetInvalidPathChars_Values ()
  1066. {
  1067. char[] invalid = Path.GetInvalidPathChars ();
  1068. if (Windows) {
  1069. Assert.AreEqual (36, invalid.Length);
  1070. foreach (char c in invalid) {
  1071. int i = (int) c;
  1072. if (i < 32)
  1073. continue;
  1074. if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
  1075. continue;
  1076. Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  1077. }
  1078. } else {
  1079. foreach (char c in invalid) {
  1080. int i = (int) c;
  1081. if (i == 0)
  1082. continue;
  1083. Assert.Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
  1084. }
  1085. }
  1086. }
  1087. [Test]
  1088. public void GetInvalidPathChars_Order ()
  1089. {
  1090. if (Windows) {
  1091. char [] invalid = Path.GetInvalidPathChars ();
  1092. char [] expected = new char [36] { '\x22', '\x3C', '\x3E', '\x7C', '\x00', '\x01', '\x02',
  1093. '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D',
  1094. '\x0E', '\x0F', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18',
  1095. '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F' };
  1096. Assert.AreEqual (expected.Length, invalid.Length);
  1097. for (int i = 0; i < expected.Length; i++ ) {
  1098. Assert.AreEqual (expected [i], invalid [i], "Character at position " + i);
  1099. }
  1100. }
  1101. }
  1102. [Test]
  1103. public void GetInvalidPathChars_Modify ()
  1104. {
  1105. char[] expected = Path.GetInvalidPathChars ();
  1106. char[] invalid = Path.GetInvalidPathChars ();
  1107. invalid[0] = 'a';
  1108. Assert.IsTrue (expected[0] != 'a', "expected");
  1109. Assert.AreEqual (expected[0], Path.GetInvalidPathChars ()[0], "readonly");
  1110. }
  1111. [Test]
  1112. public void GetRandomFileName ()
  1113. {
  1114. string s = Path.GetRandomFileName ();
  1115. Assert.AreEqual (12, s.Length, "Length");
  1116. char[] invalid = Path.GetInvalidFileNameChars ();
  1117. for (int i=0; i < s.Length; i++) {
  1118. if (i == 8)
  1119. Assert.AreEqual ('.', s[i], "8");
  1120. else
  1121. Assert.IsTrue (Array.IndexOf (invalid, s[i]) == -1, i.ToString ());
  1122. }
  1123. }
  1124. [Test]
  1125. public void GetRandomFileNameIsAlphaNumerical ()
  1126. {
  1127. string [] names = new string [1000];
  1128. for (int i = 0; i < names.Length; i++)
  1129. names [i] = Path.GetRandomFileName ();
  1130. foreach (string name in names) {
  1131. Assert.AreEqual (12, name.Length);
  1132. Assert.AreEqual ('.', name [8]);
  1133. for (int i = 0; i < 12; i++) {
  1134. if (i == 8)
  1135. continue;
  1136. char c = name [i];
  1137. Assert.IsTrue (('a' <= c && c <= 'z') || ('0' <= c && c <= '9'));
  1138. }
  1139. }
  1140. }
  1141. #endif
  1142. #if NET_4_0
  1143. string Concat (string sep, params string [] parms)
  1144. {
  1145. return String.Join (sep, parms);
  1146. }
  1147. [Test]
  1148. public void Combine_3Params ()
  1149. {
  1150. string sep = Path.DirectorySeparatorChar.ToString ();
  1151. try {
  1152. Path.Combine (null, "two", "three");
  1153. Assert.Fail ("#A1-1");
  1154. } catch {
  1155. // success
  1156. }
  1157. try {
  1158. Path.Combine ("one", null, "three");
  1159. Assert.Fail ("#A1-2");
  1160. } catch {
  1161. // success
  1162. }
  1163. try {
  1164. Path.Combine ("one", "two", null);
  1165. Assert.Fail ("#A1-3");
  1166. } catch {
  1167. // success
  1168. }
  1169. Assert.AreEqual (Concat (sep, "one", "two", "three"), Path.Combine ("one", "two", "three"), "#A2-1");
  1170. Assert.AreEqual (Concat (sep, sep + "one", "two", "three"), Path.Combine (sep + "one", "two", "three"), "#A2-2");
  1171. Assert.AreEqual (Concat (sep, sep + "one", "two", "three"), Path.Combine (sep + "one" + sep, "two", "three"), "#A2-3");
  1172. Assert.AreEqual (Concat (sep, sep + "two", "three"), Path.Combine (sep + "one" + sep, sep + "two", "three"), "#A2-4");
  1173. Assert.AreEqual (Concat (sep, sep + "three"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three"), "#A2-5");
  1174. Assert.AreEqual (Concat (sep, sep + "one" + sep, "two", "three"), Path.Combine (sep + "one" + sep + sep, "two", "three"), "#A3");
  1175. Assert.AreEqual ("", Path.Combine ("", "", ""), "#A4");
  1176. }
  1177. [Test]
  1178. public void Combine_4Params ()
  1179. {
  1180. string sep = Path.DirectorySeparatorChar.ToString ();
  1181. try {
  1182. Path.Combine (null, "two", "three", "four");
  1183. Assert.Fail ("#A1-1");
  1184. } catch {
  1185. // success
  1186. }
  1187. try {
  1188. Path.Combine ("one", null, "three", "four");
  1189. Assert.Fail ("#A1-2");
  1190. } catch {
  1191. // success
  1192. }
  1193. try {
  1194. Path.Combine ("one", "two", null, "four");
  1195. Assert.Fail ("#A1-3");
  1196. } catch {
  1197. // success
  1198. }
  1199. try {
  1200. Path.Combine ("one", "two", "three", null);
  1201. Assert.Fail ("#A1-4");
  1202. } catch {
  1203. // success
  1204. }
  1205. Assert.AreEqual (Concat (sep, "one", "two", "three", "four"), Path.Combine ("one", "two", "three", "four"), "#A2-1");
  1206. Assert.AreEqual (Concat (sep, sep + "one", "two", "three", "four"), Path.Combine (sep + "one", "two", "three", "four"), "#A2-2");
  1207. Assert.AreEqual (Concat (sep, sep + "one", "two", "three", "four"), Path.Combine (sep + "one" + sep, "two", "three", "four"), "#A2-3");
  1208. Assert.AreEqual (Concat (sep, sep + "two", "three", "four"), Path.Combine (sep + "one" + sep, sep + "two", "three", "four"), "#A2-4");
  1209. Assert.AreEqual (Concat (sep, sep + "three", "four"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", "four"), "#A2-5");
  1210. Assert.AreEqual (Concat (sep, sep + "four"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", sep + "four"), "#A2-6");
  1211. Assert.AreEqual (Concat (sep, sep + "one" + sep, "two", "three", "four"), Path.Combine (sep + "one" + sep + sep, "two", "three", "four"), "#A3");
  1212. Assert.AreEqual ("", Path.Combine ("", "", "", ""), "#A4");
  1213. }
  1214. [Test]
  1215. public void Combine_ManyParams ()
  1216. {
  1217. string sep = Path.DirectorySeparatorChar.ToString ();
  1218. try {
  1219. Path.Combine (null, "two", "three", "four", "five");
  1220. Assert.Fail ("#A1-1");
  1221. } catch {
  1222. // success
  1223. }
  1224. try {
  1225. Path.Combine ("one", null, "three", "four", "five");
  1226. Assert.Fail ("#A1-2");
  1227. } catch {
  1228. // success
  1229. }
  1230. try {
  1231. Path.Combine ("one", "two", null, "four", "five");
  1232. Assert.Fail ("#A1-3");
  1233. } catch {
  1234. // success
  1235. }
  1236. try {
  1237. Path.Combine ("one", "two", "three", null, "five");
  1238. Assert.Fail ("#A1-4");
  1239. } catch {
  1240. // success
  1241. }
  1242. try {
  1243. Path.Combine ("one", "two", "three", "four", null);
  1244. Assert.Fail ("#A1-5");
  1245. } catch {
  1246. // success
  1247. }
  1248. Assert.AreEqual (Concat (sep, "one", "two", "three", "four", "five"), Path.Combine ("one", "two", "three", "four", "five"), "#A2-1");
  1249. Assert.AreEqual (Concat (sep, sep + "one", "two", "three", "four", "five"), Path.Combine (sep + "one", "two", "three", "four", "five"), "#A2-2");
  1250. Assert.AreEqual (Concat (sep, sep + "one", "two", "three", "four", "five"), Path.Combine (sep + "one" + sep, "two", "three", "four", "five"), "#A2-3");
  1251. Assert.AreEqual (Concat (sep, sep + "two", "three", "four", "five"), Path.Combine (sep + "one" + sep, sep + "two", "three", "four", "five"), "#A2-4");
  1252. Assert.AreEqual (Concat (sep, sep + "three", "four", "five"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", "four", "five"), "#A2-5");
  1253. Assert.AreEqual (Concat (sep, sep + "four", "five"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", sep + "four", "five"), "#A2-6");
  1254. Assert.AreEqual (Concat (sep, sep + "five"), Path.Combine (sep + "one" + sep, sep + "two", sep + "three", sep + "four", sep + "five"), "#A2-6");
  1255. Assert.AreEqual (Concat (sep, sep + "one" + sep, "two", "three", "four", "five"), Path.Combine (sep + "one" + sep + sep, "two", "three", "four", "five"), "#A3");
  1256. Assert.AreEqual ("", Path.Combine ("", "", "", "", ""), "#A4");
  1257. }
  1258. #endif
  1259. }
  1260. }