FileTest.cs 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748
  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 System;
  13. using System.IO;
  14. using System.Globalization;
  15. using System.Threading;
  16. using System.Runtime.InteropServices;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.IO
  19. {
  20. [TestFixture]
  21. public class FileTest
  22. {
  23. CultureInfo old_culture;
  24. string tmpFolder;
  25. [SetUp]
  26. public void SetUp ()
  27. {
  28. tmpFolder = Path.GetTempFileName ();
  29. if (File.Exists (tmpFolder))
  30. File.Delete (tmpFolder);
  31. DeleteDirectory (tmpFolder);
  32. Directory.CreateDirectory (tmpFolder);
  33. old_culture = Thread.CurrentThread.CurrentCulture;
  34. Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
  35. }
  36. [TearDown]
  37. public void TearDown ()
  38. {
  39. DeleteDirectory (tmpFolder);
  40. Thread.CurrentThread.CurrentCulture = old_culture;
  41. }
  42. string path;
  43. string testfile;
  44. [TestFixtureSetUp]
  45. public void FixtureSetUp ()
  46. {
  47. path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
  48. testfile = Path.Combine (path, "FileStreamTest.dat");
  49. File.WriteAllText (testfile, "1");
  50. }
  51. [TestFixtureTearDown]
  52. public void FixtureTearDown ()
  53. {
  54. if (File.Exists (testfile))
  55. File.Delete (testfile);
  56. }
  57. [Test]
  58. public void TestExists ()
  59. {
  60. FileStream s = null;
  61. string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  62. try {
  63. Assert.IsFalse (File.Exists (null), "#1");
  64. Assert.IsFalse (File.Exists (string.Empty), "#2");
  65. Assert.IsFalse (File.Exists (" \t\t \t \n\t\n \n"), "#3");
  66. DeleteFile (path);
  67. s = File.Create (path);
  68. s.Close ();
  69. Assert.IsTrue (File.Exists (path), "#4");
  70. Assert.IsFalse (File.Exists (tmpFolder + Path.DirectorySeparatorChar + "doesnotexist"), "#5");
  71. } finally {
  72. if (s != null)
  73. s.Close ();
  74. DeleteFile (path);
  75. }
  76. }
  77. [Test]
  78. public void Exists_InvalidFileName ()
  79. {
  80. Assert.IsFalse (File.Exists ("><|"), "#1");
  81. Assert.IsFalse (File.Exists ("?*"), "#2");
  82. }
  83. [Test]
  84. public void Exists_InvalidDirectory ()
  85. {
  86. Assert.IsFalse (File.Exists (Path.Combine ("does not exist", "file.txt")));
  87. }
  88. [Test]
  89. public void Create_Path_Null ()
  90. {
  91. try {
  92. File.Create (null);
  93. Assert.Fail ("#1");
  94. } catch (ArgumentNullException ex) {
  95. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  96. Assert.IsNull (ex.InnerException, "#3");
  97. Assert.IsNotNull (ex.Message, "#4");
  98. Assert.AreEqual ("path", ex.ParamName, "#5");
  99. }
  100. }
  101. [Test]
  102. public void Create_Path_Directory ()
  103. {
  104. string path = Path.Combine (tmpFolder, "foo");
  105. Directory.CreateDirectory (path);
  106. try {
  107. File.Create (path);
  108. Assert.Fail ("#1");
  109. } catch (UnauthorizedAccessException ex) {
  110. // Access to the path '...' is denied
  111. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  112. Assert.IsNull (ex.InnerException, "#3");
  113. Assert.IsNotNull (ex.Message, "#4");
  114. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  115. } finally {
  116. DeleteDirectory (path);
  117. }
  118. }
  119. [Test]
  120. public void Create_Path_Empty ()
  121. {
  122. try {
  123. File.Create (string.Empty);
  124. Assert.Fail ("#1");
  125. } catch (ArgumentException ex) {
  126. // Empty file name is not legal
  127. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  128. Assert.IsNull (ex.InnerException, "#3");
  129. Assert.IsNotNull (ex.Message, "#4");
  130. Assert.IsNull (ex.ParamName, "#5");
  131. }
  132. }
  133. [Test]
  134. public void Create_Path_ReadOnly ()
  135. {
  136. string path = Path.Combine (tmpFolder, "foo");
  137. File.Create (path).Close ();
  138. File.SetAttributes (path, FileAttributes.ReadOnly);
  139. try {
  140. File.Create (path);
  141. Assert.Fail ("#1");
  142. } catch (UnauthorizedAccessException ex) {
  143. // Access to the path '...' is denied
  144. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  145. Assert.IsNull (ex.InnerException, "#3");
  146. Assert.IsNotNull (ex.Message, "#4");
  147. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  148. } finally {
  149. File.SetAttributes (path, FileAttributes.Normal);
  150. }
  151. }
  152. [Test]
  153. public void Create_Path_Whitespace ()
  154. {
  155. try {
  156. File.Create (" ");
  157. Assert.Fail ("#1");
  158. } catch (ArgumentException ex) {
  159. // The path is not of a legal form
  160. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  161. Assert.IsNull (ex.InnerException, "#3");
  162. Assert.IsNotNull (ex.Message, "#4");
  163. Assert.IsNull (ex.ParamName, "#5");
  164. }
  165. }
  166. [Test]
  167. public void Create_Directory_DoesNotExist ()
  168. {
  169. FileStream stream = null;
  170. string path = tmpFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
  171. try {
  172. stream = File.Create (path);
  173. Assert.Fail ("#1");
  174. } catch (DirectoryNotFoundException ex) {
  175. // Could not find a part of the path "..."
  176. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
  177. Assert.IsNull (ex.InnerException, "#3");
  178. Assert.IsNotNull (ex.Message, "#4");
  179. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  180. } finally {
  181. if (stream != null)
  182. stream.Close ();
  183. DeleteFile (path);
  184. }
  185. }
  186. [Test]
  187. public void Create ()
  188. {
  189. FileStream stream = null;
  190. string path = null;
  191. /* positive test: create resources/foo */
  192. path = tmpFolder + Path.DirectorySeparatorChar + "foo";
  193. try {
  194. stream = File.Create (path);
  195. Assert.IsTrue (File.Exists (path), "#1");
  196. stream.Close ();
  197. } finally {
  198. if (stream != null)
  199. stream.Close ();
  200. DeleteFile (path);
  201. }
  202. stream = null;
  203. /* positive test: repeat test above again to test for overwriting file */
  204. path = tmpFolder + Path.DirectorySeparatorChar + "foo";
  205. try {
  206. stream = File.Create (path);
  207. Assert.IsTrue (File.Exists (path), "#2");
  208. stream.Close ();
  209. } finally {
  210. if (stream != null)
  211. stream.Close ();
  212. DeleteFile (path);
  213. }
  214. }
  215. [Test]
  216. public void Copy_SourceFileName_Null ()
  217. {
  218. try {
  219. File.Copy (null, "b");
  220. Assert.Fail ("#1");
  221. } catch (ArgumentNullException ex) {
  222. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  223. Assert.IsNull (ex.InnerException, "#3");
  224. Assert.IsNotNull (ex.Message, "#4");
  225. Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
  226. }
  227. }
  228. [Test]
  229. public void Copy_DestFileName_Null ()
  230. {
  231. try {
  232. File.Copy ("a", null);
  233. Assert.Fail ("#1");
  234. } catch (ArgumentNullException ex) {
  235. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  236. Assert.IsNull (ex.InnerException, "#3");
  237. Assert.IsNotNull (ex.Message, "#4");
  238. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  239. }
  240. }
  241. [Test]
  242. public void Copy_SourceFileName_Empty ()
  243. {
  244. try {
  245. File.Copy (string.Empty, "b");
  246. Assert.Fail ("#1");
  247. } catch (ArgumentException ex) {
  248. // Empty file name is not legal
  249. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  250. Assert.IsNull (ex.InnerException, "#3");
  251. Assert.IsNotNull (ex.Message, "#4");
  252. Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
  253. }
  254. }
  255. [Test]
  256. public void Copy_DestFileName_Empty ()
  257. {
  258. try {
  259. File.Copy ("a", string.Empty);
  260. Assert.Fail ("#1");
  261. } catch (ArgumentException ex) {
  262. // Empty file name is not legal
  263. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  264. Assert.IsNull (ex.InnerException, "#3");
  265. Assert.IsNotNull (ex.Message, "#4");
  266. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  267. }
  268. }
  269. [Test]
  270. public void Copy_SourceFileName_Whitespace ()
  271. {
  272. try {
  273. File.Copy (" ", "b");
  274. Assert.Fail ("#1");
  275. } catch (ArgumentException ex) {
  276. // The path is not of a legal form
  277. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  278. Assert.IsNull (ex.InnerException, "#3");
  279. Assert.IsNotNull (ex.Message, "#4");
  280. Assert.IsNull (ex.ParamName, "#5");
  281. }
  282. }
  283. [Test]
  284. public void Copy_DestFileName_Whitespace ()
  285. {
  286. try {
  287. File.Copy ("a", " ");
  288. Assert.Fail ("#1");
  289. } catch (ArgumentException ex) {
  290. // The path is not of a legal form
  291. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  292. Assert.IsNull (ex.InnerException, "#3");
  293. Assert.IsNotNull (ex.Message, "#4");
  294. Assert.IsNull (ex.ParamName, "#5");
  295. }
  296. }
  297. [Test]
  298. public void Copy_SourceFileName_DoesNotExist ()
  299. {
  300. try {
  301. File.Copy ("doesnotexist", "b");
  302. Assert.Fail ("#1");
  303. } catch (FileNotFoundException ex) {
  304. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  305. Assert.AreEqual ("doesnotexist", ex.FileName, "#3");
  306. Assert.IsNull (ex.InnerException, "#4");
  307. Assert.IsNotNull (ex.Message, "#5");
  308. }
  309. }
  310. [Test]
  311. public void Copy_DestFileName_AlreadyExists ()
  312. {
  313. string source = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  314. string dest = tmpFolder + Path.DirectorySeparatorChar + "bar";
  315. DeleteFile (source);
  316. DeleteFile (dest);
  317. try {
  318. File.Create (source).Close ();
  319. File.Copy (source, dest);
  320. try {
  321. File.Copy (source, dest);
  322. Assert.Fail ("#1");
  323. } catch (IOException ex) {
  324. // The file '...' already exists.
  325. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  326. Assert.IsNull (ex.InnerException, "#3");
  327. Assert.IsNotNull (ex.Message, "#4");
  328. Assert.IsTrue (ex.Message.IndexOf (dest) != -1, "#5");
  329. }
  330. } finally {
  331. DeleteFile (dest);
  332. DeleteFile (source);
  333. }
  334. }
  335. [Test]
  336. public void Copy_SourceFileName_DestFileName_Same ()
  337. {
  338. string source = tmpFolder + Path.DirectorySeparatorChar + "SameFile.txt";
  339. DeleteFile (source);
  340. try {
  341. // new empty file
  342. File.Create (source).Close ();
  343. try {
  344. File.Copy (source, source, true);
  345. Assert.Fail ("#1");
  346. } catch (IOException ex) {
  347. // process cannot access file ... because it is being used by another process
  348. Assert.IsNull (ex.InnerException, "#2");
  349. Assert.IsTrue (ex.Message.IndexOf (source) != -1, "#3");
  350. }
  351. } finally {
  352. DeleteFile (source);
  353. }
  354. }
  355. [Test]
  356. public void Copy ()
  357. {
  358. string path1 = tmpFolder + Path.DirectorySeparatorChar + "bar";
  359. string path2 = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  360. /* positive test: copy resources/AFile.txt to resources/bar */
  361. try {
  362. DeleteFile (path1);
  363. DeleteFile (path2);
  364. File.Create (path2).Close ();
  365. File.Copy (path2, path1);
  366. Assert.IsTrue (File.Exists (path2), "#A1");
  367. Assert.IsTrue (File.Exists (path1), "#A2");
  368. Assert.IsTrue (File.Exists (path1), "#B1");
  369. File.Copy (path2, path1, true);
  370. Assert.IsTrue (File.Exists (path2), "#B2");
  371. Assert.IsTrue (File.Exists (path1), "#B3");
  372. } finally {
  373. DeleteFile (path1);
  374. DeleteFile (path2);
  375. }
  376. }
  377. [Test]
  378. public void Delete_Path_Null ()
  379. {
  380. try {
  381. File.Delete (null);
  382. Assert.Fail ("#1");
  383. } catch (ArgumentNullException ex) {
  384. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  385. Assert.IsNull (ex.InnerException, "#3");
  386. Assert.IsNotNull (ex.Message, "#4");
  387. Assert.AreEqual ("path", ex.ParamName, "#5");
  388. }
  389. }
  390. [Test]
  391. public void Delete_Path_Empty ()
  392. {
  393. try {
  394. File.Delete (string.Empty);
  395. Assert.Fail ("#1");
  396. } catch (ArgumentException ex) {
  397. // Empty file name is not legal
  398. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  399. Assert.IsNull (ex.InnerException, "#3");
  400. Assert.IsNotNull (ex.Message, "#4");
  401. Assert.IsNull (ex.ParamName, "#5");
  402. }
  403. }
  404. [Test]
  405. public void Delete_Path_Whitespace ()
  406. {
  407. try {
  408. File.Delete (" ");
  409. Assert.Fail ("#1");
  410. } catch (ArgumentException ex) {
  411. // The path is not of a legal form
  412. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  413. Assert.IsNull (ex.InnerException, "#3");
  414. Assert.IsNotNull (ex.Message, "#4");
  415. Assert.IsNull (ex.ParamName, "#5");
  416. }
  417. }
  418. [Test]
  419. public void Delete_Directory_DoesNotExist ()
  420. {
  421. string path = tmpFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
  422. if (Directory.Exists (path))
  423. Directory.Delete (path, true);
  424. try {
  425. File.Delete (path);
  426. Assert.Fail ("#1");
  427. } catch (DirectoryNotFoundException ex) {
  428. // Could not find a part of the path "..."
  429. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
  430. Assert.IsNull (ex.InnerException, "#3");
  431. Assert.IsNotNull (ex.Message, "#4");
  432. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  433. }
  434. }
  435. [Test]
  436. public void Delete ()
  437. {
  438. string foopath = tmpFolder + Path.DirectorySeparatorChar + "foo";
  439. DeleteFile (foopath);
  440. try {
  441. File.Create (foopath).Close ();
  442. File.Delete (foopath);
  443. Assert.IsFalse (File.Exists (foopath));
  444. File.Delete (foopath);
  445. } finally {
  446. DeleteFile (foopath);
  447. }
  448. }
  449. [Test] // bug #323389
  450. [Category ("NotWorking")]
  451. public void Delete_FileLock ()
  452. {
  453. string path = tmpFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
  454. DeleteFile (path);
  455. FileStream stream = null;
  456. try {
  457. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  458. try {
  459. File.Delete (path);
  460. Assert.Fail ("#1");
  461. } catch (IOException ex) {
  462. // The process cannot access the file '...'
  463. // because it is being used by another process
  464. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  465. Assert.IsNull (ex.InnerException, "#3");
  466. Assert.IsNotNull (ex.Message, "#4");
  467. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  468. }
  469. } finally {
  470. if (stream != null)
  471. stream.Close ();
  472. DeleteFile (path);
  473. }
  474. }
  475. [Test]
  476. [ExpectedException (typeof(UnauthorizedAccessException))]
  477. public void Delete_File_ReadOnly ()
  478. {
  479. if (RunningOnUnix)
  480. Assert.Ignore ("ReadOnly files can be deleted on unix since fdef50957f508627928c7876a905d5584da45748.");
  481. string path = tmpFolder + Path.DirectorySeparatorChar + "DeleteReadOnly";
  482. DeleteFile (path);
  483. try {
  484. File.Create (path).Close ();
  485. File.SetAttributes (path, FileAttributes.ReadOnly);
  486. File.Delete (path);
  487. } finally {
  488. File.SetAttributes (path, FileAttributes.Normal);
  489. DeleteFile (path);
  490. }
  491. }
  492. [Test]
  493. public void Delete_NonExisting_NoException ()
  494. {
  495. File.Delete (Path.Combine (Directory.GetDirectoryRoot (Directory.GetCurrentDirectory ()), "monononexistingfile.dat"));
  496. }
  497. [Test]
  498. public void GetAttributes_Archive ()
  499. {
  500. if (RunningOnUnix)
  501. Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
  502. FileAttributes attrs;
  503. string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
  504. File.Create (path).Close ();
  505. attrs = File.GetAttributes (path);
  506. Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
  507. attrs &= ~FileAttributes.Archive;
  508. File.SetAttributes (path, attrs);
  509. attrs = File.GetAttributes (path);
  510. Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
  511. }
  512. [Test]
  513. public void GetAttributes_Default_File ()
  514. {
  515. if (RunningOnUnix)
  516. Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
  517. string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
  518. File.Create (path).Close ();
  519. FileAttributes attrs = File.GetAttributes (path);
  520. Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
  521. Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
  522. Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
  523. Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
  524. Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
  525. Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
  526. }
  527. [Test]
  528. public void GetAttributes_Default_Directory ()
  529. {
  530. FileAttributes attrs = File.GetAttributes (tmpFolder);
  531. Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
  532. Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
  533. Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
  534. Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
  535. Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
  536. Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
  537. }
  538. [Test]
  539. public void GetAttributes_Directory ()
  540. {
  541. FileAttributes attrs = File.GetAttributes (tmpFolder);
  542. Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
  543. attrs &= ~FileAttributes.Directory;
  544. File.SetAttributes (tmpFolder, attrs);
  545. Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
  546. string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
  547. File.Create (path).Close ();
  548. attrs = File.GetAttributes (path);
  549. attrs |= FileAttributes.Directory;
  550. File.SetAttributes (path, attrs);
  551. Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
  552. }
  553. [Test]
  554. public void GetAttributes_ReadOnly ()
  555. {
  556. FileAttributes attrs;
  557. string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
  558. File.Create (path).Close ();
  559. attrs = File.GetAttributes (path);
  560. Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
  561. try {
  562. attrs |= FileAttributes.ReadOnly;
  563. File.SetAttributes (path, attrs);
  564. attrs = File.GetAttributes (path);
  565. Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
  566. } finally {
  567. File.SetAttributes (path, FileAttributes.Normal);
  568. }
  569. }
  570. [Test]
  571. public void GetAttributes_System ()
  572. {
  573. if (RunningOnUnix)
  574. Assert.Ignore ("FileAttributes.System is not supported on Unix.");
  575. FileAttributes attrs;
  576. string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
  577. File.Create (path).Close ();
  578. attrs = File.GetAttributes (path);
  579. Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
  580. attrs |= FileAttributes.System;
  581. File.SetAttributes (path, FileAttributes.System);
  582. attrs = File.GetAttributes (path);
  583. Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
  584. }
  585. [Test]
  586. public void GetAttributes_Path_DoesNotExist ()
  587. {
  588. string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
  589. try {
  590. File.GetAttributes (path);
  591. Assert.Fail ("#1");
  592. } catch (FileNotFoundException ex) {
  593. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  594. Assert.AreEqual (path, ex.FileName, "#3");
  595. Assert.IsNull (ex.InnerException, "#4");
  596. Assert.IsNotNull (ex.Message, "#5");
  597. }
  598. }
  599. [Test]
  600. public void GetAttributes_Path_Empty ()
  601. {
  602. try {
  603. File.GetAttributes (string.Empty);
  604. Assert.Fail ("#1");
  605. } catch (ArgumentException ex) {
  606. // Empty file name is not legal
  607. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  608. Assert.IsNull (ex.InnerException, "#3");
  609. Assert.IsNotNull (ex.Message, "#4");
  610. Assert.IsNull (ex.ParamName, "#5");
  611. }
  612. }
  613. [Test]
  614. public void GetAttributes_Path_Null ()
  615. {
  616. try {
  617. File.GetAttributes (null);
  618. Assert.Fail ("#1");
  619. } catch (ArgumentNullException ex) {
  620. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  621. Assert.IsNull (ex.InnerException, "#3");
  622. Assert.IsNotNull (ex.Message, "#4");
  623. Assert.AreEqual ("path", ex.ParamName, "#5");
  624. }
  625. }
  626. [Test]
  627. public void Move_SourceFileName_Null ()
  628. {
  629. try {
  630. File.Move (null, "b");
  631. Assert.Fail ("#1");
  632. } catch (ArgumentNullException ex) {
  633. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  634. Assert.IsNull (ex.InnerException, "#3");
  635. Assert.IsNotNull (ex.Message, "#4");
  636. Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
  637. }
  638. }
  639. [Test]
  640. public void Move_DestFileName_Null ()
  641. {
  642. try {
  643. File.Move ("a", null);
  644. Assert.Fail ("#1");
  645. } catch (ArgumentNullException ex) {
  646. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  647. Assert.IsNull (ex.InnerException, "#3");
  648. Assert.IsNotNull (ex.Message, "#4");
  649. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  650. }
  651. }
  652. [Test]
  653. public void Move_SourceFileName_Empty ()
  654. {
  655. try {
  656. File.Move (string.Empty, "b");
  657. Assert.Fail ("#1");
  658. } catch (ArgumentException ex) {
  659. // Empty file name is not legal
  660. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  661. Assert.IsNull (ex.InnerException, "#3");
  662. Assert.IsNotNull (ex.Message, "#4");
  663. Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
  664. }
  665. }
  666. [Test]
  667. public void Move_DestFileName_Empty ()
  668. {
  669. try {
  670. File.Move ("a", string.Empty);
  671. Assert.Fail ("#1");
  672. } catch (ArgumentException ex) {
  673. // Empty file name is not legal
  674. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  675. Assert.IsNull (ex.InnerException, "#3");
  676. Assert.IsNotNull (ex.Message, "#4");
  677. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  678. }
  679. }
  680. [Test]
  681. public void Move_SourceFileName_Whitespace ()
  682. {
  683. try {
  684. File.Move (" ", "b");
  685. Assert.Fail ("#1");
  686. } catch (ArgumentException ex) {
  687. // The path is not of a legal form
  688. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  689. Assert.IsNull (ex.InnerException, "#3");
  690. Assert.IsNotNull (ex.Message, "#4");
  691. Assert.IsNull (ex.ParamName, "#5");
  692. }
  693. }
  694. [Test]
  695. public void Move_DestFileName_Whitespace ()
  696. {
  697. try {
  698. File.Move ("a", " ");
  699. Assert.Fail ("#1");
  700. } catch (ArgumentException ex) {
  701. // The path is not of a legal form
  702. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  703. Assert.IsNull (ex.InnerException, "#3");
  704. Assert.IsNotNull (ex.Message, "#4");
  705. Assert.IsNull (ex.ParamName, "#5");
  706. }
  707. }
  708. [Test]
  709. public void Move_SourceFileName_DoesNotExist ()
  710. {
  711. string file = tmpFolder + Path.DirectorySeparatorChar + "doesnotexist";
  712. DeleteFile (file);
  713. try {
  714. File.Move (file, "b");
  715. Assert.Fail ("#1");
  716. } catch (FileNotFoundException ex) {
  717. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  718. Assert.AreEqual (file, ex.FileName, "#3");
  719. Assert.IsNull (ex.InnerException, "#4");
  720. Assert.IsNotNull (ex.Message, "#5");
  721. }
  722. }
  723. [Test]
  724. public void Move_DestFileName_DirectoryDoesNotExist ()
  725. {
  726. string sourceFile = tmpFolder + Path.DirectorySeparatorChar + "foo";
  727. string destFile = Path.Combine (Path.Combine (tmpFolder, "doesnotexist"), "b");
  728. DeleteFile (sourceFile);
  729. try {
  730. File.Create (sourceFile).Close ();
  731. try {
  732. File.Move (sourceFile, destFile);
  733. Assert.Fail ("#1");
  734. } catch (DirectoryNotFoundException ex) {
  735. // Could not find a part of the path
  736. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
  737. Assert.IsNull (ex.InnerException, "#3");
  738. Assert.IsNotNull (ex.Message, "#4");
  739. Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
  740. }
  741. } finally {
  742. DeleteFile (sourceFile);
  743. }
  744. }
  745. [Test]
  746. public void Move_DestFileName_AlreadyExists ()
  747. {
  748. string sourceFile = tmpFolder + Path.DirectorySeparatorChar + "foo";
  749. string destFile;
  750. // move to same directory
  751. File.Create (sourceFile).Close ();
  752. try {
  753. File.Move (sourceFile, tmpFolder);
  754. Assert.Fail ("#A1");
  755. } catch (IOException ex) {
  756. // Cannot create a file when that file already exists
  757. Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
  758. Assert.IsNull (ex.InnerException, "#A3");
  759. Assert.IsNotNull (ex.Message, "#A4");
  760. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
  761. Assert.IsFalse (ex.Message.IndexOf (tmpFolder) != -1, "#A6");
  762. } finally {
  763. DeleteFile (sourceFile);
  764. }
  765. // move to exist file
  766. File.Create (sourceFile).Close ();
  767. destFile = tmpFolder + Path.DirectorySeparatorChar + "bar";
  768. File.Create (destFile).Close ();
  769. try {
  770. File.Move (sourceFile, destFile);
  771. Assert.Fail ("#B1");
  772. } catch (IOException ex) {
  773. // Cannot create a file when that file already exists
  774. Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
  775. Assert.IsNull (ex.InnerException, "#B3");
  776. Assert.IsNotNull (ex.Message, "#B4");
  777. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
  778. Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
  779. } finally {
  780. DeleteFile (sourceFile);
  781. DeleteFile (destFile);
  782. }
  783. // move to existing directory
  784. File.Create (sourceFile).Close ();
  785. destFile = tmpFolder + Path.DirectorySeparatorChar + "bar";
  786. Directory.CreateDirectory (destFile);
  787. try {
  788. File.Move (sourceFile, destFile);
  789. Assert.Fail ("#C1");
  790. } catch (IOException ex) {
  791. // Cannot create a file when that file already exists
  792. Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
  793. Assert.IsNull (ex.InnerException, "#C3");
  794. Assert.IsNotNull (ex.Message, "#C4");
  795. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
  796. Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
  797. } finally {
  798. DeleteFile (sourceFile);
  799. DeleteDirectory (destFile);
  800. }
  801. }
  802. [Test]
  803. public void Move ()
  804. {
  805. string bar = tmpFolder + Path.DirectorySeparatorChar + "bar";
  806. string baz = tmpFolder + Path.DirectorySeparatorChar + "baz";
  807. if (!File.Exists (bar)) {
  808. FileStream f = File.Create(bar);
  809. f.Close();
  810. }
  811. Assert.IsTrue (File.Exists (bar), "#1");
  812. File.Move (bar, baz);
  813. Assert.IsFalse (File.Exists (bar), "#2");
  814. Assert.IsTrue (File.Exists (baz), "#3");
  815. // Test moving of directories
  816. string dir = Path.Combine (tmpFolder, "dir");
  817. string dir2 = Path.Combine (tmpFolder, "dir2");
  818. string dir_foo = Path.Combine (dir, "foo");
  819. string dir2_foo = Path.Combine (dir2, "foo");
  820. if (Directory.Exists (dir))
  821. Directory.Delete (dir, true);
  822. Directory.CreateDirectory (dir);
  823. Directory.CreateDirectory (dir2);
  824. File.Create (dir_foo).Close ();
  825. File.Move (dir_foo, dir2_foo);
  826. Assert.IsTrue (File.Exists (dir2_foo), "#4");
  827. Directory.Delete (dir, true);
  828. Directory.Delete (dir2, true);
  829. DeleteFile (dir_foo);
  830. DeleteFile (dir2_foo);
  831. }
  832. [Test]
  833. public void Move_FileLock ()
  834. {
  835. string sourceFile = Path.GetTempFileName ();
  836. string destFile = Path.GetTempFileName ();
  837. // source file locked
  838. using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
  839. try {
  840. File.Move (sourceFile, destFile);
  841. Assert.Fail ("#A1");
  842. } catch (IOException ex) {
  843. // The process cannot access the file because
  844. // it is being used by another process
  845. Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
  846. Assert.IsNull (ex.InnerException, "#A3");
  847. Assert.IsNotNull (ex.Message, "#A4");
  848. }
  849. }
  850. // destination file locked
  851. using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
  852. try {
  853. File.Move (sourceFile, destFile);
  854. Assert.Fail ("#B1");
  855. } catch (IOException ex) {
  856. // The process cannot access the file because
  857. // it is being used by another process
  858. Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
  859. Assert.IsNull (ex.InnerException, "#B3");
  860. Assert.IsNotNull (ex.Message, "#B4");
  861. }
  862. }
  863. }
  864. [Test]
  865. public void Open ()
  866. {
  867. string path = null;
  868. FileStream stream = null;
  869. path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  870. try {
  871. if (!File.Exists (path))
  872. stream = File.Create (path);
  873. stream.Close ();
  874. stream = File.Open (path, FileMode.Open);
  875. stream.Close ();
  876. } finally {
  877. if (stream != null)
  878. stream.Close ();
  879. DeleteFile (path);
  880. }
  881. stream = null;
  882. if (!File.Exists (path))
  883. File.Create (path).Close ();
  884. try {
  885. stream = File.Open (path, FileMode.Open);
  886. Assert.IsTrue (stream.CanRead, "#A1");
  887. Assert.IsTrue (stream.CanSeek, "#A2");
  888. Assert.IsTrue (stream.CanWrite, "#A3");
  889. stream.Close ();
  890. stream = File.Open (path, FileMode.Open, FileAccess.Write);
  891. Assert.IsFalse (stream.CanRead, "#B1");
  892. Assert.IsTrue (stream.CanSeek, "#B2");
  893. Assert.IsTrue (stream.CanWrite, "#B3");
  894. stream.Close ();
  895. stream = File.Open (path, FileMode.Open, FileAccess.Read);
  896. Assert.IsTrue (stream.CanRead, "#C1");
  897. Assert.IsTrue (stream.CanSeek, "#C2");
  898. Assert.IsFalse (stream.CanWrite, "#C3");
  899. stream.Close ();
  900. } finally {
  901. if (stream != null)
  902. stream.Close ();
  903. DeleteFile (path);
  904. }
  905. stream = null;
  906. /* Exception tests */
  907. path = tmpFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
  908. try {
  909. stream = File.Open (path, FileMode.Open);
  910. Assert.Fail ("#D1");
  911. } catch (FileNotFoundException ex) {
  912. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
  913. Assert.AreEqual (path, ex.FileName, "#D3");
  914. Assert.IsNull (ex.InnerException, "#D4");
  915. Assert.IsNotNull (ex.Message, "#D5");
  916. } finally {
  917. if (stream != null)
  918. stream.Close ();
  919. DeleteFile (path);
  920. }
  921. }
  922. [Test]
  923. public void Open_CreateNewMode_ReadAccess ()
  924. {
  925. string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  926. FileStream stream = null;
  927. try {
  928. stream = File.Open (tmpFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
  929. Assert.Fail ("#1");
  930. } catch (ArgumentException ex) {
  931. // Combining FileMode: CreateNew with FileAccess: Read is invalid
  932. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  933. Assert.IsNull (ex.InnerException, "#3");
  934. Assert.IsNotNull (ex.Message, "#4");
  935. Assert.IsNull (ex.ParamName, "#5");
  936. } finally {
  937. if (stream != null)
  938. stream.Close ();
  939. DeleteFile (path);
  940. }
  941. }
  942. [Test]
  943. public void Open_AppendMode_ReadAccess ()
  944. {
  945. string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  946. FileStream s = null;
  947. if (!File.Exists (path))
  948. File.Create (path).Close ();
  949. try {
  950. s = File.Open (path, FileMode.Append, FileAccess.Read);
  951. Assert.Fail ("#1");
  952. } catch (ArgumentException ex) {
  953. // Combining FileMode: Append with FileAccess: Read is invalid
  954. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  955. Assert.IsNull (ex.InnerException, "#3");
  956. Assert.IsNotNull (ex.Message, "#4");
  957. Assert.IsNull (ex.ParamName, "#5");
  958. } finally {
  959. if (s != null)
  960. s.Close ();
  961. DeleteFile (path);
  962. }
  963. }
  964. [Test]
  965. public void OpenRead ()
  966. {
  967. string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  968. if (!File.Exists (path))
  969. File.Create (path).Close ();
  970. FileStream stream = null;
  971. try {
  972. stream = File.OpenRead (path);
  973. Assert.IsTrue (stream.CanRead, "#1");
  974. Assert.IsTrue (stream.CanSeek, "#2");
  975. Assert.IsFalse (stream.CanWrite, "#3");
  976. } finally {
  977. if (stream != null)
  978. stream.Close ();
  979. DeleteFile (path);
  980. }
  981. }
  982. [Test]
  983. public void OpenWrite ()
  984. {
  985. string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  986. if (!File.Exists (path))
  987. File.Create (path).Close ();
  988. FileStream stream = null;
  989. try {
  990. stream = File.OpenWrite (path);
  991. Assert.IsFalse (stream.CanRead, "#1");
  992. Assert.IsTrue (stream.CanSeek, "#2");
  993. Assert.IsTrue (stream.CanWrite, "#3");
  994. stream.Close ();
  995. } finally {
  996. if (stream != null)
  997. stream.Close ();
  998. DeleteFile (path);
  999. }
  1000. }
  1001. [Test]
  1002. public void TestGetCreationTime ()
  1003. {
  1004. string path = tmpFolder + Path.DirectorySeparatorChar + "baz";
  1005. DeleteFile (path);
  1006. try {
  1007. File.Create (path).Close();
  1008. DateTime time = File.GetCreationTime (path);
  1009. Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
  1010. } finally {
  1011. DeleteFile (path);
  1012. }
  1013. }
  1014. [Test]
  1015. public void CreationTime ()
  1016. {
  1017. if (RunningOnUnix)
  1018. Assert.Ignore ("Setting the creation time on Unix is not possible.");
  1019. string path = Path.GetTempFileName ();
  1020. try {
  1021. File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  1022. DateTime time = File.GetCreationTime (path);
  1023. Assert.AreEqual (2002, time.Year, "#A1");
  1024. Assert.AreEqual (4, time.Month, "#A2");
  1025. Assert.AreEqual (6, time.Day, "#A3");
  1026. Assert.AreEqual (4, time.Hour, "#A4");
  1027. Assert.AreEqual (4, time.Second, "#A5");
  1028. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
  1029. Assert.AreEqual (2002, time.Year, "#B1");
  1030. Assert.AreEqual (4, time.Month, "#B2");
  1031. Assert.AreEqual (6, time.Day, "#B3");
  1032. Assert.AreEqual (4, time.Hour, "#B4");
  1033. Assert.AreEqual (4, time.Second, "#B5");
  1034. File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  1035. time = File.GetCreationTimeUtc (path);
  1036. Assert.AreEqual (2002, time.Year, "#C1");
  1037. Assert.AreEqual (4, time.Month, "#C2");
  1038. Assert.AreEqual (6, time.Day, "#C3");
  1039. Assert.AreEqual (4, time.Hour, "#C4");
  1040. Assert.AreEqual (4, time.Second, "#C5");
  1041. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
  1042. Assert.AreEqual (2002, time.Year, "#D1");
  1043. Assert.AreEqual (4, time.Month, "#D2");
  1044. Assert.AreEqual (6, time.Day, "#D3");
  1045. Assert.AreEqual (4, time.Hour, "#D4");
  1046. Assert.AreEqual (4, time.Second, "#D5");
  1047. } finally {
  1048. DeleteFile (path);
  1049. }
  1050. }
  1051. [Test]
  1052. public void LastAccessTime ()
  1053. {
  1054. string path = tmpFolder + Path.DirectorySeparatorChar + "lastAccessTime";
  1055. if (File.Exists (path))
  1056. File.Delete (path);
  1057. FileStream stream = null;
  1058. try {
  1059. stream = File.Create (path);
  1060. stream.Close ();
  1061. File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  1062. DateTime time = File.GetLastAccessTime (path);
  1063. Assert.AreEqual (2002, time.Year, "#A1");
  1064. Assert.AreEqual (4, time.Month, "#A2");
  1065. Assert.AreEqual (6, time.Day, "#A3");
  1066. Assert.AreEqual (4, time.Hour, "#A4");
  1067. Assert.AreEqual (4, time.Second, "#A5");
  1068. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
  1069. Assert.AreEqual (2002, time.Year, "#B1");
  1070. Assert.AreEqual (4, time.Month, "#B2");
  1071. Assert.AreEqual (6, time.Day, "#B3");
  1072. Assert.AreEqual (4, time.Hour, "#B4");
  1073. Assert.AreEqual (4, time.Second, "#B5");
  1074. File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  1075. time = File.GetLastAccessTimeUtc (path);
  1076. Assert.AreEqual (2002, time.Year, "#C1");
  1077. Assert.AreEqual (4, time.Month, "#C2");
  1078. Assert.AreEqual (6, time.Day, "#C3");
  1079. Assert.AreEqual (4, time.Hour, "#C4");
  1080. Assert.AreEqual (4, time.Second, "#C5");
  1081. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
  1082. Assert.AreEqual (2002, time.Year, "#D1");
  1083. Assert.AreEqual (4, time.Month, "#D2");
  1084. Assert.AreEqual (6, time.Day, "#D3");
  1085. Assert.AreEqual (4, time.Hour, "#D4");
  1086. Assert.AreEqual (4, time.Second, "#D5");
  1087. } finally {
  1088. if (stream != null)
  1089. stream.Close ();
  1090. DeleteFile (path);
  1091. }
  1092. }
  1093. [Test]
  1094. public void LastWriteTime ()
  1095. {
  1096. string path = tmpFolder + Path.DirectorySeparatorChar + "lastWriteTime";
  1097. if (File.Exists (path))
  1098. File.Delete (path);
  1099. FileStream stream = null;
  1100. try {
  1101. stream = File.Create (path);
  1102. stream.Close ();
  1103. File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  1104. DateTime time = File.GetLastWriteTime (path);
  1105. Assert.AreEqual (2002, time.Year, "#A1");
  1106. Assert.AreEqual (4, time.Month, "#A2");
  1107. Assert.AreEqual (6, time.Day, "#A3");
  1108. Assert.AreEqual (4, time.Hour, "#A4");
  1109. Assert.AreEqual (4, time.Second, "#A5");
  1110. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
  1111. Assert.AreEqual (2002, time.Year, "#B1");
  1112. Assert.AreEqual (4, time.Month, "#B2");
  1113. Assert.AreEqual (6, time.Day, "#B3");
  1114. Assert.AreEqual (4, time.Hour, "#B4");
  1115. Assert.AreEqual (4, time.Second, "#B5");
  1116. File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  1117. time = File.GetLastWriteTimeUtc (path);
  1118. Assert.AreEqual (2002, time.Year, "#C1");
  1119. Assert.AreEqual (4, time.Month, "#C2");
  1120. Assert.AreEqual (6, time.Day, "#C3");
  1121. Assert.AreEqual (4, time.Hour, "#C4");
  1122. Assert.AreEqual (4, time.Second, "#C5");
  1123. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
  1124. Assert.AreEqual (2002, time.Year, "#D1");
  1125. Assert.AreEqual (4, time.Month, "#D2");
  1126. Assert.AreEqual (6, time.Day, "#D3");
  1127. Assert.AreEqual (4, time.Hour, "#D4");
  1128. Assert.AreEqual (4, time.Second, "#D5");
  1129. } finally {
  1130. if (stream != null)
  1131. stream.Close ();
  1132. DeleteFile (path);
  1133. }
  1134. }
  1135. [Test]
  1136. public void GetCreationTime_Path_Null ()
  1137. {
  1138. try {
  1139. File.GetCreationTime (null as string);
  1140. Assert.Fail ("#1");
  1141. } catch (ArgumentNullException ex) {
  1142. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1143. Assert.IsNull (ex.InnerException, "#3");
  1144. Assert.IsNotNull (ex.Message, "#4");
  1145. Assert.AreEqual ("path", ex.ParamName, "#5");
  1146. }
  1147. }
  1148. [Test]
  1149. public void GetCreationTime_Path_Empty ()
  1150. {
  1151. try {
  1152. File.GetCreationTime (string.Empty);
  1153. Assert.Fail ("#1");
  1154. } catch (ArgumentException ex) {
  1155. // Empty file name is not legal
  1156. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1157. Assert.IsNull (ex.InnerException, "#3");
  1158. Assert.IsNotNull (ex.Message, "#4");
  1159. Assert.IsNull (ex.ParamName, "#5");
  1160. }
  1161. }
  1162. [Test]
  1163. public void GetCreationTime_Path_DoesNotExist ()
  1164. {
  1165. string path = tmpFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
  1166. DeleteFile (path);
  1167. DateTime time = File.GetCreationTime (path);
  1168. DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
  1169. Assert.AreEqual (expectedTime.Year, time.Year, "#1");
  1170. Assert.AreEqual (expectedTime.Month, time.Month, "#2");
  1171. Assert.AreEqual (expectedTime.Day, time.Day, "#3");
  1172. Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
  1173. Assert.AreEqual (expectedTime.Second, time.Second, "#5");
  1174. Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
  1175. }
  1176. [Test]
  1177. public void GetCreationTime_Path_Whitespace ()
  1178. {
  1179. try {
  1180. File.GetCreationTime (" ");
  1181. Assert.Fail ("#1");
  1182. } catch (ArgumentException ex) {
  1183. // The path is not of a legal form
  1184. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1185. Assert.IsNull (ex.InnerException, "#3");
  1186. Assert.IsNotNull (ex.Message, "#4");
  1187. Assert.IsNull (ex.ParamName, "#5");
  1188. }
  1189. }
  1190. [Test]
  1191. public void GetCreationTime_Path_InvalidPathChars ()
  1192. {
  1193. try {
  1194. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  1195. Assert.Fail ("#1");
  1196. } catch (ArgumentException ex) {
  1197. // Illegal characters in path
  1198. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1199. Assert.IsNull (ex.InnerException, "#3");
  1200. Assert.IsNotNull (ex.Message, "#4");
  1201. Assert.IsNull (ex.ParamName, "#5");
  1202. }
  1203. }
  1204. [Test]
  1205. public void GetCreationTimeUtc_Path_Null ()
  1206. {
  1207. try {
  1208. File.GetCreationTimeUtc (null as string);
  1209. Assert.Fail ("#1");
  1210. } catch (ArgumentNullException ex) {
  1211. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1212. Assert.IsNull (ex.InnerException, "#3");
  1213. Assert.IsNotNull (ex.Message, "#4");
  1214. Assert.AreEqual ("path", ex.ParamName, "#5");
  1215. }
  1216. }
  1217. [Test]
  1218. public void GetCreationTimeUtc_Path_Empty ()
  1219. {
  1220. try {
  1221. File.GetCreationTimeUtc (string.Empty);
  1222. Assert.Fail ("#1");
  1223. } catch (ArgumentException ex) {
  1224. // Empty file name is not legal
  1225. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1226. Assert.IsNull (ex.InnerException, "#3");
  1227. Assert.IsNotNull (ex.Message, "#4");
  1228. Assert.IsNull (ex.ParamName, "#5");
  1229. }
  1230. }
  1231. [Test]
  1232. public void GetCreationTimeUtc_Path_DoesNotExist ()
  1233. {
  1234. string path = tmpFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
  1235. DeleteFile (path);
  1236. DateTime time = File.GetCreationTimeUtc (path);
  1237. Assert.AreEqual (1601, time.Year, "#1");
  1238. Assert.AreEqual (1, time.Month, "#2");
  1239. Assert.AreEqual (1, time.Day, "#3");
  1240. Assert.AreEqual (0, time.Hour, "#4");
  1241. Assert.AreEqual (0, time.Second, "#5");
  1242. Assert.AreEqual (0, time.Millisecond, "#6");
  1243. }
  1244. [Test]
  1245. public void GetCreationTimeUtc_Path_Whitespace ()
  1246. {
  1247. try {
  1248. File.GetCreationTimeUtc (" ");
  1249. Assert.Fail ("#1");
  1250. } catch (ArgumentException ex) {
  1251. // The path is not of a legal form
  1252. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1253. Assert.IsNull (ex.InnerException, "#3");
  1254. Assert.IsNotNull (ex.Message, "#4");
  1255. Assert.IsNull (ex.ParamName, "#5");
  1256. }
  1257. }
  1258. [Test]
  1259. public void GetCreationTimeUtc_Path_InvalidPathChars ()
  1260. {
  1261. try {
  1262. File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
  1263. Assert.Fail ("#1");
  1264. } catch (ArgumentException ex) {
  1265. // Illegal characters in path
  1266. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1267. Assert.IsNull (ex.InnerException, "#3");
  1268. Assert.IsNotNull (ex.Message, "#4");
  1269. Assert.IsNull (ex.ParamName, "#5");
  1270. }
  1271. }
  1272. [Test]
  1273. public void GetLastAccessTime_Path_Null ()
  1274. {
  1275. try {
  1276. File.GetLastAccessTime (null as string);
  1277. Assert.Fail ("#1");
  1278. } catch (ArgumentNullException ex) {
  1279. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1280. Assert.IsNull (ex.InnerException, "#3");
  1281. Assert.IsNotNull (ex.Message, "#4");
  1282. Assert.AreEqual ("path", ex.ParamName, "#5");
  1283. }
  1284. }
  1285. [Test]
  1286. public void GetLastAccessTime_Path_Empty ()
  1287. {
  1288. try {
  1289. File.GetLastAccessTime (string.Empty);
  1290. Assert.Fail ("#1");
  1291. } catch (ArgumentException ex) {
  1292. // Empty file name is not legal
  1293. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1294. Assert.IsNull (ex.InnerException, "#3");
  1295. Assert.IsNotNull (ex.Message, "#4");
  1296. Assert.IsNull (ex.ParamName, "#5");
  1297. }
  1298. }
  1299. [Test]
  1300. public void GetLastAccessTime_Path_DoesNotExist ()
  1301. {
  1302. string path = tmpFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
  1303. DeleteFile (path);
  1304. DateTime time = File.GetLastAccessTime (path);
  1305. DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
  1306. Assert.AreEqual (expectedTime.Year, time.Year, "#1");
  1307. Assert.AreEqual (expectedTime.Month, time.Month, "#2");
  1308. Assert.AreEqual (expectedTime.Day, time.Day, "#3");
  1309. Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
  1310. Assert.AreEqual (expectedTime.Second, time.Second, "#5");
  1311. Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
  1312. }
  1313. [Test]
  1314. public void GetLastAccessTime_Path_Whitespace ()
  1315. {
  1316. try {
  1317. File.GetLastAccessTime (" ");
  1318. Assert.Fail ("#1");
  1319. } catch (ArgumentException ex) {
  1320. // The path is not of a legal form
  1321. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1322. Assert.IsNull (ex.InnerException, "#3");
  1323. Assert.IsNotNull (ex.Message, "#4");
  1324. Assert.IsNull (ex.ParamName, "#5");
  1325. }
  1326. }
  1327. [Test]
  1328. public void GetLastAccessTime_Path_InvalidPathChars ()
  1329. {
  1330. try {
  1331. File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
  1332. Assert.Fail ("#1");
  1333. } catch (ArgumentException ex) {
  1334. // Illegal characters in path
  1335. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1336. Assert.IsNull (ex.InnerException, "#3");
  1337. Assert.IsNotNull (ex.Message, "#4");
  1338. Assert.IsNull (ex.ParamName, "#5");
  1339. }
  1340. }
  1341. [Test]
  1342. public void GetLastAccessTimeUtc_Path_Null ()
  1343. {
  1344. try {
  1345. File.GetLastAccessTimeUtc (null as string);
  1346. Assert.Fail ("#1");
  1347. } catch (ArgumentNullException ex) {
  1348. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1349. Assert.IsNull (ex.InnerException, "#3");
  1350. Assert.IsNotNull (ex.Message, "#4");
  1351. Assert.AreEqual ("path", ex.ParamName, "#5");
  1352. }
  1353. }
  1354. [Test]
  1355. public void GetLastAccessTimeUtc_Path_Empty ()
  1356. {
  1357. try {
  1358. File.GetLastAccessTimeUtc (string.Empty);
  1359. Assert.Fail ("#1");
  1360. } catch (ArgumentException ex) {
  1361. // Empty file name is not legal
  1362. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1363. Assert.IsNull (ex.InnerException, "#3");
  1364. Assert.IsNotNull (ex.Message, "#4");
  1365. Assert.IsNull (ex.ParamName, "#5");
  1366. }
  1367. }
  1368. [Test]
  1369. public void GetLastAccessTimeUtc_Path_DoesNotExist ()
  1370. {
  1371. string path = tmpFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  1372. DeleteFile (path);
  1373. DateTime time = File.GetLastAccessTimeUtc (path);
  1374. Assert.AreEqual (1601, time.Year, "#1");
  1375. Assert.AreEqual (1, time.Month, "#2");
  1376. Assert.AreEqual (1, time.Day, "#3");
  1377. Assert.AreEqual (0, time.Hour, "#4");
  1378. Assert.AreEqual (0, time.Second, "#5");
  1379. Assert.AreEqual (0, time.Millisecond, "#6");
  1380. }
  1381. [Test]
  1382. public void GetLastAccessTimeUtc_Path_Whitespace ()
  1383. {
  1384. try {
  1385. File.GetLastAccessTimeUtc (" ");
  1386. Assert.Fail ("#1");
  1387. } catch (ArgumentException ex) {
  1388. // The path is not of a legal form
  1389. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1390. Assert.IsNull (ex.InnerException, "#3");
  1391. Assert.IsNotNull (ex.Message, "#4");
  1392. Assert.IsNull (ex.ParamName, "#5");
  1393. }
  1394. }
  1395. [Test]
  1396. public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
  1397. {
  1398. try {
  1399. File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  1400. Assert.Fail ("#1");
  1401. } catch (ArgumentException ex) {
  1402. // Illegal characters in path
  1403. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1404. Assert.IsNull (ex.InnerException, "#3");
  1405. Assert.IsNotNull (ex.Message, "#4");
  1406. Assert.IsNull (ex.ParamName, "#5");
  1407. }
  1408. }
  1409. [Test]
  1410. public void GetLastWriteTime_Path_Null ()
  1411. {
  1412. try {
  1413. File.GetLastWriteTime (null as string);
  1414. Assert.Fail ("#1");
  1415. } catch (ArgumentNullException ex) {
  1416. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1417. Assert.IsNull (ex.InnerException, "#3");
  1418. Assert.IsNotNull (ex.Message, "#4");
  1419. Assert.AreEqual ("path", ex.ParamName, "#5");
  1420. }
  1421. }
  1422. [Test]
  1423. public void GetLastWriteTime_Path_Empty ()
  1424. {
  1425. try {
  1426. File.GetLastWriteTime (string.Empty);
  1427. Assert.Fail ("#1");
  1428. } catch (ArgumentException ex) {
  1429. // Empty file name is not legal
  1430. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1431. Assert.IsNull (ex.InnerException, "#3");
  1432. Assert.IsNotNull (ex.Message, "#4");
  1433. Assert.IsNull (ex.ParamName, "#5");
  1434. }
  1435. }
  1436. [Test]
  1437. public void GetLastWriteTime_Path_DoesNotExist ()
  1438. {
  1439. string path = tmpFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  1440. DeleteFile (path);
  1441. DateTime time = File.GetLastWriteTime (path);
  1442. DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
  1443. Assert.AreEqual (expectedTime.Year, time.Year, "#1");
  1444. Assert.AreEqual (expectedTime.Month, time.Month, "#2");
  1445. Assert.AreEqual (expectedTime.Day, time.Day, "#3");
  1446. Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
  1447. Assert.AreEqual (expectedTime.Second, time.Second, "#5");
  1448. Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
  1449. }
  1450. [Test]
  1451. public void GetLastWriteTime_Path_Whitespace ()
  1452. {
  1453. try {
  1454. File.GetLastWriteTime (" ");
  1455. Assert.Fail ("#1");
  1456. } catch (ArgumentException ex) {
  1457. // The path is not of a legal form
  1458. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1459. Assert.IsNull (ex.InnerException, "#3");
  1460. Assert.IsNotNull (ex.Message, "#4");
  1461. Assert.IsNull (ex.ParamName, "#5");
  1462. }
  1463. }
  1464. [Test]
  1465. public void GetLastWriteTime_Path_InvalidPathChars ()
  1466. {
  1467. try {
  1468. File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
  1469. Assert.Fail ("#1");
  1470. } catch (ArgumentException ex) {
  1471. // Illegal characters in path
  1472. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1473. Assert.IsNull (ex.InnerException, "#3");
  1474. Assert.IsNotNull (ex.Message, "#4");
  1475. Assert.IsNull (ex.ParamName, "#5");
  1476. }
  1477. }
  1478. [Test]
  1479. public void GetLastWriteTimeUtc_Path_Null ()
  1480. {
  1481. try {
  1482. File.GetLastWriteTimeUtc (null as string);
  1483. Assert.Fail ("#1");
  1484. } catch (ArgumentNullException ex) {
  1485. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1486. Assert.IsNull (ex.InnerException, "#3");
  1487. Assert.IsNotNull (ex.Message, "#4");
  1488. Assert.AreEqual ("path", ex.ParamName, "#5");
  1489. }
  1490. }
  1491. [Test]
  1492. public void GetLastWriteTimeUtc_Path_Empty ()
  1493. {
  1494. try {
  1495. File.GetLastWriteTimeUtc (string.Empty);
  1496. Assert.Fail ("#1");
  1497. } catch (ArgumentException ex) {
  1498. // Empty file name is not legal
  1499. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1500. Assert.IsNull (ex.InnerException, "#3");
  1501. Assert.IsNotNull (ex.Message, "#4");
  1502. Assert.IsNull (ex.ParamName, "#5");
  1503. }
  1504. }
  1505. [Test]
  1506. public void GetLastWriteTimeUtc_Path_DoesNotExist ()
  1507. {
  1508. string path = tmpFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
  1509. DeleteFile (path);
  1510. DateTime time = File.GetLastWriteTimeUtc (path);
  1511. Assert.AreEqual (1601, time.Year, "#1");
  1512. Assert.AreEqual (1, time.Month, "#2");
  1513. Assert.AreEqual (1, time.Day, "#3");
  1514. Assert.AreEqual (0, time.Hour, "#4");
  1515. Assert.AreEqual (0, time.Second, "#5");
  1516. Assert.AreEqual (0, time.Millisecond, "#6");
  1517. }
  1518. [Test]
  1519. public void GetLastWriteTimeUtc_Path_Whitespace ()
  1520. {
  1521. try {
  1522. File.GetLastWriteTimeUtc (" ");
  1523. Assert.Fail ("#1");
  1524. } catch (ArgumentException ex) {
  1525. // The path is not of a legal form
  1526. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1527. Assert.IsNull (ex.InnerException, "#3");
  1528. Assert.IsNotNull (ex.Message, "#4");
  1529. Assert.IsNull (ex.ParamName, "#5");
  1530. }
  1531. }
  1532. [Test]
  1533. public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
  1534. {
  1535. try {
  1536. File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
  1537. Assert.Fail ("#1");
  1538. } catch (ArgumentException ex) {
  1539. // Illegal characters in path
  1540. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1541. Assert.IsNull (ex.InnerException, "#3");
  1542. Assert.IsNotNull (ex.Message, "#4");
  1543. Assert.IsNull (ex.ParamName, "#5");
  1544. }
  1545. }
  1546. [Test]
  1547. public void FileStreamClose ()
  1548. {
  1549. string path = tmpFolder + Path.DirectorySeparatorChar + "FileStreamClose";
  1550. FileStream stream = null;
  1551. try {
  1552. stream = File.Create (path);
  1553. stream.Close ();
  1554. File.Delete (path);
  1555. } finally {
  1556. if (stream != null)
  1557. stream.Close ();
  1558. DeleteFile (path);
  1559. }
  1560. }
  1561. // SetCreationTime and SetCreationTimeUtc exceptions
  1562. [Test]
  1563. public void SetCreationTime_Path_Null ()
  1564. {
  1565. try {
  1566. File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1567. Assert.Fail ("#1");
  1568. } catch (ArgumentNullException ex) {
  1569. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1570. Assert.IsNull (ex.InnerException, "#3");
  1571. Assert.IsNotNull (ex.Message, "#4");
  1572. Assert.AreEqual ("path", ex.ParamName, "#5");
  1573. }
  1574. }
  1575. [Test]
  1576. public void SetCreationTime_Path_Empty ()
  1577. {
  1578. try {
  1579. File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
  1580. Assert.Fail ("#1");
  1581. } catch (ArgumentException ex) {
  1582. // Empty file name is not legal
  1583. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1584. Assert.IsNull (ex.InnerException, "#3");
  1585. Assert.IsNotNull (ex.Message, "#4");
  1586. Assert.IsNull (ex.ParamName, "#5");
  1587. }
  1588. }
  1589. [Test]
  1590. public void SetCreationTime_Path_Whitespace ()
  1591. {
  1592. try {
  1593. File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1594. Assert.Fail ("#1");
  1595. } catch (ArgumentException ex) {
  1596. // The path is not of a legal form
  1597. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1598. Assert.IsNull (ex.InnerException, "#3");
  1599. Assert.IsNotNull (ex.Message, "#4");
  1600. Assert.IsNull (ex.ParamName, "#5");
  1601. }
  1602. }
  1603. [Test]
  1604. public void SetCreationTime_Path_InvalidPathChars ()
  1605. {
  1606. // On Unix there are no invalid path chars.
  1607. if (Path.InvalidPathChars.Length > 1) {
  1608. try {
  1609. File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
  1610. new DateTime (2000, 12, 12, 11, 59, 59));
  1611. Assert.Fail ("#1");
  1612. } catch (ArgumentException ex) {
  1613. // Illegal characters in path
  1614. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1615. Assert.IsNull (ex.InnerException, "#3");
  1616. Assert.IsNotNull (ex.Message, "#4");
  1617. Assert.IsNull (ex.ParamName, "#5");
  1618. }
  1619. }
  1620. }
  1621. [Test]
  1622. public void SetCreationTime_Path_DoesNotExist ()
  1623. {
  1624. string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
  1625. DeleteFile (path);
  1626. try {
  1627. File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1628. Assert.Fail ("#1");
  1629. } catch (FileNotFoundException ex) {
  1630. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1631. Assert.AreEqual (path, ex.FileName, "#3");
  1632. Assert.IsNull (ex.InnerException, "#4");
  1633. Assert.IsNotNull (ex.Message, "#5");
  1634. }
  1635. }
  1636. // [Test]
  1637. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1638. // public void SetCreationTimeArgumentOutOfRangeException1 ()
  1639. // {
  1640. // string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
  1641. // FileStream stream = null;
  1642. // DeleteFile (path);
  1643. // try {
  1644. // stream = File.Create (path);
  1645. // stream.Close ();
  1646. // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1647. // } finally {
  1648. // if (stream != null)
  1649. // stream.Close ();
  1650. // DeleteFile (path);
  1651. // }
  1652. // }
  1653. [Test]
  1654. public void SetCreationTime_FileLock ()
  1655. {
  1656. string path = tmpFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
  1657. DeleteFile (path);
  1658. FileStream stream = null;
  1659. try {
  1660. stream = File.Create (path);
  1661. try {
  1662. File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1663. Assert.Fail ("#1");
  1664. } catch (IOException ex) {
  1665. // The process cannot access the file '...'
  1666. // because it is being used by another process
  1667. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  1668. Assert.IsNull (ex.InnerException, "#3");
  1669. Assert.IsNotNull (ex.Message, "#4");
  1670. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  1671. }
  1672. } finally {
  1673. if (stream != null)
  1674. stream.Close ();
  1675. DeleteFile (path);
  1676. }
  1677. }
  1678. [Test]
  1679. public void SetCreationTimeUtc_Path_Null ()
  1680. {
  1681. try {
  1682. File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1683. Assert.Fail ("#1");
  1684. } catch (ArgumentNullException ex) {
  1685. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1686. Assert.IsNull (ex.InnerException, "#3");
  1687. Assert.IsNotNull (ex.Message, "#4");
  1688. Assert.AreEqual ("path", ex.ParamName, "#5");
  1689. }
  1690. }
  1691. [Test]
  1692. public void SetCreationTimeUtc_Path_Empty ()
  1693. {
  1694. try {
  1695. File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
  1696. Assert.Fail ("#1");
  1697. } catch (ArgumentException ex) {
  1698. // Empty file name is not legal
  1699. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1700. Assert.IsNull (ex.InnerException, "#3");
  1701. Assert.IsNotNull (ex.Message, "#4");
  1702. Assert.IsNull (ex.ParamName, "#5");
  1703. }
  1704. }
  1705. [Test]
  1706. public void SetCreationTimeUtc_Path_Whitespace ()
  1707. {
  1708. try {
  1709. File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1710. Assert.Fail ("#1");
  1711. } catch (ArgumentException ex) {
  1712. // The path is not of a legal form
  1713. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1714. Assert.IsNull (ex.InnerException, "#3");
  1715. Assert.IsNotNull (ex.Message, "#4");
  1716. Assert.IsNull (ex.ParamName, "#5");
  1717. }
  1718. }
  1719. [Test]
  1720. public void SetCreationTimeUtc_Path_InvalidPathChars ()
  1721. {
  1722. // On Unix there are no invalid path chars.
  1723. if (Path.InvalidPathChars.Length > 1) {
  1724. try {
  1725. File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
  1726. new DateTime (2000, 12, 12, 11, 59, 59));
  1727. Assert.Fail ("#1");
  1728. } catch (ArgumentException ex) {
  1729. // Illegal characters in path
  1730. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1731. Assert.IsNull (ex.InnerException, "#3");
  1732. Assert.IsNotNull (ex.Message, "#4");
  1733. Assert.IsNull (ex.ParamName, "#5");
  1734. }
  1735. }
  1736. }
  1737. [Test]
  1738. public void SetCreationTimeUtc_Path_DoesNotExist ()
  1739. {
  1740. string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
  1741. DeleteFile (path);
  1742. try {
  1743. File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1744. Assert.Fail ("#1");
  1745. } catch (FileNotFoundException ex) {
  1746. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1747. Assert.AreEqual (path, ex.FileName, "#3");
  1748. Assert.IsNull (ex.InnerException, "#3");
  1749. Assert.IsNotNull (ex.Message, "#4");
  1750. }
  1751. }
  1752. // [Test]
  1753. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1754. // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
  1755. // {
  1756. // string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
  1757. // DeleteFile (path);
  1758. // FileStream stream = null;
  1759. // try {
  1760. // stream = File.Create (path);
  1761. // stream.Close ();
  1762. // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1763. // } finally {
  1764. // if (stream != null)
  1765. // stream.Close();
  1766. // DeleteFile (path);
  1767. // }
  1768. // }
  1769. [Test]
  1770. public void SetCreationTimeUtc_FileLock ()
  1771. {
  1772. string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
  1773. DeleteFile (path);
  1774. FileStream stream = null;
  1775. try {
  1776. stream = File.Create (path);
  1777. try {
  1778. File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1779. Assert.Fail ("#1");
  1780. } catch (IOException ex) {
  1781. // The process cannot access the file "..."
  1782. // because it is being used by another process
  1783. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  1784. Assert.IsNull (ex.InnerException, "#3");
  1785. Assert.IsNotNull (ex.Message, "#4");
  1786. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  1787. }
  1788. } finally {
  1789. if (stream != null)
  1790. stream.Close ();
  1791. DeleteFile (path);
  1792. }
  1793. }
  1794. // SetLastAccessTime and SetLastAccessTimeUtc exceptions
  1795. [Test]
  1796. public void SetLastAccessTime_Path_Null ()
  1797. {
  1798. try {
  1799. File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1800. Assert.Fail ("#1");
  1801. } catch (ArgumentNullException ex) {
  1802. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1803. Assert.IsNull (ex.InnerException, "#3");
  1804. Assert.IsNotNull (ex.Message, "#4");
  1805. Assert.AreEqual ("path", ex.ParamName, "#5");
  1806. }
  1807. }
  1808. [Test]
  1809. public void SetLastAccessTime_Path_Empty ()
  1810. {
  1811. try {
  1812. File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
  1813. Assert.Fail ("#1");
  1814. } catch (ArgumentException ex) {
  1815. // Empty file name is not legal
  1816. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1817. Assert.IsNull (ex.InnerException, "#3");
  1818. Assert.IsNotNull (ex.Message, "#4");
  1819. Assert.IsNull (ex.ParamName, "#5");
  1820. }
  1821. }
  1822. [Test]
  1823. public void SetLastAccessTime_Path_Whitespace ()
  1824. {
  1825. try {
  1826. File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1827. Assert.Fail ("#1");
  1828. } catch (ArgumentException ex) {
  1829. // The path is not of a legal form
  1830. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1831. Assert.IsNull (ex.InnerException, "#3");
  1832. Assert.IsNotNull (ex.Message, "#4");
  1833. Assert.IsNull (ex.ParamName, "#5");
  1834. }
  1835. }
  1836. [Test]
  1837. public void SetLastAccessTime_Path_InvalidPathChars ()
  1838. {
  1839. // On Unix there are no invalid path chars.
  1840. if (Path.InvalidPathChars.Length > 1) {
  1841. try {
  1842. File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
  1843. new DateTime (2000, 12, 12, 11, 59, 59));
  1844. Assert.Fail ("#1");
  1845. } catch (ArgumentException ex) {
  1846. // Illegal characters in path
  1847. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1848. Assert.IsNull (ex.InnerException, "#3");
  1849. Assert.IsNotNull (ex.Message, "#4");
  1850. Assert.IsNull (ex.ParamName, "#5");
  1851. }
  1852. }
  1853. }
  1854. [Test]
  1855. public void SetLastAccessTime_Path_DoesNotExist ()
  1856. {
  1857. string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
  1858. DeleteFile (path);
  1859. try {
  1860. File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1861. Assert.Fail ("#1");
  1862. } catch (FileNotFoundException ex) {
  1863. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1864. Assert.AreEqual (path, ex.FileName, "#3");
  1865. Assert.IsNull (ex.InnerException, "#4");
  1866. Assert.IsNotNull (ex.Message, "#5");
  1867. }
  1868. }
  1869. // [Test]
  1870. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1871. // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
  1872. // {
  1873. // string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
  1874. // DeleteFile (path);
  1875. // FileStream stream = null;
  1876. // try {
  1877. // stream = File.Create (path);
  1878. // stream.Close ();
  1879. // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1880. // } finally {
  1881. // if (stream != null)
  1882. // stream.Close ();
  1883. // DeleteFile (path);
  1884. // }
  1885. // }
  1886. [Test]
  1887. public void SetLastAccessTime_FileLock ()
  1888. {
  1889. string path = tmpFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
  1890. DeleteFile (path);
  1891. FileStream stream = null;
  1892. try {
  1893. stream = File.Create (path);
  1894. try {
  1895. File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1896. Assert.Fail ("#1");
  1897. } catch (IOException ex) {
  1898. // The process cannot access the file "..."
  1899. // because it is being used by another process
  1900. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  1901. Assert.IsNull (ex.InnerException, "#3");
  1902. Assert.IsNotNull (ex.Message, "#4");
  1903. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  1904. }
  1905. } finally {
  1906. if (stream != null)
  1907. stream.Close ();
  1908. DeleteFile (path);
  1909. }
  1910. }
  1911. [Test]
  1912. public void SetLastAccessTimeUtc_Path_Null ()
  1913. {
  1914. try {
  1915. File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1916. Assert.Fail ("#1");
  1917. } catch (ArgumentNullException ex) {
  1918. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1919. Assert.IsNull (ex.InnerException, "#3");
  1920. Assert.IsNotNull (ex.Message, "#4");
  1921. Assert.AreEqual ("path", ex.ParamName, "#5");
  1922. }
  1923. }
  1924. [Test]
  1925. public void SetCLastAccessTimeUtc_Path_Empty ()
  1926. {
  1927. try {
  1928. File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
  1929. Assert.Fail ("#1");
  1930. } catch (ArgumentException ex) {
  1931. // Empty file name is not legal
  1932. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1933. Assert.IsNull (ex.InnerException, "#3");
  1934. Assert.IsNotNull (ex.Message, "#4");
  1935. Assert.IsNull (ex.ParamName, "#5");
  1936. }
  1937. }
  1938. [Test]
  1939. public void SetLastAccessTimeUtc_Path_Whitespace ()
  1940. {
  1941. try {
  1942. File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1943. Assert.Fail ("#1");
  1944. } catch (ArgumentException ex) {
  1945. // The path is not of a legal form
  1946. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1947. Assert.IsNull (ex.InnerException, "#3");
  1948. Assert.IsNotNull (ex.Message, "#4");
  1949. Assert.IsNull (ex.ParamName, "#5");
  1950. }
  1951. }
  1952. [Test]
  1953. public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
  1954. {
  1955. // On Unix there are no invalid path chars.
  1956. if (Path.InvalidPathChars.Length > 1) {
  1957. try {
  1958. File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
  1959. new DateTime (2000, 12, 12, 11, 59, 59));
  1960. Assert.Fail ("#1");
  1961. } catch (ArgumentException ex) {
  1962. // Illegal characters in path
  1963. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1964. Assert.IsNull (ex.InnerException, "#3");
  1965. Assert.IsNotNull (ex.Message, "#4");
  1966. Assert.IsNull (ex.ParamName, "#5");
  1967. }
  1968. }
  1969. }
  1970. [Test]
  1971. public void SetLastAccessTimeUtc_Path_DoesNotExist ()
  1972. {
  1973. string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
  1974. DeleteFile (path);
  1975. try {
  1976. File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1977. Assert.Fail ("#1");
  1978. } catch (FileNotFoundException ex) {
  1979. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1980. Assert.AreEqual (path, ex.FileName, "#3");
  1981. Assert.IsNull (ex.InnerException, "#4");
  1982. Assert.IsNotNull (ex.Message, "#5");
  1983. }
  1984. }
  1985. // [Test]
  1986. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1987. // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
  1988. // {
  1989. // string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
  1990. // DeleteFile (path);
  1991. // FileStream stream = null;
  1992. // try {
  1993. // stream = File.Create (path);
  1994. // stream.Close ();
  1995. // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1996. // } finally {
  1997. // if (stream != null)
  1998. // stream.Close ();
  1999. // DeleteFile (path);
  2000. // }
  2001. // }
  2002. [Test]
  2003. public void SetLastAccessTimeUtc_FileLock ()
  2004. {
  2005. string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
  2006. DeleteFile (path);
  2007. FileStream stream = null;
  2008. try {
  2009. stream = File.Create (path);
  2010. try {
  2011. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  2012. Assert.Fail ("#1");
  2013. } catch (IOException ex) {
  2014. // The process cannot access the file "..."
  2015. // because it is being used by another process
  2016. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  2017. Assert.IsNull (ex.InnerException, "#3");
  2018. Assert.IsNotNull (ex.Message, "#4");
  2019. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  2020. }
  2021. } finally {
  2022. if (stream != null)
  2023. stream.Close ();
  2024. DeleteFile (path);
  2025. }
  2026. }
  2027. // SetLastWriteTime and SetLastWriteTimeUtc exceptions
  2028. [Test]
  2029. public void SetLastWriteTime_Path_Null ()
  2030. {
  2031. try {
  2032. File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  2033. Assert.Fail ("#1");
  2034. } catch (ArgumentNullException ex) {
  2035. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2036. Assert.IsNull (ex.InnerException, "#3");
  2037. Assert.IsNotNull (ex.Message, "#4");
  2038. Assert.AreEqual ("path", ex.ParamName, "#5");
  2039. }
  2040. }
  2041. [Test]
  2042. public void SetLastWriteTime_Path_Empty ()
  2043. {
  2044. try {
  2045. File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
  2046. Assert.Fail ("#1");
  2047. } catch (ArgumentException ex) {
  2048. // Empty file name is not legal
  2049. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  2050. Assert.IsNull (ex.InnerException, "#3");
  2051. Assert.IsNotNull (ex.Message, "#4");
  2052. Assert.IsNull (ex.ParamName, "#5");
  2053. }
  2054. }
  2055. [Test]
  2056. public void SetLastWriteTime_Path_Whitespace ()
  2057. {
  2058. try {
  2059. File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  2060. Assert.Fail ("#1");
  2061. } catch (ArgumentException ex) {
  2062. // The path is not of a legal form
  2063. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  2064. Assert.IsNull (ex.InnerException, "#3");
  2065. Assert.IsNotNull (ex.Message, "#4");
  2066. Assert.IsNull (ex.ParamName, "#5");
  2067. }
  2068. }
  2069. [Test]
  2070. public void SetLastWriteTime_Path_InvalidPathChars ()
  2071. {
  2072. // On Unix there are no invalid path chars.
  2073. if (Path.InvalidPathChars.Length > 1) {
  2074. try {
  2075. File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
  2076. new DateTime (2000, 12, 12, 11, 59, 59));
  2077. Assert.Fail ("#1");
  2078. } catch (ArgumentException ex) {
  2079. // Illegal characters in path
  2080. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  2081. Assert.IsNull (ex.InnerException, "#3");
  2082. Assert.IsNotNull (ex.Message, "#4");
  2083. Assert.IsNull (ex.ParamName, "#5");
  2084. }
  2085. }
  2086. }
  2087. [Test]
  2088. public void SetLastWriteTime_Path_DoesNotExist ()
  2089. {
  2090. string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
  2091. DeleteFile (path);
  2092. try {
  2093. File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  2094. Assert.Fail ("#1");
  2095. } catch (FileNotFoundException ex) {
  2096. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  2097. Assert.AreEqual (path, ex.FileName, "#3");
  2098. Assert.IsNull (ex.InnerException, "#4");
  2099. Assert.IsNotNull (ex.Message, "#5");
  2100. }
  2101. }
  2102. // [Test]
  2103. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  2104. // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
  2105. // {
  2106. // string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
  2107. // DeleteFile (path);
  2108. // FileStream stream = null;
  2109. // try {
  2110. // stream = File.Create (path);
  2111. // stream.Close ();
  2112. // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  2113. // } finally {
  2114. // if (stream != null)
  2115. // stream.Close ();
  2116. // DeleteFile (path);
  2117. // }
  2118. // }
  2119. [Test]
  2120. public void SetLastWriteTime_FileLock ()
  2121. {
  2122. string path = tmpFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
  2123. DeleteFile (path);
  2124. FileStream stream = null;
  2125. try {
  2126. stream = File.Create (path);
  2127. try {
  2128. File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  2129. Assert.Fail ("#1");
  2130. } catch (IOException ex) {
  2131. // The process cannot access the file '...'
  2132. // because it is being used by another process
  2133. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  2134. Assert.IsNull (ex.InnerException, "#3");
  2135. Assert.IsNotNull (ex.Message, "#4");
  2136. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  2137. }
  2138. } finally {
  2139. if (stream != null)
  2140. stream.Close ();
  2141. DeleteFile (path);
  2142. }
  2143. }
  2144. [Test]
  2145. public void SetLastWriteTimeUtc_Path_Null ()
  2146. {
  2147. try {
  2148. File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  2149. Assert.Fail ("#1");
  2150. } catch (ArgumentNullException ex) {
  2151. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  2152. Assert.IsNull (ex.InnerException, "#3");
  2153. Assert.IsNotNull (ex.Message, "#4");
  2154. Assert.AreEqual ("path", ex.ParamName, "#5");
  2155. }
  2156. }
  2157. [Test]
  2158. public void SetLastWriteTimeUtc_Path_Empty ()
  2159. {
  2160. try {
  2161. File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
  2162. Assert.Fail ("#1");
  2163. } catch (ArgumentException ex) {
  2164. // Empty file name is not legal
  2165. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  2166. Assert.IsNull (ex.InnerException, "#3");
  2167. Assert.IsNotNull (ex.Message, "#4");
  2168. Assert.IsNull (ex.ParamName, "#5");
  2169. }
  2170. }
  2171. [Test]
  2172. public void SetLastWriteTimeUtc_Path_Whitespace ()
  2173. {
  2174. try {
  2175. File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  2176. Assert.Fail ("#1");
  2177. } catch (ArgumentException ex) {
  2178. // The path is not of a legal form
  2179. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  2180. Assert.IsNull (ex.InnerException, "#3");
  2181. Assert.IsNotNull (ex.Message, "#4");
  2182. Assert.IsNull (ex.ParamName, "#5");
  2183. }
  2184. }
  2185. [Test]
  2186. public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
  2187. {
  2188. // On Unix there are no invalid path chars.
  2189. if (Path.InvalidPathChars.Length > 1) {
  2190. try {
  2191. File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
  2192. new DateTime (2000, 12, 12, 11, 59, 59));
  2193. Assert.Fail ("#1");
  2194. } catch (ArgumentException ex) {
  2195. // Illegal characters in path
  2196. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  2197. Assert.IsNull (ex.InnerException, "#3");
  2198. Assert.IsNotNull (ex.Message, "#4");
  2199. Assert.IsNull (ex.ParamName, "#5");
  2200. }
  2201. }
  2202. }
  2203. [Test]
  2204. public void SetLastWriteTimeUtc_Path_DoesNotExist ()
  2205. {
  2206. string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
  2207. DeleteFile (path);
  2208. try {
  2209. File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  2210. Assert.Fail ("#1");
  2211. } catch (FileNotFoundException ex) {
  2212. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  2213. Assert.AreEqual (path, ex.FileName, "#3");
  2214. Assert.IsNull (ex.InnerException, "#4");
  2215. Assert.IsNotNull (ex.Message, "#5");
  2216. }
  2217. }
  2218. // [Test]
  2219. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  2220. // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
  2221. // {
  2222. // string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
  2223. // DeleteFile (path);
  2224. // FileStream stream = null;
  2225. // try {
  2226. // stream = File.Create (path);
  2227. // stream.Close ();
  2228. // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  2229. // } finally {
  2230. // if (stream != null)
  2231. // stream.Close ();
  2232. // DeleteFile (path);
  2233. // }
  2234. // }
  2235. [Test]
  2236. public void SetLastWriteTimeUtc_FileLock ()
  2237. {
  2238. string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
  2239. DeleteFile (path);
  2240. FileStream stream = null;
  2241. try {
  2242. stream = File.Create (path);
  2243. try {
  2244. File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  2245. Assert.Fail ("#1");
  2246. } catch (IOException ex) {
  2247. // The process cannot access the file '...'
  2248. // because it is being used by another process
  2249. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  2250. Assert.IsNull (ex.InnerException, "#3");
  2251. Assert.IsNotNull (ex.Message, "#4");
  2252. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
  2253. }
  2254. } finally {
  2255. if (stream != null)
  2256. stream.Close ();
  2257. DeleteFile (path);
  2258. }
  2259. }
  2260. [Test]
  2261. public void OpenAppend ()
  2262. {
  2263. string fn = Path.GetTempFileName ();
  2264. using (FileStream s = File.Open (fn, FileMode.Append)) {
  2265. }
  2266. DeleteFile (fn);
  2267. }
  2268. void Position (long value)
  2269. {
  2270. using (FileStream fs = File.OpenRead (testfile)) {
  2271. fs.Position = value;
  2272. Assert.AreEqual (value, fs.Position, "Position");
  2273. Assert.AreEqual (1, fs.Length, "Length");
  2274. }
  2275. }
  2276. [Test]
  2277. public void Position_Small ()
  2278. {
  2279. Position (Int32.MaxValue);
  2280. }
  2281. [Test]
  2282. [Category ("LargeFileSupport")]
  2283. public void Position_Large ()
  2284. {
  2285. // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
  2286. Position ((long) Int32.MaxValue + 1);
  2287. }
  2288. void Seek (long value)
  2289. {
  2290. using (FileStream fs = File.OpenRead (testfile)) {
  2291. fs.Seek (value, SeekOrigin.Begin);
  2292. Assert.AreEqual (value, fs.Position, "Position");
  2293. Assert.AreEqual (1, fs.Length, "Length");
  2294. }
  2295. }
  2296. [Test]
  2297. public void Seek_Small ()
  2298. {
  2299. Seek (Int32.MaxValue);
  2300. }
  2301. [Test]
  2302. [Category ("LargeFileSupport")]
  2303. public void Seek_Large ()
  2304. {
  2305. // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
  2306. Seek ((long) Int32.MaxValue + 1);
  2307. }
  2308. void LockUnlock (long value)
  2309. {
  2310. using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
  2311. fs.Lock (value - 1, 1);
  2312. fs.Unlock (value - 1, 1);
  2313. fs.Lock (0, value);
  2314. fs.Unlock (0, value);
  2315. }
  2316. }
  2317. [Test]
  2318. public void Lock_Small ()
  2319. {
  2320. LockUnlock ((long) Int32.MaxValue);
  2321. }
  2322. [Test]
  2323. [Category ("LargeFileSupport")]
  2324. public void Lock_Large ()
  2325. {
  2326. // note: already worked without HAVE_LARGE_FILE_SUPPORT
  2327. LockUnlock ((long) Int32.MaxValue + 1);
  2328. }
  2329. [Test]
  2330. public void ReadWriteAllText ()
  2331. {
  2332. // The MSDN docs said something about
  2333. // not including a final new line. it looks
  2334. // like that was not true. I'm not sure what
  2335. // that was talking about
  2336. read_all (string.Empty);
  2337. read_all ("\r");
  2338. read_all ("\n");
  2339. read_all ("\r\n");
  2340. read_all ("a\r");
  2341. read_all ("a\n");
  2342. read_all ("a\r\n");
  2343. read_all ("a\ra");
  2344. read_all ("a\na");
  2345. read_all ("a\r\na");
  2346. read_all ("a");
  2347. read_all ("\r\r");
  2348. read_all ("\n\n");
  2349. read_all ("\r\n\r\n");
  2350. }
  2351. [Test]
  2352. public void ReplaceTest ()
  2353. {
  2354. string tmp = Path.Combine (tmpFolder, "ReplaceTest");
  2355. Directory.CreateDirectory (tmp);
  2356. string origFile = Path.Combine (tmp, "origFile");
  2357. string replaceFile = Path.Combine (tmp, "replaceFile");
  2358. string backupFile = Path.Combine (tmp, "backupFile");
  2359. using (StreamWriter sw = File.CreateText (origFile)) {
  2360. sw.WriteLine ("origFile");
  2361. }
  2362. using (StreamWriter sw = File.CreateText (replaceFile)) {
  2363. sw.WriteLine ("replaceFile");
  2364. }
  2365. using (StreamWriter sw = File.CreateText (backupFile)) {
  2366. sw.WriteLine ("backupFile");
  2367. }
  2368. File.Replace (origFile, replaceFile, backupFile);
  2369. Assert.IsFalse (File.Exists (origFile), "#1");
  2370. using (StreamReader sr = File.OpenText (replaceFile)) {
  2371. string txt = sr.ReadLine ();
  2372. Assert.AreEqual ("origFile", txt, "#2");
  2373. }
  2374. using (StreamReader sr = File.OpenText (backupFile)) {
  2375. string txt = sr.ReadLine ();
  2376. Assert.AreEqual ("replaceFile", txt, "#3");
  2377. }
  2378. }
  2379. static bool RunningOnUnix {
  2380. get {
  2381. int p = (int) Environment.OSVersion.Platform;
  2382. return ((p == 4) || (p == 128) || (p == 6));
  2383. }
  2384. }
  2385. void DeleteFile (string path)
  2386. {
  2387. if (File.Exists (path))
  2388. File.Delete (path);
  2389. }
  2390. void DeleteDirectory (string path)
  2391. {
  2392. if (Directory.Exists (path))
  2393. Directory.Delete (path, true);
  2394. }
  2395. void read_all (string s)
  2396. {
  2397. string f = Path.GetTempFileName ();
  2398. try {
  2399. File.WriteAllText (f, s);
  2400. string r = File.ReadAllText (f);
  2401. Assert.AreEqual (s, r);
  2402. } finally {
  2403. DeleteFile (f);
  2404. }
  2405. }
  2406. void MoveTest (FileAccess acc, FileShare share, bool works)
  2407. {
  2408. // use TEMP so since the default location (right along with the assemblies)
  2409. // will get access denied when running under some environment (e.g. iOS devices)
  2410. var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
  2411. File.Delete (file + ".old");
  2412. using (var v = File.Create (file)) { }
  2413. using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
  2414. try {
  2415. File.Move(file, file + ".old");
  2416. if (!works)
  2417. Assert.Fail ("Move with ({0}) and ({1}) did not fail", acc, share);
  2418. } catch (IOException) {
  2419. if (works)
  2420. Assert.Fail ("Move with ({0}) and ({1}) did fail", acc, share);
  2421. }
  2422. }
  2423. }
  2424. [Test]
  2425. public void MoveTest ()
  2426. {
  2427. MoveTest (FileAccess.Read, FileShare.None, false);
  2428. MoveTest (FileAccess.Read, FileShare.Read, false);
  2429. MoveTest (FileAccess.Read, FileShare.Write, false);
  2430. MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
  2431. MoveTest (FileAccess.Read, FileShare.Delete, true);
  2432. MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
  2433. MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
  2434. MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
  2435. MoveTest (FileAccess.Write, FileShare.None, false);
  2436. MoveTest (FileAccess.Write, FileShare.Read, false);
  2437. MoveTest (FileAccess.Write, FileShare.Write, false);
  2438. MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
  2439. MoveTest (FileAccess.Write, FileShare.Delete, true);
  2440. MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
  2441. MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
  2442. MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
  2443. MoveTest (FileAccess.ReadWrite, FileShare.None, false);
  2444. MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
  2445. MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
  2446. MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
  2447. MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
  2448. MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
  2449. MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
  2450. MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);
  2451. }
  2452. [DllImport ("libc", SetLastError=true)]
  2453. public static extern int symlink (string oldpath, string newpath);
  2454. [Test]
  2455. #if __TVOS__
  2456. [Ignore ("See bug #59239")]
  2457. #endif
  2458. public void SymLinkLoop ()
  2459. {
  2460. if (!RunningOnUnix)
  2461. Assert.Ignore ("Symlink are hard on windows");
  2462. var name1 = Path.GetRandomFileName ();
  2463. var name2 = Path.GetRandomFileName ();
  2464. var path1 = Path.Combine (Path.GetTempPath (), name1);
  2465. var path2 = Path.Combine (Path.GetTempPath (), name2);
  2466. File.Delete (path1);
  2467. File.Delete (path2);
  2468. try {
  2469. if (symlink (path1, path2) != 0)
  2470. Assert.Fail ("symlink #1 failed with errno={0}", Marshal.GetLastWin32Error ());
  2471. if (symlink (path2, path1) != 0)
  2472. Assert.Fail ("symlink #2 failed with errno={0}", Marshal.GetLastWin32Error ());
  2473. Assert.IsTrue (File.Exists (path1), "File.Exists must return true for path1 symlink loop");
  2474. Assert.IsTrue (File.Exists (path2), "File.Exists must return true for path2 symlink loop");
  2475. try {
  2476. using (var f = File.Open (path1, FileMode.Open, FileAccess.Read)) {
  2477. Assert.Fail ("File.Open must throw for symlink loops");
  2478. }
  2479. } catch (IOException ex) {
  2480. Assert.AreEqual (0x80070781u, (uint)ex.HResult, "Ensure HRESULT is correct");
  2481. }
  2482. File.Delete (path1); //Delete must not throw and must work
  2483. Assert.IsFalse (File.Exists (path1), "File.Delete must delete symlink loops");
  2484. } finally {
  2485. try {
  2486. File.Delete (path1);
  2487. File.Delete (path2);
  2488. } catch (IOException) {
  2489. //Don't double fault any exception from the tests.
  2490. }
  2491. }
  2492. }
  2493. }
  2494. }