diff options
author | Michal Strehovský <MichalStrehovsky@users.noreply.github.com> | 2017-02-08 00:12:44 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2017-02-08 00:12:43 +0300 |
commit | 7291cf0408f126ce805ab63e8e0ec04fcb1395e6 (patch) | |
tree | b0c620b5ca5df0efca7801c750de4f29bd626b5f /src/System.Runtime | |
parent | 7f25cadc3102e6adf92ada810d6bd17a825a1c1b (diff) |
Add Activator.CreateInstance negative tests (#15923)
Regression tests for issues found in CoreRT/.NET Native.
Diffstat (limited to 'src/System.Runtime')
-rw-r--r-- | src/System.Runtime/tests/System/ActivatorTests.cs | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/System.Runtime/tests/System/ActivatorTests.cs b/src/System.Runtime/tests/System/ActivatorTests.cs index 5a40391038..4e8420835b 100644 --- a/src/System.Runtime/tests/System/ActivatorTests.cs +++ b/src/System.Runtime/tests/System/ActivatorTests.cs @@ -85,8 +85,10 @@ namespace System.Tests // out of Activator.CreateInstance. Accidental or not, we'll inherit that behavior on .NET Native.) Assert.Throws<InvalidCastException>(() => Activator.CreateInstance(typeof(Choice1), new object[] { new VarIntArgs(), 1, (short)2 })); - Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance<TypeWithoutDefaultCtor>()); // Type has no default constructor - Assert.Throws<TargetInvocationException>(() => Activator.CreateInstance<TypeWithDefaultCtorThatThrows>()); // Type has a default constructor throws an exception + Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(typeof(TypeWithoutDefaultCtor))); // Type has no default constructor + Assert.Throws<TargetInvocationException>(() => Activator.CreateInstance(typeof(TypeWithDefaultCtorThatThrows))); // Type has a default constructor throws an exception + Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(typeof(AbstractTypeWithDefaultCtor))); // Type is abstract + Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance(typeof(IInterfaceType))); // Type is an interface // Type is not a valid RuntimeType Assert.Throws<ArgumentException>("type", () => Activator.CreateInstance(Helpers.NonRuntimeType())); @@ -109,6 +111,23 @@ namespace System.Tests Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance<TypeWithoutDefaultCtor>()); // Type has no default constructor Assert.Throws<TargetInvocationException>(() => Activator.CreateInstance<TypeWithDefaultCtorThatThrows>()); // Type has a default constructor that throws + Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance<AbstractTypeWithDefaultCtor>()); // Type is abstract + Assert.ThrowsAny<MissingMemberException>(() => Activator.CreateInstance<IInterfaceType>()); // Type is an interface + } + + [Fact] + public static void TestActivatorOnNonActivatableFinalizableTypes() + { + // On runtimes where the generic Activator is implemented with special codegen intrinsics, we might allocate + // an uninitialized instance of the object before we realize there's no default constructor to run. + // Make sure this has no observable side effects. + Assert.ThrowsAny<MissingMemberException>(() => { Activator.CreateInstance<TypeWithPrivateDefaultCtorAndFinalizer>(); }); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + Assert.False(TypeWithPrivateDefaultCtorAndFinalizer.WasCreated); } class PrivateType @@ -193,6 +212,27 @@ namespace System.Tests public class VarIntArgs { } + public class TypeWithPrivateDefaultCtorAndFinalizer + { + public static bool WasCreated { get; private set; } + + private TypeWithPrivateDefaultCtorAndFinalizer() { } + + ~TypeWithPrivateDefaultCtorAndFinalizer() + { + WasCreated = true; + } + } + + private interface IInterfaceType + { + } + + public abstract class AbstractTypeWithDefaultCtor + { + public AbstractTypeWithDefaultCtor() { } + } + public struct StructTypeWithoutReflectionMetadata { } public class TypeWithoutDefaultCtor |