/* * Copyright (c) 2014, 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.
*/
/* * @test * @bug 6996377 * @summary shrink duplicate code in the constructor of PKIXValidator * @modules java.base/sun.security.validator
*/
// COMMON-OPTS: All certs created with the following common options: // -keystore <STORE> -storepass <PASS> -keypass <PASS> -keyalg rsa // -keysize 2048 -validity 720 -sigalg sha256withrsa
for (Certificate cert : certSet) {
anchors.add(new TrustAnchor((X509Certificate)cert, null));
}
return anchors;
}
publicstaticvoid testCtorByCollection(Set<X509Certificate> certSet) throws Exception {
Validator valOK;
Validator valNoGood;
X509Certificate[] chain = new X509Certificate[1];
Set<X509Certificate> intermeds = new HashSet<>();
// Case 1: Make a PKIXValidator with valid arguments // Expected result: Well-formed PKIXValidator
System.out.println("Constructor test 1: Valid inputs");
valOK = Validator.getInstance(Validator.TYPE_PKIX,
Validator.VAR_GENERIC, certSet);
// Convert our user cert from PEM format, then do the same for // its intermediate signer and add that as a helper for path building
chain[0] = makeCertFromPEM(USER);
intermeds.add(makeCertFromPEM(INTERMED));
PKIXBuilderParameters pbParams = ((PKIXValidator)valOK).getParameters();
pbParams.setDate(new Date(1426399200000L)); // 03-15-2014 6:00:00 GMT
// See if we can build a trusted path to a root to make sure // everything still works as expected.
showValidatedChain(valOK, chain, intermeds);
// Case 2: Make a PKIXValidator with null anchor list. // Expected result: throw NullPointerException
System.out.println("Constructor test 2: null trustedCerts"); try {
valNoGood = Validator.getInstance(Validator.TYPE_PKIX,
Validator.VAR_GENERIC, (Collection<X509Certificate>)null); // Throw something non Runtime-related to indicate we shouldn't // have succeeded on construction. thrownew IOException( "Constructor did not throw NullPointerException");
} catch (NullPointerException npe) {
System.out.println("\tCaught Exception (" + npe.toString() + ") [PASS])");
}
// Case 3: Try putting a null reference into a populated TA List // Expected result: throw NullPointerException
System.out.println("Constructor test 3: null in trustedCerts list"); try {
certSet.add(null);
valNoGood = Validator.getInstance(Validator.TYPE_PKIX,
Validator.VAR_GENERIC, certSet); // Throw something non Runtime-related to indicate we shouldn't // have succeeded on construction. thrownew IOException("Constructor did not throw RuntimeException");
} catch (NullPointerException npe) {
System.out.println("\tCaught Exception (" + npe.toString() + ") [PASS])");
} finally { // Return the certSet list to its original state
certSet.remove(null);
}
// Case 4: Provide an empty List as the X509Certificate collection // Expected result: throw RuntimeException
System.out.println("Constructor test 4: empty trustedCerts list"); try {
valNoGood = Validator.getInstance(Validator.TYPE_PKIX,
Validator.VAR_GENERIC, new ArrayList<X509Certificate>()); // Throw something non Runtime-related to indicate we shouldn't // have succeeded on construction. thrownew IOException("Constructor did not throw RuntimeException");
} catch (RuntimeException re) {
System.out.println("\tCaught RuntimeException (" + re.toString() + ") [PASS])");
}
// Case 5: Provide an invalid variant // Expected result: successful construction. // Note: subsequent calls to validate may throw CertificateException // if the submitted chain has a length > 1.
System.out.println("Constructor test 5: Unsupported variant");
valNoGood = Validator.getInstance(Validator.TYPE_PKIX, "BogusVariant", certSet);
System.out.println("\tSuccessful construction [PASS]");
}
// Case 6: Make a PKIXValidator with valid arguments // Expected result: Well-formed PKIXValidator object
System.out.println("Constructor test 6: Valid inputs");
// Set up the PKIXBuilderParameters
X509CertSelector sel = new X509CertSelector();
sel.setSubject("CN=User");
PKIXBuilderParameters pbParams = new PKIXBuilderParameters(taSet, sel);
pbParams.setRevocationEnabled(false);
pbParams.setDate(new Date(1426399200000L)); // 03-15-2014 6:00:00 GMT
// Convert our user cert from PEM format, then do the same for // its intermediate signer and add that as a helper for path building
chain[0] = makeCertFromPEM(USER);
intermeds.add(makeCertFromPEM(INTERMED));
showValidatedChain(valOK, chain, intermeds);
// Case 7: Make a PKIXValidator but provide a null PKIXBuilderParameters // Expected result: throw NullPointerException
System.out.println("Constructor test 7: null params"); try {
valNoGood = Validator.getInstance(Validator.TYPE_PKIX,
Validator.VAR_GENERIC, (PKIXBuilderParameters)null); // Throw something non Runtime-related to indicate we shouldn't // have succeeded on construction. thrownew IOException( "Constructor did not throw NullPointerException");
} catch (NullPointerException npe) {
System.out.println("\tCaught RuntimeException (" + npe.toString() + ") [PASS])");
}
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.24Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.