Bug #5829: DataContractSerializer should not check for invalid characters.
authorMartin Baulig <martin.baulig@xamarin.com>
Fri, 22 Mar 2013 00:00:56 +0000 (01:00 +0100)
committerMartin Baulig <martin.baulig@xamarin.com>
Fri, 22 Mar 2013 00:00:56 +0000 (01:00 +0100)
mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlObjectSerializer.cs
mcs/class/System.Runtime.Serialization/System.Runtime.Serialization_test.dll.sources
mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/DataContractSerializerTest_InvalidCharacters.cs [new file with mode: 0644]

index 9c0319d943382ef8b982633dbfe375cf037e64bf..2abb7663fed0fb002c3a3d00223a1fd65a5c9dec 100644 (file)
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-#if NET_2_0
 using System;
 using System.Collections;
 using System.IO;
+using System.Text;
 using System.Reflection;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Xml;
@@ -61,7 +61,9 @@ namespace System.Runtime.Serialization
 
                public virtual object ReadObject (Stream stream)
                {
-                       return ReadObject (XmlReader.Create (stream));
+                       var settings = new XmlReaderSettings ();
+                       settings.CheckCharacters = false;
+                       return ReadObject (XmlReader.Create (stream, settings));
                }
 
                public virtual object ReadObject (XmlReader reader)
@@ -86,7 +88,12 @@ namespace System.Runtime.Serialization
 
                public virtual void WriteObject (Stream stream, object graph)
                {
-                       using (XmlWriter xw = XmlDictionaryWriter.CreateTextWriter (stream)) {
+                       var settings = new XmlWriterSettings ();
+                       settings.Encoding = Encoding.UTF8;
+                       settings.CloseOutput = false;
+                       settings.OmitXmlDeclaration = true;
+                       settings.CheckCharacters = false;
+                       using (XmlWriter xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (stream, settings))) {
                                WriteObject (xw, graph);
                        }
                }
@@ -130,4 +137,3 @@ namespace System.Runtime.Serialization
                        XmlDictionaryWriter writer);
        }
 }
-#endif
index aa860040ea37f40861adb0f20c5fbf723d2c9467..9c52901aa8bac2b12b420ddf47f0cd41343119b6 100644 (file)
@@ -7,6 +7,7 @@ System.Runtime.Serialization/Bug695203Test.cs
 System.Runtime.Serialization/DataContractResolverTest.cs
 System.Runtime.Serialization/DataContractSerializerTest_DuplicateQName.cs
 System.Runtime.Serialization/DataContractSerializerTest_NullableWithDictionary.cs
+System.Runtime.Serialization/DataContractSerializerTest_InvalidCharacters.cs
 System.Runtime.Serialization/KnownTypeAttributeTest.cs
 System.Runtime.Serialization/XmlObjectSerializerTest.cs
 System.Runtime.Serialization/XsdDataContractExporterTest.cs
diff --git a/mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/DataContractSerializerTest_InvalidCharacters.cs b/mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/DataContractSerializerTest_InvalidCharacters.cs
new file mode 100644 (file)
index 0000000..260c3e3
--- /dev/null
@@ -0,0 +1,71 @@
+//
+// DataContractSerializerTest_InvalidCharacters.cs
+//
+// Author:
+//       Martin Baulig <martin.baulig@xamarin.com>
+//
+// Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using System.IO;
+using System.Text;
+using System.Runtime.Serialization;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Runtime.Serialization
+{
+       [TestFixture]
+       public class DataContractSerializerTest_InvalidCharacters
+       {
+               [Serializable]
+               public class MyData
+               {
+                       public string Text;
+               }
+
+               [Test]
+               public void Test ()
+               {
+                       var data = new MyData
+                       {
+                               Text = "Test " + ASCIIEncoding.ASCII.GetString (new byte[] { 0x06 })
+                       };
+
+                       var serializer = new DataContractSerializer (typeof(MyData), "MyData", string.Empty);
+
+                       string serialized;
+                       using (var ms = new MemoryStream ()) {
+                               serializer.WriteObject (ms, data);
+                               serialized = new string (Encoding.UTF8.GetChars (ms.GetBuffer ()));
+
+                               Assert.IsTrue (serialized.Contains ("Test &#x6;"), "#1");
+
+                               ms.Seek (0, SeekOrigin.Begin);
+
+                               var data2 = (MyData)serializer.ReadObject (ms);
+                               Assert.AreEqual (data2.Text.Length, 6, "#2");
+                               Assert.AreEqual (data2.Text [5], (char)0x06, "#3");
+                       }
+               }
+
+               
+       }
+}