8299994: java/security/Policy/Root/Root.java fails when home directory is read-only

Reviewed-by: rhalade
This commit is contained in:
Bill Huang 2023-02-03 19:19:14 +00:00
parent 5962226cc3
commit 20579e48cf
3 changed files with 40 additions and 4 deletions

View File

@ -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
############################################################################

View File

@ -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 \

View File

@ -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()));