Skip to content

Commit 04cbf14

Browse files
committed
add test
1 parent 1cfbaae commit 04cbf14

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import sun.security.util.CurveDB;
25+
26+
import javax.crypto.SecretKeyFactory;
27+
import javax.crypto.spec.DESKeySpec;
28+
import javax.crypto.spec.DESedeKeySpec;
29+
import javax.crypto.spec.DHParameterSpec;
30+
import javax.crypto.spec.GCMParameterSpec;
31+
import javax.crypto.spec.IvParameterSpec;
32+
import javax.crypto.spec.OAEPParameterSpec;
33+
import javax.crypto.spec.PBEKeySpec;
34+
import javax.crypto.spec.PBEParameterSpec;
35+
import javax.crypto.spec.RC2ParameterSpec;
36+
import javax.crypto.spec.SecretKeySpec;
37+
import java.math.BigInteger;
38+
import java.nio.charset.StandardCharsets;
39+
import java.security.AlgorithmParameters;
40+
import java.security.spec.AlgorithmParameterSpec;
41+
import java.security.spec.DSAParameterSpec;
42+
import java.security.spec.ECGenParameterSpec;
43+
import java.security.spec.ECParameterSpec;
44+
import java.security.spec.KeySpec;
45+
import java.security.spec.PSSParameterSpec;
46+
47+
/**
48+
* @test
49+
* @bug 8279800
50+
* @modules java.base/sun.security.util
51+
* @summary isAssignableFrom checks in AlgorithmParametersSpi.engineGetParameterSpec appear to be backwards
52+
*/
53+
54+
public class IsAssignableFromOrder {
55+
56+
public static void main(String[] args) throws Exception {
57+
58+
// AlgorithmParameters
59+
testAlgSpec("AES", new IvParameterSpec(new byte[16]));
60+
testAlgSpec("ChaCha20-Poly1305", new IvParameterSpec(new byte[12]));
61+
testAlgSpec("DiffieHellman", new DHParameterSpec(BigInteger.ONE, BigInteger.TWO));
62+
testAlgSpec("GCM", new GCMParameterSpec(96, new byte[16]));
63+
testAlgSpec("OAEP", OAEPParameterSpec.DEFAULT);
64+
testAlgSpec("PBEWithSHA1AndDESede", new PBEParameterSpec(
65+
"saltsalt".getBytes(StandardCharsets.UTF_8), 10000));
66+
testAlgSpec("PBEWithHmacSHA256AndAES_256", new PBEParameterSpec(
67+
"saltsalt".getBytes(StandardCharsets.UTF_8), 10000,
68+
new IvParameterSpec(new byte[16])));
69+
testAlgSpec("RC2", new RC2ParameterSpec(256, new byte[32]));
70+
testAlgSpec("DSA", new DSAParameterSpec(
71+
BigInteger.ONE, BigInteger.TWO, BigInteger.TEN));
72+
testAlgSpec("RSASSA-PSS", PSSParameterSpec.DEFAULT);
73+
testAlgSpec("EC", new ECGenParameterSpec("secp256r1"));
74+
testAlgSpec("EC", CurveDB.lookup("secp256r1"),
75+
ECParameterSpec.class, AlgorithmParameterSpec.class);
76+
77+
// SecretKeyFactory
78+
var spec = new PBEKeySpec(
79+
"password".toCharArray(),
80+
"saltsalt".getBytes(StandardCharsets.UTF_8),
81+
10000,
82+
32);
83+
84+
testKeySpec("PBE", spec, PBEKeySpec.class);
85+
testKeySpec("PBEWithHmacSHA256AndAES_256", spec, PBEKeySpec.class);
86+
testKeySpec("PBKDF2WithHmacSHA1", spec, PBEKeySpec.class);
87+
88+
testKeySpec("DES", new SecretKeySpec(new byte[8], "DES"), DESKeySpec.class);
89+
testKeySpec("DESede", new SecretKeySpec(new byte[24], "DESede"), DESedeKeySpec.class);
90+
}
91+
92+
93+
static void testAlgSpec(String algorithm, AlgorithmParameterSpec spec,
94+
Class<? extends AlgorithmParameterSpec>... classes) throws Exception {
95+
System.out.println(algorithm);
96+
var ap1 = AlgorithmParameters.getInstance(algorithm);
97+
ap1.init(spec);
98+
var ap2 = AlgorithmParameters.getInstance(algorithm);
99+
ap2.init(ap1.getEncoded());
100+
if (classes.length == 0) {
101+
classes = new Class[]{spec.getClass(), AlgorithmParameterSpec.class};
102+
}
103+
for (var c : classes) {
104+
ap1.getParameterSpec(c);
105+
ap2.getParameterSpec(c);
106+
}
107+
}
108+
109+
static void testKeySpec(String algorithm, KeySpec spec, Class<?> clazz)
110+
throws Exception {
111+
System.out.println(algorithm);
112+
var kf = SecretKeyFactory.getInstance(algorithm);
113+
var key = kf.generateSecret(spec);
114+
kf.getKeySpec(key, KeySpec.class);
115+
kf.getKeySpec(key, clazz);
116+
}
117+
}

0 commit comments

Comments
 (0)