SignatureGetAlgorithm.java revision 15381:6c96af8a34b2
1/*
2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * Portions Copyright (c) 2013 IBM Corporation
26 */
27
28/*
29 * @test
30 * @bug 8014620 8130181
31 * @summary Signature.getAlgorithm return null in special case
32 * @run main/othervm SignatureGetAlgorithm
33 * @author youdwei
34 */
35import java.security.*;
36
37public class SignatureGetAlgorithm {
38
39    public static void main(String[] args) throws Exception {
40        Provider testProvider = new TestProvider();
41        Security.addProvider(testProvider);
42        Signature sig = Signature.getInstance("MySignatureAlg");
43        String algorithm = sig.getAlgorithm();
44        System.out.println("Algorithm Name: " + algorithm);
45        if (algorithm == null) {
46            throw new Exception("algorithm name should be 'MySignatureAlg'");
47        }
48    }
49
50    public static class TestProvider extends Provider {
51        TestProvider() {
52            super("testSignatureGetAlgorithm", "1.0", "test Signatures");
53            put("Signature.MySignatureAlg",
54                "SignatureGetAlgorithm$MySignatureAlg");
55        }
56    }
57
58    public static class MySignatureAlg extends Signature {
59
60        public MySignatureAlg() {
61            super(null);
62        }
63
64        MySignatureAlg(String s) {
65            super(s);
66        }
67
68        @Override
69        protected void engineInitVerify(PublicKey publicKey)
70                throws InvalidKeyException {
71        }
72
73        @Override
74        protected void engineInitSign(PrivateKey privateKey)
75                throws InvalidKeyException {
76        }
77
78        @Override
79        protected void engineUpdate(byte b) throws SignatureException {
80        }
81
82        @Override
83        protected void engineUpdate(byte[] b, int off, int len)
84                throws SignatureException {
85        }
86
87        @Override
88        protected byte[] engineSign()
89                throws SignatureException {
90            return new byte[0];
91        }
92
93        @Override
94        protected boolean engineVerify(byte[] sigBytes)
95                throws SignatureException {
96            return false;
97        }
98
99        @Override
100        @Deprecated
101        protected void engineSetParameter(String param, Object value)
102                throws InvalidParameterException {
103        }
104
105        @Override
106        @Deprecated
107        protected Object engineGetParameter(String param)
108                throws InvalidParameterException {
109            return null;
110        }
111    }
112}
113