FileStreamTest.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. 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. [Test]
  142. [ExpectedException (typeof (DirectoryNotFoundException))]
  143. public void CtorDirectoryNotFoundException ()
  144. {
  145. string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
  146. if (Directory.Exists (path))
  147. Directory.Delete (path, true);
  148. FileStream stream = null;
  149. try {
  150. stream = new FileStream (path + DSC + "eitherthisfile.test", FileMode.CreateNew);
  151. } finally {
  152. if (stream != null)
  153. stream.Close ();
  154. if (Directory.Exists (path))
  155. Directory.Delete (path, true);
  156. }
  157. }
  158. [Test]
  159. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  160. public void CtorArgumentOutOfRangeException3 ()
  161. {
  162. string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
  163. DeleteFile (path);
  164. FileStream stream = null;
  165. try {
  166. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Inheritable);
  167. } finally {
  168. if (stream != null)
  169. stream.Close ();
  170. DeleteFile (path);
  171. }
  172. }
  173. [Test]
  174. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  175. public void CtorArgumentOutOfRangeException4 ()
  176. {
  177. string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
  178. DeleteFile (path);
  179. FileStream stream = null;
  180. try {
  181. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
  182. } finally {
  183. if (stream != null)
  184. stream.Close ();
  185. DeleteFile (path);
  186. }
  187. }
  188. [Test]
  189. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  190. public void CtorBufferSizeZero ()
  191. {
  192. // Buffer size can't be zero
  193. string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
  194. DeleteFile (path);
  195. FileStream stream = null;
  196. try {
  197. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
  198. } finally {
  199. if (stream != null)
  200. stream.Close ();
  201. DeleteFile (path);
  202. }
  203. }
  204. [Test]
  205. [ExpectedException (typeof (ArgumentException))]
  206. public void CtorArgumentException2 ()
  207. {
  208. // FileMode.CreateNew && FileAccess.Read
  209. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  210. FileStream stream = null;
  211. DeleteFile (path);
  212. try {
  213. stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
  214. } finally {
  215. if (stream != null)
  216. stream.Close ();
  217. DeleteFile (path);
  218. }
  219. }
  220. [Test]
  221. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  222. public void CtorArgumentOutOfRangeException5 ()
  223. {
  224. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  225. DeleteFile (path);
  226. FileStream stream = null;
  227. try {
  228. stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
  229. } finally {
  230. if (stream != null)
  231. stream.Close ();
  232. DeleteFile (path);
  233. }
  234. }
  235. [Test]
  236. [ExpectedException (typeof (ArgumentException))]
  237. public void CtorArgumentException3 ()
  238. {
  239. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  240. FileStream stream = null;
  241. DeleteFile (path);
  242. try {
  243. stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
  244. } finally {
  245. if (stream != null)
  246. stream.Close ();
  247. DeleteFile (path);
  248. }
  249. }
  250. [Test, ExpectedException (typeof (IOException))]
  251. public void CtorIOException2 ()
  252. {
  253. FileStream stream = null;
  254. try {
  255. stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
  256. } finally {
  257. if (stream != null)
  258. stream.Close ();
  259. }
  260. }
  261. [Test, ExpectedException(typeof(IOException))]
  262. public void CtorIOException ()
  263. {
  264. string path = TempFolder + DSC + "CTorIOException.Test";
  265. FileStream stream = null;
  266. FileStream stream2 = null;
  267. DeleteFile (path);
  268. try {
  269. stream = new FileStream (path, FileMode.CreateNew);
  270. // used by an another process
  271. stream2 = new FileStream (path, FileMode.OpenOrCreate);
  272. } finally {
  273. if (stream != null)
  274. stream.Close ();
  275. if (stream2 != null)
  276. stream2.Close ();
  277. DeleteFile (path);
  278. }
  279. }
  280. [Test]
  281. public void CtorAccess1Read2Read ()
  282. {
  283. FileStream fs = null;
  284. FileStream fs2 = null;
  285. try {
  286. if (!File.Exists ("temp")) {
  287. TextWriter tw = File.CreateText ("temp");
  288. tw.Write ("FOO");
  289. tw.Close ();
  290. }
  291. fs = new FileStream ("temp", FileMode.Open, FileAccess.Read);
  292. fs2 = new FileStream ("temp", FileMode.Open, FileAccess.Read);
  293. } finally {
  294. if (fs != null)
  295. fs.Close ();
  296. if (fs2 != null)
  297. fs2.Close ();
  298. if (File.Exists ("temp"))
  299. File.Delete ("temp");
  300. }
  301. }
  302. [Test]
  303. [ExpectedException (typeof (IOException))]
  304. public void CtorAccess1Read2Write ()
  305. {
  306. FileStream fs = null;
  307. try {
  308. if (!File.Exists ("temp")) {
  309. using (TextWriter tw = File.CreateText ("temp")) {
  310. tw.Write ("FOO");
  311. }
  312. }
  313. fs = new FileStream ("temp", FileMode.Open, FileAccess.Read);
  314. fs = new FileStream ("temp", FileMode.Create, FileAccess.Write);
  315. } finally {
  316. if (fs != null)
  317. fs.Close ();
  318. if (File.Exists ("temp"))
  319. File.Delete ("temp");
  320. }
  321. }
  322. [Test]
  323. [ExpectedException (typeof (IOException))]
  324. public void CtorAccess1Write2Write ()
  325. {
  326. FileStream fs = null;
  327. try {
  328. if (File.Exists ("temp"))
  329. File.Delete ("temp");
  330. fs = new FileStream ("temp", FileMode.Create, FileAccess.Write);
  331. fs = new FileStream ("temp", FileMode.Create, FileAccess.Write);
  332. } finally {
  333. if (fs != null)
  334. fs.Close ();
  335. if (File.Exists ("temp"))
  336. File.Delete ("temp");
  337. }
  338. }
  339. [Test]
  340. public void Write ()
  341. {
  342. string path = TempFolder + DSC + "FileStreamTest.Write";
  343. DeleteFile (path);
  344. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
  345. byte[] outbytes = new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  346. byte[] bytes = new byte [15];
  347. // Check that the data is flushed when we overflow the buffer
  348. // with a large amount of data
  349. stream.Write (outbytes, 0, 5);
  350. stream.Write (outbytes, 5, 10);
  351. stream.Seek (0, SeekOrigin.Begin);
  352. stream.Read (bytes, 0, 15);
  353. for (int i = 0; i < 15; ++i)
  354. AssertEquals (i + 1, bytes [i]);
  355. // Check that the data is flushed when we overflow the buffer
  356. // with a small amount of data
  357. stream.Write (outbytes, 0, 7);
  358. stream.Write (outbytes, 7, 7);
  359. stream.Write (outbytes, 14, 1);
  360. stream.Read (bytes, 0, 15);
  361. stream.Seek (15, SeekOrigin.Begin);
  362. for (int i = 0; i < 15; ++i)
  363. AssertEquals (i + 1, bytes [i]);
  364. stream.Close ();
  365. }
  366. [Test]
  367. public void Length ()
  368. {
  369. // Test that the Length property takes into account the data
  370. // in the buffer
  371. string path = TempFolder + DSC + "FileStreamTest.Length";
  372. DeleteFile (path);
  373. FileStream stream = new FileStream (path, FileMode.CreateNew);
  374. byte[] outbytes = new byte [] {1, 2, 3, 4};
  375. stream.Write (outbytes, 0, 4);
  376. AssertEquals (stream.Length, 4);
  377. stream.Close ();
  378. }
  379. [Test]
  380. public void Flush ()
  381. {
  382. string path = TempFolder + DSC + "FileStreamTest.Flush";
  383. FileStream stream = null;
  384. FileStream stream2 = null;
  385. DeleteFile (path);
  386. try {
  387. stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  388. stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  389. stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
  390. byte [] bytes = new byte [5];
  391. stream2.Read (bytes, 0, 5);
  392. AssertEquals ("test#01", 0, bytes [0]);
  393. AssertEquals ("test#02", 0, bytes [1]);
  394. AssertEquals ("test#03", 0, bytes [2]);
  395. AssertEquals ("test#04", 0, bytes [3]);
  396. stream.Flush ();
  397. stream2.Read (bytes, 0, 5);
  398. AssertEquals ("test#05", 1, bytes [0]);
  399. AssertEquals ("test#06", 2, bytes [1]);
  400. AssertEquals ("test#07", 3, bytes [2]);
  401. AssertEquals ("test#08", 4, bytes [3]);
  402. } finally {
  403. if (stream != null)
  404. stream.Close ();
  405. if (stream2 != null)
  406. stream2.Close ();
  407. DeleteFile (path);
  408. }
  409. }
  410. public void TestDefaultProperties ()
  411. {
  412. string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
  413. DeleteFile (path);
  414. FileStream stream = new FileStream (path, FileMode.Create);
  415. AssertEquals ("test#01", true, stream.CanRead);
  416. AssertEquals ("test#02", true, stream.CanSeek);
  417. AssertEquals ("test#03", true, stream.CanWrite);
  418. AssertEquals ("test#04", false, stream.IsAsync);
  419. AssertEquals ("test#05", true, stream.Name.EndsWith (path));
  420. AssertEquals ("test#06", 0, stream.Position);
  421. AssertEquals ("test#07", "System.IO.FileStream", stream.ToString());
  422. stream.Close ();
  423. DeleteFile (path);
  424. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  425. AssertEquals ("test#08", true, stream.CanRead);
  426. AssertEquals ("test#09", true, stream.CanSeek);
  427. AssertEquals ("test#10", false, stream.CanWrite);
  428. AssertEquals ("test#11", false, stream.IsAsync);
  429. AssertEquals ("test#12", true, stream.Name.EndsWith (path));
  430. AssertEquals ("test#13", 0, stream.Position);
  431. AssertEquals ("test#14", "System.IO.FileStream", stream.ToString());
  432. stream.Close ();
  433. stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
  434. AssertEquals ("test#15", false, stream.CanRead);
  435. AssertEquals ("test#16", true, stream.CanSeek);
  436. AssertEquals ("test#17", true, stream.CanWrite);
  437. AssertEquals ("test#18", false, stream.IsAsync);
  438. AssertEquals ("test#19", true, stream.Name.EndsWith ("testfilestream.tmp.2"));
  439. AssertEquals ("test#20", 0, stream.Position);
  440. AssertEquals ("test#21", "System.IO.FileStream", stream.ToString());
  441. stream.Close ();
  442. DeleteFile (path);
  443. }
  444. [Category("NotWorking")]
  445. // Bug: 71371
  446. public void TestLock_FailsOnMono ()
  447. {
  448. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  449. DeleteFile (path);
  450. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  451. stream.Write (new Byte [] {0,1,2,3,4,5,6,7,8,9,10}, 0, 10);
  452. stream.Close ();
  453. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  454. stream.Lock (0, 5);
  455. FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  456. byte [] bytes = new byte [5];
  457. try {
  458. stream2.Read (bytes, 0, 5);
  459. Fail ();
  460. } catch (Exception e) {
  461. // locked
  462. AssertEquals ("test#01", typeof (IOException), e.GetType ());
  463. }
  464. }
  465. public void TestLock()
  466. {
  467. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  468. DeleteFile (path);
  469. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  470. stream.Write (new Byte [] {0,1,2,3,4,5,6,7,8,9,10}, 0, 10);
  471. stream.Close ();
  472. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  473. stream.Lock (0, 5);
  474. FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  475. byte [] bytes = new byte [5];
  476. try {
  477. stream2.Read (bytes, 0, 5);
  478. Fail ();
  479. } catch (Exception e) {
  480. // locked
  481. // AssertEquals ("test#01", typeof (IOException), e.GetType ());
  482. //
  483. // Moved into the previous test case.
  484. }
  485. stream2.Seek (5, SeekOrigin.Begin);
  486. stream2.Read (bytes, 0, 5);
  487. AssertEquals ("test#02", 5, bytes [0]);
  488. AssertEquals ("test#03", 6, bytes [1]);
  489. AssertEquals ("test#04", 7, bytes [2]);
  490. AssertEquals ("test#05", 8, bytes [3]);
  491. AssertEquals ("test#06", 9, bytes [4]);
  492. stream.Unlock (0,5);
  493. stream2.Seek (0, SeekOrigin.Begin);
  494. stream2.Read (bytes, 0, 5);
  495. AssertEquals ("test#02", 0, bytes [0]);
  496. AssertEquals ("test#03", 1, bytes [1]);
  497. AssertEquals ("test#04", 2, bytes [2]);
  498. AssertEquals ("test#05", 3, bytes [3]);
  499. AssertEquals ("test#06", 4, bytes [4]);
  500. stream.Close ();
  501. stream2.Close ();
  502. DeleteFile (path);
  503. }
  504. [Test]
  505. public void Seek ()
  506. {
  507. string path = TempFolder + DSC + "FST.Seek.Test";
  508. DeleteFile (path);
  509. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  510. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  511. stream.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}, 0, 9);
  512. AssertEquals ("test#01", 5, stream2.Seek (5, SeekOrigin.Begin));
  513. AssertEquals ("test#02", -1, stream2.ReadByte ());
  514. AssertEquals ("test#03", 2, stream2.Seek (-3, SeekOrigin.Current));
  515. AssertEquals ("test#04", -1, stream2.ReadByte ());
  516. AssertEquals ("test#05", 12, stream.Seek (3, SeekOrigin.Current));
  517. AssertEquals ("test#06", -1, stream.ReadByte ());
  518. AssertEquals ("test#07", 5, stream.Seek (-7, SeekOrigin.Current));
  519. AssertEquals ("test#08", 6, stream.ReadByte ());
  520. AssertEquals ("test#09", 5, stream2.Seek (5, SeekOrigin.Begin));
  521. AssertEquals ("test#10", 6, stream2.ReadByte ());
  522. stream.Close ();
  523. stream2.Close ();
  524. DeleteFile (path);
  525. }
  526. public void TestSeek ()
  527. {
  528. string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
  529. DeleteFile (path);
  530. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  531. stream.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}, 0, 10);
  532. stream.Seek (5, SeekOrigin.End);
  533. AssertEquals ("test#01", -1, stream.ReadByte ());
  534. stream.Seek (-5, SeekOrigin.End);
  535. AssertEquals ("test#02", 6, stream.ReadByte ());
  536. try {
  537. stream.Seek (-11, SeekOrigin.End);
  538. Fail ();
  539. } catch (Exception e) {
  540. AssertEquals ("test#03", typeof (IOException), e.GetType ());
  541. }
  542. stream.Seek (19, SeekOrigin.Begin);
  543. AssertEquals ("test#04", -1, stream.ReadByte ());
  544. stream.Seek (1, SeekOrigin.Begin);
  545. AssertEquals ("test#05", 2, stream.ReadByte ());
  546. stream.Seek (3, SeekOrigin.Current);
  547. AssertEquals ("test#06", 6, stream.ReadByte ());
  548. stream.Seek (-2, SeekOrigin.Current);
  549. AssertEquals ("test#07", 5, stream.ReadByte ());
  550. stream.Flush ();
  551. // Test that seeks work correctly when seeking inside the buffer
  552. stream.Seek (0, SeekOrigin.Begin);
  553. stream.WriteByte (0);
  554. stream.WriteByte (1);
  555. stream.Seek (0, SeekOrigin.Begin);
  556. byte[] buf = new byte [1];
  557. buf [0] = 2;
  558. stream.Write (buf, 0, 1);
  559. stream.Write (buf, 0, 1);
  560. stream.Flush ();
  561. stream.Seek (0, SeekOrigin.Begin);
  562. AssertEquals ("test#08", 2, stream.ReadByte ());
  563. AssertEquals ("test#09", 2, stream.ReadByte ());
  564. stream.Close ();
  565. DeleteFile (path);
  566. }
  567. public void TestClose ()
  568. {
  569. string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
  570. DeleteFile (path);
  571. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  572. stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
  573. stream.ReadByte ();
  574. stream.Close ();
  575. try {
  576. stream.ReadByte ();
  577. Fail ();
  578. } catch (Exception e) {
  579. AssertEquals ("test#01", typeof (ObjectDisposedException), e.GetType ());
  580. }
  581. try {
  582. stream.WriteByte (64);
  583. Fail ();
  584. } catch (Exception e) {
  585. AssertEquals ("test#02", typeof (ObjectDisposedException), e.GetType ());
  586. }
  587. try {
  588. stream.Flush ();
  589. Fail ();
  590. } catch (Exception e) {
  591. AssertEquals ("test#03", typeof (ObjectDisposedException), e.GetType ());
  592. }
  593. try {
  594. long l = stream.Length;
  595. Fail ();
  596. } catch (Exception e) {
  597. AssertEquals ("test#04", typeof (ObjectDisposedException), e.GetType ());
  598. }
  599. try {
  600. long l = stream.Position;
  601. Fail ();
  602. } catch (Exception e) {
  603. AssertEquals ("test#05", typeof (ObjectDisposedException), e.GetType ());
  604. }
  605. AssertEquals ("test#06", false, stream.CanRead);
  606. AssertEquals ("test#07", false, stream.CanSeek);
  607. AssertEquals ("test#08", false, stream.CanWrite);
  608. AssertEquals ("test#09", true, stream.Name.EndsWith (path));
  609. DeleteFile (path);
  610. }
  611. /// <summary>
  612. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  613. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  614. /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
  615. /// </summary>
  616. [Test]
  617. [ExpectedException (typeof(NotSupportedException))]
  618. public void TestWriteVerifyAccessMode ()
  619. {
  620. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  621. DeleteFile (path);
  622. FileStream stream = null;
  623. byte[] buffer;
  624. try {
  625. buffer = Encoding.ASCII.GetBytes ("test");
  626. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  627. stream.Write (buffer, 0, buffer.Length);
  628. } finally {
  629. if (stream != null)
  630. stream.Close();
  631. DeleteFile (path);
  632. }
  633. }
  634. /// <summary>
  635. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  636. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  637. /// <see cref="FileStream.WriteByte(byte)" /> method is called.
  638. /// </summary>
  639. [Test]
  640. [ExpectedException (typeof (NotSupportedException))]
  641. public void TestWriteByteVerifyAccessMode ()
  642. {
  643. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  644. DeleteFile (path);
  645. FileStream stream = null;
  646. try {
  647. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  648. stream.WriteByte (Byte.MinValue);
  649. } finally {
  650. if (stream != null)
  651. stream.Close ();
  652. DeleteFile (path);
  653. }
  654. }
  655. /// <summary>
  656. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  657. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  658. /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
  659. /// </summary>
  660. [Test]
  661. [ExpectedException (typeof (NotSupportedException))]
  662. public void TestReadVerifyAccessMode ()
  663. {
  664. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  665. DeleteFile (path);
  666. FileStream stream = null;
  667. byte[] buffer = new byte [100];
  668. try {
  669. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  670. stream.Read (buffer, 0, buffer.Length);
  671. } finally {
  672. if (stream != null)
  673. stream.Close ();
  674. }
  675. }
  676. /// <summary>
  677. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  678. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  679. /// <see cref="FileStream.ReadByte()" /> method is called.
  680. /// </summary>
  681. [Test]
  682. [ExpectedException (typeof (NotSupportedException))]
  683. public void TestReadByteVerifyAccessMode ()
  684. {
  685. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  686. DeleteFile (path);
  687. FileStream stream = null;
  688. try {
  689. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  690. int readByte = stream.ReadByte ();
  691. } finally {
  692. if (stream != null)
  693. stream.Close();
  694. DeleteFile (path);
  695. }
  696. }
  697. // Check that the stream is flushed even when it doesn't own the
  698. // handle
  699. [Test]
  700. public void TestFlushNotOwningHandle ()
  701. {
  702. string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
  703. DeleteFile (path);
  704. FileStream s = new FileStream (path, FileMode.Create);
  705. using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
  706. byte[] buf = new byte [2];
  707. buf [0] = (int)'1';
  708. s2.Write (buf, 0, 1);
  709. }
  710. s.Position = 0;
  711. AssertEquals ((int)'1', s.ReadByte ());
  712. s.Close ();
  713. }
  714. private void DeleteFile (string path)
  715. {
  716. if (File.Exists (path))
  717. File.Delete (path);
  718. }
  719. [Test]
  720. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  721. public void Read_OffsetNegative ()
  722. {
  723. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  724. DeleteFile (path);
  725. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  726. stream.Read (new byte[0], -1, 1);
  727. }
  728. }
  729. [Test]
  730. [ExpectedException (typeof (ArgumentException))]
  731. public void Read_OffsetOverflow ()
  732. {
  733. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  734. DeleteFile (path);
  735. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  736. stream.Read (new byte[0], Int32.MaxValue, 1);
  737. }
  738. }
  739. [Test]
  740. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  741. public void Read_CountNegative ()
  742. {
  743. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  744. DeleteFile (path);
  745. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  746. stream.Read (new byte[0], 1, -1);
  747. }
  748. }
  749. [Test]
  750. [ExpectedException (typeof (ArgumentException))]
  751. public void Read_CountOverflow ()
  752. {
  753. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  754. DeleteFile (path);
  755. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  756. stream.Read (new byte[0], 1, Int32.MaxValue);
  757. }
  758. }
  759. [Test]
  760. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  761. public void Write_OffsetNegative ()
  762. {
  763. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  764. DeleteFile (path);
  765. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  766. stream.Write (new byte[0], -1, 1);
  767. }
  768. }
  769. [Test]
  770. [ExpectedException (typeof (ArgumentException))]
  771. public void Write_OffsetOverflow ()
  772. {
  773. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  774. DeleteFile (path);
  775. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  776. stream.Write (new byte[0], Int32.MaxValue, 1);
  777. }
  778. }
  779. [Test]
  780. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  781. public void Write_CountNegative ()
  782. {
  783. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  784. DeleteFile (path);
  785. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  786. stream.Write (new byte[0], 1, -1);
  787. }
  788. }
  789. [Test]
  790. [ExpectedException (typeof (ArgumentException))]
  791. public void Write_CountOverflow ()
  792. {
  793. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  794. DeleteFile (path);
  795. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  796. stream.Write (new byte[0], 1, Int32.MaxValue);
  797. }
  798. }
  799. [Test]
  800. [ExpectedException (typeof (ArgumentException))]
  801. public void Seek_InvalidSeekOrigin ()
  802. {
  803. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  804. DeleteFile (path);
  805. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  806. stream.Seek (0, (SeekOrigin) (-1));
  807. }
  808. }
  809. [Test]
  810. [ExpectedException (typeof (ArgumentException))]
  811. public void Constructor_InvalidFileHandle ()
  812. {
  813. new FileStream ((IntPtr)(-1), FileAccess.Read);
  814. }
  815. [Test]
  816. public void PositionAfterSetLength ()
  817. {
  818. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  819. DeleteFile (path);
  820. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  821. stream.SetLength (32);
  822. stream.Position = 32;
  823. stream.SetLength (16);
  824. AssertEquals ("Position==16", 16, stream.Position);
  825. }
  826. }
  827. [Test]
  828. [ExpectedException (typeof (ObjectDisposedException))]
  829. public void SetLength_Disposed ()
  830. {
  831. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  832. DeleteFile (path);
  833. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  834. stream.Close ();
  835. stream.SetLength (16);
  836. }
  837. [Test]
  838. [ExpectedException (typeof (ObjectDisposedException))]
  839. public void Position_Disposed ()
  840. {
  841. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  842. DeleteFile (path);
  843. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  844. stream.Close ();
  845. stream.Position = 0;
  846. }
  847. [Test]
  848. [ExpectedException (typeof (ObjectDisposedException))]
  849. public void BeginRead_Disposed ()
  850. {
  851. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  852. DeleteFile (path);
  853. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  854. stream.Close ();
  855. stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
  856. }
  857. [Test]
  858. [ExpectedException (typeof (ObjectDisposedException))]
  859. public void BeginWrite_Disposed ()
  860. {
  861. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  862. DeleteFile (path);
  863. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  864. stream.Close ();
  865. stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
  866. }
  867. [Test]
  868. [ExpectedException (typeof (ObjectDisposedException))]
  869. public void Lock_Disposed ()
  870. {
  871. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  872. DeleteFile (path);
  873. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  874. stream.Close ();
  875. stream.Lock (0,1);
  876. }
  877. [Test]
  878. [ExpectedException (typeof (ObjectDisposedException))]
  879. public void Unlock_Disposed ()
  880. {
  881. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  882. DeleteFile (path);
  883. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  884. stream.Close ();
  885. stream.Unlock (0,1);
  886. }
  887. [Test]
  888. public void ReadBytePastEndOfStream ()
  889. {
  890. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  891. DeleteFile (path);
  892. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  893. stream.Seek (0, SeekOrigin.End);
  894. AssertEquals ("ReadByte", -1, stream.ReadByte ());
  895. stream.Close ();
  896. }
  897. }
  898. [Test]
  899. [ExpectedException (typeof (NotSupportedException))]
  900. public void SetLengthWithClosedBaseStream ()
  901. {
  902. FileStream fs = new FileStream ("temp", FileMode.Create);
  903. BufferedStream bs = new BufferedStream (fs);
  904. fs.Close ();
  905. bs.SetLength (1000);
  906. }
  907. }
  908. }