0N/A/*
5359N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
5359N/A * @bug 4856968 7054918
5359N/A * @library ../testlibrary
0N/A * @summary make sure getInstance() works correctly, including failover
0N/A * and delayed provider selection for Signatures
0N/A * @author Andreas Sterbenz
0N/A */
0N/A
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.cert.*;
0N/A
0N/Apublic class GetInstance {
0N/A
0N/A private static void same(Provider p1, Provider p2) throws Exception {
0N/A if (p1 != p2) {
0N/A throw new Exception("Wrong provider");
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
5359N/A ProvidersSnapshot snapshot = ProvidersSnapshot.create();
5359N/A try {
5359N/A main0(args);
5359N/A } finally {
5359N/A snapshot.restore();
5359N/A }
5359N/A }
5359N/A
5359N/A public static void main0(String[] args) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A
0N/A Provider foo = new FooProvider();
0N/A Provider bar = new BarProvider();
0N/A Provider baz = new BazProvider();
0N/A
0N/A Security.addProvider(foo);
0N/A Security.addProvider(bar);
0N/A Security.addProvider(baz);
0N/A
0N/A System.out.println("Testing MessageDigest.getInstance()...");
0N/A MessageDigest m;
0N/A m = MessageDigest.getInstance("foo");
0N/A m = MessageDigest.getInstance("foo", "foo");
0N/A m = MessageDigest.getInstance("foo", foo);
0N/A
0N/A System.out.println("Testing Signature.getInstance() for SPI...");
0N/A Signature sig;
0N/A PrivateKey privateKey = new FooPrivateKey();
0N/A sig = Signature.getInstance("foo");
0N/A same(foo, sig.getProvider());
0N/A sig = Signature.getInstance("foo");
0N/A sig.initSign(privateKey);
0N/A same(foo, sig.getProvider());
0N/A sig = Signature.getInstance("foo", "foo");
0N/A sig.initSign(privateKey);
0N/A same(foo, sig.getProvider());
0N/A sig = Signature.getInstance("foo", foo);
0N/A sig.initSign(privateKey);
0N/A same(foo, sig.getProvider());
0N/A
0N/A System.out.println("Testing Signature.getInstance() for Signature...");
0N/A sig = Signature.getInstance("fuu");
0N/A same(foo, sig.getProvider());
0N/A sig = Signature.getInstance("fuu");
0N/A sig.initSign(privateKey);
0N/A same(foo, sig.getProvider());
0N/A sig = Signature.getInstance("fuu", "foo");
0N/A sig.initSign(privateKey);
0N/A same(foo, sig.getProvider());
0N/A sig = Signature.getInstance("fuu", foo);
0N/A sig.initSign(privateKey);
0N/A same(foo, sig.getProvider());
0N/A
0N/A System.out.println("Testing CertStore.getInstance()...");
0N/A CertStoreParameters params = new CollectionCertStoreParameters(Collections.EMPTY_LIST);
0N/A CertStore cs;
0N/A cs = CertStore.getInstance("foo", params);
0N/A cs = CertStore.getInstance("foo", params, "foo");
0N/A cs = CertStore.getInstance("foo", params, foo);
0N/A
0N/A System.out.println("Testing failover...");
0N/A m = MessageDigest.getInstance("bar");
0N/A same(m.getProvider(), baz);
0N/A sig = Signature.getInstance("bar");
0N/A same(sig.getProvider(), baz);
0N/A cs = CertStore.getInstance("bar", params);
0N/A same(cs.getProvider(), baz);
0N/A
0N/A System.out.println("Testing Signature delayed provider selection...");
0N/A sig = Signature.getInstance("baz");
0N/A sig.initVerify(new FooPublicKey());
0N/A same(sig.getProvider(), baz);
0N/A
0N/A Provider.Service s = foo.getService("CertStore", "foo");
0N/A s.newInstance(null);
0N/A s.newInstance(params);
0N/A try {
0N/A s.newInstance(0);
0N/A throw new Exception("call should not succeed");
0N/A } catch (NoSuchAlgorithmException e) {
0N/A e.printStackTrace();
0N/A Throwable cause = e.getCause();
0N/A if (cause instanceof InvalidParameterException == false) {
0N/A throw new Exception("incorrect exception");
0N/A }
0N/A }
0N/A
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("Done (" + (stop - start) + " ms).");
0N/A }
0N/A
0N/A public static class FooProvider extends Provider {
0N/A FooProvider() {
0N/A super("foo", 1.0d, "none");
0N/A put("MessageDigest.foo", "GetInstance$FooDigest");
0N/A put("CertStore.foo", "GetInstance$FooStore");
0N/A put("Signature.foo", "GetInstance$FooSignatureSpi");
0N/A
0N/A put("Signature.fuu", "GetInstance$BazSignature");
0N/A
0N/A // throws InvalidKeyException, skipped in delayed provider selection
0N/A put("Signature.baz", "GetInstance$BazSignatureSpi");
0N/A }
0N/A }
0N/A
0N/A public static class BarProvider extends Provider {
0N/A BarProvider() {
0N/A super("bar", 1.0d, "none");
0N/A // all entries invalid for failover
0N/A put("MessageDigest.bar", "GetInstance$FooKey");
0N/A put("Signature.bar", "GetInstance$FooKey");
0N/A put("Certstore.bar", "GetInstance$FooKey");
0N/A
0N/A // not an SPI, skipped in delayed provider selection
0N/A put("Signature.baz", "GetInstance$BazSignature");
0N/A }
0N/A }
0N/A
0N/A public static class BazProvider extends Provider {
0N/A BazProvider() {
0N/A super("baz", 1.0d, "none");
0N/A put("MessageDigest.bar", "GetInstance$FooDigest");
0N/A put("CertStore.bar", "GetInstance$FooStore");
0N/A put("Signature.bar", "GetInstance$FooSignatureSpi");
0N/A
0N/A put("Signature.baz", "GetInstance$FooSignatureSpi");
0N/A }
0N/A }
0N/A
0N/A public static class FooDigest extends MessageDigestSpi {
0N/A public byte[] engineDigest() { return new byte[0]; }
0N/A public void engineReset() {}
0N/A public void engineUpdate(byte input) {}
0N/A public void engineUpdate(byte[] b, int ofs, int len) {}
0N/A }
0N/A
0N/A public static class FooStore extends CertStoreSpi {
0N/A public FooStore(CertStoreParameters params) throws InvalidAlgorithmParameterException { super(params); }
0N/A public Collection engineGetCertificates(CertSelector sel) { return Collections.EMPTY_LIST; }
0N/A public Collection engineGetCRLs(CRLSelector sel) { return Collections.EMPTY_LIST; }
0N/A }
0N/A
0N/A public static class BaseSignatureSpi extends SignatureSpi {
0N/A protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
0N/A }
0N/A protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
0N/A }
0N/A protected void engineUpdate(byte b) throws SignatureException { }
0N/A protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { }
0N/A protected byte[] engineSign() throws SignatureException {
0N/A return new byte[0];
0N/A }
0N/A protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
0N/A return false;
0N/A }
0N/A protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
0N/A }
0N/A protected Object engineGetParameter(String param) throws InvalidParameterException {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public static class BaseSignature extends Signature {
0N/A BaseSignature(String s) {
0N/A super(s);
0N/A }
0N/A protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
0N/A //
0N/A }
0N/A protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException { }
0N/A protected void engineUpdate(byte b) throws SignatureException { }
0N/A protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { }
0N/A protected byte[] engineSign() throws SignatureException {
0N/A return new byte[0];
0N/A }
0N/A protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
0N/A return false;
0N/A }
0N/A protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
0N/A }
0N/A protected Object engineGetParameter(String param) throws InvalidParameterException {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public static abstract class FooKey implements Key {
0N/A public String getFormat() { return null; }
0N/A public byte[] getEncoded() { return null; }
0N/A public String getAlgorithm() { return "foo"; }
0N/A }
0N/A
0N/A public static class FooPrivateKey extends FooKey implements PrivateKey { }
0N/A
0N/A public static class FooPublicKey extends FooKey implements PublicKey { }
0N/A
0N/A public static class FooSignatureSpi extends BaseSignatureSpi {
0N/A public FooSignatureSpi() {
0N/A super();
0N/A System.out.println("FooSignatureSpi constructor");
0N/A }
0N/A }
0N/A
0N/A public static class BazSignatureSpi extends BaseSignatureSpi {
0N/A public BazSignatureSpi() {
0N/A super();
0N/A System.out.println("BazSignatureSpi constructor");
0N/A }
0N/A protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
0N/A throw new InvalidKeyException("verify not supported");
0N/A }
0N/A }
0N/A
0N/A public static class BazSignature extends BaseSignature {
0N/A public BazSignature() {
0N/A super("baz");
0N/A System.out.println("BazSignature constructor");
0N/A }
0N/A }
0N/A
0N/A}