1/* 2 * Copyright (c) 2013, 2015, 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 24import jdk.testlibrary.OutputAnalyzer; 25import jdk.testlibrary.ProcessTools; 26 27import java.util.ArrayList; 28import java.util.Arrays; 29import java.util.List; 30 31/** 32 * Base class. 33 */ 34public abstract class Test { 35 36 static final String TEST_SOURCES = System.getProperty("test.src", "."); 37 static final String TEST_CLASSES = System.getProperty("test.classes"); 38 static final String FS = System.getProperty("file.separator"); 39 static final String JAVA_HOME = System.getProperty("java.home"); 40 static final String KEYTOOL = JAVA_HOME + FS + "bin" + FS + "keytool"; 41 static final String JARSIGNER = JAVA_HOME + FS + "bin" + FS + "jarsigner"; 42 static final String UNSIGNED_JARFILE = "unsigned.jar"; 43 static final String SIGNED_JARFILE = "signed.jar"; 44 static final String UPDATED_SIGNED_JARFILE = "updated_signed.jar"; 45 static final String FIRST_FILE = "first.txt"; 46 static final String SECOND_FILE = "second.txt"; 47 static final String PASSWORD = "password"; 48 static final String BOTH_KEYS_KEYSTORE = "both_keys.jks"; 49 static final String FIRST_KEY_KEYSTORE = "first_key.jks"; 50 static final String KEYSTORE = "keystore.jks"; 51 static final String FIRST_KEY_ALIAS = "first"; 52 static final String SECOND_KEY_ALIAS = "second"; 53 static final String KEY_ALG = "RSA"; 54 static final String KEY_ALIAS = "alias"; 55 static final String CERT_REQUEST_FILENAME = "test.req"; 56 static final String CERT_FILENAME = "test.crt"; 57 static final String CA_KEY_ALIAS = "ca"; 58 static final int KEY_SIZE = 2048; 59 static final int TIMEOUT = 6 * 60 * 1000; // in millis 60 static final int VALIDITY = 365; 61 62 static final String WARNING = "Warning:"; 63 64 static final String CHAIN_NOT_VALIDATED_VERIFYING_WARNING 65 = "This jar contains entries " 66 + "whose certificate chain is not validated."; 67 68 static final String ALIAS_NOT_IN_STORE_VERIFYING_WARNING 69 = "This jar contains signed entries " 70 + "that are not signed by alias in this keystore."; 71 72 static final String BAD_EXTENDED_KEY_USAGE_SIGNING_WARNING 73 = "The signer certificate's ExtendedKeyUsage extension " 74 + "doesn't allow code signing."; 75 76 static final String BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING 77 = "This jar contains entries whose signer certificate's " 78 + "ExtendedKeyUsage extension doesn't allow code signing."; 79 80 static final String BAD_KEY_USAGE_SIGNING_WARNING 81 = "The signer certificate's KeyUsage extension " 82 + "doesn't allow code signing."; 83 84 static final String BAD_KEY_USAGE_VERIFYING_WARNING 85 = "This jar contains entries whose signer certificate's KeyUsage " 86 + "extension doesn't allow code signing."; 87 88 static final String BAD_NETSCAPE_CERT_TYPE_SIGNING_WARNING 89 = "The signer certificate's NetscapeCertType extension " 90 + "doesn't allow code signing."; 91 92 static final String BAD_NETSCAPE_CERT_TYPE_VERIFYING_WARNING 93 = "This jar contains entries " 94 + "whose signer certificate's NetscapeCertType extension " 95 + "doesn't allow code signing."; 96 97 static final String CHAIN_NOT_VALIDATED_SIGNING_WARNING 98 = "The signer's certificate chain is not validated."; 99 100 static final String HAS_EXPIRING_CERT_SIGNING_WARNING 101 = "The signer certificate will expire within six months."; 102 103 static final String HAS_EXPIRING_CERT_VERIFYING_WARNING 104 = "This jar contains entries " 105 + "whose signer certificate will expire within six months."; 106 107 static final String HAS_EXPIRED_CERT_SIGNING_WARNING 108 = "The signer certificate has expired."; 109 110 static final String HAS_EXPIRED_CERT_VERIFYING_WARNING 111 = "This jar contains entries whose signer certificate has expired."; 112 113 static final String HAS_UNSIGNED_ENTRY_VERIFYING_WARNING 114 = "This jar contains unsigned entries " 115 + "which have not been integrity-checked."; 116 117 static final String NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING 118 = "This jar contains signed entries " 119 + "which are not signed by the specified alias(es)."; 120 121 static final String NO_TIMESTAMP_SIGNING_WARN_TEMPLATE 122 = "No -tsa or -tsacert is provided " 123 + "and this jar is not timestamped. " 124 + "Without a timestamp, users may not be able to validate this jar " 125 + "after the signer certificate's expiration date " 126 + "(%1$tY-%1$tm-%1$td)."; 127 128 static final String NO_TIMESTAMP_VERIFYING_WARN_TEMPLATE 129 = "This jar contains signatures that do not include a timestamp. " 130 + "Without a timestamp, users may not be able to validate this jar " 131 + "after any of the signer certificates expire " 132 + "(as early as %1$tY-%1$tm-%1$td)."; 133 134 static final String NOT_YET_VALID_CERT_SIGNING_WARNING 135 = "The signer certificate is not yet valid."; 136 137 static final String NOT_YET_VALID_CERT_VERIFYING_WARNING 138 = "This jar contains entries " 139 + "whose signer certificate is not yet valid."; 140 141 static final String JAR_SIGNED = "jar signed."; 142 143 static final String JAR_VERIFIED = "jar verified."; 144 145 static final String JAR_VERIFIED_WITH_SIGNER_ERRORS 146 = "jar verified, with signer errors."; 147 148 static final int CHAIN_NOT_VALIDATED_EXIT_CODE = 4; 149 static final int HAS_EXPIRED_CERT_EXIT_CODE = 4; 150 static final int BAD_KEY_USAGE_EXIT_CODE = 8; 151 static final int BAD_EXTENDED_KEY_USAGE_EXIT_CODE = 8; 152 static final int BAD_NETSCAPE_CERT_TYPE_EXIT_CODE = 8; 153 static final int HAS_UNSIGNED_ENTRY_EXIT_CODE = 16; 154 static final int ALIAS_NOT_IN_STORE_EXIT_CODE = 32; 155 static final int NOT_SIGNED_BY_ALIAS_EXIT_CODE = 32; 156 157 protected void checkVerifying(OutputAnalyzer analyzer, int expectedExitCode, 158 String... warnings) { 159 analyzer.shouldHaveExitValue(expectedExitCode); 160 for (String warning : warnings) { 161 analyzer.shouldContain(warning); 162 } 163 if (warnings.length > 0) { 164 analyzer.shouldContain(WARNING); 165 } 166 if (expectedExitCode == 0) { 167 analyzer.shouldContain(JAR_VERIFIED); 168 } else { 169 analyzer.shouldContain(JAR_VERIFIED_WITH_SIGNER_ERRORS); 170 } 171 } 172 173 protected void checkSigning(OutputAnalyzer analyzer, String... warnings) { 174 analyzer.shouldHaveExitValue(0); 175 for (String warning : warnings) { 176 analyzer.shouldContain(warning); 177 } 178 if (warnings.length > 0) { 179 analyzer.shouldContain(WARNING); 180 } 181 analyzer.shouldContain(JAR_SIGNED); 182 } 183 184 protected OutputAnalyzer keytool(String... cmd) throws Throwable { 185 return tool(KEYTOOL, cmd); 186 } 187 188 protected OutputAnalyzer jarsigner(String... cmd) throws Throwable { 189 return tool(JARSIGNER, cmd); 190 } 191 192 private OutputAnalyzer tool(String tool, String... args) throws Throwable { 193 List<String> cmd = new ArrayList<>(); 194 cmd.add(tool); 195 cmd.add("-J-Duser.language=en"); 196 cmd.add("-J-Duser.country=US"); 197 cmd.addAll(Arrays.asList(args)); 198 return ProcessTools.executeCommand(cmd.toArray(new String[cmd.size()])); 199 } 200} 201