1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   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 package jdk.test.lib;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.Arrays;
  32 import java.util.List;
  33 import java.util.stream.Collectors;
  34 import java.util.stream.Stream;
  35 
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import jdk.test.lib.process.ProcessTools;
  38 
  39 public class SecurityTools {
  40 
  41     public static final String RESPONSE_FILE = "security_tools_response.txt";
  42 
  43     private static ProcessBuilder getProcessBuilder(String tool, List<String> args) {
  44         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(tool)
  45                 .addVMArg("-Duser.language=en")
  46                 .addVMArg("-Duser.country=US");
  47         if (!Platform.isWindows()) {
  48             launcher.addVMArg("-Djava.security.egd=file:/dev/./urandom");
  49         }
  50         for (String arg : args) {
  51             if (arg.startsWith("-J")) {
  52                 launcher.addVMArg(arg.substring(2));
  53             } else {
  54                 launcher.addToolArg(arg);
  55             }
  56         }
  57         return new ProcessBuilder(launcher.getCommand());
  58     }
  59 
  60     // keytool
  61 
  62     public static OutputAnalyzer keytool(List<String> args)
  63             throws Exception {
  64 
  65         ProcessBuilder pb = getProcessBuilder("keytool", args);
  66 
  67         Path p = Paths.get(RESPONSE_FILE);
  68         if (!Files.exists(p)) {
  69             Files.createFile(p);
  70         }
  71         pb.redirectInput(ProcessBuilder.Redirect.from(new File(RESPONSE_FILE)));
  72 
  73         try {
  74             return execute(pb);
  75         } finally {
  76             Files.delete(p);
  77         }
  78     }
  79 
  80     // Only call this if there is no white space in every argument
  81     public static OutputAnalyzer keytool(String args) throws Exception {
  82         return keytool(args.split("\\s+"));
  83     }
  84 
  85     public static OutputAnalyzer keytool(String... args) throws Exception {
  86         return keytool(List.of(args));
  87     }
  88 
  89     public static void setResponse(String... responses) throws IOException {
  90         String text;
  91         if (responses.length > 0) {
  92             text = Stream.of(responses).collect(
  93                     Collectors.joining("\n", "", "\n"));
  94         } else {
  95             text = "";
  96         }
  97         Files.write(Paths.get(RESPONSE_FILE), text.getBytes());
  98     }
  99 
 100     // jarsigner
 101 
 102     public static OutputAnalyzer jarsigner(List<String> args)
 103             throws Exception {
 104         return execute(getProcessBuilder("jarsigner", args));
 105     }
 106 
 107     private static OutputAnalyzer execute(ProcessBuilder pb) throws Exception {
 108         try {
 109             OutputAnalyzer oa = ProcessTools.executeCommand(pb);
 110             System.out.println("Exit value: " + oa.getExitValue());
 111             return oa;
 112         } catch (Throwable t) {
 113             if (t instanceof Exception) {
 114                 throw (Exception) t;
 115             } else {
 116                 throw new Exception(t);
 117             }
 118         }
 119     }
 120 
 121     // Only call this if there is no white space in every argument
 122     public static OutputAnalyzer jarsigner(String args) throws Exception {
 123 
 124         return jarsigner(args.split("\\s+"));
 125     }
 126 
 127     public static OutputAnalyzer jarsigner(String... args) throws Exception {
 128         return jarsigner(List.of(args));
 129     }
 130 }
 131