Put driver for security tests in separate file JEP-349-branch
authoregahlin
Tue, 13 Aug 2019 14:07:04 +0200
branchJEP-349-branch
changeset 57726 6a7fa9735caf
parent 57717 4ce66d271065
child 57727 15ae158e2fb6
Put driver for security tests in separate file
test/jdk/jdk/jfr/api/consumer/security/TestMissingPermission.java
test/jdk/jdk/jfr/api/consumer/security/TestRecordingFile.java
test/jdk/jdk/jfr/api/consumer/security/TestRecordingStream.java
test/jdk/jdk/jfr/api/consumer/security/TestStreamingFile.java
test/jdk/jdk/jfr/api/consumer/security/TestStreamingLocal.java
test/jdk/jdk/jfr/api/consumer/security/TestStreamingRemote.java
--- a/test/jdk/jdk/jfr/api/consumer/security/TestMissingPermission.java	Tue Aug 13 03:58:29 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/security/TestMissingPermission.java	Tue Aug 13 14:07:04 2019 +0200
@@ -36,6 +36,7 @@
  * @key jfr
  * @requires vm.hasJFR
  * @library /test/lib
+ *
  * @run main/othervm/secure=java.lang.SecurityManager/java.security.policy=no-permission.policy
  *      jdk.jfr.api.consumer.security.TestMissingPermission
  */
@@ -49,7 +50,7 @@
     private static void testRecordingStream() throws IOException {
         try {
             try (EventStream es = EventStream.openRepository()) {
-                fail();
+                throw new AssertionError("Should not be able to create EventStream without FlightRecorderPermission");
             }
         } catch (SecurityException se) {
             // OK, as expected
@@ -59,14 +60,10 @@
     private static void testOpenRepository() throws IOException {
         try {
             try (RecordingStream es = new RecordingStream()) {
-                fail();
+                throw new AssertionError("Should not be able to create RecordingStream without FlightRecorderPermission");
             }
         } catch (SecurityException se) {
             // OK, as expected
         }
     }
-
-    private static void fail() {
-        throw new AssertionError("Should not be able to create EventStream with FlightRecorderPermission");
-    }
 }
--- a/test/jdk/jdk/jfr/api/consumer/security/TestRecordingFile.java	Tue Aug 13 03:58:29 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/security/TestRecordingFile.java	Tue Aug 13 14:07:04 2019 +0200
@@ -24,12 +24,9 @@
  */
 package jdk.jfr.api.consumer.security;
 
-import java.nio.file.Path;
 import java.nio.file.Paths;
 
-import jdk.jfr.Recording;
 import jdk.jfr.consumer.RecordingFile;
-import jdk.test.lib.jfr.EventNames;
 
 /**
  * @test
@@ -37,29 +34,17 @@
  * @key jfr
  * @requires vm.hasJFR
  * @library /test/lib
- * @run driver jdk.jfr.api.consumer.security.TestRecordingFile$Dumper
+ *
+ * @run driver jdk.jfr.api.consumer.security.DriverRecordingDumper
+ *      test-recording-file.jfr
  * @run main/othervm/secure=java.lang.SecurityManager/java.security.policy=no-permission.policy
  *      jdk.jfr.api.consumer.security.TestRecordingFile
- *
+ *      test-recording-file.jfr
  */
 public class TestRecordingFile {
-    public final static Path DUMP_FILE = Paths.get("dump.jfr");
-
-    public static class Dumper {
-        public static void main(String... args) throws Exception {
-            try (Recording r = new Recording()) {
-                // Enable JVM event, no write permission needed
-                r.enable(EventNames.JVMInformation);
-                r.start();
-                r.stop();
-                r.dump(DUMP_FILE);
-            }
-        }
-    }
-
     public static void main(String... args) throws Exception {
         try {
-            RecordingFile.readAllEvents(DUMP_FILE);
+            RecordingFile.readAllEvents(Paths.get(args[0]));
             throw new AssertionError("Expected SecurityException");
         } catch (SecurityException se) {
             // OK, as expected
--- a/test/jdk/jdk/jfr/api/consumer/security/TestRecordingStream.java	Tue Aug 13 03:58:29 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/security/TestRecordingStream.java	Tue Aug 13 14:07:04 2019 +0200
@@ -37,6 +37,7 @@
  * @key jfr
  * @requires vm.hasJFR
  * @library /test/lib
+ *
  * @run main/othervm/secure=java.lang.SecurityManager/java.security.policy=local-streaming.policy
  *      jdk.jfr.api.consumer.security.TestStreamingLocal
  */
--- a/test/jdk/jdk/jfr/api/consumer/security/TestStreamingFile.java	Tue Aug 13 03:58:29 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/security/TestStreamingFile.java	Tue Aug 13 14:07:04 2019 +0200
@@ -24,10 +24,8 @@
  */
 package jdk.jfr.api.consumer.security;
 
-import java.nio.file.Path;
 import java.nio.file.Paths;
 
-import jdk.jfr.Recording;
 import jdk.jfr.consumer.EventStream;
 
 /**
@@ -36,29 +34,17 @@
  * @key jfr
  * @requires vm.hasJFR
  * @library /test/lib
- * @build jdk.jfr.api.consumer.security.TestStreamingFile
- * @run driver jdk.jfr.api.consumer.security.TestStreamingFile$Dumper
+ *
+ * @run driver jdk.jfr.api.consumer.security.DriverRecordingDumper
+ *      test-streaming-file.jfr
  * @run main/othervm/secure=java.lang.SecurityManager/java.security.policy=no-permission.policy
  *      jdk.jfr.api.consumer.security.TestStreamingFile
- *
+ *      test-streaming-file.jfr
  */
 public class TestStreamingFile {
-    public final static Path DUMP_FILE = Paths.get("dump.jfr");
-
-    public static class Dumper {
-        public static void main(String... args) throws Exception {
-            try (Recording r = new Recording()) {
-                // Enable JVM event, no write permission needed
-                r.enable("jdk.JVMInformation");
-                r.start();
-                r.stop();
-                r.dump(DUMP_FILE);
-            }
-        }
-    }
 
     public static void main(String... args) throws Exception {
-        try (EventStream es = EventStream.openFile(DUMP_FILE)) {
+        try (EventStream es = EventStream.openFile(Paths.get(args[0]))) {
             throw new AssertionError("Expected SecurityException");
         } catch (SecurityException se) {
             // OK, as expected
--- a/test/jdk/jdk/jfr/api/consumer/security/TestStreamingLocal.java	Tue Aug 13 03:58:29 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/security/TestStreamingLocal.java	Tue Aug 13 14:07:04 2019 +0200
@@ -38,6 +38,7 @@
  * @key jfr
  * @requires vm.hasJFR
  * @library /test/lib
+ *
  * @run main/othervm/secure=java.lang.SecurityManager/java.security.policy=local-streaming.policy
  *      jdk.jfr.api.consumer.security.TestStreamingLocal
  */
--- a/test/jdk/jdk/jfr/api/consumer/security/TestStreamingRemote.java	Tue Aug 13 03:58:29 2019 +0200
+++ b/test/jdk/jdk/jfr/api/consumer/security/TestStreamingRemote.java	Tue Aug 13 14:07:04 2019 +0200
@@ -34,7 +34,6 @@
 import java.time.Instant;
 
 import jdk.jfr.Event;
-import jdk.jfr.Name;
 import jdk.jfr.Recording;
 import jdk.jfr.consumer.EventStream;
 import jdk.test.lib.process.OutputAnalyzer;
@@ -47,13 +46,13 @@
  * @key jfr
  * @requires vm.hasJFR
  * @library /test/lib
+ *
  * @run main/othervm jdk.jfr.api.consumer.security.TestStreamingRemote
  */
 public class TestStreamingRemote {
 
     private static final String SUCCESS = "Success!";
 
-    @Name("Test")
     public static class TestEvent extends Event {
     }
 
@@ -82,7 +81,7 @@
             e.commit();
             String[] c = new String[4];
             c[0] = "-Djava.security.manager";
-            c[1] = "-Djava.security.policy=" + escape(policy.toString());
+            c[1] = "-Djava.security.policy=" + escapeBackslashes(policy.toString());
             c[2] = Test.class.getName();
             c[3] = repository;
             OutputAnalyzer oa = ProcessTools.executeTestJvm(c);
@@ -91,34 +90,27 @@
     }
 
     private static Path createPolicyFile(String repository) throws IOException {
-        Path p = Paths.get("permission.policy").toAbsolutePath();
-        try (PrintWriter pw = new PrintWriter(p.toFile())) {
+        Path policy = Paths.get("permission.policy").toAbsolutePath();
+        try (PrintWriter pw = new PrintWriter(policy.toFile())) {
             pw.println("grant {");
             // All the files and directories the contained in path
-            String dir = escape(repository);
-            String contents = escape(repository + File.separatorChar + "-");
-            pw.println("   permission java.io.FilePermission \"" + dir + "\", \"read\";");
-            pw.println("   permission java.io.FilePermission \"" + contents + "\", \"read\";");
+            String dir = escapeBackslashes(repository);
+            String contents = escapeBackslashes(repository + File.separatorChar + "-");
+            pw.println("  permission java.io.FilePermission \"" + dir + "\", \"read\";");
+            pw.println("  permission java.io.FilePermission \"" + contents + "\", \"read\";");
             pw.println("};");
             pw.println();
         }
-        System.out.println("Permission file: " + p);
-        for (String line : Files.readAllLines(p)) {
+        System.out.println("Permission file: " + policy);
+        for (String line : Files.readAllLines(policy)) {
             System.out.println(line);
         }
         System.out.println();
-        return p;
+        return policy;
     }
 
-    // Double quote needed for Windows
-    private static String escape(String text) {
-        StringBuilder sb = new StringBuilder();
-        for (char c : text.toCharArray()) {
-            if (c == '\\') {
-                sb.append(c);
-            }
-            sb.append(c);
-        }
-        return sb.toString();
+    // Needed for Windows
+    private static String escapeBackslashes(String text) {
+        return text.replace("\\", "\\\\");
     }
 }