FileStreamTest.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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. //
  11. using NUnit.Framework;
  12. using System;
  13. using System.IO;
  14. using System.Text;
  15. namespace MonoTests.System.IO
  16. {
  17. [TestFixture]
  18. public class FileStreamTest
  19. {
  20. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  21. static readonly char DSC = Path.DirectorySeparatorChar;
  22. [TearDown]
  23. public void TearDown ()
  24. {
  25. if (Directory.Exists (TempFolder))
  26. Directory.Delete (TempFolder, true);
  27. }
  28. [SetUp]
  29. public void SetUp ()
  30. {
  31. if (Directory.Exists (TempFolder))
  32. Directory.Delete (TempFolder, true);
  33. Directory.CreateDirectory (TempFolder);
  34. }
  35. public void TestCtr ()
  36. {
  37. string path = TempFolder + DSC + "testfilestream.tmp.1";
  38. DeleteFile (path);
  39. FileStream stream = null;
  40. try {
  41. stream = new FileStream (path, FileMode.Create);
  42. } finally {
  43. if (stream != null)
  44. stream.Close ();
  45. DeleteFile (path);
  46. }
  47. }
  48. [Test]
  49. [ExpectedException (typeof (ArgumentException))]
  50. public void CtorArgumentException1 ()
  51. {
  52. FileStream stream;
  53. stream = new FileStream ("", FileMode.Create);
  54. stream.Close ();
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentNullException))]
  58. public void CtorArgumentNullException ()
  59. {
  60. FileStream stream = new FileStream (null, FileMode.Create);
  61. stream.Close ();
  62. }
  63. [Test]
  64. [ExpectedException (typeof (FileNotFoundException))]
  65. public void CtorFileNotFoundException1 ()
  66. {
  67. string path = TempFolder + DSC + "thisfileshouldnotexists.test";
  68. DeleteFile (path);
  69. FileStream stream = null;
  70. try {
  71. stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
  72. } finally {
  73. if (stream != null)
  74. stream.Close ();
  75. DeleteFile (path);
  76. }
  77. }
  78. [Test]
  79. [ExpectedException (typeof (FileNotFoundException))]
  80. public void CtorFileNotFoundException2 ()
  81. {
  82. string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
  83. DeleteFile (path);
  84. FileStream stream = null;
  85. try {
  86. stream = new FileStream (TempFolder + DSC + "thisfileshouldNOTexists.test", FileMode.Truncate);
  87. } finally {
  88. if (stream != null)
  89. stream.Close ();
  90. DeleteFile (path);
  91. }
  92. }
  93. [Test]
  94. [ExpectedException (typeof (IOException))]
  95. public void CtorIOException1 ()
  96. {
  97. string path = TempFolder + DSC + "thisfileshouldexists.test";
  98. FileStream stream = null;
  99. DeleteFile (path);
  100. try {
  101. stream = new FileStream (path, FileMode.CreateNew);
  102. stream.Close ();
  103. stream = null;
  104. stream = new FileStream (path, FileMode.CreateNew);
  105. } finally {
  106. if (stream != null)
  107. stream.Close ();
  108. DeleteFile (path);
  109. }
  110. }
  111. [Test]
  112. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  113. public void CtorArgumentOutOfRangeException1 ()
  114. {
  115. FileStream stream = null;
  116. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  117. DeleteFile (path);
  118. try {
  119. stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
  120. } finally {
  121. if (stream != null)
  122. stream.Close ();
  123. DeleteFile (path);
  124. }
  125. }
  126. [Test]
  127. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  128. public void CtorArgumentOutOfRangeException2 ()
  129. {
  130. FileStream stream = null;
  131. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  132. DeleteFile (path);
  133. try {
  134. stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
  135. } finally {
  136. if (stream != null)
  137. stream.Close ();
  138. DeleteFile (path);
  139. }
  140. }
  141. private void CtorDirectoryNotFoundException (FileMode mode)
  142. {
  143. string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
  144. if (Directory.Exists (path))
  145. Directory.Delete (path, true);
  146. FileStream stream = null;
  147. try {
  148. stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
  149. } finally {
  150. if (stream != null)
  151. stream.Close ();
  152. if (Directory.Exists (path))
  153. Directory.Delete (path, true);
  154. }
  155. }
  156. [Test]
  157. [ExpectedException (typeof (DirectoryNotFoundException))]
  158. public void CtorDirectoryNotFoundException_CreateNew ()
  159. {
  160. CtorDirectoryNotFoundException (FileMode.CreateNew);
  161. }
  162. [Test]
  163. [ExpectedException (typeof (DirectoryNotFoundException))]
  164. public void CtorDirectoryNotFoundException_Create ()
  165. {
  166. CtorDirectoryNotFoundException (FileMode.Create);
  167. }
  168. [Test]
  169. [ExpectedException (typeof (DirectoryNotFoundException))]
  170. public void CtorDirectoryNotFoundException_Open ()
  171. {
  172. CtorDirectoryNotFoundException (FileMode.Open);
  173. }
  174. [Test]
  175. [ExpectedException (typeof (DirectoryNotFoundException))]
  176. public void CtorDirectoryNotFoundException_OpenOrCreate ()
  177. {
  178. CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
  179. }
  180. [Test]
  181. [ExpectedException (typeof (DirectoryNotFoundException))]
  182. public void CtorDirectoryNotFoundException_Truncate ()
  183. {
  184. CtorDirectoryNotFoundException (FileMode.Truncate);
  185. }
  186. [Test]
  187. [ExpectedException (typeof (DirectoryNotFoundException))]
  188. public void CtorDirectoryNotFoundException_Append ()
  189. {
  190. CtorDirectoryNotFoundException (FileMode.Append);
  191. }
  192. [Test]
  193. #if NET_2_0
  194. // FileShare.Inheritable is ignored, but file does not exist
  195. [ExpectedException (typeof (FileNotFoundException))]
  196. #else
  197. // share: Enum value was out of legal range.
  198. // (FileShare.Inheritable is not valid)
  199. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  200. #endif
  201. public void CtorArgumentOutOfRangeException3 ()
  202. {
  203. string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
  204. DeleteFile (path);
  205. FileStream stream = null;
  206. try {
  207. stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
  208. } finally {
  209. if (stream != null)
  210. stream.Close ();
  211. DeleteFile (path);
  212. }
  213. }
  214. [Test]
  215. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  216. public void CtorArgumentOutOfRangeException4 ()
  217. {
  218. string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
  219. DeleteFile (path);
  220. FileStream stream = null;
  221. try {
  222. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
  223. } finally {
  224. if (stream != null)
  225. stream.Close ();
  226. DeleteFile (path);
  227. }
  228. }
  229. [Test]
  230. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  231. public void CtorBufferSizeZero ()
  232. {
  233. // Buffer size can't be zero
  234. string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
  235. DeleteFile (path);
  236. FileStream stream = null;
  237. try {
  238. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
  239. } finally {
  240. if (stream != null)
  241. stream.Close ();
  242. DeleteFile (path);
  243. }
  244. }
  245. [Test]
  246. [ExpectedException (typeof (ArgumentException))]
  247. public void CtorArgumentException2 ()
  248. {
  249. // FileMode.CreateNew && FileAccess.Read
  250. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  251. FileStream stream = null;
  252. DeleteFile (path);
  253. try {
  254. stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
  255. } finally {
  256. if (stream != null)
  257. stream.Close ();
  258. DeleteFile (path);
  259. }
  260. }
  261. [Test]
  262. #if NET_2_0
  263. // FileShare.Inheritable is ignored, but file does not exist
  264. [ExpectedException (typeof (FileNotFoundException))]
  265. #else
  266. // share: Enum value was out of legal range.
  267. // (FileShare.Inheritable is not valid)
  268. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  269. #endif
  270. public void CtorArgumentOutOfRangeException5 ()
  271. {
  272. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  273. DeleteFile (path);
  274. FileStream stream = null;
  275. try {
  276. stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
  277. } finally {
  278. if (stream != null)
  279. stream.Close ();
  280. DeleteFile (path);
  281. }
  282. }
  283. [Test]
  284. [ExpectedException (typeof (ArgumentException))]
  285. public void CtorArgumentException3 ()
  286. {
  287. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  288. FileStream stream = null;
  289. DeleteFile (path);
  290. try {
  291. stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
  292. } finally {
  293. if (stream != null)
  294. stream.Close ();
  295. DeleteFile (path);
  296. }
  297. }
  298. [Test]
  299. public void ModeAndAccessCombinations ()
  300. {
  301. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  302. DeleteFile (path);
  303. FileStream stream = null;
  304. // Append / Read
  305. try {
  306. // Append access can be requested only in write-only mode
  307. stream = new FileStream (path, FileMode.Append, FileAccess.Read);
  308. Assert.Fail ("#A1");
  309. } catch (ArgumentException ex) {
  310. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  311. } finally {
  312. if (stream != null)
  313. stream.Close ();
  314. DeleteFile (path);
  315. }
  316. // Append / ReadWrite
  317. try {
  318. // Append access can be requested only in write-only mode
  319. stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
  320. Assert.Fail ("#B1");
  321. } catch (ArgumentException ex) {
  322. // make sure it is exact this exception, and not a derived type
  323. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  324. } finally {
  325. if (stream != null)
  326. stream.Close ();
  327. DeleteFile (path);
  328. }
  329. // Append / Write
  330. try {
  331. stream = new FileStream (path, FileMode.Append, FileAccess.Write);
  332. } finally {
  333. if (stream != null)
  334. stream.Close ();
  335. DeleteFile (path);
  336. }
  337. // Create / Read
  338. try {
  339. stream = new FileStream (path, FileMode.Create, FileAccess.Read);
  340. Assert.Fail ("#C1");
  341. } catch (ArgumentException ex) {
  342. // make sure it is exact this exception, and not a derived type
  343. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  344. } finally {
  345. if (stream != null)
  346. stream.Close ();
  347. DeleteFile (path);
  348. }
  349. // Create / ReadWrite
  350. try {
  351. stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
  352. } finally {
  353. if (stream != null)
  354. stream.Close ();
  355. DeleteFile (path);
  356. }
  357. // Create / Write
  358. try {
  359. stream = new FileStream (path, FileMode.Create, FileAccess.Write);
  360. } finally {
  361. if (stream != null)
  362. stream.Close ();
  363. DeleteFile (path);
  364. }
  365. // CreateNew / Read
  366. try {
  367. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
  368. Assert.Fail ("#D1");
  369. } catch (ArgumentException ex) {
  370. // make sure it is exact this exception, and not a derived type
  371. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  372. } finally {
  373. if (stream != null)
  374. stream.Close ();
  375. DeleteFile (path);
  376. }
  377. // CreateNew / ReadWrite
  378. try {
  379. stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  380. } finally {
  381. if (stream != null)
  382. stream.Close ();
  383. DeleteFile (path);
  384. }
  385. // CreateNew / Write
  386. try {
  387. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
  388. } finally {
  389. if (stream != null)
  390. stream.Close ();
  391. DeleteFile (path);
  392. }
  393. // Open / Read
  394. try {
  395. stream = new FileStream (path, FileMode.Open, FileAccess.Read);
  396. Assert.Fail ("#E1");
  397. } catch (FileNotFoundException ex) {
  398. // make sure it is exact this exception, and not a derived type
  399. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
  400. } finally {
  401. if (stream != null)
  402. stream.Close ();
  403. DeleteFile (path);
  404. }
  405. // Open / ReadWrite
  406. try {
  407. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  408. Assert.Fail ("#F1");
  409. } catch (FileNotFoundException ex) {
  410. // make sure it is exact this exception, and not a derived type
  411. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
  412. } finally {
  413. if (stream != null)
  414. stream.Close ();
  415. DeleteFile (path);
  416. }
  417. // Open / Write
  418. try {
  419. stream = new FileStream (path, FileMode.Open, FileAccess.Write);
  420. Assert.Fail ("#G1");
  421. } catch (FileNotFoundException ex) {
  422. // make sure it is exact this exception, and not a derived type
  423. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
  424. } finally {
  425. if (stream != null)
  426. stream.Close ();
  427. DeleteFile (path);
  428. }
  429. // OpenOrCreate / Read
  430. try {
  431. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  432. } finally {
  433. if (stream != null)
  434. stream.Close ();
  435. DeleteFile (path);
  436. }
  437. // OpenOrCreate / ReadWrite
  438. try {
  439. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  440. } finally {
  441. if (stream != null)
  442. stream.Close ();
  443. DeleteFile (path);
  444. }
  445. // OpenOrCreate / Write
  446. try {
  447. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  448. } finally {
  449. if (stream != null)
  450. stream.Close ();
  451. DeleteFile (path);
  452. }
  453. // Truncate / Read
  454. try {
  455. stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
  456. Assert.Fail ("#H1");
  457. } catch (ArgumentException ex) {
  458. // make sure it is exact this exception, and not a derived type
  459. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
  460. } finally {
  461. if (stream != null)
  462. stream.Close ();
  463. DeleteFile (path);
  464. }
  465. // Truncate / ReadWrite
  466. try {
  467. stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
  468. Assert.Fail ("#I1");
  469. } catch (FileNotFoundException ex) {
  470. // make sure it is exact this exception, and not a derived type
  471. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
  472. } finally {
  473. if (stream != null)
  474. stream.Close ();
  475. DeleteFile (path);
  476. }
  477. // Truncate / Write
  478. try {
  479. stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
  480. Assert.Fail ("#J1");
  481. } catch (FileNotFoundException ex) {
  482. // make sure it is exact this exception, and not a derived type
  483. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
  484. } finally {
  485. if (stream != null)
  486. stream.Close ();
  487. DeleteFile (path);
  488. }
  489. }
  490. [Test, ExpectedException (typeof (IOException))]
  491. public void CtorIOException2 ()
  492. {
  493. FileStream stream = null;
  494. try {
  495. stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
  496. } finally {
  497. if (stream != null)
  498. stream.Close ();
  499. }
  500. }
  501. [Test, ExpectedException (typeof (IOException))]
  502. public void CtorIOException ()
  503. {
  504. string path = TempFolder + DSC + "CTorIOException.Test";
  505. FileStream stream = null;
  506. FileStream stream2 = null;
  507. DeleteFile (path);
  508. try {
  509. stream = new FileStream (path, FileMode.CreateNew);
  510. // used by an another process
  511. stream2 = new FileStream (path, FileMode.OpenOrCreate);
  512. } finally {
  513. if (stream != null)
  514. stream.Close ();
  515. if (stream2 != null)
  516. stream2.Close ();
  517. DeleteFile (path);
  518. }
  519. }
  520. [Test]
  521. public void CtorAccess1Read2Read ()
  522. {
  523. FileStream fs = null;
  524. FileStream fs2 = null;
  525. string fn = Path.Combine (TempFolder, "temp");
  526. try {
  527. if (!File.Exists (fn)) {
  528. TextWriter tw = File.CreateText (fn);
  529. tw.Write ("FOO");
  530. tw.Close ();
  531. }
  532. fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
  533. fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
  534. } finally {
  535. if (fs != null)
  536. fs.Close ();
  537. if (fs2 != null)
  538. fs2.Close ();
  539. if (File.Exists (fn))
  540. File.Delete (fn);
  541. }
  542. }
  543. [Test]
  544. [ExpectedException (typeof (IOException))]
  545. public void CtorAccess1Read2Write ()
  546. {
  547. string fn = Path.Combine (TempFolder, "temp");
  548. FileStream fs = null;
  549. try {
  550. if (!File.Exists (fn)) {
  551. using (TextWriter tw = File.CreateText (fn)) {
  552. tw.Write ("FOO");
  553. }
  554. }
  555. fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
  556. fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
  557. } finally {
  558. if (fs != null)
  559. fs.Close ();
  560. if (File.Exists (fn))
  561. File.Delete (fn);
  562. }
  563. }
  564. [Test]
  565. [ExpectedException (typeof (IOException))]
  566. public void CtorAccess1Write2Write ()
  567. {
  568. string fn = Path.Combine (TempFolder, "temp");
  569. FileStream fs = null;
  570. try {
  571. if (File.Exists (fn))
  572. File.Delete (fn);
  573. fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
  574. fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
  575. } finally {
  576. if (fs != null)
  577. fs.Close ();
  578. if (File.Exists (fn))
  579. File.Delete (fn);
  580. }
  581. }
  582. [Test]
  583. [ExpectedException (typeof (UnauthorizedAccessException))]
  584. public void CtorReadDirectoryAsFile ()
  585. {
  586. FileStream stream = null;
  587. try {
  588. stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
  589. } finally {
  590. if (stream != null)
  591. stream.Close ();
  592. }
  593. }
  594. [Test]
  595. public void Write ()
  596. {
  597. string path = TempFolder + DSC + "FileStreamTest.Write";
  598. DeleteFile (path);
  599. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
  600. byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  601. byte[] bytes = new byte[15];
  602. // Check that the data is flushed when we overflow the buffer
  603. // with a large amount of data
  604. stream.Write (outbytes, 0, 5);
  605. stream.Write (outbytes, 5, 10);
  606. stream.Seek (0, SeekOrigin.Begin);
  607. stream.Read (bytes, 0, 15);
  608. for (int i = 0; i < 15; ++i)
  609. Assert.AreEqual (i + 1, bytes[i], "#1");
  610. // Check that the data is flushed when we overflow the buffer
  611. // with a small amount of data
  612. stream.Write (outbytes, 0, 7);
  613. stream.Write (outbytes, 7, 7);
  614. stream.Write (outbytes, 14, 1);
  615. stream.Read (bytes, 0, 15);
  616. stream.Seek (15, SeekOrigin.Begin);
  617. for (int i = 0; i < 15; ++i)
  618. Assert.AreEqual (i + 1, bytes[i], "#2");
  619. stream.Close ();
  620. }
  621. [Test]
  622. public void Length ()
  623. {
  624. // Test that the Length property takes into account the data
  625. // in the buffer
  626. string path = TempFolder + DSC + "FileStreamTest.Length";
  627. DeleteFile (path);
  628. FileStream stream = new FileStream (path, FileMode.CreateNew);
  629. byte[] outbytes = new byte[] { 1, 2, 3, 4 };
  630. stream.Write (outbytes, 0, 4);
  631. Assert.AreEqual (4, stream.Length);
  632. stream.Close ();
  633. }
  634. [Test]
  635. public void Flush ()
  636. {
  637. string path = TempFolder + DSC + "FileStreamTest.Flush";
  638. FileStream stream = null;
  639. FileStream stream2 = null;
  640. DeleteFile (path);
  641. try {
  642. stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  643. stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  644. stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
  645. byte[] bytes = new byte[5];
  646. stream2.Read (bytes, 0, 5);
  647. Assert.AreEqual (0, bytes[0], "#A1");
  648. Assert.AreEqual (0, bytes[1], "#A2");
  649. Assert.AreEqual (0, bytes[2], "#A3");
  650. Assert.AreEqual (0, bytes[3], "#A4");
  651. stream.Flush ();
  652. stream2.Read (bytes, 0, 5);
  653. Assert.AreEqual (1, bytes[0], "#B1");
  654. Assert.AreEqual (2, bytes[1], "#B2");
  655. Assert.AreEqual (3, bytes[2], "#B3");
  656. Assert.AreEqual (4, bytes[3], "#B4");
  657. } finally {
  658. if (stream != null)
  659. stream.Close ();
  660. if (stream2 != null)
  661. stream2.Close ();
  662. DeleteFile (path);
  663. }
  664. }
  665. public void TestDefaultProperties ()
  666. {
  667. string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
  668. DeleteFile (path);
  669. FileStream stream = new FileStream (path, FileMode.Create);
  670. Assert.IsTrue (stream.CanRead, "#A1");
  671. Assert.IsTrue (stream.CanSeek, "#A2");
  672. Assert.IsTrue (stream.CanWrite, "#A3");
  673. Assert.IsFalse (stream.IsAsync, "#A4");
  674. Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
  675. Assert.AreEqual (0, stream.Position, "#A6");
  676. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
  677. stream.Close ();
  678. DeleteFile (path);
  679. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  680. Assert.IsTrue (stream.CanRead, "#B1");
  681. Assert.IsTrue (stream.CanSeek, "#B2");
  682. Assert.IsFalse (stream.CanWrite, "#B3");
  683. Assert.IsFalse (stream.IsAsync, "#B4");
  684. Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
  685. Assert.AreEqual (0, stream.Position, "#B6");
  686. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
  687. stream.Close ();
  688. stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
  689. Assert.IsFalse (stream.CanRead, "#C1");
  690. Assert.IsTrue (stream.CanSeek, "#C2");
  691. Assert.IsTrue (stream.CanWrite, "#C3");
  692. Assert.IsFalse (stream.IsAsync, "#C4");
  693. Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
  694. Assert.AreEqual (0, stream.Position, "#C6");
  695. Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
  696. stream.Close ();
  697. DeleteFile (path);
  698. }
  699. [Category ("NotWorking")]
  700. // Bug: 71371 -> duplicate and WONTFIX.
  701. public void TestLock_FailsOnMono ()
  702. {
  703. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  704. DeleteFile (path);
  705. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  706. stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  707. stream.Close ();
  708. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  709. stream.Lock (0, 5);
  710. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  711. byte[] bytes = new byte[5];
  712. try {
  713. stream2.Read (bytes, 0, 5);
  714. Assert.Fail ("#1");
  715. } catch (Exception e) {
  716. Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
  717. }
  718. stream.Close ();
  719. stream2.Close ();
  720. DeleteFile (path);
  721. }
  722. public void TestLock ()
  723. {
  724. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  725. DeleteFile (path);
  726. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  727. stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  728. stream.Close ();
  729. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  730. stream.Lock (0, 5);
  731. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  732. byte[] bytes = new byte[5];
  733. try {
  734. stream2.Read (bytes, 0, 5);
  735. Assert.Fail ("#A1");
  736. } catch (Exception) {
  737. // Bug #71371: on MS.NET you get an IOException detailing a lock
  738. // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
  739. }
  740. stream2.Seek (5, SeekOrigin.Begin);
  741. stream2.Read (bytes, 0, 5);
  742. Assert.AreEqual (5, bytes[0], "#B1");
  743. Assert.AreEqual (6, bytes[1], "#B2");
  744. Assert.AreEqual (7, bytes[2], "#B3");
  745. Assert.AreEqual (8, bytes[3], "#B4");
  746. Assert.AreEqual (9, bytes[4], "#B5");
  747. stream.Unlock (0, 5);
  748. stream2.Seek (0, SeekOrigin.Begin);
  749. stream2.Read (bytes, 0, 5);
  750. Assert.AreEqual (0, bytes[0], "#C1");
  751. Assert.AreEqual (1, bytes[1], "#C2");
  752. Assert.AreEqual (2, bytes[2], "#C3");
  753. Assert.AreEqual (3, bytes[3], "#C4");
  754. Assert.AreEqual (4, bytes[4], "#C5");
  755. stream.Close ();
  756. stream2.Close ();
  757. DeleteFile (path);
  758. }
  759. [Test]
  760. public void Seek ()
  761. {
  762. string path = TempFolder + DSC + "FST.Seek.Test";
  763. DeleteFile (path);
  764. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  765. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  766. stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
  767. Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
  768. Assert.AreEqual (-1, stream2.ReadByte (), "#2");
  769. Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
  770. Assert.AreEqual (-1, stream2.ReadByte (), "#4");
  771. Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
  772. Assert.AreEqual (-1, stream.ReadByte (), "#6");
  773. Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
  774. Assert.AreEqual (6, stream.ReadByte (), "#8");
  775. Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
  776. Assert.AreEqual (6, stream2.ReadByte (), "#10");
  777. stream.Close ();
  778. stream2.Close ();
  779. DeleteFile (path);
  780. }
  781. public void TestSeek ()
  782. {
  783. string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
  784. DeleteFile (path);
  785. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  786. stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
  787. stream.Seek (5, SeekOrigin.End);
  788. Assert.AreEqual (-1, stream.ReadByte (), "#1");
  789. stream.Seek (-5, SeekOrigin.End);
  790. Assert.AreEqual (6, stream.ReadByte (), "#2");
  791. try {
  792. stream.Seek (-11, SeekOrigin.End);
  793. Assert.Fail ("#3");
  794. } catch (Exception e) {
  795. Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
  796. }
  797. stream.Seek (19, SeekOrigin.Begin);
  798. Assert.AreEqual (-1, stream.ReadByte (), "#5");
  799. stream.Seek (1, SeekOrigin.Begin);
  800. Assert.AreEqual (2, stream.ReadByte (), "#6");
  801. stream.Seek (3, SeekOrigin.Current);
  802. Assert.AreEqual (6, stream.ReadByte (), "#7");
  803. stream.Seek (-2, SeekOrigin.Current);
  804. Assert.AreEqual (5, stream.ReadByte (), "#8");
  805. stream.Flush ();
  806. // Test that seeks work correctly when seeking inside the buffer
  807. stream.Seek (0, SeekOrigin.Begin);
  808. stream.WriteByte (0);
  809. stream.WriteByte (1);
  810. stream.Seek (0, SeekOrigin.Begin);
  811. byte[] buf = new byte[1];
  812. buf[0] = 2;
  813. stream.Write (buf, 0, 1);
  814. stream.Write (buf, 0, 1);
  815. stream.Flush ();
  816. stream.Seek (0, SeekOrigin.Begin);
  817. Assert.AreEqual (2, stream.ReadByte (), "#9");
  818. Assert.AreEqual (2, stream.ReadByte (), "#10");
  819. stream.Close ();
  820. DeleteFile (path);
  821. }
  822. public void TestClose ()
  823. {
  824. string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
  825. DeleteFile (path);
  826. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  827. stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
  828. stream.ReadByte ();
  829. stream.Close ();
  830. try {
  831. stream.ReadByte ();
  832. Assert.Fail ("#A1");
  833. } catch (Exception e) {
  834. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
  835. }
  836. try {
  837. stream.WriteByte (64);
  838. Assert.Fail ("#B1");
  839. } catch (Exception e) {
  840. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
  841. }
  842. try {
  843. stream.Flush ();
  844. Assert.Fail ("#C1");
  845. } catch (Exception e) {
  846. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
  847. }
  848. try {
  849. long l = stream.Length;
  850. Assert.Fail ("#D1");
  851. } catch (Exception e) {
  852. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
  853. }
  854. try {
  855. long l = stream.Position;
  856. Assert.Fail ("#E1");
  857. } catch (Exception e) {
  858. Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
  859. }
  860. Assert.IsFalse (stream.CanRead, "#F1");
  861. Assert.IsFalse (stream.CanSeek, "#F2");
  862. Assert.IsFalse (stream.CanWrite, "#F3");
  863. Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
  864. DeleteFile (path);
  865. }
  866. /// <summary>
  867. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  868. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  869. /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
  870. /// </summary>
  871. [Test]
  872. [ExpectedException (typeof (NotSupportedException))]
  873. public void TestWriteVerifyAccessMode ()
  874. {
  875. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  876. DeleteFile (path);
  877. FileStream stream = null;
  878. byte[] buffer;
  879. try {
  880. buffer = Encoding.ASCII.GetBytes ("test");
  881. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  882. stream.Write (buffer, 0, buffer.Length);
  883. } finally {
  884. if (stream != null)
  885. stream.Close ();
  886. DeleteFile (path);
  887. }
  888. }
  889. /// <summary>
  890. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  891. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  892. /// <see cref="FileStream.WriteByte(byte)" /> method is called.
  893. /// </summary>
  894. [Test]
  895. [ExpectedException (typeof (NotSupportedException))]
  896. public void TestWriteByteVerifyAccessMode ()
  897. {
  898. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  899. DeleteFile (path);
  900. FileStream stream = null;
  901. try {
  902. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  903. stream.WriteByte (Byte.MinValue);
  904. } finally {
  905. if (stream != null)
  906. stream.Close ();
  907. DeleteFile (path);
  908. }
  909. }
  910. /// <summary>
  911. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  912. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  913. /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
  914. /// </summary>
  915. [Test]
  916. [ExpectedException (typeof (NotSupportedException))]
  917. public void TestReadVerifyAccessMode ()
  918. {
  919. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  920. DeleteFile (path);
  921. FileStream stream = null;
  922. byte[] buffer = new byte[100];
  923. try {
  924. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  925. stream.Read (buffer, 0, buffer.Length);
  926. } finally {
  927. if (stream != null)
  928. stream.Close ();
  929. }
  930. }
  931. /// <summary>
  932. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  933. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  934. /// <see cref="FileStream.ReadByte()" /> method is called.
  935. /// </summary>
  936. [Test]
  937. [ExpectedException (typeof (NotSupportedException))]
  938. public void TestReadByteVerifyAccessMode ()
  939. {
  940. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  941. DeleteFile (path);
  942. FileStream stream = null;
  943. try {
  944. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  945. int readByte = stream.ReadByte ();
  946. } finally {
  947. if (stream != null)
  948. stream.Close ();
  949. DeleteFile (path);
  950. }
  951. }
  952. // Check that the stream is flushed even when it doesn't own the
  953. // handle
  954. [Test]
  955. public void TestFlushNotOwningHandle ()
  956. {
  957. string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
  958. DeleteFile (path);
  959. FileStream s = new FileStream (path, FileMode.Create);
  960. using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
  961. byte[] buf = new byte[2];
  962. buf[0] = (int) '1';
  963. s2.Write (buf, 0, 1);
  964. }
  965. s.Position = 0;
  966. Assert.AreEqual ((int) '1', s.ReadByte ());
  967. s.Close ();
  968. }
  969. private void DeleteFile (string path)
  970. {
  971. if (File.Exists (path))
  972. File.Delete (path);
  973. }
  974. [Test]
  975. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  976. public void Read_OffsetNegative ()
  977. {
  978. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  979. DeleteFile (path);
  980. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  981. stream.Read (new byte[0], -1, 1);
  982. }
  983. }
  984. [Test]
  985. [ExpectedException (typeof (ArgumentException))]
  986. public void Read_OffsetOverflow ()
  987. {
  988. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  989. DeleteFile (path);
  990. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  991. stream.Read (new byte[0], Int32.MaxValue, 1);
  992. }
  993. }
  994. [Test]
  995. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  996. public void Read_CountNegative ()
  997. {
  998. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  999. DeleteFile (path);
  1000. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1001. stream.Read (new byte[0], 1, -1);
  1002. }
  1003. }
  1004. [Test]
  1005. [ExpectedException (typeof (ArgumentException))]
  1006. public void Read_CountOverflow ()
  1007. {
  1008. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1009. DeleteFile (path);
  1010. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1011. stream.Read (new byte[0], 1, Int32.MaxValue);
  1012. }
  1013. }
  1014. [Test]
  1015. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1016. public void Write_OffsetNegative ()
  1017. {
  1018. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1019. DeleteFile (path);
  1020. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1021. stream.Write (new byte[0], -1, 1);
  1022. }
  1023. }
  1024. [Test]
  1025. [ExpectedException (typeof (ArgumentException))]
  1026. public void Write_OffsetOverflow ()
  1027. {
  1028. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1029. DeleteFile (path);
  1030. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1031. stream.Write (new byte[0], Int32.MaxValue, 1);
  1032. }
  1033. }
  1034. [Test]
  1035. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1036. public void Write_CountNegative ()
  1037. {
  1038. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1039. DeleteFile (path);
  1040. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1041. stream.Write (new byte[0], 1, -1);
  1042. }
  1043. }
  1044. [Test]
  1045. [ExpectedException (typeof (ArgumentException))]
  1046. public void Write_CountOverflow ()
  1047. {
  1048. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1049. DeleteFile (path);
  1050. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1051. stream.Write (new byte[0], 1, Int32.MaxValue);
  1052. }
  1053. }
  1054. [Test]
  1055. [ExpectedException (typeof (ArgumentException))]
  1056. public void Seek_InvalidSeekOrigin ()
  1057. {
  1058. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1059. DeleteFile (path);
  1060. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1061. stream.Seek (0, (SeekOrigin) (-1));
  1062. }
  1063. }
  1064. [Test]
  1065. [ExpectedException (typeof (ArgumentException))]
  1066. public void Constructor_InvalidFileHandle ()
  1067. {
  1068. new FileStream ((IntPtr) (-1L), FileAccess.Read);
  1069. }
  1070. [Test]
  1071. public void PositionAfterSetLength ()
  1072. {
  1073. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1074. DeleteFile (path);
  1075. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  1076. stream.SetLength (32);
  1077. stream.Position = 32;
  1078. stream.SetLength (16);
  1079. Assert.AreEqual (16, stream.Position);
  1080. }
  1081. }
  1082. [Test]
  1083. [ExpectedException (typeof (ObjectDisposedException))]
  1084. public void SetLength_Disposed ()
  1085. {
  1086. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1087. DeleteFile (path);
  1088. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1089. stream.Close ();
  1090. stream.SetLength (16);
  1091. }
  1092. [Test]
  1093. [ExpectedException (typeof (ObjectDisposedException))]
  1094. public void Position_Disposed ()
  1095. {
  1096. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1097. DeleteFile (path);
  1098. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1099. stream.Close ();
  1100. stream.Position = 0;
  1101. }
  1102. [Test]
  1103. [ExpectedException (typeof (ObjectDisposedException))]
  1104. public void BeginRead_Disposed ()
  1105. {
  1106. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1107. DeleteFile (path);
  1108. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  1109. stream.Close ();
  1110. stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
  1111. }
  1112. [Test]
  1113. [ExpectedException (typeof (ObjectDisposedException))]
  1114. public void BeginWrite_Disposed ()
  1115. {
  1116. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1117. DeleteFile (path);
  1118. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1119. stream.Close ();
  1120. stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
  1121. }
  1122. [Test]
  1123. [ExpectedException (typeof (ObjectDisposedException))]
  1124. public void Lock_Disposed ()
  1125. {
  1126. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1127. DeleteFile (path);
  1128. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1129. stream.Close ();
  1130. stream.Lock (0, 1);
  1131. }
  1132. [Test]
  1133. [ExpectedException (typeof (ObjectDisposedException))]
  1134. public void Unlock_Disposed ()
  1135. {
  1136. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1137. DeleteFile (path);
  1138. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  1139. stream.Close ();
  1140. stream.Unlock (0, 1);
  1141. }
  1142. [Test]
  1143. public void ReadBytePastEndOfStream ()
  1144. {
  1145. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  1146. DeleteFile (path);
  1147. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  1148. stream.Seek (0, SeekOrigin.End);
  1149. Assert.AreEqual (-1, stream.ReadByte ());
  1150. stream.Close ();
  1151. }
  1152. }
  1153. [Test]
  1154. [ExpectedException (typeof (NotSupportedException))]
  1155. public void SetLengthWithClosedBaseStream ()
  1156. {
  1157. string fn = Path.Combine (TempFolder, "temp");
  1158. try {
  1159. FileStream fs = new FileStream (fn, FileMode.Create);
  1160. BufferedStream bs = new BufferedStream (fs);
  1161. fs.Close ();
  1162. bs.SetLength (1000);
  1163. } finally {
  1164. File.Delete (fn);
  1165. }
  1166. }
  1167. [Test]
  1168. public void LengthAfterWrite ()
  1169. {
  1170. string path = TempFolder + DSC + "oneofthefilescreated.txt";
  1171. FileStream fs = null;
  1172. DeleteFile (path);
  1173. try {
  1174. fs = new FileStream (path, FileMode.CreateNew);
  1175. fs.WriteByte (Convert.ToByte ('A'));
  1176. byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
  1177. fs.Write (buffer, 0, buffer.Length);
  1178. fs.Seek (0, SeekOrigin.Begin);
  1179. char a = Convert.ToChar (fs.ReadByte ());
  1180. Assert.AreEqual ('A', a, "#A1");
  1181. Assert.AreEqual (23, fs.Length, "#A2");
  1182. int nread = fs.Read (buffer, 0, 5);
  1183. Assert.AreEqual (5, nread, "#A3");
  1184. } finally {
  1185. if (fs != null)
  1186. fs.Close ();
  1187. DeleteFile (path);
  1188. }
  1189. }
  1190. #if NET_2_0
  1191. [Test] public void DeleteOnClose ()
  1192. {
  1193. string path = TempFolder + DSC + "created.txt";
  1194. DeleteFile (path);
  1195. FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
  1196. FileOptions.DeleteOnClose);
  1197. Assert.AreEqual (true, File.Exists (path), "DOC#1");
  1198. fs.Close ();
  1199. Assert.AreEqual (false, File.Exists (path), "DOC#2");
  1200. }
  1201. #endif
  1202. }
  1203. }