From 47e9cd55ec692323712c8b6e9afadd54853d562b Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Fri, 5 Feb 2021 12:58:31 +0100 Subject: [PATCH 1/2] Fix non-public DispatchProxy + tests --- .../Reflection/DispatchProxyGenerator.cs | 2 + .../tests/DispatchProxyTests.cs | 54 +++++++++++++++++++ ...stem.Reflection.DispatchProxy.Tests.csproj | 7 +++ .../TestDependency/InternalsVisibleTo.cs | 6 +++ ...ection.DispatchProxy.TestDependency.csproj | 12 +++++ .../tests/TestDependency/TestTypes.cs | 18 +++++++ .../tests/TestTypes.cs | 44 +++++++++++++++ 7 files changed, 143 insertions(+) create mode 100644 src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/InternalsVisibleTo.cs create mode 100644 src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/System.Reflection.DispatchProxy.TestDependency.csproj create mode 100644 src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/TestTypes.cs diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs index c21a28afd11e93..30f20c41fd1603 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs @@ -252,6 +252,8 @@ internal ProxyBuilder( _fields.Add(tb.DefineField("_methodInfos", typeof(MethodInfo[]), FieldAttributes.Private)); _methodInfos = new List(); + + _assembly.EnsureTypeIsVisible(proxyBaseType); } private void Complete() diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs index d4a421b8c9fec4..dd0563bf4800b4 100644 --- a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs +++ b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs @@ -136,6 +136,60 @@ public static void Create_Using_BaseType_Without_Default_Ctor_Throws_ArgumentExc AssertExtensions.Throws("TProxy", () => DispatchProxy.Create()); } + [Fact] + public static void Create_Using_PrivateProxy() + { + Assert.NotNull(TestType_PrivateProxy.Proxy()); + } + + [Fact] + public static void Create_Using_PrivateProxyAndInternalService() + { + Assert.NotNull(TestType_PrivateProxy.Proxy()); + } + + [Fact] + public static void Create_Using_PrivateProxyAndInternalServiceWithExternalGenericArgument() + { + Assert.NotNull(TestType_PrivateProxy.Proxy()); + } + + [Fact] + public static void Create_Using_InternalProxy() + { + Assert.NotNull(DispatchProxy.Create()); + } + + [Fact] + public static void Create_Using_ExternalNonPublicService() + { + Assert.NotNull(DispatchProxy.Create()); + } + + [Fact] + public static void Create_Using_InternalProxyWithExternalNonPublicBaseType() + { + Assert.NotNull(DispatchProxy.Create()); + } + + [Fact] + public static void Create_Using_InternalServiceImplementingNonPublicExternalService() + { + Assert.NotNull(DispatchProxy.Create()); + } + + [Fact] + public static void Create_Using_InternalServiceWithGenericArgumentBeingNonPublicExternalService() + { + Assert.NotNull(DispatchProxy.Create()); + } + + [Fact] + public static void Create_Using_InternalProxyWithBaseTypeImplementingServiceWithgenericArgumentBeingNonPublicExternalService() + { + Assert.NotNull(DispatchProxy.Create()); + } + [Fact] public static void Invoke_Receives_Correct_MethodInfo_And_Arguments() { diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/System.Reflection.DispatchProxy.Tests.csproj b/src/libraries/System.Reflection.DispatchProxy/tests/System.Reflection.DispatchProxy.Tests.csproj index 0b8d6748dfe0f0..088e8857038adc 100644 --- a/src/libraries/System.Reflection.DispatchProxy/tests/System.Reflection.DispatchProxy.Tests.csproj +++ b/src/libraries/System.Reflection.DispatchProxy/tests/System.Reflection.DispatchProxy.Tests.csproj @@ -1,9 +1,16 @@ + $(NetCoreAppCurrent) + + + + + + \ No newline at end of file diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/InternalsVisibleTo.cs b/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/InternalsVisibleTo.cs new file mode 100644 index 00000000000000..51268072a9ea4d --- /dev/null +++ b/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/InternalsVisibleTo.cs @@ -0,0 +1,6 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("System.Reflection.DispatchProxy.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")] diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/System.Reflection.DispatchProxy.TestDependency.csproj b/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/System.Reflection.DispatchProxy.TestDependency.csproj new file mode 100644 index 00000000000000..9c316ce65c01e4 --- /dev/null +++ b/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/System.Reflection.DispatchProxy.TestDependency.csproj @@ -0,0 +1,12 @@ + + + + $(NetCoreAppCurrent) + + + + + + + + diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/TestTypes.cs b/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/TestTypes.cs new file mode 100644 index 00000000000000..60d3b8ac849beb --- /dev/null +++ b/src/libraries/System.Reflection.DispatchProxy/tests/TestDependency/TestTypes.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Reflection; + +namespace DispatchProxyTestDependency +{ + internal interface TestType_IExternalNonPublicHiService + { + string Hi(string message); + } + + internal class TestType_ExternalNonPublicBaseClassForProxy : DispatchProxy + { + protected override object Invoke(MethodInfo targetMethod, object[] args) => null; + } +} diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/TestTypes.cs b/src/libraries/System.Reflection.DispatchProxy/tests/TestTypes.cs index 5057bda46b800c..d4fa174fd29814 100644 --- a/src/libraries/System.Reflection.DispatchProxy/tests/TestTypes.cs +++ b/src/libraries/System.Reflection.DispatchProxy/tests/TestTypes.cs @@ -8,6 +8,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; +using DispatchProxyTestDependency; // Test types used to make proxies. @@ -91,6 +92,20 @@ internal interface TestType_PublicInterfaceService_Implements_Internal : TestTyp string Echo2(string message); } +internal interface TestType_InternalInterfaceImplementsNonPublicExternalType : TestType_IExternalNonPublicHiService +{ + string Hi2(string message); +} + +internal interface TestType_ServiceWithGenericArgument +{ + T GetInnerService(); +} + +internal interface TestType_InternalInterfaceWithNonPublicExternalGenericArgument : TestType_ServiceWithGenericArgument +{ +} + public interface TypeType_GenericMethod { T Echo(T messages); @@ -111,6 +126,35 @@ protected override object Invoke(MethodInfo targetMethod, object[] args) } } +public class TestType_PrivateProxy +{ + public static T Proxy() => DispatchProxy.Create>(); + + private class InvokeProxy : DispatchProxy + { + protected override object Invoke(MethodInfo targetMethod, object[] args) => null; + } +} + +internal class TestType_InternalProxyInternalBaseType : TestType_ExternalNonPublicBaseClassForProxy +{ +} + +internal class TestType_InternalProxyImplementingInterfaceWithGenericArgumentBeingNonPublicExternalType : DispatchProxy, TestType_InternalInterfaceWithNonPublicExternalGenericArgument +{ + public TestType_IExternalNonPublicHiService GetInnerService() => throw new InvalidOperationException(); + + protected override object Invoke(MethodInfo targetMethod, object[] args) + { + throw new InvalidOperationException(); + } +} + +internal class InternalInvokeProxy : DispatchProxy +{ + protected override object Invoke(MethodInfo targetMethod, object[] args) => null; +} + // This test double creates a proxy instance for the requested 'ProxyT' type. // When methods are invoked on that proxy, it will call a registered callback. public class TestDispatchProxy : DispatchProxy From 1272b724de9888d684aee1fe7501186c863e0088 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Mon, 8 Feb 2021 11:07:40 +0100 Subject: [PATCH 2/2] Disable mono tests affected by #47989 --- .../tests/DispatchProxyTests.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs index dd0563bf4800b4..de2fe6af860659 100644 --- a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs +++ b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs @@ -137,24 +137,28 @@ public static void Create_Using_BaseType_Without_Default_Ctor_Throws_ArgumentExc } [Fact] + [SkipOnMono("https://github.com/dotnet/runtime/issues/47989")] public static void Create_Using_PrivateProxy() { Assert.NotNull(TestType_PrivateProxy.Proxy()); } [Fact] + [SkipOnMono("https://github.com/dotnet/runtime/issues/47989")] public static void Create_Using_PrivateProxyAndInternalService() { Assert.NotNull(TestType_PrivateProxy.Proxy()); } [Fact] + [SkipOnMono("https://github.com/dotnet/runtime/issues/47989")] public static void Create_Using_PrivateProxyAndInternalServiceWithExternalGenericArgument() { Assert.NotNull(TestType_PrivateProxy.Proxy()); } [Fact] + [SkipOnMono("https://github.com/dotnet/runtime/issues/47989")] public static void Create_Using_InternalProxy() { Assert.NotNull(DispatchProxy.Create()); @@ -167,6 +171,7 @@ public static void Create_Using_ExternalNonPublicService() } [Fact] + [SkipOnMono("https://github.com/dotnet/runtime/issues/47989")] public static void Create_Using_InternalProxyWithExternalNonPublicBaseType() { Assert.NotNull(DispatchProxy.Create()); @@ -185,6 +190,7 @@ public static void Create_Using_InternalServiceWithGenericArgumentBeingNonPublic } [Fact] + [SkipOnMono("https://github.com/dotnet/runtime/issues/47989")] public static void Create_Using_InternalProxyWithBaseTypeImplementingServiceWithgenericArgumentBeingNonPublicExternalService() { Assert.NotNull(DispatchProxy.Create());