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*/ 21 22package org.apache.harmony.security.tests.java.security.cert; 23 24import java.security.cert.CertPathValidatorException; 25import java.security.cert.CertPath; 26import java.util.Iterator; 27import java.util.List; 28import java.util.StringTokenizer; 29import java.util.Vector; 30 31import junit.framework.TestCase; 32 33 34/** 35 * Tests for <code>CertPathValidatorException</code> class constructors and 36 * methods. 37 * 38 */ 39public class CertPathValidatorExceptionTest extends TestCase { 40 41 public static void main(String[] args) { 42 } 43 44 /** 45 * Constructor for CertPathValidatorExceptionTests. 46 * 47 * @param arg0 48 */ 49 public CertPathValidatorExceptionTest(String arg0) { 50 super(arg0); 51 } 52 53 private static String[] msgs = { 54 "", 55 "Check new message", 56 "Check new message Check new message Check new message Check new message Check new message" }; 57 58 private static Throwable tCause = new Throwable("Throwable for exception"); 59 60 /** 61 * Test for <code>CertPathValidatorException()</code> constructor 62 * Assertion: constructs CertPathValidatorException with no detail message 63 */ 64 public void testCertPathValidatorException01() { 65 CertPathValidatorException tE = new CertPathValidatorException(); 66 assertNull("getMessage() must return null.", tE.getMessage()); 67 assertNull("getCause() must return null", tE.getCause()); 68 } 69 70 /** 71 * Test for <code>CertPathValidatorException(String)</code> constructor 72 * Assertion: constructs CertPathValidatorException with detail message msg. 73 * Parameter <code>msg</code> is not null. 74 */ 75 public void testCertPathValidatorException02() { 76 CertPathValidatorException tE; 77 for (int i = 0; i < msgs.length; i++) { 78 tE = new CertPathValidatorException(msgs[i]); 79 assertEquals("getMessage() must return: ".concat(msgs[i]), tE 80 .getMessage(), msgs[i]); 81 assertNull("getCause() must return null", tE.getCause()); 82 } 83 } 84 85 /** 86 * Test for <code>CertPathValidatorException(String)</code> constructor 87 * Assertion: constructs CertPathValidatorException when <code>msg</code> 88 * is null 89 */ 90 public void testCertPathValidatorException03() { 91 String msg = null; 92 CertPathValidatorException tE = new CertPathValidatorException(msg); 93 assertNull("getMessage() must return null.", tE.getMessage()); 94 assertNull("getCause() must return null", tE.getCause()); 95 } 96 97 /** 98 * Test for <code>CertPathValidatorException(Throwable)</code> constructor 99 * Assertion: constructs CertPathValidatorException when <code>cause</code> 100 * is null 101 */ 102 public void testCertPathValidatorException04() { 103 Throwable cause = null; 104 CertPathValidatorException tE = new CertPathValidatorException(cause); 105 assertNull("getMessage() must return null.", tE.getMessage()); 106 assertNull("getCause() must return null", tE.getCause()); 107 } 108 109 /** 110 * Test for <code>CertPathValidatorException(Throwable)</code> constructor 111 * Assertion: constructs CertPathValidatorException when <code>cause</code> 112 * is not null 113 */ 114 public void testCertPathValidatorException05() { 115 CertPathValidatorException tE = new CertPathValidatorException(tCause); 116 if (tE.getMessage() != null) { 117 String toS = tCause.toString(); 118 String getM = tE.getMessage(); 119 assertTrue("getMessage() should contain ".concat(toS), (getM 120 .indexOf(toS) != -1)); 121 } 122 assertNotNull("getCause() must not return null", tE.getCause()); 123 assertEquals("getCause() must return ".concat(tCause.toString()), tE 124 .getCause(), tCause); 125 } 126 127 /** 128 * Test for <code>CertPathValidatorException(String, Throwable)</code> 129 * constructor Assertion: constructs CertPathValidatorException when 130 * <code>cause</code> is null <code>msg</code> is null 131 */ 132 public void testCertPathValidatorException06() { 133 CertPathValidatorException tE = new CertPathValidatorException(null, 134 null); 135 assertNull("getMessage() must return null", tE.getMessage()); 136 assertNull("getCause() must return null", tE.getCause()); 137 } 138 139 /** 140 * Test for <code>CertPathValidatorException(String, Throwable)</code> 141 * constructor Assertion: constructs CertPathValidatorException when 142 * <code>cause</code> is null <code>msg</code> is not null 143 */ 144 public void testCertPathValidatorException07() { 145 CertPathValidatorException tE; 146 for (int i = 0; i < msgs.length; i++) { 147 tE = new CertPathValidatorException(msgs[i], null); 148 assertEquals("getMessage() must return: ".concat(msgs[i]), tE 149 .getMessage(), msgs[i]); 150 assertNull("getCause() must return null", tE.getCause()); 151 } 152 } 153 154 /** 155 * Test for <code>CertPathValidatorException(String, Throwable)</code> 156 * constructor Assertion: constructs CertPathValidatorException when 157 * <code>cause</code> is not null <code>msg</code> is null 158 */ 159 public void testCertPathValidatorException08() { 160 CertPathValidatorException tE = new CertPathValidatorException(null, 161 tCause); 162 if (tE.getMessage() != null) { 163 String toS = tCause.toString(); 164 String getM = tE.getMessage(); 165 assertTrue("getMessage() must should ".concat(toS), (getM 166 .indexOf(toS) != -1)); 167 } 168 assertNotNull("getCause() must not return null", tE.getCause()); 169 assertEquals("getCause() must return ".concat(tCause.toString()), tE 170 .getCause(), tCause); 171 } 172 173 /** 174 * Test for <code>CertPathValidatorException(String, Throwable)</code> 175 * constructor Assertion: constructs CertPathValidatorException when 176 * <code>cause</code> is not null <code>msg</code> is not null 177 */ 178 public void testCertPathValidatorException09() { 179 CertPathValidatorException tE; 180 for (int i = 0; i < msgs.length; i++) { 181 tE = new CertPathValidatorException(msgs[i], tCause); 182 String getM = tE.getMessage(); 183 String toS = tCause.toString(); 184 if (msgs[i].length() > 0) { 185 assertTrue("getMessage() must contain ".concat(msgs[i]), getM 186 .indexOf(msgs[i]) != -1); 187 if (!getM.equals(msgs[i])) { 188 assertTrue("getMessage() should contain ".concat(toS), getM 189 .indexOf(toS) != -1); 190 } 191 } 192 assertNotNull("getCause() must not return null", tE.getCause()); 193 assertEquals("getCause() must return ".concat(tCause.toString()), 194 tE.getCause(), tCause); 195 } 196 } 197 198 /** 199 * Test for 200 * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code> 201 * constructor Assertion: constructs CertPathValidatorException when 202 * <code>cause</code> is null <code>msg</code> is null 203 * <code>certPath</code> is null <code>index</code> is -1 204 */ 205 public void testCertPathValidatorException10() { 206 CertPathValidatorException tE = new CertPathValidatorException(null, 207 null, null, -1); 208 assertNull("getMessage() must return null", tE.getMessage()); 209 assertNull("getCause() must return null", tE.getCause()); 210 assertNull("getCertPath() must return null", tE.getCertPath()); 211 assertEquals("getIndex() must be -1", tE.getIndex(), -1); 212 } 213 214 /** 215 * Test for 216 * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code> 217 * constructor Assertion: constructs CertPathValidatorException when 218 * <code>cause</code> is null <code>msg</code> is null 219 * <code>certPath</code> is null <code>index</code> not -1 throws: 220 * IllegalArgumentException 221 */ 222 public void testCertPathValidatorException11() { 223 int[] indx = { 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE }; 224 for (int j = 0; j < indx.length; j++) { 225 for (int i = 0; i < msgs.length; i++) { 226 try { 227 new CertPathValidatorException(msgs[i], tCause, null, indx[j]); 228 fail("Error. IllegalArgumentException was not thrown as expected. " 229 + " msg: " 230 + msgs[i] 231 + ", certPath is null and index is " + indx[j]); 232 } catch (IllegalArgumentException e) { 233 } 234 } 235 } 236 } 237 238 /** 239 * Test for 240 * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code> 241 * constructor Assertion: constructs CertPathValidatorException when 242 * <code>cause</code> not null <code>msg</code> not null 243 * <code>certPath</code> is null <code>index</code> is -1 244 */ 245 public void testCertPathValidatorException12() { 246 CertPathValidatorException tE; 247 248 for (int i = 0; i < msgs.length; i++) { 249 try { 250 tE = new CertPathValidatorException(msgs[i], tCause, null, -1); 251 String getM = tE.getMessage(); 252 String toS = tCause.toString(); 253 if (msgs[i].length() > 0) { 254 assertTrue("getMessage() must contain ".concat(msgs[i]), 255 getM.indexOf(msgs[i]) != -1); 256 if (!getM.equals(msgs[i])) { 257 assertTrue("getMessage() should contain ".concat(toS), 258 getM.indexOf(toS) != -1); 259 } 260 } 261 assertNotNull("getCause() must not return null", tE.getCause()); 262 assertEquals("getCause() must return " 263 .concat(tCause.toString()), tE.getCause(), tCause); 264 assertNull("getCertPath() must return null", tE.getCertPath()); 265 assertEquals("getIndex() must return -1", tE.getIndex(), -1); 266 } catch (IndexOutOfBoundsException e) { 267 fail("Unexpected exception: " + e.toString() 268 + " Parameters: msg: " + msgs[i] 269 + ", certPath is null and index is -1"); 270 } 271 } 272 } 273 274 /** 275 * Test for 276 * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code> 277 * constructor Assertion: constructs CertPathValidatorException when 278 * <code>cause</code> not null <code>msg</code> not null 279 * <code>certPath</code> not null <code>index</code>< -1 || >= 280 * certPath.getCertificates().size() throws: IndexOutOfBoundsException 281 */ 282 public void testCertPathValidatorException13() { 283 myCertPath mcp = new myCertPath("X.509", ""); 284 CertPath cp = mcp.get("X.509"); 285 int[] indx = { -2, -100, 0, 1, 100, Integer.MAX_VALUE, 286 Integer.MIN_VALUE }; 287 for (int j = 0; j < indx.length; j++) { 288 for (int i = 0; i < msgs.length; i++) { 289 try { 290 new CertPathValidatorException(msgs[i], tCause, cp, indx[j]); 291 fail("IndexOutOfBoundsException was not thrown as expected. " 292 + " msg: " 293 + msgs[i] 294 + ", certPath is null and index is " + indx[j]); 295 } catch (IndexOutOfBoundsException e) { 296 } 297 } 298 } 299 } 300 301 /** 302 * Test for 303 * <code>CertPathValidatorException(String, Throwable, CertPath, int)</code> 304 * constructor Assertion: constructs CertPathValidatorException when 305 * <code>cause</code> not null <code>msg</code> not null 306 * <code>certPath</code> not null <code>index</code>< 307 * certPath.getCertificates().size() 308 */ 309 public void testCertPathValidatorException14() { 310 CertPathValidatorException tE; 311 myCertPath mcp = new myCertPath("X.509", ""); 312 CertPath cp = mcp.get("X.509"); 313 for (int i = 0; i < msgs.length; i++) { 314 try { 315 tE = new CertPathValidatorException(msgs[i], tCause, cp, -1); 316 String getM = tE.getMessage(); 317 String toS = tCause.toString(); 318 if (msgs[i].length() > 0) { 319 assertTrue("getMessage() must contain ".concat(msgs[i]), 320 getM.indexOf(msgs[i]) != -1); 321 if (!getM.equals(msgs[i])) { 322 assertTrue("getMessage() should contain ".concat(toS), 323 getM.indexOf(toS) != -1); 324 } 325 } 326 assertNotNull("getCause() must not return null", tE.getCause()); 327 assertEquals("getCause() must return " 328 .concat(tCause.toString()), tE.getCause(), tCause); 329 assertNotNull("getCertPath() must not return null", tE 330 .getCertPath()); 331 assertEquals( 332 "getCertPath() must return ".concat(cp.toString()), tE 333 .getCertPath(), cp); 334 assertEquals("getIndex() must return -1", tE.getIndex(), -1); 335 336 } catch (IndexOutOfBoundsException e) { 337 fail("Unexpected IndexOutOfBoundsException was thrown. " 338 + e.toString()); 339 } 340 } 341 } 342 343 class myCertPath extends CertPath { 344 345 public List getCertificates() { 346 return new Vector(); 347 } 348 349 public byte[] getEncoded() { 350 return new byte[0]; 351 } 352 353 public byte[] getEncoded(String s) { 354 return new byte[0]; 355 } 356 357 public Iterator getEncodings() { 358 return (Iterator) (new StringTokenizer("ss ss ss ss")); 359 } 360 361 protected myCertPath(String s) { 362 super(s); 363 } 364 365 public CertPath get(String s) { 366 return new myCertPath(s); 367 } 368 369 public myCertPath(String s, String s1) { 370 super(s); 371 } 372 373 } 374} 375 376 377