Java Code Examples for jdk.testlibrary.ProcessTools#executeCommand()

The following examples show how to use jdk.testlibrary.ProcessTools#executeCommand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static OutputAnalyzer executeKeytoolCommand(String[] command,
        int exitCode) {
    String[] keytoolCmd = new String[command.length + 1];
    OutputAnalyzer output = null;
    try {
        keytoolCmd[0] = JDKToolFinder.getJDKTool(KEYTOOL);
        System.arraycopy(command, 0, keytoolCmd, 1, command.length);
        output = ProcessTools.executeCommand(keytoolCmd);
        output.shouldHaveExitValue(exitCode);
        out.println("Executed keytool command sucessfully:"
                + Arrays.toString(keytoolCmd));
    } catch (Throwable e) {
        e.printStackTrace(System.err);
        throw new RuntimeException("Keytool Command execution failed : "
                + Arrays.toString(keytoolCmd), e);
    }
    return output;
}
 
Example 2
Source File: KinitConfPlusProps.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean checkTicketFlags() {
    String[] command = new String[] {KLIST, "-f", "-c", CC_FILENAME};

    try {
        OutputAnalyzer out = ProcessTools.executeCommand(command);
        out.shouldHaveExitValue(0);
        out.shouldContain("FORWARDABLE");
        out.shouldContain("PROXIABLE");
    } catch(Throwable e) {
        System.out.println("Unexpected exception: " + e);
        e.printStackTrace(System.out);
        return false;
    }

    return true;
}
 
Example 3
Source File: Utils.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static OutputAnalyzer executeKeytoolCommand(String[] command,
        int exitCode) {
    String[] keytoolCmd = new String[command.length + 1];
    OutputAnalyzer output = null;
    try {
        keytoolCmd[0] = JDKToolFinder.getJDKTool(KEYTOOL);
        System.arraycopy(command, 0, keytoolCmd, 1, command.length);
        output = ProcessTools.executeCommand(keytoolCmd);
        output.shouldHaveExitValue(exitCode);
        out.println("Executed keytool command sucessfully:"
                + Arrays.toString(keytoolCmd));
    } catch (Throwable e) {
        e.printStackTrace(System.err);
        throw new RuntimeException("Keytool Command execution failed : "
                + Arrays.toString(keytoolCmd), e);
    }
    return output;
}
 
Example 4
Source File: Utils.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static OutputAnalyzer executeKeytoolCommand(String[] command,
        int exitCode) {
    String[] keytoolCmd = new String[command.length + 1];
    OutputAnalyzer output = null;
    try {
        keytoolCmd[0] = JDKToolFinder.getJDKTool(KEYTOOL);
        System.arraycopy(command, 0, keytoolCmd, 1, command.length);
        output = ProcessTools.executeCommand(keytoolCmd);
        output.shouldHaveExitValue(exitCode);
        out.println("Executed keytool command sucessfully:"
                + Arrays.toString(keytoolCmd));
    } catch (Throwable e) {
        e.printStackTrace(System.err);
        throw new RuntimeException("Keytool Command execution failed : "
                + Arrays.toString(keytoolCmd), e);
    }
    return output;
}
 
Example 5
Source File: Utils.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static OutputAnalyzer executeKeytoolCommand(String[] command,
        int exitCode) {
    String[] keytoolCmd = new String[command.length + 1];
    OutputAnalyzer output = null;
    try {
        keytoolCmd[0] = JDKToolFinder.getJDKTool(KEYTOOL);
        System.arraycopy(command, 0, keytoolCmd, 1, command.length);
        output = ProcessTools.executeCommand(keytoolCmd);
        output.shouldHaveExitValue(exitCode);
        out.println("Executed keytool command sucessfully:"
                + Arrays.toString(keytoolCmd));
    } catch (Throwable e) {
        e.printStackTrace(System.err);
        throw new RuntimeException("Keytool Command execution failed : "
                + Arrays.toString(keytoolCmd), e);
    }
    return output;
}
 
Example 6
Source File: CompilerUtils.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compile all the java sources in {@code <source>/**} to
 * {@code <destination>/**}. The destination directory will be created if
 * it doesn't exist.
 *
 * All warnings/errors emitted by the compiler are output to System.out/err.
 *
 * @return true if the compilation is successful
 *
 * @throws IOException
 *         if there is an I/O error scanning the source tree or
 *         creating the destination directory
 * @throws UnsupportedOperationException
 *         if there is no system java compiler
 */
public static boolean compile(Path source, Path destination, String ... options)
    throws IOException
{
    List<Path> sources
        = Files.find(source, Integer.MAX_VALUE,
            (file, attrs) -> (file.toString().endsWith(".java")))
            .collect(Collectors.toList());

    Files.createDirectories(destination);
    List<String> opts = Arrays.asList(options);
    List<String> compileargs = new ArrayList<String> ();
    compileargs.add(JAVAC);
    compileargs.add("-d");
    compileargs.add(destination.toString());
    for(String opt: opts) {
        compileargs.add(opt);
    }
    for(Path p: sources) {
        compileargs.add(p.toString());
    }

    OutputAnalyzer output = null;
    try {
        String[] sarr = compileargs.toArray(new String[0]);
        output = ProcessTools.executeCommand(sarr);
        output.shouldHaveExitValue(0);
    } catch (Throwable t) {
        throw new RuntimeException("Javac failed", t);
    }

    return true;
}
 
Example 7
Source File: CompilerUtils.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compile all the java sources in {@code <source>/**} to
 * {@code <destination>/**}. The destination directory will be created if
 * it doesn't exist.
 *
 * All warnings/errors emitted by the compiler are output to System.out/err.
 *
 * @return true if the compilation is successful
 *
 * @throws IOException
 *         if there is an I/O error scanning the source tree or
 *         creating the destination directory
 * @throws UnsupportedOperationException
 *         if there is no system java compiler
 */
public static boolean compile(Path source, Path destination, String ... options)
    throws IOException
{
    List<Path> sources
        = Files.find(source, Integer.MAX_VALUE,
            (file, attrs) -> (file.toString().endsWith(".java")))
            .collect(Collectors.toList());

    Files.createDirectories(destination);
    List<String> opts = Arrays.asList(options);
    List<String> compileargs = new ArrayList<String> ();
    compileargs.add(JAVAC);
    compileargs.add("-d");
    compileargs.add(destination.toString());
    for(String opt: opts) {
        compileargs.add(opt);
    }
    for(Path p: sources) {
        compileargs.add(p.toString());
    }

    OutputAnalyzer output = null;
    try {
        String[] sarr = compileargs.toArray(new String[0]);
        output = ProcessTools.executeCommand(sarr);
        output.shouldHaveExitValue(0);
    } catch (Throwable t) {
        throw new RuntimeException("Javac failed", t);
    }

    return true;
}
 
Example 8
Source File: HasUnsignedEntryTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    System.out.println(String.format("Create a %s that contains %s",
            UNSIGNED_JARFILE, FIRST_FILE));
    Utils.createFiles(FIRST_FILE, SECOND_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create key pair for signing
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=Test",
            "-validity", Integer.toString(VALIDITY)).shouldHaveExitValue(0);

    // sign jar
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    checkSigning(analyzer);

    System.out.println(String.format("Copy %s to %s, and add %s.class, "
            + "so it contains unsigned entry",
            new Object[]{SIGNED_JARFILE, UPDATED_SIGNED_JARFILE,
                SECOND_FILE}));

    JarUtils.updateJar(SIGNED_JARFILE, UPDATED_SIGNED_JARFILE, SECOND_FILE);

    // verify jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            UPDATED_SIGNED_JARFILE);

    checkVerifying(analyzer, 0, HAS_UNSIGNED_ENTRY_VERIFYING_WARNING);

    // verify jar in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            UPDATED_SIGNED_JARFILE);

    checkVerifying(analyzer, HAS_UNSIGNED_ENTRY_EXIT_CODE,
            HAS_UNSIGNED_ENTRY_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 9
Source File: NoTimestampTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    String timezone = System.getProperty("user.timezone");
    System.out.println(String.format("Timezone = %s", timezone));

    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // calculate certificate expiration date
    Date expirationDate = new Date(System.currentTimeMillis() + VALIDITY
            * 24 * 60 * 60 * 1000L);

    // create key pair
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=Test",
            "-validity", Integer.toString(VALIDITY));

    // sign jar file
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-J-Duser.timezone=" + timezone,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    String warning = String.format(NO_TIMESTAMP_SIGNING_WARN_TEMPLATE,
            expirationDate);
    checkSigning(analyzer, warning);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-J-Duser.timezone=" + timezone,
            "-verify",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            KEY_ALIAS);

    warning = String.format(NO_TIMESTAMP_VERIFYING_WARN_TEMPLATE, expirationDate);
    checkVerifying(analyzer, 0, warning);

    // verify signed jar in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-J-Duser.timezone=" + timezone,
            "-verify",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            KEY_ALIAS);

    checkVerifying(analyzer, 0, warning);

    System.out.println("Test passed");
}
 
Example 10
Source File: NotSignedByAliasTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected void start() throws Throwable {
    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create first key pair for signing
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", FIRST_KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=First",
            "-validity", Integer.toString(VALIDITY)).shouldHaveExitValue(0);

    // create first key pair for signing
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", SECOND_KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=Second",
            "-validity", Integer.toString(VALIDITY)).shouldHaveExitValue(0);

    // sign jar with first key
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            FIRST_KEY_ALIAS);

    checkSigning(analyzer);

    // verify jar with second key
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            SECOND_KEY_ALIAS);

    checkVerifying(analyzer, 0, NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING);

    // verify jar with second key in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            SECOND_KEY_ALIAS);

    checkVerifying(analyzer, NOT_SIGNED_BY_ALIAS_EXIT_CODE,
            NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING);

    // verify jar with non-existing alias
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            "bogus");

    checkVerifying(analyzer, 0, NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING);

    // verify jar with non-existing alias in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            "bogus");

    checkVerifying(analyzer, NOT_SIGNED_BY_ALIAS_EXIT_CODE,
            NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 11
Source File: HasExpiredCertTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create key pair for jar signing
    createAlias(CA_KEY_ALIAS, "-ext", "bc:c");
    createAlias(KEY_ALIAS);

    issueCert(
            KEY_ALIAS,
            "-startdate", "-" + SHORT_VALIDITY * 2 + "d",
            "-validity", Integer.toString(SHORT_VALIDITY));

    // sign jar
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    checkSigning(analyzer, HAS_EXPIRED_CERT_SIGNING_WARNING);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, 0, HAS_EXPIRED_CERT_VERIFYING_WARNING);

    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, HAS_EXPIRED_CERT_EXIT_CODE,
            HAS_EXPIRED_CERT_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 12
Source File: NotYetValidCertTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected void start() throws Throwable {
    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create certificate that will be valid only tomorrow
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=Test",
            "-startdate", "+1d",
            "-validity", Integer.toString(VALIDITY));

    // sign jar
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    checkSigning(analyzer, NOT_YET_VALID_CERT_SIGNING_WARNING);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            KEY_ALIAS);

    checkVerifying(analyzer, 0, NOT_YET_VALID_CERT_VERIFYING_WARNING);

    // verify jar in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            KEY_ALIAS);

    checkVerifying(analyzer, HAS_EXPIRED_CERT_EXIT_CODE,
            NOT_YET_VALID_CERT_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 13
Source File: NoTimestampTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    String timezone = System.getProperty("user.timezone");
    System.out.println(String.format("Timezone = %s", timezone));

    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // calculate certificate expiration date
    Date expirationDate = new Date(System.currentTimeMillis() + VALIDITY
            * 24 * 60 * 60 * 1000L);

    // create key pair
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=Test",
            "-validity", Integer.toString(VALIDITY));

    // sign jar file
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-J-Duser.timezone=" + timezone,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    String warning = String.format(NO_TIMESTAMP_SIGNING_WARN_TEMPLATE,
            expirationDate);
    checkSigning(analyzer, warning);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-J-Duser.timezone=" + timezone,
            "-verify",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            KEY_ALIAS);

    warning = String.format(NO_TIMESTAMP_VERIFYING_WARN_TEMPLATE, expirationDate);
    checkVerifying(analyzer, 0, warning);

    // verify signed jar in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-J-Duser.timezone=" + timezone,
            "-verify",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            KEY_ALIAS);

    checkVerifying(analyzer, 0, warning);

    System.out.println("Test passed");
}
 
Example 14
Source File: AliasNotInStoreTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    Utils.createFiles(FIRST_FILE, SECOND_FILE);
    System.out.println(String.format("Create a %s that contains %s",
            new Object[]{UNSIGNED_JARFILE, FIRST_FILE}));
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create first key pair for signing
    createAlias(FIRST_KEY_ALIAS);
    createAlias(SECOND_KEY_ALIAS);

    // sign jar with first key
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            FIRST_KEY_ALIAS);

    checkSigning(analyzer);

    System.out.println(String.format("Copy %s to %s, and add %s",
            new Object[] {SIGNED_JARFILE, UPDATED_SIGNED_JARFILE,
                SECOND_FILE}));

    JarUtils.updateJar(SIGNED_JARFILE, UPDATED_SIGNED_JARFILE, SECOND_FILE);

    // sign jar with second key
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            UPDATED_SIGNED_JARFILE,
            SECOND_KEY_ALIAS);

    checkSigning(analyzer);

    // create keystore that contains only first key
    ProcessTools.executeCommand(KEYTOOL,
            "-importkeystore",
            "-srckeystore", KEYSTORE,
            "-srcalias", FIRST_KEY_ALIAS,
            "-srcstorepass", PASSWORD,
            "-srckeypass", PASSWORD,
            "-destkeystore", FIRST_KEY_KEYSTORE,
            "-destalias", FIRST_KEY_ALIAS,
            "-deststorepass", PASSWORD,
            "-destkeypass", PASSWORD).shouldHaveExitValue(0);

    // verify jar with keystore that contains only first key,
    // so there is signed entry (FirstClass.class) that is not signed
    // by any alias in the keystore
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", FIRST_KEY_KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            UPDATED_SIGNED_JARFILE);

    checkVerifying(analyzer, 0, CHAIN_NOT_VALIDATED_VERIFYING_WARNING,
            ALIAS_NOT_IN_STORE_VERIFYING_WARNING);

    // verify jar with keystore that contains only first key in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-strict",
            "-keystore", FIRST_KEY_KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            UPDATED_SIGNED_JARFILE);

    int expectedExitCode = ALIAS_NOT_IN_STORE_EXIT_CODE
            + CHAIN_NOT_VALIDATED_EXIT_CODE;
    checkVerifying(analyzer, expectedExitCode,
            CHAIN_NOT_VALIDATED_VERIFYING_WARNING,
            ALIAS_NOT_IN_STORE_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 15
Source File: BadKeyUsageTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create a certificate whose signer certificate's KeyUsage extension
    // doesn't allow code signing
    createAlias(CA_KEY_ALIAS, "-ext", "bc:c");
    createAlias(KEY_ALIAS);

    issueCert(
            KEY_ALIAS,
            "-ext", "KeyUsage=keyAgreement",
            "-validity", Integer.toString(VALIDITY));

    // sign jar
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    checkSigning(analyzer, BAD_KEY_USAGE_SIGNING_WARNING);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, 0, BAD_KEY_USAGE_VERIFYING_WARNING);

    // verify signed jar in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, BAD_KEY_USAGE_EXIT_CODE,
            BAD_KEY_USAGE_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 16
Source File: HasExpiredCertTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create key pair for jar signing
    createAlias(CA_KEY_ALIAS, "-ext", "bc:c");
    createAlias(KEY_ALIAS);

    issueCert(
            KEY_ALIAS,
            "-startdate", "-" + SHORT_VALIDITY * 2 + "d",
            "-validity", Integer.toString(SHORT_VALIDITY));

    // sign jar
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    checkSigning(analyzer, HAS_EXPIRED_CERT_SIGNING_WARNING);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, 0, HAS_EXPIRED_CERT_VERIFYING_WARNING);

    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, HAS_EXPIRED_CERT_EXIT_CODE,
            HAS_EXPIRED_CERT_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 17
Source File: AliasNotInStoreTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    Utils.createFiles(FIRST_FILE, SECOND_FILE);
    System.out.println(String.format("Create a %s that contains %s",
            new Object[]{UNSIGNED_JARFILE, FIRST_FILE}));
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create first key pair for signing
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", FIRST_KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", BOTH_KEYS_KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=First",
            "-validity", Integer.toString(VALIDITY)).shouldHaveExitValue(0);

    // create second key pair for signing
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", SECOND_KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", BOTH_KEYS_KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=Second",
            "-validity", Integer.toString(VALIDITY)).shouldHaveExitValue(0);

    // sign jar with first key
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", BOTH_KEYS_KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            FIRST_KEY_ALIAS);

    checkSigning(analyzer);

    System.out.println(String.format("Copy %s to %s, and add %s",
            new Object[] {SIGNED_JARFILE, UPDATED_SIGNED_JARFILE,
                SECOND_FILE}));

    JarUtils.updateJar(SIGNED_JARFILE, UPDATED_SIGNED_JARFILE, SECOND_FILE);

    // sign jar with second key
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", BOTH_KEYS_KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            UPDATED_SIGNED_JARFILE,
            SECOND_KEY_ALIAS);

    checkSigning(analyzer);

    // create keystore that contains only first key
    ProcessTools.executeCommand(KEYTOOL,
            "-importkeystore",
            "-srckeystore", BOTH_KEYS_KEYSTORE,
            "-srcalias", FIRST_KEY_ALIAS,
            "-srcstorepass", PASSWORD,
            "-srckeypass", PASSWORD,
            "-destkeystore", FIRST_KEY_KEYSTORE,
            "-destalias", FIRST_KEY_ALIAS,
            "-deststorepass", PASSWORD,
            "-destkeypass", PASSWORD).shouldHaveExitValue(0);

    // verify jar with keystore that contains only first key in strict mode,
    // so there is signed entry (FirstClass.class) that is not signed
    // by any alias in the keystore
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", FIRST_KEY_KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            UPDATED_SIGNED_JARFILE);

    checkVerifying(analyzer, 0, CHAIN_NOT_VALIDATED_VERIFYING_WARNING,
            ALIAS_NOT_IN_STORE_VERIFYING_WARNING);

    // verify jar with keystore that contains only first key in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-strict",
            "-keystore", FIRST_KEY_KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            UPDATED_SIGNED_JARFILE);

    int expectedExitCode = ALIAS_NOT_IN_STORE_EXIT_CODE
            + CHAIN_NOT_VALIDATED_EXIT_CODE;
    checkVerifying(analyzer, expectedExitCode,
            CHAIN_NOT_VALIDATED_VERIFYING_WARNING,
            ALIAS_NOT_IN_STORE_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 18
Source File: BadExtendedKeyUsageTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create a certificate whose signer certificate's
    // ExtendedKeyUsage extension doesn't allow code signing
    // create key pair for jar signing
    createAlias(CA_KEY_ALIAS, "-ext", "bc:c");
    createAlias(KEY_ALIAS);

    issueCert(
            KEY_ALIAS,
            "-ext", "ExtendedkeyUsage=serverAuth",
            "-validity", Integer.toString(VALIDITY));

    // sign jar
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    checkSigning(analyzer, BAD_EXTENDED_KEY_USAGE_SIGNING_WARNING);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, 0, BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING);

    // verity signed jar in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, BAD_EXTENDED_KEY_USAGE_EXIT_CODE,
            BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 19
Source File: NotYetValidCertTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected void start() throws Throwable {
    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create certificate that will be valid only tomorrow
    createAlias(CA_KEY_ALIAS, "-ext", "bc:c");
    createAlias(KEY_ALIAS);

    issueCert(
            KEY_ALIAS,
            "-startdate", "+1d",
            "-validity", Integer.toString(VALIDITY));

    // sign jar
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    checkSigning(analyzer, NOT_YET_VALID_CERT_SIGNING_WARNING);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            KEY_ALIAS);

    checkVerifying(analyzer, 0, NOT_YET_VALID_CERT_VERIFYING_WARNING);

    // verify jar in strict mode
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE,
            KEY_ALIAS);

    checkVerifying(analyzer, HAS_EXPIRED_CERT_EXIT_CODE,
            NOT_YET_VALID_CERT_VERIFYING_WARNING);

    System.out.println("Test passed");
}
 
Example 20
Source File: HasExpiredCertTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void start() throws Throwable {
    // create a jar file that contains one class file
    Utils.createFiles(FIRST_FILE);
    JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);

    // create key pair for jar signing
    ProcessTools.executeCommand(KEYTOOL,
            "-genkey",
            "-alias", KEY_ALIAS,
            "-keyalg", KEY_ALG,
            "-keysize", Integer.toString(KEY_SIZE),
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-dname", "CN=Test",
            "-startdate", "-" + SHORT_VALIDITY * 2 + "d",
            "-validity", Integer.toString(SHORT_VALIDITY))
            .shouldHaveExitValue(0);

    // sign jar
    OutputAnalyzer analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            "-signedjar", SIGNED_JARFILE,
            UNSIGNED_JARFILE,
            KEY_ALIAS);

    checkSigning(analyzer, HAS_EXPIRED_CERT_SIGNING_WARNING);

    // verify signed jar
    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-verbose",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, 0, HAS_EXPIRED_CERT_VERIFYING_WARNING);

    analyzer = ProcessTools.executeCommand(JARSIGNER,
            "-verify",
            "-strict",
            "-keystore", KEYSTORE,
            "-storepass", PASSWORD,
            "-keypass", PASSWORD,
            SIGNED_JARFILE);

    checkVerifying(analyzer, HAS_EXPIRED_CERT_EXIT_CODE,
            HAS_EXPIRED_CERT_VERIFYING_WARNING);

    System.out.println("Test passed");
}