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+
3338import org .testng .Assert ;
3439import org .testng .annotations .AfterTest ;
3540import org .testng .annotations .BeforeTest ;
3641import org .testng .annotations .Test ;
3742
43+ import java .io .BufferedReader ;
3844import java .io .IOException ;
45+ import java .io .InputStreamReader ;
3946import java .nio .file .Files ;
4047import java .nio .file .Path ;
4148import 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