Add Tests for VBByRefStr marshalling (#20982)
authorJeremy Koritzinsky <jkoritzinsky@gmail.com>
Fri, 16 Nov 2018 01:16:11 +0000 (17:16 -0800)
committerGitHub <noreply@github.com>
Fri, 16 Nov 2018 01:16:11 +0000 (17:16 -0800)
* Add tests for VBByRefStr marshalling.

* Compile native for VBByRefStr on all platforms.

* Fix encoding.

* PR Feedback.

* Fix PInvokeDefs file.

* Fix visibility

tests/src/Interop/CMakeLists.txt
tests/src/Interop/StringMarshalling/VBByRefStr/CMakeLists.txt [new file with mode: 0644]
tests/src/Interop/StringMarshalling/VBByRefStr/PInvokeDef.cs [new file with mode: 0644]
tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrNative.cpp [new file with mode: 0644]
tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrTest.cs [new file with mode: 0644]
tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrTest.csproj [new file with mode: 0644]

index 60fbde3..27d3049 100644 (file)
@@ -47,6 +47,7 @@ add_subdirectory(StringMarshalling/LPTSTR)
 add_subdirectory(StringMarshalling/UTF8)
 add_subdirectory(StringMarshalling/BSTR)
 add_subdirectory(StringMarshalling/AnsiBSTR)
+add_subdirectory(StringMarshalling/VBByRefStr)
 add_subdirectory(MarshalAPI/FunctionPointer)
 add_subdirectory(MarshalAPI/IUnknown)
 add_subdirectory(SizeConst)
diff --git a/tests/src/Interop/StringMarshalling/VBByRefStr/CMakeLists.txt b/tests/src/Interop/StringMarshalling/VBByRefStr/CMakeLists.txt
new file mode 100644 (file)
index 0000000..39d1f77
--- /dev/null
@@ -0,0 +1,11 @@
+cmake_minimum_required (VERSION 2.6)
+project (VBByRefStrNative)
+include_directories(${INC_PLATFORM_DIR})
+set(SOURCES VBByRefStrNative.cpp )
+
+# add the executable
+add_library (VBByRefStrNative SHARED ${SOURCES})
+target_link_libraries(VBByRefStrNative ${LINK_LIBRARIES_ADDITIONAL}) 
+
+# add the install targets
+install (TARGETS VBByRefStrNative DESTINATION bin)
diff --git a/tests/src/Interop/StringMarshalling/VBByRefStr/PInvokeDef.cs b/tests/src/Interop/StringMarshalling/VBByRefStr/PInvokeDef.cs
new file mode 100644 (file)
index 0000000..12d29ed
--- /dev/null
@@ -0,0 +1,22 @@
+// 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.Text;
+using System.Runtime.InteropServices;
+
+class VBByRefStrNative
+{
+    
+    [DllImport(nameof(VBByRefStrNative), CharSet = CharSet.Ansi)]
+    public static extern bool Marshal_Ansi(string expected, [MarshalAs(UnmanagedType.VBByRefStr)] ref string actual, string newValue);
+    [DllImport(nameof(VBByRefStrNative), CharSet = CharSet.Unicode)]
+    public static extern bool Marshal_Unicode(string expected, [MarshalAs(UnmanagedType.VBByRefStr)] ref string actual, string newValue);
+
+    [DllImport(nameof(VBByRefStrNative), EntryPoint = "Marshal_Invalid")]
+    public static extern bool Marshal_StringBuilder([MarshalAs(UnmanagedType.VBByRefStr)]ref  StringBuilder builder);
+
+    [DllImport(nameof(VBByRefStrNative), EntryPoint = "Marshal_Invalid")]
+    public static extern bool Marshal_ByVal([MarshalAs(UnmanagedType.VBByRefStr)]string str);
+}
diff --git a/tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrNative.cpp b/tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrNative.cpp
new file mode 100644 (file)
index 0000000..a23fb91
--- /dev/null
@@ -0,0 +1,30 @@
+// 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>
+#include "platformdefines.h"
+
+extern "C" DLL_EXPORT BOOL Marshal_Ansi(LPCSTR expected, LPSTR actual, LPCSTR newValue)
+{
+    bool result = strcmp(expected, actual) == 0;
+
+    strcpy_s(actual, strlen(actual), newValue);
+
+    return result;
+}
+
+extern "C" DLL_EXPORT BOOL Marshal_Unicode(LPCWSTR expected, LPWSTR actual, LPCWSTR newValue)
+{
+    bool result = wcscmp(expected, actual) == 0;
+
+    wcscpy_s(actual, wcslen(actual), newValue);
+
+    return result;
+}
+
+
+extern "C" DLL_EXPORT BOOL Marshal_Invalid(LPCSTR value)
+{
+    return FALSE;
+}
diff --git a/tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrTest.cs b/tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrTest.cs
new file mode 100644 (file)
index 0000000..8cd1cb1
--- /dev/null
@@ -0,0 +1,44 @@
+// 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 System.Text;
+using TestLibrary;
+
+#pragma warning disable CS0612, CS0618
+
+class Test
+{
+
+    public static int Main(string[] args)
+    {
+        try
+        {
+            string expected = "abcdefgh";
+            string actual;
+            string newValue = "zyxwvut\0";
+
+            actual = expected;
+            Assert.IsTrue(VBByRefStrNative.Marshal_Ansi(expected, ref actual, newValue));
+            Assert.AreEqual(newValue, actual);
+
+            actual = expected;
+            Assert.IsTrue(VBByRefStrNative.Marshal_Unicode(expected, ref actual, newValue));
+            Assert.AreEqual(newValue, actual);
+
+            StringBuilder builder = new StringBuilder();
+
+            Assert.Throws<MarshalDirectiveException>(() => VBByRefStrNative.Marshal_StringBuilder(ref builder));
+            Assert.Throws<MarshalDirectiveException>(() => VBByRefStrNative.Marshal_ByVal(string.Empty));
+        }
+        catch (Exception e)
+        {
+            Console.WriteLine(e);
+            return 101;
+        }
+        return 100;
+    }
+}
diff --git a/tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrTest.csproj b/tests/src/Interop/StringMarshalling/VBByRefStr/VBByRefStrTest.csproj
new file mode 100644 (file)
index 0000000..787fd25
--- /dev/null
@@ -0,0 +1,35 @@
+<?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" />
+  <Import Project="../../Interop.settings.targets" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>VBByRefStrTest</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>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+    <!-- Test unsupported outside of windows -->
+    <TestUnsupportedOutsideWindows>true</TestUnsupportedOutsideWindows>
+    <DisableProjectBuild Condition="'$(TargetsUnix)' == 'true'">true</DisableProjectBuild>
+  </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="*.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="CMakeLists.txt" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>