jdk/test/lib/security/SecurityTools.java
changeset 43097 69b209a7a0a4
parent 43060 ed4f16888e12
parent 43096 22875dc4eec5
child 43098 124d24e7988c
equal deleted inserted replaced
43060:ed4f16888e12 43097:69b209a7a0a4
     1 /*
       
     2  * Copyright (c) 2016, 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 import java.util.ArrayList;
       
    25 import java.util.Collections;
       
    26 import java.util.List;
       
    27 import jdk.testlibrary.JDKToolLauncher;
       
    28 import jdk.testlibrary.OutputAnalyzer;
       
    29 import jdk.testlibrary.ProcessTools;
       
    30 
       
    31 public class SecurityTools {
       
    32 
       
    33     public static final String NO_ALIAS = null;
       
    34 
       
    35     // keytool
       
    36 
       
    37     public static OutputAnalyzer keytool(List<String> options)
       
    38             throws Throwable {
       
    39 
       
    40         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("keytool")
       
    41                 .addVMArg("-Duser.language=en")
       
    42                 .addVMArg("-Duser.country=US");
       
    43         for (String option : options) {
       
    44             if (option.startsWith("-J")) {
       
    45                 launcher.addVMArg(option.substring(2));
       
    46             } else {
       
    47                 launcher.addToolArg(option);
       
    48             }
       
    49         }
       
    50         return ProcessTools.executeCommand(launcher.getCommand());
       
    51     }
       
    52 
       
    53     public static OutputAnalyzer keytool(String options) throws Throwable {
       
    54         return keytool(options.split("\\s+"));
       
    55     }
       
    56 
       
    57     public static OutputAnalyzer keytool(String... options) throws Throwable {
       
    58         return keytool(List.of(options));
       
    59     }
       
    60 
       
    61     // jarsigner
       
    62 
       
    63     public static OutputAnalyzer jarsigner(String jar, String alias,
       
    64             List<String> options) throws Throwable {
       
    65         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jarsigner")
       
    66                 .addVMArg("-Duser.language=en")
       
    67                 .addVMArg("-Duser.country=US");
       
    68         for (String option : options) {
       
    69             if (option.startsWith("-J")) {
       
    70                 launcher.addVMArg(option.substring(2));
       
    71             } else {
       
    72                 launcher.addToolArg(option);
       
    73             }
       
    74         }
       
    75         launcher.addToolArg(jar);
       
    76         if (alias != null) {
       
    77             launcher.addToolArg(alias);
       
    78         }
       
    79         return ProcessTools.executeCommand(launcher.getCommand());
       
    80     }
       
    81 
       
    82     public static OutputAnalyzer jarsigner(String jar, String alias,
       
    83             String options) throws Throwable {
       
    84 
       
    85         return jarsigner(jar, alias, options.split("\\s+"));
       
    86     }
       
    87 
       
    88     public static OutputAnalyzer jarsigner(String jar, String alias,
       
    89             String... options) throws Throwable {
       
    90 
       
    91         return jarsigner(jar, alias, List.of(options));
       
    92     }
       
    93 
       
    94     public static OutputAnalyzer sign(String jar, String alias, String... options)
       
    95             throws Throwable {
       
    96 
       
    97         return jarsigner(jar, alias,
       
    98                 mergeOptions("-J-Djava.security.egd=file:/dev/./urandom", options));
       
    99     }
       
   100 
       
   101     public static OutputAnalyzer verify(String jar, String... options)
       
   102             throws Throwable {
       
   103 
       
   104         return jarsigner(jar, NO_ALIAS, mergeOptions("-verify", options));
       
   105     }
       
   106 
       
   107     // helper methods
       
   108 
       
   109     private static List<String> mergeOptions(
       
   110             String firstOption, String... secondPart) {
       
   111 
       
   112         return mergeOptions(List.of(firstOption), secondPart);
       
   113     }
       
   114 
       
   115     private static List<String> mergeOptions(
       
   116             List<String> firstPart, String... secondPart) {
       
   117 
       
   118         List<String> options = new ArrayList<>(firstPart);
       
   119         Collections.addAll(options, secondPart);
       
   120         return options;
       
   121     }
       
   122 }