FileStreamTest.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. // FileStreamTests.cs - NUnit2 Test Cases for System.IO.FileStream class
  2. //
  3. // Authors:
  4. // Ville Palo ([email protected])
  5. // Gert Driesen ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) Ville Palo
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  11. //
  12. using NUnit.Framework;
  13. using System;
  14. using System.IO;
  15. using System.Runtime.InteropServices;
  16. using System.Text;
  17. using System.Threading;
  18. using Microsoft.Win32.SafeHandles;
  19. namespace MonoTests.System.IO
  20. {
  21. [TestFixture]
  22. public class FileStreamTest
  23. {
  24. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  25. static readonly char DSC = Path.DirectorySeparatorChar;
  26. [TearDown]
  27. public void TearDown ()
  28. {
  29. if (Directory.Exists (TempFolder))
  30. Directory.Delete (TempFolder, true);
  31. }
  32. [SetUp]
  33. public void SetUp ()
  34. {
  35. if (Directory.Exists (TempFolder))
  36. Directory.Delete (TempFolder, true);
  37. Directory.CreateDirectory (TempFolder);
  38. }
  39. public void TestCtr ()
  40. {
  41. string path = TempFolder + DSC + "testfilestream.tmp.1";
  42. DeleteFile (path);
  43. FileStream stream = null;
  44. try {
  45. stream = new FileStream (path, FileMode.Create);
  46. } finally {
  47. if (stream != null)
  48. stream.Close ();
  49. DeleteFile (path);
  50. }
  51. }
  52. [Test]
  53. [ExpectedException (typeof (ArgumentException))]
  54. public void CtorArgumentException1 ()
  55. {
  56. FileStream stream;
  57. stream = new FileStream ("", FileMode.Create);
  58. stream.Close ();
  59. }
  60. [Test]
  61. [ExpectedException (typeof (ArgumentNullException))]
  62. public void CtorArgumentNullException ()
  63. {
  64. FileStream stream = new FileStream (null, FileMode.Create);
  65. stream.Close ();
  66. }
  67. [Test]
  68. public void CtorFileNotFoundException_Mode_Open ()
  69. {
  70. const string file_name = "thisfileshouldnotexist.test";
  71. // absolute path
  72. string path = TempFolder + DSC + file_name;
  73. DeleteFile (path);
  74. FileStream stream = null;
  75. try {
  76. stream = new FileStream (TempFolder + DSC + file_name, FileMode.Open);
  77. Assert.Fail ("#A1");
  78. } catch (FileNotFoundException ex) {
  79. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
  80. Assert.AreEqual (path, ex.FileName, "#A3");
  81. Assert.IsNull (ex.InnerException, "#A4");
  82. Assert.IsNotNull (ex.Message, "#A5");
  83. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
  84. } finally {
  85. if (stream != null) {
  86. stream.Close ();
  87. stream = null;
  88. }
  89. DeleteFile (path);
  90. }
  91. // relative path
  92. string orignalCurrentDir = Directory.GetCurrentDirectory ();
  93. Directory.SetCurrentDirectory (TempFolder);
  94. // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
  95. // the real directory name, not that of the TempFolder symlink and the test will fail
  96. // (happens e.g. on Android M)
  97. string realTempDir = Directory.GetCurrentDirectory ();
  98. path = realTempDir + DSC + file_name;
  99. try {
  100. stream = new FileStream (file_name, FileMode.Open);
  101. Assert.Fail ("#B1");
  102. } catch (FileNotFoundException ex) {
  103. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
  104. Assert.AreEqual (path, ex.FileName, "#B3");
  105. Assert.IsNull (ex.InnerException, "#B4");
  106. Assert.IsNotNull (ex.Message, "#B5");
  107. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
  108. } finally {
  109. // restore original current directory
  110. Directory.SetCurrentDirectory (orignalCurrentDir);
  111. if (stream != null)
  112. stream.Close ();
  113. DeleteFile (path);
  114. }
  115. }
  116. [Test]
  117. public void CtorFileNotFoundException_Mode_Truncate ()
  118. {
  119. const string file_name = "thisfileshouldNOTexist.test";
  120. // absolute path
  121. string path = TempFolder + DSC + file_name;
  122. DeleteFile (path);
  123. FileStream stream = null;
  124. try {
  125. stream = new FileStream (path, FileMode.Truncate);
  126. Assert.Fail ("#A1");
  127. } catch (FileNotFoundException ex) {
  128. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
  129. Assert.AreEqual (path, ex.FileName, "#A3");
  130. Assert.IsNull (ex.InnerException, "#A4");
  131. Assert.IsNotNull (ex.Message, "#A5");
  132. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
  133. } finally {
  134. if (stream != null) {
  135. stream.Close ();
  136. stream = null;
  137. }
  138. DeleteFile (path);
  139. }
  140. // relative path
  141. string orignalCurrentDir = Directory.GetCurrentDirectory ();
  142. Directory.SetCurrentDirectory (TempFolder);
  143. // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
  144. // the real directory name, not that of the TempFolder symlink and the test will fail
  145. // (happens e.g. on Android M)
  146. string realTempDir = Directory.GetCurrentDirectory ();
  147. path = realTempDir + DSC + file_name;
  148. try {
  149. stream = new FileStream (file_name, FileMode.Truncate);
  150. Assert.Fail ("#B1");
  151. } catch (FileNotFoundException ex) {
  152. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
  153. Assert.AreEqual (path, ex.FileName, "#B3");
  154. Assert.IsNull (ex.InnerException, "#B4");
  155. Assert.IsNotNull (ex.Message, "#B5");
  156. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
  157. } finally {
  158. // restore original current directory
  159. Directory.SetCurrentDirectory (orignalCurrentDir);
  160. if (stream != null)
  161. stream.Close ();
  162. DeleteFile (path);
  163. }
  164. }
  165. [Test]
  166. public void CtorIOException1 ()
  167. {
  168. const string file_name = "thisfileshouldexists.test";
  169. // absolute path
  170. string path = TempFolder + DSC + file_name;
  171. FileStream stream = null;
  172. DeleteFile (path);
  173. try {
  174. stream = new FileStream (path, FileMode.CreateNew);
  175. stream.Close ();
  176. stream = null;
  177. stream = new FileStream (path, FileMode.CreateNew);
  178. Assert.Fail ("#A1");
  179. } catch (IOException ex) {
  180. Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
  181. Assert.IsNull (ex.InnerException, "#A3");
  182. Assert.IsNotNull (ex.Message, "#A4");
  183. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
  184. } finally {
  185. if (stream != null) {
  186. stream.Close ();
  187. stream = null;
  188. }
  189. DeleteFile (path);
  190. }
  191. // relative path
  192. string orignalCurrentDir = Directory.GetCurrentDirectory ();
  193. Directory.SetCurrentDirectory (TempFolder);
  194. // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
  195. // the real directory name, not that of the TempFolder symlink and the test will fail
  196. // (happens e.g. on Android M)
  197. string realTempDir = Directory.GetCurrentDirectory ();
  198. path = realTempDir + DSC + file_name;
  199. try {
  200. stream = new FileStream (file_name, FileMode.CreateNew);
  201. stream.Close ();
  202. stream = null;
  203. stream = new FileStream (file_name, FileMode.CreateNew);
  204. Assert.Fail ("#B1");
  205. } catch (IOException ex) {
  206. Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
  207. Assert.IsNull (ex.InnerException, "#B3");
  208. Assert.IsNotNull (ex.Message, "#B4");
  209. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
  210. } finally {
  211. // restore original current directory
  212. Directory.SetCurrentDirectory (orignalCurrentDir);
  213. if (stream != null)
  214. stream.Close ();
  215. DeleteFile (path);
  216. }
  217. }
  218. [Test]
  219. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  220. public void CtorArgumentOutOfRangeException1 ()
  221. {
  222. FileStream stream = null;
  223. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  224. DeleteFile (path);
  225. try {
  226. stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
  227. } finally {
  228. if (stream != null)
  229. stream.Close ();
  230. DeleteFile (path);
  231. }
  232. }
  233. [Test]
  234. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  235. public void CtorArgumentOutOfRangeException2 ()
  236. {
  237. FileStream stream = null;
  238. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  239. DeleteFile (path);
  240. try {
  241. stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
  242. } finally {
  243. if (stream != null)
  244. stream.Close ();
  245. DeleteFile (path);
  246. }
  247. }
  248. private void CtorDirectoryNotFoundException (FileMode mode)
  249. {
  250. string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
  251. if (Directory.Exists (path))
  252. Directory.Delete (path, true);
  253. FileStream stream = null;
  254. try {
  255. stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
  256. } finally {
  257. if (stream != null)
  258. stream.Close ();
  259. if (Directory.Exists (path))
  260. Directory.Delete (path, true);
  261. }
  262. }
  263. [Test]
  264. [ExpectedException (typeof (DirectoryNotFoundException))]
  265. public void CtorDirectoryNotFoundException_CreateNew ()
  266. {
  267. CtorDirectoryNotFoundException (FileMode.CreateNew);
  268. }
  269. [Test]
  270. [ExpectedException (typeof (DirectoryNotFoundException))]
  271. public void CtorDirectoryNotFoundException_Create ()
  272. {
  273. CtorDirectoryNotFoundException (FileMode.Create);
  274. }
  275. [Test]
  276. [ExpectedException (typeof (DirectoryNotFoundException))]
  277. public void CtorDirectoryNotFoundException_Open ()
  278. {
  279. CtorDirectoryNotFoundException (FileMode.Open);
  280. }
  281. [Test]
  282. [ExpectedException (typeof (DirectoryNotFoundException))]
  283. public void CtorDirectoryNotFoundException_OpenOrCreate ()
  284. {
  285. CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
  286. }
  287. [Test]
  288. [ExpectedException (typeof (DirectoryNotFoundException))]
  289. public void CtorDirectoryNotFoundException_Truncate ()
  290. {
  291. CtorDirectoryNotFoundException (FileMode.Truncate);
  292. }
  293. [Test]
  294. [ExpectedException (typeof (DirectoryNotFoundException))]
  295. public void CtorDirectoryNotFoundException_Append ()
  296. {
  297. CtorDirectoryNotFoundException (FileMode.Append);
  298. }
  299. [Test]
  300. public void CtorDirectoryNotFound_RelativePath ()
  301. {
  302. string orignalCurrentDir = Directory.GetCurrentDirectory ();
  303. Directory.SetCurrentDirectory (TempFolder);
  304. // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
  305. // the real directory name, not that of the TempFolder symlink and the test will fail
  306. // (happens e.g. on Android M)
  307. string realTempDir = Directory.GetCurrentDirectory ();
  308. string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
  309. string fullPath = Path.Combine (realTempDir, relativePath);
  310. try {
  311. new FileStream (relativePath, FileMode.Open);
  312. Assert.Fail ("#A1");
  313. } catch (DirectoryNotFoundException ex) {
  314. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
  315. Assert.IsNull (ex.InnerException, "#A3");
  316. Assert.IsNotNull (ex.Message, "#A4");
  317. Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
  318. } finally {
  319. // restore original current directory
  320. Directory.SetCurrentDirectory (orignalCurrentDir);
  321. }
  322. }
  323. [Test]
  324. // FileShare.Inheritable is ignored, but file does not exist
  325. [ExpectedException (typeof (FileNotFoundException))]
  326. public void CtorArgumentOutOfRangeException3 ()
  327. {
  328. string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
  329. DeleteFile (path);
  330. FileStream stream = null;
  331. try {
  332. stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
  333. } finally {
  334. if (stream != null)
  335. stream.Close ();
  336. DeleteFile (path);
  337. }
  338. }
  339. [Test]
  340. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  341. public void CtorArgumentOutOfRangeException4 ()
  342. {
  343. string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
  344. DeleteFile (path);
  345. FileStream stream = null;
  346. try {
  347. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
  348. } finally {
  349. if (stream != null)
  350. stream.Close ();
  351. DeleteFile (path);
  352. }
  353. }
  354. [Test]
  355. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  356. public void CtorBufferSizeZero ()
  357. {
  358. // Buffer size can't be zero
  359. string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
  360. DeleteFile (path);
  361. FileStream stream = null;
  362. try {
  363. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
  364. } finally {
  365. if (stream != null)
  366. stream.Close ();
  367. DeleteFile (path);
  368. }
  369. }
  370. [Test]
  371. [ExpectedException (typeof (ArgumentException))]
  372. public void CtorArgumentException2 ()
  373. {
  374. // FileMode.CreateNew && FileAccess.Read
  375. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  376. FileStream stream = null;
  377. DeleteFile (path);
  378. try {
  379. stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
  380. } finally {
  381. if (stream != null)
  382. stream.Close ();
  383. DeleteFile (path);
  384. }
  385. }
  386. [Test]
  387. // FileShare.Inheritable is ignored, but file does not exist
  388. [ExpectedException (typeof (FileNotFoundException))]
  389. public void CtorArgumentOutOfRangeException5 ()
  390. {
  391. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  392. DeleteFile (path);
  393. FileStream stream = null;
  394. try {
  395. stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
  396. } finally {
  397. if (stream != null)
  398. stream.Close ();
  399. DeleteFile (path);
  400. }
  401. }
  402. [Test]
  403. [ExpectedException (typeof (ArgumentException))]
  404. public void CtorArgumentException3 ()
  405. {
  406. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  407. FileStream stream = null;
  408. DeleteFile (path);
  409. try {
  410. stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
  411. } finally {
  412. if (stream != null)
  413. stream.Close ();
  414. DeleteFile (path);
  415. }
  416. }
  417. [Test]
  418. public void ModeAndAccessCombinations ()
  419. {
  420. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  421. DeleteFile (path);
  422. FileStream stream = null;
  423. // Append / Read
  424. try {
  425. // Append access can be requested only in write-only mode
  426. stream = new FileStream (path, FileMode.Append, FileAccess.Read);
  427. Assert.Fail ("#A1");
  428. } catch (ArgumentException ex) {
  429. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  430. } finally {
  431. if (stream != null)
  432. stream.Close ();
  433. DeleteFile (path);
  434. }
  435. // Append / ReadWrite
  436. try {
  437. // Append access can be requested only in write-only mode
  438. stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
  439. Assert.Fail ("#B1");
  440. } catch (ArgumentException ex) {
  441. // make sure it is exact this exception, and not a derived type
  442. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  443. } finally {
  444. if (stream != null)
  445. stream.Close ();
  446. DeleteFile (path);
  447. }
  448. // Append / Write
  449. try {
  450. stream = new FileStream (path, FileMode.Append, FileAccess.Write);
  451. } finally {
  452. if (stream != null)
  453. stream.Close ();
  454. DeleteFile (path);
  455. }
  456. // Create / Read
  457. try {
  458. stream = new FileStream (path, FileMode.Create, FileAccess.Read);
  459. Assert.Fail ("#C1");
  460. } catch (ArgumentException ex) {
  461. // make sure it is exact this exception, and not a derived type
  462. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  463. } finally {
  464. if (stream != null)
  465. stream.Close ();
  466. DeleteFile (path);
  467. }
  468. // Create / ReadWrite
  469. try {
  470. stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
  471. } finally {
  472. if (stream != null)
  473. stream.Close ();
  474. DeleteFile (path);
  475. }
  476. // Create / Write
  477. try {
  478. stream = new FileStream (path, FileMode.Create, FileAccess.Write);
  479. } finally {
  480. if (stream != null)
  481. stream.Close ();
  482. DeleteFile (path);
  483. }
  484. // CreateNew / Read
  485. try {
  486. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
  487. Assert.Fail ("#D1");
  488. } catch (ArgumentException ex) {
  489. // make sure it is exact this exception, and not a derived type
  490. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  491. } finally {
  492. if (stream != null)
  493. stream.Close ();
  494. DeleteFile (path);
  495. }
  496. // CreateNew / ReadWrite
  497. try {
  498. stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  499. } finally {
  500. if (stream != null)
  501. stream.Close ();
  502. DeleteFile (path);
  503. }
  504. // CreateNew / Write
  505. try {
  506. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
  507. } finally {
  508. if (stream != null)
  509. stream.Close ();
  510. DeleteFile (path);
  511. }
  512. // Open / Read
  513. try {
  514. stream = new FileStream (path, FileMode.Open, FileAccess.Read);
  515. Assert.Fail ("#E1");
  516. } catch (FileNotFoundException ex) {
  517. // make sure it is exact this exception, and not a derived type
  518. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
  519. Assert.AreEqual (path, ex.FileName, "#E3");
  520. Assert.IsNull (ex.InnerException, "#E4");
  521. Assert.IsNotNull (ex.Message, "#E5");
  522. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
  523. } finally {
  524. if (stream != null)
  525. stream.Close ();
  526. DeleteFile (path);
  527. }
  528. // Open / ReadWrite
  529. try {
  530. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  531. Assert.Fail ("#F1");
  532. } catch (FileNotFoundException ex) {
  533. // make sure it is exact this exception, and not a derived type
  534. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
  535. Assert.AreEqual (path, ex.FileName, "#F3");
  536. Assert.IsNull (ex.InnerException, "#F4");
  537. Assert.IsNotNull (ex.Message, "#F5");
  538. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
  539. } finally {
  540. if (stream != null)
  541. stream.Close ();
  542. DeleteFile (path);
  543. }
  544. // Open / Write
  545. try {
  546. stream = new FileStream (path, FileMode.Open, FileAccess.Write);
  547. Assert.Fail ("#G1");
  548. } catch (FileNotFoundException ex) {
  549. // make sure it is exact this exception, and not a derived type
  550. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
  551. Assert.AreEqual (path, ex.FileName, "#G3");
  552. Assert.IsNull (ex.InnerException, "#G4");
  553. Assert.IsNotNull (ex.Message, "#G5");
  554. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
  555. } finally {
  556. if (stream != null)
  557. stream.Close ();
  558. DeleteFile (path);
  559. }
  560. // OpenOrCreate / Read
  561. try {
  562. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  563. } finally {
  564. if (stream != null)
  565. stream.Close ();
  566. DeleteFile (path);
  567. }
  568. // OpenOrCreate / ReadWrite
  569. try {
  570. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  571. } finally {
  572. if (stream != null)
  573. stream.Close ();
  574. DeleteFile (path);
  575. }
  576. // OpenOrCreate / Write
  577. try {
  578. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  579. } finally {
  580. if (stream != null)
  581. stream.Close ();
  582. DeleteFile (path);
  583. }
  584. // Truncate / Read
  585. try {
  586. stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
  587. Assert.Fail ("#H1");
  588. } catch (ArgumentException ex) {
  589. // make sure it is exact this exception, and not a derived type
  590. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
  591. } finally {
  592. if (stream != null)
  593. stream.Close ();
  594. DeleteFile (path);
  595. }
  596. // Truncate / ReadWrite
  597. try {
  598. stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
  599. Assert.Fail ("#I1");
  600. } catch (FileNotFoundException ex) {
  601. // make sure it is exact this exception, and not a derived type
  602. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
  603. Assert.AreEqual (path, ex.FileName, "#I3");
  604. Assert.IsNull (ex.InnerException, "#I4");
  605. Assert.IsNotNull (ex.Message, "#I5");
  606. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
  607. } finally {
  608. if (stream != null)
  609. stream.Close ();
  610. DeleteFile (path);
  611. }
  612. // Truncate / Write
  613. try {
  614. stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
  615. Assert.Fail ("#J1");
  616. } catch (FileNotFoundException ex) {
  617. // make sure it is exact this exception, and not a derived type
  618. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
  619. Assert.AreEqual (path, ex.FileName, "#J3");
  620. Assert.IsNull (ex.InnerException, "#J4");
  621. Assert.IsNotNull (ex.Message, "#J5");
  622. Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
  623. } finally {
  624. if (stream != null)
  625. stream.Close ();
  626. DeleteFile (path);
  627. }
  628. }
  629. [Test, ExpectedException (typeof (IOException))]
  630. public void CtorIOException2 ()
  631. {
  632. FileStream stream = null;
  633. try {
  634. stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
  635. } finally {
  636. if (stream != null)
  637. stream.Close ();
  638. }
  639. }
  640. [Test, ExpectedException (typeof (IOException))]
  641. public void CtorIOException ()
  642. {
  643. string path = TempFolder + DSC + "CTorIOException.Test";
  644. FileStream stream = null;
  645. FileStream stream2 = null;
  646. DeleteFile (path);
  647. try {
  648. stream = new FileStream (path, FileMode.CreateNew);
  649. // used by an another process
  650. stream2 = new FileStream (path, FileMode.OpenOrCreate);
  651. } finally {
  652. if (stream != null)
  653. stream.Close ();
  654. if (stream2 != null)
  655. stream2.Close ();
  656. DeleteFile (path);
  657. }
  658. }
  659. [Test]
  660. public void CtorAccess1Read2Read ()
  661. {
  662. FileStream fs = null;
  663. FileStream fs2 = null;
  664. string fn = Path.Combine (TempFolder, "temp");
  665. try {
  666. if (!File.Exists (fn)) {
  667. TextWriter tw = File.CreateText (fn);
  668. tw.Write ("FOO");
  669. tw.Close ();
  670. }
  671. fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
  672. fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
  673. } finally {
  674. if (fs != null)
  675. fs.Close ();
  676. if (fs2 != null)
  677. fs2.Close ();
  678. if (File.Exists (fn))
  679. File.Delete (fn);
  680. }
  681. }
  682. [Test]
  683. [ExpectedException (typeof (IOException))]
  684. public void CtorAccess1Read2Write ()
  685. {
  686. string fn = Path.Combine (TempFolder, "temp");
  687. FileStream fs1 = null;
  688. FileStream fs2 = null;
  689. try {
  690. if (!File.Exists (fn)) {
  691. using (TextWriter tw = File.CreateText (fn)) {
  692. tw.Write ("FOO");
  693. }
  694. }
  695. fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
  696. fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
  697. } finally {
  698. if (fs1 != null)
  699. fs1.Close ();
  700. if (fs2 != null)
  701. fs2.Close ();
  702. if (File.Exists (fn))
  703. File.Delete (fn);
  704. }
  705. }
  706. [Test]
  707. [ExpectedException (typeof (IOException))]
  708. public void CtorAccess1Write2Write ()
  709. {
  710. string fn = Path.Combine (TempFolder, "temp");
  711. FileStream fs1 = null;
  712. FileStream fs2 = null;
  713. try {
  714. if (File.Exists (fn))
  715. File.Delete (fn);
  716. fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
  717. fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
  718. } finally {
  719. if (fs1 != null)
  720. fs1.Close ();
  721. if (fs2 != null)
  722. fs2.Close ();
  723. if (File.Exists (fn))
  724. File.Delete (fn);
  725. }
  726. }
  727. [Test]
  728. [ExpectedException (typeof (UnauthorizedAccessException))]
  729. public void CtorReadDirectoryAsFile ()
  730. {
  731. FileStream stream = null;
  732. try {
  733. stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
  734. } finally {
  735. if (stream != null)
  736. stream.Close ();
  737. }
  738. }
  739. [Test] // bug #79250
  740. public void FileShare_Delete ()
  741. {
  742. string fn = Path.Combine (TempFolder, "temp");
  743. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
  744. s.Write (new byte [1] { 0x5 }, 0, 1);
  745. File.Delete (fn);
  746. }
  747. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
  748. s.Write (new byte [1] { 0x5 }, 0, 1);
  749. File.Delete (fn);
  750. }
  751. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
  752. s.Write (new byte [1] { 0x5 }, 0, 1);
  753. File.Delete (fn);
  754. }
  755. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
  756. s.Write (new byte [1] { 0x5 }, 0, 1);
  757. File.Delete (fn);
  758. }
  759. using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
  760. s.Write (new byte [1] { 0x5 }, 0, 1);
  761. File.Delete (fn);
  762. }
  763. }
  764. [Test]
  765. public void Write ()
  766. {
  767. string path = TempFolder + DSC + "FileStreamTest.Write";
  768. DeleteFile (path);
  769. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
  770. byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  771. byte[] bytes = new byte[15];
  772. // Check that the data is flushed when we overflow the buffer
  773. // with a large amount of data
  774. stream.Write (outbytes, 0, 5);
  775. stream.Write (outbytes, 5, 10);
  776. stream.Seek (0, SeekOrigin.Begin);
  777. stream.Read (bytes, 0, 15);
  778. for (int i = 0; i < 15; ++i)
  779. Assert.AreEqual (i + 1, bytes[i], "#1");
  780. // Check that the data is flushed when we overflow the buffer
  781. // with a small amount of data
  782. stream.Write (outbytes, 0, 7);
  783. stream.Write (outbytes, 7, 7);
  784. stream.Write (outbytes, 14, 1);
  785. stream.Read (bytes, 0, 15);
  786. stream.Seek (15, SeekOrigin.Begin);
  787. for (int i = 0; i < 15; ++i)
  788. Assert.AreEqual (i + 1, bytes[i], "#2");
  789. stream.Close ();
  790. }
  791. [Test]
  792. public void Length ()
  793. {
  794. // Test that the Length property takes into account the data
  795. // in the buffer
  796. string path = TempFolder + DSC + "FileStreamTest.Length";
  797. DeleteFile (path);
  798. FileStream stream = new FileStream (path, FileMode.CreateNew);
  799. byte[] outbytes = new byte[] { 1, 2, 3, 4 };
  800. stream.Write (outbytes, 0, 4);
  801. Assert.AreEqual (4, stream.Length);
  802. stream.Close ();
  803. }
  804. [Test]
  805. public void Flush ()
  806. {
  807. string path = TempFolder + DSC + "FileStreamTest.Flush";
  808. FileStream stream = null;
  809. FileStream stream2 = null;
  810. DeleteFile (path);
  811. try {
  812. stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  813. stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  814. stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
  815. byte[] bytes = new byte[5];
  816. stream2.Read (bytes, 0, 5);
  817. Assert.AreEqual (0, bytes[0], "#A1");
  818. Assert.AreEqual (0, bytes[1], "#A2");
  819. Assert.AreEqual (0, bytes[2], "#A3");
  820. Assert.AreEqual (0, bytes[3], "#A4");
  821. stream.Flush ();
  822. stream2.Read (bytes, 0, 5);
  823. Assert.AreEqual (1, bytes[0], "#B1");
  824. Assert.AreEqual (2, bytes[1], "#B2");
  825. Assert.AreEqual (3, bytes[2], "#B3");
  826. Assert.AreEqual (4, bytes[3], "#B4");
  827. } finally {
  828. if (stream != null)
  829. stream.Close ();
  830. if (stream2 != null)
  831. stream2.Close ();
  832. DeleteFile (path);
  833. }
  834. }
  835. public void TestDefaultProperties ()
  836. {
  837. string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
  838. DeleteFile (path);
  839. FileStream stream = new FileStream (path, FileMode.Create);
  840. Assert.IsTrue (stream.CanRead, "#A1");
  841. Assert.IsTrue (stream.CanSeek, "#A2");
  842. Assert.IsTrue (stream.CanWrite, "#A3");
  843. Assert.IsFalse (stream.IsAsync, "#A4");
  844. Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
  845. Assert.AreEqual (0, stream.Position, "#A6");
  846. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
  847. stream.Close ();
  848. DeleteFile (path);
  849. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  850. Assert.IsTrue (stream.CanRead, "#B1");
  851. Assert.IsTrue (stream.CanSeek, "#B2");
  852. Assert.IsFalse (stream.CanWrite, "#B3");
  853. Assert.IsFalse (stream.IsAsync, "#B4");
  854. Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
  855. Assert.AreEqual (0, stream.Position, "#B6");
  856. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
  857. stream.Close ();
  858. stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
  859. Assert.IsFalse (stream.CanRead, "#C1");
  860. Assert.IsTrue (stream.CanSeek, "#C2");
  861. Assert.IsTrue (stream.CanWrite, "#C3");
  862. Assert.IsFalse (stream.IsAsync, "#C4");
  863. Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
  864. Assert.AreEqual (0, stream.Position, "#C6");
  865. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
  866. stream.Close ();
  867. DeleteFile (path);
  868. }
  869. [Category ("NotWorking")]
  870. // Bug: 71371 -> duplicate and WONTFIX.
  871. public void TestLock_FailsOnMono ()
  872. {
  873. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  874. DeleteFile (path);
  875. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  876. stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  877. stream.Close ();
  878. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  879. stream.Lock (0, 5);
  880. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  881. byte[] bytes = new byte[5];
  882. try {
  883. stream2.Read (bytes, 0, 5);
  884. Assert.Fail ("#1");
  885. } catch (Exception e) {
  886. Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
  887. }
  888. stream.Close ();
  889. stream2.Close ();
  890. DeleteFile (path);
  891. }
  892. public void TestLock ()
  893. {
  894. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  895. DeleteFile (path);
  896. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  897. stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  898. stream.Close ();
  899. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  900. stream.Lock (0, 5);
  901. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  902. byte[] bytes = new byte[5];
  903. try {
  904. stream2.Read (bytes, 0, 5);
  905. Assert.Fail ("#A1");
  906. } catch (Exception) {
  907. // Bug #71371: on MS.NET you get an IOException detailing a lock
  908. // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
  909. }
  910. stream2.Seek (5, SeekOrigin.Begin);
  911. stream2.Read (bytes, 0, 5);
  912. Assert.AreEqual (5, bytes[0], "#B1");
  913. Assert.AreEqual (6, bytes[1], "#B2");
  914. Assert.AreEqual (7, bytes[2], "#B3");
  915. Assert.AreEqual (8, bytes[3], "#B4");
  916. Assert.AreEqual (9, bytes[4], "#B5");
  917. stream.Unlock (0, 5);
  918. stream2.Seek (0, SeekOrigin.Begin);
  919. stream2.Read (bytes, 0, 5);
  920. Assert.AreEqual (0, bytes[0], "#C1");
  921. Assert.AreEqual (1, bytes[1], "#C2");
  922. Assert.AreEqual (2, bytes[2], "#C3");
  923. Assert.AreEqual (3, bytes[3], "#C4");
  924. Assert.AreEqual (4, bytes[4], "#C5");
  925. stream.Close ();
  926. stream2.Close ();
  927. DeleteFile (path);
  928. }
  929. [Test]
  930. public void Seek ()
  931. {
  932. string path = TempFolder + DSC + "FST.Seek.Test";
  933. DeleteFile (path);
  934. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  935. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  936. stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
  937. Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
  938. Assert.AreEqual (-1, stream2.ReadByte (), "#2");
  939. Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
  940. Assert.AreEqual (-1, stream2.ReadByte (), "#4");
  941. Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
  942. Assert.AreEqual (-1, stream.ReadByte (), "#6");
  943. Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
  944. Assert.AreEqual (6, stream.ReadByte (), "#8");
  945. Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
  946. Assert.AreEqual (6, stream2.ReadByte (), "#10");
  947. stream.Close ();
  948. stream2.Close ();
  949. DeleteFile (path);
  950. }
  951. public void TestSeek ()
  952. {
  953. string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
  954. DeleteFile (path);
  955. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  956. stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  957. stream.Seek (5, SeekOrigin.End);
  958. Assert.AreEqual (-1, stream.ReadByte (), "#1");
  959. stream.Seek (-5, SeekOrigin.End);
  960. Assert.AreEqual (6, stream.ReadByte (), "#2");
  961. try {
  962. stream.Seek (-11, SeekOrigin.End);
  963. Assert.Fail ("#3");
  964. } catch (Exception e) {
  965. Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
  966. }
  967. stream.Seek (19, SeekOrigin.Begin);
  968. Assert.AreEqual (-1, stream.ReadByte (), "#5");
  969. stream.Seek (1, SeekOrigin.Begin);
  970. Assert.AreEqual (2, stream.ReadByte (), "#6");
  971. stream.Seek (3, SeekOrigin.Current);
  972. Assert.AreEqual (6, stream.ReadByte (), "#7");
  973. stream.Seek (-2, SeekOrigin.Current);
  974. Assert.AreEqual (5, stream.ReadByte (), "#8");
  975. stream.Flush ();
  976. // Test that seeks work correctly when seeking inside the buffer
  977. stream.Seek (0, SeekOrigin.Begin);
  978. stream.WriteByte (0);
  979. stream.WriteByte (1);
  980. stream.Seek (0, SeekOrigin.Begin);
  981. byte[] buf = new byte[1];
  982. buf[0] = 2;
  983. stream.Write (buf, 0, 1);
  984. stream.Write (buf, 0, 1);
  985. stream.Flush ();
  986. stream.Seek (0, SeekOrigin.Begin);
  987. Assert.AreEqual (2, stream.ReadByte (), "#9");
  988. Assert.AreEqual (2, stream.ReadByte (), "#10");
  989. stream.Close ();
  990. DeleteFile (path);
  991. }
  992. public void TestClose ()
  993. {
  994. string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
  995. DeleteFile (path);
  996. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  997. stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
  998. stream.ReadByte ();
  999. stream.Close ();
  1000. try {
  1001. stream.ReadByte ();
  1002. Assert.Fail ("#A1");
  1003. } catch (Exception e) {
  1004. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
  1005. }
  1006. try {
  1007. stream.WriteByte (64);
  1008. Assert.Fail ("#B1");
  1009. } catch (Exception e) {
  1010. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
  1011. }
  1012. try {
  1013. stream.Flush ();
  1014. Assert.Fail ("#C1");
  1015. } catch (Exception e) {
  1016. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
  1017. }
  1018. try {
  1019. long l = stream.Length;
  1020. Assert.Fail ("#D1");
  1021. } catch (Exception e) {
  1022. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
  1023. }
  1024. try {
  1025. long l = stream.Position;
  1026. Assert.Fail ("#E1");
  1027. } catch (Exception e) {
  1028. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
  1029. }
  1030. Assert.IsFalse (stream.CanRead, "#F1");
  1031. Assert.IsFalse (stream.CanSeek, "#F2");
  1032. Assert.IsFalse (stream.CanWrite, "#F3");
  1033. Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
  1034. DeleteFile (path);
  1035. }
  1036. /// <summary>
  1037. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  1038. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  1039. /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
  1040. /// </summary>
  1041. [Test]
  1042. [ExpectedException (typeof (NotSupportedException))]
  1043. public void TestWriteVerifyAccessMode ()
  1044. {
  1045. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1046. DeleteFile (path);
  1047. FileStream stream = null;
  1048. byte[] buffer;
  1049. try {
  1050. buffer = Encoding.ASCII.GetBytes ("test");
  1051. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1052. stream.Write (buffer, 0, buffer.Length);
  1053. } finally {
  1054. if (stream != null)
  1055. stream.Close ();
  1056. DeleteFile (path);
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  1061. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  1062. /// <see cref="FileStream.WriteByte(byte)" /> method is called.
  1063. /// </summary>
  1064. [Test]
  1065. [ExpectedException (typeof (NotSupportedException))]
  1066. public void TestWriteByteVerifyAccessMode ()
  1067. {
  1068. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1069. DeleteFile (path);
  1070. FileStream stream = null;
  1071. try {
  1072. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1073. stream.WriteByte (Byte.MinValue);
  1074. } finally {
  1075. if (stream != null)
  1076. stream.Close ();
  1077. DeleteFile (path);
  1078. }
  1079. }
  1080. /// <summary>
  1081. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  1082. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  1083. /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
  1084. /// </summary>
  1085. [Test]
  1086. [ExpectedException (typeof (NotSupportedException))]
  1087. public void TestReadVerifyAccessMode ()
  1088. {
  1089. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1090. DeleteFile (path);
  1091. FileStream stream = null;
  1092. byte[] buffer = new byte[100];
  1093. try {
  1094. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1095. stream.Read (buffer, 0, buffer.Length);
  1096. } finally {
  1097. if (stream != null)
  1098. stream.Close ();
  1099. }
  1100. }
  1101. /// <summary>
  1102. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  1103. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  1104. /// <see cref="FileStream.ReadByte()" /> method is called.
  1105. /// </summary>
  1106. [Test]
  1107. [ExpectedException (typeof (NotSupportedException))]
  1108. public void TestReadByteVerifyAccessMode ()
  1109. {
  1110. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1111. DeleteFile (path);
  1112. FileStream stream = null;
  1113. try {
  1114. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1115. int readByte = stream.ReadByte ();
  1116. } finally {
  1117. if (stream != null)
  1118. stream.Close ();
  1119. DeleteFile (path);
  1120. }
  1121. }
  1122. // Check that the stream is flushed even when it doesn't own the
  1123. // handle
  1124. [Test]
  1125. public void TestFlushNotOwningHandle ()
  1126. {
  1127. string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
  1128. DeleteFile (path);
  1129. FileStream s = new FileStream (path, FileMode.Create);
  1130. using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
  1131. byte[] buf = new byte[2];
  1132. buf[0] = (int) '1';
  1133. s2.Write (buf, 0, 1);
  1134. }
  1135. s.Position = 0;
  1136. Assert.AreEqual ((int) '1', s.ReadByte ());
  1137. s.Close ();
  1138. }
  1139. private void DeleteFile (string path)
  1140. {
  1141. if (File.Exists (path))
  1142. File.Delete (path);
  1143. }
  1144. [Test]
  1145. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1146. public void Read_OffsetNegative ()
  1147. {
  1148. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1149. DeleteFile (path);
  1150. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1151. stream.Read (new byte[0], -1, 1);
  1152. }
  1153. }
  1154. [Test]
  1155. [ExpectedException (typeof (ArgumentException))]
  1156. public void Read_OffsetOverflow ()
  1157. {
  1158. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1159. DeleteFile (path);
  1160. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1161. stream.Read (new byte[0], Int32.MaxValue, 1);
  1162. }
  1163. }
  1164. [Test]
  1165. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1166. public void Read_CountNegative ()
  1167. {
  1168. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1169. DeleteFile (path);
  1170. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1171. stream.Read (new byte[0], 1, -1);
  1172. }
  1173. }
  1174. [Test]
  1175. [ExpectedException (typeof (ArgumentException))]
  1176. public void Read_CountOverflow ()
  1177. {
  1178. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1179. DeleteFile (path);
  1180. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1181. stream.Read (new byte[0], 1, Int32.MaxValue);
  1182. }
  1183. }
  1184. [Test]
  1185. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1186. public void Write_OffsetNegative ()
  1187. {
  1188. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1189. DeleteFile (path);
  1190. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1191. stream.Write (new byte[0], -1, 1);
  1192. }
  1193. }
  1194. [Test]
  1195. [ExpectedException (typeof (ArgumentException))]
  1196. public void Write_OffsetOverflow ()
  1197. {
  1198. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1199. DeleteFile (path);
  1200. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1201. stream.Write (new byte[0], Int32.MaxValue, 1);
  1202. }
  1203. }
  1204. [Test]
  1205. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1206. public void Write_CountNegative ()
  1207. {
  1208. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1209. DeleteFile (path);
  1210. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1211. stream.Write (new byte[0], 1, -1);
  1212. }
  1213. }
  1214. [Test]
  1215. [ExpectedException (typeof (ArgumentException))]
  1216. public void Write_CountOverflow ()
  1217. {
  1218. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1219. DeleteFile (path);
  1220. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1221. stream.Write (new byte[0], 1, Int32.MaxValue);
  1222. }
  1223. }
  1224. [Test]
  1225. [ExpectedException (typeof (ArgumentException))]
  1226. public void Seek_InvalidSeekOrigin ()
  1227. {
  1228. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1229. DeleteFile (path);
  1230. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1231. stream.Seek (0, (SeekOrigin) (-1));
  1232. }
  1233. }
  1234. [Test]
  1235. [ExpectedException (typeof (ArgumentException))]
  1236. public void Constructor_InvalidFileHandle ()
  1237. {
  1238. new FileStream ((IntPtr) (-1L), FileAccess.Read);
  1239. }
  1240. [Test]
  1241. public void PositionAfterSetLength ()
  1242. {
  1243. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1244. DeleteFile (path);
  1245. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1246. stream.SetLength (32);
  1247. stream.Position = 32;
  1248. stream.SetLength (16);
  1249. Assert.AreEqual (16, stream.Position);
  1250. }
  1251. }
  1252. [Test]
  1253. [ExpectedException (typeof (ObjectDisposedException))]
  1254. public void SetLength_Disposed ()
  1255. {
  1256. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1257. DeleteFile (path);
  1258. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1259. stream.Close ();
  1260. stream.SetLength (16);
  1261. }
  1262. [Test]
  1263. [ExpectedException (typeof (ObjectDisposedException))]
  1264. public void Position_Disposed ()
  1265. {
  1266. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1267. DeleteFile (path);
  1268. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1269. stream.Close ();
  1270. stream.Position = 0;
  1271. }
  1272. [Test]
  1273. [ExpectedException (typeof (ObjectDisposedException))]
  1274. public void BeginRead_Disposed ()
  1275. {
  1276. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1277. DeleteFile (path);
  1278. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1279. stream.Close ();
  1280. stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
  1281. }
  1282. [Test]
  1283. [ExpectedException (typeof (ObjectDisposedException))]
  1284. public void BeginWrite_Disposed ()
  1285. {
  1286. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1287. DeleteFile (path);
  1288. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1289. stream.Close ();
  1290. stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
  1291. }
  1292. static IAsyncResult DoBeginWrite(Stream stream, ManualResetEvent mre, byte[] RandomBuffer)
  1293. {
  1294. return stream.BeginWrite (RandomBuffer, 0, RandomBuffer.Length, ar => {
  1295. IAsyncResult begin_write_recursive_ares;
  1296. try {
  1297. stream.EndWrite (ar);
  1298. // we don't supply an ManualResetEvent so this will throw an NRE on the second run
  1299. // which nunit-console will ignore (but other test runners don't like that)
  1300. if (mre == null)
  1301. return;
  1302. begin_write_recursive_ares = DoBeginWrite (stream, null, RandomBuffer);
  1303. begin_write_recursive_ares.AsyncWaitHandle.WaitOne ();
  1304. mre.Set ();
  1305. } catch (ObjectDisposedException e) {
  1306. Console.WriteLine ("stream was disposed: {0}", e);
  1307. }
  1308. }, null);
  1309. }
  1310. [Test]
  1311. public void BeginWrite_Recursive ()
  1312. {
  1313. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1314. DeleteFile (path);
  1315. IAsyncResult begin_write_ares = null;
  1316. try {
  1317. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1318. var mre = new ManualResetEvent (false);
  1319. var RandomBuffer = new byte[1024];
  1320. begin_write_ares = DoBeginWrite (stream, mre, RandomBuffer);
  1321. Assert.IsTrue (mre.WaitOne (5000), "#1");
  1322. }
  1323. } finally {
  1324. if (begin_write_ares != null)
  1325. begin_write_ares.AsyncWaitHandle.WaitOne ();
  1326. }
  1327. }
  1328. [Test]
  1329. [ExpectedException (typeof (ObjectDisposedException))]
  1330. public void Lock_Disposed ()
  1331. {
  1332. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1333. DeleteFile (path);
  1334. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1335. stream.Close ();
  1336. stream.Lock (0, 1);
  1337. }
  1338. [Test]
  1339. [ExpectedException (typeof (ObjectDisposedException))]
  1340. public void Unlock_Disposed ()
  1341. {
  1342. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1343. DeleteFile (path);
  1344. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1345. stream.Close ();
  1346. stream.Unlock (0, 1);
  1347. }
  1348. [Test]
  1349. public void ReadBytePastEndOfStream ()
  1350. {
  1351. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1352. DeleteFile (path);
  1353. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1354. stream.Seek (0, SeekOrigin.End);
  1355. Assert.AreEqual (-1, stream.ReadByte ());
  1356. stream.Close ();
  1357. }
  1358. }
  1359. [Test]
  1360. [ExpectedException (typeof (NotSupportedException))]
  1361. public void SetLengthWithClosedBaseStream ()
  1362. {
  1363. string fn = Path.Combine (TempFolder, "temp");
  1364. try {
  1365. FileStream fs = new FileStream (fn, FileMode.Create);
  1366. BufferedStream bs = new BufferedStream (fs);
  1367. fs.Close ();
  1368. bs.SetLength (1000);
  1369. } finally {
  1370. File.Delete (fn);
  1371. }
  1372. }
  1373. [Test]
  1374. public void LengthAfterWrite ()
  1375. {
  1376. string path = TempFolder + DSC + "oneofthefilescreated.txt";
  1377. FileStream fs = null;
  1378. DeleteFile (path);
  1379. try {
  1380. fs = new FileStream (path, FileMode.CreateNew);
  1381. fs.WriteByte (Convert.ToByte ('A'));
  1382. byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
  1383. fs.Write (buffer, 0, buffer.Length);
  1384. fs.Seek (0, SeekOrigin.Begin);
  1385. char a = Convert.ToChar (fs.ReadByte ());
  1386. Assert.AreEqual ('A', a, "#A1");
  1387. Assert.AreEqual (23, fs.Length, "#A2");
  1388. int nread = fs.Read (buffer, 0, 5);
  1389. Assert.AreEqual (5, nread, "#A3");
  1390. } finally {
  1391. if (fs != null)
  1392. fs.Close ();
  1393. DeleteFile (path);
  1394. }
  1395. }
  1396. [Test]
  1397. public void DeleteOnClose ()
  1398. {
  1399. string path = TempFolder + DSC + "created.txt";
  1400. DeleteFile (path);
  1401. FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
  1402. FileOptions.DeleteOnClose);
  1403. Assert.AreEqual (true, File.Exists (path), "DOC#1");
  1404. fs.Close ();
  1405. Assert.AreEqual (false, File.Exists (path), "DOC#2");
  1406. }
  1407. [Test]
  1408. public void OpenCharDeviceRepeatedly ()
  1409. {
  1410. // https://bugzilla.xamarin.com/show_bug.cgi?id=38408
  1411. try {
  1412. using (var f = new FileStream ("/dev/zero", FileMode.Open))
  1413. {
  1414. }
  1415. } catch (FileNotFoundException) {
  1416. // Only run this test on platforms where /dev/zero exists
  1417. Assert.Ignore();
  1418. } catch (DirectoryNotFoundException) {
  1419. // Only run this test on platforms where /dev exists
  1420. Assert.Ignore();
  1421. }
  1422. // this shouldn't throw
  1423. using (var g = new FileStream ("/dev/zero", FileMode.Open))
  1424. {
  1425. }
  1426. }
  1427. #if !MOBILE
  1428. [Test]
  1429. public void WriteWithExposedHandle ()
  1430. {
  1431. string path = TempFolder + DSC + "exposed-handle.txt";
  1432. FileStream fs1 = null;
  1433. FileStream fs2 = null;
  1434. DeleteFile (path);
  1435. try {
  1436. fs1 = new FileStream (path, FileMode.Create);
  1437. fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.ReadWrite);
  1438. fs1.WriteByte (Convert.ToByte ('H'));
  1439. fs1.WriteByte (Convert.ToByte ('E'));
  1440. fs1.WriteByte (Convert.ToByte ('L'));
  1441. fs2.WriteByte (Convert.ToByte ('L'));
  1442. fs2.WriteByte (Convert.ToByte ('O'));
  1443. long fs1Pos = fs1.Position;
  1444. fs1.Flush ();
  1445. fs2.Flush ();
  1446. fs1.Close ();
  1447. fs2.Close ();
  1448. var check = Encoding.ASCII.GetString (File.ReadAllBytes (path));
  1449. Assert.AreEqual ("HELLO", check, "EXPOSED#1");
  1450. Assert.AreEqual (5, fs1Pos, "EXPOSED#2");
  1451. } finally {
  1452. if (fs1 != null)
  1453. fs1.Close ();
  1454. if (fs2 != null)
  1455. fs2.Close ();
  1456. DeleteFile (path);
  1457. }
  1458. }
  1459. [Test]
  1460. public void Ctor_InvalidSafeHandle ()
  1461. {
  1462. var sf = new SafeFileHandle (IntPtr.Zero, true);
  1463. try {
  1464. new FileStream (sf, FileAccess.ReadWrite);
  1465. Assert.Fail ("#1");
  1466. } catch (ArgumentException) {
  1467. }
  1468. }
  1469. #endif
  1470. [Test] // Covers #11699
  1471. public void ReadWriteFileLength ()
  1472. {
  1473. int bufferSize = 128;
  1474. int readLength = 1;
  1475. int writeLength = bufferSize + 1;
  1476. string path = TempFolder + DSC + "readwritefilelength.tmp";
  1477. try {
  1478. File.WriteAllBytes (path, new byte [readLength + 1]);
  1479. using (var file = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read,
  1480. bufferSize, FileOptions.SequentialScan))
  1481. {
  1482. file.Read (new byte [readLength], 0, readLength);
  1483. file.Write (new byte [writeLength], 0, writeLength);
  1484. Assert.AreEqual (readLength + writeLength, file.Length);
  1485. }
  1486. } finally {
  1487. DeleteFile (path);
  1488. }
  1489. }
  1490. [Test]
  1491. public void NamePropertyNormalization ()
  1492. {
  1493. string fname = TempFolder + DSC + ".." + DSC + "MonoTests.System.IO.Tests" + DSC + "tfile.txt";
  1494. using (var s = new FileStream (fname, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
  1495. Assert.AreEqual (TempFolder + DSC + "tfile.txt", s.Name);
  1496. }
  1497. }
  1498. }
  1499. }