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 NUnit.Framework;
23 namespace MonoTests.System.IO
26 public class MemoryStreamTest
28 class SignaledMemoryStream : MemoryStream
32 public SignaledMemoryStream (byte[] buffer, WaitHandle w)
38 public override int Read (byte[] buffer, int offset, int count)
40 if (!w.WaitOne (2000))
43 Assert.IsTrue (Thread.CurrentThread.IsThreadPoolThread, "IsThreadPoolThread");
44 return base.Read (buffer, offset, count);
49 MemoryStream testStream;
50 byte [] testStreamData;
55 testStreamData = new byte [100];
57 for (int i = 0; i < 100; i++)
58 testStreamData[i] = (byte) (100 - i);
60 testStream = new MemoryStream (testStreamData);
64 // Verify that the first count bytes in testBytes are the same as
65 // the count bytes from index start in testStreamData
67 void VerifyTestData (string id, byte [] testBytes, int start, int count)
69 if (testBytes == null)
70 Assert.Fail (id + "+1 testBytes is null");
74 start + count > testStreamData.Length ||
75 start > testStreamData.Length)
76 throw new ArgumentOutOfRangeException (id + "+2");
78 for (int test = 0; test < count; test++) {
79 if (testBytes [test] == testStreamData [start + test])
82 string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
83 failStr = String.Format (failStr,
87 testStreamData [start + test]);
88 Assert.Fail (id + "-3" + failStr);
92 public void AssertEquals (string message, int expected, int actual)
94 Assert.AreEqual (expected, actual, message);
97 public void AssertEquals (string message, long expected, long actual)
99 Assert.AreEqual (expected, actual, message);
102 public void AssertEquals (string message, bool expected, bool actual)
104 Assert.AreEqual (expected, actual, message);
108 public void ConstructorsOne ()
110 MemoryStream ms = new MemoryStream();
112 AssertEquals ("#01", 0L, ms.Length);
113 AssertEquals ("#02", 0, ms.Capacity);
114 AssertEquals ("#03", true, ms.CanWrite);
118 public void ConstructorsTwo ()
120 MemoryStream ms = new MemoryStream (10);
122 AssertEquals ("#01", 0L, ms.Length);
123 AssertEquals ("#02", 10, ms.Capacity);
125 byte [] buffer = ms.GetBuffer ();
127 AssertEquals ("#03", -1, ms.ReadByte ());
128 Assert.IsNull (buffer, "#04"); // <--
129 ms.Read (new byte [5], 0, 5);
130 AssertEquals ("#05", 0, ms.Position);
131 AssertEquals ("#06", 0, ms.Length);
136 public void ConstructorsThree ()
138 MemoryStream ms = new MemoryStream (testStreamData);
139 AssertEquals ("#01", 100, ms.Length);
140 AssertEquals ("#02", 0, ms.Position);
144 public void ConstructorsFour ()
146 MemoryStream ms = new MemoryStream (testStreamData, true);
147 AssertEquals ("#01", 100, ms.Length);
148 AssertEquals ("#02", 0, ms.Position);
150 byte saved = testStreamData [50];
153 AssertEquals ("#03", testStreamData [50], 23);
155 testStreamData [50] = saved;
160 } catch (Exception) {
167 public void ConstructorsFive ()
169 MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
170 AssertEquals ("#01", 50, ms.Length);
171 AssertEquals ("#02", 0, ms.Position);
172 AssertEquals ("#03", 50, ms.Capacity);
174 byte saved = testStreamData [51];
177 AssertEquals ("#04", testStreamData [51], 23);
179 testStreamData [51] = saved;
186 } catch (NotSupportedException) {
192 } catch (NotSupportedException) {
198 } catch (NotSupportedException) {
201 AssertEquals ("#08", 50, ms.ToArray ().Length);
205 [ExpectedException (typeof (ArgumentOutOfRangeException))]
206 public void ConstructorsSix ()
208 MemoryStream ms = new MemoryStream (-2);
214 byte [] readBytes = new byte [20];
216 /* Test simple read */
217 testStream.Read (readBytes, 0, 10);
218 VerifyTestData ("R1", readBytes, 0, 10);
220 /* Seek back to beginning */
222 testStream.Seek (0, SeekOrigin.Begin);
224 /* Read again, bit more this time */
225 testStream.Read (readBytes, 0, 20);
226 VerifyTestData ("R2", readBytes, 0, 20);
228 /* Seek to 20 bytes from End */
229 testStream.Seek (-20, SeekOrigin.End);
230 testStream.Read (readBytes, 0, 20);
231 VerifyTestData ("R3", readBytes, 80, 20);
233 int readByte = testStream.ReadByte();
234 Assert.AreEqual (-1, readByte, "R4");
238 public void BeginRead ()
240 byte [] readBytes = new byte [5];
242 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
243 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
244 Assert.AreEqual (5, testStream.EndRead (res), "#2");
248 public void BeginRead_WithState ()
250 byte [] readBytes = new byte [5];
251 string async_state = null;
252 var wh = new ManualResetEvent (false);
254 var res = testStream.BeginRead (readBytes, 0, 5, l => {
255 async_state = l.AsyncState as string;
259 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
260 Assert.AreEqual ("state", res.AsyncState, "#2");
261 Assert.IsTrue (res.IsCompleted, "#3");
262 Assert.AreEqual (5, testStream.EndRead (res), "#4");
265 Assert.AreEqual ("state", async_state, "#5");
270 public void BeginReadAsync ()
272 byte[] readBytes = new byte[5];
273 var wh = new ManualResetEvent (false);
274 using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
275 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
276 Assert.IsFalse (res.IsCompleted, "#1");
277 Assert.IsFalse (res.CompletedSynchronously, "#2");
279 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
280 Assert.IsTrue (res.IsCompleted, "#4");
281 Assert.AreEqual (5, testStream.EndRead (res), "#5");
288 public void BeginReadIsBlockingNextRead ()
290 byte[] readBytes = new byte[5];
291 byte[] readBytes2 = new byte[3];
292 var wh = new ManualResetEvent (false);
293 var end = new ManualResetEvent (false);
295 using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
296 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
298 bool blocking = true;
299 ThreadPool.QueueUserWorkItem (l => {
300 var res2 = testStream.BeginRead (readBytes2, 0, 3, null, null);
302 Assert.IsTrue (res2.AsyncWaitHandle.WaitOne (2000), "#10");
303 Assert.IsTrue (res2.IsCompleted, "#11");
304 Assert.AreEqual (3, testStream.EndRead (res2), "#12");
305 Assert.AreEqual (95, readBytes2[0], "#13");
309 Assert.IsFalse (res.IsCompleted, "#1");
310 Thread.Sleep (500); // Lame but don't know how to wait for another BeginRead which does not return
311 Assert.IsTrue (blocking, "#2");
314 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
315 Assert.IsTrue (res.IsCompleted, "#4");
316 Assert.AreEqual (5, testStream.EndRead (res), "#5");
317 Assert.IsTrue (end.WaitOne (2000), "#6");
318 Assert.AreEqual (100, readBytes[0], "#7");
323 public void BeginRead_Read ()
325 byte[] readBytes = new byte[5];
326 var wh = new ManualResetEvent (false);
327 using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
328 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
329 Assert.AreEqual (100, testStream.ReadByte (), "#0");
330 Assert.IsFalse (res.IsCompleted, "#1");
331 Assert.IsFalse (res.CompletedSynchronously, "#2");
333 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
334 Assert.IsTrue (res.IsCompleted, "#4");
335 Assert.AreEqual (5, testStream.EndRead (res), "#5");
336 Assert.AreEqual (99, readBytes [0], "#6");
343 public void BeginRead_BeginWrite ()
345 byte[] readBytes = new byte[5];
346 byte[] readBytes2 = new byte[3] { 1, 2, 3 };
347 var wh = new ManualResetEvent (false);
348 var end = new ManualResetEvent (false);
350 using (var testStream = new SignaledMemoryStream (testStreamData, wh)) {
351 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
353 bool blocking = true;
354 ThreadPool.QueueUserWorkItem (l => {
355 var res2 = testStream.BeginWrite (readBytes2, 0, 3, null, null);
357 Assert.IsTrue (res2.AsyncWaitHandle.WaitOne (2000), "#10");
358 Assert.IsTrue (res2.IsCompleted, "#11");
359 testStream.EndWrite (res2);
363 Assert.IsFalse (res.IsCompleted, "#1");
364 Thread.Sleep (500); // Lame but don't know how to wait for another BeginWrite which does not return
365 Assert.IsTrue (blocking, "#2");
368 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (2000), "#3");
369 Assert.IsTrue (res.IsCompleted, "#4");
370 Assert.AreEqual (5, testStream.EndRead (res), "#5");
371 Assert.IsTrue (end.WaitOne (2000), "#6");
376 public void BeginWrite ()
378 var writeBytes = new byte [5] { 2, 3, 4, 10, 12 };
380 var res = testStream.BeginWrite (writeBytes, 0, 5, null, null);
381 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
382 testStream.EndWrite (res);
386 public void BeginWrite_WithState ()
388 var writeBytes = new byte[5] { 2, 3, 4, 10, 12 };
389 string async_state = null;
390 var wh = new ManualResetEvent (false);
392 var res = testStream.BeginWrite (writeBytes, 0, 5, l => {
393 async_state = l.AsyncState as string;
397 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
398 Assert.IsTrue (res.IsCompleted, "#2");
399 Assert.AreEqual ("state", res.AsyncState, "#3");
400 testStream.EndWrite (res);
403 Assert.AreEqual ("state", async_state, "#4");
408 public void EndRead_Twice ()
410 byte[] readBytes = new byte[5];
412 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
413 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
414 Assert.AreEqual (5, testStream.EndRead (res), "#2");
417 testStream.EndRead (res);
419 } catch (ArgumentException) {
425 public void EndRead_Disposed ()
427 byte[] readBytes = new byte[5];
429 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
430 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
431 testStream.Dispose ();
432 Assert.AreEqual (5, testStream.EndRead (res), "#2");
436 public void EndWrite_OnBeginRead ()
438 byte[] readBytes = new byte[5];
440 var res = testStream.BeginRead (readBytes, 0, 5, null, null);
441 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
444 testStream.EndWrite (res);
446 } catch (ArgumentException) {
449 testStream.EndRead (res);
453 public void EndWrite_Twice ()
455 var wBytes = new byte[5];
457 var res = testStream.BeginWrite (wBytes, 0, 5, null, null);
458 Assert.IsTrue (res.AsyncWaitHandle.WaitOne (1000), "#1");
459 testStream.EndWrite (res);
462 testStream.EndWrite (res);
464 } catch (ArgumentException) {
471 public void WriteBytes ()
473 byte[] readBytes = new byte[100];
475 MemoryStream ms = new MemoryStream (100);
477 for (int i = 0; i < 100; i++)
478 ms.WriteByte (testStreamData [i]);
480 ms.Seek (0, SeekOrigin.Begin);
481 testStream.Read (readBytes, 0, 100);
482 VerifyTestData ("W1", readBytes, 0, 100);
486 public void WriteBlock ()
488 byte[] readBytes = new byte[100];
490 MemoryStream ms = new MemoryStream (100);
492 ms.Write (testStreamData, 0, 100);
493 ms.Seek (0, SeekOrigin.Begin);
494 testStream.Read (readBytes, 0, 100);
495 VerifyTestData ("WB1", readBytes, 0, 100);
496 byte[] arrayBytes = testStream.ToArray();
497 AssertEquals ("#01", 100, arrayBytes.Length);
498 VerifyTestData ("WB2", arrayBytes, 0, 100);
502 public void PositionLength ()
504 MemoryStream ms = new MemoryStream ();
506 ms.WriteByte ((byte) 'M');
507 ms.WriteByte ((byte) 'O');
508 AssertEquals ("#01", 6, ms.Length);
509 AssertEquals ("#02", 6, ms.Position);
511 AssertEquals ("#03", 0, ms.Position);
515 [ExpectedException (typeof (NotSupportedException))]
516 public void MorePositionLength ()
518 MemoryStream ms = new MemoryStream (testStreamData);
520 AssertEquals ("#01", 101, ms.Position);
521 AssertEquals ("#02", 100, ms.Length);
522 ms.WriteByte (1); // This should throw the exception
526 public void GetBufferOne ()
528 MemoryStream ms = new MemoryStream ();
529 byte [] buffer = ms.GetBuffer ();
530 AssertEquals ("#01", 0, buffer.Length);
534 public void GetBufferTwo ()
536 MemoryStream ms = new MemoryStream (100);
537 byte [] buffer = ms.GetBuffer ();
538 AssertEquals ("#01", 100, buffer.Length);
540 ms.Write (testStreamData, 0, 100);
541 ms.Write (testStreamData, 0, 100);
542 AssertEquals ("#02", 200, ms.Length);
543 buffer = ms.GetBuffer ();
544 AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
548 public void Closed ()
550 MemoryStream ms = new MemoryStream (100);
555 } catch (ObjectDisposedException) {
565 } catch (ObjectDisposedException) {
573 ms.Read (null, 0, 1);
575 } catch (ArgumentNullException) {
579 ms.Write (null, 0, 1);
581 } catch (ArgumentNullException) {
587 [ExpectedException (typeof (ObjectDisposedException))]
588 public void Close_get_Length ()
590 MemoryStream ms = new MemoryStream (100);
596 [ExpectedException (typeof (ObjectDisposedException))]
597 public void Close_get_Position ()
599 MemoryStream ms = new MemoryStream (100);
601 long x = ms.Position;
605 [ExpectedException (typeof (ObjectDisposedException))]
606 public void Close_set_Position ()
608 MemoryStream ms = new MemoryStream (100);
616 MemoryStream ms = new MemoryStream (100);
617 ms.Write (testStreamData, 0, 100);
618 ms.Seek (0, SeekOrigin.Begin);
620 ms.Seek (-50, SeekOrigin.Current);
621 ms.Seek (-50, SeekOrigin.End);
626 ms.Seek (-50, SeekOrigin.Current);
627 } catch (IOException) {
635 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
636 } catch (ArgumentOutOfRangeException) {
645 // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
646 ms.Seek (Int64.MinValue, SeekOrigin.Begin);
647 } catch (IOException) {
654 ms=new MemoryStream (256);
656 ms.Write (testStreamData, 0, 100);
658 AssertEquals ("#01", 100, ms.Length);
659 AssertEquals ("#02", 0, ms.Position);
662 AssertEquals ("#03", 100, ms.Length);
663 AssertEquals ("#04", 128, ms.Position);
666 AssertEquals ("#05", 100, ms.Length);
667 AssertEquals ("#06", 768, ms.Position);
670 AssertEquals ("#07", 769, ms.Length);
671 AssertEquals ("#08", 769, ms.Position);
675 public void Seek_Disposed ()
677 MemoryStream ms = new MemoryStream ();
680 ms.Seek (0, SeekOrigin.Begin);
682 } catch (ObjectDisposedException) {
687 public void SetLength ()
689 MemoryStream ms = new MemoryStream ();
690 ms.Write (testStreamData, 0, 100);
693 AssertEquals ("#01", 150, ms.Length);
694 AssertEquals ("#02", 100, ms.Position);
696 AssertEquals ("#03", 80, ms.Length);
697 AssertEquals ("#04", 80, ms.Position);
701 [ExpectedException (typeof (NotSupportedException))]
702 public void SetLength_ReadOnly ()
704 MemoryStream ms = new MemoryStream (testStreamData, false);
709 public void Capacity ()
711 MemoryStream ms = new MemoryStream ();
713 Assert.AreEqual (0, ms.Capacity, "#A1");
714 Assert.AreEqual (0, ms.GetBuffer ().Length, "#A2");
716 ms.WriteByte ((byte)'6');
717 Assert.AreEqual (256, ms.Capacity, "#B1");
718 Assert.AreEqual (256, ms.GetBuffer ().Length, "#B2");
722 Assert.AreEqual (100, ms.Capacity, "#C1");
723 Assert.AreEqual (100, ms.GetBuffer ().Length, "#C2");
727 Assert.AreEqual (120, ms.Capacity, "#D1");
728 Assert.AreEqual (120, ms.GetBuffer ().Length, "#D2");
730 // Grow the buffer, reduce length -so we have a dirty area-
731 // and then we assign capacity to the same. The idea is that we should
732 // avoid creating a new internal buffer it's not needed.
734 ms = new MemoryStream ();
736 byte [] buff = new byte [] { 0x01, 0x02, 0x03, 0x04, 0x05 };
737 ms.Write (buff, 0, buff.Length);
738 Assert.AreEqual (8, ms.Capacity, "#E1");
739 Assert.AreEqual (8, ms.GetBuffer ().Length, "#E2");
741 // Reduce *length*, not capacity
742 byte [] buff_copy = ms.GetBuffer ();
744 Assert.AreEqual (3, ms.Length, "#F1");
745 Assert.AreEqual (true, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#F2");
747 // Set Capacity to the very same value it has now
748 ms.Capacity = ms.Capacity;
749 Assert.AreEqual (true, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#G1"); // keep the same buffer
751 // Finally, growing it discards the prev buff
752 ms.Capacity = ms.Capacity + 1;
753 Assert.AreEqual (false, AreBuffersEqual (buff_copy, ms.GetBuffer ()), "#H1");
756 bool AreBuffersEqual (byte [] buff1, byte [] buff2)
758 if ((buff1 == null) != (buff2 == null))
761 if (buff1.Length != buff2.Length)
764 for (int i = 0; i < buff1.Length; i++)
765 if (buff1 [i] != buff2 [i])
771 [Test] // bug #327053
772 public void ZeroingOnExpand ()
774 byte [] values = { 3, 2, 1 };
775 byte [] reference = { 3, 2, 1 };
776 byte [] cropped = { 3, 0, 0 };
777 MemoryStream ms = new MemoryStream (values);
778 Assert.AreEqual (values, reference, "#A1");
779 ms.Seek (3, SeekOrigin.Begin);
780 Assert.AreEqual (reference, values, "#A2");
782 Assert.AreEqual (reference, values, "#B1");
783 byte [] read = new byte [5];
784 ms.Read (read, 0, 5);
785 Assert.AreEqual (new byte [] { 0, 0, 0, 0, 0 }, read, "#B2");
786 Assert.AreEqual (reference, values, "#B3");
788 Assert.AreEqual (cropped, values, "#C1");
789 ms.Seek (0, SeekOrigin.Begin);
791 ms.Read (read, 0, 3);
792 Assert.AreEqual (cropped, read, "#C2");
793 Assert.AreEqual (cropped, values, "#C3");
797 [ExpectedException (typeof (NotSupportedException))]
798 public void WriteNonWritable ()
800 MemoryStream ms = new MemoryStream (testStreamData, false);
801 ms.Write (testStreamData, 0, 100);
805 [ExpectedException (typeof (NotSupportedException))]
806 public void WriteExpand ()
808 MemoryStream ms = new MemoryStream (testStreamData);
809 ms.Write (testStreamData, 0, 100);
810 ms.Write (testStreamData, 0, 100); // This one throws the exception
814 public void WriteByte ()
816 MemoryStream ms = new MemoryStream (100);
817 ms.Write (testStreamData, 0, 100);
820 AssertEquals ("#01", 101, ms.Position);
821 AssertEquals ("#02", 101, ms.Length);
822 AssertEquals ("#03", 256, ms.Capacity);
823 ms.Write (testStreamData, 0, 100);
824 ms.Write (testStreamData, 0, 100);
826 AssertEquals ("#04", 301, ms.Position);
827 AssertEquals ("#05", 301, ms.Length);
828 AssertEquals ("#06", 512, ms.Capacity);
832 public void WriteLengths () {
833 MemoryStream ms=new MemoryStream (256);
834 BinaryWriter writer=new BinaryWriter (ms);
836 writer.Write ((byte)'1');
837 AssertEquals ("#01", 1, ms.Length);
838 AssertEquals ("#02", 256, ms.Capacity);
840 writer.Write ((ushort)0);
841 AssertEquals ("#03", 3, ms.Length);
842 AssertEquals ("#04", 256, ms.Capacity);
844 writer.Write (testStreamData, 0, 23);
845 AssertEquals ("#05", 26, ms.Length);
846 AssertEquals ("#06", 256, ms.Capacity);
848 writer.Write (testStreamData);
849 writer.Write (testStreamData);
850 writer.Write (testStreamData);
851 AssertEquals ("#07", 326, ms.Length);
855 public void MoreWriteByte ()
857 byte[] buffer = new byte [44];
859 MemoryStream ms = new MemoryStream (buffer);
860 BinaryWriter bw = new BinaryWriter (ms);
861 for(int i=0; i < 44; i++)
866 [ExpectedException (typeof (NotSupportedException))]
867 public void MoreWriteByte2 ()
869 byte[] buffer = new byte [43]; // Note the 43 here
871 MemoryStream ms = new MemoryStream (buffer);
872 BinaryWriter bw = new BinaryWriter (ms);
873 for(int i=0; i < 44; i++)
878 public void Expand ()
880 byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
881 MemoryStream ms = new MemoryStream ();
882 ms.Write (array, 0, array.Length);
884 ms.Seek (4, SeekOrigin.End);
886 Assert.AreEqual ("01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()), "Result");
890 public void PubliclyVisible ()
892 MemoryStream ms = new MemoryStream ();
893 Assert.IsNotNull (ms.GetBuffer (), "ctor()");
895 ms = new MemoryStream (1);
896 Assert.IsNotNull (ms.GetBuffer (), "ctor(1)");
898 ms = new MemoryStream (new byte[1], 0, 1, true, true);
899 Assert.IsNotNull (ms.GetBuffer (), "ctor(byte[],int,int,bool,bool");
903 [ExpectedException (typeof (UnauthorizedAccessException))]
904 public void PubliclyVisible_Ctor_ByteArray ()
906 MemoryStream ms = new MemoryStream (new byte[0]);
907 Assert.IsNotNull (ms.GetBuffer ());
911 [ExpectedException (typeof (UnauthorizedAccessException))]
912 public void PubliclyVisible_Ctor_ByteArray_Boolean ()
914 MemoryStream ms = new MemoryStream (new byte[0], true);
915 Assert.IsNotNull (ms.GetBuffer ());
919 [ExpectedException (typeof (UnauthorizedAccessException))]
920 public void PubliclyVisible_Ctor_ByteArray_Int_Int ()
922 MemoryStream ms = new MemoryStream (new byte[1], 0, 1);
923 Assert.IsNotNull (ms.GetBuffer ());
927 [ExpectedException (typeof (UnauthorizedAccessException))]
928 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean ()
930 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true);
931 Assert.IsNotNull (ms.GetBuffer ());
935 [ExpectedException (typeof (UnauthorizedAccessException))]
936 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean_Boolean ()
938 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
939 Assert.IsNotNull (ms.GetBuffer ());
942 [Test] // bug #350860
943 public void ToArray_Empty ()
945 MemoryStream ms = new MemoryStream (1);
951 [Category ("NotWorking")]
952 public void SerializeTest ()
954 MemoryStream input = new MemoryStream ();
955 byte [] bufferIn = Encoding.UTF8.GetBytes ("some test");
956 input.Write (bufferIn, 0, bufferIn.Length);
959 BinaryFormatter bf = new BinaryFormatter ();
960 MemoryStream ms = new MemoryStream ();
961 bf.Serialize (ms, input);
963 byte [] bufferOut = new byte [ms.Length];
965 ms.Read (bufferOut, 0, bufferOut.Length);
967 Assert.AreEqual (_serialized, bufferOut);
970 [Test] // bug #676060
971 public void ZeroCapacity ()
973 MemoryStream ms = new MemoryStream();
979 byte[] bytes = ms.ToArray();
983 [Category ("NotWorking")]
984 public void DeserializeTest ()
986 MemoryStream ms = new MemoryStream ();
987 ms.Write (_serialized, 0, _serialized.Length);
990 BinaryFormatter bf = new BinaryFormatter ();
991 MemoryStream output = (MemoryStream) bf.Deserialize (ms);
992 using (StreamReader sr = new StreamReader (output)) {
993 Assert.AreEqual ("some test", sr.ReadToEnd ());
997 private static byte [] _serialized = new byte [] {
998 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
999 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
1000 0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f, 0x2e,
1001 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x72, 0x65, 0x61,
1002 0x6d, 0x0a, 0x00, 0x00, 0x00, 0x07, 0x5f, 0x62, 0x75, 0x66, 0x66,
1003 0x65, 0x72, 0x07, 0x5f, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x09,
1004 0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x07, 0x5f,
1005 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x09, 0x5f, 0x63, 0x61, 0x70,
1006 0x61, 0x63, 0x69, 0x74, 0x79, 0x0b, 0x5f, 0x65, 0x78, 0x70, 0x61,
1007 0x6e, 0x64, 0x61, 0x62, 0x6c, 0x65, 0x09, 0x5f, 0x77, 0x72, 0x69,
1008 0x74, 0x61, 0x62, 0x6c, 0x65, 0x0a, 0x5f, 0x65, 0x78, 0x70, 0x6f,
1009 0x73, 0x61, 0x62, 0x6c, 0x65, 0x07, 0x5f, 0x69, 0x73, 0x4f, 0x70,
1010 0x65, 0x6e, 0x1d, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x42,
1011 0x79, 0x52, 0x65, 0x66, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x2b,
1012 0x5f, 0x5f, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x07,
1013 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08,
1014 0x08, 0x08, 0x08, 0x01, 0x01, 0x01, 0x01, 0x09, 0x02, 0x00, 0x00,
1015 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
1016 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x0a,
1017 0x0f, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x73,
1018 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x00, 0x00, 0x00,
1019 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1020 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1021 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1022 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1023 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1024 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1025 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1026 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1027 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1028 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1029 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1030 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1031 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1032 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1033 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1034 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1035 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1036 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1037 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1038 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1039 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1040 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1043 class MyMemoryStream : MemoryStream {
1045 public bool DisposedCalled = false;
1047 protected override void Dispose(bool disposing)
1049 DisposedCalled = true;
1053 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=322672
1054 public void BaseDisposeCalled ()
1056 MyMemoryStream ms = new MyMemoryStream ();
1057 Assert.IsFalse (ms.DisposedCalled, "Before");
1059 Assert.IsTrue (ms.DisposedCalled, "After");
1064 public void ReadAsync ()
1066 var buffer = new byte[3];
1067 var t = testStream.ReadAsync (buffer, 0, buffer.Length);
1068 Assert.AreEqual (t.Result, 3, "#1");
1069 Assert.AreEqual (99, buffer [1], "#2");
1071 testStream.Seek (99, SeekOrigin.Begin);
1072 t = testStream.ReadAsync (buffer, 0, 1);
1073 Assert.AreEqual (t.Result, 1, "#3");
1074 Assert.AreEqual (1, buffer[0], "#4");
1078 public void ReadAsync_Canceled ()
1080 var buffer = new byte[3];
1081 var t = testStream.ReadAsync (buffer, 0, buffer.Length, new CancellationToken (true));
1082 Assert.IsTrue (t.IsCanceled);
1084 t = testStream.ReadAsync (buffer, 0, buffer.Length);
1085 Assert.AreEqual (t.Result, 3, "#1");
1086 Assert.AreEqual (99, buffer[1], "#2");
1090 public void WriteAsync ()
1092 var buffer = new byte[3] { 3, 5, 9 };
1094 var ms = new MemoryStream ();
1095 var t = ms.WriteAsync (buffer, 0, buffer.Length);
1096 Assert.IsTrue (t.IsCompleted, "#1");
1098 ms.Seek (0, SeekOrigin.Begin);
1099 Assert.AreEqual (3, ms.ReadByte (), "#2");
1103 public void WriteAsync_Canceled ()
1105 var buffer = new byte[3] { 1, 2, 3 };
1106 var t = testStream.WriteAsync (buffer, 0, buffer.Length, new CancellationToken (true));
1107 Assert.IsTrue (t.IsCanceled);
1109 t = testStream.WriteAsync (buffer, 0, buffer.Length);
1110 Assert.IsTrue (t.IsCompleted, "#1");