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 Alexander Y. Kleymenov
20* @version $Revision$
21*/
22
23package tests.security.cert;
24
25import dalvik.annotation.AndroidOnly;
26
27import junit.framework.Test;
28import junit.framework.TestCase;
29import junit.framework.TestSuite;
30
31import java.io.ByteArrayInputStream;
32import java.math.BigInteger;
33import java.security.InvalidKeyException;
34import java.security.NoSuchAlgorithmException;
35import java.security.NoSuchProviderException;
36import java.security.Principal;
37import java.security.PublicKey;
38import java.security.SignatureException;
39import java.security.cert.CRLException;
40import java.security.cert.Certificate;
41import java.security.cert.CertificateFactory;
42import java.security.cert.X509CRL;
43import java.security.cert.X509CRLEntry;
44import java.security.cert.X509Certificate;
45import java.util.Date;
46import java.util.Set;
47
48import javax.security.auth.x500.X500Principal;
49
50import org.apache.harmony.security.tests.support.cert.TestUtils;
51
52import tests.security.cert.X509CRL2Test.MyX509CRL;
53/**
54 */
55public class X509CRLTest extends TestCase {
56
57    private X509CRL tbt_crl;
58
59    String certificate = "-----BEGIN CERTIFICATE-----\n"
60        + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
61        + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
62        + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
63        + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
64        + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
65        + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
66        + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
67        + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
68        + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
69        + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
70        + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
71        + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
72        + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
73        + "-----END CERTIFICATE-----\n";
74
75    ByteArrayInputStream certArray = new ByteArrayInputStream(certificate
76        .getBytes());
77
78    /**
79     * The stub class used for testing of non abstract methods.
80     */
81    private class TBTCRL extends X509CRL {
82        public String toString() {
83            return null;
84        }
85
86        public boolean isRevoked(Certificate cert) {
87            return true;
88        }
89
90        public Set<String> getNonCriticalExtensionOIDs() {
91            return null;
92        }
93
94        public Set<String> getCriticalExtensionOIDs() {
95            return null;
96        }
97
98        public byte[] getExtensionValue(String oid) {
99            return null;
100        }
101
102        public boolean hasUnsupportedCriticalExtension() {
103            return false;
104        }
105
106        public byte[] getEncoded() {
107            return null;
108        }
109
110        public void verify(PublicKey key)
111                 throws CRLException, NoSuchAlgorithmException,
112                        InvalidKeyException, NoSuchProviderException,
113                        SignatureException
114        {
115        }
116
117        public void verify(PublicKey key, String sigProvider)
118                 throws CRLException, NoSuchAlgorithmException,
119                        InvalidKeyException, NoSuchProviderException,
120                        SignatureException
121        {
122        }
123
124        public int getVersion() {
125            return 2;
126        }
127
128        public Principal getIssuerDN() {
129            return null;
130        }
131
132        public Date getThisUpdate() {
133            return null;
134        }
135
136        public Date getNextUpdate() {
137            return null;
138        }
139
140        public X509CRLEntry getRevokedCertificate(BigInteger serialNumber) {
141            return null;
142        }
143
144        public Set<X509CRLEntry> getRevokedCertificates() {
145            return null;
146        }
147
148        public byte[] getTBSCertList() {
149            return null;
150        }
151
152        public byte[] getSignature() {
153            return null;
154        }
155
156        public String getSigAlgName() {
157            return null;
158        }
159
160        public String getSigAlgOID() {
161            return null;
162        }
163
164        public byte[] getSigAlgParams() {
165            return null;
166        }
167    }
168
169
170    public X509CRLTest() {
171
172    }
173
174    public void setUp() {
175        tbt_crl = new TBTCRL() {
176            public byte[] getEncoded() {
177                return new byte[] {1, 2, 3};
178            }
179        };
180    }
181
182    /**
183     * getType() method testing. Tests that getType() method returns
184     * the value "X.509"
185     */
186    public void testGetType() {
187        assertEquals("The type of X509CRL should be X.509",
188                                            tbt_crl.getType(), "X.509");
189    }
190
191    /**
192     * equals(Object other) method testing. Tests the correctness of equal
193     * operation: it should be reflexive, symmetric, transitive, consistent
194     * and should be false on null object.
195     */
196    public void testEquals() {
197        TBTCRL tbt_crl_1 = new TBTCRL() {
198            public byte[] getEncoded() {
199                return new byte[] {1, 2, 3};
200            }
201        };
202
203        TBTCRL tbt_crl_2 = new TBTCRL() {
204            public byte[] getEncoded() {
205                return new byte[] {1, 2, 3};
206            }
207        };
208
209        TBTCRL tbt_crl_3 = new TBTCRL() {
210            public byte[] getEncoded() {
211                return new byte[] {3, 2, 1};
212            }
213        };
214
215        // checking for reflexive law:
216        assertTrue("The equivalence relation should be reflexive.",
217                                                    tbt_crl.equals(tbt_crl));
218
219        assertEquals("The CRLs with equal encoded form should be equal",
220                                                    tbt_crl, tbt_crl_1);
221        // checking for symmetric law:
222        assertTrue("The equivalence relation should be symmetric.",
223                                                    tbt_crl_1.equals(tbt_crl));
224
225        assertEquals("The CRLs with equal encoded form should be equal",
226                                                    tbt_crl_1, tbt_crl_2);
227        // checking for transitive law:
228        assertTrue("The equivalence relation should be transitive.",
229                                                    tbt_crl.equals(tbt_crl_2));
230
231        assertFalse("Should not be equal to null object.",
232                                                    tbt_crl.equals(null));
233
234        assertFalse("The CRLs with differing encoded form should not be equal",
235                                                    tbt_crl.equals(tbt_crl_3));
236        assertFalse("The CRL should not be equals to the object which is not "
237                    + "an instance of X509CRL", tbt_crl.equals(new Object()));
238    }
239
240    /**
241     * hashCode() method testing. Tests that for equal objects hash codes
242     * are equal.
243     */
244    public void testHashCode() {
245        TBTCRL tbt_crl_1 = new TBTCRL() {
246            public byte[] getEncoded() {
247                return new byte[] {1, 2, 3};
248            }
249        };
250        assertTrue("Equal objects should have the same hash codes.",
251                                    tbt_crl.hashCode() == tbt_crl_1.hashCode());
252    }
253
254    /**
255     * java.security.cert.X509CRL#getIssuerX500Principal()
256     */
257    public void testGetIssuerX500Principal() {
258        // return valid encoding
259        TBTCRL crl = new TBTCRL() {
260            public byte[] getEncoded() {
261                return TestUtils.getX509CRL_v1();
262            }
263        };
264
265        assertEquals(new X500Principal("CN=Z"), crl.getIssuerX500Principal());
266    }
267
268    /**
269     * getRevokedCertificate(X509Certificate certificate) method testing.
270     * Check if the default implementation throws NullPointerException
271     * on null input data.
272     */
273    @AndroidOnly("Test filed on RI: getRevokedCertificate throws " +
274            "RuntimeException.")
275    public void testGetRevokedCertificate() {
276        try {
277            tbt_crl.getRevokedCertificate((X509Certificate) null);
278            fail("NullPointerException should be thrown "
279                        + "in the case of null input data.");
280        } catch (NullPointerException e) {
281        }
282
283
284        try {
285            CertificateFactory cf = CertificateFactory.getInstance("X.509");
286            X509Certificate cert = (X509Certificate) cf.generateCertificate(certArray);
287            tbt_crl.getRevokedCertificate(cert);
288        } catch (Exception e) {
289            fail("Unexpected exception: " + e);
290        }
291    }
292
293    public void testAbstractMethods() {
294        TBTCRL crl = new TBTCRL() {
295            public byte[] getEncoded() {
296                return TestUtils.getX509CRL_v1();
297            }
298        };
299
300        try {
301            crl.getEncoded();
302            crl.getIssuerDN();
303            crl.getNextUpdate();
304            crl.getRevokedCertificate(BigInteger.ONE);
305            crl.getRevokedCertificates();
306            crl.getSigAlgName();
307            crl.getSigAlgOID();
308            crl.getSigAlgParams();
309            crl.getSignature();
310            crl.getTBSCertList();
311            crl.getThisUpdate();
312            crl.getVersion();
313
314            crl.verify(null);
315            crl.verify(null, "test");
316        } catch (Exception e) {
317            fail("Unexpected exception for constructor");
318        }
319    }
320
321    public void testX509CRL() {
322        try {
323            TBTCRL crl = new TBTCRL();
324            assertTrue(crl instanceof X509CRL);
325        } catch (Exception e) {
326            fail("Unexpected exception for constructor");
327        }
328    }
329
330    public static Test suite() {
331        return new TestSuite(X509CRLTest.class);
332    }
333}
334
335