Skip to content

Commit

Permalink
8299994: java/security/Policy/Root/Root.java fails when home director…
Browse files Browse the repository at this point in the history
…y is read-only

Reviewed-by: rhalade
  • Loading branch information
Bill Huang committed Feb 3, 2023
1 parent 5962226 commit 20579e4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 0 additions & 1 deletion test/jdk/ProblemList.txt
Expand Up @@ -615,7 +615,6 @@ sun/security/pkcs11/rsa/TestKeyPairGenerator.java 8295343 linux-al
sun/security/pkcs11/rsa/TestKeyFactory.java 8295343 linux-all
sun/security/pkcs11/KeyStore/Basic.java 8295343 linux-all

java/security/Policy/Root/Root.java 8299994 generic-all
sun/security/provider/certpath/OCSP/OCSPNoContentLength.java 8300939 generic-all

############################################################################
Expand Down
3 changes: 2 additions & 1 deletion test/jdk/TEST.groups
Expand Up @@ -635,7 +635,8 @@ jdk_core_manual_no_input_security = \
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_requires_human_input = \
com/sun/jndi/dns/Test6991580.java \
Expand Down
40 changes: 38 additions & 2 deletions test/jdk/java/security/Policy/Root/Root.java
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

1 comment on commit 20579e4

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.