In Test/System.IO:
[mono.git] / mcs / class / corlib / Test / System.IO / MemoryStreamTest.cs
index b6cc40f9e4f3907a123dc88078f0c2f99e4f2b9d..88ad55cab9f50c9be11458b5f4a04a4728c4495f 100644 (file)
 // Copyright (C) 2004 Novell (http://www.novell.com)
 //
 
-using NUnit.Framework;
-using System.IO;
 using System;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
 using System.Text;
 
+using NUnit.Framework;
+
 namespace MonoTests.System.IO
 {
        [TestFixture]
@@ -225,7 +227,7 @@ namespace MonoTests.System.IO
                        ms.Seek (0, SeekOrigin.Begin); 
                        testStream.Read (readBytes, 0, 100);
                        VerifyTestData ("W1", readBytes, 0, 100);
-               }               
+               }
 
                [Test]
                public void WriteBlock ()
@@ -425,12 +427,15 @@ namespace MonoTests.System.IO
                }
 
                [Test]
-               [ExpectedException (typeof (ObjectDisposedException))]
                public void Seek_Disposed () 
                {
                        MemoryStream ms = new MemoryStream ();
                        ms.Close ();
-                       ms.Seek (0, SeekOrigin.Begin);
+                       try {
+                               ms.Seek (0, SeekOrigin.Begin);
+                               Assert.Fail ();
+                       } catch (ObjectDisposedException) {
+                       }
                }
 
                [Test]
@@ -455,6 +460,32 @@ namespace MonoTests.System.IO
                        ms.SetLength (10);
                }
 
+               [Test] // bug #327053
+               [Category ("NotWorking")]
+               public void ZeroingOnExpand ()
+               {
+                       byte [] values = { 3, 2, 1 };
+                       byte [] reference = { 3, 2, 1 };
+                       byte [] cropped = { 3, 0, 0 };
+                       MemoryStream ms = new MemoryStream (values);
+                       Assert.AreEqual (values, reference, "#A1");
+                       ms.Seek (3, SeekOrigin.Begin);
+                       Assert.AreEqual (reference, values, "#A2");
+                       ms.SetLength (1);
+                       Assert.AreEqual (reference, values, "#B1");
+                       byte [] read = new byte [5];
+                       ms.Read (read, 0, 5);
+                       Assert.AreEqual (new byte [] { 0, 0, 0, 0, 0 }, read, "#B2");
+                       Assert.AreEqual (reference, values, "#B3");
+                       ms.SetLength (3);
+                       Assert.AreEqual (cropped, values, "#C1");
+                       ms.Seek (0, SeekOrigin.Begin);
+                       read = new byte [3];
+                       ms.Read (read, 0, 3);
+                       Assert.AreEqual (cropped, read, "#C2");
+                       Assert.AreEqual (cropped, values, "#C3");
+               }
+
                [Test]
                [ExpectedException (typeof (NotSupportedException))]
                public void WriteNonWritable ()
@@ -600,6 +631,114 @@ namespace MonoTests.System.IO
                        MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
                        Assert.IsNotNull (ms.GetBuffer ());
                }
+
+               [Test] // bug #350860
+               public void ToArray_Empty ()
+               {
+                       MemoryStream ms = new MemoryStream (1);
+                       ms.Capacity = 0;
+                       ms.ToArray ();
+               }
+
+               [Test] // bug #80205
+               [Category ("NotWorking")]
+               public void SerializeTest ()
+               {
+                       MemoryStream input = new MemoryStream ();
+                       byte [] bufferIn = Encoding.UTF8.GetBytes ("some test");
+                       input.Write (bufferIn, 0, bufferIn.Length);
+                       input.Position = 0;
+
+                       BinaryFormatter bf = new BinaryFormatter ();
+                       MemoryStream ms = new MemoryStream ();
+                       bf.Serialize (ms, input);
+
+                       byte [] bufferOut = new byte [ms.Length];
+                       ms.Position = 0;
+                       ms.Read (bufferOut, 0, bufferOut.Length);
+
+                       Assert.AreEqual (_serialized, bufferOut);
+               }
+
+               [Test] // bug #80205
+               [Category ("NotWorking")]
+               public void DeserializeTest ()
+               {
+                       MemoryStream ms = new MemoryStream ();
+                       ms.Write (_serialized, 0, _serialized.Length);
+                       ms.Position = 0;
+
+                       BinaryFormatter bf = new BinaryFormatter ();
+                       MemoryStream output = (MemoryStream) bf.Deserialize (ms);
+                       using (StreamReader sr = new StreamReader (output)) {
+                               Assert.AreEqual ("some test", sr.ReadToEnd ());
+                       }
+               }
+
+               private static byte [] _serialized = new byte [] {
+                       0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
+                       0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f, 0x2e,
+                       0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x72, 0x65, 0x61,
+                       0x6d, 0x0a, 0x00, 0x00, 0x00, 0x07, 0x5f, 0x62, 0x75, 0x66, 0x66,
+                       0x65, 0x72, 0x07, 0x5f, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x09,
+                       0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x07, 0x5f,
+                       0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x09, 0x5f, 0x63, 0x61, 0x70,
+                       0x61, 0x63, 0x69, 0x74, 0x79, 0x0b, 0x5f, 0x65, 0x78, 0x70, 0x61,
+                       0x6e, 0x64, 0x61, 0x62, 0x6c, 0x65, 0x09, 0x5f, 0x77, 0x72, 0x69,
+                       0x74, 0x61, 0x62, 0x6c, 0x65, 0x0a, 0x5f, 0x65, 0x78, 0x70, 0x6f,
+                       0x73, 0x61, 0x62, 0x6c, 0x65, 0x07, 0x5f, 0x69, 0x73, 0x4f, 0x70,
+                       0x65, 0x6e, 0x1d, 0x4d, 0x61, 0x72, 0x73, 0x68, 0x61, 0x6c, 0x42,
+                       0x79, 0x52, 0x65, 0x66, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x2b,
+                       0x5f, 0x5f, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x07,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08,
+                       0x08, 0x08, 0x08, 0x01, 0x01, 0x01, 0x01, 0x09, 0x02, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
+                       0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x0a,
+                       0x0f, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x73,
+                       0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       0x00, 0x00, 0x0b };
+#if NET_2_0
+               class MyMemoryStream : MemoryStream {
+
+                       public bool DisposedCalled = false;
+
+                       protected override void Dispose(bool disposing)
+                       {
+                               DisposedCalled = true;
+                       }
+               }
+
+               [Test] // https://bugzilla.novell.com/show_bug.cgi?id=322672
+               public void BaseDisposeCalled ()
+               {
+                       MyMemoryStream ms = new MyMemoryStream ();
+                       Assert.IsFalse (ms.DisposedCalled, "Before");
+                       ms.Close ();
+                       Assert.IsTrue (ms.DisposedCalled, "After");
+               }
+#endif
        }
 }
-