From: Jeremy Koritzinsky Date: Wed, 5 Dec 2018 18:04:52 +0000 (-0800) Subject: Add tests for marshalling RuntimeHandle types. (#21091) X-Git-Tag: accepted/tizen/unified/20190422.045933~442 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a3304a1fc6a442efc54f2fc765e6877ffba14a5a;p=platform%2Fupstream%2Fcoreclr.git Add tests for marshalling RuntimeHandle types. (#21091) * Add tests for marshalling runtime handles. * Remove DLL signature for impossible test --- diff --git a/tests/src/Interop/CMakeLists.txt b/tests/src/Interop/CMakeLists.txt index 54e20a8..d9f3200 100644 --- a/tests/src/Interop/CMakeLists.txt +++ b/tests/src/Interop/CMakeLists.txt @@ -21,6 +21,7 @@ add_subdirectory(PInvoke/BestFitMapping/LPStr) add_subdirectory(PInvoke/Delegate/MarshalDelegateAsField) add_subdirectory(PInvoke/Delegate/MarshalDelegateAsParam) add_subdirectory(PInvoke/Primitives/Int) +add_subdirectory(PInvoke/Primitives/RuntimeHandles) add_subdirectory(PInvoke/SizeParamIndex/PInvoke/PassingByOut) add_subdirectory(PInvoke/SizeParamIndex/PInvoke/PassingByRef) add_subdirectory(PInvoke/SizeParamIndex/ReversePInvoke/PassingByOut) diff --git a/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/CMakeLists.txt b/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/CMakeLists.txt new file mode 100644 index 0000000..e12b647 --- /dev/null +++ b/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required (VERSION 2.6) +project (RuntimeHandlesNative) +include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake") +include_directories(${INC_PLATFORM_DIR}) +set(SOURCES RuntimeHandlesNative.cpp) +add_library (RuntimeHandlesNative SHARED ${SOURCES}) +# add the install targets +install (TARGETS RuntimeHandlesNative DESTINATION bin) diff --git a/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesNative.cpp b/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesNative.cpp new file mode 100644 index 0000000..2a95a1a --- /dev/null +++ b/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesNative.cpp @@ -0,0 +1,10 @@ +// 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. + +#include + +extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Marshal_In(HANDLE expected, HANDLE actual) +{ + return expected == actual ? TRUE : FALSE; +} diff --git a/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesTest.cs b/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesTest.cs new file mode 100644 index 0000000..5452be0 --- /dev/null +++ b/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesTest.cs @@ -0,0 +1,61 @@ +// 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.Runtime.InteropServices; +using System; +using System.Reflection; +using TestLibrary; + +class TestClass +{ + public int field; + + public void Method() + { + } +} + +class RuntimeHandlesTest +{ + [DllImport("RuntimeHandlesNative")] + private static extern bool Marshal_In(RuntimeMethodHandle expected, IntPtr handle); + [DllImport("RuntimeHandlesNative")] + private static extern bool Marshal_In(RuntimeFieldHandle expected, IntPtr handle); + [DllImport("RuntimeHandlesNative")] + private static extern bool Marshal_In(RuntimeTypeHandle expected, IntPtr handle); + + private static void TestRuntimeMethodHandle() + { + RuntimeMethodHandle handle = typeof(TestClass).GetMethod(nameof(TestClass.Method)).MethodHandle; + Assert.IsTrue(Marshal_In(handle, handle.Value)); + } + + private static void TestRuntimeFieldHandle() + { + RuntimeFieldHandle handle = typeof(TestClass).GetField(nameof(TestClass.field)).FieldHandle; + Assert.IsTrue(Marshal_In(handle, handle.Value)); + } + + private static void TestRuntimeTypeHandle() + { + RuntimeTypeHandle handle = typeof(TestClass).TypeHandle; + Assert.IsTrue(Marshal_In(handle, handle.Value)); + } + + public static int Main() + { + try + { + TestRuntimeTypeHandle(); + TestRuntimeFieldHandle(); + TestRuntimeMethodHandle(); + } + catch (Exception e) + { + Console.WriteLine(e); + return 101; + } + return 100; + } +} diff --git a/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesTest.csproj b/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesTest.csproj new file mode 100644 index 0000000..ed72ddb --- /dev/null +++ b/tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesTest.csproj @@ -0,0 +1,34 @@ + + + + + Debug + AnyCPU + RuntimeHandlesTest + 2.0 + {F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\..\ + $(DefineConstants);STATIC + + + + + + + False + + + + + + + + + + + + + + diff --git a/tests/src/Interop/common/types.h b/tests/src/Interop/common/types.h index 4bec762..cabcd42 100755 --- a/tests/src/Interop/common/types.h +++ b/tests/src/Interop/common/types.h @@ -18,6 +18,7 @@ typedef const WCHAR *LPCWSTR, *PCWSTR; typedef char* LPSTR; typedef const char* LPCSTR; typedef void* FARPROC; +typedef void* HANDLE; typedef void* HMODULE; typedef int error_t; typedef void* LPVOID;