Skip to content

Commit 20579e4

Browse files
author
Bill Huang
committed
8299994: java/security/Policy/Root/Root.java fails when home directory is read-only
Reviewed-by: rhalade
1 parent 5962226 commit 20579e4

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

test/jdk/ProblemList.txt

-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,6 @@ sun/security/pkcs11/rsa/TestKeyPairGenerator.java 8295343 linux-al
615615
sun/security/pkcs11/rsa/TestKeyFactory.java 8295343 linux-all
616616
sun/security/pkcs11/KeyStore/Basic.java 8295343 linux-all
617617

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

621620
############################################################################

test/jdk/TEST.groups

+2-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,8 @@ jdk_core_manual_no_input_security = \
635635
sun/security/smartcardio/TestMultiplePresent.java \
636636
sun/security/smartcardio/TestPresent.java \
637637
sun/security/smartcardio/TestTransmit.java \
638-
sun/security/tools/jarsigner/compatibility/Compatibility.java
638+
sun/security/tools/jarsigner/compatibility/Compatibility.java \
639+
java/security/Policy/Root/Root.java
639640

640641
jdk_core_manual_requires_human_input = \
641642
com/sun/jndi/dns/Test6991580.java \

test/jdk/java/security/Policy/Root/Root.java

+38-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@
2727
* @summary User Policy Setting is not recognized on Netscape 6
2828
* when invoked as root.
2929
* @library /test/lib
30-
* @run testng/othervm Root
30+
* @requires os.family != "windows"
31+
* @run testng/othervm/manual Root
3132
*/
3233

34+
/*
35+
* Run test as root user.
36+
* */
37+
3338
import org.testng.Assert;
3439
import org.testng.annotations.AfterTest;
3540
import org.testng.annotations.BeforeTest;
3641
import org.testng.annotations.Test;
3742

43+
import java.io.BufferedReader;
3844
import java.io.IOException;
45+
import java.io.InputStreamReader;
3946
import java.nio.file.Files;
4047
import java.nio.file.Path;
4148
import java.nio.file.Paths;
@@ -47,19 +54,48 @@ public class Root {
4754
private static final String ROOT = System.getProperty("user.home");
4855
private static final Path SOURCE = Paths.get(SRC, "Root.policy");
4956
private static final Path TARGET = Paths.get(ROOT, ".java.policy");
57+
private static final Path BACKUP = Paths.get(ROOT, ".backup.policy");
58+
private static final String ROOT_USER_ID = "0";
5059

5160
@BeforeTest
5261
public void setup() throws IOException {
62+
// Backup user policy file if it already exists
63+
if (TARGET.toFile().exists()) {
64+
Files.copy(TARGET, BACKUP, StandardCopyOption.REPLACE_EXISTING);
65+
}
5366
Files.copy(SOURCE, TARGET, StandardCopyOption.REPLACE_EXISTING);
5467
}
5568

5669
@AfterTest
5770
public void cleanUp() throws IOException {
5871
Files.delete(TARGET);
72+
// Restore original policy file if backup exists
73+
if (BACKUP.toFile().exists()) {
74+
Files.copy(BACKUP, TARGET, StandardCopyOption.REPLACE_EXISTING);
75+
Files.delete(BACKUP);
76+
}
5977
}
6078

6179
@Test
62-
private void test() {
80+
private void test() throws InterruptedException, IOException {
81+
System.out.println("Run test as root user.");
82+
83+
Process process = Runtime.getRuntime().exec("id -u");
84+
process.waitFor();
85+
if (process.exitValue() != 0) {
86+
throw new RuntimeException("Failed to retrieve user id.");
87+
}
88+
89+
try (BufferedReader reader = new BufferedReader(
90+
new InputStreamReader(process.getInputStream()))) {
91+
String line = reader.readLine();
92+
93+
if (!ROOT_USER_ID.equals(line)) {
94+
throw new RuntimeException(
95+
"This test needs to be run with root privilege.");
96+
}
97+
}
98+
6399
Policy p = Policy.getPolicy();
64100
Assert.assertTrue(p.implies(Root.class.getProtectionDomain(),
65101
new AllPermission()));

0 commit comments

Comments
 (0)