FileStreamTest.cs 34 KB

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