private static void ValidateAndRoundtrip(object obj, TypeSerializableValue[] blobs, bool isEqualityComparer) { if (obj == null) { throw new ArgumentNullException("The serializable object must not be null", nameof(obj)); } if (blobs == null || blobs.Length == 0) { throw new ArgumentOutOfRangeException($"Type {obj} has no blobs to deserialize and test equality against. Blob: " + BinaryFormatterHelpers.ToBase64String(obj, FormatterAssemblyStyle.Full)); } // Check if the passed in value in a serialization entry is assignable by the passed in type. if (obj is ISerializable customSerializableObj && HasObjectTypeIntegrity(customSerializableObj)) { CheckObjectTypeIntegrity(customSerializableObj); } SanityCheckBlob(obj, blobs); // SqlException, ReflectionTypeLoadException and LicenseException aren't deserializable from Desktop --> Core. // Therefore we remove the second blob which is the one from Desktop. if (!PlatformDetection.IsFullFramework && (obj is SqlException || obj is ReflectionTypeLoadException || obj is LicenseException)) { var tmpList = new List <TypeSerializableValue>(blobs); tmpList.RemoveAt(1); int index = tmpList.FindIndex(b => b.Platform.IsNetfxPlatform()); if (index >= 0) { tmpList.RemoveAt(index); } blobs = tmpList.ToArray(); } // We store our framework blobs in index 1 int platformBlobIndex = blobs.GetPlatformIndex(); for (int i = 0; i < blobs.Length; i++) { // Check if the current blob is from the current running platform. bool isSamePlatform = i == platformBlobIndex; if (isEqualityComparer) { ValidateEqualityComparer(BinaryFormatterHelpers.FromBase64String(blobs[i].Base64Blob, FormatterAssemblyStyle.Simple)); ValidateEqualityComparer(BinaryFormatterHelpers.FromBase64String(blobs[i].Base64Blob, FormatterAssemblyStyle.Full)); } else { EqualityExtensions.CheckEquals(obj, BinaryFormatterHelpers.FromBase64String(blobs[i].Base64Blob, FormatterAssemblyStyle.Simple), isSamePlatform); EqualityExtensions.CheckEquals(obj, BinaryFormatterHelpers.FromBase64String(blobs[i].Base64Blob, FormatterAssemblyStyle.Full), isSamePlatform); } } }
public void ArraySegmentDefaultCtor() { // This is workaround for Xunit bug which tries to pretty print test case name and enumerate this object. // When inner array is not initialized it throws an exception when this happens. object obj = new ArraySegment <int>(); string corefxBlob = "AAEAAAD/////AQAAAAAAAAAEAQAAAHJTeXN0ZW0uQXJyYXlTZWdtZW50YDFbW1N5c3RlbS5JbnQzMiwgbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0DAAAABl9hcnJheQdfb2Zmc2V0Bl9jb3VudAcAAAgICAoAAAAAAAAAAAs="; string netfxBlob = "AAEAAAD/////AQAAAAAAAAAEAQAAAHJTeXN0ZW0uQXJyYXlTZWdtZW50YDFbW1N5c3RlbS5JbnQzMiwgbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0DAAAABl9hcnJheQdfb2Zmc2V0Bl9jb3VudAcAAAgICAoAAAAAAAAAAAs="; EqualityExtensions.CheckEquals(obj, BinaryFormatterHelpers.FromBase64String(corefxBlob, FormatterAssemblyStyle.Full), isSamePlatform: true); EqualityExtensions.CheckEquals(obj, BinaryFormatterHelpers.FromBase64String(netfxBlob, FormatterAssemblyStyle.Full), isSamePlatform: true); }
public void ValidateBasicObjectsRoundtrip(object obj, FormatterAssemblyStyle assemblyFormat, TypeFilterLevel filterLevel, FormatterTypeStyle typeFormat) { object clone = BinaryFormatterHelpers.Clone(obj, null, assemblyFormat, filterLevel, typeFormat); // string.Empty and DBNull are both singletons if (!ReferenceEquals(obj, string.Empty) && !(obj is DBNull)) { Assert.NotSame(obj, clone); } EqualityExtensions.CheckEquals(obj, clone, isSamePlatform: true); }
public void RoundtripManyObjectsInOneStream() { object[][] objects = SerializableObjects_MemberData().ToArray(); var s = new MemoryStream(); var f = new BinaryFormatter(); foreach (object[] obj in objects) { f.Serialize(s, obj[0]); } s.Position = 0; foreach (object[] obj in objects) { object clone = f.Deserialize(s); EqualityExtensions.CheckEquals(obj[0], clone, isSamePlatform: true); } }
private static void ValidateAndRoundtrip(object obj, string[] blobs, bool isEqualityComparer) { if (obj == null) { throw new ArgumentNullException("The serializable object must not be null", nameof(obj)); } if (blobs == null || blobs.Length == 0) { throw new ArgumentOutOfRangeException($"Type {obj} has no blobs to deserialize and test equality against. Blob: " + BinaryFormatterHelpers.ToBase64String(obj, FormatterAssemblyStyle.Full)); } SanityCheckBlob(obj, blobs); // SqlException isn't deserializable from Desktop --> Core. // Therefore we remove the second blob which is the one from Desktop. if (!PlatformDetection.IsFullFramework && (obj is SqlException || obj is ReflectionTypeLoadException || obj is LicenseException)) { var tmpList = new List <string>(blobs); tmpList.RemoveAt(1); blobs = tmpList.ToArray(); } // We store our framework blobs in index 1 int platformBlobIndex = PlatformDetection.IsFullFramework ? 1 : 0; for (int i = 0; i < blobs.Length; i++) { // Check if the current blob is from the current running platform. bool isSamePlatform = i == platformBlobIndex; if (isEqualityComparer) { ValidateEqualityComparer(BinaryFormatterHelpers.FromBase64String(blobs[i], FormatterAssemblyStyle.Simple)); ValidateEqualityComparer(BinaryFormatterHelpers.FromBase64String(blobs[i], FormatterAssemblyStyle.Full)); } else { EqualityExtensions.CheckEquals(obj, BinaryFormatterHelpers.FromBase64String(blobs[i], FormatterAssemblyStyle.Simple), isSamePlatform); EqualityExtensions.CheckEquals(obj, BinaryFormatterHelpers.FromBase64String(blobs[i], FormatterAssemblyStyle.Full), isSamePlatform); } } }
private static void ValidateAndRoundtrip(object obj, TypeSerializableValue[] blobs, bool isEqualityComparer) { if (obj == null) { throw new ArgumentNullException("The serializable object must not be null", nameof(obj)); } if (blobs == null || blobs.Length == 0) { throw new ArgumentOutOfRangeException($"Type {obj} has no blobs to deserialize and test equality against. Blob: " + BinaryFormatterHelpers.ToBase64String(obj, FormatterAssemblyStyle.Full)); } // Check if the passed in value in a serialization entry is assignable by the passed in type. if (obj is ISerializable customSerializableObj && HasObjectTypeIntegrity(customSerializableObj)) { CheckObjectTypeIntegrity(customSerializableObj); } // TimeZoneInfo objects have three properties (DisplayName, StandardName, DaylightName) // that are localized. Since the blobs were generated from the invariant culture, they // will have English strings embedded. Thus, we can only test them against English // language cultures or the invariant culture. if (obj is TimeZoneInfo && ( CultureInfo.CurrentUICulture.TwoLetterISOLanguageName != "en" || CultureInfo.CurrentUICulture.Name.Length != 0)) { return; } SanityCheckBlob(obj, blobs); // ReflectionTypeLoadException and LicenseException aren't deserializable from Desktop --> Core. // Therefore we remove the second blob which is the one from Desktop. if (!PlatformDetection.IsNetFramework && (obj is ReflectionTypeLoadException || obj is LicenseException)) { var tmpList = new List <TypeSerializableValue>(blobs); tmpList.RemoveAt(1); int index = tmpList.FindIndex(b => b.Platform.ToString().StartsWith("netfx")); if (index >= 0) { tmpList.RemoveAt(index); } blobs = tmpList.ToArray(); } // We store our framework blobs in index 1 int platformBlobIndex = blobs.GetPlatformIndex(); for (int i = 0; i < blobs.Length; i++) { // Check if the current blob is from the current running platform. bool isSamePlatform = i == platformBlobIndex; if (isEqualityComparer) { ValidateEqualityComparer(BinaryFormatterHelpers.FromBase64String(blobs[i].Base64Blob, FormatterAssemblyStyle.Simple)); ValidateEqualityComparer(BinaryFormatterHelpers.FromBase64String(blobs[i].Base64Blob, FormatterAssemblyStyle.Full)); } else { EqualityExtensions.CheckEquals(obj, BinaryFormatterHelpers.FromBase64String(blobs[i].Base64Blob, FormatterAssemblyStyle.Simple), isSamePlatform); EqualityExtensions.CheckEquals(obj, BinaryFormatterHelpers.FromBase64String(blobs[i].Base64Blob, FormatterAssemblyStyle.Full), isSamePlatform); } } }
private static void CheckForAnyEquals(object obj, object deserializedObj) { Assert.True(EqualityExtensions.CheckEquals(obj, deserializedObj), "Error during equality check of type " + obj?.GetType()?.FullName); }