Skip to content

Commit 5084a8b

Browse files
committed
Fix formatting of tests
1 parent d5ddf9f commit 5084a8b

File tree

6 files changed

+121
-57
lines changed

6 files changed

+121
-57
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
import java.io.IOException;
66

7-
public class HexDumpUtil {
7+
public class HexDumpUtil
8+
{
89

910
/**
1011
* Return a formatted hex dump of the given byte array.
1112
* @param array byte array
1213
*/
13-
public static String hexdump(byte[] array) {
14+
public static String hexdump(byte[] array)
15+
{
1416
return hexdump(0, array);
1517
}
1618

@@ -20,34 +22,41 @@ public static String hexdump(byte[] array) {
2022
* @param startIndent shift the octet stream between by a number of bytes
2123
* @param array byte array
2224
*/
23-
public static String hexdump(int startIndent, byte[] array) {
24-
if (startIndent < 0) {
25+
public static String hexdump(int startIndent, byte[] array)
26+
{
27+
if (startIndent < 0)
28+
{
2529
throw new IllegalArgumentException("Start-Indent must be a positive number");
2630
}
27-
if (array == null) {
31+
if (array == null)
32+
{
2833
return "<null>";
2934
}
3035
String hex = Hex.toHexString(array);
3136
StringBuilder withWhiteSpace = new StringBuilder();
3237
// shift the dump a number of octets to the right
33-
for (int i = 0; i < startIndent; i++) {
38+
for (int i = 0; i < startIndent; i++)
39+
{
3440
withWhiteSpace.append(" ");
3541
}
3642
// Split into hex octets (pairs of two chars)
3743
String[] octets = withWhiteSpace.append(hex).toString().split("(?<=\\G.{2})");
3844

3945
StringBuilder out = new StringBuilder();
4046
int l = 0;
41-
while (l < octets.length) {
47+
while (l < octets.length)
48+
{
4249
// index row
4350
out.append(String.format("%08X", l)).append(" ");
4451
// first 8 octets of a line
45-
for (int i = l ; i < l + 8 && i < octets.length; i++) {
52+
for (int i = l ; i < l + 8 && i < octets.length; i++)
53+
{
4654
out.append(octets[i]).append(" ");
4755
}
4856
out.append(" ");
4957
// second 8 octets of a line
50-
for (int i = l+8; i < l + 16 && i < octets.length; i++) {
58+
for (int i = l+8; i < l + 16 && i < octets.length; i++)
59+
{
5160
out.append(octets[i]).append(" ");
5261
}
5362
out.append("\n");
@@ -64,7 +73,8 @@ public static String hexdump(int startIndent, byte[] array) {
6473
* @throws IOException if an exception happens during packet encoding
6574
*/
6675
public static String hexdump(ContainedPacket packet)
67-
throws IOException {
76+
throws IOException
77+
{
6878
return hexdump(packet.getEncoded());
6979
}
7080

@@ -77,7 +87,8 @@ public static String hexdump(ContainedPacket packet)
7787
* @throws IOException if an exception happens during packet encoding
7888
*/
7989
public static String hexdump(int startIndent, ContainedPacket packet)
80-
throws IOException {
90+
throws IOException
91+
{
8192
return hexdump(startIndent, packet.getEncoded());
8293
}
8394
}

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77

88
import java.io.IOException;
99

10-
public abstract class AbstractPacketTest extends SimpleTest {
10+
public abstract class AbstractPacketTest
11+
extends SimpleTest
12+
{
1113

1214
/**
1315
* Test, whether the first byte array and the second byte array are identical.
1416
* If a mismatch is detected, a formatted hex dump of both arrays is printed to stdout.
1517
* @param first first array
1618
* @param second second array
1719
*/
18-
public void isEncodingEqual(byte[] first, byte[] second) {
20+
public void isEncodingEqual(byte[] first, byte[] second)
21+
{
1922
isEncodingEqual(null, first, second);
2023
}
2124

@@ -26,9 +29,11 @@ public void isEncodingEqual(byte[] first, byte[] second) {
2629
* @param first first array
2730
* @param second second array
2831
*/
29-
public void isEncodingEqual(String message, byte[] first, byte[] second) {
32+
public void isEncodingEqual(String message, byte[] first, byte[] second)
33+
{
3034
StringBuilder sb = new StringBuilder();
31-
if (message != null) {
35+
if (message != null)
36+
{
3237
sb.append(message).append("\n");
3338
}
3439
sb.append("Expected: \n").append(HexDumpUtil.hexdump(first)).append("\n");
@@ -44,7 +49,8 @@ public void isEncodingEqual(String message, byte[] first, byte[] second) {
4449
* @param second second packet
4550
*/
4651
public void isEncodingEqual(ContainedPacket first, ContainedPacket second)
47-
throws IOException {
52+
throws IOException
53+
{
4854
isEncodingEqual(null, first, second);
4955
}
5056

@@ -56,9 +62,11 @@ public void isEncodingEqual(ContainedPacket first, ContainedPacket second)
5662
* @param second second packet
5763
*/
5864
public void isEncodingEqual(String message, ContainedPacket first, ContainedPacket second)
59-
throws IOException {
65+
throws IOException
66+
{
6067
StringBuilder sb = new StringBuilder();
61-
if (message != null) {
68+
if (message != null)
69+
{
6270
sb.append(message).append("\n");
6371
}
6472
sb.append("Expected: \n").append(HexDumpUtil.hexdump(first)).append("\n");
@@ -70,7 +78,8 @@ public void isEncodingEqual(String message, ContainedPacket first, ContainedPack
7078
* Test, whether the value is false.
7179
* @param value value
7280
*/
73-
public void isFalse(boolean value) {
81+
public void isFalse(boolean value)
82+
{
7483
isFalse("Value is not false.", value);
7584
}
7685

@@ -79,15 +88,17 @@ public void isFalse(boolean value) {
7988
* @param message custom error message
8089
* @param value value
8190
*/
82-
public void isFalse(String message, boolean value) {
91+
public void isFalse(String message, boolean value)
92+
{
8393
isTrue(message, !value);
8494
}
8595

8696
/**
8797
* Test, whether the value is null.
8898
* @param value value
8999
*/
90-
public void isNull(Object value) {
100+
public void isNull(Object value)
101+
{
91102
isNull("Value is not null.", value);
92103
}
93104

@@ -96,15 +107,17 @@ public void isNull(Object value) {
96107
* @param message custom error message
97108
* @param value value
98109
*/
99-
public void isNull(String message, Object value) {
110+
public void isNull(String message, Object value)
111+
{
100112
isTrue(message, value == null);
101113
}
102114

103115
/**
104116
* Test, whether the value is not null.
105117
* @param value value
106118
*/
107-
public void isNotNull(Object value) {
119+
public void isNotNull(Object value)
120+
{
108121
isNotNull("Value is not null.", value);
109122
}
110123

@@ -113,7 +126,8 @@ public void isNotNull(Object value) {
113126
* @param message custom error message
114127
* @param value value
115128
*/
116-
public void isNotNull(String message, Object value) {
129+
public void isNotNull(String message, Object value)
130+
{
117131
isTrue(message, value != null);
118132
}
119133
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111
import java.security.Security;
1212

1313
public class AllTests
14-
extends TestCase {
14+
extends TestCase
15+
{
1516

1617
public void testPacketParsing()
1718
{
1819
Security.addProvider(new BouncyCastleProvider());
1920

20-
org.bouncycastle.util.test.Test[] tests = new org.bouncycastle.util.test.Test[] {
21-
new SignaturePacketTest(),
22-
new OnePassSignaturePacketTest(),
23-
new OpenPgpMessageTest()
24-
};
21+
org.bouncycastle.util.test.Test[] tests = new org.bouncycastle.util.test.Test[]
22+
{
23+
new SignaturePacketTest(),
24+
new OnePassSignaturePacketTest(),
25+
new OpenPgpMessageTest()
26+
};
2527

2628
for (int i = 0; i != tests.length; i++)
2729
{

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

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public class OnePassSignaturePacketTest
1414
{
1515

1616
// Parse v6 OPS packet and compare its values to a known-good test vector
17-
private void testParseV6OnePassSignaturePacket() throws IOException {
17+
private void testParseV6OnePassSignaturePacket()
18+
throws IOException
19+
{
1820
// Version 6 OnePassSignature packet
1921
// extracted from https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-sample-inline-signed-messag
2022
byte[] encOPS = Hex.decode("c44606010a1b2076495f50218890f7f5e2ee3c1822514f70500f551d86e5c921e404e34a53fbaccb186c4f0609a697e4d52dfa6c722b0c1f1e27c18a56708f6525ec27bad9acc901");
@@ -48,7 +50,9 @@ private void testParseV6OnePassSignaturePacket() throws IOException {
4850
isEncodingEqual("OPS Packet encoding mismatch", encOPS, bOut.toByteArray());
4951
}
5052

51-
private void roundtripV3Packet() throws IOException {
53+
private void roundtripV3Packet()
54+
throws IOException
55+
{
5256
OnePassSignaturePacket before = new OnePassSignaturePacket(
5357
PGPSignature.BINARY_DOCUMENT,
5458
HashAlgorithmTags.SHA256,
@@ -73,7 +77,8 @@ private void roundtripV3Packet() throws IOException {
7377
isNull("OPS v3 MUST NOT have salt",
7478
before.getSalt());
7579

76-
for (boolean newTypeIdFormat : new boolean[] {true, false}) {
80+
for (boolean newTypeIdFormat : new boolean[] {true, false})
81+
{
7782
// round-trip the packet by encoding and decoding it
7883
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
7984
BCPGOutputStream pOut = new BCPGOutputStream(bOut, newTypeIdFormat);
@@ -105,7 +110,9 @@ private void roundtripV3Packet() throws IOException {
105110
}
106111
}
107112

108-
private void roundtripV6Packet() throws IOException {
113+
private void roundtripV6Packet()
114+
throws IOException
115+
{
109116
byte[] salt = new byte[32];
110117
byte[] fingerprint = Hex.decode("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9");
111118
long keyID = ((fingerprint[0] & 0xffL) << 56) |
@@ -143,7 +150,8 @@ private void roundtripV6Packet() throws IOException {
143150
isTrue("non-nested OPS is expected to be containing",
144151
before.isContaining());
145152

146-
for (boolean newTypeIdFormat : new boolean[] {true, false}) {
153+
for (boolean newTypeIdFormat : new boolean[] {true, false})
154+
{
147155
// round-trip the packet by encoding and decoding it
148156
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
149157
BCPGOutputStream pOut = new BCPGOutputStream(bOut, newTypeIdFormat);
@@ -174,7 +182,9 @@ private void roundtripV6Packet() throws IOException {
174182
}
175183
}
176184

177-
private void roundtripV6PacketWithZeroLengthSalt() throws IOException {
185+
private void roundtripV6PacketWithZeroLengthSalt()
186+
throws IOException
187+
{
178188
byte[] salt = new byte[0];
179189
byte[] fingerprint = Hex.decode("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9");
180190

@@ -189,7 +199,8 @@ private void roundtripV6PacketWithZeroLengthSalt() throws IOException {
189199
isEncodingEqual("Salt mismatch",
190200
salt, before.getSalt());
191201

192-
for (boolean newTypeIdFormat : new boolean[] {true, false}) {
202+
for (boolean newTypeIdFormat : new boolean[] {true, false})
203+
{
193204
// round-trip the packet by encoding and decoding it
194205
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
195206
BCPGOutputStream pOut = new BCPGOutputStream(bOut, newTypeIdFormat);
@@ -218,24 +229,31 @@ private void roundtripV6PacketWithZeroLengthSalt() throws IOException {
218229
}
219230
}
220231

221-
private void parsingOfPacketWithUnknownVersionFails() {
232+
private void parsingOfPacketWithUnknownVersionFails()
233+
{
222234
// Version 0x99 OnePassSignature packet
223235
byte[] encOPS = Hex.decode("c44699010a1b2076495f50218890f7f5e2ee3c1822514f70500f551d86e5c921e404e34a53fbaccb186c4f0609a697e4d52dfa6c722b0c1f1e27c18a56708f6525ec27bad9acc901");
224236

225237
ByteArrayInputStream bIn = new ByteArrayInputStream(encOPS);
226238
BCPGInputStream pIn = new BCPGInputStream(bIn);
227239

228-
try {
240+
try
241+
{
229242
pIn.readPacket();
230243
fail("Expected UnsupportedPacketVersionException");
231-
} catch (IOException e) {
244+
}
245+
catch (IOException e)
246+
{
232247
fail("Expected UnsupportedPacketVersionException", e);
233-
} catch (UnsupportedPacketVersionException e) {
248+
}
249+
catch (UnsupportedPacketVersionException e)
250+
{
234251
// expected
235252
}
236253
}
237254

238-
private void parsingOfPacketWithTruncatedFingerprintFails() {
255+
private void parsingOfPacketWithTruncatedFingerprintFails()
256+
{
239257
// Version 6 OnePassSignature packet with truncated fingerprint field (20 bytes instead of 32)
240258
// This error would happen, if a v6 OPS packet was generated with a v4 fingerprint.
241259
// extracted from https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-sample-inline-signed-messag
@@ -244,21 +262,27 @@ private void parsingOfPacketWithTruncatedFingerprintFails() {
244262
ByteArrayInputStream bIn = new ByteArrayInputStream(encOPS);
245263
BCPGInputStream pIn = new BCPGInputStream(bIn);
246264

247-
try {
265+
try
266+
{
248267
pIn.readPacket();
249268
fail("Expected IOException");
250-
} catch (IOException e) {
269+
}
270+
catch (IOException e)
271+
{
251272
// expected
252273
}
253274
}
254275

255276
@Override
256-
public String getName() {
277+
public String getName()
278+
{
257279
return "OnePassSignaturePacketTest";
258280
}
259281

260282
@Override
261-
public void performTest() throws Exception {
283+
public void performTest()
284+
throws Exception
285+
{
262286
testParseV6OnePassSignaturePacket();
263287
roundtripV3Packet();
264288
roundtripV6Packet();
@@ -267,7 +291,8 @@ public void performTest() throws Exception {
267291
roundtripV6PacketWithZeroLengthSalt();
268292
}
269293

270-
public static void main(String[] args) {
294+
public static void main(String[] args)
295+
{
271296
runTest(new OnePassSignaturePacketTest());
272297
}
273298
}

0 commit comments

Comments
 (0)