Skip to content

Commit 7075fa1

Browse files
[release/8.0.1xx-xcode15.1] [msbuild] Process custom entitlements as if they came from an Entitlements.plist file. (#19955)
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. Backport of #19942
1 parent dae5688 commit 7075fa1

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

msbuild/Xamarin.MacDev.Tasks/Tasks/CompileEntitlementsTaskBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ PDictionary MergeEntitlementDictionary (PDictionary dict, MobileProvision? profi
241241
return result;
242242
}
243243

244-
void AddCustomEntitlements (PDictionary dict)
244+
void AddCustomEntitlements (PDictionary dict, MobileProvision? profile)
245245
{
246246
if (CustomEntitlements is null)
247247
return;
@@ -280,7 +280,7 @@ void AddCustomEntitlements (PDictionary dict)
280280
dict [entitlement] = new PBoolean (booleanValue);
281281
break;
282282
case "string":
283-
dict [entitlement] = new PString (value ?? string.Empty);
283+
dict [entitlement] = MergeEntitlementString (new PString (value), profile, entitlement == ApplicationIdentifierKey);
284284
break;
285285
case "stringarray":
286286
var arraySeparator = item.GetMetadata ("ArraySeparator");
@@ -289,7 +289,7 @@ void AddCustomEntitlements (PDictionary dict)
289289
var arrayContent = value.Split (new string [] { arraySeparator }, StringSplitOptions.None);
290290
var parray = new PArray ();
291291
foreach (var element in arrayContent)
292-
parray.Add (new PString (element));
292+
parray.Add (MergeEntitlementString (new PString (element), profile, entitlement == ApplicationIdentifierKey));
293293
dict [entitlement] = parray;
294294
break;
295295
default:
@@ -407,7 +407,7 @@ protected virtual PDictionary GetCompiledEntitlements (MobileProvision? profile,
407407
break;
408408
}
409409

410-
AddCustomEntitlements (entitlements);
410+
AddCustomEntitlements (entitlements, profile);
411411

412412
return entitlements;
413413
}

tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/CompileEntitlementsTaskTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,19 @@ CustomCompileEntitlements CreateEntitlementsTask (out string compiledEntitlement
4444
compiledEntitlements = task.CompiledEntitlements.ItemSpec;
4545
archivedEntitlements = Path.Combine (AppBundlePath, "archived-expanded-entitlements.xcent");
4646

47+
DeleteDirectory (Path.Combine (MonoTouchProjectPath, "bin"));
48+
DeleteDirectory (Path.Combine (MonoTouchProjectPath, "obj"));
49+
4750
return task;
4851
}
4952

53+
void DeleteDirectory (string directory)
54+
{
55+
if (!Directory.Exists (directory))
56+
return;
57+
Directory.Delete (directory, true);
58+
}
59+
5060
[Test (Description = "Xambug #46298")]
5161
public void ValidateEntitlement ()
5262
{
@@ -207,5 +217,46 @@ public void AllowJit_None ()
207217
Assert.IsFalse (compiled.ContainsKey (EntitlementKeys.AllowExecutionOfJitCode), "#1");
208218
}
209219

220+
[Test]
221+
public void AppIdentifierPrefix ()
222+
{
223+
var customEntitlements = new TaskItem [] {
224+
new TaskItem ("keychain-access-group", new Dictionary<string, string> { { "Type", "String" }, { "Value", "$(AppIdentifierPrefix)org.xamarin" } }),
225+
};
226+
var task = CreateEntitlementsTask (out var compiledEntitlements, out var archivedEntitlements);
227+
task.TargetFrameworkMoniker = ".NETCoreApp,Version=v6.0,Profile=ios";
228+
task.CustomEntitlements = customEntitlements;
229+
ExecuteTask (task);
230+
var compiled = PDictionary.FromFile (compiledEntitlements);
231+
Assert.IsFalse (compiled.ContainsKey (EntitlementKeys.AllowExecutionOfJitCode), "#1");
232+
var kag = ((PString) compiled ["keychain-access-group"]).Value;
233+
Assert.That (kag, Is.EqualTo ("32UV7A8CDE.org.xamarin"), "value 1");
234+
235+
var archived = PDictionary.FromFile (archivedEntitlements);
236+
Assert.IsTrue (archived.ContainsKey ("keychain-access-group"), "archived");
237+
var archivedKag = ((PString) archived ["keychain-access-group"]).Value;
238+
Assert.That (archivedKag, Is.EqualTo ("32UV7A8CDE.org.xamarin"), "archived value 1");
239+
}
240+
241+
[Test]
242+
public void TeamIdentifierPrefix ()
243+
{
244+
var customEntitlements = new TaskItem [] {
245+
new TaskItem ("keychain-access-group", new Dictionary<string, string> { { "Type", "String" }, { "Value", "$(TeamIdentifierPrefix)org.xamarin" } }),
246+
};
247+
var task = CreateEntitlementsTask (out var compiledEntitlements, out var archivedEntitlements);
248+
task.TargetFrameworkMoniker = ".NETCoreApp,Version=v6.0,Profile=ios";
249+
task.CustomEntitlements = customEntitlements;
250+
ExecuteTask (task);
251+
var compiled = PDictionary.FromFile (compiledEntitlements);
252+
Assert.IsFalse (compiled.ContainsKey (EntitlementKeys.AllowExecutionOfJitCode), "#1");
253+
var kag = ((PString) compiled ["keychain-access-group"]).Value;
254+
Assert.That (kag, Is.EqualTo ("Z8CSQKJE7R.org.xamarin"), "value 1");
255+
256+
var archived = PDictionary.FromFile (archivedEntitlements);
257+
Assert.IsTrue (archived.ContainsKey ("keychain-access-group"), "archived");
258+
var archivedKag = ((PString) archived ["keychain-access-group"]).Value;
259+
Assert.That (archivedKag, Is.EqualTo ("Z8CSQKJE7R.org.xamarin"), "archived value 1");
260+
}
210261
}
211262
}

0 commit comments

Comments
 (0)