summaryrefslogtreecommitdiff
path: root/tests/src/Interop/MarshalAPI
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src/Interop/MarshalAPI')
-rw-r--r--tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.cs116
-rw-r--r--tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.csproj40
-rw-r--r--tests/src/Interop/MarshalAPI/String/StringMarshalingTest.cs34
3 files changed, 190 insertions, 0 deletions
diff --git a/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.cs b/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.cs
new file mode 100644
index 0000000000..a11c10b01b
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.cs
@@ -0,0 +1,116 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+
+using CoreFXTestLibrary;
+
+internal struct BlittableStruct
+{
+ internal int a;
+ internal int b;
+ internal byte c;
+ internal short d;
+ internal IntPtr p;
+}
+
+internal struct StructWithReferenceTypes
+{
+ internal IntPtr ptr;
+ internal string str;
+ [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
+ internal int[] byValArr;
+}
+
+class Test
+{
+ static int Main(string[] args)
+ {
+ TestNegativeCases();
+ TestBlittableStruct();
+ TestStructWithReferenceType();
+
+ return 100;
+ }
+
+ static void TestNegativeCases()
+ {
+ Assert.Throws<ArgumentNullException>(() => { Marshal.WriteByte(null, 0, 0); });
+ Assert.Throws<ArgumentNullException>(() => { Marshal.WriteInt16(null, 0, 0); });
+ Assert.Throws<ArgumentNullException>(() => { Marshal.WriteInt32(null, 0, 0); });
+ Assert.Throws<ArgumentNullException>(() => { Marshal.WriteInt64(null, 0, 0); });
+ Assert.Throws<ArgumentNullException>(() => { Marshal.WriteIntPtr(null, 0, IntPtr.Zero); });
+ Assert.Throws<ArgumentNullException>(() => { Marshal.ReadByte(null, 0); });
+ Assert.Throws<ArgumentNullException>(() => { Marshal.ReadInt16(null, 0); });
+ Assert.Throws<ArgumentNullException>(() => { Marshal.ReadInt32(null, 0); });
+ Assert.Throws<ArgumentNullException>(() => { Marshal.ReadIntPtr(null, 0); });
+ }
+
+ static void TestBlittableStruct()
+ {
+ Console.WriteLine("TestBlittableStruct");
+
+ BlittableStruct blittableStruct = new BlittableStruct();
+ blittableStruct.a = 200;
+ blittableStruct.b = 300;
+ blittableStruct.c = 10;
+ blittableStruct.d = 123;
+ blittableStruct.p = new IntPtr(100);
+
+ object boxedBlittableStruct = (object)blittableStruct;
+
+ int offsetOfB = Marshal.OffsetOf<BlittableStruct>("b").ToInt32();
+ int offsetOfC = Marshal.OffsetOf<BlittableStruct>("c").ToInt32();
+ int offsetOfD = Marshal.OffsetOf<BlittableStruct>("d").ToInt32();
+ int offsetOfP = Marshal.OffsetOf<BlittableStruct>("p").ToInt32();
+
+ Assert.AreEqual(Marshal.ReadInt32(boxedBlittableStruct, 0), 200);
+ Assert.AreEqual(Marshal.ReadInt32(boxedBlittableStruct, offsetOfB), 300);
+ Assert.AreEqual(Marshal.ReadByte(boxedBlittableStruct, offsetOfC), 10);
+ Assert.AreEqual(Marshal.ReadInt16(boxedBlittableStruct, offsetOfD), 123);
+ Assert.AreEqual(Marshal.ReadIntPtr(boxedBlittableStruct, offsetOfP), new IntPtr(100));
+
+ Marshal.WriteInt32(boxedBlittableStruct, 0, 300);
+ Marshal.WriteInt32(boxedBlittableStruct, offsetOfB, 400);
+ Marshal.WriteByte(boxedBlittableStruct, offsetOfC, 20);
+ Marshal.WriteInt16(boxedBlittableStruct, offsetOfD, 144);
+
+ Marshal.WriteIntPtr(boxedBlittableStruct, offsetOfP, new IntPtr(500));
+
+ Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).a, 300);
+ Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).b, 400);
+ Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).c, 20);
+ Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).d, 144);
+ Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).p, new IntPtr(500));
+ }
+
+ static void TestStructWithReferenceType()
+ {
+ Console.WriteLine("TestStructWithReferenceType");
+
+ StructWithReferenceTypes structWithReferenceTypes = new StructWithReferenceTypes();
+ structWithReferenceTypes.ptr = new IntPtr(100);
+ structWithReferenceTypes.str = "ABC";
+ structWithReferenceTypes.byValArr = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ object boxedStruct = (object)structWithReferenceTypes;
+
+ int offsetOfStr = Marshal.OffsetOf<StructWithReferenceTypes>("str").ToInt32();
+ int offsetOfByValArr = Marshal.OffsetOf<StructWithReferenceTypes>("byValArr").ToInt32();
+
+ Assert.AreEqual(Marshal.ReadInt32(boxedStruct, 0), 100);
+ Assert.AreNotEqual(Marshal.ReadIntPtr(boxedStruct, offsetOfStr), IntPtr.Zero);
+ Assert.AreEqual(Marshal.ReadInt32(boxedStruct, offsetOfByValArr + sizeof(int) * 2), 3);
+
+ Marshal.WriteInt32(boxedStruct, 0, 200);
+ Marshal.WriteInt32(boxedStruct, offsetOfByValArr + sizeof(int) * 9, 100);
+
+ Assert.AreEqual(((StructWithReferenceTypes)boxedStruct).ptr, new IntPtr(200));
+ Assert.AreEqual(((StructWithReferenceTypes)boxedStruct).byValArr[9], 100);
+ Assert.AreEqual(((StructWithReferenceTypes)boxedStruct).byValArr[8], 9);
+ Assert.AreEqual(((StructWithReferenceTypes)boxedStruct).str, "ABC");
+ }
+}
+
diff --git a/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.csproj b/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.csproj
new file mode 100644
index 0000000000..14fcd2c97d
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.csproj
@@ -0,0 +1,40 @@
+<?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>ReadWriteObject</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="ReadWriteObject.cs" />
+ <Compile Include="..\..\common\Assertion.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\..\..\Common\CoreCLRTestLibrary\CoreCLRTestLibrary.csproj">
+ <Project>{c8c0dc74-fac4-45b1-81fe-70c4808366e0}</Project>
+ <Name>CoreCLRTestLibrary</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
diff --git a/tests/src/Interop/MarshalAPI/String/StringMarshalingTest.cs b/tests/src/Interop/MarshalAPI/String/StringMarshalingTest.cs
index 714dac83e4..29ee83a8c0 100644
--- a/tests/src/Interop/MarshalAPI/String/StringMarshalingTest.cs
+++ b/tests/src/Interop/MarshalAPI/String/StringMarshalingTest.cs
@@ -52,6 +52,39 @@ public class StringMarshalingTest
}
}
+ private unsafe void SecureStringToBSTRToString()
+ {
+ foreach (String ts in TestStrings)
+ {
+ SecureString secureString = new SecureString();
+ foreach (char character in ts)
+ {
+ secureString.AppendChar(character);
+ }
+
+ IntPtr BStr = IntPtr.Zero;
+ String str;
+
+ try
+ {
+ BStr = Marshal.SecureStringToBSTR(secureString);
+ str = Marshal.PtrToStringBSTR(BStr);
+ }
+ finally
+ {
+ if (BStr != IntPtr.Zero)
+ {
+ Marshal.ZeroFreeBSTR(BStr);
+ }
+ }
+
+ if (!str.Equals(ts))
+ {
+ throw new Exception();
+ }
+ }
+ }
+
private void StringToCoTaskMemAnsiToString()
{
foreach (String ts in TestStrings)
@@ -201,6 +234,7 @@ public class StringMarshalingTest
public bool RunTests()
{
StringToBStrToString();
+ SecureStringToBSTRToString();
StringToCoTaskMemAnsiToString();
StringToCoTaskMemUniToString();
StringToHGlobalAnsiToString();