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