-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18773f2
commit 7ae92ce
Showing
38 changed files
with
1,537 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// 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.Runtime.InteropServices; | ||
using System.Runtime.Versioning; | ||
using System.Xml; | ||
using Microsoft.Build.Evaluation; | ||
using Microsoft.Build.UnitTests; | ||
using Microsoft.Build.Utilities; | ||
using Shouldly; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Build.Tasks.UnitTests | ||
{ | ||
public class AddToWin32Manifest_Tests | ||
{ | ||
private static string TestAssetsRootPath { get; } = Path.Combine( | ||
Path.GetDirectoryName(typeof(AddToWin32Manifest_Tests).Assembly.Location) ?? AppContext.BaseDirectory, | ||
"TestResources", | ||
"Manifests"); | ||
|
||
private readonly ITestOutputHelper _testOutput; | ||
|
||
public AddToWin32Manifest_Tests(ITestOutputHelper testOutput) => _testOutput = testOutput; | ||
|
||
[Theory] | ||
[InlineData("testManifestWithInvalidSupportedArchs.manifest", false)] | ||
[InlineData("testManifestWithApplicationDefined.manifest", true)] | ||
[InlineData("testManifestSavesTheCurrentNodesPositions.manifest", true)] | ||
[InlineData("testManifestNoPrefixes.manifest", true)] | ||
[InlineData(null, true)] | ||
public void ManifestPopulationCheck(string manifestName, bool expectedResult) | ||
{ | ||
AddToWin32Manifest task = new AddToWin32Manifest() | ||
{ | ||
BuildEngine = new MockEngine(_testOutput) | ||
}; | ||
|
||
using (TestEnvironment env = TestEnvironment.Create()) | ||
{ | ||
var tempOutput = env.CreateFolder().Path; | ||
task.OutputDirectory = tempOutput; | ||
task.SupportedArchitectures = "amd64 arm64"; | ||
if (!string.IsNullOrEmpty(manifestName)) | ||
{ | ||
task.ApplicationManifest = new TaskItem(Path.Combine(TestAssetsRootPath, manifestName)); | ||
} | ||
|
||
var result = task.Execute(); | ||
|
||
result.ShouldBe(expectedResult); | ||
|
||
if (result) | ||
{ | ||
string generatedManifest = task.ManifestPath; | ||
string expectedManifest = Path.Combine(TestAssetsRootPath, $"{manifestName ?? "default.win32manifest"}_expected"); | ||
|
||
XmlDocument expectedDoc = new XmlDocument(); | ||
XmlDocument actualDoc = new XmlDocument(); | ||
|
||
expectedDoc.Load(expectedManifest); | ||
actualDoc.Load(generatedManifest); | ||
|
||
expectedDoc.OuterXml.ShouldBe(actualDoc.OuterXml); | ||
expectedDoc.InnerXml.ShouldBe(actualDoc.InnerXml); | ||
} | ||
} | ||
} | ||
|
||
[SupportedOSPlatform("windows")] | ||
[WindowsOnlyTheory] | ||
[InlineData(null, true)] | ||
[InlineData("buildIn.manifest", true)] | ||
[InlineData("testManifestWithValidSupportedArchs.manifest", true)] | ||
public void E2EScenarioTests(string manifestName, bool expectedResult) | ||
{ | ||
using (TestEnvironment env = TestEnvironment.Create()) | ||
{ | ||
var outputPath = env.CreateFolder().Path; | ||
string projectContent = @$" | ||
<Project DefaultTargets=""Build""> | ||
<Import Project=""$(MSBuildBinPath)\Microsoft.Common.props"" /> | ||
<PropertyGroup> | ||
<Platform>AnyCPU</Platform> | ||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> | ||
<OutputType>Library</OutputType> | ||
<PreferNativeArm64>true</PreferNativeArm64> | ||
<Prefer32Bit>false</Prefer32Bit> | ||
{(!string.IsNullOrEmpty(manifestName) ? $"<ApplicationManifest>{manifestName}</ApplicationManifest>" : "")} | ||
<IntermediateOutputPath>{outputPath}</IntermediateOutputPath> | ||
</PropertyGroup> | ||
<Target Name=""Build""/> | ||
<Import Project=""$(MSBuildBinPath)\Microsoft.CSharp.targets"" /> | ||
</Project> | ||
"; | ||
|
||
var projectFolder = env.CreateFolder(); | ||
var projectFile = env.CreateFile(projectFolder, "test.csproj", projectContent).Path; | ||
|
||
// copy application manifest | ||
if (!string.IsNullOrEmpty(manifestName)) | ||
{ | ||
File.Copy(Path.Combine(TestAssetsRootPath, manifestName), Path.Combine(projectFolder.Path, manifestName)); | ||
} | ||
|
||
Project project = ObjectModelHelpers.LoadProjectFileInTempProjectDirectory(projectFile, touchProject: false); | ||
|
||
bool result = project.Build(new MockLogger(_testOutput)); | ||
result.ShouldBe(expectedResult); | ||
|
||
// #2 - represents the name for native resource (Win 32 resource), #24 - the type (Manifest) | ||
byte[]? actualManifestBytes = AssemblyNativeResourceManager.GetResourceFromExecutable(Path.Combine(outputPath, "test.dll"), "#2", "#24"); | ||
|
||
// check manifest content | ||
if (actualManifestBytes != null) | ||
{ | ||
string expectedManifest = Path.Combine(TestAssetsRootPath, $"{manifestName ?? "default.win32manifest"}_expected"); | ||
|
||
XmlDocument expectedDoc = new XmlDocument(); | ||
XmlDocument actualDoc = new XmlDocument(); | ||
|
||
expectedDoc.Load(expectedManifest); | ||
using (MemoryStream stream = new MemoryStream(actualManifestBytes)) | ||
{ | ||
actualDoc.Load(stream); | ||
} | ||
|
||
NormalizeLineEndings(expectedDoc.OuterXml).ShouldBe(NormalizeLineEndings(actualDoc.OuterXml)); | ||
NormalizeLineEndings(expectedDoc.InnerText).ShouldBe(NormalizeLineEndings(actualDoc.InnerText)); | ||
} | ||
} | ||
|
||
static string NormalizeLineEndings(string input) => input.Replace("\r\n", "\n").Replace("\r", "\n"); | ||
} | ||
|
||
[SupportedOSPlatform("windows")] | ||
internal sealed class AssemblyNativeResourceManager | ||
{ | ||
public enum LoadLibraryFlags : uint { LOAD_LIBRARY_AS_DATAFILE = 2 }; | ||
|
||
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] | ||
public static extern IntPtr LoadLibrary(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern IntPtr FindResource(IntPtr hModule, string lpName, string lpType); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern IntPtr LockResource(IntPtr hResData); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo); | ||
|
||
public static byte[]? GetResourceFromExecutable(string assembly, string lpName, string lpType) | ||
{ | ||
IntPtr hModule = LoadLibrary(assembly, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE); | ||
try | ||
{ | ||
if (hModule != IntPtr.Zero) | ||
{ | ||
IntPtr hResource = FindResource(hModule, lpName, lpType); | ||
if (hResource != IntPtr.Zero) | ||
{ | ||
uint resSize = SizeofResource(hModule, hResource); | ||
IntPtr resData = LoadResource(hModule, hResource); | ||
if (resData != IntPtr.Zero) | ||
{ | ||
byte[] uiBytes = new byte[resSize]; | ||
IntPtr ipMemorySource = LockResource(resData); | ||
Marshal.Copy(ipMemorySource, uiBytes, 0, (int)resSize); | ||
|
||
return uiBytes; | ||
} | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
NativeMethodsShared.FreeLibrary(hModule); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
} |
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,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Build.Evaluation; | ||
using Microsoft.Build.Shared; | ||
using Microsoft.Build.UnitTests; | ||
using Shouldly; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Build.Tasks.UnitTests | ||
{ | ||
public class MSBuildInternalMessage_Tests | ||
{ | ||
private readonly ITestOutputHelper _testOutput; | ||
|
||
public MSBuildInternalMessage_Tests(ITestOutputHelper testOutput) => _testOutput = testOutput; | ||
|
||
[Theory] | ||
[InlineData(true, true, "CommonTarget.Prefer32BitAndPreferNativeArm64Enabled", false)] | ||
[InlineData(false, false, "CommonTarget.PlatformIsAnyCPUAndPreferNativeArm64Enabled", true, new[] { "Release" })] | ||
public void E2EScenarioTests(bool prefer32, bool isPlatformAnyCpu, string expectedResourceName, bool isNetWarningExpected, string[]? formatArgs = null) | ||
{ | ||
using (TestEnvironment env = TestEnvironment.Create()) | ||
{ | ||
var outputPath = env.CreateFolder().Path; | ||
string projectContent = @$" | ||
<Project DefaultTargets=""Build""> | ||
<Import Project=""$(MSBuildBinPath)\Microsoft.Common.props"" /> | ||
<PropertyGroup> | ||
<Platform>{(isPlatformAnyCpu ? "AnyCPU" : "Release")}</Platform> | ||
<OutputType>Library</OutputType> | ||
<PreferNativeArm64>true</PreferNativeArm64> | ||
<Prefer32Bit>{(prefer32 ? "true" : "false")}</Prefer32Bit> | ||
</PropertyGroup> | ||
<Target Name=""Build""/> | ||
<Import Project=""$(MSBuildBinPath)\Microsoft.CSharp.targets"" /> | ||
</Project> | ||
"; | ||
|
||
var projectFile = env.CreateFile(env.CreateFolder(), "test.csproj", projectContent).Path; | ||
Project project = ObjectModelHelpers.LoadProjectFileInTempProjectDirectory(projectFile, touchProject: false); | ||
|
||
string expectedBuildMessage = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(expectedResourceName, formatArgs); | ||
MockLogger logger = new MockLogger(_testOutput); | ||
|
||
project.Build(logger); | ||
|
||
if (isNetWarningExpected) | ||
{ | ||
logger.Warnings[0].RawMessage.ShouldBe(expectedBuildMessage); | ||
} | ||
else | ||
{ | ||
logger.Errors[0].RawMessage.ShouldBe(expectedBuildMessage); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
src/Tasks.UnitTests/TestResources/Manifests/buildIn.manifest
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,69 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<assemblyIdentity version="1.0.0.0" name="MyApplication.app" /> | ||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> | ||
<security> | ||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<!-- UAC Manifest Options | ||
If you want to change the Windows User Account Control level replace the | ||
requestedExecutionLevel node with one of the following. | ||
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> | ||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> | ||
<requestedExecutionLevel level="highestAvailable" uiAccess="false" /> | ||
Specifying requestedExecutionLevel element will disable file and registry virtualization. | ||
Remove this element if your application requires this virtualization for backwards | ||
compatibility. | ||
--> | ||
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> | ||
</requestedPrivileges> | ||
</security> | ||
</trustInfo> | ||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> | ||
<application> | ||
<!-- A list of the Windows versions that this application has been tested on | ||
and is designed to work with. Uncomment the appropriate elements | ||
and Windows will automatically select the most compatible environment. --> | ||
<!-- Windows Vista --> | ||
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />--> | ||
<!-- Windows 7 --> | ||
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />--> | ||
<!-- Windows 8 --> | ||
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />--> | ||
<!-- Windows 8.1 --> | ||
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />--> | ||
<!-- Windows 10 --> | ||
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />--> | ||
</application> | ||
</compatibility> | ||
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher | ||
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need | ||
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should | ||
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. | ||
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation --> | ||
<!-- | ||
<application xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<windowsSettings> | ||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware> | ||
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware> | ||
</windowsSettings> | ||
</application> | ||
--> | ||
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) --> | ||
<!-- | ||
<dependency> | ||
<dependentAssembly> | ||
<assemblyIdentity | ||
type="win32" | ||
name="Microsoft.Windows.Common-Controls" | ||
version="6.0.0.0" | ||
processorArchitecture="*" | ||
publicKeyToken="6595b64144ccf1df" | ||
language="*" | ||
/> | ||
</dependentAssembly> | ||
</dependency> | ||
--> | ||
</assembly> |
Oops, something went wrong.