2 // System.IO.MemoryStreamTest
5 // Marcin Szczepanski (marcins@zipworld.com.au)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Sebastien Pouliot <sebastien@ximian.com>
8 // Marek Safar (marek.safar@gmail.com)
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
11 // Copyright (C) 2004 Novell (http://www.novell.com)
12 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
17 using System.Runtime.Serialization.Formatters.Binary;
19 using System.Threading;
21 using System.Threading.Tasks;
24 using NUnit.Framework;
26 namespace MonoTests.System.IO
29 public class MemoryStreamTest
31 class SignaledMemoryStream : MemoryStream
35 public SignaledMemoryStream (byte[] buffer, WaitHandle w)
41 public override int Read (byte[] buffer, int offset, int count)
43 if (!w.WaitOne (2000))
46 Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread, "IsThreadPoolThread");
47 return base.Read (buffer, offset, count);
51 class ExceptionalStream : MemoryStream
53 public static string Message = "ExceptionalMessage";
54 public bool Throw = false;
56 public ExceptionalStream ()
62 public ExceptionalStream (byte [] buffer, bool writable) : base (buffer, writable)
65 AllowWrite = true; // we are testing the inherited write property
69 public override int Read(byte[] buffer, int offset, int count)
72 throw new Exception(Message);
74 return base.Read(buffer, offset, count);
77 public override void Write(byte[] buffer, int offset, int count)
80 throw new Exception(Message);
82 base.Write(buffer, offset, count);
85 public bool AllowRead { get; set; }
86 public override bool CanRead { get { return AllowRead; } }
88 public bool AllowWrite { get; set; }
89 public override bool CanWrite { get { return AllowWrite; } }
91 public override void Flush()
94 throw new Exception(Message);
100 MemoryStream testStream;
101 byte [] testStreamData;
106 testStreamData = new byte [100];
108 for (int i = 0; i < 100; i++)
109 testStreamData[i] = (byte) (100 - i);
111 testStream = new MemoryStream (testStreamData);
115 // Verify that the first count bytes in testBytes are the same as
116 // the count bytes from index start in testStreamData
118 void VerifyTestData (string id, byte [] testBytes, int start, int count)
120 if (testBytes == null)
121 Assert.Fail (id + "+1 testBytes is null");
125 start + count > testStreamData.Length ||
126 start > testStreamData.Length)
127 throw new ArgumentOutOfRangeException (id + "+2");
129 for (int test = 0; test < count; test++) {
130 if (testBytes [test] == testStreamData [start + test])
133 string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
134 failStr = String.Format (failStr,
138 testStreamData [start + test]);
139 Assert.Fail (id + "-3" + failStr);
143 public void AssertEquals (string message, int expected, int actual)
145 Assert.AreEqual (expected, actual, message);
148 public void AssertEquals (string message, long expected, long actual)
150 Assert.AreEqual (expected, actual, message);
153 public void AssertEquals (string message, bool expected, bool actual)
155 Assert.AreEqual (expected, actual, message);
159 public void ConstructorsOne ()
161 MemoryStream ms = new MemoryStream();
163 AssertEquals ("#01", 0L, ms.Length);
164 AssertEquals ("#02", 0, ms.Capacity);
165 AssertEquals ("#03", true, ms.CanWrite);
169 public void ConstructorsTwo ()
171 MemoryStream ms = new MemoryStream (10);
173 AssertEquals ("#01", 0L, ms.Length);
174 AssertEquals ("#02", 10, ms.Capacity);
176 byte [] buffer = ms.GetBuffer ();
178 AssertEquals ("#03", -1, ms.ReadByte ());
179 Assert.IsNull (buffer, "#04"); // <--
180 ms.Read (new byte [5], 0, 5);
181 AssertEquals ("#05", 0, ms.Position);
182 AssertEquals ("#06", 0, ms.Length);
187 public void ConstructorsThree ()
189 MemoryStream ms = new MemoryStream (testStreamData);
190 AssertEquals ("#01", 100, ms.Length);
191 AssertEquals ("#02", 0, ms.Position);
195 public void ConstructorsFour ()
197 MemoryStream ms = new MemoryStream (testStreamData, true);
198 AssertEquals ("#01", 100, ms.Length);
199 AssertEquals ("#02", 0, ms.Position);
201 byte saved = testStreamData [50];
204 AssertEquals ("#03", testStreamData [50], 23);
206 testStreamData [50] = saved;
211 } catch (Exception) {
218 public void ConstructorsFive ()
220 MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
221 AssertEquals ("#01", 50, ms.Length);
222 AssertEquals ("#02", 0, ms.Position);
223 AssertEquals ("#03", 50, ms.Capacity);
225 byte saved = testStreamData [51];
228 AssertEquals ("#04", testStreamData [51], 23);
230 testStreamData [51] = saved;
237 } catch (NotSupportedException) {
243 } catch (NotSupportedException) {
249 } catch (NotSupportedException) {
252 AssertEquals ("#08", 50, ms.ToArray ().Length);
256 [ExpectedException (typeof (ArgumentOutOfRangeException))]
257 public void ConstructorsSix ()
259 MemoryStream ms = new MemoryStream (-2);
265 byte [] readBytes = new byte [20];
267 /* Test simple read */
268 testStream.Read (readBytes, 0, 10);
269 VerifyTestData ("R1", readBytes, 0, 10);
271 /* Seek back to beginning */
273 testStream.Seek (0, SeekOrigin.Begin);
275 /* Read again, bit more this time */
276 testStream.Read (readBytes, 0, 20);
277 VerifyTestData ("R2", readBytes, 0, 20);
279 /* Seek to 20 bytes from End */
280 testStream.Seek (-20, SeekOrigin.End);
281 testStream.Read (readBytes, 0, 20);
282 VerifyTestData ("R3", readBytes, 80, 20);
284 int readByte = testStream.ReadByte();
285 Assert.AreEqual (-1, readByte, "R4");
289 public void BeginRead ()
291 byte [] readBytes = new byte [5];
293 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
294 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
295 Assert.AreEqual (5, testStream.EndRead (res), "#2");
299 public void BeginRead_WithState ()
301 byte [] readBytes = new byte [5];
302 string async_state = null;
303 var wh = new ManualResetEvent (false);
305 var res = testStream.BeginRead (readBytes, 0, 5, l => {
306 async_state = l.AsyncState as string;
310 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
311 Assert.AreEqual ("state", res.AsyncState, "#2");
312 Assert.IsTrue (res.IsCompleted, "#3");
313 Assert.AreEqual (5, testStream.EndRead (res), "#4");
316 Assert.AreEqual ("state", async_state, "#5");
321 public void BeginReadAsync ()
323 byte[] readBytes = new byte[5];
324 var wh = new ManualResetEvent (false);
325 using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
326 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
327 Assert.IsFalse (res.IsCompleted, "#1");
328 Assert.IsFalse (res.CompletedSynchronously, "#2");
330 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
331 Assert.IsTrue (res.IsCompleted, "#4");
332 Assert.AreEqual (5, testStream.EndRead (res), "#5");
339 public void BeginReadIsBlockingNextRead ()
341 byte[] readBytes = new byte[5];
342 byte[] readBytes2 = new byte[3];
343 var wh = new ManualResetEvent (false);
344 var end = new ManualResetEvent (false);
346 using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
347 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
349 bool blocking = true;
350 ThreadPool.QueueUserWorkItem (l => {
351 var res2 = testStream.BeginRead (readBytes2, 0, 3, null, null);
353 Assert.IsTrue (res2.AsyncWaitHandle.WaitOne (2000), "#10");
354 Assert.IsTrue (res2.IsCompleted, "#11");
355 Assert.AreEqual (3, testStream.EndRead (res2), "#12");
356 Assert.AreEqual (95, readBytes2[0], "#13");
360 Assert.IsFalse (res.IsCompleted, "#1");
361 Thread.Sleep (500); // Lame but don't know how to wait for another BeginRead which does not return
362 Assert.IsTrue (blocking, "#2");
365 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
366 Assert.IsTrue (res.IsCompleted, "#4");
367 Assert.AreEqual (5, testStream.EndRead (res), "#5");
368 Assert.IsTrue (end.WaitOne (2000), "#6");
369 Assert.AreEqual (100, readBytes[0], "#7");
374 public void BeginRead_Read ()
376 byte[] readBytes = new byte[5];
377 var wh = new ManualResetEvent (false);
378 using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
379 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
380 Assert.AreEqual (100, testStream.ReadByte (), "#0");
381 Assert.IsFalse (res.IsCompleted, "#1");
382 Assert.IsFalse (res.CompletedSynchronously, "#2");
384 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
385 Assert.IsTrue (res.IsCompleted, "#4");
386 Assert.AreEqual (5, testStream.EndRead (res), "#5");
387 Assert.AreEqual (99, readBytes [0], "#6");
394 public void BeginRead_BeginWrite ()
396 byte[] readBytes = new byte[5];
397 byte[] readBytes2 = new byte[3] { 1, 2, 3 };
398 var wh = new ManualResetEvent (false);
399 var end = new ManualResetEvent (false);
401 using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
402 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
404 bool blocking = true;
405 ThreadPool.QueueUserWorkItem (l => {
406 var res2 = testStream.BeginWrite (readBytes2, 0, 3, null, null);
408 Assert.IsTrue (res2.AsyncWaitHandle.WaitOne (2000), "#10");
409 Assert.IsTrue (res2.IsCompleted, "#11");
410 testStream.EndWrite (res2);
414 Assert.IsFalse (res.IsCompleted, "#1");
415 Thread.Sleep (500); // Lame but don't know how to wait for another BeginWrite which does not return
416 Assert.IsTrue (blocking, "#2");
419 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
420 Assert.IsTrue (res.IsCompleted, "#4");
421 Assert.AreEqual (5, testStream.EndRead (res), "#5");
422 Assert.IsTrue (end.WaitOne (2000), "#6");
427 public void BeginWrite ()
429 var writeBytes = new byte [5] { 2, 3, 4, 10, 12 };
431 var res = testStream.BeginWrite (writeBytes, 0, 5, null, null);
432 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
433 testStream.EndWrite (res);
437 public void BeginWrite_WithState ()
439 var writeBytes = new byte[5] { 2, 3, 4, 10, 12 };
440 string async_state = null;
441 var wh = new ManualResetEvent (false);
443 var res = testStream.BeginWrite (writeBytes, 0, 5, l => {
444 async_state = l.AsyncState as string;
448 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
449 Assert.IsTrue (res.IsCompleted, "#2");
450 Assert.AreEqual ("state", res.AsyncState, "#3");
451 testStream.EndWrite (res);
454 Assert.AreEqual ("state", async_state, "#4");
459 public void EndRead_Twice ()
461 byte[] readBytes = new byte[5];
463 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
464 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
465 Assert.AreEqual (5, testStream.EndRead (res), "#2");
468 testStream.EndRead (res);
470 } catch (ArgumentException) {
476 public void EndRead_Disposed ()
478 byte[] readBytes = new byte[5];
480 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
481 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
482 testStream.Dispose ();
483 Assert.AreEqual (5, testStream.EndRead (res), "#2");
487 public void EndWrite_OnBeginRead ()
489 byte[] readBytes = new byte[5];
491 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
492 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
495 testStream.EndWrite (res);
497 } catch (ArgumentException) {
500 testStream.EndRead (res);
504 public void EndWrite_Twice ()
506 var wBytes = new byte[5];
508 var res = testStream.BeginWrite (wBytes, 0, 5, null, null);
509 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
510 testStream.EndWrite (res);
513 testStream.EndWrite (res);
515 } catch (ArgumentException) {
522 public void WriteBytes ()
524 byte[] readBytes = new byte[100];
526 MemoryStream ms = new MemoryStream (100);
528 for (int i = 0; i < 100; i++)
529 ms.WriteByte (testStreamData [i]);
531 ms.Seek (0, SeekOrigin.Begin);
532 testStream.Read (readBytes, 0, 100);
533 VerifyTestData ("W1", readBytes, 0, 100);
537 public void WriteBlock ()
539 byte[] readBytes = new byte[100];
541 MemoryStream ms = new MemoryStream (100);
543 ms.Write (testStreamData, 0, 100);
544 ms.Seek (0, SeekOrigin.Begin);
545 testStream.Read (readBytes, 0, 100);
546 VerifyTestData ("WB1", readBytes, 0, 100);
547 byte[] arrayBytes = testStream.ToArray();
548 AssertEquals ("#01", 100, arrayBytes.Length);
549 VerifyTestData ("WB2", arrayBytes, 0, 100);
553 public void PositionLength ()
555 MemoryStream ms = new MemoryStream ();
557 ms.WriteByte ((byte) 'M');
558 ms.WriteByte ((byte) 'O');
559 AssertEquals ("#01", 6, ms.Length);
560 AssertEquals ("#02", 6, ms.Position);
562 AssertEquals ("#03", 0, ms.Position);
566 [ExpectedException (typeof (NotSupportedException))]
567 public void MorePositionLength ()
569 MemoryStream ms = new MemoryStream (testStreamData);
571 AssertEquals ("#01", 101, ms.Position);
572 AssertEquals ("#02", 100, ms.Length);
573 ms.WriteByte (1); // This should throw the exception
577 public void GetBufferOne ()
579 MemoryStream ms = new MemoryStream ();
580 byte [] buffer = ms.GetBuffer ();
581 AssertEquals ("#01", 0, buffer.Length);
585 public void GetBufferTwo ()
587 MemoryStream ms = new MemoryStream (100);
588 byte [] buffer = ms.GetBuffer ();
589 AssertEquals ("#01", 100, buffer.Length);
591 ms.Write (testStreamData, 0, 100);
592 ms.Write (testStreamData, 0, 100);
593 AssertEquals ("#02", 200, ms.Length);
594 buffer = ms.GetBuffer ();
595 AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
599 public void Closed ()
601 MemoryStream ms = new MemoryStream (100);
606 } catch (ObjectDisposedException) {
616 } catch (ObjectDisposedException) {
624 ms.Read (null, 0, 1);
626 } catch (ArgumentNullException) {
630 ms.Write (null, 0, 1);
632 } catch (ArgumentNullException) {
638 [ExpectedException (typeof (ObjectDisposedException))]
639 public void Close_get_Length ()
641 MemoryStream ms = new MemoryStream (100);
647 [ExpectedException (typeof (ObjectDisposedException))]
648 public void Close_get_Position ()
650 MemoryStream ms = new MemoryStream (100);
652 long x = ms.Position;
656 [ExpectedException (typeof (ObjectDisposedException))]
657 public void Close_set_Position ()
659 MemoryStream ms = new MemoryStream (100);
667 MemoryStream ms = new MemoryStream (100);
668 ms.Write (testStreamData, 0, 100);
669 ms.Seek (0, SeekOrigin.Begin);
671 ms.Seek (-50, SeekOrigin.Current);
672 ms.Seek (-50, SeekOrigin.End);
677 ms.Seek (-50, SeekOrigin.Current);
678 } catch (IOException) {
686 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
687 } catch (ArgumentOutOfRangeException) {
696 // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
697 ms.Seek (Int64.MinValue, SeekOrigin.Begin);
698 } catch (IOException) {
705 ms=new MemoryStream (256);
707 ms.Write (testStreamData, 0, 100);
709 AssertEquals ("#01", 100, ms.Length);
710 AssertEquals ("#02", 0, ms.Position);
713 AssertEquals ("#03", 100, ms.Length);
714 AssertEquals ("#04", 128, ms.Position);
717 AssertEquals ("#05", 100, ms.Length);
718 AssertEquals ("#06", 768, ms.Position);
721 AssertEquals ("#07", 769, ms.Length);
722 AssertEquals ("#08", 769, ms.Position);
726 public void Seek_Disposed ()
728 MemoryStream ms = new MemoryStream ();
731 ms.Seek (0, SeekOrigin.Begin);
733 } catch (ObjectDisposedException) {
738 public void SetLength ()
740 MemoryStream ms = new MemoryStream ();
741 ms.Write (testStreamData, 0, 100);
744 AssertEquals ("#01", 150, ms.Length);
745 AssertEquals ("#02", 100, ms.Position);
747 AssertEquals ("#03", 80, ms.Length);
748 AssertEquals ("#04", 80, ms.Position);
752 [ExpectedException (typeof (NotSupportedException))]
753 public void SetLength_ReadOnly ()
755 MemoryStream ms = new MemoryStream (testStreamData, false);
760 public void Capacity ()
762 MemoryStream ms = new MemoryStream ();
764 Assert.AreEqual (0, ms.Capacity, "#A1");
765 Assert.AreEqual (0, ms.GetBuffer ().Length, "#A2");
767 ms.WriteByte ((byte)'6');
768 Assert.AreEqual (256, ms.Capacity, "#B1");
769 Assert.AreEqual (256, ms.GetBuffer ().Length, "#B2");
773 Assert.AreEqual (100, ms.Capacity, "#C1");
774 Assert.AreEqual (100, ms.GetBuffer ().Length, "#C2");
778 Assert.AreEqual (120, ms.Capacity, "#D1");
779 Assert.AreEqual (120, ms.GetBuffer ().Length, "#D2");
781 // Grow the buffer, reduce length -so we have a dirty area-
782 // and then we assign capacity to the same. The idea is that we should
783 // avoid creating a new internal buffer it's not needed.
785 ms = new MemoryStream ();
787 byte [] buff = new byte [] { 0x01, 0x02, 0x03, 0x04, 0x05 };
788 ms.Write (buff, 0, buff.Length);
789 Assert.AreEqual (8, ms.Capacity, "#E1");
790 Assert.AreEqual (8, ms.GetBuffer ().Length, "#E2");
792 // Reduce *length*, not capacity
793 byte [] buff_copy = ms.GetBuffer ();
795 Assert.AreEqual (3, ms.Length, "#F1");
796 Assert.AreEqual (true, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#F2");
798 // Set Capacity to the very same value it has now
799 ms.Capacity = ms.Capacity;
800 Assert.AreEqual (true, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#G1"); // keep the same buffer
802 // Finally, growing it discards the prev buff
803 ms.Capacity = ms.Capacity + 1;
804 Assert.AreEqual (false, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#H1");
807 bool AreBuffersEqual (byte [] buff1, byte [] buff2)
809 if ((buff1 == null) != (buff2 == null))
812 if (buff1.Length != buff2.Length)
815 for (int i = 0; i < buff1.Length; i++)
816 if (buff1 [i] != buff2 [i])
822 [Test] // bug #327053
823 public void ZeroingOnExpand ()
825 byte [] values = { 3, 2, 1 };
826 byte [] reference = { 3, 2, 1 };
827 byte [] cropped = { 3, 0, 0 };
828 MemoryStream ms = new MemoryStream (values);
829 Assert.AreEqual (values, reference, "#A1");
830 ms.Seek (3, SeekOrigin.Begin);
831 Assert.AreEqual (reference, values, "#A2");
833 Assert.AreEqual (reference, values, "#B1");
834 byte [] read = new byte [5];
835 ms.Read (read, 0, 5);
836 Assert.AreEqual (new byte [] { 0, 0, 0, 0, 0 }, read, "#B2");
837 Assert.AreEqual (reference, values, "#B3");
839 Assert.AreEqual (cropped, values, "#C1");
840 ms.Seek (0, SeekOrigin.Begin);
842 ms.Read (read, 0, 3);
843 Assert.AreEqual (cropped, read, "#C2");
844 Assert.AreEqual (cropped, values, "#C3");
848 [ExpectedException (typeof (NotSupportedException))]
849 public void WriteNonWritable ()
851 MemoryStream ms = new MemoryStream (testStreamData, false);
852 ms.Write (testStreamData, 0, 100);
856 [ExpectedException (typeof (NotSupportedException))]
857 public void WriteExpand ()
859 MemoryStream ms = new MemoryStream (testStreamData);
860 ms.Write (testStreamData, 0, 100);
861 ms.Write (testStreamData, 0, 100); // This one throws the exception
865 public void WriteByte ()
867 MemoryStream ms = new MemoryStream (100);
868 ms.Write (testStreamData, 0, 100);
871 AssertEquals ("#01", 101, ms.Position);
872 AssertEquals ("#02", 101, ms.Length);
873 AssertEquals ("#03", 256, ms.Capacity);
874 ms.Write (testStreamData, 0, 100);
875 ms.Write (testStreamData, 0, 100);
877 AssertEquals ("#04", 301, ms.Position);
878 AssertEquals ("#05", 301, ms.Length);
879 AssertEquals ("#06", 512, ms.Capacity);
883 public void WriteLengths () {
884 MemoryStream ms=new MemoryStream (256);
885 BinaryWriter writer=new BinaryWriter (ms);
887 writer.Write ((byte)'1');
888 AssertEquals ("#01", 1, ms.Length);
889 AssertEquals ("#02", 256, ms.Capacity);
891 writer.Write ((ushort)0);
892 AssertEquals ("#03", 3, ms.Length);
893 AssertEquals ("#04", 256, ms.Capacity);
895 writer.Write (testStreamData, 0, 23);
896 AssertEquals ("#05", 26, ms.Length);
897 AssertEquals ("#06", 256, ms.Capacity);
899 writer.Write (testStreamData);
900 writer.Write (testStreamData);
901 writer.Write (testStreamData);
902 AssertEquals ("#07", 326, ms.Length);
906 public void MoreWriteByte ()
908 byte[] buffer = new byte [44];
910 MemoryStream ms = new MemoryStream (buffer);
911 BinaryWriter bw = new BinaryWriter (ms);
912 for(int i=0; i < 44; i++)
917 [ExpectedException (typeof (NotSupportedException))]
918 public void MoreWriteByte2 ()
920 byte[] buffer = new byte [43]; // Note the 43 here
922 MemoryStream ms = new MemoryStream (buffer);
923 BinaryWriter bw = new BinaryWriter (ms);
924 for(int i=0; i < 44; i++)
929 public void Expand ()
931 byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
932 MemoryStream ms = new MemoryStream ();
933 ms.Write (array, 0, array.Length);
935 ms.Seek (4, SeekOrigin.End);
937 Assert.AreEqual ("01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()), "Result");
941 public void PubliclyVisible ()
943 MemoryStream ms = new MemoryStream ();
944 Assert.IsNotNull (ms.GetBuffer (), "ctor()");
946 ms = new MemoryStream (1);
947 Assert.IsNotNull (ms.GetBuffer (), "ctor(1)");
949 ms = new MemoryStream (new byte[1], 0, 1, true, true);
950 Assert.IsNotNull (ms.GetBuffer (), "ctor(byte[],int,int,bool,bool");
954 [ExpectedException (typeof (UnauthorizedAccessException))]
955 public void PubliclyVisible_Ctor_ByteArray ()
957 MemoryStream ms = new MemoryStream (new byte[0]);
958 Assert.IsNotNull (ms.GetBuffer ());
962 [ExpectedException (typeof (UnauthorizedAccessException))]
963 public void PubliclyVisible_Ctor_ByteArray_Boolean ()
965 MemoryStream ms = new MemoryStream (new byte[0], true);
966 Assert.IsNotNull (ms.GetBuffer ());
970 [ExpectedException (typeof (UnauthorizedAccessException))]
971 public void PubliclyVisible_Ctor_ByteArray_Int_Int ()
973 MemoryStream ms = new MemoryStream (new byte[1], 0, 1);
974 Assert.IsNotNull (ms.GetBuffer ());
978 [ExpectedException (typeof (UnauthorizedAccessException))]
979 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean ()
981 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true);
982 Assert.IsNotNull (ms.GetBuffer ());
986 [ExpectedException (typeof (UnauthorizedAccessException))]
987 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean_Boolean ()
989 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
990 Assert.IsNotNull (ms.GetBuffer ());
993 [Test] // bug #350860
994 public void ToArray_Empty ()
996 MemoryStream ms = new MemoryStream (1);
1001 [Test] // bug #80205
1002 [Category ("NotWorking")]
1003 public void SerializeTest ()
1005 MemoryStream input = new MemoryStream ();
1006 byte [] bufferIn = Encoding.UTF8.GetBytes ("some test");
1007 input.Write (bufferIn, 0, bufferIn.Length);
1010 BinaryFormatter bf = new BinaryFormatter ();
1011 MemoryStream ms = new MemoryStream ();
1012 bf.Serialize (ms, input);
1014 byte [] bufferOut = new byte [ms.Length];
1016 ms.Read (bufferOut, 0, bufferOut.Length);
1018 Assert.AreEqual (_serialized, bufferOut);
1021 [Test] // bug #676060
1022 public void ZeroCapacity ()
1024 MemoryStream ms = new MemoryStream();
1030 byte[] bytes = ms.ToArray();
1033 [Test] // bug #80205
1034 [Category ("NotWorking")]
1035 public void DeserializeTest ()
1037 MemoryStream ms = new MemoryStream ();
1038 ms.Write (_serialized, 0, _serialized.Length);
1041 BinaryFormatter bf = new BinaryFormatter ();
1042 MemoryStream output = (MemoryStream) bf.Deserialize (ms);
1043 using (StreamReader sr = new StreamReader (output)) {
1044 Assert.AreEqual ("some test", sr.ReadToEnd ());
1048 private static byte [] _serialized = new byte [] {
1049 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
1050 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
1051 0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f, 0x2e,
1052 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x72, 0x65, 0x61,
1053 0x6d, 0x0a, 0x00, 0x00, 0x00, 0x07, 0x5f, 0x62, 0x75, 0x66, 0x66,
1054 0x65, 0x72, 0x07, 0x5f, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x09,
1055 0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x07, 0x5f,
1056 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x09, 0x5f, 0x63, 0x61, 0x70,
1057 0x61, 0x63, 0x69, 0x74, 0x79, 0x0b, 0x5f, 0x65, 0x78, 0x70, 0x61,
1058 0x6e, 0x64, 0x61, 0x62, 0x6c, 0x65, 0x09, 0x5f, 0x77, 0x72, 0x69,
1059 0x74, 0x61, 0x62, 0x6c, 0x65, 0x0a, 0x5f, 0x65, 0x78, 0x70, 0x6f,
1060 0x73, 0x61, 0x62, 0x6c, 0x65, 0x07, 0x5f, 0x69, 0x73, 0x4f, 0x70,
1061 0x65, 0x6e, 0x1d, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x42,
1062 0x79, 0x52, 0x65, 0x66, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x2b,
1063 0x5f, 0x5f, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x07,
1064 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08,
1065 0x08, 0x08, 0x08, 0x01, 0x01, 0x01, 0x01, 0x09, 0x02, 0x00, 0x00,
1066 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
1067 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x0a,
1068 0x0f, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x73,
1069 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x00, 0x00, 0x00,
1070 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1071 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1072 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1073 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1074 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1075 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1076 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1077 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1078 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1079 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1080 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1081 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1082 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1083 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1084 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1085 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1086 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1087 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1088 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1089 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1090 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1091 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1094 class MyMemoryStream : MemoryStream {
1096 public bool DisposedCalled = false;
1098 protected override void Dispose(bool disposing)
1100 DisposedCalled = true;
1104 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=322672
1105 public void BaseDisposeCalled ()
1107 MyMemoryStream ms = new MyMemoryStream ();
1108 Assert.IsFalse (ms.DisposedCalled, "Before");
1110 Assert.IsTrue (ms.DisposedCalled, "After");
1115 public void ReadAsync ()
1117 var buffer = new byte[3];
1118 var t = testStream.ReadAsync (buffer, 0, buffer.Length);
1119 Assert.AreEqual (t.Result, 3, "#1");
1120 Assert.AreEqual (99, buffer [1], "#2");
1122 testStream.Seek (99, SeekOrigin.Begin);
1123 t = testStream.ReadAsync (buffer, 0, 1);
1124 Assert.AreEqual (t.Result, 1, "#3");
1125 Assert.AreEqual (1, buffer[0], "#4");
1129 public void TestAsyncReadExceptions ()
1131 var buffer = new byte [3];
1132 using (var stream = new ExceptionalStream ()) {
1133 stream.Write (buffer, 0, buffer.Length);
1134 stream.Write (buffer, 0, buffer.Length);
1135 stream.Position = 0;
1136 var task = stream.ReadAsync (buffer, 0, buffer.Length);
1137 Assert.AreEqual (TaskStatus.RanToCompletion, task.Status, "#1");
1139 stream.Throw = true;
1140 task = stream.ReadAsync (buffer, 0, buffer.Length);
1141 Assert.IsTrue (task.IsFaulted, "#2");
1142 Assert.AreEqual (ExceptionalStream.Message, task.Exception.InnerException.Message, "#3");
1147 public void TestAsyncWriteExceptions ()
1149 var buffer = new byte [3];
1150 using (var stream = new ExceptionalStream ()) {
1151 var task = stream.WriteAsync (buffer, 0, buffer.Length);
1152 Assert.AreEqual(TaskStatus.RanToCompletion, task.Status, "#1");
1154 stream.Throw = true;
1155 task = stream.WriteAsync (buffer, 0, buffer.Length);
1156 Assert.IsTrue (task.IsFaulted, "#2");
1157 Assert.AreEqual (ExceptionalStream.Message, task.Exception.InnerException.Message, "#3");
1162 public void TestAsyncArgumentExceptions ()
1164 var buffer = new byte [3];
1165 using (var stream = new ExceptionalStream ()) {
1166 var task = stream.WriteAsync (buffer, 0, buffer.Length);
1167 Assert.IsTrue (task.IsCompleted);
1169 Assert.IsTrue (Throws<ArgumentException> (() => { stream.WriteAsync (buffer, 0, 1000); }), "#2");
1170 Assert.IsTrue (Throws<ArgumentException> (() => { stream.ReadAsync (buffer, 0, 1000); }), "#3");
1171 Assert.IsTrue (Throws<ArgumentException> (() => { stream.WriteAsync (buffer, 0, 1000, new CancellationToken (true)); }), "#4");
1172 Assert.IsTrue (Throws<ArgumentException> (() => { stream.ReadAsync (buffer, 0, 1000, new CancellationToken (true)); }), "#5");
1173 Assert.IsTrue (Throws<ArgumentException> (() => { stream.WriteAsync (null, 0, buffer.Length, new CancellationToken (true)); }), "#6");
1174 Assert.IsTrue (Throws<ArgumentException> (() => { stream.ReadAsync (null, 0, buffer.Length, new CancellationToken (true)); }), "#7");
1175 Assert.IsTrue (Throws<ArgumentException> (() => { stream.WriteAsync (buffer, 1000, buffer.Length, new CancellationToken (true)); }), "#8");
1176 Assert.IsTrue (Throws<ArgumentException> (() => { stream.ReadAsync (buffer, 1000, buffer.Length, new CancellationToken (true)); }), "#9");
1178 stream.AllowRead = false;
1179 var read_task = stream.ReadAsync (buffer, 0, buffer.Length);
1180 Assert.AreEqual (TaskStatus.RanToCompletion, read_task.Status, "#8");
1181 Assert.AreEqual (0, read_task.Result, "#9");
1183 stream.Position = 0;
1184 read_task = stream.ReadAsync (buffer, 0, buffer.Length);
1185 Assert.AreEqual (TaskStatus.RanToCompletion, read_task.Status, "#9");
1186 Assert.AreEqual (3, read_task.Result, "#10");
1188 var write_task = stream.WriteAsync (buffer, 0, buffer.Length);
1189 Assert.AreEqual (TaskStatus.RanToCompletion, write_task.Status, "#10");
1191 // test what happens when CanRead is overridden
1192 using (var norm = new ExceptionalStream (buffer, false)) {
1193 write_task = norm.WriteAsync (buffer, 0, buffer.Length);
1194 Assert.AreEqual (TaskStatus.RanToCompletion, write_task.Status, "#11");
1197 stream.AllowWrite = false;
1198 Assert.IsTrue (Throws<NotSupportedException> (() => { stream.Write (buffer, 0, buffer.Length); }), "#12");
1199 write_task = stream.WriteAsync (buffer, 0, buffer.Length);
1200 Assert.AreEqual (TaskStatus.Faulted, write_task.Status, "#13");
1205 public void TestAsyncFlushExceptions ()
1207 using (var stream = new ExceptionalStream ()) {
1208 var task = stream.FlushAsync ();
1209 Assert.IsTrue (task.IsCompleted, "#1");
1211 task = stream.FlushAsync (new CancellationToken(true));
1212 Assert.IsTrue (task.IsCanceled, "#2");
1214 stream.Throw = true;
1215 task = stream.FlushAsync ();
1216 Assert.IsTrue (task.IsFaulted, "#3");
1217 Assert.AreEqual (ExceptionalStream.Message, task.Exception.InnerException.Message, "#4");
1219 task = stream.FlushAsync (new CancellationToken (true));
1220 Assert.IsTrue (task.IsCanceled, "#5");
1225 public void TestCopyAsync ()
1227 using (var stream = new ExceptionalStream ()) {
1228 using (var dest = new ExceptionalStream ()) {
1229 byte [] buffer = new byte [] { 12, 13, 8 };
1231 stream.Write (buffer, 0, buffer.Length);
1232 stream.Position = 0;
1233 var task = stream.CopyToAsync (dest, 1);
1234 Assert.AreEqual (TaskStatus.RanToCompletion, task.Status);
1235 Assert.AreEqual (3, stream.Length);
1236 Assert.AreEqual (3, dest.Length);
1238 stream.Position = 0;
1240 task = stream.CopyToAsync (dest, 1);
1241 Assert.AreEqual (TaskStatus.Faulted, task.Status);
1242 Assert.AreEqual (3, stream.Length);
1243 Assert.AreEqual (3, dest.Length);
1249 public void WritableOverride ()
1251 var buffer = new byte [3];
1252 var stream = new MemoryStream (buffer, false);
1253 Assert.IsTrue (Throws<NotSupportedException> (() => { stream.Write (buffer, 0, buffer.Length); }), "#1");
1254 Assert.IsTrue (Throws<ArgumentNullException> (() => { stream.Write (null, 0, buffer.Length); }), "#1.1");
1256 Assert.IsTrue (Throws<ObjectDisposedException> (() => { stream.Write (buffer, 0, buffer.Length); }), "#2");
1257 stream = new MemoryStream (buffer, true);
1259 Assert.IsFalse (stream.CanWrite, "#3");
1261 var estream = new ExceptionalStream (buffer, false);
1262 Assert.IsFalse (Throws<Exception> (() => { estream.Write (buffer, 0, buffer.Length); }), "#4");
1263 estream.AllowWrite = false;
1264 estream.Position = 0;
1265 Assert.IsTrue (Throws<NotSupportedException> (() => { estream.Write (buffer, 0, buffer.Length); }), "#5");
1266 estream.AllowWrite = true;
1268 Assert.IsTrue (estream.CanWrite, "#6");
1269 Assert.IsTrue (Throws<ObjectDisposedException> (() => { stream.Write (buffer, 0, buffer.Length); }), "#7");
1273 public void ReadAsync_Canceled ()
1275 var buffer = new byte[3];
1276 var t = testStream.ReadAsync (buffer, 0, buffer.Length, new CancellationToken (true));
1277 Assert.IsTrue (t.IsCanceled);
1279 t = testStream.ReadAsync (buffer, 0, buffer.Length);
1280 Assert.AreEqual (t.Result, 3, "#1");
1281 Assert.AreEqual (99, buffer[1], "#2");
1285 public void WriteAsync ()
1287 var buffer = new byte[3] { 3, 5, 9 };
1289 var ms = new MemoryStream ();
1290 var t = ms.WriteAsync (buffer, 0, buffer.Length);
1291 Assert.IsTrue (t.IsCompleted, "#1");
1293 ms.Seek (0, SeekOrigin.Begin);
1294 Assert.AreEqual (3, ms.ReadByte (), "#2");
1298 public void WriteAsync_Canceled ()
1300 var buffer = new byte[3] { 1, 2, 3 };
1301 var t = testStream.WriteAsync (buffer, 0, buffer.Length, new CancellationToken (true));
1302 Assert.IsTrue (t.IsCanceled);
1304 t = testStream.WriteAsync (buffer, 0, buffer.Length);
1305 Assert.IsTrue (t.IsCompleted, "#1");
1308 bool Throws<T> (Action a) where T : Exception