Skip to content

Commit

Permalink
Disable BinaryFormatter tests in wasm; throw PNSE in wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks authored Jul 15, 2020
1 parent 4900453 commit 47c8ff6
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public abstract partial class IEnumerable_Generic_Tests<T> : TestBase<T>
{
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
[PlatformSpecific(~TestPlatforms.Browser)] // BinaryFormatter not supported in browser
public void IGenericSharedAPI_SerializeDeserialize(int count)
{
IEnumerable<T> expected = GenericIEnumerableFactory(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public abstract partial class IEnumerable_NonGeneric_Tests : TestBase
{
[Theory]
[MemberData(nameof(ValidCollectionSizes))]
[PlatformSpecific(~TestPlatforms.Browser)] // BinaryFormatter not supported in browser
public void IGenericSharedAPI_SerializeDeserialize(int count)
{
IEnumerable expected = NonGenericIEnumerableFactory(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace System.Collections.Generic.Tests
public abstract partial class ComparersGenericTests<T>
{
[Fact]
[PlatformSpecific(~TestPlatforms.Browser)] // BinaryFormatter not supported in browser
public void EqualityComparer_SerializationRoundtrip()
{
var bf = new BinaryFormatter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ private static IDictionary<T, T> CreateDictionary<T>(int size, Func<int, T> keyV
}

[Fact]
[PlatformSpecific(~TestPlatforms.Browser)] // BinaryFormatter not supported in browser
public void ComparerSerialization()
{
// Strings switch between randomized and non-randomized comparers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ public void Remove_NonDefaultComparer_ComparerUsed(int capacity)
#region Serialization

[Fact]
[PlatformSpecific(~TestPlatforms.Browser)] // BinaryFormatter not supported in browser
public void ComparerSerialization()
{
// Strings switch between randomized and non-randomized comparers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<!-- On certain runtimes BinaryFormatter isn't supported at all. -->
<DefineConstants Condition="'$(TargetsBrowser)' == 'true'">$(DefineConstants);BINARYFORMATTER_PNSE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Runtime.Serialization.Formatters.TypeForwards.cs" />
<Compile Include="System\Runtime\Serialization\DeserializationEventHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public BinaryFormatter(ISurrogateSelector? selector, StreamingContext context)

public object Deserialize(Stream serializationStream)
{
// don't refactor this check out into a helper method; linker will have difficulty pruning
// don't refactor the 'throw' into a helper method; linker will have difficulty trimming
if (!SerializationInfo.BinaryFormatterEnabled)
{
throw new NotSupportedException(SR.BinaryFormatter_SerializationDisallowed);
throw CreateBinaryFormatterDisallowedException();
}

if (serializationStream == null)
Expand Down Expand Up @@ -82,10 +82,10 @@ public object Deserialize(Stream serializationStream)

public void Serialize(Stream serializationStream, object graph)
{
// don't refactor this check out into a helper method; linker will have difficulty pruning
// don't refactor the 'throw' into a helper method; linker will have difficulty trimming
if (!SerializationInfo.BinaryFormatterEnabled)
{
throw new NotSupportedException(SR.BinaryFormatter_SerializationDisallowed);
throw CreateBinaryFormatterDisallowedException();
}

if (serializationStream == null)
Expand All @@ -112,5 +112,19 @@ internal static TypeInformation GetTypeInformation(Type type) =>
string assemblyName = FormatterServices.GetClrAssemblyName(t, out bool hasTypeForwardedFrom);
return new TypeInformation(FormatterServices.GetClrTypeFullName(t), assemblyName, hasTypeForwardedFrom);
});

private static Exception CreateBinaryFormatterDisallowedException()
{
// If we're in an environment where BinaryFormatter never has a
// chance of succeeding, use PNSE. Otherwise use regular NSE.

string message = SR.BinaryFormatter_SerializationDisallowed;

#if BINARYFORMATTER_PNSE
throw new PlatformNotSupportedException(message);
#else
throw new NotSupportedException(message);
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace System.Runtime.Serialization.Formatters.Tests
{
[PlatformSpecific(~TestPlatforms.Browser)] // BinaryFormatter not supported in browser
public partial class BinaryFormatterTests : FileCleanupTestBase
{
// On 32-bit we can't test these high inputs as they cause OutOfMemoryExceptions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public static void DisabledAlwaysInBrowser()

MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
var ex = Assert.Throws<NotSupportedException>(() => bf.Serialize(ms, "A string to serialize."));
var ex = Assert.Throws<PlatformNotSupportedException>(() => bf.Serialize(ms, "A string to serialize."));
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL

// Then test deserialization

ex = Assert.Throws<NotSupportedException>(() => bf.Deserialize(ms));
ex = Assert.Throws<PlatformNotSupportedException>(() => bf.Deserialize(ms));
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void BindToName_NullDefaults()
}

[Fact]
[PlatformSpecific(~TestPlatforms.Browser)] // BinaryFormatter not supported in browser
public void BindToType_AllValuesTracked()
{
var s = new MemoryStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace System.Runtime.Serialization.Formatters.Tests
{
[PlatformSpecific(~TestPlatforms.Browser)] // BinaryFormatter not supported in browser
public static class SerializationGuardTests
{
[Fact]
Expand Down

0 comments on commit 47c8ff6

Please sign in to comment.