Skip to content

Commit cac2519

Browse files
author
Alexander Matveev
committed
8356578: Test --mac-entitlements
Reviewed-by: asemenyuk
1 parent aab3fc5 commit cac2519

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LauncherVerifier.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,20 @@ private void verifyMacEntitlements(JPackageCommand cmd) throws ParserConfigurati
339339

340340
TKit.assertTrue(entitlements.isPresent(), String.format("Check [%s] launcher is signed with entitlements", name));
341341

342+
var customFile = Optional.ofNullable(cmd.getArgumentValue("--mac-entitlements")).map(Path::of);
343+
if (customFile.isEmpty()) {
344+
// Try from the resource dir.
345+
var resourceDirFile = Optional.ofNullable(cmd.getArgumentValue("--resource-dir")).map(Path::of).map(resourceDir -> {
346+
return resourceDir.resolve(cmd.name() + ".entitlements");
347+
}).filter(Files::exists);
348+
if (resourceDirFile.isPresent()) {
349+
customFile = resourceDirFile;
350+
}
351+
}
352+
342353
Map<String, Object> expected;
343-
if (cmd.hasArgument("--mac-entitlements")) {
344-
expected = new PListReader(Files.readAllBytes(Path.of(cmd.getArgumentValue("--mac-entitlements")))).toMap(true);
354+
if (customFile.isPresent()) {
355+
expected = new PListReader(Files.readAllBytes(customFile.orElseThrow())).toMap(true);
345356
} else if (cmd.hasArgument("--mac-app-store")) {
346357
expected = DefaultEntitlements.APP_STORE;
347358
} else {
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import static jdk.jpackage.internal.util.PListWriter.writeDict;
25+
import static jdk.jpackage.internal.util.PListWriter.writePList;
26+
import static jdk.jpackage.internal.util.PListWriter.writeBoolean;
27+
import static jdk.jpackage.internal.util.XmlUtils.createXml;
28+
import static jdk.jpackage.internal.util.XmlUtils.toXmlConsumer;
29+
30+
import java.io.IOException;
31+
import java.nio.file.Path;
32+
import java.util.Date;
33+
34+
import jdk.jpackage.test.JPackageCommand;
35+
import jdk.jpackage.test.TKit;
36+
import jdk.jpackage.test.Annotations.Test;
37+
import jdk.jpackage.test.Annotations.Parameter;
38+
39+
/*
40+
* Test generates signed app-image with custom entitlements file from the
41+
* "--mac-entitlements" parameter and the resource directory. Following cases
42+
* are covered:
43+
* - Custom entitlements file in the resource directory.
44+
* - Custom entitlements file specified with the "--mac-entitlements" parameter.
45+
* - Custom entitlements file in the resource directory and specified with the
46+
* "--mac-entitlements" parameter.
47+
*/
48+
49+
/*
50+
* @test
51+
* @summary jpackage with --type app-image "--mac-entitlements" parameter
52+
* @library /test/jdk/tools/jpackage/helpers
53+
* @library base
54+
* @build SigningBase
55+
* @build jdk.jpackage.test.*
56+
* @build EntitlementsTest
57+
* @requires (jpackage.test.MacSignTests == "run")
58+
* @run main/othervm/timeout=720 -Xmx512m jdk.jpackage.test.Main
59+
* --jpt-run=EntitlementsTest
60+
* --jpt-before-run=SigningBase.verifySignTestEnvReady
61+
*/
62+
public class EntitlementsTest {
63+
64+
void createEntitlementsFile(Path file, boolean microphone) throws IOException {
65+
createXml(file, xml -> {
66+
writePList(xml, toXmlConsumer(() -> {
67+
writeDict(xml, toXmlConsumer(() -> {
68+
writeBoolean(xml, "com.apple.security.cs.allow-jit", true);
69+
writeBoolean(xml, "com.apple.security.cs.allow-unsigned-executable-memory", true);
70+
writeBoolean(xml, "com.apple.security.cs.disable-library-validation", true);
71+
writeBoolean(xml, "com.apple.security.cs.allow-dyld-environment-variables", true);
72+
writeBoolean(xml, "com.apple.security.cs.debugger", true);
73+
writeBoolean(xml, "com.apple.security.device.audio-input", true);
74+
writeBoolean(xml, "com.apple.security.device.microphone", microphone);
75+
}));
76+
}));
77+
});
78+
}
79+
80+
@Test
81+
// ({"--mac-app-store", doMacEntitlements", "doResources"})
82+
@Parameter({"false", "true", "false"})
83+
@Parameter({"false", "false", "true"})
84+
@Parameter({"false", "true", "true"})
85+
@Parameter({"true", "true", "false"})
86+
@Parameter({"true", "false", "true"})
87+
@Parameter({"true", "true", "true"})
88+
public void test(boolean appStore, boolean doMacEntitlements, boolean doResources) throws Exception {
89+
final Path macEntitlementsFile;
90+
final Path resourcesDir;
91+
92+
if (doMacEntitlements) {
93+
macEntitlementsFile = TKit.createTempFile("EntitlementsTest.plist");
94+
createEntitlementsFile(macEntitlementsFile, true);
95+
} else {
96+
macEntitlementsFile = null;
97+
}
98+
99+
if (doResources) {
100+
resourcesDir = TKit.createTempDirectory("resources");
101+
createEntitlementsFile(resourcesDir.resolve("EntitlementsTest.entitlements"), false);
102+
} else {
103+
resourcesDir = null;
104+
}
105+
106+
JPackageCommand cmd = JPackageCommand.helloAppImage()
107+
.addArguments("--mac-sign", "--mac-signing-keychain",
108+
SigningBase.getKeyChain(), "--mac-app-image-sign-identity",
109+
SigningBase.getAppCert(SigningBase.CertIndex.ASCII_INDEX.value()));
110+
if (appStore) {
111+
cmd.addArguments("--mac-app-store");
112+
}
113+
if (doMacEntitlements) {
114+
cmd.addArguments("--mac-entitlements",
115+
macEntitlementsFile.toAbsolutePath().toString());
116+
}
117+
if (doResources) {
118+
cmd.addArguments("--resource-dir",
119+
resourcesDir.toAbsolutePath().toString());
120+
}
121+
122+
cmd.executeAndAssertHelloAppImageCreated();
123+
}
124+
}

0 commit comments

Comments
 (0)