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 [ExpectedException (typeof (ObjectDisposedException))]
431 public void Seek_Disposed ()
433 MemoryStream ms = new MemoryStream ();
435 ms.Seek (0, SeekOrigin.Begin);
439 public void SetLength ()
441 MemoryStream ms = new MemoryStream ();
442 ms.Write (testStreamData, 0, 100);
445 AssertEquals ("#01", 150, ms.Length);
446 AssertEquals ("#02", 100, ms.Position);
448 AssertEquals ("#03", 80, ms.Length);
449 AssertEquals ("#04", 80, ms.Position);
453 [ExpectedException (typeof (NotSupportedException))]
454 public void SetLength_ReadOnly ()
456 MemoryStream ms = new MemoryStream (testStreamData, false);
461 [ExpectedException (typeof (NotSupportedException))]
462 public void WriteNonWritable ()
464 MemoryStream ms = new MemoryStream (testStreamData, false);
465 ms.Write (testStreamData, 0, 100);
469 [ExpectedException (typeof (NotSupportedException))]
470 public void WriteExpand ()
472 MemoryStream ms = new MemoryStream (testStreamData);
473 ms.Write (testStreamData, 0, 100);
474 ms.Write (testStreamData, 0, 100); // This one throws the exception
478 public void WriteByte ()
480 MemoryStream ms = new MemoryStream (100);
481 ms.Write (testStreamData, 0, 100);
484 AssertEquals ("#01", 101, ms.Position);
485 AssertEquals ("#02", 101, ms.Length);
486 AssertEquals ("#03", 256, ms.Capacity);
487 ms.Write (testStreamData, 0, 100);
488 ms.Write (testStreamData, 0, 100);
490 AssertEquals ("#04", 301, ms.Position);
491 AssertEquals ("#05", 301, ms.Length);
492 AssertEquals ("#06", 512, ms.Capacity);
496 public void WriteLengths () {
497 MemoryStream ms=new MemoryStream (256);
498 BinaryWriter writer=new BinaryWriter (ms);
500 writer.Write ((byte)'1');
501 AssertEquals ("#01", 1, ms.Length);
502 AssertEquals ("#02", 256, ms.Capacity);
504 writer.Write ((ushort)0);
505 AssertEquals ("#03", 3, ms.Length);
506 AssertEquals ("#04", 256, ms.Capacity);
508 writer.Write (testStreamData, 0, 23);
509 AssertEquals ("#05", 26, ms.Length);
510 AssertEquals ("#06", 256, ms.Capacity);
512 writer.Write (testStreamData);
513 writer.Write (testStreamData);
514 writer.Write (testStreamData);
515 AssertEquals ("#07", 326, ms.Length);
519 public void MoreWriteByte ()
521 byte[] buffer = new byte [44];
523 MemoryStream ms = new MemoryStream (buffer);
524 BinaryWriter bw = new BinaryWriter (ms);
525 for(int i=0; i < 44; i++)
530 [ExpectedException (typeof (NotSupportedException))]
531 public void MoreWriteByte2 ()
533 byte[] buffer = new byte [43]; // Note the 43 here
535 MemoryStream ms = new MemoryStream (buffer);
536 BinaryWriter bw = new BinaryWriter (ms);
537 for(int i=0; i < 44; i++)
542 public void Expand ()
544 byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
545 MemoryStream ms = new MemoryStream ();
546 ms.Write (array, 0, array.Length);
548 ms.Seek (4, SeekOrigin.End);
550 Assert.AreEqual ("01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()), "Result");
554 public void PubliclyVisible ()
556 MemoryStream ms = new MemoryStream ();
557 Assert.IsNotNull (ms.GetBuffer (), "ctor()");
559 ms = new MemoryStream (1);
560 Assert.IsNotNull (ms.GetBuffer (), "ctor(1)");
562 ms = new MemoryStream (new byte[1], 0, 1, true, true);
563 Assert.IsNotNull (ms.GetBuffer (), "ctor(byte[],int,int,bool,bool");
567 [ExpectedException (typeof (UnauthorizedAccessException))]
568 public void PubliclyVisible_Ctor_ByteArray ()
570 MemoryStream ms = new MemoryStream (new byte[0]);
571 Assert.IsNotNull (ms.GetBuffer ());
575 [ExpectedException (typeof (UnauthorizedAccessException))]
576 public void PubliclyVisible_Ctor_ByteArray_Boolean ()
578 MemoryStream ms = new MemoryStream (new byte[0], true);
579 Assert.IsNotNull (ms.GetBuffer ());
583 [ExpectedException (typeof (UnauthorizedAccessException))]
584 public void PubliclyVisible_Ctor_ByteArray_Int_Int ()
586 MemoryStream ms = new MemoryStream (new byte[1], 0, 1);
587 Assert.IsNotNull (ms.GetBuffer ());
591 [ExpectedException (typeof (UnauthorizedAccessException))]
592 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean ()
594 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true);
595 Assert.IsNotNull (ms.GetBuffer ());
599 [ExpectedException (typeof (UnauthorizedAccessException))]
600 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean_Boolean ()
602 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
603 Assert.IsNotNull (ms.GetBuffer ());
607 [Category ("NotWorking")]
608 public void SerializeTest ()
610 MemoryStream input = new MemoryStream ();
611 byte [] bufferIn = Encoding.UTF8.GetBytes ("some test");
612 input.Write (bufferIn, 0, bufferIn.Length);
615 BinaryFormatter bf = new BinaryFormatter ();
616 MemoryStream ms = new MemoryStream ();
617 bf.Serialize (ms, input);
619 byte [] bufferOut = new byte [ms.Length];
621 ms.Read (bufferOut, 0, bufferOut.Length);
623 Assert.AreEqual (_serialized, bufferOut);
627 [Category ("NotWorking")]
628 public void DeserializeTest ()
630 MemoryStream ms = new MemoryStream ();
631 ms.Write (_serialized, 0, _serialized.Length);
634 BinaryFormatter bf = new BinaryFormatter ();
635 MemoryStream output = (MemoryStream) bf.Deserialize (ms);
636 using (StreamReader sr = new StreamReader (output)) {
637 Assert.AreEqual ("some test", sr.ReadToEnd ());
641 private static byte [] _serialized = new byte [] {
642 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
643 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
644 0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f, 0x2e,
645 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x72, 0x65, 0x61,
646 0x6d, 0x0a, 0x00, 0x00, 0x00, 0x07, 0x5f, 0x62, 0x75, 0x66, 0x66,
647 0x65, 0x72, 0x07, 0x5f, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x09,
648 0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x07, 0x5f,
649 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x09, 0x5f, 0x63, 0x61, 0x70,
650 0x61, 0x63, 0x69, 0x74, 0x79, 0x0b, 0x5f, 0x65, 0x78, 0x70, 0x61,
651 0x6e, 0x64, 0x61, 0x62, 0x6c, 0x65, 0x09, 0x5f, 0x77, 0x72, 0x69,
652 0x74, 0x61, 0x62, 0x6c, 0x65, 0x0a, 0x5f, 0x65, 0x78, 0x70, 0x6f,
653 0x73, 0x61, 0x62, 0x6c, 0x65, 0x07, 0x5f, 0x69, 0x73, 0x4f, 0x70,
654 0x65, 0x6e, 0x1d, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x42,
655 0x79, 0x52, 0x65, 0x66, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x2b,
656 0x5f, 0x5f, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x07,
657 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08,
658 0x08, 0x08, 0x08, 0x01, 0x01, 0x01, 0x01, 0x09, 0x02, 0x00, 0x00,
659 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
660 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x0a,
661 0x0f, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x73,
662 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x00, 0x00, 0x00,
663 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
664 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
665 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
666 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
667 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
668 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
669 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
670 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
671 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
672 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
673 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
674 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
675 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
676 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
677 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
678 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
679 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
680 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
681 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
682 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
683 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
684 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,