Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d1e38e7

Browse files
committed
[WIP] BinaryFormatter - Reenable sanity check (#21507)
* Reenable sanity check * Avoid different blobs for CookieContainer dependent on machine configuration * Fixing diffent blobs for CompareInfo as it used machine config depdent values * Fixing different in DataTable and DataSet * Fixing CookieCollection missing field in serialization
1 parent 2f2fff8 commit d1e38e7

File tree

4 files changed

+58
-47
lines changed

4 files changed

+58
-47
lines changed

src/System.Net.Primitives/src/System/Net/CookieCollection.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Runtime.Serialization;
78

89
namespace System.Net
910
{
@@ -24,9 +25,7 @@ internal enum Stamp
2425

2526
private readonly ArrayList m_list = new ArrayList();
2627

27-
#pragma warning disable 0414
28-
private int m_version = 0; // Do not rename (binary serialization). This field only exists for netfx serialization compatibility.
29-
#pragma warning restore 0414
28+
private int m_version; // Do not rename (binary serialization). This field only exists for netfx serialization compatibility.
3029
private DateTime m_TimeStamp = DateTime.MinValue; // Do not rename (binary serialization)
3130
private bool m_has_other_versions; // Do not rename (binary serialization)
3231

@@ -61,6 +60,12 @@ public Cookie this[string name]
6160
}
6261
}
6362

63+
[OnSerializing]
64+
private void OnSerializing(StreamingContext context)
65+
{
66+
m_version = m_list.Count;
67+
}
68+
6469
public void Add(Cookie cookie)
6570
{
6671
if (cookie == null)

src/System.Runtime.Serialization.Formatters/tests/BinaryFormatterHelper.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ public static void ValidateEqualityComparer(object obj)
127127
Assert.Equal(obj.GetType().GetGenericArguments()[0], objType.GetGenericArguments()[0]);
128128
}
129129

130+
private static void SanityCheckBlob(object obj, string[] blobs)
131+
{
132+
// Check if runtime generated blob is the same as the stored one
133+
int frameworkBlobNumber = PlatformDetection.IsFullFramework ? 1 : 0;
134+
if (frameworkBlobNumber < blobs.Length &&
135+
// WeakReference<Point> and HybridDictionary with default constructor are generating
136+
// different blobs at runtime for some obscure reason. Excluding those from the check.
137+
!(obj is WeakReference<Point>) &&
138+
!(obj is Collections.Specialized.HybridDictionary))
139+
{
140+
string runtimeBlob = SerializeObjectToBlob(obj, FormatterAssemblyStyle.Full);
141+
142+
string storedComparableBlob = CreateComparableBlobInfo(blobs[frameworkBlobNumber]);
143+
string runtimeComparableBlob = CreateComparableBlobInfo(runtimeBlob);
144+
145+
Assert.True(storedComparableBlob == runtimeComparableBlob,
146+
$"The stored blob for type {obj.GetType().FullName} is outdated and needs to be updated.{Environment.NewLine}Stored blob: {blobs[frameworkBlobNumber]}{Environment.NewLine}Generated runtime blob: {runtimeBlob}");
147+
}
148+
}
149+
130150
public static string GetTestDataFilePath()
131151
{
132152
string GetRepoRootPath()
@@ -171,9 +191,18 @@ public static IEnumerable<string> GetCoreTypeBlobs(IEnumerable<object[]> records
171191

172192
public static string CreateComparableBlobInfo(string base64Blob)
173193
{
194+
string lineSeparator = ((char)0x2028).ToString();
195+
string paragraphSeparator = ((char)0x2029).ToString();
196+
174197
byte[] data = Convert.FromBase64String(base64Blob);
175-
string decodedString = Encoding.UTF8.GetString(data);
176-
return Regex.Replace(decodedString, @"Version=\d.\d.\d.\d.", "Version=0.0.0.0", RegexOptions.Multiline);
198+
base64Blob = Encoding.UTF8.GetString(data);
199+
200+
return Regex.Replace(base64Blob, @"Version=\d.\d.\d.\d.", "Version=0.0.0.0", RegexOptions.Multiline)
201+
.Replace("\r\n", string.Empty)
202+
.Replace("\n", string.Empty)
203+
.Replace("\r", string.Empty)
204+
.Replace(lineSeparator, string.Empty)
205+
.Replace(paragraphSeparator, string.Empty);
177206
}
178207

179208
public static (int blobs, int foundBlobs, int updatedBlobs) UpdateCoreTypeBlobs(string testDataFilePath, string[] blobs)

0 commit comments

Comments
 (0)