Skip to content

Commit 7c2fb0e

Browse files
committed
Add test for default behavior when _EnableMacOSCodesign is unset
1 parent 8ff3639 commit 7c2fb0e

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

test/Microsoft.NET.Build.Tests/AppHostTests.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,27 @@ public void It_does_not_try_to_codesign_non_osx_app_hosts(string targetFramework
127127
[InlineData("net8.0", "osx-arm64", false)]
128128
[InlineData(ToolsetInfo.CurrentTargetFramework, "osx-x64", false)]
129129
[InlineData(ToolsetInfo.CurrentTargetFramework, "osx-arm64", false)]
130-
public void It_codesigns_an_app_targeting_osx(string targetFramework, string rid, bool shouldSign)
130+
[InlineData("net8.0", "osx-x64", null)]
131+
[InlineData("net8.0", "osx-arm64", null)]
132+
[InlineData(ToolsetInfo.CurrentTargetFramework, "osx-x64", null)]
133+
[InlineData(ToolsetInfo.CurrentTargetFramework, "osx-arm64", null)]
134+
public void It_codesigns_an_app_targeting_osx(string targetFramework, string rid, bool? enableMacOSCodesign)
131135
{
136+
const bool CodesignsByDefault = true;
132137
const string testAssetName = "HelloWorld";
133138
var testAsset = _testAssetsManager
134139
.CopyTestAsset(testAssetName, identifier: targetFramework)
135140
.WithSource()
136141
.WithTargetFramework(targetFramework);
137142

138143
var buildCommand = new BuildCommand(testAsset);
139-
var buildArgs = new List<string>() { $"/p:RuntimeIdentifier={rid}", $"/p:_EnableMacOSCodeSign={shouldSign}" };
144+
145+
var buildArgs = new List<string>() { $"/p:RuntimeIdentifier={rid}" };
146+
if (enableMacOSCodesign.HasValue)
147+
{
148+
buildArgs.Add($"/p:_EnableMacOSCodeSign={enableMacOSCodesign.Value}");
149+
}
150+
140151
buildCommand
141152
.Execute(buildArgs.ToArray())
142153
.Should()
@@ -145,13 +156,14 @@ public void It_codesigns_an_app_targeting_osx(string targetFramework, string rid
145156
var outputDirectory = buildCommand.GetOutputDirectory(targetFramework: targetFramework, runtimeIdentifier: rid);
146157
var appHostFullPath = Path.Combine(outputDirectory.FullName, testAssetName);
147158

148-
// Check that the apphost is signed
149-
MachOSignature.HasMachOSignatureLoadCommand(new FileInfo(appHostFullPath)).Should().Be(shouldSign, $"The app host should {(shouldSign ? "" : "not ")}have a Mach-O signature load command.");
159+
// Check that the apphost is signed if expected
160+
var shouldBeSigned = enableMacOSCodesign ?? CodesignsByDefault;
161+
MachOSignature.HasMachOSignatureLoadCommand(new FileInfo(appHostFullPath)).Should().Be(shouldBeSigned, $"The app host should {(shouldBeSigned ? "" : "not ")}have a Mach-O signature load command.");
150162
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
151163
{
152164
MachOSignature.HasValidMachOSignature(new FileInfo(appHostFullPath), Log)
153165
.Should()
154-
.Be(shouldSign, $"The app host should have a valid Mach-O signature for {rid}.");
166+
.Be(shouldBeSigned, $"The app host should have a valid Mach-O signature for {rid}.");
155167
}
156168
}
157169

test/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishASingleFileApp.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private string GetNativeDll(string baseName)
8585
private DirectoryInfo GetPublishDirectory(PublishCommand publishCommand, string targetFramework = ToolsetInfo.CurrentTargetFramework, string runtimeIdentifier = null)
8686
{
8787
return publishCommand.GetOutputDirectory(targetFramework: targetFramework,
88-
runtimeIdentifier: runtimeIdentifier ?? RuntimeInformation.RuntimeIdentifier);
88+
runtimeIdentifier: runtimeIdentifier ?? RuntimeInformation.RuntimeIdentifier);
8989
}
9090

9191
[Fact]
@@ -416,7 +416,7 @@ public void It_generates_a_single_file_with_all_content_for_self_contained_apps(
416416
}
417417

418418
// https://github.com/dotnet/sdk/issues/49665
419-
// error NETSDK1084: There is no application host available for the specified RuntimeIdentifier 'osx-arm64'.
419+
// error NETSDK1084: There is no application host available for the specified RuntimeIdentifier 'osx-arm64'.
420420
[PlatformSpecificTheory(TestPlatforms.Any & ~TestPlatforms.OSX)]
421421
[InlineData("netcoreapp3.0")]
422422
[InlineData("netcoreapp3.1")]
@@ -756,17 +756,20 @@ public void EnableSingleFile_warns_when_expected_for_not_correctly_multitargeted
756756
testProject.AdditionalProperties["CheckEolTargetFramework"] = "false"; // Silence warning about targeting EOL TFMs
757757
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFrameworks)
758758
.WithProjectChanges(AddTargetFrameworkAliases);
759-
759+
760760
var buildCommand = new BuildCommand(testAsset);
761761
var resultAssertion = buildCommand.Execute("/p:CheckEolTargetFramework=false")
762762
.Should().Pass();
763-
if (shouldWarn) {
763+
if (shouldWarn)
764+
{
764765
// Note: can't check for Strings.EnableSingleFileAnalyzerUnsupported because each line of
765766
// the message gets prefixed with a file path by MSBuild.
766767
resultAssertion
767768
.And.HaveStdOutContaining($"warning NETSDK1211")
768769
.And.HaveStdOutContaining($"<EnableSingleFileAnalyzer Condition=\"$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))\">true</EnableSingleFileAnalyzer>");
769-
} else {
770+
}
771+
else
772+
{
770773
resultAssertion.And.NotHaveStdOutContaining($"warning");
771774
}
772775
}
@@ -1144,38 +1147,52 @@ static void VerifyPrepareForBundle(XDocument project)
11441147
[InlineData("osx-arm64", true)]
11451148
[InlineData("osx-x64", false)]
11461149
[InlineData("osx-arm64", false)]
1147-
public void It_codesigns_an_app_targeting_osx(string rid, bool enableMacOSCodeSign)
1150+
[InlineData("osx-x64", null)]
1151+
[InlineData("osx-arm64", null)]
1152+
public void It_codesigns_an_app_targeting_osx(string rid, bool? enableMacOSCodeSign)
11481153
{
1154+
const bool CodesignsByDefault = true;
11491155
var targetFramework = ToolsetInfo.CurrentTargetFramework;
11501156
var testProject = new TestProject()
11511157
{
11521158
Name = "SingleFileTest",
11531159
TargetFrameworks = targetFramework,
11541160
IsExe = true,
11551161
};
1156-
testProject.AdditionalProperties.Add("SelfContained", $"true");
1162+
testProject.AdditionalProperties.Add("SelfContained", "true");
11571163

11581164
var testAsset = _testAssetsManager.CreateTestProject(
11591165
testProject,
11601166
identifier: $"{rid}_{enableMacOSCodeSign}");
11611167
var publishCommand = new PublishCommand(testAsset);
11621168

1163-
publishCommand.Execute(PublishSingleFile, $"/p:RuntimeIdentifier={rid}", $"/p:_EnableMacOSCodeSign={enableMacOSCodeSign}")
1169+
List<string> publishArgs = new List<string>(3)
1170+
{
1171+
PublishSingleFile,
1172+
$"/p:RuntimeIdentifier={rid}"
1173+
};
1174+
if (enableMacOSCodeSign.HasValue)
1175+
{
1176+
publishArgs.Add($"/p:_EnableMacOSCodeSign={enableMacOSCodeSign.Value}");
1177+
}
1178+
1179+
publishCommand.Execute(publishArgs)
11641180
.Should()
11651181
.Pass();
11661182

11671183
var publishDir = GetPublishDirectory(publishCommand, targetFramework, runtimeIdentifier: rid).FullName;
11681184
var singleFilePath = Path.Combine(publishDir, $"{testProject.Name}{Constants.ExeSuffix}");
11691185

1186+
bool shouldBeSigned = enableMacOSCodeSign ?? CodesignsByDefault;
1187+
11701188
MachOSignature.HasMachOSignatureLoadCommand(new FileInfo(singleFilePath))
11711189
.Should()
1172-
.Be(enableMacOSCodeSign, $"The app host should {(enableMacOSCodeSign ? "" : "not ")}have a Mach-O signature load command.");
1173-
1190+
.Be(shouldBeSigned, $"The app host should {(shouldBeSigned ? "" : "not ")}have a Mach-O signature load command.");
11741191
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
11751192
{
11761193
MachOSignature.HasValidMachOSignature(new FileInfo(singleFilePath), Log)
11771194
.Should()
1178-
.Be(enableMacOSCodeSign, $"The app host should {(enableMacOSCodeSign ? "" : "not ")}have a valid Mach-O signature for {rid}.");
1195+
.Be(shouldBeSigned, $"The app host should {(shouldBeSigned ? "" : "not ")}have a valid Mach-O signature for {rid}.");
11791196
}
11801197
}
11811198
}

test/Microsoft.NET.TestFramework/Utilities/MachOSignature.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ namespace Microsoft.NET.TestFramework.Utilities
99
{
1010
public static class MachOSignature
1111
{
12+
/// <summary>
13+
/// Calls the 'codesign' utility to verify if the file has a valid Mach-O signature.
14+
/// </summary>
15+
/// <param name="file">The Mach Object file to check.</param>
16+
/// <param name="log">The output helper for logging.</param>
17+
/// <returns>True if the file has a valid Mach-O signature, otherwise false.</returns>
1218
#if NET
1319
[SupportedOSPlatform("osx")]
1420
#endif
@@ -19,7 +25,11 @@ public static bool HasValidMachOSignature(FileInfo file, ITestOutputHelper log)
1925
.Execute().ExitCode == 0;
2026
}
2127

22-
// Reads the Mach-O load commands and returns true if an LC_CODE_SIGNATURE command is found, otherwise returns false
28+
/// <summary>
29+
/// Reads the Mach-O load commands and returns true if an LC_CODE_SIGNATURE command is found, otherwise returns false. Does not validate the signature.
30+
/// </summary>
31+
/// <param name="file">The Mach Object file to check.</param>
32+
/// <returns>True if the file has a Mach-O signature load command, otherwise false.</returns>
2333
public static bool HasMachOSignatureLoadCommand(FileInfo file)
2434
{
2535
/* Mach-O files have the following structure:

0 commit comments

Comments
 (0)