1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Vera Y. Petrashkova
* @version $Revision$
*/
package tests.security.cert;
import junit.framework.TestCase;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.Security;
import java.security.cert.CertPathParameters;
import java.security.cert.CertPathValidator;
import java.security.cert.CertPathValidatorException;
import java.security.cert.CertPathValidatorSpi;
import java.security.cert.CertificateException;
import org.apache.harmony.security.tests.support.cert.MyCertPath;
import org.apache.harmony.security.tests.support.cert.MyCertPathValidatorSpi;
import org.apache.harmony.security.tests.support.SpiEngUtils;
/**
* Tests for <code>CertPathValidator</code> class constructors and
* methods.
*
*/
public class CertPathValidator1Test extends TestCase {
public static final String srvCertPathValidator = "CertPathValidator";
private static final String defaultType = "PKIX";
public static final String [] validValues = {
"PKIX", "pkix", "PkiX", "pKiX" };
private static String [] invalidValues = SpiEngUtils.invalidValues;
private static boolean PKIXSupport = false;
private static Provider defaultProvider;
private static String defaultProviderName;
private static String NotSupportMsg = "";
static {
defaultProvider = SpiEngUtils.isSupport(defaultType,
srvCertPathValidator);
PKIXSupport = (defaultProvider != null);
defaultProviderName = (PKIXSupport ? defaultProvider.getName() : null);
NotSupportMsg = defaultType.concat(" is not supported");
}
private static CertPathValidator[] createCPVs() {
if (!PKIXSupport) {
fail(NotSupportMsg);
return null;
}
try {
CertPathValidator[] certPVs = new CertPathValidator[3];
certPVs[0] = CertPathValidator.getInstance(defaultType);
certPVs[1] = CertPathValidator.getInstance(defaultType,
defaultProviderName);
certPVs[2] = CertPathValidator.getInstance(defaultType,
defaultProvider);
return certPVs;
} catch (Exception e) {
return null;
}
}
/**
* Test for <code>getDefaultType()</code> method
* Assertion: returns security property "certpathvalidator.type" or "PKIX"
*/
public void testCertPathValidator01() {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
String propName = "certpathvalidator.type";
String defCPV = Security.getProperty(propName);
String dt = CertPathValidator.getDefaultType();
String resType = defCPV;
if (resType == null) {
resType = defaultType;
}
assertNotNull("Default type have not be null", dt);
assertEquals("Incorrect default type", dt, resType);
if (defCPV == null) {
Security.setProperty(propName, defaultType);
dt = CertPathValidator.getDefaultType();
resType = Security.getProperty(propName);
assertNotNull("Incorrect default type", resType);
assertNotNull("Default type have not be null", dt);
assertEquals("Incorrect default type", dt, resType);
}
}
/**
* Test for <code>getInstance(String algorithm)</code> method
* Assertion:
* throws NullPointerException when algorithm is null
* throws NoSuchAlgorithmException when algorithm is not available
*/
public void testCertPathValidator02() {
try {
CertPathValidator.getInstance(null);
fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
} catch (NullPointerException e) {
} catch (NoSuchAlgorithmException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
CertPathValidator.getInstance(invalidValues[i]);
fail("NoSuchAlgorithmException must be thrown");
} catch (NoSuchAlgorithmException e) {
}
}
}
/**
* Test for <code>getInstance(String algorithm)</code> method
* Assertion: returns CertPathValidator object
*/
public void testCertPathValidator03() throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathValidator certPV;
for (int i = 0; i < validValues.length; i++) {
certPV = CertPathValidator.getInstance(validValues[i]);
assertEquals("Incorrect algorithm", certPV.getAlgorithm(), validValues[i]);
}
}
/**
* Test for <code>getInstance(String algorithm, String provider)</code> method
* Assertion: throws IllegalArgumentException when provider is null or empty
*
* FIXME: verify what exception will be thrown if provider is empty
*/
public void testCertPathValidator04()
throws NoSuchAlgorithmException, NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
String provider = null;
for (int i = 0; i < validValues.length; i++) {
try {
CertPathValidator.getInstance(validValues[i], provider);
fail("IllegalArgumentException must be thrown thrown");
} catch (IllegalArgumentException e) {
}
try {
CertPathValidator.getInstance(validValues[i], "");
fail("IllegalArgumentException must be thrown thrown");
} catch (IllegalArgumentException e) {
}
}
}
/**
* Test for <code>getInstance(String algorithm, String provider)</code> method
* Assertion:
* throws NoSuchProviderException when provider has invalid value
*/
public void testCertPathValidator05() throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
for (int t = 0; t < validValues.length; t++) {
for (int i = 1; i < invalidValues.length; i++) {
try {
CertPathValidator.getInstance(validValues[t],
invalidValues[i]);
fail("NoSuchProviderException must be thrown");
} catch (NoSuchProviderException e1) {
}
}
}
}
/**
* Test for <code>getInstance(String algorithm, String provider)</code> method
* Assertion:
* throws NullPointerException when algorithm is null
* throws NoSuchAlgorithmException when algorithm is not available
*/
public void testCertPathValidator06()
throws NoSuchAlgorithmException, NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
try {
CertPathValidator.getInstance(null, defaultProviderName);
fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
} catch (NullPointerException e) {
} catch (NoSuchAlgorithmException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
CertPathValidator.getInstance(invalidValues[i], defaultProviderName);
fail("NoSuchAlgorithmException must be thrown");
} catch (NoSuchAlgorithmException e1) {
}
}
}
/**
* Test for <code>getInstance(String algorithm, String provider)</code> method
* Assertion: returns CertPathValidator object
*/
public void testCertPathValidator07() throws NoSuchAlgorithmException,
NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathValidator certPV;
for (int i = 0; i < validValues.length; i++) {
certPV = CertPathValidator.getInstance(validValues[i],
defaultProviderName);
assertEquals("Incorrect algorithm", certPV.getAlgorithm(),
validValues[i]);
assertEquals("Incorrect provider name", certPV.getProvider()
.getName(), defaultProviderName);
}
}
/**
* Test for <code>getInstance(String algorithm, Provider provider)</code> method
* Assertion: throws IllegalArgumentException when provider is null
*/
public void testCertPathValidator08()
throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
Provider prov = null;
for (int t = 0; t < validValues.length; t++ ) {
try {
CertPathValidator.getInstance(validValues[t], prov);
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e1) {
}
}
}
/**
* Test for <code>getInstance(String algorithm, String provider)</code> method
* Assertion:
* throws NullPointerException when algorithm is null
* throws NoSuchAlgorithmException when algorithm is not available
*/
public void testCertPathValidator09()
throws NoSuchAlgorithmException, NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
try {
CertPathValidator.getInstance(null, defaultProvider);
fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
} catch (NullPointerException e) {
} catch (NoSuchAlgorithmException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
CertPathValidator.getInstance(invalidValues[i], defaultProvider);
fail("NoSuchAlgorithm must be thrown");
} catch (NoSuchAlgorithmException e1) {
}
}
}
/**
* Test for <code>getInstance(String algorithm, String provider)</code> method
* Assertion: returns CertPathValidator object
*/
public void testCertPathValidator10() throws NoSuchAlgorithmException,
NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathValidator certPV;
for (int i = 0; i < invalidValues.length; i++) {
certPV = CertPathValidator.getInstance(validValues[i],
defaultProvider);
assertEquals("Incorrect algorithm", certPV.getAlgorithm(),
validValues[i]);
assertEquals("Incorrect provider name", certPV.getProvider(),
defaultProvider);
}
}
/**
* Test for <code>validate(CertPath certpath, CertPathParameters params)</code> method
* Assertion: throws InvalidAlgorithmParameterException params is not
* instance of PKIXParameters or null
*/
public void testCertPathValidator11()
throws NoSuchAlgorithmException, NoSuchProviderException, CertPathValidatorException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathValidator [] certPV = createCPVs();
assertNotNull("CertPathValidator objects were not created", certPV);
MyCertPath mCP = new MyCertPath(new byte[0]);
invalidParams mPar = new invalidParams();
for (int i = 0; i < certPV.length; i++) {
try {
certPV[i].validate(mCP, mPar);
fail("InvalidAlgorithmParameterException must be thrown");
} catch(InvalidAlgorithmParameterException e) {
}
try {
certPV[i].validate(mCP, null);
fail("InvalidAlgorithmParameterException must be thrown");
} catch(InvalidAlgorithmParameterException e) {
}
}
}
/**
* Test for
* <code>CertPathValidator</code> constructor
* Assertion: returns CertPathValidator object
*/
public void testCertPathValidator12()
throws CertificateException, NoSuchProviderException, NoSuchAlgorithmException,
CertPathValidatorException, InvalidAlgorithmParameterException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathValidatorSpi spi = new MyCertPathValidatorSpi();
CertPathValidator certPV = new myCertPathValidator(spi,
defaultProvider, defaultType);
assertEquals("Incorrect algorithm", certPV.getAlgorithm(), defaultType);
assertEquals("Incorrect provider", certPV.getProvider(), defaultProvider);
certPV.validate(null, null);
try {
certPV.validate(null, null);
fail("CertPathValidatorException must be thrown");
} catch (CertPathValidatorException e) {
}
certPV = new myCertPathValidator(null, null, null);
assertNull("Incorrect algorithm", certPV.getAlgorithm());
assertNull("Incorrect provider", certPV.getProvider());
try {
certPV.validate(null, null);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
}
/**
* Test for <code>getAlgorithm()</code> method
*/
public void testCertPathValidator13() throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathValidator certPV;
for (int i = 0; i < validValues.length; i++) {
certPV = CertPathValidator.getInstance(validValues[i]);
assertEquals("Incorrect algorithm", certPV.getAlgorithm(),
validValues[i]);
try {
certPV = CertPathValidator.getInstance(validValues[i],
defaultProviderName);
assertEquals("Incorrect algorithm", certPV.getAlgorithm(),
validValues[i]);
} catch (NoSuchProviderException e) {
fail("Unexpected NoSuchAlgorithmException " + e.getMessage());
}
certPV = CertPathValidator.getInstance(validValues[i],
defaultProvider);
assertEquals("Incorrect algorithm", certPV.getAlgorithm(),
validValues[i]);
}
}
/**
* Test for <code>getProvider()</code> method
*/
public void testCertPathValidator14() throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathValidator certPV;
for (int i = 0; i < validValues.length; i++) {
try {
certPV = CertPathValidator.getInstance(validValues[i],
defaultProviderName);
assertEquals("Incorrect provider", certPV.getProvider(),
defaultProvider);
} catch (NoSuchProviderException e) {
fail("Unexpected NoSuchProviderException " + e.getMessage());
}
certPV = CertPathValidator.getInstance(validValues[i],
defaultProvider);
assertEquals("Incorrect provider", certPV.getProvider(),
defaultProvider);
}
}
}
/**
* Additional class to verify CertPathValidator constructor
*/
class myCertPathValidator extends CertPathValidator {
public myCertPathValidator(CertPathValidatorSpi spi, Provider prov, String type) {
super(spi, prov, type);
}
}
/**
* Additional class to verify validate method
*/
class invalidParams implements CertPathParameters {
public Object clone() {
return new invalidParams();
}
}
|