test/jdk/tools/jpackage/macosx/base/SigningBase.java
author herrick
Wed, 16 Oct 2019 10:32:08 -0400
branchJDK-8200758-branch
changeset 58648 3bf53ffa9ae7
parent 58464 d82489644b15
permissions -rw-r--r--
8232279 : Improve test helpers #2 Submitted-by: asemenyuk Reviewed-by: aherrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58464
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     1
/*
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     4
 *
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     8
 *
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    13
 * accompanied this code).
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    14
 *
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    18
 *
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    21
 * questions.
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    22
 */
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    23
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    24
import java.nio.file.Path;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    25
import java.util.List;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    26
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    27
import jdk.jpackage.test.TKit;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    28
import jdk.jpackage.test.Executor;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    29
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    30
public class SigningBase {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    31
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    32
    public static String DEV_NAME = "jpackage.openjdk.java.net";
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    33
    public static String APP_CERT
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    34
            = "Developer ID Application: " + DEV_NAME;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    35
    public static String INSTALLER_CERT
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    36
            = "Developer ID Installer: " + DEV_NAME;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    37
    public static String KEYCHAIN = "jpackagerTest.keychain";
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    38
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    39
    private static void checkString(List<String> result, String lookupString) {
58648
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58464
diff changeset
    40
        TKit.assertTextStream(lookupString).predicate(
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58464
diff changeset
    41
                (line, what) -> line.trim().equals(what)).apply(result.stream());
58464
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    42
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    43
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    44
    private static List<String> codesignResult(Path target, boolean signed) {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    45
        int exitCode = signed ? 0 : 1;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    46
        List<String> result = new Executor()
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    47
                .setExecutable("codesign")
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    48
                .addArguments("--verify", "--deep", "--strict", "--verbose=2",
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    49
                        target.toString())
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    50
                .saveOutput()
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    51
                .execute()
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    52
                .assertExitCodeIs(exitCode).getOutput();
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    53
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    54
        return result;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    55
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    56
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    57
    private static void verifyCodesignResult(List<String> result, Path target,
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    58
            boolean signed) {
58648
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58464
diff changeset
    59
        result.stream().forEachOrdered(TKit::trace);
58464
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    60
        if (signed) {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    61
            String lookupString = target.toString() + ": valid on disk";
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    62
            checkString(result, lookupString);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    63
            lookupString = target.toString() + ": satisfies its Designated Requirement";
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    64
            checkString(result, lookupString);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    65
        } else {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    66
            String lookupString = target.toString()
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    67
                    + ": code object is not signed at all";
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    68
            checkString(result, lookupString);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    69
        }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    70
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    71
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    72
    private static List<String> spctlResult(Path target, String type) {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    73
        List<String> result = new Executor()
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    74
                .setExecutable("/usr/sbin/spctl")
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    75
                .addArguments("-vvv", "--assess", "--type", type,
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    76
                        target.toString())
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    77
                .executeAndGetOutput();
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    78
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    79
        return result;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    80
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    81
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    82
    private static void verifySpctlResult(List<String> result, Path target, String type) {
58648
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58464
diff changeset
    83
        result.stream().forEachOrdered(TKit::trace);
58464
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    84
        String lookupString = target.toString() + ": accepted";
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    85
        checkString(result, lookupString);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    86
        lookupString = "source=" + DEV_NAME;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    87
        checkString(result, lookupString);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    88
        if (type.equals("install")) {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    89
            lookupString = "origin=" + INSTALLER_CERT;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    90
        } else {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    91
            lookupString = "origin=" + APP_CERT;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    92
        }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    93
        checkString(result, lookupString);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    94
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    95
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    96
    private static List<String> pkgutilResult(Path target) {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    97
        List<String> result = new Executor()
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    98
                .setExecutable("/usr/sbin/pkgutil")
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
    99
                .addArguments("--check-signature",
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   100
                        target.toString())
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   101
                .executeAndGetOutput();
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   102
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   103
        return result;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   104
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   105
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   106
    private static void verifyPkgutilResult(List<String> result) {
58648
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58464
diff changeset
   107
        result.stream().forEachOrdered(TKit::trace);
58464
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   108
        String lookupString = "Status: signed by a certificate trusted for current user";
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   109
        checkString(result, lookupString);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   110
        lookupString = "1. " + INSTALLER_CERT;
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   111
        checkString(result, lookupString);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   112
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   113
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   114
    public static void verifyCodesign(Path target, boolean signed) {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   115
        List<String> result = codesignResult(target, signed);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   116
        verifyCodesignResult(result, target, signed);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   117
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   118
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   119
    public static void verifySpctl(Path target, String type) {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   120
        List<String> result = spctlResult(target, type);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   121
        verifySpctlResult(result, target, type);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   122
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   123
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   124
    public static void verifyPkgutil(Path target) {
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   125
        List<String> result = pkgutilResult(target);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   126
        verifyPkgutilResult(result);
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   127
    }
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   128
d82489644b15 8215895: Verify and create tests for Mac installer specific signing options
herrick
parents:
diff changeset
   129
}