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.TestTargets;
26import dalvik.annotation.TestLevel;
27import dalvik.annotation.TestTargetNew;
28import dalvik.annotation.TestTargetClass;
29
30import junit.framework.Test;
31import junit.framework.TestCase;
32import junit.framework.TestSuite;
33
34import java.math.BigInteger;
35import java.security.cert.CRLException;
36import java.security.cert.X509CRLEntry;
37import java.util.Date;
38import java.util.Set;
39
40/**
41 */
42@TestTargetClass(X509CRLEntry.class)
43public class X509CRLEntryTest extends TestCase {
44
45    X509CRLEntry tbt_crlentry;
46
47    /**
48     * The stub class used for testing of non abstract methods.
49     */
50    private class TBTCRLEntry extends X509CRLEntry {
51        public TBTCRLEntry() {
52            super();
53        }
54
55        public Set<String> getNonCriticalExtensionOIDs() {
56            return null;
57        }
58
59        public Set<String> getCriticalExtensionOIDs() {
60            return null;
61        }
62
63        public byte[] getExtensionValue(String oid) {
64            return null;
65        }
66
67        public boolean hasUnsupportedCriticalExtension() {
68            return false;
69        }
70
71        public byte[] getEncoded() throws CRLException {
72            return null;
73        }
74
75        public BigInteger getSerialNumber() {
76            return null;
77        }
78
79        public Date getRevocationDate() {
80            return null;
81        }
82
83        public boolean hasExtensions() {
84            return false;
85        }
86
87        public String toString() {
88            return null;
89        }
90    }
91
92    public X509CRLEntryTest() {
93        tbt_crlentry = new TBTCRLEntry() {
94            public byte[] getEncoded() throws CRLException {
95                return new byte[] {1, 2, 3};
96            }
97        };
98    }
99
100    /**
101     * X509CRLEntry() method testing. Tests for creating object.
102     */
103    @TestTargetNew(
104        level = TestLevel.COMPLETE,
105        notes = "",
106        method = "X509CRLEntry",
107        args = {}
108    )
109    public void testX509CRLEntry() {
110        TBTCRLEntry tbt_crlentry = new TBTCRLEntry();
111
112        assertNull(tbt_crlentry.getCertificateIssuer());
113        assertNull(tbt_crlentry.getCriticalExtensionOIDs());
114        try {
115            assertNull(tbt_crlentry.getEncoded());
116        } catch (CRLException e) {
117            fail("Unexpected exception " + e.getMessage());
118        }
119        assertNull(tbt_crlentry.getNonCriticalExtensionOIDs());
120        assertNull(tbt_crlentry.getRevocationDate());
121
122    }
123
124    /**
125     * equals(Object other) method testing. Tests the correctness of equal
126     * operation: it should be reflexive, symmetric, transitive, consistent
127     * and should be false on null object.
128     */
129    @TestTargetNew(
130        level = TestLevel.COMPLETE,
131        notes = "",
132        method = "equals",
133        args = {java.lang.Object.class}
134    )
135    public void testEquals() {
136        TBTCRLEntry tbt_crlentry_1 = new TBTCRLEntry() {
137            public byte[] getEncoded() {
138                return new byte[] {1, 2, 3};
139            }
140        };
141
142        TBTCRLEntry tbt_crlentry_2 = new TBTCRLEntry() {
143            public byte[] getEncoded() {
144                return new byte[] {1, 2, 3};
145            }
146        };
147
148        TBTCRLEntry tbt_crlentry_3 = new TBTCRLEntry() {
149            public byte[] getEncoded() {
150                return new byte[] {3, 2, 1};
151            }
152        };
153
154        // checking for reflexive law:
155        assertTrue("The equivalence relation should be reflexive.",
156                                            tbt_crlentry.equals(tbt_crlentry));
157
158        assertEquals("The CRL Entries with equals encoded form should be equal",
159                                            tbt_crlentry, tbt_crlentry_1);
160        // checking for symmetric law:
161        assertTrue("The equivalence relation should be symmetric.",
162                                            tbt_crlentry_1.equals(tbt_crlentry));
163
164        assertEquals("The CRL Entries with equals encoded form should be equal",
165                                            tbt_crlentry_1, tbt_crlentry_2);
166        // checking for transitive law:
167        assertTrue("The equivalence relation should be transitive.",
168                                            tbt_crlentry.equals(tbt_crlentry_2));
169
170        assertFalse("Should not be equal to null object.",
171                                            tbt_crlentry.equals(null));
172
173        assertFalse("The CRL Entries with differing encoded form "
174                                            + "should not be equal.",
175                                            tbt_crlentry.equals(tbt_crlentry_3));
176        assertFalse("The CRL Entries should not be equals to the object "
177                                + "which is not an instance of X509CRLEntry.",
178                                            tbt_crlentry.equals(new Object()));
179    }
180
181    /**
182     * hashCode() method testing. Tests that for equal objects hash codes
183     * are equal.
184     */
185    @TestTargetNew(
186        level = TestLevel.COMPLETE,
187        notes = "",
188        method = "hashCode",
189        args = {}
190    )
191    public void testHashCode() {
192        TBTCRLEntry tbt_crlentry_1 = new TBTCRLEntry() {
193            public byte[] getEncoded() {
194                return new byte[] {1, 2, 3};
195            }
196        };
197        assertTrue("Equal objects should have the same hash codes.",
198                        tbt_crlentry.hashCode() == tbt_crlentry_1.hashCode());
199    }
200
201    /**
202     * getCertificateIssuer() method testing. Tests if the method throws
203     * appropriate exception.
204     */
205    @TestTargetNew(
206        level = TestLevel.COMPLETE,
207        notes = "",
208        method = "getCertificateIssuer",
209        args = {}
210    )
211    public void testGetCertificateIssuer() {
212        assertNull("The default implementation should return null.",
213                tbt_crlentry.getCertificateIssuer());
214    }
215
216    @TestTargets({
217        @TestTargetNew(
218            level = TestLevel.COMPLETE,
219            notes = "",
220            method = "getEncoded",
221            args = {}
222        ),
223        @TestTargetNew(
224            level = TestLevel.COMPLETE,
225            notes = "",
226            method = "getRevocationDate",
227            args = {}
228        ),
229        @TestTargetNew(
230            level = TestLevel.COMPLETE,
231            notes = "",
232            method = "getSerialNumber",
233            args = {}
234        ),
235        @TestTargetNew(
236            level = TestLevel.COMPLETE,
237            notes = "",
238            method = "hasExtensions",
239            args = {}
240        ),
241        @TestTargetNew(
242            level = TestLevel.COMPLETE,
243            notes = "",
244            method = "toString",
245            args = {}
246        )
247    })
248    public void testAbstractMethods() {
249        TBTCRLEntry tbt = new TBTCRLEntry() {
250            public byte[] getEncoded() {
251                return new byte[] {1, 2, 3};
252            }
253        };
254
255        try {
256            tbt.getEncoded();
257            tbt.getRevocationDate();
258            tbt.getSerialNumber();
259            tbt.hasExtensions();
260            tbt.toString();
261        } catch (Exception e) {
262            fail("Unexpected exception");
263        }
264    }
265
266    public static Test suite() {
267        return new TestSuite(X509CRLEntryTest.class);
268    }
269
270    public static void main(String[] args) {
271        junit.textui.TestRunner.run(suite());
272    }
273}
274
275