Skip to content

Commit

Permalink
Backport 20579e48cf598e890aa35c5518ec8d0594f45385
Browse files Browse the repository at this point in the history
  • Loading branch information
GoeLin committed Dec 9, 2024
1 parent ace471e commit e3250dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion test/jdk/TEST.groups
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
40 changes: 38 additions & 2 deletions test/jdk/java/security/Policy/Root/Root.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
Expand Down

0 comments on commit e3250dc

Please sign in to comment.