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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/*
* 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 dalvik.annotation.KnownFailure;
import org.apache.harmony.security.tests.support.SpiEngUtils;
import org.apache.harmony.security.tests.support.cert.MyCertPathBuilderSpi;
import org.apache.harmony.security.tests.support.cert.TestUtils;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.Security;
import java.security.cert.CertPath;
import java.security.cert.CertPathBuilder;
import java.security.cert.CertPathBuilderException;
import java.security.cert.CertPathBuilderResult;
import java.security.cert.CertPathBuilderSpi;
import java.security.cert.CertPathParameters;
import java.security.cert.CertificateException;
import junit.framework.TestCase;
/**
* Tests for <code>CertPathBuilder</code> class constructors and
* methods.
*
*/
public class CertPathBuilder1Test extends TestCase {
public static final String srvCertPathBuilder = "CertPathBuilder";
public 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 = "";
public static final String DEFAULT_TYPE_PROPERTY = "certpathbuilder.type";
static {
defaultProvider = SpiEngUtils.isSupport(defaultType,
srvCertPathBuilder);
PKIXSupport = (defaultProvider != null);
defaultProviderName = (PKIXSupport ? defaultProvider.getName() : null);
NotSupportMsg = defaultType.concat(" is not supported");
}
private static CertPathBuilder[] createCPBs() {
if (!PKIXSupport) {
fail(NotSupportMsg);
return null;
}
try {
CertPathBuilder[] certPBs = new CertPathBuilder[3];
certPBs[0] = CertPathBuilder.getInstance(defaultType);
certPBs[1] = CertPathBuilder.getInstance(defaultType,
defaultProviderName);
certPBs[2] = CertPathBuilder.getInstance(defaultType,
defaultProvider);
return certPBs;
} catch (Exception e) {
return null;
}
}
/**
* java.security.cert.CertPathBuilder#getDefaultType()
*/
public void test_getDefaultType() throws Exception {
// Regression for HARMONY-2785
// test: default value
assertNull(Security.getProperty(DEFAULT_TYPE_PROPERTY));
assertEquals("PKIX", CertPathBuilder.getDefaultType());
}
/**
* Test for <code>getInstance(String algorithm)</code> method
* Assertion:
* throws NullPointerException when algorithm is null
* throws NoSuchAlgorithmException when algorithm is not correct
* or it is not available
*/
public void testCertPathBuilder02() throws NoSuchAlgorithmException {
try {
CertPathBuilder.getInstance(null);
fail("No expected NullPointerException");
} catch (NullPointerException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
CertPathBuilder.getInstance(invalidValues[i]);
fail("NoSuchAlgorithmException must be thrown");
} catch (NoSuchAlgorithmException e) {
}
}
}
/**
* Test for <code>getInstance(String algorithm)</code> method
* Assertion: returns CertPathBuilder object
*/
public void testCertPathBuilder03() throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
for (int i = 0; i < validValues.length; i++) {
CertPathBuilder cpb = CertPathBuilder.getInstance(validValues[i]);
assertEquals("Incorrect algorithm", cpb.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 testCertPathBuilder04()
throws NoSuchAlgorithmException, NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
String provider = null;
for (int i = 0; i < validValues.length; i++) {
try {
CertPathBuilder.getInstance(validValues[i], provider);
fail("IllegalArgumentException must be thrown thrown");
} catch (IllegalArgumentException e) {
}
try {
CertPathBuilder.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 testCertPathBuilder05()
throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
for (int i = 0; i < validValues.length; i++ ) {
for (int j = 1; j < invalidValues.length; j++) {
try {
CertPathBuilder.getInstance(validValues[i], invalidValues[j]);
fail("NoSuchProviderException must be hrown");
} 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 correct
*/
public void testCertPathBuilder06()
throws NoSuchAlgorithmException, NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
try {
CertPathBuilder.getInstance(null, defaultProviderName);
fail("No expected NullPointerException");
} catch (NullPointerException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
CertPathBuilder.getInstance(invalidValues[i], defaultProviderName);
fail("NoSuchAlgorithmException must be thrown");
} catch (NoSuchAlgorithmException e1) {
}
}
}
/**
* Test for <code>getInstance(String algorithm, String provider)</code> method
* Assertion: returns CertPathBuilder object
*/
public void testCertPathBuilder07()
throws NoSuchAlgorithmException, NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathBuilder certPB;
for (int i = 0; i < validValues.length; i++) {
certPB = CertPathBuilder.getInstance(validValues[i], defaultProviderName);
assertEquals("Incorrect algorithm", certPB.getAlgorithm(), validValues[i]);
assertEquals("Incorrect provider name", certPB.getProvider().getName(), defaultProviderName);
}
}
/**
* Test for <code>getInstance(String algorithm, Provider provider)</code> method
* Assertion: throws IllegalArgumentException when provider is null
*/
public void testCertPathBuilder08()
throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
Provider prov = null;
for (int t = 0; t < validValues.length; t++ ) {
try {
CertPathBuilder.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 correct
*/
public void testCertPathBuilder09()
throws NoSuchAlgorithmException, NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
try {
CertPathBuilder.getInstance(null, defaultProvider);
fail("No expected NullPointerException");
} catch (NullPointerException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
CertPathBuilder.getInstance(invalidValues[i], defaultProvider);
fail("NoSuchAlgorithm must be thrown");
} catch (NoSuchAlgorithmException e1) {
}
}
}
/**
* Test for <code>getInstance(String algorithm, String provider)</code> method
* Assertion: returns CertPathBuilder object
*/
public void testCertPathBuilder10()
throws NoSuchAlgorithmException, NoSuchProviderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathBuilder certPB;
for (int i = 0; i < invalidValues.length; i++) {
certPB = CertPathBuilder.getInstance(validValues[i], defaultProvider);
assertEquals("Incorrect algorithm", certPB.getAlgorithm(), validValues[i]);
assertEquals("Incorrect provider name", certPB.getProvider(), defaultProvider);
}
}
/**
* Test for <code>build(CertPathParameters params)</code> method
* Assertion: throws InvalidAlgorithmParameterException params is null
*/
public void testCertPathBuilder11()
throws NoSuchAlgorithmException, NoSuchProviderException,
CertPathBuilderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathBuilder [] certPB = createCPBs();
assertNotNull("CertPathBuilder objects were not created", certPB);
for (int i = 0; i < certPB.length; i++ ){
try {
certPB[i].build(null);
fail("InvalidAlgorithmParameterException must be thrown");
} catch(InvalidAlgorithmParameterException e) {
}
}
}
// Test passed on RI
@KnownFailure(value="expired certificate bug 2322662")
public void testBuild() throws Exception {
TestUtils.initCertPathSSCertChain();
CertPathParameters params = TestUtils.getCertPathParameters();
CertPathBuilder builder = TestUtils.getCertPathBuilder();
try {
CertPathBuilderResult result = builder.build(params);
assertNotNull("builder result is null", result);
CertPath certPath = result.getCertPath();
assertNotNull("certpath of builder result is null", certPath);
} catch (InvalidAlgorithmParameterException e) {
fail("unexpected Exception: " + e);
}
}
/**
* Test for
* <code>CertPathBuilder</code> constructor
* Assertion: returns CertPathBuilder object
*/
public void testCertPathBuilder12()
throws CertificateException, NoSuchProviderException,
NoSuchAlgorithmException, InvalidAlgorithmParameterException,
CertPathBuilderException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
CertPathBuilderSpi spi = new MyCertPathBuilderSpi();
CertPathBuilder certPB = new myCertPathBuilder(spi,
defaultProvider, defaultType);
assertEquals("Incorrect algorithm", certPB.getAlgorithm(), defaultType);
assertEquals("Incorrect provider", certPB.getProvider(), defaultProvider);
try {
certPB.build(null);
fail("CertPathBuilderException must be thrown ");
} catch (CertPathBuilderException e) {
}
certPB = new myCertPathBuilder(null, null, null);
assertNull("Incorrect algorithm", certPB.getAlgorithm());
assertNull("Incorrect provider", certPB.getProvider());
try {
certPB.build(null);
fail("NullPointerException must be thrown ");
} catch (NullPointerException e) {
}
}
/**
* Test for <code>getAlgorithm()</code> method Assertion: returns
* CertPathBuilder object
*/
public void testCertPathBuilder13() throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
for (int i = 0; i < validValues.length; i++) {
CertPathBuilder cpb = CertPathBuilder.getInstance(validValues[i]);
assertEquals("Incorrect algorithm", cpb.getAlgorithm(),
validValues[i]);
try {
cpb = CertPathBuilder.getInstance(validValues[i],
defaultProviderName);
assertEquals("Incorrect algorithm", cpb.getAlgorithm(),
validValues[i]);
} catch (NoSuchProviderException e) {
fail("Unexpected NoSuchProviderException exeption "
+ e.getMessage());
}
try {
cpb = CertPathBuilder.getInstance(validValues[i],
defaultProviderName);
assertEquals("Incorrect algorithm", cpb.getAlgorithm(),
validValues[i]);
} catch (NoSuchProviderException e) {
fail("Unexpected NoSuchProviderException " + e.getMessage());
}
}
}
/**
* Test for <code>getProvider()</code> method Assertion: returns
* CertPathBuilder object
*/
public void testCertPathBuilder14() throws NoSuchAlgorithmException {
if (!PKIXSupport) {
fail(NotSupportMsg);
return;
}
for (int i = 0; i < validValues.length; i++) {
CertPathBuilder cpb2 = CertPathBuilder.getInstance(validValues[i],
defaultProvider);
assertEquals("Incorrect provider", cpb2.getProvider(),
defaultProvider);
try {
CertPathBuilder cpb3 = CertPathBuilder.getInstance(
validValues[i], defaultProviderName);
assertEquals("Incorrect provider", cpb3.getProvider(),
defaultProvider);
} catch (NoSuchProviderException e) {
fail("Unexpected NoSuchProviderException " + e.getMessage());
}
}
}
}
/**
* Additional class to verify CertPathBuilder constructor
*/
class myCertPathBuilder extends CertPathBuilder {
private static Provider provider;
public myCertPathBuilder(CertPathBuilderSpi spi, Provider prov, String type) {
super(spi, prov, type);
}
public static CertPathBuilder getInstance(String algorithm)
throws NoSuchAlgorithmException {
myCertPathBuilder mcpb = new myCertPathBuilder(null, null, null);
provider = mcpb.new MyProvider();
return CertPathBuilder.getInstance(algorithm);
}
public Provider getMyProvider() {
return provider;
}
public class MyProvider extends Provider {
private static final long serialVersionUID = -6537447905658191184L;
MyProvider() {
super("MyProvider", 1.0, "Provider for testing");
}
MyProvider(String name, double version, String info) {
super(name, version, info);
}
public void putService(Provider.Service s) {
super.putService(s);
}
public void removeService(Provider.Service s) {
super.removeService(s);
}
}
}
|