FileStreamTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. public class FileStreamTest : TestCase
  18. {
  19. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  20. public FileStreamTest()
  21. {
  22. if (Directory.Exists (TempFolder))
  23. Directory.Delete (TempFolder, true);
  24. Directory.CreateDirectory (TempFolder);
  25. }
  26. ~FileStreamTest()
  27. {
  28. if (Directory.Exists (TempFolder))
  29. Directory.Delete (TempFolder, true);
  30. }
  31. [SetUp]
  32. protected override void SetUp ()
  33. {
  34. if (!Directory.Exists (TempFolder))
  35. Directory.CreateDirectory (TempFolder);
  36. }
  37. [TearDown]
  38. protected override void TearDown ()
  39. {
  40. }
  41. public void TestCtr ()
  42. {
  43. string path = TempFolder + "/testfilestream.tmp.1";
  44. DeleteFile (path);
  45. FileStream stream = null;
  46. try {
  47. stream = new FileStream (path, FileMode.Create);
  48. } finally {
  49. if (stream != null)
  50. stream.Close ();
  51. DeleteFile (path);
  52. }
  53. }
  54. [Test]
  55. [ExpectedException (typeof (ArgumentException))]
  56. public void CtorArgumentException1 ()
  57. {
  58. FileStream stream;
  59. stream = new FileStream ("", FileMode.Create);
  60. stream.Close ();
  61. }
  62. [Test]
  63. [ExpectedException (typeof (ArgumentNullException))]
  64. public void CtorArgumentNullException ()
  65. {
  66. FileStream stream = new FileStream (null, FileMode.Create);
  67. stream.Close ();
  68. }
  69. [Test]
  70. [ExpectedException (typeof (FileNotFoundException))]
  71. public void CtorFileNotFoundException1 ()
  72. {
  73. string path = TempFolder + "/thisfileshouldnotexists.test";
  74. DeleteFile (path);
  75. FileStream stream = null;
  76. try {
  77. stream = new FileStream (TempFolder + "/thisfileshouldnotexists.test", FileMode.Open);
  78. } finally {
  79. if (stream != null)
  80. stream.Close ();
  81. DeleteFile (path);
  82. }
  83. }
  84. [Test]
  85. [ExpectedException (typeof (FileNotFoundException))]
  86. public void CtorFileNotFoundException2 ()
  87. {
  88. string path = TempFolder + "/thisfileshouldNOTexists.test";
  89. DeleteFile (path);
  90. FileStream stream = null;
  91. try {
  92. stream = new FileStream (TempFolder + "/thisfileshouldNOTexists.test", FileMode.Truncate);
  93. } finally {
  94. if (stream != null)
  95. stream.Close ();
  96. DeleteFile (path);
  97. }
  98. }
  99. [Test]
  100. [ExpectedException (typeof (IOException))]
  101. public void CtorIOException1 ()
  102. {
  103. string path = TempFolder + "/thisfileshouldexists.test";
  104. FileStream stream = null;
  105. DeleteFile (path);
  106. try {
  107. stream = new FileStream (path, FileMode.CreateNew);
  108. stream.Close ();
  109. stream = null;
  110. stream = new FileStream (path, FileMode.CreateNew);
  111. } finally {
  112. if (stream != null)
  113. stream.Close ();
  114. DeleteFile (path);
  115. }
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  119. public void CtorArgumentOutOfRangeException1 ()
  120. {
  121. FileStream stream = null;
  122. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  123. DeleteFile (path);
  124. try {
  125. stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
  126. } finally {
  127. if (stream != null)
  128. stream.Close ();
  129. DeleteFile (path);
  130. }
  131. }
  132. [Test]
  133. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  134. public void CtorArgumentOutOfRangeException2 ()
  135. {
  136. FileStream stream = null;
  137. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  138. DeleteFile (path);
  139. try {
  140. stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
  141. } finally {
  142. if (stream != null)
  143. stream.Close ();
  144. DeleteFile (path);
  145. }
  146. }
  147. [Test]
  148. [ExpectedException (typeof (DirectoryNotFoundException))]
  149. public void CtorDirectoryNotFoundException ()
  150. {
  151. string path = TempFolder + "/thisDicrectoryShouldNotExists";
  152. if (Directory.Exists (path))
  153. Directory.Delete (path, true);
  154. FileStream stream = null;
  155. try {
  156. stream = new FileStream (path + "/eitherthisfile.test", FileMode.CreateNew);
  157. } finally {
  158. if (stream != null)
  159. stream.Close ();
  160. if (Directory.Exists (path))
  161. Directory.Delete (path, true);
  162. }
  163. }
  164. [Test]
  165. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  166. public void CtorArgumentOutOfRangeException3 ()
  167. {
  168. string path = TempFolder + "/CtorArgumentOutOfRangeException1";
  169. DeleteFile (path);
  170. FileStream stream = null;
  171. try {
  172. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Inheritable);
  173. } finally {
  174. if (stream != null)
  175. stream.Close ();
  176. DeleteFile (path);
  177. }
  178. }
  179. [Test]
  180. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  181. public void CtorArgumentOutOfRangeException4 ()
  182. {
  183. string path = TempFolder + "/CtorArgumentOutOfRangeException2";
  184. DeleteFile (path);
  185. FileStream stream = null;
  186. try {
  187. stream = new FileStream (path, FileMode.Truncate, FileAccess.Read, FileShare.ReadWrite, -1);
  188. } finally {
  189. if (stream != null)
  190. stream.Close ();
  191. DeleteFile (path);
  192. }
  193. }
  194. [Test]
  195. [ExpectedException (typeof (ArgumentException))]
  196. public void CtorArgumentException2 ()
  197. {
  198. // FileMode.CreateNew && FileAccess.Read
  199. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  200. FileStream stream = null;
  201. DeleteFile (path);
  202. try {
  203. stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
  204. } finally {
  205. if (stream != null)
  206. stream.Close ();
  207. DeleteFile (path);
  208. }
  209. }
  210. [Test]
  211. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  212. public void CtorArgumentOutOfRangeException5 ()
  213. {
  214. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  215. DeleteFile (path);
  216. FileStream stream = null;
  217. try {
  218. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
  219. } finally {
  220. if (stream != null)
  221. stream.Close ();
  222. DeleteFile (path);
  223. }
  224. }
  225. [Test]
  226. [ExpectedException (typeof (ArgumentException))]
  227. public void CtorArgumentException3 ()
  228. {
  229. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  230. FileStream stream = null;
  231. DeleteFile (path);
  232. try {
  233. stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
  234. } finally {
  235. if (stream != null)
  236. stream.Close ();
  237. DeleteFile (path);
  238. }
  239. }
  240. [Test]
  241. [ExpectedException (typeof (IOException))]
  242. public void CtorIOException2 ()
  243. {
  244. FileStream stream = null;
  245. try {
  246. stream = new FileStream (new IntPtr (12), FileAccess.Read);
  247. } finally {
  248. if (stream != null)
  249. stream.Close ();
  250. }
  251. }
  252. [Test]
  253. [ExpectedException(typeof(IOException))]
  254. public void CtorIOException ()
  255. {
  256. string path = TempFolder + "/CTorIOException.Test";
  257. FileStream stream = null;
  258. FileStream stream2 = null;
  259. DeleteFile (path);
  260. try {
  261. stream = new FileStream (path, FileMode.CreateNew);
  262. // used by an another process
  263. stream2 = new FileStream (path, FileMode.OpenOrCreate);
  264. } finally {
  265. if (stream != null)
  266. stream.Close ();
  267. if (stream2 != null)
  268. stream2.Close ();
  269. DeleteFile (path);
  270. }
  271. }
  272. [Test]
  273. public void Flush ()
  274. {
  275. string path = TempFolder + "/FileStreamTest.Flush";
  276. FileStream stream = null;
  277. FileStream stream2 = null;
  278. DeleteFile (path);
  279. try {
  280. stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  281. stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  282. stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
  283. byte [] bytes = new byte [5];
  284. stream2.Read (bytes, 0, 5);
  285. Assertion.AssertEquals ("test#01", 0, bytes [0]);
  286. Assertion.AssertEquals ("test#02", 0, bytes [1]);
  287. Assertion.AssertEquals ("test#03", 0, bytes [2]);
  288. Assertion.AssertEquals ("test#04", 0, bytes [3]);
  289. stream.Flush ();
  290. stream2.Read (bytes, 0, 5);
  291. Assertion.AssertEquals ("test#05", 1, bytes [0]);
  292. Assertion.AssertEquals ("test#06", 2, bytes [1]);
  293. Assertion.AssertEquals ("test#07", 3, bytes [2]);
  294. Assertion.AssertEquals ("test#08", 4, bytes [3]);
  295. } finally {
  296. if (stream != null)
  297. stream.Close ();
  298. if (stream2 != null)
  299. stream2.Close ();
  300. DeleteFile (path);
  301. }
  302. }
  303. public void TestDefaultProperties ()
  304. {
  305. string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
  306. DeleteFile (path);
  307. FileStream stream = new FileStream (path, FileMode.Create);
  308. AssertEquals ("test#01", true, stream.CanRead);
  309. AssertEquals ("test#02", true, stream.CanSeek);
  310. AssertEquals ("test#03", true, stream.CanWrite);
  311. AssertEquals ("test#04", false, stream.IsAsync);
  312. AssertEquals ("test#05", true, stream.Name.EndsWith (path));
  313. AssertEquals ("test#06", 0, stream.Position);
  314. AssertEquals ("test#07", "System.IO.FileStream", stream.ToString());
  315. stream.Close ();
  316. DeleteFile (path);
  317. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  318. AssertEquals ("test#08", true, stream.CanRead);
  319. AssertEquals ("test#09", true, stream.CanSeek);
  320. AssertEquals ("test#10", false, stream.CanWrite);
  321. AssertEquals ("test#11", false, stream.IsAsync);
  322. AssertEquals ("test#12", true, stream.Name.EndsWith (path));
  323. AssertEquals ("test#13", 0, stream.Position);
  324. AssertEquals ("test#14", "System.IO.FileStream", stream.ToString());
  325. stream.Close ();
  326. stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
  327. AssertEquals ("test#15", false, stream.CanRead);
  328. AssertEquals ("test#16", true, stream.CanSeek);
  329. AssertEquals ("test#17", true, stream.CanWrite);
  330. AssertEquals ("test#18", false, stream.IsAsync);
  331. AssertEquals ("test#19", true, stream.Name.EndsWith ("testfilestream.tmp.2"));
  332. AssertEquals ("test#20", 0, stream.Position);
  333. AssertEquals ("test#21", "System.IO.FileStream", stream.ToString());
  334. stream.Close ();
  335. DeleteFile (path);
  336. }
  337. public void TestLock()
  338. {
  339. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  340. DeleteFile (path);
  341. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  342. stream.Write (new Byte [] {0,1,2,3,4,5,6,7,8,9,10}, 0, 10);
  343. stream.Close ();
  344. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  345. stream.Lock (0, 5);
  346. FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  347. byte [] bytes = new byte [5];
  348. try {
  349. stream2.Read (bytes, 0, 5);
  350. Fail ();
  351. } catch (Exception e) {
  352. // locked
  353. AssertEquals ("test#01", typeof (IOException), e.GetType ());
  354. }
  355. stream2.Seek (5, SeekOrigin.Begin);
  356. stream2.Read (bytes, 0, 5);
  357. AssertEquals ("test#02", 5, bytes [0]);
  358. AssertEquals ("test#03", 6, bytes [1]);
  359. AssertEquals ("test#04", 7, bytes [2]);
  360. AssertEquals ("test#05", 8, bytes [3]);
  361. AssertEquals ("test#06", 9, bytes [4]);
  362. stream.Unlock (0,5);
  363. stream2.Seek (0, SeekOrigin.Begin);
  364. stream2.Read (bytes, 0, 5);
  365. AssertEquals ("test#02", 0, bytes [0]);
  366. AssertEquals ("test#03", 1, bytes [1]);
  367. AssertEquals ("test#04", 2, bytes [2]);
  368. AssertEquals ("test#05", 3, bytes [3]);
  369. AssertEquals ("test#06", 4, bytes [4]);
  370. stream.Close ();
  371. stream2.Close ();
  372. DeleteFile (path);
  373. }
  374. [Test]
  375. public void Seek ()
  376. {
  377. string path = TempFolder + "/FST.Seek.Test";
  378. DeleteFile (path);
  379. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  380. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  381. stream.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}, 0, 9);
  382. Assertion.AssertEquals ("test#01", 5, stream2.Seek (5, SeekOrigin.Begin));
  383. Assertion.AssertEquals ("test#02", -1, stream2.ReadByte ());
  384. Assertion.AssertEquals ("test#03", 2, stream2.Seek (-3, SeekOrigin.Current));
  385. Assertion.AssertEquals ("test#04", -1, stream2.ReadByte ());
  386. Assertion.AssertEquals ("test#05", 12, stream.Seek (3, SeekOrigin.Current));
  387. Assertion.AssertEquals ("test#06", -1, stream.ReadByte ());
  388. Assertion.AssertEquals ("test#07", 5, stream.Seek (-7, SeekOrigin.Current));
  389. Assertion.AssertEquals ("test#08", 6, stream.ReadByte ());
  390. Assertion.AssertEquals ("test#09", 5, stream2.Seek (5, SeekOrigin.Begin));
  391. Assertion.AssertEquals ("test#10", 6, stream2.ReadByte ());
  392. stream.Close ();
  393. stream2.Close ();
  394. DeleteFile (path);
  395. }
  396. public void TestSeek ()
  397. {
  398. string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
  399. DeleteFile (path);
  400. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  401. stream.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}, 0, 10);
  402. stream.Seek (5, SeekOrigin.End);
  403. AssertEquals ("test#01", -1, stream.ReadByte ());
  404. stream.Seek (-5, SeekOrigin.End);
  405. AssertEquals ("test#02", 6, stream.ReadByte ());
  406. try {
  407. stream.Seek (-11, SeekOrigin.End);
  408. Fail ();
  409. } catch (Exception e) {
  410. AssertEquals ("test#03", typeof (IOException), e.GetType ());
  411. }
  412. stream.Seek (19, SeekOrigin.Begin);
  413. AssertEquals ("test#04", -1, stream.ReadByte ());
  414. stream.Seek (1, SeekOrigin.Begin);
  415. AssertEquals ("test#05", 2, stream.ReadByte ());
  416. stream.Seek (3, SeekOrigin.Current);
  417. AssertEquals ("test#06", 6, stream.ReadByte ());
  418. stream.Seek (-2, SeekOrigin.Current);
  419. AssertEquals ("test#07", 5, stream.ReadByte ());
  420. stream.Flush ();
  421. // Test that seeks work correctly when seeking inside the buffer
  422. stream.Seek (0, SeekOrigin.Begin);
  423. stream.WriteByte (0);
  424. stream.WriteByte (1);
  425. stream.Seek (0, SeekOrigin.Begin);
  426. byte[] buf = new byte [1];
  427. buf [0] = 2;
  428. stream.Write (buf, 0, 1);
  429. stream.Write (buf, 0, 1);
  430. stream.Flush ();
  431. stream.Seek (0, SeekOrigin.Begin);
  432. AssertEquals ("test#08", 2, stream.ReadByte ());
  433. AssertEquals ("test#09", 2, stream.ReadByte ());
  434. stream.Close ();
  435. DeleteFile (path);
  436. }
  437. public void TestClose ()
  438. {
  439. string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
  440. DeleteFile (path);
  441. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  442. stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
  443. stream.ReadByte ();
  444. stream.Close ();
  445. try {
  446. stream.ReadByte ();
  447. Fail ();
  448. } catch (Exception e) {
  449. AssertEquals ("test#01", typeof (ObjectDisposedException), e.GetType ());
  450. }
  451. try {
  452. stream.WriteByte (64);
  453. Fail ();
  454. } catch (Exception e) {
  455. AssertEquals ("test#02", typeof (ObjectDisposedException), e.GetType ());
  456. }
  457. try {
  458. stream.Flush ();
  459. Fail ();
  460. } catch (Exception e) {
  461. AssertEquals ("test#03", typeof (ObjectDisposedException), e.GetType ());
  462. }
  463. try {
  464. long l = stream.Length;
  465. Fail ();
  466. } catch (Exception e) {
  467. AssertEquals ("test#04", typeof (ObjectDisposedException), e.GetType ());
  468. }
  469. try {
  470. long l = stream.Position;
  471. Fail ();
  472. } catch (Exception e) {
  473. AssertEquals ("test#05", typeof (ObjectDisposedException), e.GetType ());
  474. }
  475. AssertEquals ("test#06", false, stream.CanRead);
  476. AssertEquals ("test#07", false, stream.CanSeek);
  477. AssertEquals ("test#08", false, stream.CanWrite);
  478. AssertEquals ("test#09", true, stream.Name.EndsWith (path));
  479. DeleteFile (path);
  480. }
  481. /// <summary>
  482. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  483. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  484. /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
  485. /// </summary>
  486. [Test]
  487. [ExpectedException (typeof(NotSupportedException))]
  488. public void TestWriteVerifyAccessMode ()
  489. {
  490. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  491. DeleteFile (path);
  492. FileStream stream = null;
  493. byte[] buffer;
  494. try {
  495. buffer = Encoding.ASCII.GetBytes ("test");
  496. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  497. stream.Write (buffer, 0, buffer.Length);
  498. } finally {
  499. if (stream != null)
  500. stream.Close();
  501. DeleteFile (path);
  502. }
  503. }
  504. /// <summary>
  505. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  506. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  507. /// <see cref="FileStream.WriteByte(byte)" /> method is called.
  508. /// </summary>
  509. [Test]
  510. [ExpectedException (typeof (NotSupportedException))]
  511. public void TestWriteByteVerifyAccessMode ()
  512. {
  513. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  514. DeleteFile (path);
  515. FileStream stream = null;
  516. try {
  517. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  518. stream.WriteByte (Byte.MinValue);
  519. } finally {
  520. if (stream != null)
  521. stream.Close ();
  522. DeleteFile (path);
  523. }
  524. }
  525. /// <summary>
  526. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  527. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  528. /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
  529. /// </summary>
  530. [Test]
  531. [ExpectedException (typeof (NotSupportedException))]
  532. public void TestReadVerifyAccessMode ()
  533. {
  534. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  535. DeleteFile (path);
  536. FileStream stream = null;
  537. byte[] buffer = new byte [100];
  538. try {
  539. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  540. stream.Read (buffer, 0, buffer.Length);
  541. } finally {
  542. if (stream != null)
  543. stream.Close ();
  544. }
  545. }
  546. /// <summary>
  547. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  548. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  549. /// <see cref="FileStream.ReadByte()" /> method is called.
  550. /// </summary>
  551. [Test]
  552. [ExpectedException (typeof (NotSupportedException))]
  553. public void TestReadByteVerifyAccessMode ()
  554. {
  555. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  556. DeleteFile (path);
  557. FileStream stream = null;
  558. try {
  559. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  560. int readByte = stream.ReadByte ();
  561. } finally {
  562. if (stream != null)
  563. stream.Close();
  564. DeleteFile (path);
  565. }
  566. }
  567. private void DeleteFile (string path)
  568. {
  569. if (File.Exists (path))
  570. File.Delete (path);
  571. }
  572. }
  573. }