< prev index next >

jdk/test/java/security/testlibrary/Proc.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.BufferedReader;
  25 import java.io.File;
  26 import java.io.IOException;
  27 import java.io.InputStreamReader;
  28 import java.net.URL;
  29 import java.net.URLClassLoader;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.security.Permission;
  34 import java.util.ArrayList;

  35 import java.util.Base64;
  36 import java.util.Collections;
  37 import java.util.HashMap;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.Map.Entry;

  41 
  42 /**
  43  * This is a test library that makes writing a Java test that spawns multiple
  44  * Java processes easily.
  45  *
  46  * Usage:
  47  *
  48  *    Proc.create("Clazz")      // The class to launch
  49  *        .args("x")            // with args
  50  *        .env("env", "value")  // and an environment variable
  51  *        .prop("key","value")  // and a system property
  52  *        .perm(perm)           // with granted permissions
  53  *        .start();             // and start
  54  *
  55  * create/start must be called, args/env/prop/perm can be called zero or
  56  * multiple times between create and start.
  57  *
  58  * The controller can call inheritIO to share its I/O to the process.
  59  * Otherwise, it can send data into a proc's stdin with write/println, and
  60  * read its stdout with readLine. stderr is always redirected to DFILE


 167     public Proc prop(String a, String b) {
 168         prop.put(a, b);
 169         return this;
 170     }
 171     // Adds a perm to policy. Can be called multiple times. In order to make it
 172     // effective, please also call prop("java.security.manager", "").
 173     public Proc perm(Permission p) {
 174         perms.add(p);
 175         return this;
 176     }
 177     // Starts the proc
 178     public Proc start() throws IOException {
 179         List<String> cmd = new ArrayList<>();
 180         if (launcher != null) {
 181             cmd.add(launcher);
 182         } else {
 183             cmd.add(new File(new File(System.getProperty("java.home"), "bin"),
 184                         "java").getPath());
 185         }
 186 
 187         int n = 0;
 188         String addexports;
 189         while ((addexports = System.getProperty("jdk.launcher.addexports." + n)) != null) {
 190             prop("jdk.launcher.addexports." + n, addexports);
 191             n++;
 192         }
 193 
 194         Collections.addAll(cmd, splitProperty("test.vm.opts"));
 195         Collections.addAll(cmd, splitProperty("test.java.opts"));
 196 
 197         cmd.add("-cp");
 198         cmd.add(System.getProperty("test.class.path") + File.pathSeparator +
 199                 System.getProperty("test.src.path"));
 200 
 201         for (Entry<String,String> e: prop.entrySet()) {
 202             cmd.add("-D" + e.getKey() + "=" + e.getValue());
 203         }
 204         if (!perms.isEmpty()) {
 205             Path p = Files.createTempFile(
 206                     Paths.get(".").toAbsolutePath(), "policy", null);
 207             StringBuilder sb = new StringBuilder();
 208             sb.append("grant {\n");
 209             for (Permission perm: perms) {
 210                 // Sometimes a permission has no name or actions.
 211                 // but it's safe to use an empty string.
 212                 String s = String.format("%s \"%s\", \"%s\"",




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.BufferedReader;
  25 import java.io.File;
  26 import java.io.IOException;
  27 import java.io.InputStreamReader;


  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.security.Permission;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.Base64;
  35 import java.util.Collections;
  36 import java.util.HashMap;
  37 import java.util.List;
  38 import java.util.Map;
  39 import java.util.Map.Entry;
  40 import java.util.stream.Stream;
  41 
  42 /**
  43  * This is a test library that makes writing a Java test that spawns multiple
  44  * Java processes easily.
  45  *
  46  * Usage:
  47  *
  48  *    Proc.create("Clazz")      // The class to launch
  49  *        .args("x")            // with args
  50  *        .env("env", "value")  // and an environment variable
  51  *        .prop("key","value")  // and a system property
  52  *        .perm(perm)           // with granted permissions
  53  *        .start();             // and start
  54  *
  55  * create/start must be called, args/env/prop/perm can be called zero or
  56  * multiple times between create and start.
  57  *
  58  * The controller can call inheritIO to share its I/O to the process.
  59  * Otherwise, it can send data into a proc's stdin with write/println, and
  60  * read its stdout with readLine. stderr is always redirected to DFILE


 167     public Proc prop(String a, String b) {
 168         prop.put(a, b);
 169         return this;
 170     }
 171     // Adds a perm to policy. Can be called multiple times. In order to make it
 172     // effective, please also call prop("java.security.manager", "").
 173     public Proc perm(Permission p) {
 174         perms.add(p);
 175         return this;
 176     }
 177     // Starts the proc
 178     public Proc start() throws IOException {
 179         List<String> cmd = new ArrayList<>();
 180         if (launcher != null) {
 181             cmd.add(launcher);
 182         } else {
 183             cmd.add(new File(new File(System.getProperty("java.home"), "bin"),
 184                         "java").getPath());
 185         }
 186 
 187         Stream.of(jdk.internal.misc.VM.getRuntimeArguments())
 188             .filter(arg -> arg.startsWith("--add-exports="))
 189             .forEach(cmd::add);



 190 
 191         Collections.addAll(cmd, splitProperty("test.vm.opts"));
 192         Collections.addAll(cmd, splitProperty("test.java.opts"));
 193 
 194         cmd.add("-cp");
 195         cmd.add(System.getProperty("test.class.path") + File.pathSeparator +
 196                 System.getProperty("test.src.path"));
 197 
 198         for (Entry<String,String> e: prop.entrySet()) {
 199             cmd.add("-D" + e.getKey() + "=" + e.getValue());
 200         }
 201         if (!perms.isEmpty()) {
 202             Path p = Files.createTempFile(
 203                     Paths.get(".").toAbsolutePath(), "policy", null);
 204             StringBuilder sb = new StringBuilder();
 205             sb.append("grant {\n");
 206             for (Permission perm: perms) {
 207                 // Sometimes a permission has no name or actions.
 208                 // but it's safe to use an empty string.
 209                 String s = String.format("%s \"%s\", \"%s\"",


< prev index next >