FileStreamTest.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. public void TestLock()
  445. {
  446. string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
  447. DeleteFile (path);
  448. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  449. stream.Write (new Byte [] {0,1,2,3,4,5,6,7,8,9,10}, 0, 10);
  450. stream.Close ();
  451. stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
  452. stream.Lock (0, 5);
  453. FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  454. byte [] bytes = new byte [5];
  455. try {
  456. stream2.Read (bytes, 0, 5);
  457. Fail ();
  458. } catch (Exception e) {
  459. // locked
  460. AssertEquals ("test#01", typeof (IOException), e.GetType ());
  461. }
  462. stream2.Seek (5, SeekOrigin.Begin);
  463. stream2.Read (bytes, 0, 5);
  464. AssertEquals ("test#02", 5, bytes [0]);
  465. AssertEquals ("test#03", 6, bytes [1]);
  466. AssertEquals ("test#04", 7, bytes [2]);
  467. AssertEquals ("test#05", 8, bytes [3]);
  468. AssertEquals ("test#06", 9, bytes [4]);
  469. stream.Unlock (0,5);
  470. stream2.Seek (0, SeekOrigin.Begin);
  471. stream2.Read (bytes, 0, 5);
  472. AssertEquals ("test#02", 0, bytes [0]);
  473. AssertEquals ("test#03", 1, bytes [1]);
  474. AssertEquals ("test#04", 2, bytes [2]);
  475. AssertEquals ("test#05", 3, bytes [3]);
  476. AssertEquals ("test#06", 4, bytes [4]);
  477. stream.Close ();
  478. stream2.Close ();
  479. DeleteFile (path);
  480. }
  481. [Test]
  482. public void Seek ()
  483. {
  484. string path = TempFolder + DSC + "FST.Seek.Test";
  485. DeleteFile (path);
  486. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  487. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  488. stream.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}, 0, 9);
  489. AssertEquals ("test#01", 5, stream2.Seek (5, SeekOrigin.Begin));
  490. AssertEquals ("test#02", -1, stream2.ReadByte ());
  491. AssertEquals ("test#03", 2, stream2.Seek (-3, SeekOrigin.Current));
  492. AssertEquals ("test#04", -1, stream2.ReadByte ());
  493. AssertEquals ("test#05", 12, stream.Seek (3, SeekOrigin.Current));
  494. AssertEquals ("test#06", -1, stream.ReadByte ());
  495. AssertEquals ("test#07", 5, stream.Seek (-7, SeekOrigin.Current));
  496. AssertEquals ("test#08", 6, stream.ReadByte ());
  497. AssertEquals ("test#09", 5, stream2.Seek (5, SeekOrigin.Begin));
  498. AssertEquals ("test#10", 6, stream2.ReadByte ());
  499. stream.Close ();
  500. stream2.Close ();
  501. DeleteFile (path);
  502. }
  503. public void TestSeek ()
  504. {
  505. string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
  506. DeleteFile (path);
  507. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  508. stream.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}, 0, 10);
  509. stream.Seek (5, SeekOrigin.End);
  510. AssertEquals ("test#01", -1, stream.ReadByte ());
  511. stream.Seek (-5, SeekOrigin.End);
  512. AssertEquals ("test#02", 6, stream.ReadByte ());
  513. try {
  514. stream.Seek (-11, SeekOrigin.End);
  515. Fail ();
  516. } catch (Exception e) {
  517. AssertEquals ("test#03", typeof (IOException), e.GetType ());
  518. }
  519. stream.Seek (19, SeekOrigin.Begin);
  520. AssertEquals ("test#04", -1, stream.ReadByte ());
  521. stream.Seek (1, SeekOrigin.Begin);
  522. AssertEquals ("test#05", 2, stream.ReadByte ());
  523. stream.Seek (3, SeekOrigin.Current);
  524. AssertEquals ("test#06", 6, stream.ReadByte ());
  525. stream.Seek (-2, SeekOrigin.Current);
  526. AssertEquals ("test#07", 5, stream.ReadByte ());
  527. stream.Flush ();
  528. // Test that seeks work correctly when seeking inside the buffer
  529. stream.Seek (0, SeekOrigin.Begin);
  530. stream.WriteByte (0);
  531. stream.WriteByte (1);
  532. stream.Seek (0, SeekOrigin.Begin);
  533. byte[] buf = new byte [1];
  534. buf [0] = 2;
  535. stream.Write (buf, 0, 1);
  536. stream.Write (buf, 0, 1);
  537. stream.Flush ();
  538. stream.Seek (0, SeekOrigin.Begin);
  539. AssertEquals ("test#08", 2, stream.ReadByte ());
  540. AssertEquals ("test#09", 2, stream.ReadByte ());
  541. stream.Close ();
  542. DeleteFile (path);
  543. }
  544. public void TestClose ()
  545. {
  546. string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
  547. DeleteFile (path);
  548. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
  549. stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
  550. stream.ReadByte ();
  551. stream.Close ();
  552. try {
  553. stream.ReadByte ();
  554. Fail ();
  555. } catch (Exception e) {
  556. AssertEquals ("test#01", typeof (ObjectDisposedException), e.GetType ());
  557. }
  558. try {
  559. stream.WriteByte (64);
  560. Fail ();
  561. } catch (Exception e) {
  562. AssertEquals ("test#02", typeof (ObjectDisposedException), e.GetType ());
  563. }
  564. try {
  565. stream.Flush ();
  566. Fail ();
  567. } catch (Exception e) {
  568. AssertEquals ("test#03", typeof (ObjectDisposedException), e.GetType ());
  569. }
  570. try {
  571. long l = stream.Length;
  572. Fail ();
  573. } catch (Exception e) {
  574. AssertEquals ("test#04", typeof (ObjectDisposedException), e.GetType ());
  575. }
  576. try {
  577. long l = stream.Position;
  578. Fail ();
  579. } catch (Exception e) {
  580. AssertEquals ("test#05", typeof (ObjectDisposedException), e.GetType ());
  581. }
  582. AssertEquals ("test#06", false, stream.CanRead);
  583. AssertEquals ("test#07", false, stream.CanSeek);
  584. AssertEquals ("test#08", false, stream.CanWrite);
  585. AssertEquals ("test#09", true, stream.Name.EndsWith (path));
  586. DeleteFile (path);
  587. }
  588. /// <summary>
  589. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  590. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  591. /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
  592. /// </summary>
  593. [Test]
  594. [ExpectedException (typeof(NotSupportedException))]
  595. public void TestWriteVerifyAccessMode ()
  596. {
  597. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  598. DeleteFile (path);
  599. FileStream stream = null;
  600. byte[] buffer;
  601. try {
  602. buffer = Encoding.ASCII.GetBytes ("test");
  603. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  604. stream.Write (buffer, 0, buffer.Length);
  605. } finally {
  606. if (stream != null)
  607. stream.Close();
  608. DeleteFile (path);
  609. }
  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.WriteByte(byte)" /> method is called.
  615. /// </summary>
  616. [Test]
  617. [ExpectedException (typeof (NotSupportedException))]
  618. public void TestWriteByteVerifyAccessMode ()
  619. {
  620. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  621. DeleteFile (path);
  622. FileStream stream = null;
  623. try {
  624. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  625. stream.WriteByte (Byte.MinValue);
  626. } finally {
  627. if (stream != null)
  628. stream.Close ();
  629. DeleteFile (path);
  630. }
  631. }
  632. /// <summary>
  633. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  634. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  635. /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
  636. /// </summary>
  637. [Test]
  638. [ExpectedException (typeof (NotSupportedException))]
  639. public void TestReadVerifyAccessMode ()
  640. {
  641. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  642. DeleteFile (path);
  643. FileStream stream = null;
  644. byte[] buffer = new byte [100];
  645. try {
  646. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  647. stream.Read (buffer, 0, buffer.Length);
  648. } finally {
  649. if (stream != null)
  650. stream.Close ();
  651. }
  652. }
  653. /// <summary>
  654. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  655. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  656. /// <see cref="FileStream.ReadByte()" /> method is called.
  657. /// </summary>
  658. [Test]
  659. [ExpectedException (typeof (NotSupportedException))]
  660. public void TestReadByteVerifyAccessMode ()
  661. {
  662. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  663. DeleteFile (path);
  664. FileStream stream = null;
  665. try {
  666. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  667. int readByte = stream.ReadByte ();
  668. } finally {
  669. if (stream != null)
  670. stream.Close();
  671. DeleteFile (path);
  672. }
  673. }
  674. // Check that the stream is flushed even when it doesn't own the
  675. // handle
  676. [Test]
  677. public void TestFlushNotOwningHandle ()
  678. {
  679. string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
  680. DeleteFile (path);
  681. FileStream s = new FileStream (path, FileMode.Create);
  682. using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
  683. byte[] buf = new byte [2];
  684. buf [0] = (int)'1';
  685. s2.Write (buf, 0, 1);
  686. }
  687. s.Position = 0;
  688. AssertEquals ((int)'1', s.ReadByte ());
  689. s.Close ();
  690. }
  691. private void DeleteFile (string path)
  692. {
  693. if (File.Exists (path))
  694. File.Delete (path);
  695. }
  696. [Test]
  697. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  698. public void Read_OffsetNegative ()
  699. {
  700. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  701. DeleteFile (path);
  702. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  703. stream.Read (new byte[0], -1, 1);
  704. }
  705. }
  706. [Test]
  707. [ExpectedException (typeof (ArgumentException))]
  708. public void Read_OffsetOverflow ()
  709. {
  710. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  711. DeleteFile (path);
  712. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  713. stream.Read (new byte[0], Int32.MaxValue, 1);
  714. }
  715. }
  716. [Test]
  717. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  718. public void Read_CountNegative ()
  719. {
  720. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  721. DeleteFile (path);
  722. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  723. stream.Read (new byte[0], 1, -1);
  724. }
  725. }
  726. [Test]
  727. [ExpectedException (typeof (ArgumentException))]
  728. public void Read_CountOverflow ()
  729. {
  730. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  731. DeleteFile (path);
  732. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  733. stream.Read (new byte[0], 1, Int32.MaxValue);
  734. }
  735. }
  736. [Test]
  737. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  738. public void Write_OffsetNegative ()
  739. {
  740. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  741. DeleteFile (path);
  742. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  743. stream.Write (new byte[0], -1, 1);
  744. }
  745. }
  746. [Test]
  747. [ExpectedException (typeof (ArgumentException))]
  748. public void Write_OffsetOverflow ()
  749. {
  750. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  751. DeleteFile (path);
  752. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  753. stream.Write (new byte[0], Int32.MaxValue, 1);
  754. }
  755. }
  756. [Test]
  757. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  758. public void Write_CountNegative ()
  759. {
  760. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  761. DeleteFile (path);
  762. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  763. stream.Write (new byte[0], 1, -1);
  764. }
  765. }
  766. [Test]
  767. [ExpectedException (typeof (ArgumentException))]
  768. public void Write_CountOverflow ()
  769. {
  770. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  771. DeleteFile (path);
  772. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  773. stream.Write (new byte[0], 1, Int32.MaxValue);
  774. }
  775. }
  776. [Test]
  777. [ExpectedException (typeof (ArgumentException))]
  778. public void Seek_InvalidSeekOrigin ()
  779. {
  780. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  781. DeleteFile (path);
  782. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  783. stream.Seek (0, (SeekOrigin) (-1));
  784. }
  785. }
  786. [Test]
  787. [ExpectedException (typeof (ArgumentException))]
  788. public void Constructor_InvalidFileHandle ()
  789. {
  790. new FileStream ((IntPtr)(-1), FileAccess.Read);
  791. }
  792. [Test]
  793. public void PositionAfterSetLength ()
  794. {
  795. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  796. DeleteFile (path);
  797. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
  798. stream.SetLength (32);
  799. stream.Position = 32;
  800. stream.SetLength (16);
  801. AssertEquals ("Position==16", 16, stream.Position);
  802. }
  803. }
  804. [Test]
  805. [ExpectedException (typeof (ObjectDisposedException))]
  806. public void SetLength_Disposed ()
  807. {
  808. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  809. DeleteFile (path);
  810. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  811. stream.Close ();
  812. stream.SetLength (16);
  813. }
  814. [Test]
  815. [ExpectedException (typeof (ObjectDisposedException))]
  816. public void Position_Disposed ()
  817. {
  818. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  819. DeleteFile (path);
  820. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  821. stream.Close ();
  822. stream.Position = 0;
  823. }
  824. [Test]
  825. [ExpectedException (typeof (ObjectDisposedException))]
  826. public void BeginRead_Disposed ()
  827. {
  828. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  829. DeleteFile (path);
  830. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
  831. stream.Close ();
  832. stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
  833. }
  834. [Test]
  835. [ExpectedException (typeof (ObjectDisposedException))]
  836. public void BeginWrite_Disposed ()
  837. {
  838. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  839. DeleteFile (path);
  840. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  841. stream.Close ();
  842. stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
  843. }
  844. [Test]
  845. [ExpectedException (typeof (ObjectDisposedException))]
  846. public void Lock_Disposed ()
  847. {
  848. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  849. DeleteFile (path);
  850. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  851. stream.Close ();
  852. stream.Lock (0,1);
  853. }
  854. [Test]
  855. [ExpectedException (typeof (ObjectDisposedException))]
  856. public void Unlock_Disposed ()
  857. {
  858. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  859. DeleteFile (path);
  860. FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  861. stream.Close ();
  862. stream.Unlock (0,1);
  863. }
  864. [Test]
  865. public void ReadBytePastEndOfStream ()
  866. {
  867. string path = TempFolder + Path.DirectorySeparatorChar + "temp";
  868. DeleteFile (path);
  869. using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
  870. stream.Seek (0, SeekOrigin.End);
  871. AssertEquals ("ReadByte", -1, stream.ReadByte ());
  872. stream.Close ();
  873. }
  874. }
  875. [Test]
  876. [ExpectedException (typeof (NotSupportedException))]
  877. public void SetLengthWithClosedBaseStream ()
  878. {
  879. FileStream fs = new FileStream ("temp", FileMode.Create);
  880. BufferedStream bs = new BufferedStream (fs);
  881. fs.Close ();
  882. bs.SetLength (1000);
  883. }
  884. }
  885. }