FileStreamTest.cs 24 KB

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