Skip to content

Commit a0c8322

Browse files
committed
Add missing tests
1 parent da81fe1 commit a0c8322

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name = NSS
2+
3+
showInfo = true
4+
5+
slot = 1
6+
7+
library = ${pkcs11test.nss.lib}
8+
9+
disabledMechanisms = {
10+
CKM_SHA224_HMAC
11+
CKM_SHA256_HMAC
12+
}
13+
14+
nssArgs = "configdir='${pkcs11test.nss.db}' certPrefix='' keyPrefix=''"
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2024, 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+
/*
25+
* @test
26+
* @bug 8335288
27+
* @library /test/lib ..
28+
* @modules jdk.crypto.cryptoki
29+
* @summary check that if any required mech is unavailable, then the
30+
* mechanism will be unavailable as well.
31+
* @run testng/othervm RequiredMechCheck
32+
*/
33+
import java.nio.file.Path;
34+
import java.security.Provider;
35+
import java.security.NoSuchAlgorithmException;
36+
import javax.crypto.Cipher;
37+
import javax.crypto.Mac;
38+
import javax.crypto.SecretKeyFactory;
39+
40+
import jtreg.SkippedException;
41+
import org.testng.SkipException;
42+
import org.testng.annotations.BeforeClass;
43+
import org.testng.annotations.Test;
44+
45+
public class RequiredMechCheck extends PKCS11Test {
46+
47+
private static record TestData(String serviceType, String algo,
48+
boolean disabled) {}
49+
50+
private static TestData[] testValues = {
51+
new TestData("MAC", "HmacPBESHA1", false),
52+
new TestData("MAC", "HmacPBESHA224", true),
53+
new TestData("MAC", "HmacPBESHA256", true),
54+
new TestData("MAC", "HmacPBESHA384", false),
55+
new TestData("MAC", "HmacPBESHA512", false),
56+
new TestData("SKF", "PBEWithHmacSHA1AndAES_128", false),
57+
new TestData("SKF", "PBEWithHmacSHA224AndAES_128", true),
58+
new TestData("SKF", "PBEWithHmacSHA256AndAES_128", true),
59+
new TestData("SKF", "PBEWithHmacSHA384AndAES_128", false),
60+
new TestData("SKF", "PBEWithHmacSHA512AndAES_128", false),
61+
new TestData("CIP", "PBEWithHmacSHA1AndAES_128", false),
62+
new TestData("CIP", "PBEWithHmacSHA224AndAES_128", true),
63+
new TestData("CIP", "PBEWithHmacSHA256AndAES_128", true),
64+
new TestData("CIP", "PBEWithHmacSHA384AndAES_128", false),
65+
new TestData("CIP", "PBEWithHmacSHA512AndAES_128", false),
66+
};
67+
68+
@BeforeClass
69+
public void setUp() throws Exception {
70+
Path configPath = Path.of(BASE).resolve("RequiredMechCheck.cfg");
71+
System.setProperty("CUSTOM_P11_CONFIG", configPath.toString());
72+
}
73+
74+
@Test
75+
public void test() throws Exception {
76+
try {
77+
main(new RequiredMechCheck());
78+
} catch (SkippedException se) {
79+
throw new SkipException("One or more tests are skipped");
80+
}
81+
}
82+
83+
public void main(Provider p) throws Exception {
84+
for (TestData td : testValues) {
85+
String desc = td.serviceType + " " + td.algo;
86+
Object t;
87+
try {
88+
switch (td.serviceType) {
89+
case "MAC":
90+
t = Mac.getInstance(td.algo, p);
91+
break;
92+
case "SKF":
93+
t = SecretKeyFactory.getInstance(td.algo, p);
94+
break;
95+
case "CIP":
96+
t = Cipher.getInstance(td.algo, p);
97+
break;
98+
default:
99+
throw new RuntimeException("Unsupported Test Type!");
100+
}
101+
102+
if (td.disabled) {
103+
throw new RuntimeException("Fail, no NSAE for " + desc);
104+
} else {
105+
System.out.println("Ok, getInstance() works for " + desc);
106+
}
107+
} catch (NoSuchAlgorithmException e) {
108+
if (td.disabled) {
109+
System.out.println("Ok, NSAE thrown for " + desc);
110+
} else {
111+
throw new RuntimeException("Unexpected Ex for " + desc, e);
112+
}
113+
}
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)