From e3250dc6890585f604549da762bf1336f869b5b5 Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Mon, 9 Dec 2024 14:42:26 +0100 Subject: [PATCH] Backport 20579e48cf598e890aa35c5518ec8d0594f45385 --- test/jdk/TEST.groups | 3 +- test/jdk/java/security/Policy/Root/Root.java | 40 +++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/test/jdk/TEST.groups b/test/jdk/TEST.groups index f1c09e1dc52..38014cbcb32 100644 --- a/test/jdk/TEST.groups +++ b/test/jdk/TEST.groups @@ -585,7 +585,8 @@ jdk_security_manual_no_input = \ sun/security/smartcardio/TestMultiplePresent.java \ sun/security/smartcardio/TestPresent.java \ sun/security/smartcardio/TestTransmit.java \ - sun/security/tools/jarsigner/compatibility/Compatibility.java + sun/security/tools/jarsigner/compatibility/Compatibility.java \ + java/security/Policy/Root/Root.java jdk_core_manual_interactive = \ com/sun/jndi/dns/Test6991580.java \ diff --git a/test/jdk/java/security/Policy/Root/Root.java b/test/jdk/java/security/Policy/Root/Root.java index bb339ee342c..8610a14be9b 100644 --- a/test/jdk/java/security/Policy/Root/Root.java +++ b/test/jdk/java/security/Policy/Root/Root.java @@ -27,15 +27,22 @@ * @summary User Policy Setting is not recognized on Netscape 6 * when invoked as root. * @library /test/lib - * @run testng/othervm Root + * @requires os.family != "windows" + * @run testng/othervm/manual Root */ +/* +* Run test as root user. +* */ + import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -47,19 +54,48 @@ public class Root { private static final String ROOT = System.getProperty("user.home"); private static final Path SOURCE = Paths.get(SRC, "Root.policy"); private static final Path TARGET = Paths.get(ROOT, ".java.policy"); + private static final Path BACKUP = Paths.get(ROOT, ".backup.policy"); + private static final String ROOT_USER_ID = "0"; @BeforeTest public void setup() throws IOException { + // Backup user policy file if it already exists + if (TARGET.toFile().exists()) { + Files.copy(TARGET, BACKUP, StandardCopyOption.REPLACE_EXISTING); + } Files.copy(SOURCE, TARGET, StandardCopyOption.REPLACE_EXISTING); } @AfterTest public void cleanUp() throws IOException { Files.delete(TARGET); + // Restore original policy file if backup exists + if (BACKUP.toFile().exists()) { + Files.copy(BACKUP, TARGET, StandardCopyOption.REPLACE_EXISTING); + Files.delete(BACKUP); + } } @Test - private void test() { + private void test() throws InterruptedException, IOException { + System.out.println("Run test as root user."); + + Process process = Runtime.getRuntime().exec("id -u"); + process.waitFor(); + if (process.exitValue() != 0) { + throw new RuntimeException("Failed to retrieve user id."); + } + + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(process.getInputStream()))) { + String line = reader.readLine(); + + if (!ROOT_USER_ID.equals(line)) { + throw new RuntimeException( + "This test needs to be run with root privilege."); + } + } + Policy p = Policy.getPolicy(); Assert.assertTrue(p.implies(Root.class.getProtectionDomain(), new AllPermission()));