MemoryStreamTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. //
  2. // System.IO.MemoryStreamTest
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. // Copyright (C) 2004 Novell (http://www.novell.com)
  11. //
  12. using NUnit.Framework;
  13. using System.IO;
  14. using System;
  15. using System.Text;
  16. namespace MonoTests.System.IO
  17. {
  18. [TestFixture]
  19. public class MemoryStreamTest
  20. {
  21. MemoryStream testStream;
  22. byte [] testStreamData;
  23. [SetUp]
  24. public void SetUp ()
  25. {
  26. testStreamData = new byte [100];
  27. for (int i = 0; i < 100; i++)
  28. testStreamData[i] = (byte) (100 - i);
  29. testStream = new MemoryStream (testStreamData);
  30. }
  31. //
  32. // Verify that the first count bytes in testBytes are the same as
  33. // the count bytes from index start in testStreamData
  34. //
  35. void VerifyTestData (string id, byte [] testBytes, int start, int count)
  36. {
  37. if (testBytes == null)
  38. Assert.Fail (id + "+1 testBytes is null");
  39. if (start < 0 ||
  40. count < 0 ||
  41. start + count > testStreamData.Length ||
  42. start > testStreamData.Length)
  43. throw new ArgumentOutOfRangeException (id + "+2");
  44. for (int test = 0; test < count; test++) {
  45. if (testBytes [test] == testStreamData [start + test])
  46. continue;
  47. string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
  48. failStr = String.Format (failStr,
  49. test,
  50. start + test,
  51. testBytes [test],
  52. testStreamData [start + test]);
  53. Assert.Fail (id + "-3" + failStr);
  54. }
  55. }
  56. public void AssertEquals (string message, int expected, int actual)
  57. {
  58. Assert.AreEqual (expected, actual, message);
  59. }
  60. public void AssertEquals (string message, long expected, long actual)
  61. {
  62. Assert.AreEqual (expected, actual, message);
  63. }
  64. public void AssertEquals (string message, bool expected, bool actual)
  65. {
  66. Assert.AreEqual (expected, actual, message);
  67. }
  68. [Test]
  69. public void ConstructorsOne ()
  70. {
  71. MemoryStream ms = new MemoryStream();
  72. AssertEquals ("#01", 0L, ms.Length);
  73. AssertEquals ("#02", 0, ms.Capacity);
  74. AssertEquals ("#03", true, ms.CanWrite);
  75. }
  76. [Test]
  77. public void ConstructorsTwo ()
  78. {
  79. MemoryStream ms = new MemoryStream (10);
  80. AssertEquals ("#01", 0L, ms.Length);
  81. AssertEquals ("#02", 10, ms.Capacity);
  82. ms.Capacity = 0;
  83. byte [] buffer = ms.GetBuffer ();
  84. // Begin: wow!!!
  85. AssertEquals ("#03", -1, ms.ReadByte ());
  86. Assert.IsNull (buffer, "#04"); // <--
  87. ms.Read (new byte [5], 0, 5);
  88. AssertEquals ("#05", 0, ms.Position);
  89. AssertEquals ("#06", 0, ms.Length);
  90. // End
  91. }
  92. [Test]
  93. public void ConstructorsThree ()
  94. {
  95. MemoryStream ms = new MemoryStream (testStreamData);
  96. AssertEquals ("#01", 100, ms.Length);
  97. AssertEquals ("#02", 0, ms.Position);
  98. }
  99. [Test]
  100. public void ConstructorsFour ()
  101. {
  102. MemoryStream ms = new MemoryStream (testStreamData, true);
  103. AssertEquals ("#01", 100, ms.Length);
  104. AssertEquals ("#02", 0, ms.Position);
  105. ms.Position = 50;
  106. byte saved = testStreamData [50];
  107. try {
  108. ms.WriteByte (23);
  109. AssertEquals ("#03", testStreamData [50], 23);
  110. } finally {
  111. testStreamData [50] = saved;
  112. }
  113. ms.Position = 100;
  114. try {
  115. ms.WriteByte (23);
  116. } catch (Exception) {
  117. return;
  118. }
  119. Assert.Fail ("#04");
  120. }
  121. [Test]
  122. public void ConstructorsFive ()
  123. {
  124. MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
  125. AssertEquals ("#01", 50, ms.Length);
  126. AssertEquals ("#02", 0, ms.Position);
  127. AssertEquals ("#03", 50, ms.Capacity);
  128. ms.Position = 1;
  129. byte saved = testStreamData [51];
  130. try {
  131. ms.WriteByte (23);
  132. AssertEquals ("#04", testStreamData [51], 23);
  133. } finally {
  134. testStreamData [51] = saved;
  135. }
  136. ms.Position = 100;
  137. bool gotException = false;
  138. try {
  139. ms.WriteByte (23);
  140. } catch (NotSupportedException) {
  141. gotException = true;
  142. }
  143. if (!gotException)
  144. Assert.Fail ("#05");
  145. ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
  146. // This is lame, as the length is 50!!!
  147. gotException = false;
  148. try {
  149. ms.Capacity = 51;
  150. } catch (NotSupportedException) {
  151. gotException = true;
  152. }
  153. if (!gotException)
  154. Assert.Fail ("#07");
  155. AssertEquals ("#08", 50, ms.ToArray ().Length);
  156. }
  157. [Test]
  158. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  159. public void ConstructorsSix ()
  160. {
  161. MemoryStream ms = new MemoryStream (-2);
  162. }
  163. [Test]
  164. public void Read ()
  165. {
  166. byte [] readBytes = new byte [20];
  167. /* Test simple read */
  168. testStream.Read (readBytes, 0, 10);
  169. VerifyTestData ("R1", readBytes, 0, 10);
  170. /* Seek back to beginning */
  171. testStream.Seek (0, SeekOrigin.Begin);
  172. /* Read again, bit more this time */
  173. testStream.Read (readBytes, 0, 20);
  174. VerifyTestData ("R2", readBytes, 0, 20);
  175. /* Seek to 20 bytes from End */
  176. testStream.Seek (-20, SeekOrigin.End);
  177. testStream.Read (readBytes, 0, 20);
  178. VerifyTestData ("R3", readBytes, 80, 20);
  179. int readByte = testStream.ReadByte();
  180. Assert.AreEqual (-1, readByte, "R4");
  181. }
  182. [Test]
  183. public void WriteBytes ()
  184. {
  185. byte[] readBytes = new byte[100];
  186. MemoryStream ms = new MemoryStream (100);
  187. for (int i = 0; i < 100; i++)
  188. ms.WriteByte (testStreamData [i]);
  189. ms.Seek (0, SeekOrigin.Begin);
  190. testStream.Read (readBytes, 0, 100);
  191. VerifyTestData ("W1", readBytes, 0, 100);
  192. }
  193. [Test]
  194. public void WriteBlock ()
  195. {
  196. byte[] readBytes = new byte[100];
  197. MemoryStream ms = new MemoryStream (100);
  198. ms.Write (testStreamData, 0, 100);
  199. ms.Seek (0, SeekOrigin.Begin);
  200. testStream.Read (readBytes, 0, 100);
  201. VerifyTestData ("WB1", readBytes, 0, 100);
  202. byte[] arrayBytes = testStream.ToArray();
  203. AssertEquals ("#01", 100, arrayBytes.Length);
  204. VerifyTestData ("WB2", arrayBytes, 0, 100);
  205. }
  206. [Test]
  207. public void PositionLength ()
  208. {
  209. MemoryStream ms = new MemoryStream ();
  210. ms.Position = 4;
  211. ms.WriteByte ((byte) 'M');
  212. ms.WriteByte ((byte) 'O');
  213. AssertEquals ("#01", 6, ms.Length);
  214. AssertEquals ("#02", 6, ms.Position);
  215. ms.Position = 0;
  216. AssertEquals ("#03", 0, ms.Position);
  217. }
  218. [Test]
  219. [ExpectedException (typeof (NotSupportedException))]
  220. public void MorePositionLength ()
  221. {
  222. MemoryStream ms = new MemoryStream (testStreamData);
  223. ms.Position = 101;
  224. AssertEquals ("#01", 101, ms.Position);
  225. AssertEquals ("#02", 100, ms.Length);
  226. ms.WriteByte (1); // This should throw the exception
  227. }
  228. [Test]
  229. public void GetBufferOne ()
  230. {
  231. MemoryStream ms = new MemoryStream ();
  232. byte [] buffer = ms.GetBuffer ();
  233. AssertEquals ("#01", 0, buffer.Length);
  234. }
  235. [Test]
  236. public void GetBufferTwo ()
  237. {
  238. MemoryStream ms = new MemoryStream (100);
  239. byte [] buffer = ms.GetBuffer ();
  240. AssertEquals ("#01", 100, buffer.Length);
  241. ms.Write (testStreamData, 0, 100);
  242. ms.Write (testStreamData, 0, 100);
  243. AssertEquals ("#02", 200, ms.Length);
  244. buffer = ms.GetBuffer ();
  245. AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
  246. }
  247. [Test]
  248. public void Closed ()
  249. {
  250. MemoryStream ms = new MemoryStream (100);
  251. ms.Close ();
  252. bool thrown = false;
  253. try {
  254. int x = ms.Capacity;
  255. } catch (ObjectDisposedException) {
  256. thrown = true;
  257. }
  258. if (!thrown)
  259. Assert.Fail ("#01");
  260. thrown = false;
  261. try {
  262. ms.Capacity = 1;
  263. } catch (ObjectDisposedException) {
  264. thrown = true;
  265. }
  266. if (!thrown)
  267. Assert.Fail ("#02");
  268. // The first exception thrown is ObjectDisposed, not ArgumentNull
  269. thrown = false;
  270. try {
  271. ms.Read (null, 0, 1);
  272. } catch (ObjectDisposedException) {
  273. thrown = true;
  274. }
  275. if (!thrown)
  276. Assert.Fail ("#03");
  277. thrown = false;
  278. try {
  279. ms.Write (null, 0, 1);
  280. } catch (ObjectDisposedException) {
  281. thrown = true;
  282. }
  283. if (!thrown)
  284. Assert.Fail ("#03");
  285. }
  286. [Test]
  287. [ExpectedException (typeof (ObjectDisposedException))]
  288. public void Close_get_Length ()
  289. {
  290. MemoryStream ms = new MemoryStream (100);
  291. ms.Close ();
  292. long x = ms.Length;
  293. }
  294. [Test]
  295. [ExpectedException (typeof (ObjectDisposedException))]
  296. public void Close_get_Position ()
  297. {
  298. MemoryStream ms = new MemoryStream (100);
  299. ms.Close ();
  300. long x = ms.Position;
  301. }
  302. [Test]
  303. [ExpectedException (typeof (ObjectDisposedException))]
  304. public void Close_set_Position ()
  305. {
  306. MemoryStream ms = new MemoryStream (100);
  307. ms.Close ();
  308. ms.Position = 0;
  309. }
  310. [Test]
  311. public void Seek ()
  312. {
  313. MemoryStream ms = new MemoryStream (100);
  314. ms.Write (testStreamData, 0, 100);
  315. ms.Seek (0, SeekOrigin.Begin);
  316. ms.Position = 50;
  317. ms.Seek (-50, SeekOrigin.Current);
  318. ms.Seek (-50, SeekOrigin.End);
  319. bool thrown = false;
  320. ms.Position = 49;
  321. try {
  322. ms.Seek (-50, SeekOrigin.Current);
  323. } catch (IOException) {
  324. thrown = true;
  325. }
  326. if (!thrown)
  327. Assert.Fail ("#01");
  328. thrown = false;
  329. try {
  330. ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
  331. } catch (ArgumentOutOfRangeException) {
  332. thrown = true;
  333. }
  334. if (!thrown)
  335. Assert.Fail ("#02");
  336. thrown=false;
  337. try {
  338. // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
  339. ms.Seek (Int64.MinValue, SeekOrigin.Begin);
  340. } catch (IOException) {
  341. thrown = true;
  342. }
  343. if (!thrown)
  344. Assert.Fail ("#03");
  345. ms=new MemoryStream (256);
  346. ms.Write (testStreamData, 0, 100);
  347. ms.Position=0;
  348. AssertEquals ("#01", 100, ms.Length);
  349. AssertEquals ("#02", 0, ms.Position);
  350. ms.Position=128;
  351. AssertEquals ("#03", 100, ms.Length);
  352. AssertEquals ("#04", 128, ms.Position);
  353. ms.Position=768;
  354. AssertEquals ("#05", 100, ms.Length);
  355. AssertEquals ("#06", 768, ms.Position);
  356. ms.WriteByte (0);
  357. AssertEquals ("#07", 769, ms.Length);
  358. AssertEquals ("#08", 769, ms.Position);
  359. }
  360. [Test]
  361. [ExpectedException (typeof (ObjectDisposedException))]
  362. public void Seek_Disposed ()
  363. {
  364. MemoryStream ms = new MemoryStream ();
  365. ms.Close ();
  366. ms.Seek (0, SeekOrigin.Begin);
  367. }
  368. [Test]
  369. public void SetLength ()
  370. {
  371. MemoryStream ms = new MemoryStream ();
  372. ms.Write (testStreamData, 0, 100);
  373. ms.Position = 100;
  374. ms.SetLength (150);
  375. AssertEquals ("#01", 150, ms.Length);
  376. AssertEquals ("#02", 100, ms.Position);
  377. ms.SetLength (80);
  378. AssertEquals ("#03", 80, ms.Length);
  379. AssertEquals ("#04", 80, ms.Position);
  380. }
  381. [Test]
  382. [ExpectedException (typeof (NotSupportedException))]
  383. public void SetLength_ReadOnly ()
  384. {
  385. MemoryStream ms = new MemoryStream (testStreamData, false);
  386. ms.SetLength (10);
  387. }
  388. [Test]
  389. [ExpectedException (typeof (NotSupportedException))]
  390. public void WriteNonWritable ()
  391. {
  392. MemoryStream ms = new MemoryStream (testStreamData, false);
  393. ms.Write (testStreamData, 0, 100);
  394. }
  395. [Test]
  396. [ExpectedException (typeof (NotSupportedException))]
  397. public void WriteExpand ()
  398. {
  399. MemoryStream ms = new MemoryStream (testStreamData);
  400. ms.Write (testStreamData, 0, 100);
  401. ms.Write (testStreamData, 0, 100); // This one throws the exception
  402. }
  403. [Test]
  404. public void WriteByte ()
  405. {
  406. MemoryStream ms = new MemoryStream (100);
  407. ms.Write (testStreamData, 0, 100);
  408. ms.Position = 100;
  409. ms.WriteByte (101);
  410. AssertEquals ("#01", 101, ms.Position);
  411. AssertEquals ("#02", 101, ms.Length);
  412. AssertEquals ("#03", 256, ms.Capacity);
  413. ms.Write (testStreamData, 0, 100);
  414. ms.Write (testStreamData, 0, 100);
  415. // 301
  416. AssertEquals ("#04", 301, ms.Position);
  417. AssertEquals ("#05", 301, ms.Length);
  418. AssertEquals ("#06", 512, ms.Capacity);
  419. }
  420. [Test]
  421. public void WriteLengths () {
  422. MemoryStream ms=new MemoryStream (256);
  423. BinaryWriter writer=new BinaryWriter (ms);
  424. writer.Write ((byte)'1');
  425. AssertEquals ("#01", 1, ms.Length);
  426. AssertEquals ("#02", 256, ms.Capacity);
  427. writer.Write ((ushort)0);
  428. AssertEquals ("#03", 3, ms.Length);
  429. AssertEquals ("#04", 256, ms.Capacity);
  430. writer.Write (testStreamData, 0, 23);
  431. AssertEquals ("#05", 26, ms.Length);
  432. AssertEquals ("#06", 256, ms.Capacity);
  433. writer.Write (testStreamData);
  434. writer.Write (testStreamData);
  435. writer.Write (testStreamData);
  436. AssertEquals ("#07", 326, ms.Length);
  437. }
  438. [Test]
  439. public void MoreWriteByte ()
  440. {
  441. byte[] buffer = new byte [44];
  442. MemoryStream ms = new MemoryStream (buffer);
  443. BinaryWriter bw = new BinaryWriter (ms);
  444. for(int i=0; i < 44; i++)
  445. bw.Write ((byte) 1);
  446. }
  447. [Test]
  448. [ExpectedException (typeof (NotSupportedException))]
  449. public void MoreWriteByte2 ()
  450. {
  451. byte[] buffer = new byte [43]; // Note the 43 here
  452. MemoryStream ms = new MemoryStream (buffer);
  453. BinaryWriter bw = new BinaryWriter (ms);
  454. for(int i=0; i < 44; i++)
  455. bw.Write ((byte) 1);
  456. }
  457. [Test]
  458. public void Expand ()
  459. {
  460. byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
  461. MemoryStream ms = new MemoryStream ();
  462. ms.Write (array, 0, array.Length);
  463. ms.SetLength (4);
  464. ms.Seek (4, SeekOrigin.End);
  465. ms.WriteByte (0xFF);
  466. Assert.AreEqual ("01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()), "Result");
  467. }
  468. [Test]
  469. public void PubliclyVisible ()
  470. {
  471. MemoryStream ms = new MemoryStream ();
  472. Assert.IsNotNull (ms.GetBuffer (), "ctor()");
  473. ms = new MemoryStream (1);
  474. Assert.IsNotNull (ms.GetBuffer (), "ctor(1)");
  475. ms = new MemoryStream (new byte[1], 0, 1, true, true);
  476. Assert.IsNotNull (ms.GetBuffer (), "ctor(byte[],int,int,bool,bool");
  477. }
  478. [Test]
  479. [ExpectedException (typeof (UnauthorizedAccessException))]
  480. public void PubliclyVisible_Ctor_ByteArray ()
  481. {
  482. MemoryStream ms = new MemoryStream (new byte[0]);
  483. Assert.IsNotNull (ms.GetBuffer ());
  484. }
  485. [Test]
  486. [ExpectedException (typeof (UnauthorizedAccessException))]
  487. public void PubliclyVisible_Ctor_ByteArray_Boolean ()
  488. {
  489. MemoryStream ms = new MemoryStream (new byte[0], true);
  490. Assert.IsNotNull (ms.GetBuffer ());
  491. }
  492. [Test]
  493. [ExpectedException (typeof (UnauthorizedAccessException))]
  494. public void PubliclyVisible_Ctor_ByteArray_Int_Int ()
  495. {
  496. MemoryStream ms = new MemoryStream (new byte[1], 0, 1);
  497. Assert.IsNotNull (ms.GetBuffer ());
  498. }
  499. [Test]
  500. [ExpectedException (typeof (UnauthorizedAccessException))]
  501. public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean ()
  502. {
  503. MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true);
  504. Assert.IsNotNull (ms.GetBuffer ());
  505. }
  506. [Test]
  507. [ExpectedException (typeof (UnauthorizedAccessException))]
  508. public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean_Boolean ()
  509. {
  510. MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
  511. Assert.IsNotNull (ms.GetBuffer ());
  512. }
  513. }
  514. }