Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,140 @@
//
// BinaryFormatterCas.cs - CAS unit tests for
// System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.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.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security;
using System.Security.Permissions;
using MonoTests.System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
namespace MonoCasTests.System.Runtime.Serialization.Formatters.Binary {
[TestFixture]
[Category ("CAS")]
public class BinaryFormatterCas {
private BinaryFormatterTest unit;
private Stream stream;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
// executes at full trust
unit = new BinaryFormatterTest ();
stream = unit.GetSerializedStream ();
}
[SetUp]
public virtual void SetUp ()
{
if (!SecurityManager.SecurityEnabled)
Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
stream.Position = 0;
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void ReuseUnitTest ()
{
unit.Constructor_Default ();
unit.Constructor ();
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
public void Serialization_Deny_Unrestricted ()
{
unit.GetSerializedStream ();
}
[Test]
[SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
public void Serialization_PermitOnly_SerializationFormatter ()
{
Assert.IsNotNull (unit.GetSerializedStream ());
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
public void Deserialization_Deny_Unrestricted ()
{
BinaryFormatter bf = new BinaryFormatter ();
SerializationTest clone = (SerializationTest) bf.Deserialize (stream);
Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
Assert.IsFalse (clone.Boolean, "Boolean");
}
[Test]
[SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
public void Deserialization_PermitOnly_SerializationFormatter ()
{
BinaryFormatter bf = new BinaryFormatter ();
SerializationTest clone = (SerializationTest) bf.Deserialize (stream);
Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
Assert.IsFalse (clone.Boolean, "Boolean");
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
// no SecurityException here because a LinkDemand is used
public void UnsafeDeserialization_Deny_Unrestricted ()
{
BinaryFormatter bf = new BinaryFormatter ();
SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (stream, null);
Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
Assert.IsFalse (clone.Boolean, "Boolean");
}
[Test]
[SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
public void UnsafeDeserialization_PermitOnly_SerializationFormatter ()
{
BinaryFormatter bf = new BinaryFormatter ();
SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (stream, null);
Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
Assert.IsFalse (clone.Boolean, "Boolean");
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void LinkDemand_Deny_Unrestricted ()
{
ConstructorInfo ci = typeof (BinaryFormatter).GetConstructor (new Type[0]);
Assert.IsNotNull (ci, "default .ctor()");
Assert.IsNotNull (ci.Invoke (null), "invoke");
}
}
}

View File

@ -0,0 +1,21 @@
2007-12-29 Gert Driesen <drieseng@users.sourceforge.net>
* BinaryFormatterTest.cs: Added test for inherited fields. Code
formatting.
2006-09-06 Lluis Sanchez gual <lluis@novell.com>
* BinaryFormatterTest.cs: Added test for bug #78749 (nested
IObjectReference objects).
2006-09-05 Raja R Harinath <rharinath@novell.com>
* BinaryFormatterTest.cs (DateTimeArray): New test for
round-tripping DateTime arrays.
2005-12-07 Sebastien Pouliot <sebastien@ximian.com>
* BinaryFormatterCas.cs: New. Partial CAS tests for BinaryFormatter.
Added to test UnsafeDeserialization.
* BinaryFormatterTest.cs: New. Partial Unit tests for BinaryFormatter.
Added to test UnsafeDeserialization.

View File

@ -0,0 +1,141 @@
using System;
using System.Collections;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
namespace MonoTests.System.Runtime.Serialization.Formatters.Binary
{
//TODO: add tests that use SoapFormatter (if you go this
// path, it would be interesting also to test a
// custom formatter, like the XML-RPC.net one!)
[TestFixture]
public class BinarySerializationOverVersions {
[Test]
public void TestDroppedField () //eliminate CountryCode
{
Deserialize ("2.0", Serialize ("3.0"));
}
[Test]
public void TestAddedField () //add CountryCode
{
Deserialize ("3.0", Serialize ("2.0"));
}
[Test]
public void TestAddedFieldWithData () //add Country
{
Deserialize( "1.0", Serialize ("2.0"));
}
[Test]
public void TestDroppedFieldWithData () //eliminate Country
{
Deserialize ("2.0", Serialize ("3.0"));
}
[Test]
public void TestAddedFieldWithOptionalAttrib () //add PostCode
{
Deserialize ("4.0", Serialize ("3.0"));
}
[Test]
public void TestDroppedFieldWithOptionalAttrib () //eliminate PostCode
{
Deserialize ("3.0", Serialize ("4.0"));
}
[Test]
public void TestAddedFieldWithOptionalAttribAndData () //add AreaCode
{
Deserialize ("5.0", Serialize ("4.0"));
}
[Test]
public void TestDroppedFieldWithOptionalAttribAndData () //eliminate AreaCode
{
Deserialize ("4.0", Serialize ("5.0"));
}
[Test]
public void TestDroppedPrimitiveTypeField() //eliminate Id (int)
{
Deserialize ("5.0", Serialize ("6.0"));
}
[Test]
public void TestAddedPrimitiveTypeField () //add Id (int)
{
Deserialize ("6.0", Serialize ("5.0"));
}
const string AssemblyName = "Address.dll";
const string TypeName = "VersionTolerantSerializationTestLib.Address";
class Serializer : MarshalByRefObject {
static IFormatter formatter = new BinaryFormatter ();
public static IFormatter Formatter { get { return formatter; } }
public byte[] Serialize (string version) {
var assembly = Assembly.LoadFrom (Find (version));
var type = assembly.GetType (TypeName);
var obj = Activator.CreateInstance (type);
var stream = new MemoryStream ();
Formatter.Serialize (stream, obj);
return stream.ToArray ();
}
public void Deserialize (string version, byte[] payload) {
var assembly = Assembly.LoadFrom (Find (version));
var stream = new MemoryStream (payload);
var obj = Formatter.Deserialize (stream);
//Console.WriteLine ("obj version {0} -> {1}", version, obj);
}
}
private static byte[] Serialize (string assemblyVersion)
{
var setup = new AppDomainSetup ();
setup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
AppDomain ad = AppDomain.CreateDomain (assemblyVersion, null, setup);
Serializer ser = (Serializer) ad.CreateInstanceAndUnwrap (typeof(Serializer).Assembly.FullName, typeof(Serializer).FullName);
byte[] stuff = ser.Serialize (assemblyVersion);
AppDomain.Unload (ad);
return stuff;
}
private static void Deserialize (string assemblyVersion, byte[] payload)
{
var setup = new AppDomainSetup ();
setup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
AppDomain ad = AppDomain.CreateDomain (assemblyVersion, null, setup);
Serializer ser = (Serializer) ad.CreateInstanceAndUnwrap (typeof(Serializer).Assembly.FullName, typeof(Serializer).FullName);
ser.Deserialize (assemblyVersion, payload);
AppDomain.Unload (ad);
}
static void Main () {
throw new Exception ();
}
private static string Find (string assemblyVersion)
{
string initDir = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
return Find (assemblyVersion, initDir);
}
private static string Find (string assemblyVersion, string path)
{
return Path.Combine (Path.Combine (path, assemblyVersion), AssemblyName);
}
}
}

View File

@ -0,0 +1,16 @@
using System;
namespace VersionTolerantSerializationTestLib
{
[Serializable]
public class Address
{
private string Street = "v1-street";
private string City = "v1-city";
private string Country = "v1-country";
public override string ToString () {
return String.Format ("v1 obj {0} {1} {2}", Street, City, Country);
}
}
}

View File

@ -0,0 +1,15 @@
using System;
namespace VersionTolerantSerializationTestLib
{
[Serializable]
public class Address
{
private string Street = "v2-Street";
private string City = "v2-City";
public override string ToString () {
return String.Format ("v2 obj {0} {1}", Street, City);
}
}
}

View File

@ -0,0 +1,16 @@
using System;
namespace VersionTolerantSerializationTestLib
{
[Serializable]
public class Address
{
private string Street = "v3-Street";
private string City = "v3-City";
private string CountryCode = "v3-CountryCode";
public override string ToString () {
return String.Format ("v3 obj {0} {1} {2}", Street, City, CountryCode);
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Runtime.Serialization;
namespace VersionTolerantSerializationTestLib
{
[Serializable]
public class Address
{
private string Street = "v4-Street";
private string City = "v4-City";
private string CountryCode = "v4-CountryCode";
[OptionalField (VersionAdded = 4)]
private string PostCode;
public override string ToString () {
return String.Format ("v4 obj {0} {1} {2}", Street, City, CountryCode);
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Runtime.Serialization;
namespace VersionTolerantSerializationTestLib
{
[Serializable]
public class Address
{
private string Street = "v5-Street";
private string City = "v5-City";
private string CountryCode = "v5-CountryCode";
[OptionalField (VersionAdded = 4)]
private string PostCode;
[OptionalField (VersionAdded = 5)]
private string AreaCode = "5";
public override string ToString () {
return String.Format ("v5 obj {0} {1} {2}", Street, City, CountryCode);
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Runtime.Serialization;
namespace VersionTolerantSerializationTestLib
{
[Serializable]
public class Address
{
private string Street = "v6-Street";
private string City = "v6-City";
private string CountryCode = "v6-CountryCode";
[OptionalField (VersionAdded = 4)]
private string PostCode;
[OptionalField (VersionAdded = 5)]
private string AreaCode = "6";
[OptionalField (VersionAdded = 6)]
private int Id = 6;
public override string ToString () {
return String.Format ("v6 obj {0} {1} {2}", Street, City, CountryCode);
}
}
}