Skip to content

Commit 161201f

Browse files
committed
part 2 of prev commit and minor cleanup
1 parent 1790e81 commit 161201f

File tree

3 files changed

+53
-67
lines changed

3 files changed

+53
-67
lines changed

test/jdk/sun/security/pkcs11/Cipher/TestCipherMode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void main(Provider p) throws Exception {
113113
}
114114

115115
if (!skipped.isEmpty()) {
116-
throw new SkippedException("Some tests failed: " + skipped);
116+
throw new SkippedException("Some tests skipped: " + skipped);
117117
} else {
118118
System.out.println("All tests passed");
119119
}

test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void main(Provider p) throws Exception {
127127
}
128128

129129
if (!skippedList.isEmpty()){
130-
throw new SkippedException("Some tests failed: " + skippedList);
130+
throw new SkippedException("Some tests skipped: " + skippedList);
131131
}
132132
}
133133

test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.security.AlgorithmParameters;
4242
import java.security.NoSuchAlgorithmException;
4343
import java.security.Provider;
44+
import java.util.ArrayList;
4445
import java.util.List;
4546
import java.util.Random;
4647
import javax.crypto.Cipher;
@@ -50,57 +51,68 @@
5051

5152
public class TestSymmCiphersNoPad extends PKCS11Test {
5253

53-
private static final StringBuffer debugBuf = new StringBuffer();
54+
private record CI (String transformation, String keyAlgo, int dataSize){} // record for holding Cipher Information
5455

55-
private final String transformation;
56-
private final String keyAlgo;
57-
private final int dataSize;
56+
private static final StringBuffer debugBuf = new StringBuffer();
5857

59-
public TestSymmCiphersNoPad(String transformation,
60-
String keyAlgo,
61-
int dataSize) {
62-
this.transformation = transformation;
63-
this.keyAlgo = keyAlgo;
64-
this.dataSize = dataSize;
65-
}
58+
private static final CI[] TEST_LIST = {
59+
new CI("ARCFOUR", "ARCFOUR", 400),
60+
new CI("RC4", "RC4", 401),
61+
new CI("DES/CBC/NoPadding", "DES", 400),
62+
new CI("DESede/CBC/NoPadding", "DESede", 160),
63+
new CI("AES/CBC/NoPadding", "AES", 4800),
64+
new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),
65+
new CI("AES/CTR/NoPadding", "AES", 1600),
66+
new CI("AES/CTR/NoPadding", "AES", 65),
67+
new CI("AES/CTS/NoPadding", "AES", 1600),
68+
new CI("AES/CTS/NoPadding", "AES", 65),
69+
};
6670

6771
@Override
6872
public void main(Provider p) throws Exception {
73+
List<CI> skippedList = new ArrayList<>();
6974
Random random = new Random();
7075
try {
71-
System.out.println("===" + transformation + "===");
72-
try {
73-
KeyGenerator kg =
74-
KeyGenerator.getInstance(keyAlgo, p);
75-
SecretKey key = kg.generateKey();
76-
Cipher c1 = Cipher.getInstance(transformation, p);
77-
Cipher c2 = Cipher.getInstance(transformation,
78-
System.getProperty("test.provider.name", "SunJCE"));
79-
80-
byte[] plainTxt = new byte[dataSize];
81-
random.nextBytes(plainTxt);
82-
System.out.println("Testing inLen = " + plainTxt.length);
83-
84-
c2.init(Cipher.ENCRYPT_MODE, key);
85-
AlgorithmParameters params = c2.getParameters();
86-
byte[] answer = c2.doFinal(plainTxt);
87-
test(c1, Cipher.ENCRYPT_MODE, key, params,
88-
plainTxt, answer);
89-
System.out.println("Encryption tests: DONE");
90-
c2.init(Cipher.DECRYPT_MODE, key, params);
91-
byte[] answer2 = c2.doFinal(answer);
92-
test(c1, Cipher.DECRYPT_MODE, key, params,
93-
answer, answer2);
94-
System.out.println("Decryption tests: DONE");
95-
} catch (NoSuchAlgorithmException nsae) {
96-
throw new SkippedException("Skipping unsupported algorithm: " +
97-
nsae);
76+
for (CI currTest : TEST_LIST) {
77+
System.out.println("===" + currTest.transformation + "===");
78+
try {
79+
KeyGenerator kg =
80+
KeyGenerator.getInstance(currTest.keyAlgo, p);
81+
SecretKey key = kg.generateKey();
82+
Cipher c1 = Cipher.getInstance(currTest.transformation, p);
83+
Cipher c2 = Cipher.getInstance(currTest.transformation,
84+
System.getProperty("test.provider.name", "SunJCE"));
85+
86+
byte[] plainTxt = new byte[currTest.dataSize];
87+
random.nextBytes(plainTxt);
88+
System.out.println("Testing inLen = " + plainTxt.length);
89+
90+
c2.init(Cipher.ENCRYPT_MODE, key);
91+
AlgorithmParameters params = c2.getParameters();
92+
byte[] answer = c2.doFinal(plainTxt);
93+
test(c1, Cipher.ENCRYPT_MODE, key, params,
94+
plainTxt, answer);
95+
System.out.println("Encryption tests: DONE");
96+
c2.init(Cipher.DECRYPT_MODE, key, params);
97+
byte[] answer2 = c2.doFinal(answer);
98+
test(c1, Cipher.DECRYPT_MODE, key, params,
99+
answer, answer2);
100+
System.out.println("Decryption tests: DONE");
101+
} catch (NoSuchAlgorithmException nsae) {
102+
System.out.println("Skipping unsupported algorithm: " +
103+
nsae);
104+
skippedList.add(currTest);
105+
}
98106
}
99107
} catch (Exception ex) {
100108
// print out debug info when exception is encountered
101109
System.out.println(debugBuf);
102110
throw ex;
103111
}
112+
113+
if (!skippedList.isEmpty()){
114+
throw new SkippedException("Some tests skipped: " + skippedList);
115+
}
104116
}
105117

106118
private static void test(Cipher cipher, int mode, SecretKey key,
@@ -225,32 +237,6 @@ private static void match(ByteBuffer bb, byte[] answer) throws Exception {
225237
}
226238

227239
public static void main(String[] args) throws Exception {
228-
229-
final List<String[]> tests = List.of(
230-
new String[]{"ARCFOUR", "ARCFOUR", "400"},
231-
new String[]{"RC4", "RC4", "401"},
232-
new String[]{"DES/CBC/NoPadding", "DES", "400"},
233-
new String[]{"DESede/CBC/NoPadding", "DESede", "160"},
234-
new String[]{"AES/CBC/NoPadding", "AES", "4800"},
235-
new String[]{"Blowfish/CBC/NoPadding", "Blowfish", "24"},
236-
new String[]{"AES/CTR/NoPadding", "AES", "1600"},
237-
new String[]{"AES/CTR/NoPadding", "AES", "65"},
238-
new String[]{"AES/CTS/NoPadding", "AES", "1600"},
239-
new String[]{"AES/CTS/NoPadding", "AES", "65"}
240-
);
241-
242-
boolean skipEncountered = false;
243-
for (final String[] t : tests) {
244-
try {
245-
main(new TestSymmCiphersNoPad(t[0], t[1], Integer.parseInt(t[2])), args);
246-
} catch (SkippedException skippedException) {
247-
skippedException.printStackTrace(System.err);
248-
skipEncountered = true;
249-
}
250-
}
251-
252-
if (skipEncountered) {
253-
throw new SkippedException("One or more tests skipped");
254-
}
240+
main(new TestSymmCiphersNoPad(), args);
255241
}
256242
}

0 commit comments

Comments
 (0)