test/jdk/jdk/jfr/api/consumer/security/TestStreamingRemote.java
author egahlin
Tue, 13 Aug 2019 14:07:04 +0200
branchJEP-349-branch
changeset 57726 6a7fa9735caf
parent 57717 4ce66d271065
child 58076 ca625d28c580
permissions -rw-r--r--
Put driver for security tests in separate file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     1
/*
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     4
 *
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    10
 *
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    15
 * accompanied this code).
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    16
 *
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    20
 *
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    23
 * questions.
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    24
 */
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    25
package jdk.jfr.api.consumer.security;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    26
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    27
import java.io.File;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    28
import java.io.IOException;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    29
import java.io.PrintWriter;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    30
import java.nio.file.Files;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    31
import java.nio.file.Path;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    32
import java.nio.file.Paths;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    33
import java.time.Duration;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    34
import java.time.Instant;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    35
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    36
import jdk.jfr.Event;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    37
import jdk.jfr.Recording;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    38
import jdk.jfr.consumer.EventStream;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    39
import jdk.test.lib.process.OutputAnalyzer;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    40
import jdk.test.lib.process.ProcessTools;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    41
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    42
/**
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    43
 * @test
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    44
 * @summary Test that a stream can be opened against a remote repository using
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    45
 *          only file permission
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    46
 * @key jfr
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    47
 * @requires vm.hasJFR
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    48
 * @library /test/lib
57726
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
    49
 *
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    50
 * @run main/othervm jdk.jfr.api.consumer.security.TestStreamingRemote
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    51
 */
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    52
public class TestStreamingRemote {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    53
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    54
    private static final String SUCCESS = "Success!";
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    55
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    56
    public static class TestEvent extends Event {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    57
    }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    58
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    59
    public static class Test {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    60
        public static void main(String... args) throws Exception {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    61
            Path repo = Paths.get(args[0]);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    62
            System.out.println("Repository: " + repo);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    63
            try (EventStream es = EventStream.openRepository(repo)) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    64
                es.setStartTime(Instant.EPOCH);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    65
                es.onEvent(e -> {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    66
                    System.out.println(SUCCESS);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    67
                    es.close();
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    68
                });
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    69
                es.start();
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    70
            }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    71
        }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    72
    }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    73
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    74
    public static void main(String... args) throws Exception {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    75
        try (Recording r = new Recording()) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    76
            r.setFlushInterval(Duration.ofSeconds(1));
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    77
            r.start();
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    78
            String repository = System.getProperty("jdk.jfr.repository");
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    79
            Path policy = createPolicyFile(repository);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    80
            TestEvent e = new TestEvent();
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    81
            e.commit();
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    82
            String[] c = new String[4];
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    83
            c[0] = "-Djava.security.manager";
57726
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
    84
            c[1] = "-Djava.security.policy=" + escapeBackslashes(policy.toString());
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    85
            c[2] = Test.class.getName();
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    86
            c[3] = repository;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    87
            OutputAnalyzer oa = ProcessTools.executeTestJvm(c);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    88
            oa.shouldContain(SUCCESS);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    89
        }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    90
    }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    91
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    92
    private static Path createPolicyFile(String repository) throws IOException {
57726
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
    93
        Path policy = Paths.get("permission.policy").toAbsolutePath();
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
    94
        try (PrintWriter pw = new PrintWriter(policy.toFile())) {
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    95
            pw.println("grant {");
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
    96
            // All the files and directories the contained in path
57726
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
    97
            String dir = escapeBackslashes(repository);
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
    98
            String contents = escapeBackslashes(repository + File.separatorChar + "-");
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
    99
            pw.println("  permission java.io.FilePermission \"" + dir + "\", \"read\";");
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
   100
            pw.println("  permission java.io.FilePermission \"" + contents + "\", \"read\";");
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   101
            pw.println("};");
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   102
            pw.println();
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   103
        }
57726
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
   104
        System.out.println("Permission file: " + policy);
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
   105
        for (String line : Files.readAllLines(policy)) {
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   106
            System.out.println(line);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   107
        }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   108
        System.out.println();
57726
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
   109
        return policy;
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   110
    }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   111
57726
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
   112
    // Needed for Windows
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
   113
    private static String escapeBackslashes(String text) {
6a7fa9735caf Put driver for security tests in separate file
egahlin
parents: 57717
diff changeset
   114
        return text.replace("\\", "\\\\");
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   115
    }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents:
diff changeset
   116
}