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