|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Reflection; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using Xunit; |
| 9 | +using static TestHelpers; |
| 10 | + |
| 11 | +class GetLibraryExportTests : IDisposable |
| 12 | +{ |
| 13 | + private readonly IntPtr handle; |
| 14 | + |
| 15 | + public GetLibraryExportTests() |
| 16 | + { |
| 17 | + handle = NativeLibrary.Load(NativeLibraryToLoad.GetFullPath()); |
| 18 | + } |
| 19 | + |
| 20 | + [ConditionalFact(typeof(TestLibrary.Utilities), nameof(TestLibrary.Utilities.IsX86))] |
| 21 | + public void GetValidExport_ManualMangling() |
| 22 | + { |
| 23 | + EXPECT(GetLibraryExport(handle, "_NativeSum@8")); |
| 24 | + EXPECT(TryGetLibraryExport(handle, "_NativeSum@8")); |
| 25 | + } |
| 26 | + |
| 27 | + [ConditionalFact(typeof(TestLibrary.Utilities), nameof(TestLibrary.Utilities.IsNotX86))] |
| 28 | + public void GetValidExport() |
| 29 | + { |
| 30 | + EXPECT(GetLibraryExport(handle, "NativeSum")); |
| 31 | + EXPECT(TryGetLibraryExport(handle, "NativeSum")); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void NullHandle() |
| 36 | + { |
| 37 | + EXPECT(GetLibraryExport(IntPtr.Zero, "NativeSum"), TestResult.ArgumentNull); |
| 38 | + EXPECT(TryGetLibraryExport(IntPtr.Zero, "NativeSum"), TestResult.ArgumentNull); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void NullExport() |
| 43 | + { |
| 44 | + EXPECT(GetLibraryExport(handle, null), TestResult.ArgumentNull); |
| 45 | + EXPECT(TryGetLibraryExport(handle, null), TestResult.ArgumentNull); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void ExportDoesNotExist() |
| 50 | + { |
| 51 | + EXPECT(GetLibraryExport(handle, "NonNativeSum"), TestResult.EntryPointNotFound); |
| 52 | + EXPECT(TryGetLibraryExport(handle, "NonNativeSum"), TestResult.ReturnFailure); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + public void Dispose() => NativeLibrary.Free(handle); |
| 57 | + |
| 58 | + static TestResult GetLibraryExport(IntPtr handle, string name) |
| 59 | + { |
| 60 | + return Run(() => { |
| 61 | + IntPtr address = NativeLibrary.GetExport(handle, name); |
| 62 | + if (address == IntPtr.Zero) |
| 63 | + return TestResult.ReturnNull; |
| 64 | + if (RunExportedFunction(address, 1, 1) != 2) |
| 65 | + return TestResult.IncorrectEvaluation; |
| 66 | + return TestResult.Success; |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + static TestResult TryGetLibraryExport(IntPtr handle, string name) |
| 71 | + { |
| 72 | + return Run(() => { |
| 73 | + IntPtr address = IntPtr.Zero; |
| 74 | + bool success = NativeLibrary.TryGetExport(handle, name, out address); |
| 75 | + if (!success) |
| 76 | + return TestResult.ReturnFailure; |
| 77 | + if (address == IntPtr.Zero) |
| 78 | + return TestResult.ReturnNull; |
| 79 | + if (RunExportedFunction(address, 1, 1) != 2) |
| 80 | + return TestResult.IncorrectEvaluation; |
| 81 | + return TestResult.Success; |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + private static unsafe int RunExportedFunction(IntPtr address, int arg1, int arg2) |
| 86 | + { |
| 87 | + // We use a delegate here instead of a function pointer to avoid hitting issues |
| 88 | + // where Mono AOT doesn't generate the managed->native wrapper and then fails |
| 89 | + // when in AOT-only mode. |
| 90 | + NativeFunctionWrapper wrapper = Marshal.GetDelegateForFunctionPointer<NativeFunctionWrapper>(address); |
| 91 | + return wrapper(arg1, arg2); |
| 92 | + } |
| 93 | + |
| 94 | + [UnmanagedFunctionPointer(CallingConvention.Winapi)] |
| 95 | + private delegate int NativeFunctionWrapper(int arg1, int arg2); |
| 96 | +} |
0 commit comments