MyCertificate.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Vladimir N. Molotkov
20*/
21
22package org.apache.harmony.security.tests.support.cert;
23
24import java.security.InvalidKeyException;
25import java.security.NoSuchAlgorithmException;
26import java.security.NoSuchProviderException;
27import java.security.PublicKey;
28import java.security.SignatureException;
29import java.security.cert.Certificate;
30import java.security.cert.CertificateEncodingException;
31import java.security.cert.CertificateException;
32
33/**
34 * Stub class for <code>java.security.cert.Certificate</code> tests
35 */
36public class MyCertificate extends Certificate {
37
38    // MyCertificate encoding
39    private final byte[] encoding;
40
41    /**
42     * Constructs new object of class <code>MyCertificate</code>
43     *
44     * @param type
45     * @param encoding
46     */
47    public MyCertificate(String type, byte[] encoding) {
48        super(type);
49        // don't copy to allow null parameter in test
50        this.encoding = encoding;
51    }
52
53    /**
54     * Returns <code>MyCertificate</code> encoding
55     */
56    public byte[] getEncoded() throws CertificateEncodingException {
57        // do copy to force NPE in test
58        return encoding.clone();
59    }
60
61    /**
62     * Does nothing
63     */
64    public void verify(PublicKey key) throws CertificateException,
65            NoSuchAlgorithmException, InvalidKeyException,
66            NoSuchProviderException, SignatureException {
67    }
68
69    /**
70     * Does nothing
71     */
72    public void verify(PublicKey key, String sigProvider)
73            throws CertificateException, NoSuchAlgorithmException,
74            InvalidKeyException, NoSuchProviderException, SignatureException {
75    }
76
77    /**
78     * Returns formatted <code>String</code>
79     * describing <code>MyCertificate</code> object
80     */
81    public String toString() {
82        return "[My test Certificate, type: " + getType() + "]";
83    }
84
85    /**
86     * Returns public key (stub) from <code>MyCertificate</code> object
87     */
88    public PublicKey getPublicKey() {
89        return new PublicKey() {
90            public String getAlgorithm() {
91                return "TEST";
92            }
93            public byte[] getEncoded() {
94                return new byte[] {(byte)1, (byte)2, (byte)3};
95            }
96            public String getFormat() {
97                return "TEST_FORMAT";
98            }
99        };
100    }
101
102}
103