FileStreamTest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // FileStreamTests.cs - NUnit2 Test Cases for System.IO.FileStream class
  2. //
  3. // Authors:
  4. // Ville Palo ([email protected])
  5. // Gert Driesen ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) Ville Palo
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. using NUnit.Framework;
  12. using System;
  13. using System.IO;
  14. using System.Text;
  15. namespace MonoTests.System.IO
  16. {
  17. public class FileStreamTest : TestCase
  18. {
  19. string bazFileName = Path.Combine ("resources", "baz");
  20. public FileStreamTest() {}
  21. [SetUp]
  22. protected override void SetUp ()
  23. {
  24. File.Delete (bazFileName);
  25. }
  26. [TearDown]
  27. protected override void TearDown ()
  28. {
  29. File.Delete (bazFileName);
  30. }
  31. public void TestCtr ()
  32. {
  33. FileStream stream = new FileStream ("testfilestream.tmp.1", FileMode.Create);
  34. stream.Close ();
  35. File.Delete ("testfilestream.tmp.1");
  36. }
  37. public void TestCtorExceptions ()
  38. {
  39. FileStream stream;
  40. try {
  41. stream = new FileStream ("", FileMode.Create);
  42. Fail ();
  43. } catch (Exception e) {
  44. AssertEquals ("test#01", typeof (ArgumentException), e.GetType ());
  45. }
  46. try {
  47. stream = new FileStream (null, FileMode.Create);
  48. Fail ();
  49. } catch (Exception e) {
  50. AssertEquals ("test#02", typeof (ArgumentNullException), e.GetType ());
  51. }
  52. try {
  53. if (File.Exists ("thisfileshouldnotexists.test"))
  54. File.Delete ("thisfileshouldnotexists.test");
  55. stream = new FileStream ("thisfileshouldnotexists.test", FileMode.Open);
  56. Fail ();
  57. } catch (Exception e) {
  58. AssertEquals ("test#03", typeof (FileNotFoundException), e.GetType ());
  59. }
  60. try {
  61. if (File.Exists ("thisfileshouldNOTexists.test"))
  62. File.Delete ("thisfileshouldNOTexists.test");
  63. stream = new FileStream ("thisfileshouldNOTexists.test", FileMode.Truncate);
  64. Fail ();
  65. } catch (Exception e) {
  66. AssertEquals ("test#04", typeof (FileNotFoundException), e.GetType ());
  67. }
  68. try {
  69. stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
  70. stream.Close ();
  71. stream = null;
  72. stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
  73. Fail ();
  74. } catch (Exception e) {
  75. if (File.Exists ("thisfileshouldexists.test")) // remove file
  76. File.Delete ("thisfileshouldexists.test");
  77. AssertEquals ("test#04", typeof (IOException), e.GetType ());
  78. }
  79. try {
  80. if (Directory.Exists ("thisDicrectoryShouldNotExists"))
  81. Directory.Delete ("thisDicrectoryShouldNotExists");
  82. stream = new FileStream ("thisDicrectoryShouldNotExists/eitherthisfile.test", FileMode.CreateNew);
  83. Fail ();
  84. } catch (Exception e) {
  85. AssertEquals ("test#05", typeof (DirectoryNotFoundException), e.GetType ());
  86. }
  87. try {
  88. stream = new FileStream ("test.test.test", FileMode.Append | FileMode.CreateNew);
  89. Fail ();
  90. } catch (Exception e) {
  91. AssertEquals ("test#08", typeof (ArgumentOutOfRangeException), e.GetType ());
  92. }
  93. try {
  94. stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
  95. Fail ();
  96. } catch (Exception e) {
  97. AssertEquals ("test#09", typeof (ArgumentOutOfRangeException), e.GetType ());
  98. }
  99. }
  100. public void TestCtorExceptions2 ()
  101. {
  102. FileStream stream;
  103. try {
  104. stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Inheritable);
  105. Fail ();
  106. } catch (Exception e) {
  107. AssertEquals ("test#01", typeof (ArgumentOutOfRangeException), e.GetType ());
  108. }
  109. try {
  110. stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
  111. Fail ();
  112. } catch (Exception e) {
  113. // FileMode.CreateNew && FileAccess.Read
  114. AssertEquals ("test#02", typeof (ArgumentException), e.GetType ());
  115. }
  116. try {
  117. stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
  118. Fail ();
  119. } catch (Exception e) {
  120. AssertEquals ("test#03", typeof (ArgumentOutOfRangeException), e.GetType ());
  121. }
  122. try {
  123. stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
  124. Fail ();
  125. } catch (Exception e) {
  126. // FileMode.Truncate && FileAccess.Read
  127. AssertEquals ("test#04", typeof (ArgumentException), e.GetType ());
  128. }
  129. try {
  130. stream = new FileStream (new IntPtr (12), FileAccess.Read);
  131. Fail ();
  132. } catch (Exception e) {
  133. // Invalid handle
  134. AssertEquals ("test#05", typeof (IOException), e.GetType ());
  135. }
  136. try {
  137. stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read, FileShare.ReadWrite, -1);
  138. Fail ();
  139. } catch (Exception e) {
  140. // FileMode.Truncate && FileAccess.Read
  141. AssertEquals ("test#06", typeof (ArgumentOutOfRangeException), e.GetType ());
  142. }
  143. }
  144. [Test]
  145. [ExpectedException(typeof(IOException))]
  146. public void CtorIOException ()
  147. {
  148. string path = "resources/CTorIOException.Test";
  149. if (File.Exists (path))
  150. File.Delete (path);
  151. FileStream stream = new FileStream (path, FileMode.CreateNew);
  152. // used by an another process
  153. FileStream stream2 = new FileStream (path, FileMode.OpenOrCreate);
  154. }
  155. [Test]
  156. public void Flush ()
  157. {
  158. string path = "resources/FileStreamTest.Flush";
  159. if (File.Exists (path))
  160. File.Delete (path);
  161. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  162. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  163. stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
  164. byte [] bytes = new byte [5];
  165. stream2.Read (bytes, 0, 5);
  166. Assertion.AssertEquals ("test#01", 0, bytes [0]);
  167. Assertion.AssertEquals ("test#02", 0, bytes [1]);
  168. Assertion.AssertEquals ("test#03", 0, bytes [2]);
  169. Assertion.AssertEquals ("test#04", 0, bytes [3]);
  170. stream.Flush ();
  171. stream2.Read (bytes, 0, 5);
  172. Assertion.AssertEquals ("test#05", 1, bytes [0]);
  173. Assertion.AssertEquals ("test#06", 2, bytes [1]);
  174. Assertion.AssertEquals ("test#07", 3, bytes [2]);
  175. Assertion.AssertEquals ("test#08", 4, bytes [3]);
  176. stream.Close ();
  177. stream2.Close ();
  178. if (File.Exists (path))
  179. File.Delete (path);
  180. }
  181. public void TestDefaultProperties ()
  182. {
  183. FileStream stream = new FileStream ("testfilestream.tmp.2", FileMode.Create);
  184. AssertEquals ("test#01", true, stream.CanRead);
  185. AssertEquals ("test#02", true, stream.CanSeek);
  186. AssertEquals ("test#03", true, stream.CanWrite);
  187. AssertEquals ("test#04", false, stream.IsAsync);
  188. AssertEquals ("test#05", true, stream.Name.EndsWith ("testfilestream.tmp.2"));
  189. AssertEquals ("test#06", 0, stream.Position);
  190. AssertEquals ("test#07", "System.IO.FileStream", stream.ToString());
  191. stream.Close ();
  192. File.Delete ("testfilestream.tmp.2");
  193. stream = new FileStream ("testfilestream.tmp.2", FileMode.OpenOrCreate, FileAccess.Read);
  194. AssertEquals ("test#08", true, stream.CanRead);
  195. AssertEquals ("test#09", true, stream.CanSeek);
  196. AssertEquals ("test#10", false, stream.CanWrite);
  197. AssertEquals ("test#11", false, stream.IsAsync);
  198. AssertEquals ("test#12", true, stream.Name.EndsWith ("testfilestream.tmp.2"));
  199. AssertEquals ("test#13", 0, stream.Position);
  200. AssertEquals ("test#14", "System.IO.FileStream", stream.ToString());
  201. stream.Close ();
  202. stream = new FileStream ("testfilestream.tmp.2", FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
  203. AssertEquals ("test#15", false, stream.CanRead);
  204. AssertEquals ("test#16", true, stream.CanSeek);
  205. AssertEquals ("test#17", true, stream.CanWrite);
  206. AssertEquals ("test#18", false, stream.IsAsync);
  207. AssertEquals ("test#19", true, stream.Name.EndsWith ("testfilestream.tmp.2"));
  208. AssertEquals ("test#20", 0, stream.Position);
  209. AssertEquals ("test#21", "System.IO.FileStream", stream.ToString());
  210. stream.Close ();
  211. File.Delete ("testfilestream.tmp.2");
  212. }
  213. public void TestLock()
  214. {
  215. if (File.Exists (".testFileStream.Test.1"))
  216. File.Delete (".testFileStream.Test.1");
  217. FileStream stream = new FileStream (".testFileStream.Test.1", FileMode.CreateNew, FileAccess.ReadWrite);
  218. stream.Write (new Byte [] {0,1,2,3,4,5,6,7,8,9,10}, 0, 10);
  219. stream.Close ();
  220. stream = new FileStream (".testFileStream.Test.1", FileMode.Open, FileAccess.ReadWrite);
  221. stream.Lock (0, 5);
  222. FileStream stream2 = new FileStream (".testFileStream.Test.1", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  223. byte [] bytes = new byte [5];
  224. try {
  225. stream2.Read (bytes, 0, 5);
  226. Fail ();
  227. } catch (Exception e) {
  228. // locked
  229. AssertEquals ("test#01", typeof (IOException), e.GetType ());
  230. }
  231. stream2.Seek (5, SeekOrigin.Begin);
  232. stream2.Read (bytes, 0, 5);
  233. AssertEquals ("test#02", 5, bytes [0]);
  234. AssertEquals ("test#03", 6, bytes [1]);
  235. AssertEquals ("test#04", 7, bytes [2]);
  236. AssertEquals ("test#05", 8, bytes [3]);
  237. AssertEquals ("test#06", 9, bytes [4]);
  238. stream.Unlock (0,5);
  239. stream2.Seek (0, SeekOrigin.Begin);
  240. stream2.Read (bytes, 0, 5);
  241. AssertEquals ("test#02", 0, bytes [0]);
  242. AssertEquals ("test#03", 1, bytes [1]);
  243. AssertEquals ("test#04", 2, bytes [2]);
  244. AssertEquals ("test#05", 3, bytes [3]);
  245. AssertEquals ("test#06", 4, bytes [4]);
  246. stream.Close ();
  247. stream2.Close ();
  248. if (File.Exists (".testFileStream.Test.1"))
  249. File.Delete (".testFileStream.Test.1");
  250. }
  251. [Test]
  252. public void Seek ()
  253. {
  254. string path = "resources/FST.Seek.Test";
  255. if (File.Exists (path))
  256. File.Delete (path);
  257. FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  258. FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  259. stream.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}, 0, 9);
  260. Assertion.AssertEquals ("test#01", 5, stream2.Seek (5, SeekOrigin.Begin));
  261. Assertion.AssertEquals ("test#02", -1, stream2.ReadByte ());
  262. Assertion.AssertEquals ("test#03", 2, stream2.Seek (-3, SeekOrigin.Current));
  263. Assertion.AssertEquals ("test#04", -1, stream2.ReadByte ());
  264. Assertion.AssertEquals ("test#05", 12, stream.Seek (3, SeekOrigin.Current));
  265. Assertion.AssertEquals ("test#06", -1, stream.ReadByte ());
  266. Assertion.AssertEquals ("test#07", 5, stream.Seek (-7, SeekOrigin.Current));
  267. Assertion.AssertEquals ("test#08", 6, stream.ReadByte ());
  268. Assertion.AssertEquals ("test#09", 5, stream2.Seek (5, SeekOrigin.Begin));
  269. Assertion.AssertEquals ("test#10", 6, stream2.ReadByte ());
  270. stream.Close ();
  271. stream2.Close ();
  272. if (File.Exists (path))
  273. File.Delete (path);
  274. }
  275. public void TestSeek ()
  276. {
  277. if (File.Exists (".testFileStream.Test.2"))
  278. File.Delete (".testFileStream.Test.2");
  279. FileStream stream = new FileStream (".testFileStream.Test.2", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
  280. stream.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}, 0, 10);
  281. stream.Seek (5, SeekOrigin.End);
  282. AssertEquals ("test#01", -1, stream.ReadByte ());
  283. stream.Seek (-5, SeekOrigin.End);
  284. AssertEquals ("test#02", 6, stream.ReadByte ());
  285. try {
  286. stream.Seek (-11, SeekOrigin.End);
  287. Fail ();
  288. } catch (Exception e) {
  289. AssertEquals ("test#03", typeof (IOException), e.GetType ());
  290. }
  291. stream.Seek (19, SeekOrigin.Begin);
  292. AssertEquals ("test#04", -1, stream.ReadByte ());
  293. stream.Seek (1, SeekOrigin.Begin);
  294. AssertEquals ("test#05", 2, stream.ReadByte ());
  295. stream.Seek (3, SeekOrigin.Current);
  296. AssertEquals ("test#06", 6, stream.ReadByte ());
  297. stream.Seek (-2, SeekOrigin.Current);
  298. AssertEquals ("test#07", 5, stream.ReadByte ());
  299. stream.Flush ();
  300. stream.Close ();
  301. if (File.Exists (".testFileStream.Test.2"))
  302. File.Delete (".testFileStream.Test.2");
  303. }
  304. public void TestClose ()
  305. {
  306. if (File.Exists (".testFileStream.Test.3"))
  307. File.Delete (".testFileStream.Test.3");
  308. FileStream stream = new FileStream (".testFileStream.Test.3", FileMode.CreateNew, FileAccess.ReadWrite);
  309. stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
  310. stream.ReadByte ();
  311. stream.Close ();
  312. try {
  313. stream.ReadByte ();
  314. Fail ();
  315. } catch (Exception e) {
  316. AssertEquals ("test#01", typeof (ObjectDisposedException), e.GetType ());
  317. }
  318. try {
  319. stream.WriteByte (64);
  320. Fail ();
  321. } catch (Exception e) {
  322. AssertEquals ("test#02", typeof (ObjectDisposedException), e.GetType ());
  323. }
  324. try {
  325. stream.Flush ();
  326. Fail ();
  327. } catch (Exception e) {
  328. AssertEquals ("test#03", typeof (ObjectDisposedException), e.GetType ());
  329. }
  330. try {
  331. long l = stream.Length;
  332. Fail ();
  333. } catch (Exception e) {
  334. AssertEquals ("test#04", typeof (ObjectDisposedException), e.GetType ());
  335. }
  336. try {
  337. long l = stream.Position;
  338. Fail ();
  339. } catch (Exception e) {
  340. AssertEquals ("test#05", typeof (ObjectDisposedException), e.GetType ());
  341. }
  342. AssertEquals ("test#06", false, stream.CanRead);
  343. AssertEquals ("test#07", false, stream.CanSeek);
  344. AssertEquals ("test#08", false, stream.CanWrite);
  345. AssertEquals ("test#09", true, stream.Name.EndsWith (".testFileStream.Test.3"));
  346. if (File.Exists (".testFileStream.Test.3"))
  347. File.Delete (".testFileStream.Test.3");
  348. }
  349. /// <summary>
  350. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  351. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  352. /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
  353. /// </summary>
  354. [Test]
  355. [ExpectedException (typeof(NotSupportedException))]
  356. public void TestWriteVerifyAccessMode ()
  357. {
  358. FileStream bazFileStream = null;
  359. byte[] buffer;
  360. try {
  361. buffer = Encoding.ASCII.GetBytes ("test");
  362. bazFileStream = new FileStream (bazFileName, FileMode.OpenOrCreate, FileAccess.Read);
  363. bazFileStream.Write (buffer, 0, buffer.Length);
  364. } finally {
  365. if (bazFileStream != null)
  366. bazFileStream.Close();
  367. }
  368. }
  369. /// <summary>
  370. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  371. /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
  372. /// <see cref="FileStream.WriteByte(byte)" /> method is called.
  373. /// </summary>
  374. [Test]
  375. [ExpectedException (typeof (NotSupportedException))]
  376. public void TestWriteByteVerifyAccessMode ()
  377. {
  378. FileStream bazFileStream = null;
  379. try {
  380. bazFileStream = new FileStream (bazFileName, FileMode.OpenOrCreate, FileAccess.Read);
  381. bazFileStream.WriteByte (Byte.MinValue);
  382. } finally {
  383. if (bazFileStream != null)
  384. bazFileStream.Close ();
  385. }
  386. }
  387. /// <summary>
  388. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  389. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  390. /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
  391. /// </summary>
  392. [Test]
  393. [ExpectedException (typeof (NotSupportedException))]
  394. public void TestReadVerifyAccessMode ()
  395. {
  396. FileStream bazFileStream = null;
  397. byte[] buffer = new byte [100];
  398. try {
  399. bazFileStream = new FileStream (bazFileName, FileMode.OpenOrCreate, FileAccess.Write);
  400. bazFileStream.Read (buffer, 0, buffer.Length);
  401. } finally {
  402. if (bazFileStream != null)
  403. bazFileStream.Close ();
  404. }
  405. }
  406. /// <summary>
  407. /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
  408. /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
  409. /// <see cref="FileStream.ReadByte()" /> method is called.
  410. /// </summary>
  411. [Test]
  412. [ExpectedException (typeof (NotSupportedException))]
  413. public void TestReadByteVerifyAccessMode ()
  414. {
  415. FileStream bazFileStream = null;
  416. try {
  417. bazFileStream = new FileStream (bazFileName, FileMode.OpenOrCreate, FileAccess.Write);
  418. int readByte = bazFileStream.ReadByte ();
  419. } finally {
  420. if (bazFileStream != null)
  421. bazFileStream.Close();
  422. }
  423. }
  424. }
  425. }