Skip to content

Commit

Permalink
Merge branch '1717-pg-test-style' into 'main'
Browse files Browse the repository at this point in the history
1717 pg test style

See merge request root/bc-java!30
  • Loading branch information
dghgit committed Sep 11, 2024
2 parents c51be49 + a70c092 commit bc26a9a
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import java.util.ArrayList;
import java.util.List;

public class BCPGOutputStreamTest extends SimpleTest {
public class BCPGOutputStreamTest
extends SimpleTest
{

private void testForceNewPacketFormat() throws IOException {
private void testForceNewPacketFormat()
throws IOException
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
BCPGOutputStream pOut = new BCPGOutputStream(bOut, PacketFormat.CURRENT);

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

private void testForceOldPacketFormat() throws IOException {
private void testForceOldPacketFormat()
throws IOException
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
BCPGOutputStream pOut = new BCPGOutputStream(bOut, PacketFormat.LEGACY);

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

private void testRoundTripPacketFormat() throws IOException {
private void testRoundTripPacketFormat()
throws IOException
{
List<UserIDPacket> oldPackets = new ArrayList<>();
ByteArrayInputStream obIn = new ByteArrayInputStream(Hex.decode("b405416c696365b403426f62"));
BCPGInputStream opIn = new BCPGInputStream(obIn);
Expand Down Expand Up @@ -79,7 +87,9 @@ private void testRoundTripPacketFormat() throws IOException {
isTrue(pIn.readPacket().hasNewPacketFormat());
}

private void testRoundtripMixedPacketFormats() throws IOException {
private void testRoundtripMixedPacketFormats()
throws IOException
{
// Certificate with mixed new and old packet formats
// The primary key + sigs use new format
// The signing subkey + sigs use old format
Expand Down Expand Up @@ -244,7 +254,8 @@ private void testRoundtripMixedPacketFormats() throws IOException {
aIn = new ArmoredInputStream(bIn);
pIn = new BCPGInputStream(aIn);
Packet packet;
while ((packet = pIn.readPacket()) != null) {
while ((packet = pIn.readPacket()) != null)
{
isTrue(packet.hasNewPacketFormat());
}

Expand All @@ -259,25 +270,30 @@ private void testRoundtripMixedPacketFormats() throws IOException {
bIn = new ByteArrayInputStream(bOut.toByteArray());
aIn = new ArmoredInputStream(bIn);
pIn = new BCPGInputStream(aIn);
while ((packet = pIn.readPacket()) != null) {
while ((packet = pIn.readPacket()) != null)
{
isTrue(!packet.hasNewPacketFormat());
}
}

@Override
public String getName() {
public String getName()
{
return "BCPGOutputStreamTest";
}

@Override
public void performTest() throws Exception {
public void performTest()
throws Exception
{
testForceOldPacketFormat();
testForceNewPacketFormat();
testRoundTripPacketFormat();
testRoundtripMixedPacketFormats();
}

public static void main(String[] args) {
public static void main(String[] args)
{
runTest(new BCPGOutputStreamTest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class OCBEncryptedDataPacketTest extends AbstractPacketTest {
public class OCBEncryptedDataPacketTest
extends AbstractPacketTest
{
@Override
public String getName() {
public String getName()
{
return "OCBEncryptedDataPacketTest";
}

@Override
public void performTest() throws Exception {
public void performTest()
throws Exception
{
parseTestVector();
parseUnsupportedPacketVersion();
}

private void parseTestVector() throws IOException {
private void parseTestVector()
throws IOException
{
String testVector = "" +
"d45301090210c265ff63a61ed8af00fa" +
"43866be8eb9eef77241518a3d60e387b" +
Expand All @@ -39,7 +46,9 @@ private void parseTestVector() throws IOException {
isEncodingEqual("IV mismatch", Hex.decode("C265FF63A61ED8AF00FA43866BE8EB"), p.getIV());
}

private void parseUnsupportedPacketVersion() throws IOException {
private void parseUnsupportedPacketVersion()
throws IOException
{
// Test vector with modified packet version 99
String testVector = "" +
"d45399090210c265ff63a61ed8af00fa" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private void testParseV6OnePassSignaturePacket()
issuerFp, ops.getFingerprint());
isTrue("OPS packet key-ID mismatch",
// key-ID are the first 8 octets of the fingerprint
// -DM Hex.toHexString
Hex.toHexString(issuerFp).startsWith(Long.toHexString(ops.getKeyID())));
isEncodingEqual("OPS packet salt mismatch",
salt, ops.getSalt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public void performTest()
parseUnknownPublicKey();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ private void testUnlockKeyWithWrongPassphraseJca()
{
sk.extractPrivateKey(jceDecBuilder.build("Yang".toCharArray()));
fail("Expected PGPException due to wrong passphrase");
} catch (PGPException e)
}
catch (PGPException e)
{
// expected
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Iterator;

import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.HashAlgorithmTags;
import org.bouncycastle.bcpg.S2K;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.bcpg.SymmetricKeyEncSessionPacket;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedDataList;
import org.bouncycastle.openpgp.PGPException;
Expand Down
10 changes: 0 additions & 10 deletions pg/src/test/java/org/bouncycastle/openpgp/test/CRC24Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ public void performanceTest()
fastImpl.update(0);
fastImpl.reset();

long start = System.currentTimeMillis();

for (int j = 0; j < 100; ++j)
{
for (int i = 0; i != LARGE_RANDOM.length; i += 3)
Expand All @@ -95,7 +93,6 @@ public void performanceTest()
}
}
int defVal = defaultImpl.getValue();
long afterDefault = System.currentTimeMillis();

for (int j = 0; j < 100; ++j)
{
Expand All @@ -105,14 +102,7 @@ public void performanceTest()
}
}
int fastVal = fastImpl.getValue();
long afterFast = System.currentTimeMillis();

isEquals("Calculated value of default and fast CRC-24 implementations diverges", defVal, fastVal);
long defDuration = afterDefault - start;
System.out.println("Default Implementation: " + defDuration / 1000 + "s" + defDuration % 1000);

long fastDuration = afterFast - afterDefault;
System.out.println("Fast Implementation: " + fastDuration / 1000 + "s" + fastDuration % 1000);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ public boolean containsSubsequence(byte[] sequence, byte[] subsequence)
/**
* Test proper functionality of the {@link #containsSubsequence(byte[], byte[])} method.
*/
private void containsTest() {
private void containsTest()
{
// Make sure our containsSubsequence method functions correctly
byte[] s = new byte[] {0x00, 0x01, 0x02, 0x03};
isTrue(containsSubsequence(s, new byte[] {0x00, 0x01}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ private void testV4SigningVerificationWithBcKey()
isTrue(signature.verify());
}

private void testConversionOfTestVectorKey() throws PGPException, IOException {
private void testConversionOfTestVectorKey()
throws PGPException, IOException
{
JcaPGPKeyConverter jc = new JcaPGPKeyConverter().setProvider(new BouncyCastleProvider());
BcPGPKeyConverter bc = new BcPGPKeyConverter();
// ed25519 public key from https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-hashed-data-stream-for-sign
Expand Down
45 changes: 8 additions & 37 deletions pg/src/test/java/org/bouncycastle/openpgp/test/PGPAeadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePBEKeyEncryptionMethodGenerator;
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
import org.bouncycastle.test.DumpUtil;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Exceptions;
import org.bouncycastle.util.Pack;
Expand Down Expand Up @@ -174,11 +175,9 @@ private void knownV5TestVectorDecryptionTests()
throws IOException, PGPException
{
// test known-good V5 test vectors
System.out.println("Test V5 BC Decryption");
testBcDecryption(V5_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testBcDecryption(V5_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testBcDecryption(V5_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
System.out.println("Test V5 JCA Decryption");
testJceDecryption(V5_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testJceDecryption(V5_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testJceDecryption(V5_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
Expand All @@ -187,12 +186,10 @@ private void knownV5TestVectorDecryptionTests()
private void knownV6TestVectorDecryptionTests()
throws IOException, PGPException
{
// Test known-good V6 test vectors TODO: decryption tests should be working...
System.out.println("Test V6 BC Decryption");
// Test known-good V6 test vectors TODO: decryption tests
testBcDecryption(V6_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testBcDecryption(V6_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testBcDecryption(V6_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
System.out.println("Test V6 JCA Decryption");
testJceDecryption(V6_EAX_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testJceDecryption(V6_OCB_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
testJceDecryption(V6_GCM_PACKET_SEQUENCE, PASSWORD, PLAINTEXT);
Expand All @@ -201,36 +198,28 @@ private void knownV6TestVectorDecryptionTests()
private void testBcRoundTrip(boolean v5AEAD, int aeadAlg, int symAlg, byte[] plaintext, char[] password)
throws PGPException, IOException
{
System.out.println("Test BC RoundTrip " + (v5AEAD ? "V5" : "V6") + " " + algNames(aeadAlg, symAlg));
String armored = testBcEncryption(v5AEAD, aeadAlg, symAlg, plaintext, password);
System.out.println(armored);
testBcDecryption(armored, password, plaintext);
}

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

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

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

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

if (o != null)
{
// -DM System.out.println
System.out.println("Unexpected trailing packet.");
// -DM System.out.println
System.out.println(o);
}
}
Expand Down Expand Up @@ -421,7 +412,9 @@ private void testJceDecryption(String armoredMessage, char[] password, byte[] ex

if (o != null)
{
// -DM System.out.println
System.out.println("Unexpected trailing packet.");
// -DM System.out.println
System.out.println(o);
}
}
Expand All @@ -435,30 +428,8 @@ private void testJceDecryption(String armoredMessage, char[] password, byte[] ex

public static void printHex(byte[] bytes)
{
boolean separate = true;
boolean prefix = true;
String hex = Hex.toHexString(bytes);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < hex.length() / 2; i++)
{
if (prefix && i % 8 == 0)
{
sb.append("0x").append(Hex.toHexString(Pack.intToBigEndian(i & 0xFFFFF))).append(" ");
}
sb.append(hex.substring(i * 2, i * 2 + 2));
if (separate)
{
if ((i + 1) % 8 == 0)
{
sb.append('\n');
}
else
{
sb.append(' ');
}
}
}
System.out.println(sb);
// -DM System.out.println
System.out.println(DumpUtil.hexdump(bytes));
}

private static String algNames(int aeadAlg, int symAlg)
Expand Down
Loading

0 comments on commit bc26a9a

Please sign in to comment.