summaryrefslogtreecommitdiff
path: root/tests/src/Interop
diff options
context:
space:
mode:
authortijoytom <tijoytk@microsoft.com>2016-03-10 15:19:19 -0800
committertijoytom <tijoytk@microsoft.com>2016-03-10 15:19:19 -0800
commitc3fc5138510fa1093b0dec56fede3615f10ceee2 (patch)
treef5d57933328d5e0eec300ff8599c607609a27189 /tests/src/Interop
parentc661bd0b7a6535713b4d7ec443c787ebb07c7688 (diff)
downloadcoreclr-c3fc5138510fa1093b0dec56fede3615f10ceee2.tar.gz
coreclr-c3fc5138510fa1093b0dec56fede3615f10ceee2.tar.bz2
coreclr-c3fc5138510fa1093b0dec56fede3615f10ceee2.zip
Couple of marshal API tests.
Diffstat (limited to 'tests/src/Interop')
-rw-r--r--tests/src/Interop/CMakeLists.txt4
-rw-r--r--tests/src/Interop/MarshalAPI/FunctionPointer/CMakeLists.txt12
-rw-r--r--tests/src/Interop/MarshalAPI/FunctionPointer/FunctionPointerNative.cpp23
-rw-r--r--tests/src/Interop/MarshalAPI/FunctionPointer/FunctionPtrTest.csproj47
-rw-r--r--tests/src/Interop/MarshalAPI/FunctionPointer/GetDelForFcnPtr_Negative_Catchable.cs90
-rw-r--r--tests/src/Interop/MarshalAPI/FunctionPointer/GetFcnPtrForDel_Negative_Catchable.cs42
-rw-r--r--tests/src/Interop/MarshalAPI/FunctionPointer/GetFcnPtrForDel_Negative_Security.cs85
-rw-r--r--tests/src/Interop/MarshalAPI/FunctionPointer/project.json34
-rw-r--r--tests/src/Interop/MarshalAPI/IUnknown/CMakeLists.txt12
-rw-r--r--tests/src/Interop/MarshalAPI/IUnknown/IUnknownNative.cpp18
-rw-r--r--tests/src/Interop/MarshalAPI/IUnknown/IUnknownTest.cs239
-rw-r--r--tests/src/Interop/MarshalAPI/IUnknown/IUnknownTest.csproj47
-rw-r--r--tests/src/Interop/MarshalAPI/IUnknown/project.json34
13 files changed, 686 insertions, 1 deletions
diff --git a/tests/src/Interop/CMakeLists.txt b/tests/src/Interop/CMakeLists.txt
index b9e9c1d835..1f221f3462 100644
--- a/tests/src/Interop/CMakeLists.txt
+++ b/tests/src/Interop/CMakeLists.txt
@@ -9,4 +9,6 @@ add_subdirectory(BestFitMapping)
add_subdirectory(RefInt)
add_subdirectory(RefCharArray)
add_subdirectory(StringMarshalling/LPSTR)
-add_subdirectory(StringMarshalling/LPTSTR) \ No newline at end of file
+add_subdirectory(StringMarshalling/LPTSTR)
+add_subdirectory(MarshalAPI/FunctionPointer)
+add_subdirectory(MarshalAPI/IUnknown) \ No newline at end of file
diff --git a/tests/src/Interop/MarshalAPI/FunctionPointer/CMakeLists.txt b/tests/src/Interop/MarshalAPI/FunctionPointer/CMakeLists.txt
new file mode 100644
index 0000000000..6a41f54e34
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/FunctionPointer/CMakeLists.txt
@@ -0,0 +1,12 @@
+cmake_minimum_required (VERSION 2.6)
+project (FunctionPointerNative)
+include_directories(${INC_PLATFORM_DIR})
+set(SOURCES FunctionPointerNative.cpp)
+
+# add the executable
+add_library (FunctionPointerNative SHARED ${SOURCES})
+
+# add the install targets
+install (TARGETS FunctionPointerNative DESTINATION bin)
+
+
diff --git a/tests/src/Interop/MarshalAPI/FunctionPointer/FunctionPointerNative.cpp b/tests/src/Interop/MarshalAPI/FunctionPointer/FunctionPointerNative.cpp
new file mode 100644
index 0000000000..4eeafcb4a3
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/FunctionPointer/FunctionPointerNative.cpp
@@ -0,0 +1,23 @@
+// 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>
+
+extern "C" DLL_EXPORT bool __cdecl CheckFcnPtr(bool(__stdcall *fcnptr)(__int64))
+{
+ if (fcnptr == 0)
+ {
+ printf("CheckFcnPtr: Unmanaged received a null function pointer");
+ return false;
+ }
+ else
+ {
+ return fcnptr(999999999999);
+ }
+}
+
+int Return100()
+{
+ return 100;
+} \ No newline at end of file
diff --git a/tests/src/Interop/MarshalAPI/FunctionPointer/FunctionPtrTest.csproj b/tests/src/Interop/MarshalAPI/FunctionPointer/FunctionPtrTest.csproj
new file mode 100644
index 0000000000..bea63f6dc2
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/FunctionPointer/FunctionPtrTest.csproj
@@ -0,0 +1,47 @@
+<?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>FunctionPtrTest</AssemblyName>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+ <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+ <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="*.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="project.json" />
+ </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>
+ <ProjectReference Include="CMakeLists.txt" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
diff --git a/tests/src/Interop/MarshalAPI/FunctionPointer/GetDelForFcnPtr_Negative_Catchable.cs b/tests/src/Interop/MarshalAPI/FunctionPointer/GetDelForFcnPtr_Negative_Catchable.cs
new file mode 100644
index 0000000000..3fea23f442
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/FunctionPointer/GetDelForFcnPtr_Negative_Catchable.cs
@@ -0,0 +1,90 @@
+// 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.Security;
+using System.Threading;
+using System.Globalization;
+using System.Runtime.InteropServices;
+
+public partial class FunctionPtr
+{
+
+ public static int RunGetDelForFcnPtrTest()
+ {
+ int retVal = 100;
+ VoidDelegate md = new VoidDelegate(FunctionPtr.Method);
+ IntPtr fcnptr = (IntPtr)0;
+ Console.WriteLine("\r\nTesting Marshal.GetDelegateForFunctionPointer().");
+
+ try
+ {
+ fcnptr = Marshal.GetFunctionPointerForDelegate<VoidDelegate>(md);
+ }
+ catch (Exception e)
+ {
+ retVal = 0;
+ Console.WriteLine("Exception during initialization: {0}", e.ToString());
+ return retVal;
+ }
+
+ try
+ {
+ Marshal.GetDelegateForFunctionPointer((IntPtr)0, typeof(MyDelegate));
+ retVal = 0;
+ Console.WriteLine("Failure - did not receive an exception while passing 0 as the function pointer");
+ }
+ catch (ArgumentNullException e)
+ {
+ Console.WriteLine("Pass - threw the right exception passing a 0 function pointer");
+ }
+ catch (Exception e)
+ {
+ retVal = 0;
+ Console.WriteLine("Failure - receive an incorrect exception while passing 0 as the function pointer");
+ Console.WriteLine(e);
+ }
+
+ try
+ {
+ Marshal.GetDelegateForFunctionPointer(fcnptr, null);
+ retVal = 0;
+ Console.WriteLine("Failure - did not receive an exception while passing a null type");
+ }
+ catch (ArgumentException e)
+ {
+ Console.WriteLine("Fail - passing a null type received the right exception type, wrong message, message was '{0}'", e.Message);
+ }
+ catch (Exception e)
+ {
+ retVal = 0;
+ Console.WriteLine("Failure - receive an incorrect exception while passing a null type");
+ Console.WriteLine(e);
+ }
+
+ try
+ {
+ Marshal.GetDelegateForFunctionPointer(fcnptr, typeof(Object));
+ retVal = 0;
+ Console.WriteLine("Failure - did not receive an exception while passing a non-delegate type");
+ }
+ catch (ArgumentException e)
+ {
+ Console.WriteLine("Faile - threw the right exception passing a non-delegate type, but a wrong message, message was '{0}'", e.Message);
+ }
+ catch (Exception e)
+ {
+ retVal = 0;
+ Console.WriteLine("Failure - receive an incorrect exception while passing a non-delegate type");
+ Console.WriteLine(e);
+ }
+
+ Console.WriteLine(retVal == 100 ? "Done - PASSED" : "Done - FAILED");
+ return retVal;
+ }
+
+ public static void Method()
+ {
+ Console.WriteLine("Simple method to get a delegate for");
+ }
+}
diff --git a/tests/src/Interop/MarshalAPI/FunctionPointer/GetFcnPtrForDel_Negative_Catchable.cs b/tests/src/Interop/MarshalAPI/FunctionPointer/GetFcnPtrForDel_Negative_Catchable.cs
new file mode 100644
index 0000000000..b33ea4c70c
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/FunctionPointer/GetFcnPtrForDel_Negative_Catchable.cs
@@ -0,0 +1,42 @@
+// 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.Security;
+using System.Threading;
+using System.Globalization;
+using System.Runtime.InteropServices;
+
+public partial class FunctionPtr
+{
+ delegate void VoidDelegate();
+
+ public static int Main()
+ {
+ RunGetFncSecTest();
+
+ int retVal = 100;
+ VoidDelegate md = new VoidDelegate(FunctionPtr.Method);
+ Console.WriteLine("\r\nTesting Marshal.GetFunctionPointerForDelegate().");
+
+ try
+ {
+ Marshal.GetFunctionPointerForDelegate<Object>(null);
+ retVal = 0;
+ Console.WriteLine("Failure - did not receive an exception while passing null as the delegate");
+ }
+ catch (ArgumentNullException e)
+ {
+ Console.WriteLine("Pass - threw the right exception passing null as the delegate");
+ }
+ catch (Exception e)
+ {
+ retVal = 0;
+ Console.WriteLine("Failure - receive an incorrect exception while passing null as the delegate");
+ Console.WriteLine(e);
+ }
+ RunGetDelForFcnPtrTest();
+ return retVal;
+ }
+
+} \ No newline at end of file
diff --git a/tests/src/Interop/MarshalAPI/FunctionPointer/GetFcnPtrForDel_Negative_Security.cs b/tests/src/Interop/MarshalAPI/FunctionPointer/GetFcnPtrForDel_Negative_Security.cs
new file mode 100644
index 0000000000..271b71ca63
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/FunctionPointer/GetFcnPtrForDel_Negative_Security.cs
@@ -0,0 +1,85 @@
+// 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.Security;
+using System.Runtime.InteropServices;
+
+partial class FunctionPtr
+{
+ [DllImport("FunctionPointerNative")]
+ public static extern bool CheckFcnPtr(IntPtr fcnptr);
+
+ public delegate bool DelegateWithLong(long l); //Singlecast delegate
+ public delegate void MultiDelegateWithLong(long l); //Multicast delegate
+
+ public static DelegateWithLong del = new DelegateWithLong(FunctionPtr.Method);
+ public static MultiDelegateWithLong multidel = new MultiDelegateWithLong(FunctionPtr.Method2);
+
+ private static IntPtr fcnptr;
+
+ public static int RunGetFncSecTest()
+ {
+ Console.WriteLine("\r\nTesting Marshal.GetDelegateForFunctionPointer().");
+
+ bool pass = true;
+
+ try
+ {
+ fcnptr = Marshal.GetFunctionPointerForDelegate<DelegateWithLong>(del);
+ if (CheckFcnPtr(fcnptr) == true)
+ {
+ Console.WriteLine("\tPass - singlecast case");
+ }
+ else
+ {
+ pass = false;
+ Console.WriteLine("\tFail - singlecast case, created a function pointer but the call failed");
+ }
+ }
+ catch (Exception e)
+ {
+ pass = false;
+ Console.WriteLine("\tFailure - singlecast case");
+ Console.WriteLine(e);
+ }
+
+ try
+ {
+ fcnptr = Marshal.GetFunctionPointerForDelegate<MultiDelegateWithLong>(multidel);
+ CheckFcnPtr(fcnptr);
+ Console.WriteLine("\tPass - multicast case");
+ }
+ catch (Exception e)
+ {
+ pass = false;
+ Console.WriteLine("\tFailure - multicast case");
+ Console.WriteLine(e);
+ }
+
+ if (pass)
+ {
+ Console.WriteLine("Pass - the base case");
+ return 100;
+ }
+ else
+ {
+ Console.WriteLine("Fail - the base case");
+ return 99;
+ }
+ }
+
+ public static bool Method(long l)
+ {
+ if (l != 999999999999)
+ return false;
+ else
+ return true;
+ }
+
+ public static void Method2(long l)
+ {
+ if (l != 999999999999)
+ throw new Exception("Failed multicast call");
+ }
+} \ No newline at end of file
diff --git a/tests/src/Interop/MarshalAPI/FunctionPointer/project.json b/tests/src/Interop/MarshalAPI/FunctionPointer/project.json
new file mode 100644
index 0000000000..0976b60932
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/FunctionPointer/project.json
@@ -0,0 +1,34 @@
+{
+ "dependencies": {
+ "Microsoft.NETCore.Platforms": "1.0.1-rc2-23816",
+ "System.Collections": "4.0.10-beta-23302",
+ "System.Collections.NonGeneric": "4.0.1-beta-23302",
+ "System.Collections.Specialized": "4.0.1-beta-23302",
+ "System.ComponentModel": "4.0.1-beta-23302",
+ "System.Console": "4.0.0-beta-23302",
+ "System.Diagnostics.Process": "4.0.0-beta-23302",
+ "System.Globalization": "4.0.10-beta-23302",
+ "System.Globalization.Calendars": "4.0.0-beta-23302",
+ "System.IO": "4.0.10-beta-23302",
+ "System.IO.FileSystem": "4.0.0-beta-23302",
+ "System.IO.FileSystem.Primitives": "4.0.0-beta-23302",
+ "System.Linq": "4.0.1-beta-23302",
+ "System.Linq.Queryable": "4.0.1-beta-23302",
+ "System.Reflection": "4.0.10-beta-23302",
+ "System.Reflection.Primitives": "4.0.0-beta-23302",
+ "System.Runtime": "4.0.20-beta-23302",
+ "System.Runtime.Extensions": "4.0.10-beta-23302",
+ "System.Runtime.Handles": "4.0.0-beta-23302",
+ "System.Runtime.InteropServices": "4.0.20-beta-23302",
+ "System.Runtime.Loader": "4.0.0-beta-23302",
+ "System.Text.Encoding": "4.0.10-beta-23302",
+ "System.Threading": "4.0.10-beta-23302",
+ "System.Xml.ReaderWriter": "4.0.11-beta-23302",
+ "System.Xml.XDocument": "4.0.11-beta-23302",
+ "System.Xml.XmlDocument": "4.0.1-beta-23302",
+ "System.Xml.XmlSerializer": "4.0.11-beta-23302"
+ },
+ "frameworks": {
+ "dnxcore50": {}
+ }
+}
diff --git a/tests/src/Interop/MarshalAPI/IUnknown/CMakeLists.txt b/tests/src/Interop/MarshalAPI/IUnknown/CMakeLists.txt
new file mode 100644
index 0000000000..af1afb5e2b
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/IUnknown/CMakeLists.txt
@@ -0,0 +1,12 @@
+cmake_minimum_required (VERSION 2.6)
+project (IUnknownNative)
+include_directories(${INC_PLATFORM_DIR})
+set(SOURCES IUnknownNative.cpp)
+
+# add the executable
+add_library (IUnknownNative SHARED ${SOURCES})
+
+# add the install targets
+install (TARGETS IUnknownNative DESTINATION bin)
+
+
diff --git a/tests/src/Interop/MarshalAPI/IUnknown/IUnknownNative.cpp b/tests/src/Interop/MarshalAPI/IUnknown/IUnknownNative.cpp
new file mode 100644
index 0000000000..3e2a76d81d
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/IUnknown/IUnknownNative.cpp
@@ -0,0 +1,18 @@
+// 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>
+
+extern "C" DLL_EXPORT BOOL __stdcall Marshal_IUnknown(/*[in]*/IUnknown *o)
+{
+ //Call AddRef and Release on the passed IUnknown
+ //test if the ref counts get updated as expected
+
+ ULONG refCount = o->AddRef();
+
+ if((refCount-1) != o->Release())
+ return FALSE;
+
+ return TRUE;
+}
diff --git a/tests/src/Interop/MarshalAPI/IUnknown/IUnknownTest.cs b/tests/src/Interop/MarshalAPI/IUnknown/IUnknownTest.cs
new file mode 100644
index 0000000000..8ca31f2d95
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/IUnknown/IUnknownTest.cs
@@ -0,0 +1,239 @@
+// 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.IO;
+using System.Reflection;
+using System.Security;
+using System.Runtime.InteropServices;
+using System.Collections.Generic;
+
+
+public class TestClass
+{
+ public int value;
+ public string str;
+
+ public TestClass()
+ {
+ value = int.MaxValue;
+ str = "Test String";
+ }
+
+ public override string ToString()
+ {
+ return str + value;
+ }
+}
+
+public class IUnknownMarshalingTest
+{
+ [DllImport(@"IUnknownNative", CallingConvention = CallingConvention.Cdecl)]
+ private static extern bool Marshal_IUnknown([In]IntPtr ptr);
+
+ private object[] TestObjects;
+
+ public void GetIUnknownForObjectTest()
+ {
+ try
+ {
+ //test null
+ IntPtr nullPtr = Marshal.GetIUnknownForObject(null);
+ }
+ catch (ArgumentNullException) { }
+
+ foreach (object obj in TestObjects)
+ {
+ IntPtr ptr = IntPtr.Zero;
+
+ try
+ {
+ ptr = Marshal.GetIUnknownForObject(obj);
+
+ if (!Marshal_IUnknown(ptr))
+ {
+ throw new Exception("Failure on native side. Ref counts do not work as expected");
+ }
+ }
+ finally
+ {
+ if (ptr != IntPtr.Zero)
+ Marshal.Release(ptr);
+ }
+ }
+ }
+
+ public void GetComInterfaceForObjectTest()
+ {
+ //test null
+ IntPtr nullPtr = Marshal.GetComInterfaceForObject(null, typeof(object));
+ if (nullPtr != IntPtr.Zero)
+ throw new Exception("A valid ptr was returned for null object.");
+
+ foreach (object obj in TestObjects)
+ {
+ IntPtr ptr = IntPtr.Zero;
+
+ try
+ {
+ ptr = Marshal.GetComInterfaceForObject(obj, typeof(object));
+
+ if (!Marshal_IUnknown(ptr))
+ {
+ throw new Exception("Failure on native side. Ref counts do not work as expected");
+ }
+ }
+ finally
+ {
+ if (ptr != IntPtr.Zero)
+ Marshal.Release(ptr);
+ }
+ }
+ }
+
+ public void GetComInterfaceForObjectQueryInterfaceTest()
+ {
+ try
+ {
+ //test null
+ IntPtr nullPtr = Marshal.GetComInterfaceForObject(null, typeof(object), CustomQueryInterfaceMode.Allow);
+
+ if (nullPtr != IntPtr.Zero)
+ throw new Exception("A valid ptr was returned for null object.");
+ }
+ catch (Exception ex)
+ {
+ TestTools.ErrorWriteLine("Failed GetComInterfaceForObjectQueryInterface test.");
+ TestTools.ErrorWriteLine("Exception occurred: {0}", ex);
+ }
+
+ foreach (object obj in TestObjects)
+ {
+ IntPtr ptr = IntPtr.Zero;
+
+ try
+ {
+ ptr = Marshal.GetComInterfaceForObject(obj, typeof(object), CustomQueryInterfaceMode.Allow);
+
+ if (!Marshal_IUnknown(ptr))
+ {
+ throw new Exception("Failure on native side. Ref counts do not work as expected");
+ }
+ }
+ catch (Exception ex)
+ {
+ TestTools.ErrorWriteLine("Failed GetComInterfaceForObjectQueryInterface test.");
+ TestTools.ErrorWriteLine("Exception occurred: {0}", ex);
+ }
+ finally
+ {
+ if (ptr != IntPtr.Zero)
+ Marshal.Release(ptr);
+ }
+ }
+ }
+
+ public void GetObjectForIUnknownTest()
+ {
+ try
+ {
+ //test IntPtr.Zero
+ Object nullObj = Marshal.GetObjectForIUnknown(IntPtr.Zero);
+
+ }
+ catch (ArgumentNullException) { }
+
+ foreach (object obj in TestObjects)
+ {
+ IntPtr ptr = IntPtr.Zero;
+
+ try
+ {
+ ptr = Marshal.GetIUnknownForObject(obj);
+
+ Object tmpObj = Marshal.GetObjectForIUnknown(ptr);
+
+ //compare the new object reference with the original object, they should point to the same underlying object
+ if (!object.ReferenceEquals(obj, tmpObj))
+ throw new Exception("GetObjectForIUnknown returned a different object. Original: " + obj + ", New: " + tmpObj);
+ }
+ finally
+ {
+ if (ptr != IntPtr.Zero)
+ Marshal.Release(ptr);
+ }
+ }
+ }
+
+ public void GetUniqueObjectForIUnknownTest()
+ {
+
+ //test IntPtr.Zero
+ Object nullObj = Marshal.GetUniqueObjectForIUnknown(IntPtr.Zero);
+
+ if (nullObj != null)
+ throw new Exception("Object returned for IntPtr.Zero is not null.");
+
+
+ foreach (object obj in TestObjects)
+ {
+ IntPtr ptr = IntPtr.Zero;
+ object tmpObj = null;
+
+ try
+ {
+ ptr = Marshal.GetIUnknownForObject(obj);
+
+ tmpObj = Marshal.GetUniqueObjectForIUnknown(ptr);
+
+ //compare the new object reference with the original object, they should point to differnet objects
+ if (object.ReferenceEquals(obj, tmpObj))
+ throw new Exception("GetUniqueObjectForIUnknown returned the original object");
+
+ //The value should be the same
+ if (!obj.Equals(tmpObj))
+ throw new Exception("GetUniqueObjectForIUnknown returned an object with different value. Original: " + obj + ", New: " + tmpObj);
+
+ }
+ finally
+ {
+ if (tmpObj != null)
+ Marshal.ReleaseComObject(tmpObj);
+ if (ptr != IntPtr.Zero)
+ Marshal.Release(ptr);
+ }
+ }
+ }
+
+ public bool RunTests()
+ {
+ Initialize();
+ GetIUnknownForObjectTest();
+ GetObjectForIUnknownTest();
+ return TestTools.Pass;
+ }
+
+ public bool Initialize()
+ {
+
+
+ TestObjects = new object[8];
+
+ TestObjects[0] = 1; //int
+ TestObjects[1] = 'a'; //char
+ TestObjects[2] = false; //bool
+ TestObjects[3] = "string"; //string
+ TestObjects[4] = new TestClass(); //Object of type TestClass
+ TestObjects[5] = new List<int>(); //Projected Type
+ TestObjects[6] = new Nullable<int>(2); //Nullable Type
+ TestObjects[7] = new PropertySet(); //RCW Type
+ return true;
+ }
+
+ public static int Main(String[] unusedArgs)
+ {
+ new IUnknownMarshalingTest().RunTests();
+ return 100;
+ }
+
+}
diff --git a/tests/src/Interop/MarshalAPI/IUnknown/IUnknownTest.csproj b/tests/src/Interop/MarshalAPI/IUnknown/IUnknownTest.csproj
new file mode 100644
index 0000000000..da3accdf1f
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/IUnknown/IUnknownTest.csproj
@@ -0,0 +1,47 @@
+<?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>IUnknownTest</AssemblyName>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+ <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+ <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="*.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="project.json" />
+ </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>
+ <ProjectReference Include="CMakeLists.txt" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
diff --git a/tests/src/Interop/MarshalAPI/IUnknown/project.json b/tests/src/Interop/MarshalAPI/IUnknown/project.json
new file mode 100644
index 0000000000..0976b60932
--- /dev/null
+++ b/tests/src/Interop/MarshalAPI/IUnknown/project.json
@@ -0,0 +1,34 @@
+{
+ "dependencies": {
+ "Microsoft.NETCore.Platforms": "1.0.1-rc2-23816",
+ "System.Collections": "4.0.10-beta-23302",
+ "System.Collections.NonGeneric": "4.0.1-beta-23302",
+ "System.Collections.Specialized": "4.0.1-beta-23302",
+ "System.ComponentModel": "4.0.1-beta-23302",
+ "System.Console": "4.0.0-beta-23302",
+ "System.Diagnostics.Process": "4.0.0-beta-23302",
+ "System.Globalization": "4.0.10-beta-23302",
+ "System.Globalization.Calendars": "4.0.0-beta-23302",
+ "System.IO": "4.0.10-beta-23302",
+ "System.IO.FileSystem": "4.0.0-beta-23302",
+ "System.IO.FileSystem.Primitives": "4.0.0-beta-23302",
+ "System.Linq": "4.0.1-beta-23302",
+ "System.Linq.Queryable": "4.0.1-beta-23302",
+ "System.Reflection": "4.0.10-beta-23302",
+ "System.Reflection.Primitives": "4.0.0-beta-23302",
+ "System.Runtime": "4.0.20-beta-23302",
+ "System.Runtime.Extensions": "4.0.10-beta-23302",
+ "System.Runtime.Handles": "4.0.0-beta-23302",
+ "System.Runtime.InteropServices": "4.0.20-beta-23302",
+ "System.Runtime.Loader": "4.0.0-beta-23302",
+ "System.Text.Encoding": "4.0.10-beta-23302",
+ "System.Threading": "4.0.10-beta-23302",
+ "System.Xml.ReaderWriter": "4.0.11-beta-23302",
+ "System.Xml.XDocument": "4.0.11-beta-23302",
+ "System.Xml.XmlDocument": "4.0.1-beta-23302",
+ "System.Xml.XmlSerializer": "4.0.11-beta-23302"
+ },
+ "frameworks": {
+ "dnxcore50": {}
+ }
+}