test/jdk/jdk/jfr/api/consumer/security/TestStreamingRemote.java
branchJEP-349-branch
changeset 57726 6a7fa9735caf
parent 57717 4ce66d271065
child 58076 ca625d28c580
equal deleted inserted replaced
57717:4ce66d271065 57726:6a7fa9735caf
    32 import java.nio.file.Paths;
    32 import java.nio.file.Paths;
    33 import java.time.Duration;
    33 import java.time.Duration;
    34 import java.time.Instant;
    34 import java.time.Instant;
    35 
    35 
    36 import jdk.jfr.Event;
    36 import jdk.jfr.Event;
    37 import jdk.jfr.Name;
       
    38 import jdk.jfr.Recording;
    37 import jdk.jfr.Recording;
    39 import jdk.jfr.consumer.EventStream;
    38 import jdk.jfr.consumer.EventStream;
    40 import jdk.test.lib.process.OutputAnalyzer;
    39 import jdk.test.lib.process.OutputAnalyzer;
    41 import jdk.test.lib.process.ProcessTools;
    40 import jdk.test.lib.process.ProcessTools;
    42 
    41 
    45  * @summary Test that a stream can be opened against a remote repository using
    44  * @summary Test that a stream can be opened against a remote repository using
    46  *          only file permission
    45  *          only file permission
    47  * @key jfr
    46  * @key jfr
    48  * @requires vm.hasJFR
    47  * @requires vm.hasJFR
    49  * @library /test/lib
    48  * @library /test/lib
       
    49  *
    50  * @run main/othervm jdk.jfr.api.consumer.security.TestStreamingRemote
    50  * @run main/othervm jdk.jfr.api.consumer.security.TestStreamingRemote
    51  */
    51  */
    52 public class TestStreamingRemote {
    52 public class TestStreamingRemote {
    53 
    53 
    54     private static final String SUCCESS = "Success!";
    54     private static final String SUCCESS = "Success!";
    55 
    55 
    56     @Name("Test")
       
    57     public static class TestEvent extends Event {
    56     public static class TestEvent extends Event {
    58     }
    57     }
    59 
    58 
    60     public static class Test {
    59     public static class Test {
    61         public static void main(String... args) throws Exception {
    60         public static void main(String... args) throws Exception {
    80             Path policy = createPolicyFile(repository);
    79             Path policy = createPolicyFile(repository);
    81             TestEvent e = new TestEvent();
    80             TestEvent e = new TestEvent();
    82             e.commit();
    81             e.commit();
    83             String[] c = new String[4];
    82             String[] c = new String[4];
    84             c[0] = "-Djava.security.manager";
    83             c[0] = "-Djava.security.manager";
    85             c[1] = "-Djava.security.policy=" + escape(policy.toString());
    84             c[1] = "-Djava.security.policy=" + escapeBackslashes(policy.toString());
    86             c[2] = Test.class.getName();
    85             c[2] = Test.class.getName();
    87             c[3] = repository;
    86             c[3] = repository;
    88             OutputAnalyzer oa = ProcessTools.executeTestJvm(c);
    87             OutputAnalyzer oa = ProcessTools.executeTestJvm(c);
    89             oa.shouldContain(SUCCESS);
    88             oa.shouldContain(SUCCESS);
    90         }
    89         }
    91     }
    90     }
    92 
    91 
    93     private static Path createPolicyFile(String repository) throws IOException {
    92     private static Path createPolicyFile(String repository) throws IOException {
    94         Path p = Paths.get("permission.policy").toAbsolutePath();
    93         Path policy = Paths.get("permission.policy").toAbsolutePath();
    95         try (PrintWriter pw = new PrintWriter(p.toFile())) {
    94         try (PrintWriter pw = new PrintWriter(policy.toFile())) {
    96             pw.println("grant {");
    95             pw.println("grant {");
    97             // All the files and directories the contained in path
    96             // All the files and directories the contained in path
    98             String dir = escape(repository);
    97             String dir = escapeBackslashes(repository);
    99             String contents = escape(repository + File.separatorChar + "-");
    98             String contents = escapeBackslashes(repository + File.separatorChar + "-");
   100             pw.println("   permission java.io.FilePermission \"" + dir + "\", \"read\";");
    99             pw.println("  permission java.io.FilePermission \"" + dir + "\", \"read\";");
   101             pw.println("   permission java.io.FilePermission \"" + contents + "\", \"read\";");
   100             pw.println("  permission java.io.FilePermission \"" + contents + "\", \"read\";");
   102             pw.println("};");
   101             pw.println("};");
   103             pw.println();
   102             pw.println();
   104         }
   103         }
   105         System.out.println("Permission file: " + p);
   104         System.out.println("Permission file: " + policy);
   106         for (String line : Files.readAllLines(p)) {
   105         for (String line : Files.readAllLines(policy)) {
   107             System.out.println(line);
   106             System.out.println(line);
   108         }
   107         }
   109         System.out.println();
   108         System.out.println();
   110         return p;
   109         return policy;
   111     }
   110     }
   112 
   111 
   113     // Double quote needed for Windows
   112     // Needed for Windows
   114     private static String escape(String text) {
   113     private static String escapeBackslashes(String text) {
   115         StringBuilder sb = new StringBuilder();
   114         return text.replace("\\", "\\\\");
   116         for (char c : text.toCharArray()) {
       
   117             if (c == '\\') {
       
   118                 sb.append(c);
       
   119             }
       
   120             sb.append(c);
       
   121         }
       
   122         return sb.toString();
       
   123     }
   115     }
   124 }
   116 }