Skip to content

Commit

Permalink
[msbuild] Process custom entitlements as if they came from an Entitle…
Browse files Browse the repository at this point in the history
…ments.plist file. (#19942)

We need to process custom entitlements just like if they came from an
Entitlements.plist file - which means replacing terms such as
`$(AppIdentifierPrefix)` and `$(TeamIdentifierPrefix)` with their
correct value
depending on the provisioning profile.

Partial fix for #19903.
  • Loading branch information
rolfbjarne authored Jan 30, 2024
1 parent 1279d54 commit e9ae5ef
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ PDictionary MergeEntitlementDictionary (PDictionary dict, MobileProvision? profi
return result;
}

void AddCustomEntitlements (PDictionary dict)
void AddCustomEntitlements (PDictionary dict, MobileProvision? profile)
{
if (CustomEntitlements is null)
return;
Expand Down Expand Up @@ -286,7 +286,7 @@ void AddCustomEntitlements (PDictionary dict)
dict [entitlement] = new PBoolean (booleanValue);
break;
case "string":
dict [entitlement] = new PString (value ?? string.Empty);
dict [entitlement] = MergeEntitlementString (new PString (value), profile, entitlement == ApplicationIdentifierKey);
break;
case "stringarray":
var arraySeparator = item.GetMetadata ("ArraySeparator");
Expand All @@ -295,7 +295,7 @@ void AddCustomEntitlements (PDictionary dict)
var arrayContent = value.Split (new string [] { arraySeparator }, StringSplitOptions.None);
var parray = new PArray ();
foreach (var element in arrayContent)
parray.Add (new PString (element));
parray.Add (MergeEntitlementString (new PString (element), profile, entitlement == ApplicationIdentifierKey));
dict [entitlement] = parray;
break;
default:
Expand Down Expand Up @@ -413,7 +413,7 @@ protected virtual PDictionary GetCompiledEntitlements (MobileProvision? profile,
break;
}

AddCustomEntitlements (entitlements);
AddCustomEntitlements (entitlements, profile);

return entitlements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,19 @@ CustomCompileEntitlements CreateEntitlementsTask (out string compiledEntitlement
compiledEntitlements = task.CompiledEntitlements.ItemSpec;
archivedEntitlements = Path.Combine (AppBundlePath, "archived-expanded-entitlements.xcent");

DeleteDirectory (Path.Combine (MonoTouchProjectPath, "bin"));
DeleteDirectory (Path.Combine (MonoTouchProjectPath, "obj"));

return task;
}

void DeleteDirectory (string directory)
{
if (!Directory.Exists (directory))
return;
Directory.Delete (directory, true);
}

[Test (Description = "Xambug #46298")]
public void ValidateEntitlement ()
{
Expand Down Expand Up @@ -207,5 +217,46 @@ public void AllowJit_None ()
Assert.IsFalse (compiled.ContainsKey (EntitlementKeys.AllowExecutionOfJitCode), "#1");
}

[Test]
public void AppIdentifierPrefix ()
{
var customEntitlements = new TaskItem [] {
new TaskItem ("keychain-access-group", new Dictionary<string, string> { { "Type", "String" }, { "Value", "$(AppIdentifierPrefix)org.xamarin" } }),
};
var task = CreateEntitlementsTask (out var compiledEntitlements, out var archivedEntitlements);
task.TargetFrameworkMoniker = ".NETCoreApp,Version=v6.0,Profile=ios";
task.CustomEntitlements = customEntitlements;
ExecuteTask (task);
var compiled = PDictionary.FromFile (compiledEntitlements);
Assert.IsFalse (compiled.ContainsKey (EntitlementKeys.AllowExecutionOfJitCode), "#1");
var kag = ((PString) compiled ["keychain-access-group"]).Value;
Assert.That (kag, Is.EqualTo ("32UV7A8CDE.org.xamarin"), "value 1");

var archived = PDictionary.FromFile (archivedEntitlements);
Assert.IsTrue (archived.ContainsKey ("keychain-access-group"), "archived");
var archivedKag = ((PString) archived ["keychain-access-group"]).Value;
Assert.That (archivedKag, Is.EqualTo ("32UV7A8CDE.org.xamarin"), "archived value 1");
}

[Test]
public void TeamIdentifierPrefix ()
{
var customEntitlements = new TaskItem [] {
new TaskItem ("keychain-access-group", new Dictionary<string, string> { { "Type", "String" }, { "Value", "$(TeamIdentifierPrefix)org.xamarin" } }),
};
var task = CreateEntitlementsTask (out var compiledEntitlements, out var archivedEntitlements);
task.TargetFrameworkMoniker = ".NETCoreApp,Version=v6.0,Profile=ios";
task.CustomEntitlements = customEntitlements;
ExecuteTask (task);
var compiled = PDictionary.FromFile (compiledEntitlements);
Assert.IsFalse (compiled.ContainsKey (EntitlementKeys.AllowExecutionOfJitCode), "#1");
var kag = ((PString) compiled ["keychain-access-group"]).Value;
Assert.That (kag, Is.EqualTo ("Z8CSQKJE7R.org.xamarin"), "value 1");

var archived = PDictionary.FromFile (archivedEntitlements);
Assert.IsTrue (archived.ContainsKey ("keychain-access-group"), "archived");
var archivedKag = ((PString) archived ["keychain-access-group"]).Value;
Assert.That (archivedKag, Is.EqualTo ("Z8CSQKJE7R.org.xamarin"), "archived value 1");
}
}
}

6 comments on commit e9ae5ef

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.