2 // System.IO.MemoryStreamTest
5 // Marcin Szczepanski (marcins@zipworld.com.au)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004 Novell (http://www.novell.com)
15 using System.Runtime.Serialization.Formatters.Binary;
18 using NUnit.Framework;
20 namespace MonoTests.System.IO
23 public class MemoryStreamTest
25 MemoryStream testStream;
26 byte [] testStreamData;
31 testStreamData = new byte [100];
33 for (int i = 0; i < 100; i++)
34 testStreamData[i] = (byte) (100 - i);
36 testStream = new MemoryStream (testStreamData);
40 // Verify that the first count bytes in testBytes are the same as
41 // the count bytes from index start in testStreamData
43 void VerifyTestData (string id, byte [] testBytes, int start, int count)
45 if (testBytes == null)
46 Assert.Fail (id + "+1 testBytes is null");
50 start + count > testStreamData.Length ||
51 start > testStreamData.Length)
52 throw new ArgumentOutOfRangeException (id + "+2");
54 for (int test = 0; test < count; test++) {
55 if (testBytes [test] == testStreamData [start + test])
58 string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
59 failStr = String.Format (failStr,
63 testStreamData [start + test]);
64 Assert.Fail (id + "-3" + failStr);
68 public void AssertEquals (string message, int expected, int actual)
70 Assert.AreEqual (expected, actual, message);
73 public void AssertEquals (string message, long expected, long actual)
75 Assert.AreEqual (expected, actual, message);
78 public void AssertEquals (string message, bool expected, bool actual)
80 Assert.AreEqual (expected, actual, message);
84 public void ConstructorsOne ()
86 MemoryStream ms = new MemoryStream();
88 AssertEquals ("#01", 0L, ms.Length);
89 AssertEquals ("#02", 0, ms.Capacity);
90 AssertEquals ("#03", true, ms.CanWrite);
94 public void ConstructorsTwo ()
96 MemoryStream ms = new MemoryStream (10);
98 AssertEquals ("#01", 0L, ms.Length);
99 AssertEquals ("#02", 10, ms.Capacity);
101 byte [] buffer = ms.GetBuffer ();
103 AssertEquals ("#03", -1, ms.ReadByte ());
104 Assert.IsNull (buffer, "#04"); // <--
105 ms.Read (new byte [5], 0, 5);
106 AssertEquals ("#05", 0, ms.Position);
107 AssertEquals ("#06", 0, ms.Length);
112 public void ConstructorsThree ()
114 MemoryStream ms = new MemoryStream (testStreamData);
115 AssertEquals ("#01", 100, ms.Length);
116 AssertEquals ("#02", 0, ms.Position);
120 public void ConstructorsFour ()
122 MemoryStream ms = new MemoryStream (testStreamData, true);
123 AssertEquals ("#01", 100, ms.Length);
124 AssertEquals ("#02", 0, ms.Position);
126 byte saved = testStreamData [50];
129 AssertEquals ("#03", testStreamData [50], 23);
131 testStreamData [50] = saved;
136 } catch (Exception) {
143 public void ConstructorsFive ()
145 MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
146 AssertEquals ("#01", 50, ms.Length);
147 AssertEquals ("#02", 0, ms.Position);
148 AssertEquals ("#03", 50, ms.Capacity);
150 byte saved = testStreamData [51];
153 AssertEquals ("#04", testStreamData [51], 23);
155 testStreamData [51] = saved;
158 bool gotException = false;
161 } catch (NotSupportedException) {
168 ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
169 // This is lame, as the length is 50!!!
171 gotException = false;
174 } catch (NotSupportedException) {
181 AssertEquals ("#08", 50, ms.ToArray ().Length);
185 [ExpectedException (typeof (ArgumentOutOfRangeException))]
186 public void ConstructorsSix ()
188 MemoryStream ms = new MemoryStream (-2);
194 byte [] readBytes = new byte [20];
196 /* Test simple read */
197 testStream.Read (readBytes, 0, 10);
198 VerifyTestData ("R1", readBytes, 0, 10);
200 /* Seek back to beginning */
202 testStream.Seek (0, SeekOrigin.Begin);
204 /* Read again, bit more this time */
205 testStream.Read (readBytes, 0, 20);
206 VerifyTestData ("R2", readBytes, 0, 20);
208 /* Seek to 20 bytes from End */
209 testStream.Seek (-20, SeekOrigin.End);
210 testStream.Read (readBytes, 0, 20);
211 VerifyTestData ("R3", readBytes, 80, 20);
213 int readByte = testStream.ReadByte();
214 Assert.AreEqual (-1, readByte, "R4");
218 public void WriteBytes ()
220 byte[] readBytes = new byte[100];
222 MemoryStream ms = new MemoryStream (100);
224 for (int i = 0; i < 100; i++)
225 ms.WriteByte (testStreamData [i]);
227 ms.Seek (0, SeekOrigin.Begin);
228 testStream.Read (readBytes, 0, 100);
229 VerifyTestData ("W1", readBytes, 0, 100);
233 public void WriteBlock ()
235 byte[] readBytes = new byte[100];
237 MemoryStream ms = new MemoryStream (100);
239 ms.Write (testStreamData, 0, 100);
240 ms.Seek (0, SeekOrigin.Begin);
241 testStream.Read (readBytes, 0, 100);
242 VerifyTestData ("WB1", readBytes, 0, 100);
243 byte[] arrayBytes = testStream.ToArray();
244 AssertEquals ("#01", 100, arrayBytes.Length);
245 VerifyTestData ("WB2", arrayBytes, 0, 100);
249 public void PositionLength ()
251 MemoryStream ms = new MemoryStream ();
253 ms.WriteByte ((byte) 'M');
254 ms.WriteByte ((byte) 'O');
255 AssertEquals ("#01", 6, ms.Length);
256 AssertEquals ("#02", 6, ms.Position);
258 AssertEquals ("#03", 0, ms.Position);
262 [ExpectedException (typeof (NotSupportedException))]
263 public void MorePositionLength ()
265 MemoryStream ms = new MemoryStream (testStreamData);
267 AssertEquals ("#01", 101, ms.Position);
268 AssertEquals ("#02", 100, ms.Length);
269 ms.WriteByte (1); // This should throw the exception
273 public void GetBufferOne ()
275 MemoryStream ms = new MemoryStream ();
276 byte [] buffer = ms.GetBuffer ();
277 AssertEquals ("#01", 0, buffer.Length);
281 public void GetBufferTwo ()
283 MemoryStream ms = new MemoryStream (100);
284 byte [] buffer = ms.GetBuffer ();
285 AssertEquals ("#01", 100, buffer.Length);
287 ms.Write (testStreamData, 0, 100);
288 ms.Write (testStreamData, 0, 100);
289 AssertEquals ("#02", 200, ms.Length);
290 buffer = ms.GetBuffer ();
291 AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
295 public void Closed ()
297 MemoryStream ms = new MemoryStream (100);
302 } catch (ObjectDisposedException) {
312 } catch (ObjectDisposedException) {
319 // The first exception thrown is ObjectDisposed, not ArgumentNull
322 ms.Read (null, 0, 1);
323 } catch (ObjectDisposedException) {
332 ms.Write (null, 0, 1);
333 } catch (ObjectDisposedException) {
342 [ExpectedException (typeof (ObjectDisposedException))]
343 public void Close_get_Length ()
345 MemoryStream ms = new MemoryStream (100);
351 [ExpectedException (typeof (ObjectDisposedException))]
352 public void Close_get_Position ()
354 MemoryStream ms = new MemoryStream (100);
356 long x = ms.Position;
360 [ExpectedException (typeof (ObjectDisposedException))]
361 public void Close_set_Position ()
363 MemoryStream ms = new MemoryStream (100);
371 MemoryStream ms = new MemoryStream (100);
372 ms.Write (testStreamData, 0, 100);
373 ms.Seek (0, SeekOrigin.Begin);
375 ms.Seek (-50, SeekOrigin.Current);
376 ms.Seek (-50, SeekOrigin.End);
381 ms.Seek (-50, SeekOrigin.Current);
382 } catch (IOException) {
390 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
391 } catch (ArgumentOutOfRangeException) {
400 // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
401 ms.Seek (Int64.MinValue, SeekOrigin.Begin);
402 } catch (IOException) {
409 ms=new MemoryStream (256);
411 ms.Write (testStreamData, 0, 100);
413 AssertEquals ("#01", 100, ms.Length);
414 AssertEquals ("#02", 0, ms.Position);
417 AssertEquals ("#03", 100, ms.Length);
418 AssertEquals ("#04", 128, ms.Position);
421 AssertEquals ("#05", 100, ms.Length);
422 AssertEquals ("#06", 768, ms.Position);
425 AssertEquals ("#07", 769, ms.Length);
426 AssertEquals ("#08", 769, ms.Position);
430 public void Seek_Disposed ()
432 MemoryStream ms = new MemoryStream ();
435 ms.Seek (0, SeekOrigin.Begin);
437 } catch (ObjectDisposedException) {
442 public void SetLength ()
444 MemoryStream ms = new MemoryStream ();
445 ms.Write (testStreamData, 0, 100);
448 AssertEquals ("#01", 150, ms.Length);
449 AssertEquals ("#02", 100, ms.Position);
451 AssertEquals ("#03", 80, ms.Length);
452 AssertEquals ("#04", 80, ms.Position);
456 [ExpectedException (typeof (NotSupportedException))]
457 public void SetLength_ReadOnly ()
459 MemoryStream ms = new MemoryStream (testStreamData, false);
463 [Test] // bug #327053
464 [Category ("NotWorking")]
465 public void ZeroingOnExpand ()
467 byte [] values = { 3, 2, 1 };
468 byte [] reference = { 3, 2, 1 };
469 byte [] cropped = { 3, 0, 0 };
470 MemoryStream ms = new MemoryStream (values);
471 Assert.AreEqual (values, reference, "#A1");
472 ms.Seek (3, SeekOrigin.Begin);
473 Assert.AreEqual (reference, values, "#A2");
475 Assert.AreEqual (reference, values, "#B1");
476 byte [] read = new byte [5];
477 ms.Read (read, 0, 5);
478 Assert.AreEqual (new byte [] { 0, 0, 0, 0, 0 }, read, "#B2");
479 Assert.AreEqual (reference, values, "#B3");
481 Assert.AreEqual (cropped, values, "#C1");
482 ms.Seek (0, SeekOrigin.Begin);
484 ms.Read (read, 0, 3);
485 Assert.AreEqual (cropped, read, "#C2");
486 Assert.AreEqual (cropped, values, "#C3");
490 [ExpectedException (typeof (NotSupportedException))]
491 public void WriteNonWritable ()
493 MemoryStream ms = new MemoryStream (testStreamData, false);
494 ms.Write (testStreamData, 0, 100);
498 [ExpectedException (typeof (NotSupportedException))]
499 public void WriteExpand ()
501 MemoryStream ms = new MemoryStream (testStreamData);
502 ms.Write (testStreamData, 0, 100);
503 ms.Write (testStreamData, 0, 100); // This one throws the exception
507 public void WriteByte ()
509 MemoryStream ms = new MemoryStream (100);
510 ms.Write (testStreamData, 0, 100);
513 AssertEquals ("#01", 101, ms.Position);
514 AssertEquals ("#02", 101, ms.Length);
515 AssertEquals ("#03", 256, ms.Capacity);
516 ms.Write (testStreamData, 0, 100);
517 ms.Write (testStreamData, 0, 100);
519 AssertEquals ("#04", 301, ms.Position);
520 AssertEquals ("#05", 301, ms.Length);
521 AssertEquals ("#06", 512, ms.Capacity);
525 public void WriteLengths () {
526 MemoryStream ms=new MemoryStream (256);
527 BinaryWriter writer=new BinaryWriter (ms);
529 writer.Write ((byte)'1');
530 AssertEquals ("#01", 1, ms.Length);
531 AssertEquals ("#02", 256, ms.Capacity);
533 writer.Write ((ushort)0);
534 AssertEquals ("#03", 3, ms.Length);
535 AssertEquals ("#04", 256, ms.Capacity);
537 writer.Write (testStreamData, 0, 23);
538 AssertEquals ("#05", 26, ms.Length);
539 AssertEquals ("#06", 256, ms.Capacity);
541 writer.Write (testStreamData);
542 writer.Write (testStreamData);
543 writer.Write (testStreamData);
544 AssertEquals ("#07", 326, ms.Length);
548 public void MoreWriteByte ()
550 byte[] buffer = new byte [44];
552 MemoryStream ms = new MemoryStream (buffer);
553 BinaryWriter bw = new BinaryWriter (ms);
554 for(int i=0; i < 44; i++)
559 [ExpectedException (typeof (NotSupportedException))]
560 public void MoreWriteByte2 ()
562 byte[] buffer = new byte [43]; // Note the 43 here
564 MemoryStream ms = new MemoryStream (buffer);
565 BinaryWriter bw = new BinaryWriter (ms);
566 for(int i=0; i < 44; i++)
571 public void Expand ()
573 byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
574 MemoryStream ms = new MemoryStream ();
575 ms.Write (array, 0, array.Length);
577 ms.Seek (4, SeekOrigin.End);
579 Assert.AreEqual ("01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()), "Result");
583 public void PubliclyVisible ()
585 MemoryStream ms = new MemoryStream ();
586 Assert.IsNotNull (ms.GetBuffer (), "ctor()");
588 ms = new MemoryStream (1);
589 Assert.IsNotNull (ms.GetBuffer (), "ctor(1)");
591 ms = new MemoryStream (new byte[1], 0, 1, true, true);
592 Assert.IsNotNull (ms.GetBuffer (), "ctor(byte[],int,int,bool,bool");
596 [ExpectedException (typeof (UnauthorizedAccessException))]
597 public void PubliclyVisible_Ctor_ByteArray ()
599 MemoryStream ms = new MemoryStream (new byte[0]);
600 Assert.IsNotNull (ms.GetBuffer ());
604 [ExpectedException (typeof (UnauthorizedAccessException))]
605 public void PubliclyVisible_Ctor_ByteArray_Boolean ()
607 MemoryStream ms = new MemoryStream (new byte[0], true);
608 Assert.IsNotNull (ms.GetBuffer ());
612 [ExpectedException (typeof (UnauthorizedAccessException))]
613 public void PubliclyVisible_Ctor_ByteArray_Int_Int ()
615 MemoryStream ms = new MemoryStream (new byte[1], 0, 1);
616 Assert.IsNotNull (ms.GetBuffer ());
620 [ExpectedException (typeof (UnauthorizedAccessException))]
621 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean ()
623 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true);
624 Assert.IsNotNull (ms.GetBuffer ());
628 [ExpectedException (typeof (UnauthorizedAccessException))]
629 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean_Boolean ()
631 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
632 Assert.IsNotNull (ms.GetBuffer ());
636 [Category ("NotWorking")]
637 public void SerializeTest ()
639 MemoryStream input = new MemoryStream ();
640 byte [] bufferIn = Encoding.UTF8.GetBytes ("some test");
641 input.Write (bufferIn, 0, bufferIn.Length);
644 BinaryFormatter bf = new BinaryFormatter ();
645 MemoryStream ms = new MemoryStream ();
646 bf.Serialize (ms, input);
648 byte [] bufferOut = new byte [ms.Length];
650 ms.Read (bufferOut, 0, bufferOut.Length);
652 Assert.AreEqual (_serialized, bufferOut);
656 [Category ("NotWorking")]
657 public void DeserializeTest ()
659 MemoryStream ms = new MemoryStream ();
660 ms.Write (_serialized, 0, _serialized.Length);
663 BinaryFormatter bf = new BinaryFormatter ();
664 MemoryStream output = (MemoryStream) bf.Deserialize (ms);
665 using (StreamReader sr = new StreamReader (output)) {
666 Assert.AreEqual ("some test", sr.ReadToEnd ());
670 private static byte [] _serialized = new byte [] {
671 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
672 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
673 0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f, 0x2e,
674 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x72, 0x65, 0x61,
675 0x6d, 0x0a, 0x00, 0x00, 0x00, 0x07, 0x5f, 0x62, 0x75, 0x66, 0x66,
676 0x65, 0x72, 0x07, 0x5f, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x09,
677 0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x07, 0x5f,
678 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x09, 0x5f, 0x63, 0x61, 0x70,
679 0x61, 0x63, 0x69, 0x74, 0x79, 0x0b, 0x5f, 0x65, 0x78, 0x70, 0x61,
680 0x6e, 0x64, 0x61, 0x62, 0x6c, 0x65, 0x09, 0x5f, 0x77, 0x72, 0x69,
681 0x74, 0x61, 0x62, 0x6c, 0x65, 0x0a, 0x5f, 0x65, 0x78, 0x70, 0x6f,
682 0x73, 0x61, 0x62, 0x6c, 0x65, 0x07, 0x5f, 0x69, 0x73, 0x4f, 0x70,
683 0x65, 0x6e, 0x1d, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x42,
684 0x79, 0x52, 0x65, 0x66, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x2b,
685 0x5f, 0x5f, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x07,
686 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08,
687 0x08, 0x08, 0x08, 0x01, 0x01, 0x01, 0x01, 0x09, 0x02, 0x00, 0x00,
688 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
689 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x0a,
690 0x0f, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x73,
691 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x00, 0x00, 0x00,
692 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
693 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
694 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
695 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
696 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
697 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
698 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
699 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
700 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
701 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
702 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
703 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
704 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
705 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
706 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
707 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
708 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
709 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
710 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
711 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
712 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
713 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
716 class MyMemoryStream : MemoryStream {
718 public bool DisposedCalled = false;
720 protected override void Dispose(bool disposing)
722 DisposedCalled = true;
726 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=322672
727 public void BaseDisposeCalled ()
729 MyMemoryStream ms = new MyMemoryStream ();
730 Assert.IsFalse (ms.DisposedCalled, "Before");
732 Assert.IsTrue (ms.DisposedCalled, "After");