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