Skip to content

Commit a70c092

Browse files
author
gefeili
committed
#1717 Fix some checkstyle issues in pg tests
2 parents 902266f + 2597de2 commit a70c092

14 files changed

+94
-117
lines changed

pg/src/test/java/org/bouncycastle/bcpg/test/BCPGOutputStreamTest.java

+26-10
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
import java.util.ArrayList;
1313
import java.util.List;
1414

15-
public class BCPGOutputStreamTest extends SimpleTest {
15+
public class BCPGOutputStreamTest
16+
extends SimpleTest
17+
{
1618

17-
private void testForceNewPacketFormat() throws IOException {
19+
private void testForceNewPacketFormat()
20+
throws IOException
21+
{
1822
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
1923
BCPGOutputStream pOut = new BCPGOutputStream(bOut, PacketFormat.CURRENT);
2024

@@ -30,7 +34,9 @@ private void testForceNewPacketFormat() throws IOException {
3034
isTrue(pIn.readPacket().hasNewPacketFormat());
3135
}
3236

33-
private void testForceOldPacketFormat() throws IOException {
37+
private void testForceOldPacketFormat()
38+
throws IOException
39+
{
3440
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
3541
BCPGOutputStream pOut = new BCPGOutputStream(bOut, PacketFormat.LEGACY);
3642

@@ -46,7 +52,9 @@ private void testForceOldPacketFormat() throws IOException {
4652
isTrue(!pIn.readPacket().hasNewPacketFormat());
4753
}
4854

49-
private void testRoundTripPacketFormat() throws IOException {
55+
private void testRoundTripPacketFormat()
56+
throws IOException
57+
{
5058
List<UserIDPacket> oldPackets = new ArrayList<>();
5159
ByteArrayInputStream obIn = new ByteArrayInputStream(Hex.decode("b405416c696365b403426f62"));
5260
BCPGInputStream opIn = new BCPGInputStream(obIn);
@@ -79,7 +87,9 @@ private void testRoundTripPacketFormat() throws IOException {
7987
isTrue(pIn.readPacket().hasNewPacketFormat());
8088
}
8189

82-
private void testRoundtripMixedPacketFormats() throws IOException {
90+
private void testRoundtripMixedPacketFormats()
91+
throws IOException
92+
{
8393
// Certificate with mixed new and old packet formats
8494
// The primary key + sigs use new format
8595
// The signing subkey + sigs use old format
@@ -244,7 +254,8 @@ private void testRoundtripMixedPacketFormats() throws IOException {
244254
aIn = new ArmoredInputStream(bIn);
245255
pIn = new BCPGInputStream(aIn);
246256
Packet packet;
247-
while ((packet = pIn.readPacket()) != null) {
257+
while ((packet = pIn.readPacket()) != null)
258+
{
248259
isTrue(packet.hasNewPacketFormat());
249260
}
250261

@@ -259,25 +270,30 @@ private void testRoundtripMixedPacketFormats() throws IOException {
259270
bIn = new ByteArrayInputStream(bOut.toByteArray());
260271
aIn = new ArmoredInputStream(bIn);
261272
pIn = new BCPGInputStream(aIn);
262-
while ((packet = pIn.readPacket()) != null) {
273+
while ((packet = pIn.readPacket()) != null)
274+
{
263275
isTrue(!packet.hasNewPacketFormat());
264276
}
265277
}
266278

267279
@Override
268-
public String getName() {
280+
public String getName()
281+
{
269282
return "BCPGOutputStreamTest";
270283
}
271284

272285
@Override
273-
public void performTest() throws Exception {
286+
public void performTest()
287+
throws Exception
288+
{
274289
testForceOldPacketFormat();
275290
testForceNewPacketFormat();
276291
testRoundTripPacketFormat();
277292
testRoundtripMixedPacketFormats();
278293
}
279294

280-
public static void main(String[] args) {
295+
public static void main(String[] args)
296+
{
281297
runTest(new BCPGOutputStreamTest());
282298
}
283299
}

pg/src/test/java/org/bouncycastle/bcpg/test/OCBEncryptedDataPacketTest.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,26 @@
66
import java.io.ByteArrayInputStream;
77
import java.io.IOException;
88

9-
public class OCBEncryptedDataPacketTest extends AbstractPacketTest {
9+
public class OCBEncryptedDataPacketTest
10+
extends AbstractPacketTest
11+
{
1012
@Override
11-
public String getName() {
13+
public String getName()
14+
{
1215
return "OCBEncryptedDataPacketTest";
1316
}
1417

1518
@Override
16-
public void performTest() throws Exception {
19+
public void performTest()
20+
throws Exception
21+
{
1722
parseTestVector();
1823
parseUnsupportedPacketVersion();
1924
}
2025

21-
private void parseTestVector() throws IOException {
26+
private void parseTestVector()
27+
throws IOException
28+
{
2229
String testVector = "" +
2330
"d45301090210c265ff63a61ed8af00fa" +
2431
"43866be8eb9eef77241518a3d60e387b" +
@@ -39,7 +46,9 @@ private void parseTestVector() throws IOException {
3946
isEncodingEqual("IV mismatch", Hex.decode("C265FF63A61ED8AF00FA43866BE8EB"), p.getIV());
4047
}
4148

42-
private void parseUnsupportedPacketVersion() throws IOException {
49+
private void parseUnsupportedPacketVersion()
50+
throws IOException
51+
{
4352
// Test vector with modified packet version 99
4453
String testVector = "" +
4554
"d45399090210c265ff63a61ed8af00fa" +

pg/src/test/java/org/bouncycastle/bcpg/test/OnePassSignaturePacketTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private void testParseV6OnePassSignaturePacket()
3636
issuerFp, ops.getFingerprint());
3737
isTrue("OPS packet key-ID mismatch",
3838
// key-ID are the first 8 octets of the fingerprint
39+
// -DM Hex.toHexString
3940
Hex.toHexString(issuerFp).startsWith(Long.toHexString(ops.getKeyID())));
4041
isEncodingEqual("OPS packet salt mismatch",
4142
salt, ops.getSalt());

pg/src/test/java/org/bouncycastle/bcpg/test/UnknownPublicKeyPacketTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public void performTest()
3232
parseUnknownPublicKey();
3333
}
3434

35-
private void parseUnknownPublicKey() throws ParseException, IOException {
35+
private void parseUnknownPublicKey()
36+
throws ParseException, IOException
37+
{
3638
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
3739
parser.setTimeZone(TimeZone.getTimeZone("UTC"));
3840

pg/src/test/java/org/bouncycastle/openpgp/test/AEADProtectedPGPSecretKeyTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ private void testUnlockKeyWithWrongPassphraseJca()
272272
{
273273
sk.extractPrivateKey(jceDecBuilder.build("Yang".toCharArray()));
274274
fail("Expected PGPException due to wrong passphrase");
275-
} catch (PGPException e)
275+
}
276+
catch (PGPException e)
276277
{
277278
// expected
278279
}

pg/src/test/java/org/bouncycastle/openpgp/test/Argon2S2KTest.java

-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77
import java.io.OutputStream;
88
import java.security.SecureRandom;
99
import java.util.Date;
10-
import java.util.Iterator;
1110

1211
import org.bouncycastle.bcpg.ArmoredInputStream;
1312
import org.bouncycastle.bcpg.ArmoredOutputStream;
14-
import org.bouncycastle.bcpg.BCPGInputStream;
1513
import org.bouncycastle.bcpg.BCPGOutputStream;
16-
import org.bouncycastle.bcpg.HashAlgorithmTags;
1714
import org.bouncycastle.bcpg.S2K;
1815
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
19-
import org.bouncycastle.bcpg.SymmetricKeyEncSessionPacket;
20-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
2116
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
2217
import org.bouncycastle.openpgp.PGPEncryptedDataList;
2318
import org.bouncycastle.openpgp.PGPException;

pg/src/test/java/org/bouncycastle/openpgp/test/CRC24Test.java

-10
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ public void performanceTest()
8585
fastImpl.update(0);
8686
fastImpl.reset();
8787

88-
long start = System.currentTimeMillis();
89-
9088
for (int j = 0; j < 100; ++j)
9189
{
9290
for (int i = 0; i != LARGE_RANDOM.length; i += 3)
@@ -95,7 +93,6 @@ public void performanceTest()
9593
}
9694
}
9795
int defVal = defaultImpl.getValue();
98-
long afterDefault = System.currentTimeMillis();
9996

10097
for (int j = 0; j < 100; ++j)
10198
{
@@ -105,14 +102,7 @@ public void performanceTest()
105102
}
106103
}
107104
int fastVal = fastImpl.getValue();
108-
long afterFast = System.currentTimeMillis();
109105

110106
isEquals("Calculated value of default and fast CRC-24 implementations diverges", defVal, fastVal);
111-
long defDuration = afterDefault - start;
112-
System.out.println("Default Implementation: " + defDuration / 1000 + "s" + defDuration % 1000);
113-
114-
long fastDuration = afterFast - afterDefault;
115-
System.out.println("Fast Implementation: " + fastDuration / 1000 + "s" + fastDuration % 1000);
116-
117107
}
118108
}

pg/src/test/java/org/bouncycastle/openpgp/test/Curve25519PrivateKeyEncodingTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ public boolean containsSubsequence(byte[] sequence, byte[] subsequence)
169169
/**
170170
* Test proper functionality of the {@link #containsSubsequence(byte[], byte[])} method.
171171
*/
172-
private void containsTest() {
172+
private void containsTest()
173+
{
173174
// Make sure our containsSubsequence method functions correctly
174175
byte[] s = new byte[] {0x00, 0x01, 0x02, 0x03};
175176
isTrue(containsSubsequence(s, new byte[] {0x00, 0x01}));

pg/src/test/java/org/bouncycastle/openpgp/test/DedicatedEd25519KeyPairTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ private void testV4SigningVerificationWithBcKey()
190190
isTrue(signature.verify());
191191
}
192192

193-
private void testConversionOfTestVectorKey() throws PGPException, IOException {
193+
private void testConversionOfTestVectorKey()
194+
throws PGPException, IOException
195+
{
194196
JcaPGPKeyConverter jc = new JcaPGPKeyConverter().setProvider(new BouncyCastleProvider());
195197
BcPGPKeyConverter bc = new BcPGPKeyConverter();
196198
// ed25519 public key from https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-hashed-data-stream-for-sign

pg/src/test/java/org/bouncycastle/openpgp/test/PGPAeadTest.java

+8-37
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder;
4545
import org.bouncycastle.openpgp.operator.jcajce.JcePBEKeyEncryptionMethodGenerator;
4646
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
47+
import org.bouncycastle.test.DumpUtil;
4748
import org.bouncycastle.util.Arrays;
4849
import org.bouncycastle.util.Exceptions;
4950
import org.bouncycastle.util.Pack;
@@ -174,11 +175,9 @@ private void knownV5TestVectorDecryptionTests()
174175
throws IOException, PGPException
175176
{
176177
// test known-good V5 test vectors
177-
System.out.println("Test V5 BC Decryption");
178178
testBcDecryption(V5_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
179179
testBcDecryption(V5_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
180180
testBcDecryption(V5_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
181-
System.out.println("Test V5 JCA Decryption");
182181
testJceDecryption(V5_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
183182
testJceDecryption(V5_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
184183
testJceDecryption(V5_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
@@ -187,12 +186,10 @@ private void knownV5TestVectorDecryptionTests()
187186
private void knownV6TestVectorDecryptionTests()
188187
throws IOException, PGPException
189188
{
190-
// Test known-good V6 test vectors TODO: decryption tests should be working...
191-
System.out.println("Test V6 BC Decryption");
189+
// Test known-good V6 test vectors TODO: decryption tests
192190
testBcDecryption(V6_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
193191
testBcDecryption(V6_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
194192
testBcDecryption(V6_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
195-
System.out.println("Test V6 JCA Decryption");
196193
testJceDecryption(V6_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
197194
testJceDecryption(V6_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
198195
testJceDecryption(V6_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
@@ -201,36 +198,28 @@ private void knownV6TestVectorDecryptionTests()
201198
private void testBcRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
202199
throws PGPException, IOException
203200
{
204-
System.out.println("Test BC RoundTrip " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
205201
String armored = testBcEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
206-
System.out.println(armored);
207202
testBcDecryption(armored, password, plaintext);
208203
}
209204

210205
private void testJceRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
211206
throws PGPException, IOException
212207
{
213-
System.out.println("Test JCE RoundTrip " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
214208
String armored = testJceEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
215-
System.out.println(armored);
216209
testJceDecryption(armored, password, plaintext);
217210
}
218211

219212
private void testBcJceRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
220213
throws PGPException, IOException
221214
{
222-
System.out.println("Test BC encrypt, JCE decrypt " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
223215
String armored = testBcEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
224-
System.out.println(armored);
225216
testJceDecryption(armored, password, plaintext);
226217
}
227218

228219
private void testJceBcRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
229220
throws PGPException, IOException
230221
{
231-
System.out.println("Test JCE encrypt, BC decrypt " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
232222
String armored = testJceEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
233-
System.out.println(armored);
234223
testBcDecryption(armored, password, plaintext);
235224
}
236225

@@ -365,7 +354,9 @@ private void testBcDecryption(String armoredMessage, char[] password, byte[] exp
365354

366355
if (o != null)
367356
{
357+
// -DM System.out.println
368358
System.out.println("Unexpected trailing packet.");
359+
// -DM System.out.println
369360
System.out.println(o);
370361
}
371362
}
@@ -421,7 +412,9 @@ private void testJceDecryption(String armoredMessage, char[] password, byte[] ex
421412

422413
if (o != null)
423414
{
415+
// -DM System.out.println
424416
System.out.println("Unexpected trailing packet.");
417+
// -DM System.out.println
425418
System.out.println(o);
426419
}
427420
}
@@ -435,30 +428,8 @@ private void testJceDecryption(String armoredMessage, char[] password, byte[] ex
435428

436429
public static void printHex(byte[] bytes)
437430
{
438-
boolean separate = true;
439-
boolean prefix = true;
440-
String hex = Hex.toHexString(bytes);
441-
StringBuffer sb = new StringBuffer();
442-
for (int i = 0; i < hex.length() / 2; i++)
443-
{
444-
if (prefix && i % 8 == 0)
445-
{
446-
sb.append("0x").append(Hex.toHexString(Pack.intToBigEndian(i & 0xFFFFF))).append(" ");
447-
}
448-
sb.append(hex.substring(i * 2, i * 2 + 2));
449-
if (separate)
450-
{
451-
if ((i + 1) % 8 == 0)
452-
{
453-
sb.append('\n');
454-
}
455-
else
456-
{
457-
sb.append(' ');
458-
}
459-
}
460-
}
461-
System.out.println(sb);
431+
// -DM System.out.println
432+
System.out.println(DumpUtil.hexdump(bytes));
462433
}
463434

464435
private static String algNames(int aeadAlg, int symAlg)

0 commit comments

Comments
 (0)