Path: blob/master/test/jdk/java/security/Provider/GetInstance.java
41152 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4856968 7054918 813018126* @library ../testlibrary27* @summary make sure getInstance() works correctly, including failover28* and delayed provider selection for Signatures29* @author Andreas Sterbenz30*/3132import java.util.*;3334import java.security.*;35import java.security.cert.*;3637public class GetInstance {3839private static void same(Provider p1, Provider p2) throws Exception {40if (p1 != p2) {41throw new Exception("Wrong provider");42}43}4445public static void main(String[] args) throws Exception {46ProvidersSnapshot snapshot = ProvidersSnapshot.create();47try {48main0(args);49} finally {50snapshot.restore();51}52}5354public static void main0(String[] args) throws Exception {55long start = System.currentTimeMillis();5657Provider foo = new FooProvider();58Provider bar = new BarProvider();59Provider baz = new BazProvider();6061Security.addProvider(foo);62Security.addProvider(bar);63Security.addProvider(baz);6465System.out.println("Testing MessageDigest.getInstance()...");66MessageDigest m;67m = MessageDigest.getInstance("foo");68m = MessageDigest.getInstance("foo", "foo");69m = MessageDigest.getInstance("foo", foo);7071System.out.println("Testing Signature.getInstance() for SPI...");72Signature sig;73PrivateKey privateKey = new FooPrivateKey();74sig = Signature.getInstance("foo");75same(foo, sig.getProvider());76sig = Signature.getInstance("foo");77sig.initSign(privateKey);78same(foo, sig.getProvider());79sig = Signature.getInstance("foo", "foo");80sig.initSign(privateKey);81same(foo, sig.getProvider());82sig = Signature.getInstance("foo", foo);83sig.initSign(privateKey);84same(foo, sig.getProvider());8586System.out.println("Testing Signature.getInstance() for Signature...");87sig = Signature.getInstance("fuu");88same(foo, sig.getProvider());89sig = Signature.getInstance("fuu");90sig.initSign(privateKey);91same(foo, sig.getProvider());92sig = Signature.getInstance("fuu", "foo");93sig.initSign(privateKey);94same(foo, sig.getProvider());95sig = Signature.getInstance("fuu", foo);96sig.initSign(privateKey);97same(foo, sig.getProvider());9899System.out.println("Testing CertStore.getInstance()...");100CertStoreParameters params = new CollectionCertStoreParameters(Collections.EMPTY_LIST);101CertStore cs;102cs = CertStore.getInstance("foo", params);103cs = CertStore.getInstance("foo", params, "foo");104cs = CertStore.getInstance("foo", params, foo);105106System.out.println("Testing failover...");107m = MessageDigest.getInstance("bar");108same(m.getProvider(), baz);109sig = Signature.getInstance("bar");110same(sig.getProvider(), baz);111cs = CertStore.getInstance("bar", params);112same(cs.getProvider(), baz);113114System.out.println("Testing Signature delayed provider selection...");115sig = Signature.getInstance("baz");116sig.initVerify(new FooPublicKey());117same(sig.getProvider(), baz);118119Provider.Service s = foo.getService("CertStore", "foo");120s.newInstance(null);121s.newInstance(params);122try {123s.newInstance(0);124throw new Exception("call should not succeed");125} catch (NoSuchAlgorithmException e) {126e.printStackTrace();127Throwable cause = e.getCause();128if (cause instanceof InvalidParameterException == false) {129throw new Exception("incorrect exception");130}131}132133long stop = System.currentTimeMillis();134System.out.println("Done (" + (stop - start) + " ms).");135}136137public static class FooProvider extends Provider {138FooProvider() {139super("foo", "1.0", "none");140put("MessageDigest.foo", "GetInstance$FooDigest");141put("CertStore.foo", "GetInstance$FooStore");142put("Signature.foo", "GetInstance$FooSignatureSpi");143144put("Signature.fuu", "GetInstance$BazSignature");145146// throws InvalidKeyException, skipped in delayed provider selection147put("Signature.baz", "GetInstance$BazSignatureSpi");148}149}150151public static class BarProvider extends Provider {152BarProvider() {153super("bar", "1.0", "none");154// all entries invalid for failover155put("MessageDigest.bar", "GetInstance$FooKey");156put("Signature.bar", "GetInstance$FooKey");157put("Certstore.bar", "GetInstance$FooKey");158159// not an SPI, skipped in delayed provider selection160put("Signature.baz", "GetInstance$BazSignature");161}162}163164public static class BazProvider extends Provider {165BazProvider() {166super("baz", "1.0", "none");167put("MessageDigest.bar", "GetInstance$FooDigest");168put("CertStore.bar", "GetInstance$FooStore");169put("Signature.bar", "GetInstance$FooSignatureSpi");170171put("Signature.baz", "GetInstance$FooSignatureSpi");172}173}174175public static class FooDigest extends MessageDigestSpi {176public byte[] engineDigest() { return new byte[0]; }177public void engineReset() {}178public void engineUpdate(byte input) {}179public void engineUpdate(byte[] b, int ofs, int len) {}180}181182public static class FooStore extends CertStoreSpi {183public FooStore(CertStoreParameters params) throws InvalidAlgorithmParameterException { super(params); }184public Collection engineGetCertificates(CertSelector sel) { return Collections.EMPTY_LIST; }185public Collection engineGetCRLs(CRLSelector sel) { return Collections.EMPTY_LIST; }186}187188public static class BaseSignatureSpi extends SignatureSpi {189protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {190}191protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {192}193protected void engineUpdate(byte b) throws SignatureException { }194protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { }195protected byte[] engineSign() throws SignatureException {196return new byte[0];197}198protected boolean engineVerify(byte[] sigBytes) throws SignatureException {199return false;200}201protected void engineSetParameter(String param, Object value) throws InvalidParameterException {202}203protected Object engineGetParameter(String param) throws InvalidParameterException {204return null;205}206}207208public static class BaseSignature extends Signature {209BaseSignature(String s) {210super(s);211}212protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {213//214}215protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException { }216protected void engineUpdate(byte b) throws SignatureException { }217protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { }218protected byte[] engineSign() throws SignatureException {219return new byte[0];220}221protected boolean engineVerify(byte[] sigBytes) throws SignatureException {222return false;223}224protected void engineSetParameter(String param, Object value) throws InvalidParameterException {225}226protected Object engineGetParameter(String param) throws InvalidParameterException {227return null;228}229}230231public static abstract class FooKey implements Key {232public String getFormat() { return null; }233public byte[] getEncoded() { return null; }234public String getAlgorithm() { return "foo"; }235}236237public static class FooPrivateKey extends FooKey implements PrivateKey { }238239public static class FooPublicKey extends FooKey implements PublicKey { }240241public static class FooSignatureSpi extends BaseSignatureSpi {242public FooSignatureSpi() {243super();244System.out.println("FooSignatureSpi constructor");245}246}247248public static class BazSignatureSpi extends BaseSignatureSpi {249public BazSignatureSpi() {250super();251System.out.println("BazSignatureSpi constructor");252}253protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {254throw new InvalidKeyException("verify not supported");255}256}257258public static class BazSignature extends BaseSignature {259public BazSignature() {260super("baz");261System.out.println("BazSignature constructor");262}263}264265}266267268