summaryrefslogtreecommitdiff
path: root/tests/src/Interop
diff options
context:
space:
mode:
authorZeng Jiang <v-jiazen@microsoft.com>2018-10-20 01:07:59 +0800
committerJeremy Koritzinsky <jkoritzinsky@gmail.com>2018-10-19 10:07:59 -0700
commitf56dc8e76dc4bdf66b1f4376a9b8d0fc107c5926 (patch)
tree2e49a4214a8c142acb8d6aa925e4ec1fc5996798 /tests/src/Interop
parent6091ddbd6e531c82c7a5db015b587b57aa09fbf4 (diff)
downloadcoreclr-f56dc8e76dc4bdf66b1f4376a9b8d0fc107c5926.tar.gz
coreclr-f56dc8e76dc4bdf66b1f4376a9b8d0fc107c5926.tar.bz2
coreclr-f56dc8e76dc4bdf66b1f4376a9b8d0fc107c5926.zip
Add PInvoke/ExactSpelling tests (#19303)
* Add PInvoke/ExactSpelling tests Refactor tests to fit with the rest of the Interop tests. Fix up test to cleanly run. Change CMakeLists.txt to match the rest of the tests. Include Interop.cmake in CMakeLists.txt Remove Service. * On x86 enable stdcall mangling irrespective of ExactSpelling and account for the charset suffix when ExactSpelling = false. Change variable name. Clean up the FindEntryPoint. The logic flow now matches CoreRT + CoreCLR specific features (ordinals and stdcall mangling). PR Feedback. Fix format specifier. Add back probing null check. Fix offset calculation for stdcall mangling. Probe the stdcall-mangled versions of the original entry-point names when ExactSpelling isn't set. Cleanup.
Diffstat (limited to 'tests/src/Interop')
-rw-r--r--tests/src/Interop/CMakeLists.txt1
-rw-r--r--tests/src/Interop/PInvoke/ExactSpelling/CMakeLists.txt14
-rw-r--r--tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingNative.cpp179
-rw-r--r--tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingTest.cs110
-rw-r--r--tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingTest.csproj29
5 files changed, 333 insertions, 0 deletions
diff --git a/tests/src/Interop/CMakeLists.txt b/tests/src/Interop/CMakeLists.txt
index 934ab7a483..c81008bd2b 100644
--- a/tests/src/Interop/CMakeLists.txt
+++ b/tests/src/Interop/CMakeLists.txt
@@ -13,6 +13,7 @@ SET(CLR_INTEROP_TEST_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(common)
add_subdirectory(PInvoke/Primitives/Int)
+add_subdirectory(PInvoke/ExactSpelling)
add_subdirectory(NativeCallable)
add_subdirectory(PrimitiveMarshalling/Bool)
add_subdirectory(PrimitiveMarshalling/UIntPtr)
diff --git a/tests/src/Interop/PInvoke/ExactSpelling/CMakeLists.txt b/tests/src/Interop/PInvoke/ExactSpelling/CMakeLists.txt
new file mode 100644
index 0000000000..27850f4d3b
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ExactSpelling/CMakeLists.txt
@@ -0,0 +1,14 @@
+cmake_minimum_required (VERSION 2.6)
+project (ExactSpellingNative)
+include_directories(${INC_PLATFORM_DIR})
+include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake")
+set(SOURCES ExactSpellingNative.cpp)
+
+# add the executable
+add_library (ExactSpellingNative SHARED ${SOURCES})
+target_link_libraries(ExactSpellingNative ${LINK_LIBRARIES_ADDITIONAL})
+
+# add the install targets
+install (TARGETS ExactSpellingNative DESTINATION bin)
+
+
diff --git a/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingNative.cpp b/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingNative.cpp
new file mode 100644
index 0000000000..a4dad9df67
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingNative.cpp
@@ -0,0 +1,179 @@
+// 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 <stdio.h>
+#include <xplatform.h>
+
+int intManaged = 1000;
+int intNative = 2000;
+
+int intReturn = 3000;
+int intReturnA = 4000;
+int intReturnW = 5000;
+
+int intErrReturn = 6000;
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE Marshal_Int_InOut(/*[In,Out]*/int intValue)
+{
+ //Check the input
+ if(intValue != intManaged)
+ {
+ printf("Error in Function Marshal_Int_InOut(Native Client)\n");
+
+ //Expected
+ printf("Expected:%u\n", intManaged);
+
+ //Actual
+ printf("Actual:%u\n",intValue);
+
+ //Return the error value instead if verification failed
+ return intErrReturn;
+ }
+
+ //In-Place Change
+ intValue = intNative;
+
+ //Return
+ return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE Marshal_Int_InOutA(/*[In,Out]*/int intValue)
+{
+ //Check the input
+ if(intValue != intManaged)
+ {
+ printf("Error in Function Marshal_Int_InOutA(Native Client)\n");
+
+ //Expected
+ printf("Expected:%u\n", intManaged);
+
+ //Actual
+ printf("Actual:%u\n",intValue);
+
+ //Return the error value instead if verification failed
+ return intErrReturn;
+ }
+
+ //In-Place Change
+ intValue = intNative;
+
+ //Return
+ return intReturnA;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE Marshal_Int_InOutW(/*[In,Out]*/int intValue)
+{
+ //Check the input
+ if(intValue != intManaged)
+ {
+ printf("Error in Function Marshal_Int_InOutW(Native Client)\n");
+
+ //Expected
+ printf("Expected:%u\n", intManaged);
+
+ //Actual
+ printf("Actual:%u\n",intValue);
+
+ //Return the error value instead if verification failed
+ return intErrReturn;
+ }
+
+ //In-Place Change
+ intValue = intNative;
+
+ //Return
+ return intReturnW;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_Int_InOut(/*[in,out]*/int *pintValue)
+{
+ //Check the input
+ if(*pintValue != intManaged)
+ {
+ printf("Error in Function MarshalPointer_Int_InOut(Native Client)\n");
+
+ //Expected
+ printf("Expected:%u\n", intManaged);
+
+ //Actual
+ printf("Actual:%u\n",*pintValue);
+
+ //Return the error value instead if verification failed
+ return intErrReturn;
+ }
+
+ //In-Place Change
+ *pintValue = intNative;
+
+ //Return
+ return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_Int_InOutA(/*[in,out]*/int *pintValue)
+{
+ //Check the input
+ if(*pintValue != intManaged)
+ {
+ printf("Error in Function MarshalPointer_Int_InOutA(Native Client)\n");
+
+ //Expected
+ printf("Expected:%u\n", intManaged);
+
+ //Actual
+ printf("Actual:%u\n",*pintValue);
+
+ //Return the error value instead if verification failed
+ return intErrReturn;
+ }
+
+ //In-Place Change
+ *pintValue = intNative;
+
+ //Return
+ return intReturnA;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_Int_InOutW(/*[in,out]*/int *pintValue)
+{
+ //Check the input
+ if(*pintValue != intManaged)
+ {
+ printf("Error in Function MarshalPointer_Int_InOutW(Native Client)\n");
+
+ //Expected
+ printf("Expected:%u\n", intManaged);
+
+ //Actual
+ printf("Actual:%u\n",*pintValue);
+
+ //Return the error value instead if verification failed
+ return intErrReturn;
+ }
+
+ //In-Place Change
+ *pintValue = intNative;
+
+ //Return
+ return intReturnW;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE Marshal_Int_InOut2A(/*[In,Out]*/int intValue)
+{
+ return Marshal_Int_InOutA(intValue);
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE Marshal_Int_InOut2W(/*[In,Out]*/int intValue)
+{
+ return Marshal_Int_InOutW(intValue);
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_Int_InOut2A(/*[in,out]*/int *pintValue)
+{
+ return MarshalPointer_Int_InOutA(pintValue);
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_Int_InOut2W(/*[in,out]*/int *pintValue)
+{
+ return MarshalPointer_Int_InOutW(pintValue);
+}
diff --git a/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingTest.cs b/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingTest.cs
new file mode 100644
index 0000000000..7616e6d6d8
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingTest.cs
@@ -0,0 +1,110 @@
+// 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;
+using System.Runtime.InteropServices;
+
+class ExactSpellingTest
+{
+ class Ansi
+ {
+ [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = true)]
+ public static extern int Marshal_Int_InOut([In, Out] int intValue);
+
+ [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = true)]
+ public static extern int MarshalPointer_Int_InOut([In, Out] ref int intValue);
+
+ [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = false)]
+ public static extern int Marshal_Int_InOut2([In, Out] int intValue);
+
+ [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = false)]
+ public static extern int MarshalPointer_Int_InOut2([In, Out] ref int intValue);
+ }
+
+ class Unicode
+ {
+ [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = true)]
+ public static extern int Marshal_Int_InOut([In, Out] int intValue);
+
+ [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = true)]
+ public static extern int MarshalPointer_Int_InOut([In, Out] ref int intValue);
+
+ [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = false)]
+ public static extern int Marshal_Int_InOut2([In, Out] int intValue);
+
+ [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = false)]
+ public static extern int MarshalPointer_Int_InOut2([In, Out] ref int intValue);
+ }
+
+ public static int Main(string[] args)
+ {
+ int failures = 0;
+ int intManaged = 1000;
+ int intNative = 2000;
+ int intReturn = 3000;
+
+ Console.WriteLine("Method Unicode.Marshal_Int_InOut: ExactSpelling = true");
+ int int1 = intManaged;
+ int intRet1 = Unicode.Marshal_Int_InOut(int1);
+ failures += Verify(intReturn, intManaged, intRet1, int1);
+
+ Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut: ExactSpelling = true");
+ int int2 = intManaged;
+ int intRet2 = Unicode.MarshalPointer_Int_InOut(ref int2);
+
+ failures += Verify(intReturn, intNative, intRet2, int2);
+
+ Console.WriteLine("Method Ansi.Marshal_Int_InOut: ExactSpelling = true");
+ int int3 = intManaged;
+ int intRet3 = Ansi.Marshal_Int_InOut(int3);
+ failures += Verify(intReturn, intManaged, intRet3, int3);
+
+ Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut: ExactSpelling = true");
+ int int4 = intManaged;
+ int intRet4 = Ansi.MarshalPointer_Int_InOut(ref int4);
+ failures += Verify(intReturn, intNative, intRet4, int4);
+
+ int intReturnAnsi = 4000;
+ int intReturnUnicode = 5000;
+
+ Console.WriteLine("Method Unicode.Marshal_Int_InOut2: ExactSpelling = false");
+ int int5 = intManaged;
+ int intRet5 = Unicode.Marshal_Int_InOut2(int5);
+ failures += Verify(intReturnUnicode, intManaged, intRet5, int5);
+
+ Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut2: ExactSpelling = false");
+ int int6 = intManaged;
+ int intRet6 = Unicode.MarshalPointer_Int_InOut2(ref int6);
+ failures += Verify(intReturnUnicode, intNative, intRet6, int6);
+
+ Console.WriteLine("Method Ansi.Marshal_Int_InOut2: ExactSpelling = false");
+ int int7 = intManaged;
+ int intRet7 = Ansi.Marshal_Int_InOut2(int7);
+ failures += Verify(intReturnAnsi, intManaged, intRet7, int7);
+
+ Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut2: ExactSpelling = false");
+ int int8 = intManaged;
+ int intRet8 = Ansi.MarshalPointer_Int_InOut2(ref int8);
+ failures += Verify(intReturnAnsi, intNative, intRet8, int8);
+
+ return 100 + failures;
+ }
+
+ private static int Verify(int expectedReturnValue, int expectedParameterValue, int actualReturnValue, int actualParameterValue)
+ {
+ int failures = 0;
+ if (expectedReturnValue != actualReturnValue)
+ {
+ failures++;
+ Console.WriteLine($"The return value is wrong. Expected {expectedReturnValue}, got {actualReturnValue}");
+ }
+ if (expectedParameterValue != actualParameterValue)
+ {
+ failures++;
+ Console.WriteLine($"The parameter value is changed. Expected {expectedParameterValue}, got {actualParameterValue}");
+ }
+
+ return failures;
+ }
+}
diff --git a/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingTest.csproj b/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingTest.csproj
new file mode 100644
index 0000000000..77a6dcc087
--- /dev/null
+++ b/tests/src/Interop/PInvoke/ExactSpelling/ExactSpellingTest.csproj
@@ -0,0 +1,29 @@
+<?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>ExactSpellingTest</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>
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'"></PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'"></PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="ExactSpellingTest.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="CMakeLists.txt" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>