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 Vera Y. Petrashkova
20* @version $Revision$
21*/
22
23package tests.security.cert;
24
25import junit.framework.TestCase;
26
27import java.security.cert.CertPath;
28import java.security.cert.CertPathValidatorException;
29import java.security.cert.Certificate;
30import java.util.Iterator;
31import java.util.List;
32import java.util.StringTokenizer;
33import java.util.Vector;
34
35
36/**
37 * Tests for <code>CertPathValidatorException</code> class constructors and
38 * methods.
39 *
40 */
41public class CertPathValidatorExceptionTest extends TestCase {
42
43    private static String[] msgs = {
44            "",
45            "Check new message",
46            "Check new message Check new message Check new message Check new message Check new message" };
47
48    private static Throwable tCause = new Throwable("Throwable for exception");
49
50    /**
51     * Test for <code>CertPathValidatorException()</code> constructor
52     * Assertion: constructs CertPathValidatorException with no detail message
53     */
54    public void testCertPathValidatorException01() {
55        CertPathValidatorException tE = new CertPathValidatorException();
56        assertNull("getMessage() must return null.", tE.getMessage());
57        assertNull("getCause() must return null", tE.getCause());
58    }
59
60    /**
61     * Test for <code>CertPathValidatorException(String)</code> constructor
62     * Assertion: constructs CertPathValidatorException with detail message msg.
63     * Parameter <code>msg</code> is not null.
64     */
65    public void testCertPathValidatorException02() {
66        CertPathValidatorException tE;
67        for (int i = 0; i < msgs.length; i++) {
68            tE = new CertPathValidatorException(msgs[i]);
69            assertEquals("getMessage() must return: ".concat(msgs[i]), tE
70                    .getMessage(), msgs[i]);
71            assertNull("getCause() must return null", tE.getCause());
72        }
73    }
74
75    /**
76     * Test for <code>CertPathValidatorException(String)</code> constructor
77     * Assertion: constructs CertPathValidatorException when <code>msg</code>
78     * is null
79     */
80    public void testCertPathValidatorException03() {
81        String msg = null;
82        CertPathValidatorException tE = new CertPathValidatorException(msg);
83        assertNull("getMessage() must return null.", tE.getMessage());
84        assertNull("getCause() must return null", tE.getCause());
85    }
86
87    /**
88     * Test for <code>CertPathValidatorException(Throwable)</code> constructor
89     * Assertion: constructs CertPathValidatorException when <code>cause</code>
90     * is null
91     */
92    public void testCertPathValidatorException04() {
93        Throwable cause = null;
94        CertPathValidatorException tE = new CertPathValidatorException(cause);
95        assertNull("getMessage() must return null.", tE.getMessage());
96        assertNull("getCause() must return null", tE.getCause());
97    }
98
99    /**
100     * Test for <code>CertPathValidatorException(Throwable)</code> constructor
101     * Assertion: constructs CertPathValidatorException when <code>cause</code>
102     * is not null
103     */
104    public void testCertPathValidatorException05() {
105        CertPathValidatorException tE = new CertPathValidatorException(tCause);
106        if (tE.getMessage() != null) {
107            String toS = tCause.toString();
108            String getM = tE.getMessage();
109            assertTrue("getMessage() should contain ".concat(toS), (getM
110                    .indexOf(toS) != -1));
111        }
112        assertNotNull("getCause() must not return null", tE.getCause());
113        assertEquals("getCause() must return ".concat(tCause.toString()), tE
114                .getCause(), tCause);
115    }
116
117    /**
118     * Test for <code>CertPathValidatorException(String, Throwable)</code>
119     * constructor Assertion: constructs CertPathValidatorException when
120     * <code>cause</code> is null <code>msg</code> is null
121     */
122    public void testCertPathValidatorException06() {
123        CertPathValidatorException tE = new CertPathValidatorException(null,
124                null);
125        assertNull("getMessage() must return null", tE.getMessage());
126        assertNull("getCause() must return null", tE.getCause());
127    }
128
129    /**
130     * Test for <code>CertPathValidatorException(String, Throwable)</code>
131     * constructor Assertion: constructs CertPathValidatorException when
132     * <code>cause</code> is null <code>msg</code> is not null
133     */
134    public void testCertPathValidatorException07() {
135        CertPathValidatorException tE;
136        for (int i = 0; i < msgs.length; i++) {
137            tE = new CertPathValidatorException(msgs[i], null);
138            assertEquals("getMessage() must return: ".concat(msgs[i]), tE
139                    .getMessage(), msgs[i]);
140            assertNull("getCause() must return null", tE.getCause());
141        }
142    }
143
144    /**
145     * Test for <code>CertPathValidatorException(String, Throwable)</code>
146     * constructor Assertion: constructs CertPathValidatorException when
147     * <code>cause</code> is not null <code>msg</code> is null
148     */
149    public void testCertPathValidatorException08() {
150        CertPathValidatorException tE = new CertPathValidatorException(null,
151                tCause);
152        if (tE.getMessage() != null) {
153            String toS = tCause.toString();
154            String getM = tE.getMessage();
155            assertTrue("getMessage() must should ".concat(toS), (getM
156                    .indexOf(toS) != -1));
157        }
158        assertNotNull("getCause() must not return null", tE.getCause());
159        assertEquals("getCause() must return ".concat(tCause.toString()), tE
160                .getCause(), tCause);
161    }
162
163    /**
164     * Test for <code>CertPathValidatorException(String, Throwable)</code>
165     * constructor Assertion: constructs CertPathValidatorException when
166     * <code>cause</code> is not null <code>msg</code> is not null
167     */
168    public void testCertPathValidatorException09() {
169        CertPathValidatorException tE;
170        for (int i = 0; i < msgs.length; i++) {
171            tE = new CertPathValidatorException(msgs[i], tCause);
172            String getM = tE.getMessage();
173            String toS = tCause.toString();
174            if (msgs[i].length() > 0) {
175                assertTrue("getMessage() must contain ".concat(msgs[i]), getM
176                        .indexOf(msgs[i]) != -1);
177                if (!getM.equals(msgs[i])) {
178                    assertTrue("getMessage() should contain ".concat(toS), getM
179                            .indexOf(toS) != -1);
180                }
181            }
182            assertNotNull("getCause() must not return null", tE.getCause());
183            assertEquals("getCause() must return ".concat(tCause.toString()),
184                    tE.getCause(), tCause);
185        }
186    }
187
188    /**
189     * Test for
190     * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code>
191     * constructor Assertion: constructs CertPathValidatorException when
192     * <code>cause</code> is null <code>msg</code> is null
193     * <code>certPath</code> is null <code>index</code> is -1
194     */
195    public void testCertPathValidatorException10() {
196        CertPathValidatorException tE = new CertPathValidatorException(null,
197                null, null, -1);
198        assertNull("getMessage() must return null", tE.getMessage());
199        assertNull("getCause() must return null", tE.getCause());
200        assertNull("getCertPath() must return null", tE.getCertPath());
201        assertEquals("getIndex() must be -1", tE.getIndex(), -1);
202    }
203
204    /**
205     * Test for
206     * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code>
207     * constructor Assertion: constructs CertPathValidatorException when
208     * <code>cause</code> is null <code>msg</code> is null
209     * <code>certPath</code> is null <code>index</code> not -1 throws:
210     * IllegalArgumentException
211     */
212    public void testCertPathValidatorException11() {
213        int[] indx = { 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };
214        for (int j = 0; j < indx.length; j++) {
215            for (int i = 0; i < msgs.length; i++) {
216                try {
217                    new CertPathValidatorException(msgs[i], tCause, null, indx[j]);
218                    fail("Error. IllegalArgumentException was not thrown as expected. "
219                            + " msg: "
220                            + msgs[i]
221                            + ", certPath is null and index is " + indx[j]);
222                } catch (IllegalArgumentException e) {
223                }
224            }
225        }
226    }
227
228    /**
229     * Test for
230     * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code>
231     * constructor Assertion: constructs CertPathValidatorException when
232     * <code>cause</code> not null <code>msg</code> not null
233     * <code>certPath</code> is null <code>index</code> is -1
234     */
235    public void testCertPathValidatorException12() {
236        CertPathValidatorException tE;
237
238        for (int i = 0; i < msgs.length; i++) {
239            try {
240                tE = new CertPathValidatorException(msgs[i], tCause, null, -1);
241                String getM = tE.getMessage();
242                String toS = tCause.toString();
243                if (msgs[i].length() > 0) {
244                    assertTrue("getMessage() must contain ".concat(msgs[i]),
245                            getM.indexOf(msgs[i]) != -1);
246                    if (!getM.equals(msgs[i])) {
247                        assertTrue("getMessage() should contain ".concat(toS),
248                                getM.indexOf(toS) != -1);
249                    }
250                }
251                assertNotNull("getCause() must not return null", tE.getCause());
252                assertEquals("getCause() must return "
253                        .concat(tCause.toString()), tE.getCause(), tCause);
254                assertNull("getCertPath() must return null", tE.getCertPath());
255                assertEquals("getIndex() must return -1", tE.getIndex(), -1);
256            } catch (IndexOutOfBoundsException e) {
257                fail("Unexpected exception: " + e.toString()
258                        + " Parameters: msg: " + msgs[i]
259                        + ", certPath is null and index is -1");
260            }
261        }
262    }
263
264    /**
265     * Test for
266     * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code>
267     * constructor Assertion: constructs CertPathValidatorException when
268     * <code>cause</code> not null <code>msg</code> not null
269     * <code>certPath</code> not null <code>index</code>< -1 || >=
270     * certPath.getCertificates().size() throws: IndexOutOfBoundsException
271     */
272    public void testCertPathValidatorException13() {
273        myCertPath mcp = new myCertPath("X.509", "");
274        CertPath cp = mcp.get("X.509");
275        int[] indx = { -2, -100, 0, 1, 100, Integer.MAX_VALUE,
276                Integer.MIN_VALUE };
277        for (int j = 0; j < indx.length; j++) {
278            for (int i = 0; i < msgs.length; i++) {
279                try {
280                    new CertPathValidatorException(msgs[i], tCause, cp, indx[j]);
281                    fail("IndexOutOfBoundsException was not thrown as expected. "
282                            + " msg: "
283                            + msgs[i]
284                            + ", certPath is null and index is " + indx[j]);
285                } catch (IndexOutOfBoundsException e) {
286                }
287            }
288        }
289    }
290
291    /**
292     * Test for
293     * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code>
294     * constructor Assertion: constructs CertPathValidatorException when
295     * <code>cause</code> not null <code>msg</code> not null
296     * <code>certPath</code> not null <code>index</code><
297     * certPath.getCertificates().size()
298     */
299    public void testCertPathValidatorException14() {
300        CertPathValidatorException tE;
301        myCertPath mcp = new myCertPath("X.509", "");
302        CertPath cp = mcp.get("X.509");
303        for (int i = 0; i < msgs.length; i++) {
304            try {
305                tE = new CertPathValidatorException(msgs[i], tCause, cp, -1);
306                String getM = tE.getMessage();
307                String toS = tCause.toString();
308                if (msgs[i].length() > 0) {
309                    assertTrue("getMessage() must contain ".concat(msgs[i]),
310                            getM.indexOf(msgs[i]) != -1);
311                    if (!getM.equals(msgs[i])) {
312                        assertTrue("getMessage() should contain ".concat(toS),
313                                getM.indexOf(toS) != -1);
314                    }
315                }
316                assertNotNull("getCause() must not return null", tE.getCause());
317                assertEquals("getCause() must return "
318                        .concat(tCause.toString()), tE.getCause(), tCause);
319                assertNotNull("getCertPath() must not return null", tE
320                        .getCertPath());
321                assertEquals(
322                        "getCertPath() must return ".concat(cp.toString()), tE
323                                .getCertPath(), cp);
324                assertEquals("getIndex() must return -1", tE.getIndex(), -1);
325
326            } catch (IndexOutOfBoundsException e) {
327                fail("Unexpected IndexOutOfBoundsException was thrown. "
328                        + e.toString());
329            }
330        }
331    }
332
333    /**
334     * Test for <code>getCertPath()</code>. Returns the certification path
335     * that was being validated when the exception was thrown.
336     */
337    public void testCertPathValidatorException15() {
338        CertPathValidatorException tE = new CertPathValidatorException();
339        assertNull("getCertPath() must return null.", tE.getCertPath());
340
341        for (int i = 0; i < msgs.length; i++) {
342            tE = new CertPathValidatorException(msgs[i]);
343            assertNull("getCertPath() must return null ", tE.getCertPath());
344        }
345
346        Throwable cause = null;
347        tE = new CertPathValidatorException(cause);
348        assertNull("getCertPath() must return null.", tE.getCertPath());
349
350        tE = new CertPathValidatorException(tCause);
351        assertNull("getCertPath() must return null.", tE.getCertPath());
352
353        for (int i = 0; i < msgs.length; i++) {
354            tE = new CertPathValidatorException(msgs[i], tCause);
355            assertNull("getCertPath() must return null", tE.getCertPath());
356        }
357
358        tE = new CertPathValidatorException(null, null, null, -1);
359        assertNull("getCertPath() must return null", tE.getCertPath());
360
361        for (int i = 0; i < msgs.length; i++) {
362            try {
363                tE = new CertPathValidatorException(msgs[i], tCause, null, -1);
364                assertNull("getCertPath() must return null", tE.getCertPath());
365            } catch (IndexOutOfBoundsException e) {
366                fail("Unexpected exception: " + e.getMessage());
367            }
368        }
369
370        myCertPath mcp = new myCertPath("X.509", "");
371        CertPath cp = mcp.get("X.509");
372        for (int i = 0; i < msgs.length; i++) {
373            try {
374                tE = new CertPathValidatorException(msgs[i], tCause, cp, -1);
375                assertNotNull("getCertPath() must not return null", tE
376                        .getCertPath());
377                assertEquals(
378                        "getCertPath() must return ".concat(cp.toString()), tE
379                                .getCertPath(), cp);
380            } catch (IndexOutOfBoundsException e) {
381                fail("Unexpected IndexOutOfBoundsException was thrown. "
382                        + e.toString());
383            }
384        }
385    }
386
387    /**
388     * Test for <code>getIndex()</code>. Returns the index of the certificate
389     * in the certification path that caused the exception to be thrown. Note
390     * that the list of certificates in a CertPath is zero based. If no index
391     * has been set, -1 is returned.
392     */
393    public void testCertPathValidatorException16() {
394        CertPathValidatorException tE = new CertPathValidatorException();
395        assertEquals("getIndex() must be equals -1", -1, tE.getIndex());
396
397        for (int i = 0; i < msgs.length; i++) {
398            tE = new CertPathValidatorException(msgs[i]);
399            assertEquals("getIndex() must be equals -1", -1, tE.getIndex());
400        }
401
402        Throwable cause = null;
403        tE = new CertPathValidatorException(cause);
404        assertEquals("getIndex() must be equals -1", -1, tE.getIndex());
405
406        tE = new CertPathValidatorException(tCause);
407        assertEquals("getIndex() must be equals -1", -1, tE.getIndex());
408
409        for (int i = 0; i < msgs.length; i++) {
410            tE = new CertPathValidatorException(msgs[i], tCause);
411            assertEquals("getIndex() must be equals -1", -1, tE.getIndex());
412        }
413
414        tE = new CertPathValidatorException(null, null, null, -1);
415        assertEquals("getIndex() must be equals -1", -1, tE.getIndex());
416
417        myCertPath mcp = new myCertPath("X.509", "");
418        CertPath cp = mcp.get("X.509");
419        for (int i = 0; i < msgs.length; i++) {
420            try {
421                tE = new CertPathValidatorException(msgs[i], tCause, cp, -1);
422                assertNotNull("getIndex() must not return null", tE
423                        .getCertPath());
424                assertEquals(
425                        "getIndex() must return ".concat(cp.toString()), tE
426                                .getCertPath(), cp);
427            } catch (IndexOutOfBoundsException e) {
428                fail("Unexpected IndexOutOfBoundsException was thrown. "
429                        + e.getMessage());
430            }
431        }
432    }
433
434    class myCertPath extends CertPath {
435
436        private static final long serialVersionUID = 5871603047244722511L;
437
438        public List<Certificate> getCertificates() {
439            return new Vector<Certificate>();
440        }
441
442        public byte[] getEncoded() {
443            return new byte[0];
444        }
445
446        public byte[] getEncoded(String s) {
447            return new byte[0];
448        }
449
450        public Iterator<String> getEncodings() {
451            return (Iterator<String>) (new StringTokenizer("ss ss ss ss"));
452        }
453
454        protected myCertPath(String s) {
455            super(s);
456        }
457
458        public CertPath get(String s) {
459            return new myCertPath(s);
460        }
461
462        public myCertPath(String s, String s1) {
463            super(s);
464        }
465
466    }
467}
468
469
470