Add tests for marshalling RuntimeHandle types. (#21091)
authorJeremy Koritzinsky <jkoritzinsky@gmail.com>
Wed, 5 Dec 2018 18:04:52 +0000 (10:04 -0800)
committerGitHub <noreply@github.com>
Wed, 5 Dec 2018 18:04:52 +0000 (10:04 -0800)
* Add tests for marshalling runtime handles.

* Remove DLL signature for impossible test

tests/src/Interop/CMakeLists.txt
tests/src/Interop/PInvoke/Primitives/RuntimeHandles/CMakeLists.txt [new file with mode: 0644]
tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesNative.cpp [new file with mode: 0644]
tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesTest.cs [new file with mode: 0644]
tests/src/Interop/PInvoke/Primitives/RuntimeHandles/RuntimeHandlesTest.csproj [new file with mode: 0644]
tests/src/Interop/common/types.h

index 54e20a8..d9f3200 100644 (file)
@@ -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 (file)
index 0000000..e12b647
--- /dev/null
@@ -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 (file)
index 0000000..2a95a1a
--- /dev/null
@@ -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 <xplatform.h>
+
+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 (file)
index 0000000..5452be0
--- /dev/null
@@ -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 (file)
index 0000000..ed72ddb
--- /dev/null
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>RuntimeHandlesTest</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\</SolutionDir>
+    <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="RuntimeHandlesTest.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="CMakeLists.txt" />
+  </ItemGroup>
+  <Import Project="../../../Interop.settings.targets" />
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
index 4bec762..cabcd42 100755 (executable)
@@ -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;