-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reviewed classes that participate in binary serialization
- Loading branch information
1 parent
afa8a07
commit 46f1f71
Showing
74 changed files
with
1,513 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/Common/tests/InternalUtilitiesForTests/src/BinarySerialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.Serialization.Formatters; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using Xunit; | ||
|
||
namespace System | ||
{ | ||
public static class BinarySerialization | ||
{ | ||
public static void EnsureSerializableAttribute(Assembly assemblyUnderTest, Dictionary<string, bool> serializableTypes) | ||
{ | ||
foreach (Type type in assemblyUnderTest.GetTypes()) | ||
{ | ||
var serializableAttribute = Attribute.GetCustomAttribute(type, typeof(SerializableAttribute)); | ||
|
||
// Ensure that all types required by known serialization scenarions | ||
// and only these types are decorated with the SerializableAttribute. | ||
if (serializableTypes.ContainsKey(type.FullName)) | ||
{ | ||
Assert.NotNull(serializableAttribute); | ||
serializableTypes[type.FullName] = true; | ||
} | ||
else | ||
{ | ||
Assert.True(null == serializableAttribute, $"Serializable attribute is not expected on {type.FullName}"); | ||
} | ||
} | ||
|
||
foreach (KeyValuePair<string, bool> entry in serializableTypes) | ||
{ | ||
Assert.True(entry.Value, $"Did we remove {entry.Key}?"); | ||
} | ||
} | ||
|
||
public static object EnsureDeserialize(string blob) | ||
{ | ||
var @object = FromBase64String(blob); | ||
Assert.NotNull(@object); | ||
return @object; | ||
} | ||
|
||
public static string ToBase64String(object @object, | ||
FormatterAssemblyStyle assemblyStyle = FormatterAssemblyStyle.Simple) | ||
{ | ||
byte[] raw = ToByteArray(@object, assemblyStyle); | ||
return Convert.ToBase64String(raw); | ||
} | ||
|
||
private static object FromBase64String(string base64String, | ||
FormatterAssemblyStyle assemblyStyle = FormatterAssemblyStyle.Simple) | ||
{ | ||
byte[] raw = Convert.FromBase64String(base64String); | ||
return FromByteArray(raw, assemblyStyle); | ||
} | ||
|
||
private static object FromByteArray(byte[] raw, | ||
FormatterAssemblyStyle assemblyStyle = FormatterAssemblyStyle.Simple) | ||
{ | ||
var binaryFormatter = new BinaryFormatter | ||
{ | ||
AssemblyFormat = assemblyStyle | ||
}; | ||
|
||
using (var serializedStream = new MemoryStream(raw)) | ||
{ | ||
return binaryFormatter.Deserialize(serializedStream); | ||
} | ||
} | ||
|
||
private static byte[] ToByteArray(object obj, | ||
FormatterAssemblyStyle assemblyStyle = FormatterAssemblyStyle.Simple) | ||
{ | ||
var binaryFormatter = new BinaryFormatter | ||
{ | ||
AssemblyFormat = assemblyStyle | ||
}; | ||
|
||
using (MemoryStream ms = new MemoryStream()) | ||
{ | ||
binaryFormatter.Serialize(ms, obj); | ||
return ms.ToArray(); | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/System.Windows.Forms.Design.Editors/tests/UnitTests/SerializableAttributeTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.ComponentModel.Design; | ||
using Xunit; | ||
|
||
namespace System.Windows.Forms.Design.Editors.Tests.Serialization | ||
{ | ||
public class SerializableAttributeTests | ||
{ | ||
[Fact] | ||
public void EnsureSerializableAttribute() | ||
{ | ||
BinarySerialization.EnsureSerializableAttribute( | ||
typeof(ArrayEditor).Assembly, | ||
new Dictionary<string, bool> | ||
{ | ||
// This Assembly does not have any serializable types. | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/System.Windows.Forms.Design/tests/UnitTests/SerializableAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.ComponentModel.Design; | ||
using System.ComponentModel.Design.Serialization; | ||
using System.Drawing.Design; | ||
using Xunit; | ||
|
||
namespace System.Windows.Forms.Design.Tests.Serialization | ||
{ | ||
public class SerializableAttributeTests | ||
{ | ||
[Fact] | ||
public void EnsureSerializableAttribute() | ||
{ | ||
BinarySerialization.EnsureSerializableAttribute( | ||
typeof(ToolboxItem).Assembly, | ||
new Dictionary<string, bool> | ||
{ | ||
// Following classes are participating in resx serialization scenarios. | ||
{ typeof(ExceptionCollection).FullName, false }, | ||
{ "System.ComponentModel.Design.Serialization.CodeDomComponentSerializationService+CodeDomSerializationStore", false }, // This type is private. | ||
{ typeof(CodeDomSerializerException).FullName, false }, | ||
{ typeof(ToolboxItem).FullName, false }, | ||
{ "System.Windows.Forms.Design.Behavior.DesignerActionKeyboardBehavior+<>c", false } | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.