forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDllImportSearchPathsTest.cs
More file actions
66 lines (56 loc) · 2.16 KB
/
DllImportSearchPathsTest.cs
File metadata and controls
66 lines (56 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Xunit;
public class DllImportSearchPathsTest
{
private static string Subdirectory => Path.Combine(NativeLibraryToLoad.GetDirectory(), "subdirectory");
[Fact]
public static void AssemblyDirectory_NotFound()
{
// Library should not be found in the assembly directory
Assert.Throws<DllNotFoundException>(() => NativeLibraryPInvoke.Sum(1, 2));
}
public static bool CanLoadAssemblyInSubdirectory =>
!TestLibrary.Utilities.IsNativeAot && !TestLibrary.PlatformDetection.IsMonoLLVMFULLAOT;
[ConditionalFact(nameof(CanLoadAssemblyInSubdirectory))]
public static void AssemblyDirectory_Found()
{
// Library should be found in the assembly directory
var assembly = Assembly.LoadFile(Path.Combine(Subdirectory, $"{nameof(DllImportSearchPathsTest)}.dll"));
var type = assembly.GetType(nameof(NativeLibraryPInvoke));
var method = type.GetMethod(nameof(NativeLibraryPInvoke.Sum));
int sum = (int)method.Invoke(null, new object[] { 1, 2 });
Assert.Equal(3, sum);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public static void AssemblyDirectory_Fallback_Found()
{
string currentDirectory = Environment.CurrentDirectory;
try
{
Environment.CurrentDirectory = Subdirectory;
// Library should not be found in the assembly directory, but should fall back to the default OS search which includes CWD on Windows
int sum = NativeLibraryPInvoke.Sum(1, 2);
Assert.Equal(3, sum);
}
finally
{
Environment.CurrentDirectory = currentDirectory;
}
}
}
public class NativeLibraryPInvoke
{
public static int Sum(int a, int b)
{
return NativeSum(a, b);
}
[DllImport(NativeLibraryToLoad.Name)]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
static extern int NativeSum(int arg1, int arg2);
}