Ejemplo n.º 1
0
        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));
            }

            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 == TargetFrameworkMoniker.netfx461 || b.Platform == TargetFrameworkMoniker.netfx471);
                if (index >= 0)
                {
                    tmpList.RemoveAt(index);
                }

                blobs = tmpList.ToArray();
            }

            // We store our framework blobs in index 1
            int platformBlobIndex = TypeSerializableValue.GetPlatformIndex(blobs);

            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);
                }
            }
        }
Ejemplo n.º 2
0
        private static void SanityCheckBlob(object obj, TypeSerializableValue[] blobs)
        {
            // These types are unstable during serialization and produce different blobs.
            if (obj is WeakReference <Point> ||
                obj is Collections.Specialized.HybridDictionary)
            {
                return;
            }

            // In most cases exceptions in Core have a different layout than in Desktop,
            // therefore we are skipping the string comparison of the blobs.
            if (obj is Exception)
            {
                return;
            }

            // Check if runtime generated blob is the same as the stored one
            int frameworkBlobNumber = TypeSerializableValue.GetPlatformIndex(blobs);

            if (frameworkBlobNumber < blobs.Length)
            {
                string runtimeBlob = BinaryFormatterHelpers.ToBase64String(obj, FormatterAssemblyStyle.Full);

                string storedComparableBlob  = CreateComparableBlobInfo(blobs[frameworkBlobNumber].Base64Blob);
                string runtimeComparableBlob = CreateComparableBlobInfo(runtimeBlob);

                Assert.True(storedComparableBlob == runtimeComparableBlob,
                            $"The stored blob for type {obj.GetType().FullName} is outdated and needs to be updated.{Environment.NewLine}{Environment.NewLine}" +
                            $"-------------------- Stored blob ---------------------{Environment.NewLine}" +
                            $"Encoded: {blobs[frameworkBlobNumber].Base64Blob}{Environment.NewLine}" +
                            $"Decoded: {storedComparableBlob}{Environment.NewLine}{Environment.NewLine}" +
                            $"--------------- Runtime generated blob ---------------{Environment.NewLine}" +
                            $"Encoded: {runtimeBlob}{Environment.NewLine}" +
                            $"Decoded: {runtimeComparableBlob}");
            }
        }