Skip to content

Commit 628c546

Browse files
author
Roger Riggs
committed
8258796: [test] Apply HexFormat to tests for java.security
Reviewed-by: xuelei
1 parent 876c7fb commit 628c546

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+292
-677
lines changed

test/jdk/com/sun/crypto/provider/Cipher/AEAD/SameBuffer.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
2525
import java.security.AlgorithmParameters;
2626
import java.security.Provider;
2727
import java.security.Security;
28+
import java.util.HexFormat;
2829
import javax.crypto.SecretKey;
2930
import javax.crypto.Cipher;
3031
import javax.crypto.KeyGenerator;
@@ -102,7 +103,7 @@ static void runTest(Provider p, String algo, String mode,
102103
String padding, int keyLength, int textLength, int AADLength,
103104
int offset) throws Exception {
104105
System.out.println("Testing " + keyLength + " key length; "
105-
+ textLength + " text lenght; " + AADLength + " AAD length; "
106+
+ textLength + " text length; " + AADLength + " AAD length; "
106107
+ offset + " offset");
107108
if (keyLength > Cipher.getMaxAllowedKeyLength(algo)) {
108109
// skip this if this key length is larger than what's
@@ -239,30 +240,30 @@ private void doTestWithSameBuffer(int offset, AlgorithmParameters params)
239240
* Check if two results are equal
240241
*/
241242
private void runGCMWithSeparateArray(int mode, byte[] AAD, byte[] text,
242-
int txtOffset, int lenght, int offset, AlgorithmParameters params)
243+
int txtOffset, int length, int offset, AlgorithmParameters params)
243244
throws Exception {
244245
// first, generate the cipher text at an allocated buffer
245246
Cipher cipher = createCipher(mode, params);
246247
cipher.updateAAD(AAD);
247-
byte[] outputText = cipher.doFinal(text, txtOffset, lenght);
248+
byte[] outputText = cipher.doFinal(text, txtOffset, length);
248249

249250
// new cipher for encrypt operation
250251
Cipher anotherCipher = createCipher(mode, params);
251252
anotherCipher.updateAAD(AAD);
252253

253254
// next, generate cipher text again at the same buffer of plain text
254255
int myoff = offset;
255-
int off = anotherCipher.update(text, txtOffset, lenght, text, myoff);
256+
int off = anotherCipher.update(text, txtOffset, length, text, myoff);
256257
anotherCipher.doFinal(text, myoff + off);
257258

258259
// check if two resutls are equal
259260
if (!isEqual(text, myoff, outputText, 0, outputText.length)) {
260261
System.err.println(
261262
"\noutputText: len = " + outputText.length + " txtOffset = " + txtOffset + "\n" +
262-
jdk.test.lib.Convert.byteArrayToHexString(outputText) + "\n" +
263+
HexFormat.of().withUpperCase().formatHex(outputText) + "\n" +
263264
"text: len = " + text.length + " myoff = " + myoff + "\n" +
264-
jdk.test.lib.Convert.byteArrayToHexString(text) + "\n" +
265-
"lenght " + lenght);
265+
HexFormat.of().withUpperCase().formatHex(text) + "\n" +
266+
"length " + length);
266267
System.err.println("tlen = " + params.getParameterSpec(GCMParameterSpec.class).getTLen() / 8);
267268
throw new RuntimeException("Two results not equal, mode:" + mode);
268269
}

test/jdk/com/sun/crypto/provider/Cipher/Blowfish/BlowfishTestVector.java

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -112,33 +112,4 @@ public static void main(String[] argv) throws Exception {
112112
}
113113
System.out.println("Test passed");
114114
}
115-
116-
/*
117-
* Converts a byte to hex digit and writes to the supplied buffer
118-
*/
119-
static private void byte2hex(byte b, StringBuffer buf) {
120-
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
121-
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
122-
int high = ((b & 0xf0) >> 4);
123-
int low = (b & 0x0f);
124-
buf.append(hexChars[high]);
125-
buf.append(hexChars[low]);
126-
}
127-
128-
/*
129-
* Converts a byte array to hex string
130-
*/
131-
static private String toHexString(byte[] block) {
132-
StringBuffer buf = new StringBuffer();
133-
134-
int len = block.length;
135-
136-
for (int i = 0; i < len; i++) {
137-
byte2hex(block[i], buf);
138-
if (i < len-1) {
139-
buf.append(":");
140-
}
141-
}
142-
return buf.toString();
143-
}
144115
}

test/jdk/com/sun/crypto/provider/Cipher/ChaCha20/ChaCha20KAT.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -45,9 +45,9 @@ public static class TestData {
4545
public TestData(String name, String keyStr, String nonceStr, int ctr,
4646
int dir, String inputStr, String aadStr, String outStr) {
4747
testName = Objects.requireNonNull(name);
48-
key = Convert.hexStringToByteArray(Objects.requireNonNull(keyStr));
49-
nonce = Convert.hexStringToByteArray(
50-
Objects.requireNonNull(nonceStr));
48+
HexFormat hex = HexFormat.of();
49+
key = hex.parseHex(Objects.requireNonNull(keyStr));
50+
nonce = hex.parseHex(Objects.requireNonNull(nonceStr));
5151
if ((counter = ctr) < 0) {
5252
throw new IllegalArgumentException(
5353
"counter must be 0 or greater");
@@ -58,12 +58,9 @@ public TestData(String name, String keyStr, String nonceStr, int ctr,
5858
throw new IllegalArgumentException(
5959
"Direction must be ENCRYPT_MODE or DECRYPT_MODE");
6060
}
61-
input = Convert.hexStringToByteArray(
62-
Objects.requireNonNull(inputStr));
63-
aad = (aadStr != null) ?
64-
Convert.hexStringToByteArray(aadStr) : null;
65-
expOutput = Convert.hexStringToByteArray(
66-
Objects.requireNonNull(outStr));
61+
input = hex.parseHex(Objects.requireNonNull(inputStr));
62+
aad = (aadStr != null) ? hex.parseHex(aadStr) : null;
63+
expOutput = hex.parseHex(Objects.requireNonNull(outStr));
6764
}
6865

6966
public final String testName;

test/jdk/com/sun/crypto/provider/Cipher/ChaCha20/ChaCha20KeyGeneratorTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,21 +30,20 @@
3030

3131
import java.security.InvalidAlgorithmParameterException;
3232
import java.security.InvalidParameterException;
33+
import java.util.HexFormat;
3334

3435
import javax.crypto.KeyGenerator;
3536
import javax.crypto.SecretKey;
3637
import javax.crypto.spec.ChaCha20ParameterSpec;
3738

38-
import jdk.test.lib.Utils;
39-
4039
public class ChaCha20KeyGeneratorTest {
4140

4241
public static void main(String[] args) throws Exception {
4342
KeyGenerator generator = KeyGenerator.getInstance("ChaCha20");
4443

4544
try {
4645
generator.init(new ChaCha20ParameterSpec(
47-
Utils.toByteArray("100000000000000000000000"), 0));
46+
HexFormat.of().parseHex("100000000000000000000000"), 0));
4847
throw new RuntimeException(
4948
"ChaCha20 key generation should not consume AlgorithmParameterSpec");
5049
} catch (InvalidAlgorithmParameterException e) {
@@ -65,7 +64,7 @@ public static void main(String[] args) throws Exception {
6564
generator.init(256);
6665
SecretKey key = generator.generateKey();
6766
byte[] keyValue = key.getEncoded();
68-
System.out.println("Key: " + Utils.toHexString(keyValue));
67+
System.out.println("Key: " + HexFormat.of().formatHex(keyValue));
6968
if (keyValue.length != 32) {
7069
throw new RuntimeException("The size of generated key must be 256");
7170
}

test/jdk/com/sun/crypto/provider/Cipher/ChaCha20/ChaCha20NoReuse.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,6 @@
2525
* @test
2626
* @bug 8153029
2727
* @library /test/lib
28-
* @build jdk.test.lib.Convert
2928
* @run main ChaCha20NoReuse
3029
* @summary ChaCha20 Cipher Implementation (key/nonce reuse protection)
3130
*/
@@ -39,7 +38,6 @@
3938
import javax.crypto.AEADBadTagException;
4039
import javax.crypto.SecretKey;
4140
import java.security.InvalidKeyException;
42-
import jdk.test.lib.Convert;
4341

4442
public class ChaCha20NoReuse {
4543

@@ -76,9 +74,9 @@ public static class TestData {
7674
public TestData(String name, String keyStr, String nonceStr, int ctr,
7775
int dir, String inputStr, String aadStr, String outStr) {
7876
testName = Objects.requireNonNull(name);
79-
key = Convert.hexStringToByteArray(Objects.requireNonNull(keyStr));
80-
nonce = Convert.hexStringToByteArray(
81-
Objects.requireNonNull(nonceStr));
77+
HexFormat hex = HexFormat.of();
78+
key = hex.parseHex(keyStr);
79+
nonce = hex.parseHex(nonceStr);
8280
if ((counter = ctr) < 0) {
8381
throw new IllegalArgumentException(
8482
"counter must be 0 or greater");
@@ -89,12 +87,9 @@ public TestData(String name, String keyStr, String nonceStr, int ctr,
8987
throw new IllegalArgumentException(
9088
"Direction must be ENCRYPT_MODE or DECRYPT_MODE");
9189
}
92-
input = Convert.hexStringToByteArray(
93-
Objects.requireNonNull(inputStr));
94-
aad = (aadStr != null) ?
95-
Convert.hexStringToByteArray(aadStr) : null;
96-
expOutput = Convert.hexStringToByteArray(
97-
Objects.requireNonNull(outStr));
90+
input = hex.parseHex(inputStr);
91+
aad = (aadStr != null) ? hex.parseHex(aadStr) : null;
92+
expOutput = hex.parseHex(outStr);
9893
}
9994

10095
public final String testName;

test/jdk/com/sun/crypto/provider/Cipher/ChaCha20/ChaCha20Poly1305ParamTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,6 @@
2525
* @test
2626
* @bug 8153029 8257769
2727
* @library /test/lib
28-
* @build jdk.test.lib.Convert
2928
* @run main ChaCha20Poly1305ParamTest
3029
* @summary ChaCha20 Cipher Implementation (parameters)
3130
*/
@@ -53,9 +52,9 @@ public static class TestData {
5352
public TestData(String name, String keyStr, String nonceStr, int ctr,
5453
int dir, String inputStr, String aadStr, String outStr) {
5554
testName = Objects.requireNonNull(name);
56-
key = Convert.hexStringToByteArray(Objects.requireNonNull(keyStr));
57-
nonce = Convert.hexStringToByteArray(
58-
Objects.requireNonNull(nonceStr));
55+
HexFormat hex = HexFormat.of();
56+
key = hex.parseHex(keyStr);
57+
nonce = hex.parseHex(nonceStr);
5958
if ((counter = ctr) < 0) {
6059
throw new IllegalArgumentException(
6160
"counter must be 0 or greater");
@@ -66,12 +65,9 @@ public TestData(String name, String keyStr, String nonceStr, int ctr,
6665
throw new IllegalArgumentException(
6766
"Direction must be ENCRYPT_MODE or DECRYPT_MODE");
6867
}
69-
input = Convert.hexStringToByteArray(
70-
Objects.requireNonNull(inputStr));
71-
aad = (aadStr != null) ?
72-
Convert.hexStringToByteArray(aadStr) : null;
73-
expOutput = Convert.hexStringToByteArray(
74-
Objects.requireNonNull(outStr));
68+
input = hex.parseHex(inputStr);
69+
aad = (aadStr != null) ? hex.parseHex(aadStr) : null;
70+
expOutput = hex.parseHex(outStr);
7571
}
7672

7773
public final String testName;

test/jdk/com/sun/crypto/provider/Cipher/ChaCha20/unittest/ChaCha20CipherUnitTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
3535
import java.security.NoSuchAlgorithmException;
3636
import java.security.SecureRandom;
3737
import java.util.Arrays;
38+
import java.util.HexFormat;
3839

3940
import javax.crypto.Cipher;
4041
import javax.crypto.spec.ChaCha20ParameterSpec;
@@ -46,10 +47,9 @@
4647
public class ChaCha20CipherUnitTest {
4748

4849
private static final byte[] NONCE
49-
= Utils.toByteArray("012345670123456701234567");
50+
= HexFormat.of().parseHex("012345670123456701234567");
5051
private static final SecretKeySpec KEY = new SecretKeySpec(
51-
Utils.toByteArray(
52-
"0123456701234567012345670123456701234567012345670123456701234567"),
52+
HexFormat.of().parseHex("0123456701234567012345670123456701234567012345670123456701234567"),
5353
"ChaCha20");
5454
private static final ChaCha20ParameterSpec CHACHA20_PARAM_SPEC
5555
= new ChaCha20ParameterSpec(NONCE, 0);
@@ -165,7 +165,7 @@ private static void testInitOnWrap(int opMode) throws Exception {
165165
}
166166

167167
private static void testAEAD() throws Exception {
168-
byte[] expectedPlainttext = Utils.toByteArray("01234567");
168+
byte[] expectedPlainttext = HexFormat.of().parseHex("01234567");
169169
byte[] ciphertext = testUpdateAAD(Cipher.ENCRYPT_MODE, expectedPlainttext);
170170
byte[] plaintext = testUpdateAAD(Cipher.DECRYPT_MODE, ciphertext);
171171
if (!Arrays.equals(plaintext, expectedPlainttext)) {
@@ -180,7 +180,7 @@ private static byte[] testUpdateAAD(int opMode, byte[] input)
180180
String opModeName = getOpModeName(opMode);
181181
System.out.println("== updateAAD (" + opModeName + ") ==");
182182

183-
byte[] aad = Utils.toByteArray("0000");
183+
byte[] aad = HexFormat.of().parseHex("0000");
184184
ByteBuffer aadBuf = ByteBuffer.wrap(aad);
185185

186186
Cipher cipher = Cipher.getInstance("ChaCha20");

test/jdk/com/sun/crypto/provider/KeyAgreement/DHKeyAgreement2.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@
3434
import java.security.*;
3535
import java.security.spec.*;
3636
import java.security.interfaces.*;
37+
import java.util.HexFormat;
3738
import javax.crypto.*;
3839
import javax.crypto.spec.*;
3940
import javax.crypto.interfaces.*;
@@ -51,6 +52,10 @@
5152
public class DHKeyAgreement2 {
5253

5354
private static final String SUNJCE = "SunJCE";
55+
56+
// Hex formatter to upper case with ":" delimiter
57+
private static final HexFormat HEX_FORMATTER = HexFormat.ofDelimiter(":").withUpperCase();
58+
5459
private DHKeyAgreement2() {}
5560

5661
public static void main(String argv[]) throws Exception {
@@ -209,8 +214,8 @@ private void run(String mode) throws Exception {
209214
System.out.println("EXPECTED: " + e.getMessage());
210215
}
211216

212-
System.out.println("Alice secret: " + toHexString(aliceSharedSecret));
213-
System.out.println("Bob secret: " + toHexString(bobSharedSecret));
217+
System.out.println("Alice secret: " + HEX_FORMATTER.formatHex(aliceSharedSecret));
218+
System.out.println("Bob secret: " + HEX_FORMATTER.formatHex(bobSharedSecret));
214219

215220
if (aliceLen != bobLen) {
216221
throw new Exception("Shared secrets have different lengths");
@@ -262,23 +267,6 @@ private void byte2hex(byte b, StringBuffer buf) {
262267
buf.append(hexChars[low]);
263268
}
264269

265-
/*
266-
* Converts a byte array to hex string
267-
*/
268-
private String toHexString(byte[] block) {
269-
StringBuffer buf = new StringBuffer();
270-
271-
int len = block.length;
272-
273-
for (int i = 0; i < len; i++) {
274-
byte2hex(block[i], buf);
275-
if (i < len-1) {
276-
buf.append(":");
277-
}
278-
}
279-
return buf.toString();
280-
}
281-
282270
/*
283271
* Prints the usage of this test.
284272
*/

0 commit comments

Comments
 (0)