File: Test.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (279 lines) | stat: -rw-r--r-- 11,211 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;

/**
 * Base class.
 */
public abstract class Test {

    static final String TEST_SOURCES = System.getProperty("test.src", ".");
    static final String TEST_CLASSES = System.getProperty("test.classes");
    static final String FS = System.getProperty("file.separator");
    static final String JAVA_HOME = System.getProperty("java.home");
    static final String KEYTOOL = JAVA_HOME + FS + "bin" + FS + "keytool";
    static final String JARSIGNER = JAVA_HOME + FS + "bin" + FS + "jarsigner";
    static final String UNSIGNED_JARFILE = "unsigned.jar";
    static final String SIGNED_JARFILE = "signed.jar";
    static final String UPDATED_SIGNED_JARFILE = "updated_signed.jar";
    static final String FIRST_FILE = "first.txt";
    static final String SECOND_FILE = "second.txt";
    static final String PASSWORD = "password";
    static final String FIRST_KEY_KEYSTORE = "first_key.jks";
    static final String KEYSTORE = "keystore.jks";
    static final String FIRST_KEY_ALIAS = "first";
    static final String SECOND_KEY_ALIAS = "second";
    static final String KEY_ALG = "RSA";
    static final String KEY_ALIAS = "alias";
    static final String CERT_REQUEST_FILENAME = "test.req";
    static final String CERT_FILENAME = "test.crt";
    static final String CA_KEY_ALIAS = "ca";
    static final String CA2_KEY_ALIAS = "ca2";
    static final int KEY_SIZE = 2048;
    static final int TIMEOUT = 6 * 60 * 1000;   // in millis
    static final int VALIDITY = 365;

    static final String WARNING = "Warning:";
    static final String ERROR = "[Ee]rror:";
    static final String WARNING_OR_ERROR = "(" + WARNING + "|" + ERROR + ")";

    static final String CHAIN_NOT_VALIDATED_VERIFYING_WARNING
            = "This jar contains entries "
            + "whose certificate chain is invalid.";

    static final String CERTIFICATE_SELF_SIGNED
            = "The signer's certificate is self-signed.";

    static final String ALIAS_NOT_IN_STORE_VERIFYING_WARNING
            = "This jar contains signed entries "
            + "that are not signed by alias in this keystore.";

    static final String BAD_EXTENDED_KEY_USAGE_SIGNING_WARNING
            = "The signer certificate's ExtendedKeyUsage extension "
            + "doesn't allow code signing.";

    static final String BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING
            = "This jar contains entries whose signer certificate's "
            + "ExtendedKeyUsage extension doesn't allow code signing.";

    static final String BAD_KEY_USAGE_SIGNING_WARNING
            = "The signer certificate's KeyUsage extension "
            + "doesn't allow code signing.";

    static final String BAD_KEY_USAGE_VERIFYING_WARNING
            = "This jar contains entries whose signer certificate's KeyUsage "
            + "extension doesn't allow code signing.";

    static final String BAD_NETSCAPE_CERT_TYPE_SIGNING_WARNING
            = "The signer certificate's NetscapeCertType extension "
            + "doesn't allow code signing.";

    static final String BAD_NETSCAPE_CERT_TYPE_VERIFYING_WARNING
            = "This jar contains entries "
            + "whose signer certificate's NetscapeCertType extension "
            + "doesn't allow code signing.";

    static final String CHAIN_NOT_VALIDATED_SIGNING_WARNING
            = "The signer's certificate chain is invalid.";

    static final String HAS_EXPIRING_CERT_SIGNING_WARNING
            = "The signer certificate will expire within six months.";

    static final String HAS_EXPIRING_CERT_VERIFYING_WARNING
            = "This jar contains entries "
            + "whose signer certificate will expire within six months.";

    static final String HAS_EXPIRED_CERT_SIGNING_WARNING
            = "The signer certificate has expired.";

    static final String HAS_EXPIRED_CERT_VERIFYING_WARNING
            = "This jar contains entries whose signer certificate has expired.";

    static final String HAS_UNSIGNED_ENTRY_VERIFYING_WARNING
            = "This jar contains unsigned entries "
            + "which have not been integrity-checked.";

    static final String NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING
            = "This jar contains signed entries "
            + "which are not signed by the specified alias(es).";

    static final String NO_TIMESTAMP_SIGNING_WARN_TEMPLATE
            = "No -tsa or -tsacert is provided "
            + "and this jar is not timestamped. "
            + "Without a timestamp, users may not be able to validate this jar "
            + "after the signer certificate's expiration date "
            + "(%1$tY-%1$tm-%1$td).";

    static final String NO_TIMESTAMP_VERIFYING_WARN_TEMPLATE
            = "This jar contains signatures that do not include a timestamp. "
            + "Without a timestamp, users may not be able to validate this jar "
            + "after any of the signer certificates expire "
            + "(as early as %1$tY-%1$tm-%1$td).";

    static final String NOT_YET_VALID_CERT_SIGNING_WARNING
            = "The signer certificate is not yet valid.";

    static final String NOT_YET_VALID_CERT_VERIFYING_WARNING
            = "This jar contains entries "
            + "whose signer certificate is not yet valid.";

    static final String WEAK_ALGORITHM_WARNING
            = "algorithm is considered a security risk. "
            + "This algorithm will be disabled in a future update.";

    static final String WEAK_KEY_WARNING
            = "This key size will be disabled in a future update.";

    static final String JAR_SIGNED = "jar signed.";

    static final String JAR_VERIFIED = "jar verified.";

    static final String JAR_VERIFIED_WITH_SIGNER_ERRORS
            = "jar verified, with signer errors.";

    static final int CHAIN_NOT_VALIDATED_EXIT_CODE = 4;
    static final int HAS_EXPIRED_CERT_EXIT_CODE = 4;
    static final int BAD_KEY_USAGE_EXIT_CODE = 8;
    static final int BAD_EXTENDED_KEY_USAGE_EXIT_CODE = 8;
    static final int BAD_NETSCAPE_CERT_TYPE_EXIT_CODE = 8;
    static final int HAS_UNSIGNED_ENTRY_EXIT_CODE = 16;
    static final int ALIAS_NOT_IN_STORE_EXIT_CODE = 32;
    static final int NOT_SIGNED_BY_ALIAS_EXIT_CODE = 32;

    protected void createAlias(String alias, String ... options)
            throws Throwable {
        List<String> cmd = new ArrayList<>();
        cmd.addAll(List.of(
                "-genkeypair",
                "-alias", alias,
                "-keyalg", KEY_ALG,
                "-keysize", Integer.toString(KEY_SIZE),
                "-keystore", KEYSTORE,
                "-storepass", PASSWORD,
                "-keypass", PASSWORD,
                "-dname", "CN=" + alias));
        cmd.addAll(Arrays.asList(options));

        keytool(cmd.toArray(new String[cmd.size()]))
                .shouldHaveExitValue(0);
    }

    protected void issueCert(String alias, String ... options)
            throws Throwable {
        keytool("-certreq",
                "-alias", alias,
                "-keystore", KEYSTORE,
                "-storepass", PASSWORD,
                "-keypass", PASSWORD,
                "-file", alias + ".req")
                    .shouldHaveExitValue(0);

        List<String> cmd = new ArrayList<>();
        cmd.addAll(List.of(
                "-gencert",
                "-alias", CA_KEY_ALIAS,
                "-infile", alias + ".req",
                "-outfile", alias + ".cert",
                "-keystore", KEYSTORE,
                "-storepass", PASSWORD,
                "-keypass", PASSWORD,
                "-file", alias + ".req"));
        cmd.addAll(Arrays.asList(options));

        keytool(cmd.toArray(new String[cmd.size()]))
                .shouldHaveExitValue(0);

        keytool("-importcert",
                "-alias", alias,
                "-keystore", KEYSTORE,
                "-storepass", PASSWORD,
                "-keypass", PASSWORD,
                "-file", alias + ".cert")
                    .shouldHaveExitValue(0);
    }

    protected void checkVerifying(OutputAnalyzer analyzer, int expectedExitCode,
            String... warnings) {
        analyzer.shouldHaveExitValue(expectedExitCode);
        int count = 0;
        for (String warning : warnings) {
            if (warning.startsWith("!")) {
                analyzer.shouldNotContain(warning.substring(1));
            } else {
                count++;
                analyzer.shouldContain(warning);
            }
        }
        if (count > 0) {
            analyzer.shouldMatch(WARNING_OR_ERROR);
        }
        if (expectedExitCode == 0) {
            analyzer.shouldContain(JAR_VERIFIED);
        } else {
            analyzer.shouldContain(JAR_VERIFIED_WITH_SIGNER_ERRORS);
        }
    }

    protected void checkSigning(OutputAnalyzer analyzer, String... warnings) {
        analyzer.shouldHaveExitValue(0);
        int count = 0;
        for (String warning : warnings) {
            if (warning.startsWith("!")) {
                analyzer.shouldNotContain(warning.substring(1));
            } else {
                count++;
                analyzer.shouldContain(warning);
            }
        }
        if (count > 0) {
            analyzer.shouldMatch(WARNING_OR_ERROR);
        }
        analyzer.shouldContain(JAR_SIGNED);
    }

    protected OutputAnalyzer keytool(String... cmd) throws Throwable {
        return tool(KEYTOOL, cmd);
    }

    protected OutputAnalyzer jarsigner(String... cmd) throws Throwable {
        return tool(JARSIGNER, cmd);
    }

    private OutputAnalyzer tool(String tool, String... args) throws Throwable {
        List<String> cmd = new ArrayList<>();
        cmd.add(tool);
        cmd.add("-J-Duser.language=en");
        cmd.add("-J-Duser.country=US");
        cmd.addAll(Arrays.asList(args).stream().filter(arg -> {
            return arg != null && !arg.isEmpty();
        }).collect(Collectors.toList()));
        return ProcessTools.executeCommand(cmd.toArray(new String[cmd.size()]));
    }
}