namespace MonoTests.System.Runtime.Serialization.Formatters.Binary
{
- [TestFixture]
- public class BinarySerializationOverVersions {
-
- static readonly string dirName = typeof(VersionTolerantSerializationTestLib.Address).Namespace;
- static readonly string assemblyName = typeof(VersionTolerantSerializationTestLib.Address).Name;
- static readonly string assemblyFileName = assemblyName + ".dll";
- static readonly string binName = Path.GetFileName (Assembly.GetExecutingAssembly ().Location);
- const bool cleanup = true;
- enum OopOperation { Serialize, Deserialize }
- static IFormatter formatter = new BinaryFormatter ();
-
-
//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!)
- public virtual IFormatter Formatter { get { return formatter; } }
-
- static void Main (string [] args)
- {
- var p = new BinarySerializationOverVersions ();
-
- /* this block is useful for testing this without NUnit:
- if (args.Length == 0)
- {
- Console.WriteLine ("Starting...");
- p.TestDroppedField ();
- Console.Write ("x");
- p.TestAddedField ();
- Console.Write ("x");
-
- p.TestAddedFieldWithData ();
- Console.Write ("x");
- p.TestDroppedFieldWithData ();
- Console.Write ("x");
-
- p.TestAddedFieldWithOptionalAttrib ();
- Console.Write ("x");
- p.TestDroppedFieldWithOptionalAttrib ();
- Console.Write ("x");
-
- p.TestAddedFieldWithOptionalAttribAndData ();
- Console.Write ("x");
- p.TestDroppedFieldWithOptionalAttribAndData ();
- Console.Write ("x");
-
- Console.WriteLine ();
- Environment.Exit (0);
- }*/
-
- if (args.Length < 2)
- throw new Exception ("Please specify arguments");
-
- if (args [0] == OopOperation.Serialize.ToString ())
- {
- p.SerializeToFile (args [1]);
- }
- else if (args [0] == OopOperation.Deserialize.ToString ())
- {
- p.DeserializeFromFile (args [1]);
- }
- else
- {
- throw new Exception(String.Format ("{0} operation not recognized. Only {Serialize|Deserialize} operations are supported.", args [0]));
- }
- }
+ [TestFixture]
+ public class BinarySerializationOverVersions {
[Test]
public void TestDroppedField () //eliminate CountryCode
Deserialize ("6.0", Serialize ("5.0"));
}
- private static string Serialize (string assemblyVersion)
- {
- return SerializeOOP (SetEnvironment (assemblyVersion)); ;
- }
-
- private static void Deserialize (string assemblyVersion, string filename)
- {
- DeserializeOOP (SetEnvironment (assemblyVersion), filename);
- }
+ const string AssemblyName = "Address.dll";
+ const string TypeName = "VersionTolerantSerializationTestLib.Address";
- private static string SerializeOOP (string executionDir)
- {
- string filename = Path.GetTempFileName ();
- var p = new Process ();
- p.StartInfo.WorkingDirectory = executionDir;
- p.StartInfo.FileName = Path.Combine (executionDir, binName);
- p.StartInfo.Arguments = OopOperation.Serialize.ToString () + " \"" + filename + "\"";
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.EnvironmentVariables["PATH"] = Environment.GetEnvironmentVariable ("PATH");
- p.StartInfo.EnvironmentVariables["MONO_PATH"] = Environment.GetEnvironmentVariable ("MONO_PATH");
- p.Start();
- p.WaitForExit();
- if (p.ExitCode != 0)
- throw new Exception ("Problem in serialization operation");
+ class Serializer : MarshalByRefObject {
+ static IFormatter formatter = new BinaryFormatter ();
+ public static IFormatter Formatter { get { return formatter; } }
- if (cleanup)
- Directory.Delete (executionDir, true);
+ 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 ();
+ }
- return filename;
+ 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 void DeserializeOOP (string executionDir, string filename)
+ private static byte[] Serialize (string assemblyVersion)
{
- var p = new Process ();
- p.StartInfo.WorkingDirectory = executionDir;
- p.StartInfo.FileName = Path.Combine (executionDir, binName);
- p.StartInfo.Arguments = OopOperation.Deserialize.ToString () + " \"" + filename + "\"";
- p.StartInfo.UseShellExecute = false;
- p.Start ();
- p.WaitForExit ();
- if (p.ExitCode != 0)
- throw new Exception("Problem in deserialization operation");
-
- if (cleanup)
- {
- Directory.Delete (executionDir, true);
- File.Delete (filename);
- }
+ 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 string SetEnvironment (string assemblyVersion)
+ private static void Deserialize (string assemblyVersion, byte[] payload)
{
- if (!assemblyVersion.Contains ("."))
- throw new NotSupportedException ("The version number should contain a dot, i.e.: 2.0");
-
- string tmpDir = Path.Combine (Path.GetTempPath (), "deleteme" + Guid.NewGuid ());
- Directory.CreateDirectory (tmpDir);
- string currentBin = Assembly.GetExecutingAssembly ().Location;
- File.Copy (currentBin, Path.Combine (tmpDir, Path.GetFileName (currentBin)));
+ 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);
+ }
- string ass = Find (assemblyVersion);
- File.Copy (ass, Path.Combine (tmpDir, Path.GetFileName (ass)));
- return tmpDir;
+ static void Main () {
+ throw new Exception ();
}
+
private static string Find (string assemblyVersion)
{
string initDir = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
private static string Find (string assemblyVersion, string path)
{
- return Path.Combine (Path.Combine (path, assemblyVersion), assemblyFileName);
-/*
- Console.WriteLine ("Looking in " + path);
- string test;
-
- if (!path.Contains(assemblyVersion))
- {
- //outside or here
-
- if (Path.GetFileName(path) == dirName)
- {
- test = Path.Combine (path, assemblyVersion);
- if (Directory.Exists (test))
- return Find (assemblyVersion, test);
- else
- throw new DirectoryNotFoundException (String.Format ("{0} was not found", test));
- }
-
- test = Path.Combine (path, dirName);
- if (Directory.Exists (test))
- return Find (assemblyVersion, test);
-
- return Find (assemblyVersion, Path.Combine (path, ".."));
- }
- else
- {
- //inside
- test = Path.Combine (path, assemblyFileName);
- if (File.Exists (test))
- return test;
-
- test = Path.Combine (path, "bin");
- if (Directory.Exists (test))
- return Find (assemblyVersion, test);
-
- test = Path.Combine (path, "Debug");
- if (Directory.Exists (test))
- return Find (assemblyVersion, test);
-
- test = Path.Combine (path, "Release");
- if (Directory.Exists (test))
- return Find (assemblyVersion, test);
-
- test = Path.Combine (path.Replace ("Debug", "Release"), assemblyFileName);
- if (File.Exists (test))
- return test;
-
- throw new NotSupportedException(
- String.Format(
- "The tree is not predictible according to the philosophy of the test. (Stuck in {0})",
- path));
- }
-*/
- }
-
- private void SerializeToFile (string filename)
- {
- var type = typeof (VersionTolerantSerializationTestLib.Address);
- object obj = Activator.CreateInstance (type);
- Stream stream = new FileStream (filename,
- FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
- Formatter.Serialize (stream, obj);
- stream.Dispose ();
- }
-
- private void DeserializeFromFile (string filename)
- {
- //Console.WriteLine("Trying to deserialize {0}...", filename);
- FileStream readStream = new FileStream (filename, FileMode.Open);
- //var readData =
- Formatter.Deserialize (readStream);
+ return Path.Combine (Path.Combine (path, assemblyVersion), AssemblyName);
}
}
}