27
27
* @summary User Policy Setting is not recognized on Netscape 6
28
28
* when invoked as root.
29
29
* @library /test/lib
30
- * @run testng/othervm Root
30
+ * @requires os.family != "windows"
31
+ * @run testng/othervm/manual Root
31
32
*/
32
33
34
+ /*
35
+ * Run test as root user.
36
+ * */
37
+
33
38
import org .testng .Assert ;
34
39
import org .testng .annotations .AfterTest ;
35
40
import org .testng .annotations .BeforeTest ;
36
41
import org .testng .annotations .Test ;
37
42
43
+ import java .io .BufferedReader ;
38
44
import java .io .IOException ;
45
+ import java .io .InputStreamReader ;
39
46
import java .nio .file .Files ;
40
47
import java .nio .file .Path ;
41
48
import java .nio .file .Paths ;
@@ -47,19 +54,48 @@ public class Root {
47
54
private static final String ROOT = System .getProperty ("user.home" );
48
55
private static final Path SOURCE = Paths .get (SRC , "Root.policy" );
49
56
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" ;
50
59
51
60
@ BeforeTest
52
61
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
+ }
53
66
Files .copy (SOURCE , TARGET , StandardCopyOption .REPLACE_EXISTING );
54
67
}
55
68
56
69
@ AfterTest
57
70
public void cleanUp () throws IOException {
58
71
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
+ }
59
77
}
60
78
61
79
@ 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
+
63
99
Policy p = Policy .getPolicy ();
64
100
Assert .assertTrue (p .implies (Root .class .getProtectionDomain (),
65
101
new AllPermission ()));
0 commit comments