diff --git a/src/TestUtils/src/Microsoft.Maui.IntegrationTests/Apple/Codesign.cs b/src/TestUtils/src/Microsoft.Maui.IntegrationTests/Apple/Codesign.cs new file mode 100644 index 000000000000..611f6c3737a9 --- /dev/null +++ b/src/TestUtils/src/Microsoft.Maui.IntegrationTests/Apple/Codesign.cs @@ -0,0 +1,33 @@ +using System.Diagnostics; + +namespace Microsoft.Maui.IntegrationTests.Apple +{ + public static class Codesign + { + public static List SearchForExpectedEntitlements( + string entitlementsPath, + string appLocation, + List expectedEntitlements) + { + List foundEntitlements = new(); + string procOutput = ToolRunner.Run(new ProcessStartInfo() + { + FileName = "/usr/bin/codesign", + Arguments = $"-d --entitlements {entitlementsPath} --xml {appLocation}" + }, out int errorCode); + + Assert.AreEqual(errorCode, 0, procOutput); + Assert.IsTrue(File.Exists(entitlementsPath)); + + string fileContent = File.ReadAllText(entitlementsPath); + foreach (string entitlement in expectedEntitlements) + { + if (fileContent.Contains(entitlement, StringComparison.OrdinalIgnoreCase)) + foundEntitlements.Add(entitlement); + } + + return foundEntitlements; + } + } +} + diff --git a/src/TestUtils/src/Microsoft.Maui.IntegrationTests/TemplateTests.cs b/src/TestUtils/src/Microsoft.Maui.IntegrationTests/TemplateTests.cs index 2af95af79cd4..bba4e778eac8 100644 --- a/src/TestUtils/src/Microsoft.Maui.IntegrationTests/TemplateTests.cs +++ b/src/TestUtils/src/Microsoft.Maui.IntegrationTests/TemplateTests.cs @@ -1,3 +1,4 @@ +using Microsoft.Maui.IntegrationTests.Apple; namespace Microsoft.Maui.IntegrationTests { @@ -182,6 +183,39 @@ public void BuildWithDifferentVersionNumber(string id, string config, string dis $"Project {Path.GetFileName(projectFile)} failed to build. Check test output/attachments for errors."); } + [Test] + [TestCase("maui-blazor", "Debug", DotNetCurrent)] + [TestCase("maui-blazor", "Release", DotNetCurrent)] + public void CheckEntitlementsForMauiBlazorOnMacCatalyst(string id, string config, string framework) + { + if(TestEnvironment.IsWindows) + Assert.Ignore("Running MacCatalyst templates is only supported on Mac."); + + string projectDir = TestDirectory; + string projectFile = Path.Combine(projectDir, $"{Path.GetFileName(projectDir)}.csproj"); + // Note: Debug app is stored in the maccatalyst-x64 folder, while the Release is in parent directory + string appLocation = config == "Release" ? + Path.Combine(projectDir, "bin", config, $"{framework}-maccatalyst", $"{Path.GetFileName(projectDir)}.app") : + Path.Combine(projectDir, "bin", config, $"{framework}-maccatalyst", "maccatalyst-x64", $"{Path.GetFileName(projectDir)}.app"); + string entitlementsPath = Path.Combine(projectDir, "x.xml"); + + List buildWithCodeSignProps = new List(BuildProps) + { + "EnableCodeSigning=true" + }; + + Assert.IsTrue(DotnetInternal.New(id, projectDir, framework), $"Unable to create template {id}. Check test output for errors."); + Assert.IsTrue(DotnetInternal.Build(projectFile, config, framework: $"{framework}-maccatalyst", properties: buildWithCodeSignProps), + $"Project {Path.GetFileName(projectFile)} failed to build. Check test output/attachments for errors."); + + List expectedEntitlements = config == "Release" ? + new() { "com.apple.security.app-sandbox", "com.apple.security.network.client" } : + new() { "com.apple.security.get-task-allow" }; + List foundEntitlements = Codesign.SearchForExpectedEntitlements(entitlementsPath, appLocation, expectedEntitlements); + + CollectionAssert.AreEqual(expectedEntitlements, foundEntitlements, "Entitlements missing from executable."); + } + void EnableTizen(string projectFile) { FileUtilities.ReplaceInFile(projectFile, new Dictionary()