01: package org.bouncycastle.jce.provider.test;
02:
03: import org.bouncycastle.jce.provider.BouncyCastleProvider;
04: import org.bouncycastle.util.encoders.Base64;
05: import org.bouncycastle.util.test.SimpleTest;
06:
07: import java.io.IOException;
08: import java.security.AlgorithmParameters;
09: import java.security.Security;
10: import java.security.spec.AlgorithmParameterSpec;
11: import java.security.spec.DSAParameterSpec;
12: import java.security.spec.InvalidParameterSpecException;
13:
14: public class AlgorithmParametersTest extends SimpleTest {
15: private byte[] dsaParams = Base64
16: .decode("MIGcAkEAjfKklEkidqo9JXWbsGhpy+rA2Dr7jQz3y7gyTw14guXQdi/FtyEOr8Lprawyq3qsSWk9+/g3J"
17: + "MLsBzbuMcgCkQIVAMdzIYxzfsjumTtPLe0w9I7azpFfAkBP3Z9K7oNeZMXEXYpqvrMUgVdFjq4lnWJoV8"
18: + "Rwe+TERStHTkqSO7sp0lq7EEggVMcuXtarKNsxaJ+qyYv/n1t6");
19:
20: private void basicTest(String algorithm,
21: Class algorithmParameterSpec, byte[] asn1Encoded)
22: throws Exception {
23: AlgorithmParameters alg = AlgorithmParameters.getInstance(
24: algorithm, "BC");
25:
26: alg.init(asn1Encoded);
27:
28: try {
29: alg.init(asn1Encoded);
30: fail("encoded re-initialization not detected");
31: } catch (IOException e) {
32: // expected already initialized
33: }
34:
35: AlgorithmParameterSpec spec = alg
36: .getParameterSpec(algorithmParameterSpec);
37:
38: try {
39: alg.init(spec);
40: fail("spec re-initialization not detected");
41: } catch (InvalidParameterSpecException e) {
42: // expected already initialized
43: }
44:
45: try {
46: spec = alg.getParameterSpec(AlgorithmParameterSpec.class);
47: fail("wrong spec not detected");
48: } catch (InvalidParameterSpecException e) {
49: // expected unknown object
50: }
51:
52: try {
53: spec = alg.getParameterSpec(null);
54: fail("null spec not detected");
55: } catch (NullPointerException e) {
56: // expected unknown object
57: }
58:
59: alg = AlgorithmParameters.getInstance(algorithm, "BC");
60:
61: alg.init(asn1Encoded, "ASN.1");
62:
63: alg = AlgorithmParameters.getInstance(algorithm, "BC");
64:
65: alg.init(asn1Encoded, null);
66:
67: alg = AlgorithmParameters.getInstance(algorithm, "BC");
68:
69: try {
70: alg.init(asn1Encoded, "FRED");
71: fail("unknown spec not detected");
72: } catch (IOException e) {
73: // expected already initialized
74: }
75: }
76:
77: public void performTest() throws Exception {
78: basicTest("DSA", DSAParameterSpec.class, dsaParams);
79: }
80:
81: public String getName() {
82: return "AlgorithmParameters";
83: }
84:
85: public static void main(String[] args) {
86: Security.addProvider(new BouncyCastleProvider());
87:
88: runTest(new AlgorithmParametersTest());
89: }
90: }
|