summaryrefslogtreecommitdiff
path: root/tests/src/Interop
diff options
context:
space:
mode:
authorSwaroop Sridhar <Swaroop.Sridhar@microsoft.com>2019-01-05 16:40:59 -0800
committerJan Kotas <jkotas@microsoft.com>2019-01-05 16:40:59 -0800
commit110835b1b818b333f27ab76db3f223a03027698a (patch)
tree72b080215d0ade7e583ba2e1560e21548d54fedb /tests/src/Interop
parentdcf9f36c21022b8fce374559ec5fea8d11a0852b (diff)
downloadcoreclr-110835b1b818b333f27ab76db3f223a03027698a.tar.gz
coreclr-110835b1b818b333f27ab76db3f223a03027698a.tar.bz2
coreclr-110835b1b818b333f27ab76db3f223a03027698a.zip
Move Native Library APIs to NativeLibrary class (#21821)
Move APIs that manipulate native libraries from System.Runtime.InteropServices.Marshal class to the new System.Runtime.InteropServices.NativeLibrary class. In API review: #32015, The LoadLibrary APIs were originally Approved to live in System.Runtime.InteropServices.Marshal class. However, recently the decision was changed such that the APIs are in a new NativeLibrary class.
Diffstat (limited to 'tests/src/Interop')
-rw-r--r--tests/src/Interop/CMakeLists.txt2
-rw-r--r--tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibraryTests.cs385
-rw-r--r--tests/src/Interop/NativeLibrary/CMakeLists.txt (renamed from tests/src/Interop/MarshalAPI/NativeLibrary/CMakeLists.txt)0
-rw-r--r--tests/src/Interop/NativeLibrary/NativeLibrary.cpp (renamed from tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibrary.cpp)0
-rw-r--r--tests/src/Interop/NativeLibrary/NativeLibraryTests.cs395
-rw-r--r--tests/src/Interop/NativeLibrary/NativeLibraryTests.csproj (renamed from tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibraryTests.csproj)2
6 files changed, 397 insertions, 387 deletions
diff --git a/tests/src/Interop/CMakeLists.txt b/tests/src/Interop/CMakeLists.txt
index 5685e79762..9e0bf85e9c 100644
--- a/tests/src/Interop/CMakeLists.txt
+++ b/tests/src/Interop/CMakeLists.txt
@@ -55,7 +55,7 @@ add_subdirectory(StringMarshalling/AnsiBSTR)
add_subdirectory(StringMarshalling/VBByRefStr)
add_subdirectory(MarshalAPI/FunctionPointer)
add_subdirectory(MarshalAPI/IUnknown)
-add_subdirectory(MarshalAPI/NativeLibrary)
+add_subdirectory(NativeLibrary)
add_subdirectory(SizeConst)
add_subdirectory(DllImportAttribute/ExeFile)
add_subdirectory(DllImportAttribute/FileNameContainDot)
diff --git a/tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibraryTests.cs b/tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibraryTests.cs
deleted file mode 100644
index dfc7335b30..0000000000
--- a/tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibraryTests.cs
+++ /dev/null
@@ -1,385 +0,0 @@
-// 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.Runtime.InteropServices;
-using TestLibrary;
-
-using Console = Internal.Console;
-
-enum TestResult {
- Success,
- ReturnFailure,
- ReturnNull,
- IncorrectEvaluation,
- ArgumentNull,
- ArgumentBad,
-
- DllNotFound,
- BadImage,
- InvalidOperation,
- EntryPointNotFound,
- GenericException
- };
-
-public class NativeLibraryTest
-{
- static string CurrentTest;
- static bool Verbose = false;
-
- public static int Main()
- {
- bool success = true;
-
- Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
- string testBinDir = Path.GetDirectoryName(assembly.Location);
- string libName;
- IntPtr handle;
-
- // -----------------------------------------------
- // Simple LoadLibrary() API Tests
- // -----------------------------------------------
-
- // Calls on correct full-path to native lib
- libName = Path.Combine(testBinDir, GetNativeLibraryName());
- success &= EXPECT(LoadLibrarySimple(libName));
- success &= EXPECT(TryLoadLibrarySimple(libName));
-
- // Calls on non-existant file
- libName = Path.Combine(testBinDir, "notfound");
- success &= EXPECT(LoadLibrarySimple(libName), TestResult.DllNotFound);
- success &= EXPECT(TryLoadLibrarySimple(libName), TestResult.ReturnFailure);
-
- // Calls on an invalid file
- libName = Path.Combine(testBinDir, "NativeLibrary.cpp");
- success &= EXPECT(LoadLibrarySimple(libName),
- (TestLibrary.Utilities.IsWindows) ? TestResult.BadImage : TestResult.DllNotFound);
- success &= EXPECT(TryLoadLibrarySimple(libName), TestResult.ReturnFailure);
-
- // Calls on null input
- libName = null;
- success &= EXPECT(LoadLibrarySimple(libName), TestResult.ArgumentNull);
- success &= EXPECT(TryLoadLibrarySimple(libName), TestResult.ArgumentNull);
-
- // -----------------------------------------------
- // Advanced LoadLibrary() API Tests
- // -----------------------------------------------
-
- // Advanced LoadLibrary() API Tests
- // Calls on correct full-path to native lib
- libName = Path.Combine(testBinDir, GetNativeLibraryName());
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null));
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null));
-
- // Calls on non-existant file
- libName = Path.Combine(testBinDir, "notfound");
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null), TestResult.DllNotFound);
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null), TestResult.ReturnFailure);
-
- // Calls on an invalid file
- libName = Path.Combine(testBinDir, "NativeLibrary.cpp");
- // The VM can only distinguish BadImageFormatException from DllNotFoundException on Windows.
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null),
- (TestLibrary.Utilities.IsWindows) ? TestResult.BadImage : TestResult.DllNotFound);
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null), TestResult.ReturnFailure);
-
- // Calls on just Native Library name
- libName = GetNativeLibraryPlainName();
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null));
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null));
-
- // Calls on Native Library name with correct prefix-suffix
- libName = GetNativeLibraryName();
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null));
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null));
-
- // Calls on full path without prefix-siffix
- libName = Path.Combine(testBinDir, GetNativeLibraryPlainName());
- // DllImport doesn't add a prefix if the name is preceeded by a path specification.
- // Windows only needs a suffix, but Linux and Mac need both prefix and suffix
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null),
- (TestLibrary.Utilities.IsWindows) ? TestResult.Success : TestResult.DllNotFound);
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null),
- (TestLibrary.Utilities.IsWindows) ? TestResult.Success : TestResult.ReturnFailure);
-
- if (TestLibrary.Utilities.IsWindows)
- {
- libName = GetWin32LibName();
-
- // Calls on a valid library from System32 directory
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.System32));
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.System32));
-
- // Calls on a valid library from application directory
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.ApplicationDirectory), TestResult.DllNotFound);
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.ApplicationDirectory), TestResult.ReturnFailure);
- }
-
- // Calls with null libName input
- success &= EXPECT(LoadLibraryAdvanced(null, assembly, null), TestResult.ArgumentNull);
- success &= EXPECT(TryLoadLibraryAdvanced(null, assembly, null), TestResult.ArgumentNull);
-
- // Calls with null assembly
- libName = GetNativeLibraryPlainName();
- success &= EXPECT(LoadLibraryAdvanced(libName, null, null), TestResult.ArgumentNull);
- success &= EXPECT(TryLoadLibraryAdvanced(libName, null, null), TestResult.ArgumentNull);
-
- // Ensure that a lib is not picked up from current directory when
- // a different full-path is specified.
- libName = Path.Combine(testBinDir, Path.Combine("lib", GetNativeLibraryPlainName()));
- success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory), TestResult.DllNotFound);
- success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory), TestResult.ReturnFailure);
-
- // -----------------------------------------------
- // FreeLibrary Tests
- // -----------------------------------------------
-
- libName = Path.Combine(testBinDir, GetNativeLibraryName());
- handle = Marshal.LoadLibrary(libName);
-
- // Valid Free
- success &= EXPECT(FreeLibrary(handle));
-
- // Double Free
- success &= EXPECT(FreeLibrary(handle), TestResult.InvalidOperation);
-
- // Null Free
- success &= EXPECT(FreeLibrary(IntPtr.Zero));
-
- // -----------------------------------------------
- // GetLibraryExport Tests
- // -----------------------------------------------
- libName = Path.Combine(testBinDir, GetNativeLibraryName());
- handle = Marshal.LoadLibrary(libName);
-
- // Valid Call (with some hard-coded name mangling)
- success &= EXPECT(GetLibraryExport(handle, TestLibrary.Utilities.IsX86 ? "_NativeSum@8" : "NativeSum"));
- success &= EXPECT(TryGetLibraryExport(handle, TestLibrary.Utilities.IsX86 ? "_NativeSum@8" : "NativeSum"));
-
- // Call with null handle
- success &= EXPECT(GetLibraryExport(IntPtr.Zero, "NativeSum"), TestResult.ArgumentNull);
- success &= EXPECT(TryGetLibraryExport(IntPtr.Zero, "NativeSum"), TestResult.ArgumentNull);
-
- // Call with null string
- success &= EXPECT(GetLibraryExport(handle, null), TestResult.ArgumentNull);
- success &= EXPECT(TryGetLibraryExport(handle, null), TestResult.ArgumentNull);
-
- // Call with wrong string
- success &= EXPECT(GetLibraryExport(handle, "NonNativeSum"), TestResult.EntryPointNotFound);
- success &= EXPECT(TryGetLibraryExport(handle, "NonNativeSum"), TestResult.ReturnFailure);
-
- Marshal.FreeLibrary(handle);
-
- return (success) ? 100 : -100;
- }
-
- static string GetNativeLibraryPlainName()
- {
- return "NativeLibrary";
- }
-
- static string GetWin32LibName()
- {
- return "msi.dll";
- }
-
- static string GetNativeLibraryName()
- {
- string baseName = GetNativeLibraryPlainName();
-
- if (TestLibrary.Utilities.IsWindows)
- {
- return baseName + ".dll";
- }
- if (TestLibrary.Utilities.IsLinux)
- {
- return "lib" + baseName + ".so";
- }
- if (TestLibrary.Utilities.IsMacOSX)
- {
- return "lib" + baseName + ".dylib";
- }
-
- return "ERROR";
- }
-
- static bool EXPECT(TestResult actualValue, TestResult expectedValue = TestResult.Success)
- {
- if (actualValue == expectedValue)
- {
- if (Verbose)
- Console.WriteLine(String.Format("{0} : {1} : [OK]", CurrentTest, actualValue));
- return true;
- }
- else
- {
- Console.WriteLine(String.Format(" {0} : {1} : [FAIL]", CurrentTest, actualValue));
- return false;
- }
- }
-
- static TestResult Run (Func<TestResult> test)
- {
-
- TestResult result;
-
- try
- {
- result = test();
- }
- catch (ArgumentNullException e)
- {
- return TestResult.ArgumentNull;
- }
- catch (ArgumentException e)
- {
- return TestResult.ArgumentBad;
- }
- catch (DllNotFoundException e)
- {
- return TestResult.DllNotFound;
- }
- catch (BadImageFormatException e)
- {
- return TestResult.BadImage;
- }
- catch (InvalidOperationException e)
- {
- return TestResult.InvalidOperation;
- }
- catch (EntryPointNotFoundException e)
- {
- return TestResult.EntryPointNotFound;
- }
- catch (Exception e)
- {
- //Console.WriteLine(e.ToString());
- return TestResult.GenericException;
- }
-
- return result;
- }
-
- static TestResult LoadLibrarySimple(string libPath)
- {
- CurrentTest = String.Format("LoadLibrary({0})", libPath);
-
- IntPtr handle = IntPtr.Zero;
-
- TestResult result = Run(() => {
- handle = Marshal.LoadLibrary(libPath);
- if (handle == IntPtr.Zero)
- return TestResult.ReturnNull;
- return TestResult.Success;
- });
-
- Marshal.FreeLibrary(handle);
-
- return result;
- }
-
- static TestResult TryLoadLibrarySimple(string libPath)
- {
- CurrentTest = String.Format("TryLoadLibrary({0})", libPath);
-
- IntPtr handle = IntPtr.Zero;
-
- TestResult result = Run(() => {
- bool success = Marshal.TryLoadLibrary(libPath, out handle);
- if(!success)
- return TestResult.ReturnFailure;
- if (handle == null)
- return TestResult.ReturnNull;
- return TestResult.Success;
- });
-
- Marshal.FreeLibrary(handle);
-
- return result;
- }
-
-
- static TestResult LoadLibraryAdvanced(string libName, Assembly assembly, DllImportSearchPath? searchPath)
- {
- CurrentTest = String.Format("LoadLibrary({0}, {1}, {2})", libName, assembly, searchPath);
-
- IntPtr handle = IntPtr.Zero;
-
- TestResult result = Run(() => {
- handle = Marshal.LoadLibrary(libName, assembly, searchPath);
- if (handle == IntPtr.Zero)
- return TestResult.ReturnNull;
- return TestResult.Success;
- });
-
- Marshal.FreeLibrary(handle);
-
- return result;
- }
-
- static TestResult TryLoadLibraryAdvanced(string libName, Assembly assembly, DllImportSearchPath? searchPath)
- {
- CurrentTest = String.Format("TryLoadLibrary({0}, {1}, {2})", libName, assembly, searchPath);
-
- IntPtr handle = IntPtr.Zero;
-
- TestResult result = Run(() => {
- bool success = Marshal.TryLoadLibrary(libName, assembly, searchPath, out handle);
- if (!success)
- return TestResult.ReturnFailure;
- if (handle == IntPtr.Zero)
- return TestResult.ReturnNull;
- return TestResult.Success;
- });
-
- Marshal.FreeLibrary(handle);
-
- return result;
- }
-
- static TestResult FreeLibrary(IntPtr handle)
- {
- CurrentTest = String.Format("FreeLibrary({0})", handle);
-
- return Run(() => {
- Marshal.FreeLibrary(handle);
- return TestResult.Success;
- });
- }
-
- static TestResult GetLibraryExport(IntPtr handle, string name)
- {
- CurrentTest = String.Format("GetLibraryExport({0}, {1})", handle, name);
-
- return Run(() => {
- IntPtr address = Marshal.GetLibraryExport(handle, name);
- if (address == null)
- return TestResult.ReturnNull;
- if (RunExportedFunction(address, 1, 1) != 2)
- return TestResult.IncorrectEvaluation;
- return TestResult.Success;
- });
- }
-
- static TestResult TryGetLibraryExport(IntPtr handle, string name)
- {
- CurrentTest = String.Format("TryGetLibraryExport({0}, {1})", handle, name);
-
- return Run(() => {
- IntPtr address = IntPtr.Zero;
- bool success = Marshal.TryGetLibraryExport(handle, name, out address);
- if (!success)
- return TestResult.ReturnFailure;
- if (address == null)
- return TestResult.ReturnNull;
- if (RunExportedFunction(address, 1, 1) != 2)
- return TestResult.IncorrectEvaluation;
- return TestResult.Success;
- });
- }
-
- [DllImport("NativeLibrary")]
- static extern int RunExportedFunction(IntPtr address, int arg1, int arg2);
-}
diff --git a/tests/src/Interop/MarshalAPI/NativeLibrary/CMakeLists.txt b/tests/src/Interop/NativeLibrary/CMakeLists.txt
index f7d98ba507..f7d98ba507 100644
--- a/tests/src/Interop/MarshalAPI/NativeLibrary/CMakeLists.txt
+++ b/tests/src/Interop/NativeLibrary/CMakeLists.txt
diff --git a/tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibrary.cpp b/tests/src/Interop/NativeLibrary/NativeLibrary.cpp
index 7c1090f10f..7c1090f10f 100644
--- a/tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibrary.cpp
+++ b/tests/src/Interop/NativeLibrary/NativeLibrary.cpp
diff --git a/tests/src/Interop/NativeLibrary/NativeLibraryTests.cs b/tests/src/Interop/NativeLibrary/NativeLibraryTests.cs
new file mode 100644
index 0000000000..19eb125c32
--- /dev/null
+++ b/tests/src/Interop/NativeLibrary/NativeLibraryTests.cs
@@ -0,0 +1,395 @@
+// 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.Runtime.InteropServices;
+using TestLibrary;
+
+using Console = Internal.Console;
+
+enum TestResult {
+ Success,
+ ReturnFailure,
+ ReturnNull,
+ IncorrectEvaluation,
+ ArgumentNull,
+ ArgumentBad,
+ DllNotFound,
+ BadImage,
+ InvalidOperation,
+ EntryPointNotFound,
+ GenericException
+ };
+
+public class NativeLibraryTest
+{
+ static string CurrentTest;
+ static bool Verbose = false;
+
+ public static int Main()
+ {
+ bool success = true;
+
+ Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
+ string testBinDir = Path.GetDirectoryName(assembly.Location);
+ string libName;
+ IntPtr handle;
+
+ try
+ {
+ // -----------------------------------------------
+ // Simple LoadLibrary() API Tests
+ // -----------------------------------------------
+
+ // Calls on correct full-path to native lib
+ libName = Path.Combine(testBinDir, GetNativeLibraryName());
+ success &= EXPECT(LoadLibrarySimple(libName));
+ success &= EXPECT(TryLoadLibrarySimple(libName));
+
+ // Calls on non-existant file
+ libName = Path.Combine(testBinDir, "notfound");
+ success &= EXPECT(LoadLibrarySimple(libName), TestResult.DllNotFound);
+ success &= EXPECT(TryLoadLibrarySimple(libName), TestResult.ReturnFailure);
+
+ // Calls on an invalid file
+ libName = Path.Combine(testBinDir, "NativeLibrary.cpp");
+ success &= EXPECT(LoadLibrarySimple(libName),
+ (TestLibrary.Utilities.IsWindows) ? TestResult.BadImage : TestResult.DllNotFound);
+ success &= EXPECT(TryLoadLibrarySimple(libName), TestResult.ReturnFailure);
+
+ // Calls on null input
+ libName = null;
+ success &= EXPECT(LoadLibrarySimple(libName), TestResult.ArgumentNull);
+ success &= EXPECT(TryLoadLibrarySimple(libName), TestResult.ArgumentNull);
+
+ // -----------------------------------------------
+ // Advanced LoadLibrary() API Tests
+ // -----------------------------------------------
+
+ // Advanced LoadLibrary() API Tests
+ // Calls on correct full-path to native lib
+ libName = Path.Combine(testBinDir, GetNativeLibraryName());
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null));
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null));
+
+ // Calls on non-existant file
+ libName = Path.Combine(testBinDir, "notfound");
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null), TestResult.DllNotFound);
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null), TestResult.ReturnFailure);
+
+ // Calls on an invalid file
+ libName = Path.Combine(testBinDir, "NativeLibrary.cpp");
+ // The VM can only distinguish BadImageFormatException from DllNotFoundException on Windows.
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null),
+ (TestLibrary.Utilities.IsWindows) ? TestResult.BadImage : TestResult.DllNotFound);
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null), TestResult.ReturnFailure);
+
+ // Calls on just Native Library name
+ libName = GetNativeLibraryPlainName();
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null));
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null));
+
+ // Calls on Native Library name with correct prefix-suffix
+ libName = GetNativeLibraryName();
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null));
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null));
+
+ // Calls on full path without prefix-siffix
+ libName = Path.Combine(testBinDir, GetNativeLibraryPlainName());
+ // DllImport doesn't add a prefix if the name is preceeded by a path specification.
+ // Windows only needs a suffix, but Linux and Mac need both prefix and suffix
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, null),
+ (TestLibrary.Utilities.IsWindows) ? TestResult.Success : TestResult.DllNotFound);
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, null),
+ (TestLibrary.Utilities.IsWindows) ? TestResult.Success : TestResult.ReturnFailure);
+
+ if (TestLibrary.Utilities.IsWindows)
+ {
+ libName = GetWin32LibName();
+
+ // Calls on a valid library from System32 directory
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.System32));
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.System32));
+
+ // Calls on a valid library from application directory
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.ApplicationDirectory), TestResult.DllNotFound);
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.ApplicationDirectory), TestResult.ReturnFailure);
+ }
+
+ // Calls with null libName input
+ success &= EXPECT(LoadLibraryAdvanced(null, assembly, null), TestResult.ArgumentNull);
+ success &= EXPECT(TryLoadLibraryAdvanced(null, assembly, null), TestResult.ArgumentNull);
+
+ // Calls with null assembly
+ libName = GetNativeLibraryPlainName();
+ success &= EXPECT(LoadLibraryAdvanced(libName, null, null), TestResult.ArgumentNull);
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, null, null), TestResult.ArgumentNull);
+
+ // Ensure that a lib is not picked up from current directory when
+ // a different full-path is specified.
+ libName = Path.Combine(testBinDir, Path.Combine("lib", GetNativeLibraryPlainName()));
+ success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory), TestResult.DllNotFound);
+ success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory), TestResult.ReturnFailure);
+
+ // -----------------------------------------------
+ // FreeLibrary Tests
+ // -----------------------------------------------
+
+ libName = Path.Combine(testBinDir, GetNativeLibraryName());
+ handle = NativeLibrary.Load(libName);
+
+ // Valid Free
+ success &= EXPECT(FreeLibrary(handle));
+
+ // Double Free
+ success &= EXPECT(FreeLibrary(handle), TestResult.InvalidOperation);
+
+ // Null Free
+ success &= EXPECT(FreeLibrary(IntPtr.Zero));
+
+ // -----------------------------------------------
+ // GetLibraryExport Tests
+ // -----------------------------------------------
+ libName = Path.Combine(testBinDir, GetNativeLibraryName());
+ handle = NativeLibrary.Load(libName);
+
+ // Valid Call (with some hard-coded name mangling)
+ success &= EXPECT(GetLibraryExport(handle, TestLibrary.Utilities.IsX86 ? "_NativeSum@8" : "NativeSum"));
+ success &= EXPECT(TryGetLibraryExport(handle, TestLibrary.Utilities.IsX86 ? "_NativeSum@8" : "NativeSum"));
+
+ // Call with null handle
+ success &= EXPECT(GetLibraryExport(IntPtr.Zero, "NativeSum"), TestResult.ArgumentNull);
+ success &= EXPECT(TryGetLibraryExport(IntPtr.Zero, "NativeSum"), TestResult.ArgumentNull);
+
+ // Call with null string
+ success &= EXPECT(GetLibraryExport(handle, null), TestResult.ArgumentNull);
+ success &= EXPECT(TryGetLibraryExport(handle, null), TestResult.ArgumentNull);
+
+ // Call with wrong string
+ success &= EXPECT(GetLibraryExport(handle, "NonNativeSum"), TestResult.EntryPointNotFound);
+ success &= EXPECT(TryGetLibraryExport(handle, "NonNativeSum"), TestResult.ReturnFailure);
+
+ NativeLibrary.Free(handle);
+ }
+ catch (Exception e)
+ {
+ // Catch any exceptions in NativeLibrary calls directly within this function.
+ // These calls are used to setup the environment for tests that follow, and are not expected to fail.
+ // If they do fail (ex: incorrect build environment) fail with an error code, rather than segmentation fault.
+ Console.WriteLine(String.Format("Unhandled exception {0}", e));
+ success = false;
+ }
+
+ return (success) ? 100 : -100;
+ }
+
+ static string GetNativeLibraryPlainName()
+ {
+ return "NativeLibrary";
+ }
+
+ static string GetWin32LibName()
+ {
+ return "msi.dll";
+ }
+
+ static string GetNativeLibraryName()
+ {
+ string baseName = GetNativeLibraryPlainName();
+
+ if (TestLibrary.Utilities.IsWindows)
+ {
+ return baseName + ".dll";
+ }
+ if (TestLibrary.Utilities.IsLinux)
+ {
+ return "lib" + baseName + ".so";
+ }
+ if (TestLibrary.Utilities.IsMacOSX)
+ {
+ return "lib" + baseName + ".dylib";
+ }
+
+ return "ERROR";
+ }
+
+ static bool EXPECT(TestResult actualValue, TestResult expectedValue = TestResult.Success)
+ {
+ if (actualValue == expectedValue)
+ {
+ if (Verbose)
+ Console.WriteLine(String.Format("{0} : {1} : [OK]", CurrentTest, actualValue));
+ return true;
+ }
+ else
+ {
+ Console.WriteLine(String.Format(" {0} : {1} : [FAIL]", CurrentTest, actualValue));
+ return false;
+ }
+ }
+
+ static TestResult Run (Func<TestResult> test)
+ {
+
+ TestResult result;
+
+ try
+ {
+ result = test();
+ }
+ catch (ArgumentNullException e)
+ {
+ return TestResult.ArgumentNull;
+ }
+ catch (ArgumentException e)
+ {
+ return TestResult.ArgumentBad;
+ }
+ catch (DllNotFoundException e)
+ {
+ return TestResult.DllNotFound;
+ }
+ catch (BadImageFormatException e)
+ {
+ return TestResult.BadImage;
+ }
+ catch (InvalidOperationException e)
+ {
+ return TestResult.InvalidOperation;
+ }
+ catch (EntryPointNotFoundException e)
+ {
+ return TestResult.EntryPointNotFound;
+ }
+ catch (Exception e)
+ {
+ //Console.WriteLine(e.ToString());
+ return TestResult.GenericException;
+ }
+
+ return result;
+ }
+
+ static TestResult LoadLibrarySimple(string libPath)
+ {
+ CurrentTest = String.Format("LoadLibrary({0})", libPath);
+
+ IntPtr handle = IntPtr.Zero;
+
+ TestResult result = Run(() => {
+ handle = NativeLibrary.Load(libPath);
+ if (handle == IntPtr.Zero)
+ return TestResult.ReturnNull;
+ return TestResult.Success;
+ });
+
+ NativeLibrary.Free(handle);
+
+ return result;
+ }
+
+ static TestResult TryLoadLibrarySimple(string libPath)
+ {
+ CurrentTest = String.Format("TryLoadLibrary({0})", libPath);
+
+ IntPtr handle = IntPtr.Zero;
+
+ TestResult result = Run(() => {
+ bool success = NativeLibrary.TryLoad(libPath, out handle);
+ if(!success)
+ return TestResult.ReturnFailure;
+ if (handle == null)
+ return TestResult.ReturnNull;
+ return TestResult.Success;
+ });
+
+ NativeLibrary.Free(handle);
+
+ return result;
+ }
+
+
+ static TestResult LoadLibraryAdvanced(string libName, Assembly assembly, DllImportSearchPath? searchPath)
+ {
+ CurrentTest = String.Format("LoadLibrary({0}, {1}, {2})", libName, assembly, searchPath);
+
+ IntPtr handle = IntPtr.Zero;
+
+ TestResult result = Run(() => {
+ handle = NativeLibrary.Load(libName, assembly, searchPath);
+ if (handle == IntPtr.Zero)
+ return TestResult.ReturnNull;
+ return TestResult.Success;
+ });
+
+ NativeLibrary.Free(handle);
+
+ return result;
+ }
+
+ static TestResult TryLoadLibraryAdvanced(string libName, Assembly assembly, DllImportSearchPath? searchPath)
+ {
+ CurrentTest = String.Format("TryLoadLibrary({0}, {1}, {2})", libName, assembly, searchPath);
+
+ IntPtr handle = IntPtr.Zero;
+
+ TestResult result = Run(() => {
+ bool success = NativeLibrary.TryLoad(libName, assembly, searchPath, out handle);
+ if (!success)
+ return TestResult.ReturnFailure;
+ if (handle == IntPtr.Zero)
+ return TestResult.ReturnNull;
+ return TestResult.Success;
+ });
+
+ NativeLibrary.Free(handle);
+
+ return result;
+ }
+
+ static TestResult FreeLibrary(IntPtr handle)
+ {
+ CurrentTest = String.Format("FreeLibrary({0})", handle);
+
+ return Run(() => {
+ NativeLibrary.Free(handle);
+ return TestResult.Success;
+ });
+ }
+
+ static TestResult GetLibraryExport(IntPtr handle, string name)
+ {
+ CurrentTest = String.Format("GetLibraryExport({0}, {1})", handle, name);
+
+ return Run(() => {
+ IntPtr address = NativeLibrary.GetExport(handle, name);
+ if (address == null)
+ return TestResult.ReturnNull;
+ if (RunExportedFunction(address, 1, 1) != 2)
+ return TestResult.IncorrectEvaluation;
+ return TestResult.Success;
+ });
+ }
+
+ static TestResult TryGetLibraryExport(IntPtr handle, string name)
+ {
+ CurrentTest = String.Format("TryGetLibraryExport({0}, {1})", handle, name);
+
+ return Run(() => {
+ IntPtr address = IntPtr.Zero;
+ bool success = NativeLibrary.TryGetExport(handle, name, out address);
+ if (!success)
+ return TestResult.ReturnFailure;
+ if (address == null)
+ return TestResult.ReturnNull;
+ if (RunExportedFunction(address, 1, 1) != 2)
+ return TestResult.IncorrectEvaluation;
+ return TestResult.Success;
+ });
+ }
+
+ [DllImport("NativeLibrary")]
+ static extern int RunExportedFunction(IntPtr address, int arg1, int arg2);
+}
diff --git a/tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibraryTests.csproj b/tests/src/Interop/NativeLibrary/NativeLibraryTests.csproj
index d3d7eb2be7..9f01b9f70c 100644
--- a/tests/src/Interop/MarshalAPI/NativeLibrary/NativeLibraryTests.csproj
+++ b/tests/src/Interop/NativeLibrary/NativeLibraryTests.csproj
@@ -35,7 +35,7 @@
</None>
</ItemGroup>
<ItemGroup>
- <ProjectReference Include="..\..\..\Common\CoreCLRTestLibrary\CoreCLRTestLibrary.csproj">
+ <ProjectReference Include="..\..\Common\CoreCLRTestLibrary\CoreCLRTestLibrary.csproj">
<Project>{c8c0dc74-fac4-45b1-81fe-70c4808366e0}</Project>
<Name>CoreCLRTestLibrary</Name>
</ProjectReference>