-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84639 from vseanreesermsft/internal-merge-6.0-202…
…3-04-11-1027 Merging internal commits for release/6.0
- Loading branch information
Showing
9 changed files
with
191 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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"); | ||
|
||
static int Main(string[] args) | ||
{ | ||
try | ||
{ | ||
AssemblyDirectory_NotFound(); | ||
if (!TestLibrary.Utilities.IsMonoLLVMFULLAOT) | ||
AssemblyDirectory_Found(); | ||
|
||
if (OperatingSystem.IsWindows()) | ||
AssemblyDirectory_Fallback_Found(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"Test Failure: {e}"); | ||
return 101; | ||
} | ||
|
||
return 100; | ||
} | ||
|
||
public static void AssemblyDirectory_NotFound() | ||
{ | ||
// Library should not be found in the assembly directory | ||
Assert.Throws<DllNotFoundException>(() => NativeLibraryPInvoke.Sum(1, 2)); | ||
} | ||
|
||
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); | ||
} | ||
|
||
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); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="*.cs" /> | ||
<Compile Include="../NativeLibrary/NativeLibraryToLoad/NativeLibraryToLoad.cs" /> | ||
<ProjectReference Include="../NativeLibrary/NativeLibraryToLoad/CMakeLists.txt" /> | ||
</ItemGroup> | ||
|
||
<Target Name="SetUpSubdirectory" AfterTargets="CopyNativeProjectBinaries"> | ||
<PropertyGroup> | ||
<NativeLibrarySubdirectory>$(OutDir)/subdirectory</NativeLibrarySubdirectory> | ||
<FileNameSuffix>-in-subdirectory</FileNameSuffix> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<_FilesToCopy Include="$(OutDir)/$(TargetName).dll" /> | ||
<_FilesToMove Include="$(OutDir)/libNativeLibrary.*" /> | ||
<_FilesToMove Include="$(OutDir)/NativeLibrary.*" /> | ||
</ItemGroup> | ||
<Copy SourceFiles="@(_FilesToCopy)" DestinationFiles="@(_FilesToCopy -> '$(NativeLibrarySubdirectory)/%(Filename)%(Extension)')" /> | ||
<Move SourceFiles="@(_FilesToMove)" DestinationFiles="@(_FilesToMove -> '$(NativeLibrarySubdirectory)/%(Filename)%(Extension)')" /> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters