FileTest.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. //
  2. // FileTest.cs: Test cases for System.IO.File
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. // Ville Palo ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. http://www.ximian.com
  9. //
  10. // TODO: Find out why ArgumentOutOfRangeExceptions does not manage to close streams properly
  11. //
  12. using NUnit.Framework;
  13. using System;
  14. using System.IO;
  15. using System.Globalization;
  16. using System.Threading;
  17. namespace MonoTests.System.IO
  18. {
  19. [TestFixture]
  20. public class FileTest : Assertion
  21. {
  22. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  23. [SetUp]
  24. public void SetUp ()
  25. {
  26. if (Directory.Exists (TempFolder))
  27. Directory.Delete (TempFolder, true);
  28. Directory.CreateDirectory (TempFolder);
  29. Thread.CurrentThread.CurrentCulture = new CultureInfo ("EN-us");
  30. }
  31. [TearDown]
  32. public void TearDown ()
  33. {
  34. if (Directory.Exists (TempFolder))
  35. Directory.Delete (TempFolder, true);
  36. }
  37. [Test]
  38. public void TestExists ()
  39. {
  40. int i = 0;
  41. FileStream s = null;
  42. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  43. try {
  44. Assert ("null filename should not exist", !File.Exists (null));
  45. i++;
  46. Assert ("empty filename should not exist", !File.Exists (""));
  47. i++;
  48. Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
  49. i++;
  50. DeleteFile (path);
  51. s = File.Create (path);
  52. s.Close ();
  53. Assert ("File " + path + " should exists", File.Exists (path));
  54. i++;
  55. Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"));
  56. } catch (Exception e) {
  57. Fail ("Unexpected exception at i = " + i + ". e=" + e);
  58. } finally {
  59. if (s != null)
  60. s.Close ();
  61. DeleteFile (path);
  62. }
  63. }
  64. [Test]
  65. public void Exists_InvalidFileName ()
  66. {
  67. Assert ("><|", !File.Exists ("><|"));
  68. Assert ("?*", !File.Exists ("?*"));
  69. }
  70. [Test]
  71. public void Exists_InvalidDirectory ()
  72. {
  73. Assert ("InvalidDirectory", !File.Exists (Path.Combine ("does not exist", "file.txt")));
  74. }
  75. [Test]
  76. [ExpectedException(typeof (ArgumentNullException))]
  77. public void CtorArgumentNullException1 ()
  78. {
  79. FileStream stream = File.Create (null);
  80. }
  81. [Test]
  82. [ExpectedException(typeof (ArgumentException))]
  83. public void CtorArgumentException1 ()
  84. {
  85. FileStream stream = File.Create ("");
  86. }
  87. [Test]
  88. [ExpectedException(typeof (ArgumentException))]
  89. public void CtorArgumentException2 ()
  90. {
  91. FileStream stream = File.Create (" ");
  92. }
  93. [Test]
  94. [ExpectedException(typeof (DirectoryNotFoundException))]
  95. public void CtorDirectoryNotFoundException ()
  96. {
  97. FileStream stream = null;
  98. string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
  99. try {
  100. stream = File.Create (path);
  101. } finally {
  102. if (stream != null)
  103. stream.Close ();
  104. DeleteFile (path);
  105. }
  106. }
  107. [Test]
  108. public void TestCreate ()
  109. {
  110. FileStream stream = null;
  111. string path = "";
  112. /* positive test: create resources/foo */
  113. try {
  114. path = TempFolder + Path.DirectorySeparatorChar + "foo";
  115. stream = File.Create (path);
  116. Assert ("File should exist", File.Exists (path));
  117. stream.Close ();
  118. } catch (Exception e) {
  119. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  120. } finally {
  121. if (stream != null)
  122. stream.Close ();
  123. DeleteFile (path);
  124. }
  125. path = "";
  126. stream = null;
  127. /* positive test: repeat test above again to test for overwriting file */
  128. try {
  129. path = TempFolder + Path.DirectorySeparatorChar + "foo";
  130. stream = File.Create (path);
  131. Assert ("File should exist", File.Exists (path));
  132. stream.Close ();
  133. } catch (Exception e) {
  134. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  135. } finally {
  136. if (stream != null)
  137. stream.Close ();
  138. DeleteFile (path);
  139. }
  140. }
  141. [Test]
  142. [ExpectedException(typeof(ArgumentNullException))]
  143. public void CopyArgumentNullException1 ()
  144. {
  145. File.Copy (null, "b");
  146. }
  147. [Test]
  148. [ExpectedException(typeof(ArgumentNullException))]
  149. public void CopyArgumentNullException2 ()
  150. {
  151. File.Copy ("a", null);
  152. }
  153. [Test]
  154. [ExpectedException(typeof(ArgumentException))]
  155. public void CopyArgumentException1 ()
  156. {
  157. File.Copy ("", "b");
  158. }
  159. [Test]
  160. [ExpectedException(typeof(ArgumentException))]
  161. public void CopyArgumentException2 ()
  162. {
  163. File.Copy ("a", "");
  164. }
  165. [Test]
  166. [ExpectedException(typeof(ArgumentException))]
  167. public void CopyArgumentException3 ()
  168. {
  169. File.Copy (" ", "b");
  170. }
  171. [Test]
  172. [ExpectedException(typeof(ArgumentException))]
  173. public void CopyArgumentException4 ()
  174. {
  175. File.Copy ("a", " ");
  176. }
  177. [Test]
  178. [ExpectedException(typeof(FileNotFoundException))]
  179. public void CopyFileNotFoundException ()
  180. {
  181. File.Copy ("doesnotexist", "b");
  182. }
  183. [ExpectedException(typeof(IOException))]
  184. public void CopyIOException ()
  185. {
  186. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
  187. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  188. try {
  189. File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
  190. File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
  191. File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
  192. } finally {
  193. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
  194. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  195. }
  196. }
  197. [Test]
  198. public void TestCopy ()
  199. {
  200. string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
  201. string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  202. /* positive test: copy resources/AFile.txt to resources/bar */
  203. try {
  204. try {
  205. DeleteFile (path1);
  206. DeleteFile (path2);
  207. File.Create (path2).Close ();
  208. File.Copy (path2, path1);
  209. Assert ("File AFile.txt should still exist", File.Exists (path2));
  210. Assert ("File bar should exist after File.Copy", File.Exists (path1));
  211. } catch (Exception e) {
  212. Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
  213. }
  214. /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
  215. try {
  216. Assert ("File bar should exist before File.Copy", File.Exists (path1));
  217. File.Copy (path2, path1, true);
  218. Assert ("File AFile.txt should still exist", File.Exists (path2));
  219. Assert ("File bar should exist after File.Copy", File.Exists (path1));
  220. } catch (Exception e) {
  221. Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
  222. }
  223. }finally {
  224. DeleteFile (path1);
  225. DeleteFile (path2);
  226. }
  227. }
  228. [Test]
  229. [ExpectedException (typeof (ArgumentNullException))]
  230. public void DeleteArgumentNullException ()
  231. {
  232. File.Delete (null);
  233. }
  234. [Test]
  235. [ExpectedException (typeof (ArgumentException))]
  236. public void DeleteArgumentException1 ()
  237. {
  238. File.Delete ("");
  239. }
  240. [Test]
  241. [ExpectedException (typeof (ArgumentException))]
  242. public void DeleteArgumentException2 ()
  243. {
  244. File.Delete (" ");
  245. }
  246. [Test]
  247. [ExpectedException (typeof (DirectoryNotFoundException))]
  248. public void DeleteDirectoryNotFoundException ()
  249. {
  250. string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
  251. if (Directory.Exists (path))
  252. Directory.Delete (path, true);
  253. File.Delete (path);
  254. }
  255. [Test]
  256. public void TestDelete ()
  257. {
  258. string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
  259. DeleteFile (foopath);
  260. try {
  261. File.Create (foopath).Close ();
  262. try {
  263. File.Delete (foopath);
  264. } catch (Exception e) {
  265. Fail ("Unable to delete " + foopath + " e=" + e.ToString());
  266. }
  267. Assert ("File " + foopath + " should not exist after File.Delete", !File.Exists (foopath));
  268. } finally {
  269. DeleteFile (foopath);
  270. }
  271. }
  272. [Test]
  273. [ExpectedException(typeof (IOException))]
  274. [Category("NotWorking")]
  275. public void DeleteOpenStreamException ()
  276. {
  277. string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
  278. DeleteFile (path);
  279. FileStream stream = null;
  280. try {
  281. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  282. File.Delete (path);
  283. } finally {
  284. if (stream != null)
  285. stream.Close ();
  286. DeleteFile (path);
  287. }
  288. }
  289. [Test]
  290. [ExpectedException(typeof (ArgumentNullException))]
  291. public void MoveException1 ()
  292. {
  293. File.Move (null, "b");
  294. }
  295. [Test]
  296. [ExpectedException(typeof (ArgumentNullException))]
  297. public void MoveException2 ()
  298. {
  299. File.Move ("a", null);
  300. }
  301. [Test]
  302. [ExpectedException(typeof (ArgumentException))]
  303. public void MoveException3 ()
  304. {
  305. File.Move ("", "b");
  306. }
  307. [Test]
  308. [ExpectedException(typeof (ArgumentException))]
  309. public void MoveException4 ()
  310. {
  311. File.Move ("a", "");
  312. }
  313. [Test]
  314. [ExpectedException(typeof (ArgumentException))]
  315. public void MoveException5 ()
  316. {
  317. File.Move (" ", "b");
  318. }
  319. [Test]
  320. [ExpectedException(typeof (ArgumentException))]
  321. public void MoveException6 ()
  322. {
  323. File.Move ("a", " ");
  324. }
  325. [Test]
  326. [ExpectedException(typeof (FileNotFoundException))]
  327. public void MoveException7 ()
  328. {
  329. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist");
  330. File.Move (TempFolder + Path.DirectorySeparatorChar + "doesnotexist", "b");
  331. }
  332. [Test]
  333. [ExpectedException(typeof (DirectoryNotFoundException))]
  334. public void MoveException8 ()
  335. {
  336. string path = TempFolder + Path.DirectorySeparatorChar + "foo";
  337. DeleteFile (path);
  338. try {
  339. File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
  340. File.Copy(TempFolder + Path.DirectorySeparatorChar + "AFile.txt", path, true);
  341. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
  342. File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
  343. } finally {
  344. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  345. DeleteFile (path);
  346. }
  347. }
  348. [Test]
  349. [ExpectedException(typeof (IOException))]
  350. public void MoveException9 ()
  351. {
  352. File.Create (TempFolder + Path.DirectorySeparatorChar + "foo").Close ();
  353. try {
  354. File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder);
  355. } finally {
  356. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "foo");
  357. }
  358. }
  359. [Test]
  360. public void TestMove ()
  361. {
  362. string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
  363. string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
  364. if (!File.Exists (bar)) {
  365. FileStream f = File.Create(bar);
  366. f.Close();
  367. }
  368. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should exist", File.Exists (bar));
  369. File.Move (bar, baz);
  370. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists (bar));
  371. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "baz should exist", File.Exists (baz));
  372. // Test moving of directories
  373. string dir = Path.Combine (TempFolder, "dir");
  374. string dir2 = Path.Combine (TempFolder, "dir2");
  375. string dir_foo = Path.Combine (dir, "foo");
  376. string dir2_foo = Path.Combine (dir2, "foo");
  377. if (Directory.Exists (dir))
  378. Directory.Delete (dir, true);
  379. Directory.CreateDirectory (dir);
  380. Directory.CreateDirectory (dir2);
  381. File.Create (dir_foo).Close ();
  382. File.Move (dir_foo, dir2_foo);
  383. Assert (File.Exists (dir2_foo));
  384. Directory.Delete (dir, true);
  385. Directory.Delete (dir2, true);
  386. DeleteFile (dir_foo);
  387. DeleteFile (dir2_foo);
  388. }
  389. [Test]
  390. public void TestOpen ()
  391. {
  392. string path = "";
  393. FileStream stream = null;
  394. try {
  395. path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  396. if (!File.Exists (path))
  397. stream = File.Create (path);
  398. stream.Close ();
  399. stream = File.Open (path, FileMode.Open);
  400. stream.Close ();
  401. } catch (Exception e) {
  402. Fail ("Unable to open " + TempFolder + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
  403. } finally {
  404. if (stream != null)
  405. stream.Close ();
  406. DeleteFile (path);
  407. }
  408. path = "";
  409. stream = null;
  410. /* Exception tests */
  411. try {
  412. path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
  413. stream = File.Open (path, FileMode.Open);
  414. Fail ("File 'filedoesnotexist' should not exist");
  415. } catch (FileNotFoundException) {
  416. // do nothing, this is what we expect
  417. } catch (Exception e) {
  418. Fail ("Unexpect exception caught: e=" + e.ToString());
  419. } finally {
  420. if (stream != null)
  421. stream.Close ();
  422. DeleteFile (path);
  423. }
  424. }
  425. [Test]
  426. public void Open ()
  427. {
  428. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  429. if (!File.Exists (path))
  430. File.Create (path).Close ();
  431. FileStream stream = null;
  432. try {
  433. stream = File.Open (path, FileMode.Open);
  434. Assertion.AssertEquals ("test#01", true, stream.CanRead);
  435. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  436. Assertion.AssertEquals ("test#03", true, stream.CanWrite);
  437. stream.Close ();
  438. stream = File.Open (path, FileMode.Open, FileAccess.Write);
  439. Assertion.AssertEquals ("test#04", false, stream.CanRead);
  440. Assertion.AssertEquals ("test#05", true, stream.CanSeek);
  441. Assertion.AssertEquals ("test#06", true, stream.CanWrite);
  442. stream.Close ();
  443. stream = File.Open (path, FileMode.Open, FileAccess.Read);
  444. Assertion.AssertEquals ("test#04", true, stream.CanRead);
  445. Assertion.AssertEquals ("test#05", true, stream.CanSeek);
  446. Assertion.AssertEquals ("test#06", false, stream.CanWrite);
  447. stream.Close ();
  448. } finally {
  449. if (stream != null)
  450. stream.Close ();
  451. DeleteFile (path);
  452. }
  453. }
  454. [Test]
  455. [ExpectedException(typeof(ArgumentException))]
  456. public void OpenException1 ()
  457. {
  458. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  459. FileStream stream = null;
  460. // CreateNew + Read throws an exceptoin
  461. try {
  462. stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
  463. } finally {
  464. if (stream != null)
  465. stream.Close ();
  466. DeleteFile (path);
  467. }
  468. }
  469. [Test]
  470. [ExpectedException(typeof(ArgumentException))]
  471. public void OpenException2 ()
  472. {
  473. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  474. FileStream s = null;
  475. // Append + Read throws an exceptoin
  476. if (!File.Exists (path))
  477. File.Create (path).Close ();
  478. try {
  479. s = File.Open (path, FileMode.Append, FileAccess.Read);
  480. } finally {
  481. if (s != null)
  482. s.Close ();
  483. DeleteFile (path);
  484. }
  485. }
  486. [Test]
  487. public void OpenRead ()
  488. {
  489. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  490. if (!File.Exists (path))
  491. File.Create (path).Close ();
  492. FileStream stream = null;
  493. try {
  494. stream = File.OpenRead (path);
  495. Assertion.AssertEquals ("test#01", true, stream.CanRead);
  496. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  497. Assertion.AssertEquals ("test#03", false, stream.CanWrite);
  498. } finally {
  499. if (stream != null)
  500. stream.Close ();
  501. DeleteFile (path);
  502. }
  503. }
  504. [Test]
  505. public void OpenWrite ()
  506. {
  507. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  508. if (!File.Exists (path))
  509. File.Create (path).Close ();
  510. FileStream stream = null;
  511. try {
  512. stream = File.OpenWrite (path);
  513. Assertion.AssertEquals ("test#01", false, stream.CanRead);
  514. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  515. Assertion.AssertEquals ("test#03", true, stream.CanWrite);
  516. stream.Close ();
  517. } finally {
  518. if (stream != null)
  519. stream.Close ();
  520. DeleteFile (path);
  521. }
  522. }
  523. [Test]
  524. public void TestGetCreationTime ()
  525. {
  526. string path = TempFolder + Path.DirectorySeparatorChar + "baz";
  527. DeleteFile (path);
  528. try {
  529. File.Create (path).Close();
  530. DateTime time = File.GetCreationTime (path);
  531. Assert ("GetCreationTime incorrect", (DateTime.Now - time).TotalSeconds < 10);
  532. } finally {
  533. DeleteFile (path);
  534. }
  535. }
  536. [Test]
  537. [ExpectedException(typeof(IOException))]
  538. public void TestGetCreationTimeException ()
  539. {
  540. // Test nonexistent files
  541. string path2 = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
  542. DeleteFile (path2);
  543. // should throw an exception
  544. File.GetCreationTime (path2);
  545. }
  546. // Setting the creation time on Unix is not possible
  547. [Test]
  548. public void CreationTime ()
  549. {
  550. int platform = (int) Environment.OSVersion.Platform;
  551. if ((platform != 4) && (platform != 128))
  552. return;
  553. string path = TempFolder + Path.DirectorySeparatorChar + "creationTime";
  554. if (File.Exists (path))
  555. File.Delete (path);
  556. FileStream stream = null;
  557. try {
  558. stream = File.Create (path);
  559. stream.Close ();
  560. File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  561. DateTime time = File.GetCreationTime (path);
  562. Assertion.AssertEquals ("test#01", 2002, time.Year);
  563. Assertion.AssertEquals ("test#02", 4, time.Month);
  564. Assertion.AssertEquals ("test#03", 6, time.Day);
  565. Assertion.AssertEquals ("test#04", 4, time.Hour);
  566. Assertion.AssertEquals ("test#05", 4, time.Second);
  567. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
  568. Assertion.AssertEquals ("test#06", 2002, time.Year);
  569. Assertion.AssertEquals ("test#07", 4, time.Month);
  570. Assertion.AssertEquals ("test#08", 6, time.Day);
  571. Assertion.AssertEquals ("test#09", 4, time.Hour);
  572. Assertion.AssertEquals ("test#10", 4, time.Second);
  573. File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  574. time = File.GetCreationTimeUtc (path);
  575. Assertion.AssertEquals ("test#11", 2002, time.Year);
  576. Assertion.AssertEquals ("test#12", 4, time.Month);
  577. Assertion.AssertEquals ("test#13", 6, time.Day);
  578. Assertion.AssertEquals ("test#14", 4, time.Hour);
  579. Assertion.AssertEquals ("test#15", 4, time.Second);
  580. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
  581. Assertion.AssertEquals ("test#16", 2002, time.Year);
  582. Assertion.AssertEquals ("test#17", 4, time.Month);
  583. Assertion.AssertEquals ("test#18", 6, time.Day);
  584. Assertion.AssertEquals ("test#19", 4, time.Hour);
  585. Assertion.AssertEquals ("test#20", 4, time.Second);
  586. } finally {
  587. if (stream != null)
  588. stream.Close ();
  589. DeleteFile (path);
  590. }
  591. }
  592. [Test]
  593. public void LastAccessTime ()
  594. {
  595. string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
  596. if (File.Exists (path))
  597. File.Delete (path);
  598. FileStream stream = null;
  599. try {
  600. stream = File.Create (path);
  601. stream.Close ();
  602. File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  603. DateTime time = File.GetLastAccessTime (path);
  604. Assertion.AssertEquals ("test#01", 2002, time.Year);
  605. Assertion.AssertEquals ("test#02", 4, time.Month);
  606. Assertion.AssertEquals ("test#03", 6, time.Day);
  607. Assertion.AssertEquals ("test#04", 4, time.Hour);
  608. Assertion.AssertEquals ("test#05", 4, time.Second);
  609. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
  610. Assertion.AssertEquals ("test#06", 2002, time.Year);
  611. Assertion.AssertEquals ("test#07", 4, time.Month);
  612. Assertion.AssertEquals ("test#08", 6, time.Day);
  613. Assertion.AssertEquals ("test#09", 4, time.Hour);
  614. Assertion.AssertEquals ("test#10", 4, time.Second);
  615. File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  616. time = File.GetLastAccessTimeUtc (path);
  617. Assertion.AssertEquals ("test#11", 2002, time.Year);
  618. Assertion.AssertEquals ("test#12", 4, time.Month);
  619. Assertion.AssertEquals ("test#13", 6, time.Day);
  620. Assertion.AssertEquals ("test#14", 4, time.Hour);
  621. Assertion.AssertEquals ("test#15", 4, time.Second);
  622. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
  623. Assertion.AssertEquals ("test#16", 2002, time.Year);
  624. Assertion.AssertEquals ("test#17", 4, time.Month);
  625. Assertion.AssertEquals ("test#18", 6, time.Day);
  626. Assertion.AssertEquals ("test#19", 4, time.Hour);
  627. Assertion.AssertEquals ("test#20", 4, time.Second);
  628. } finally {
  629. if (stream != null)
  630. stream.Close ();
  631. DeleteFile (path);
  632. }
  633. }
  634. [Test]
  635. public void LastWriteTime ()
  636. {
  637. string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
  638. if (File.Exists (path))
  639. File.Delete (path);
  640. FileStream stream = null;
  641. try {
  642. stream = File.Create (path);
  643. stream.Close ();
  644. File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  645. DateTime time = File.GetLastWriteTime (path);
  646. Assertion.AssertEquals ("test#01", 2002, time.Year);
  647. Assertion.AssertEquals ("test#02", 4, time.Month);
  648. Assertion.AssertEquals ("test#03", 6, time.Day);
  649. Assertion.AssertEquals ("test#04", 4, time.Hour);
  650. Assertion.AssertEquals ("test#05", 4, time.Second);
  651. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
  652. Assertion.AssertEquals ("test#06", 2002, time.Year);
  653. Assertion.AssertEquals ("test#07", 4, time.Month);
  654. Assertion.AssertEquals ("test#08", 6, time.Day);
  655. Assertion.AssertEquals ("test#09", 4, time.Hour);
  656. Assertion.AssertEquals ("test#10", 4, time.Second);
  657. File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  658. time = File.GetLastWriteTimeUtc (path);
  659. Assertion.AssertEquals ("test#11", 2002, time.Year);
  660. Assertion.AssertEquals ("test#12", 4, time.Month);
  661. Assertion.AssertEquals ("test#13", 6, time.Day);
  662. Assertion.AssertEquals ("test#14", 4, time.Hour);
  663. Assertion.AssertEquals ("test#15", 4, time.Second);
  664. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
  665. Assertion.AssertEquals ("test#16", 2002, time.Year);
  666. Assertion.AssertEquals ("test#17", 4, time.Month);
  667. Assertion.AssertEquals ("test#18", 6, time.Day);
  668. Assertion.AssertEquals ("test#19", 4, time.Hour);
  669. Assertion.AssertEquals ("test#20", 4, time.Second);
  670. } finally {
  671. if (stream != null)
  672. stream.Close ();
  673. DeleteFile (path);
  674. }
  675. }
  676. [Test]
  677. [ExpectedException(typeof(ArgumentNullException))]
  678. public void GetCreationTimeException1 ()
  679. {
  680. File.GetCreationTime (null as string);
  681. }
  682. [Test]
  683. [ExpectedException(typeof(ArgumentException))]
  684. public void GetCreationTimeException2 ()
  685. {
  686. File.GetCreationTime ("");
  687. }
  688. [Test]
  689. [ExpectedException(typeof(IOException))]
  690. public void GetCreationTimeException3 ()
  691. {
  692. string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
  693. DeleteFile (path);
  694. File.GetCreationTime (path);
  695. }
  696. [Test]
  697. [ExpectedException(typeof(ArgumentException))]
  698. public void GetCreationTimeException4 ()
  699. {
  700. File.GetCreationTime (" ");
  701. }
  702. [Test]
  703. [ExpectedException(typeof(ArgumentException))]
  704. public void GetCreationTimeException5 ()
  705. {
  706. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  707. }
  708. [Test]
  709. [ExpectedException(typeof(ArgumentNullException))]
  710. public void GetCreationTimeUtcException1 ()
  711. {
  712. File.GetCreationTimeUtc (null as string);
  713. }
  714. [Test]
  715. [ExpectedException(typeof(ArgumentException))]
  716. public void GetCreationTimeUtcException2 ()
  717. {
  718. File.GetCreationTimeUtc ("");
  719. }
  720. [Test]
  721. [ExpectedException(typeof(IOException))]
  722. public void GetCreationTimeUtcException3 ()
  723. {
  724. string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
  725. DeleteFile (path);
  726. File.GetCreationTimeUtc (path);
  727. }
  728. [Test]
  729. [ExpectedException(typeof(ArgumentException))]
  730. public void GetCreationTimeUtcException4 ()
  731. {
  732. File.GetCreationTimeUtc (" ");
  733. }
  734. [Test]
  735. [ExpectedException(typeof(ArgumentException))]
  736. public void GetCreationTimeUtcException5 ()
  737. {
  738. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  739. }
  740. [Test]
  741. [ExpectedException(typeof(ArgumentNullException))]
  742. public void GetLastAccessTimeException1 ()
  743. {
  744. File.GetLastAccessTime (null as string);
  745. }
  746. [Test]
  747. [ExpectedException(typeof(ArgumentException))]
  748. public void GetLastAccessTimeException2 ()
  749. {
  750. File.GetLastAccessTime ("");
  751. }
  752. [Test]
  753. [ExpectedException(typeof(IOException))]
  754. public void GetLastAccessTimeException3 ()
  755. {
  756. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
  757. DeleteFile (path);
  758. File.GetLastAccessTime (path);
  759. }
  760. [Test]
  761. [ExpectedException(typeof(ArgumentException))]
  762. public void GetLastAccessTimeException4 ()
  763. {
  764. File.GetLastAccessTime (" ");
  765. }
  766. [Test]
  767. [ExpectedException(typeof(ArgumentException))]
  768. public void GetLastAccessTimeException5 ()
  769. {
  770. File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
  771. }
  772. [Test]
  773. [ExpectedException(typeof(ArgumentNullException))]
  774. public void GetLastAccessTimeUtcException1 ()
  775. {
  776. File.GetLastAccessTimeUtc (null as string);
  777. }
  778. [Test]
  779. [ExpectedException(typeof(ArgumentException))]
  780. public void GetLastAccessTimeUtcException2 ()
  781. {
  782. File.GetLastAccessTimeUtc ("");
  783. }
  784. [Test]
  785. [ExpectedException(typeof(IOException))]
  786. public void GetLastAccessTimeUtcException3 ()
  787. {
  788. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  789. DeleteFile (path);
  790. File.GetLastAccessTimeUtc (path);
  791. }
  792. [Test]
  793. [ExpectedException(typeof(ArgumentException))]
  794. public void GetLastAccessTimeUtcException4 ()
  795. {
  796. File.GetLastAccessTimeUtc (" ");
  797. }
  798. [Test]
  799. [ExpectedException(typeof(ArgumentException))]
  800. public void GetLastAccessTimeUtcException5 ()
  801. {
  802. File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  803. }
  804. [Test]
  805. [ExpectedException(typeof(ArgumentNullException))]
  806. public void GetLastWriteTimeException1 ()
  807. {
  808. File.GetLastWriteTime (null as string);
  809. }
  810. [Test]
  811. [ExpectedException(typeof(ArgumentException))]
  812. public void GetLastWriteTimeException2 ()
  813. {
  814. File.GetLastWriteTime ("");
  815. }
  816. [Test]
  817. [ExpectedException(typeof(IOException))]
  818. public void GetLastWriteTimeException3 ()
  819. {
  820. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  821. DeleteFile (path);
  822. File.GetLastWriteTime (path);
  823. }
  824. [Test]
  825. [ExpectedException(typeof(ArgumentException))]
  826. public void GetLastWriteTimeException4 ()
  827. {
  828. File.GetLastWriteTime (" ");
  829. }
  830. [Test]
  831. [ExpectedException(typeof(ArgumentException))]
  832. public void GetLastWriteTimeException5 ()
  833. {
  834. File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
  835. }
  836. [Test]
  837. [ExpectedException(typeof(ArgumentNullException))]
  838. public void GetLastWriteTimeUtcException1 ()
  839. {
  840. File.GetLastWriteTimeUtc (null as string);
  841. }
  842. [Test]
  843. [ExpectedException(typeof(ArgumentException))]
  844. public void GetLastWriteTimeUtcException2 ()
  845. {
  846. File.GetLastAccessTimeUtc ("");
  847. }
  848. [Test]
  849. [ExpectedException(typeof(IOException))]
  850. public void GetLastWriteTimeUtcException3 ()
  851. {
  852. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
  853. DeleteFile (path);
  854. File.GetLastAccessTimeUtc (path);
  855. }
  856. [Test]
  857. [ExpectedException(typeof(ArgumentException))]
  858. public void GetLastWriteTimeUtcException4 ()
  859. {
  860. File.GetLastAccessTimeUtc (" ");
  861. }
  862. [Test]
  863. [ExpectedException(typeof(ArgumentException))]
  864. public void GetLastWriteTimeUtcException5 ()
  865. {
  866. File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  867. }
  868. [Test]
  869. public void FileStreamClose ()
  870. {
  871. string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
  872. FileStream stream = null;
  873. try {
  874. stream = File.Create (path);
  875. stream.Close ();
  876. File.Delete (path);
  877. } finally {
  878. if (stream != null)
  879. stream.Close ();
  880. DeleteFile (path);
  881. }
  882. }
  883. // SetCreationTime and SetCreationTimeUtc exceptions
  884. [Test]
  885. [ExpectedException(typeof (ArgumentNullException))]
  886. public void SetCreationTimeArgumentNullException1 ()
  887. {
  888. File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  889. }
  890. [Test]
  891. [ExpectedException(typeof (ArgumentException))]
  892. public void SetCreationTimeArgumenException1 ()
  893. {
  894. File.SetCreationTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  895. }
  896. [Test]
  897. [ExpectedException(typeof (ArgumentException))]
  898. public void SetCreationTimeArgumenException2 ()
  899. {
  900. File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  901. }
  902. [Test]
  903. // On Unix there are no invalid path chars.
  904. public void SetCreationTimeArgumenException3 ()
  905. {
  906. if (Path.InvalidPathChars.Length > 1) {
  907. bool pass = false;
  908. try {
  909. File.SetCreationTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  910. } catch (ArgumentException) {
  911. pass = true;
  912. }
  913. Assertion.Assert ("#01", pass);
  914. }
  915. }
  916. [Test]
  917. [ExpectedException(typeof (FileNotFoundException))]
  918. public void SetCreationTimeFileNotFoundException1 ()
  919. {
  920. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
  921. DeleteFile (path);
  922. File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  923. }
  924. // [Test]
  925. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  926. // public void SetCreationTimeArgumentOutOfRangeException1 ()
  927. // {
  928. // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
  929. // FileStream stream = null;
  930. // DeleteFile (path);
  931. // try {
  932. // stream = File.Create (path);
  933. // stream.Close ();
  934. // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  935. // } finally {
  936. // if (stream != null)
  937. // stream.Close ();
  938. // DeleteFile (path);
  939. // }
  940. // }
  941. [Test]
  942. [ExpectedException(typeof (IOException))]
  943. public void SetCreationTimeIOException1 ()
  944. {
  945. string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
  946. DeleteFile (path);
  947. FileStream stream = null;
  948. try {
  949. stream = File.Create (path);
  950. File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  951. } finally {
  952. if (stream != null)
  953. stream.Close ();
  954. DeleteFile (path);
  955. }
  956. }
  957. [Test]
  958. [ExpectedException(typeof (ArgumentNullException))]
  959. public void SetCreationTimeUtcArgumentNullException1 ()
  960. {
  961. File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  962. }
  963. [Test]
  964. [ExpectedException(typeof (ArgumentException))]
  965. public void SetCreationTimeUtcArgumenException1 ()
  966. {
  967. File.SetCreationTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  968. }
  969. [Test]
  970. [ExpectedException(typeof (ArgumentException))]
  971. public void SetCreationTimeUtcArgumenException2 ()
  972. {
  973. File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  974. }
  975. [Test]
  976. // On Unix there are no invalid path chars.
  977. public void SetCreationTimeUtcArgumentException3 ()
  978. {
  979. if (Path.InvalidPathChars.Length > 1) {
  980. bool pass = false;
  981. try {
  982. File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  983. } catch (ArgumentException) {
  984. pass = true;
  985. }
  986. Assertion.Assert ("#01", pass);
  987. }
  988. }
  989. [Test]
  990. [ExpectedException(typeof (FileNotFoundException))]
  991. public void SetCreationTimeUtcFileNotFoundException1 ()
  992. {
  993. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
  994. DeleteFile (path);
  995. File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  996. }
  997. // [Test]
  998. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  999. // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
  1000. // {
  1001. // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
  1002. // DeleteFile (path);
  1003. // FileStream stream = null;
  1004. // try {
  1005. // stream = File.Create (path);
  1006. // stream.Close ();
  1007. // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1008. // } finally {
  1009. // if (stream != null)
  1010. // stream.Close();
  1011. // DeleteFile (path);
  1012. // }
  1013. // }
  1014. [Test]
  1015. [ExpectedException(typeof (IOException))]
  1016. public void SetCreationTimeUtcIOException1 ()
  1017. {
  1018. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
  1019. DeleteFile (path);
  1020. FileStream stream = null;
  1021. try {
  1022. stream = File.Create (path);
  1023. File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1024. } finally {
  1025. if (stream != null)
  1026. stream.Close ();
  1027. DeleteFile (path);
  1028. }
  1029. }
  1030. // SetLastAccessTime and SetLastAccessTimeUtc exceptions
  1031. [Test]
  1032. [ExpectedException(typeof (ArgumentNullException))]
  1033. public void SetLastAccessTimeArgumentNullException1 ()
  1034. {
  1035. File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1036. }
  1037. [Test]
  1038. [ExpectedException(typeof (ArgumentException))]
  1039. public void SetLastAccessTimeArgumenException1 ()
  1040. {
  1041. File.SetLastAccessTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1042. }
  1043. [Test]
  1044. [ExpectedException(typeof (ArgumentException))]
  1045. public void SetLastAccessTimeArgumenException2 ()
  1046. {
  1047. File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1048. }
  1049. [Test]
  1050. // On Unix there are no invalid path chars.
  1051. public void SetLastAccessTimeArgumenException3 ()
  1052. {
  1053. if (Path.InvalidPathChars.Length > 1) {
  1054. bool pass = false;
  1055. try {
  1056. File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1057. } catch (ArgumentException) {
  1058. pass = true;
  1059. }
  1060. Assertion.Assert ("#01", pass);
  1061. }
  1062. }
  1063. [Test]
  1064. [ExpectedException(typeof (FileNotFoundException))]
  1065. public void SetLastAccessTimeFileNotFoundException1 ()
  1066. {
  1067. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
  1068. DeleteFile (path);
  1069. File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1070. }
  1071. // [Test]
  1072. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1073. // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
  1074. // {
  1075. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
  1076. // DeleteFile (path);
  1077. // FileStream stream = null;
  1078. // try {
  1079. // stream = File.Create (path);
  1080. // stream.Close ();
  1081. // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1082. // } finally {
  1083. // if (stream != null)
  1084. // stream.Close ();
  1085. // DeleteFile (path);
  1086. // }
  1087. // }
  1088. [Test]
  1089. [ExpectedException(typeof (IOException))]
  1090. public void SetLastAccessTimeIOException1 ()
  1091. {
  1092. string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
  1093. DeleteFile (path);
  1094. FileStream stream = null;
  1095. try {
  1096. stream = File.Create (path);
  1097. File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1098. } finally {
  1099. if (stream != null)
  1100. stream.Close ();
  1101. DeleteFile (path);
  1102. }
  1103. }
  1104. [Test]
  1105. [ExpectedException(typeof (ArgumentNullException))]
  1106. public void SetLastAccessTimeUtcArgumentNullException1 ()
  1107. {
  1108. File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1109. }
  1110. [Test]
  1111. [ExpectedException(typeof (ArgumentException))]
  1112. public void SetCLastAccessTimeUtcArgumenException1 ()
  1113. {
  1114. File.SetLastAccessTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1115. }
  1116. [Test]
  1117. [ExpectedException(typeof (ArgumentException))]
  1118. public void SetLastAccessTimeUtcArgumenException2 ()
  1119. {
  1120. File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1121. }
  1122. [Test]
  1123. // On Unix there are no invalid path chars.
  1124. public void SetLastAccessTimeUtcArgumenException3 ()
  1125. {
  1126. if (Path.InvalidPathChars.Length > 1) {
  1127. bool pass = false;
  1128. try {
  1129. File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1130. } catch (ArgumentException) {
  1131. pass = true;
  1132. }
  1133. Assertion.Assert ("#01", pass);
  1134. }
  1135. }
  1136. [Test]
  1137. [ExpectedException(typeof (FileNotFoundException))]
  1138. public void SetLastAccessTimeUtcFileNotFoundException1 ()
  1139. {
  1140. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
  1141. DeleteFile (path);
  1142. File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1143. }
  1144. // [Test]
  1145. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1146. // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
  1147. // {
  1148. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
  1149. // DeleteFile (path);
  1150. // FileStream stream = null;
  1151. // try {
  1152. // stream = File.Create (path);
  1153. // stream.Close ();
  1154. // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1155. // } finally {
  1156. // if (stream != null)
  1157. // stream.Close ();
  1158. // DeleteFile (path);
  1159. // }
  1160. // }
  1161. [Test]
  1162. [ExpectedException(typeof (IOException))]
  1163. public void SetLastAccessTimeUtcIOException1 ()
  1164. {
  1165. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
  1166. DeleteFile (path);
  1167. FileStream stream = null;
  1168. try {
  1169. stream = File.Create (path);
  1170. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1171. } finally {
  1172. if (stream != null)
  1173. stream.Close ();
  1174. DeleteFile (path);
  1175. }
  1176. }
  1177. // SetLastWriteTime and SetLastWriteTimeUtc exceptions
  1178. [Test]
  1179. [ExpectedException(typeof (ArgumentNullException))]
  1180. public void SetLastWriteTimeArgumentNullException1 ()
  1181. {
  1182. File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1183. }
  1184. [Test]
  1185. [ExpectedException(typeof (ArgumentException))]
  1186. public void SetLastWriteTimeArgumenException1 ()
  1187. {
  1188. File.SetLastWriteTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1189. }
  1190. [Test]
  1191. [ExpectedException(typeof (ArgumentException))]
  1192. public void SetLastWriteTimeArgumenException2 ()
  1193. {
  1194. File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1195. }
  1196. [Test]
  1197. // On Unix there are no invalid path chars.
  1198. public void SetLastWriteTimeArgumenException3 ()
  1199. {
  1200. if (Path.InvalidPathChars.Length > 1) {
  1201. bool pass = false;
  1202. try {
  1203. File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1204. } catch (ArgumentException) {
  1205. pass = true;
  1206. }
  1207. Assertion.Assert ("#01", pass);
  1208. }
  1209. }
  1210. [Test]
  1211. [ExpectedException(typeof (FileNotFoundException))]
  1212. public void SetLastWriteTimeFileNotFoundException1 ()
  1213. {
  1214. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
  1215. DeleteFile (path);
  1216. File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1217. }
  1218. // [Test]
  1219. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1220. // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
  1221. // {
  1222. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
  1223. // DeleteFile (path);
  1224. // FileStream stream = null;
  1225. // try {
  1226. // stream = File.Create (path);
  1227. // stream.Close ();
  1228. // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1229. // } finally {
  1230. // if (stream != null)
  1231. // stream.Close ();
  1232. // DeleteFile (path);
  1233. // }
  1234. // }
  1235. [Test]
  1236. [ExpectedException(typeof (IOException))]
  1237. public void SetLastWriteTimeIOException1 ()
  1238. {
  1239. string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
  1240. DeleteFile (path);
  1241. FileStream stream = null;
  1242. try {
  1243. stream = File.Create (path);
  1244. File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1245. } finally {
  1246. if (stream != null)
  1247. stream.Close ();
  1248. DeleteFile (path);
  1249. }
  1250. }
  1251. [Test]
  1252. [ExpectedException(typeof (ArgumentNullException))]
  1253. public void SetLastWriteTimeUtcArgumentNullException1 ()
  1254. {
  1255. File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1256. }
  1257. [Test]
  1258. [ExpectedException(typeof (ArgumentException))]
  1259. public void SetCLastWriteTimeUtcArgumenException1 ()
  1260. {
  1261. File.SetLastWriteTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1262. }
  1263. [Test]
  1264. [ExpectedException(typeof (ArgumentException))]
  1265. public void SetLastWriteTimeUtcArgumenException2 ()
  1266. {
  1267. File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1268. }
  1269. [Test]
  1270. // On Unix there are no invalid path chars.
  1271. public void SetLastWriteTimeUtcArgumenException3 ()
  1272. {
  1273. if (Path.InvalidPathChars.Length > 1) {
  1274. bool pass = false;
  1275. try {
  1276. File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1277. } catch (ArgumentException) {
  1278. pass = true;
  1279. }
  1280. Assertion.Assert ("#01", pass);
  1281. }
  1282. }
  1283. [Test]
  1284. [ExpectedException(typeof (FileNotFoundException))]
  1285. public void SetLastWriteTimeUtcFileNotFoundException1 ()
  1286. {
  1287. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
  1288. DeleteFile (path);
  1289. File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1290. }
  1291. // [Test]
  1292. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1293. // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
  1294. // {
  1295. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
  1296. // DeleteFile (path);
  1297. // FileStream stream = null;
  1298. // try {
  1299. // stream = File.Create (path);
  1300. // stream.Close ();
  1301. // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1302. // } finally {
  1303. // if (stream != null)
  1304. // stream.Close ();
  1305. // DeleteFile (path);
  1306. // }
  1307. // }
  1308. //
  1309. [Test]
  1310. [ExpectedException(typeof (IOException))]
  1311. public void SetLastWriteTimeUtcIOException1 ()
  1312. {
  1313. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
  1314. DeleteFile (path);
  1315. FileStream stream = null;
  1316. try {
  1317. stream = File.Create (path);
  1318. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1319. } finally {
  1320. if (stream != null)
  1321. stream.Close ();
  1322. DeleteFile (path);
  1323. }
  1324. }
  1325. [Test]
  1326. public void OpenAppend ()
  1327. {
  1328. string fn = Path.GetTempFileName ();
  1329. using (FileStream s = File.Open (fn, FileMode.Append))
  1330. ;
  1331. DeleteFile (fn);
  1332. }
  1333. private void DeleteFile (string path)
  1334. {
  1335. if (File.Exists (path))
  1336. File.Delete (path);
  1337. }
  1338. }
  1339. }