Skip to content

Commit

Permalink
Added back JVM version testing for prov and tls.
Browse files Browse the repository at this point in the history
Commented print statements in mls tests.
  • Loading branch information
mwcw committed Feb 5, 2024
1 parent 9279d29 commit bec33f3
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 50 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export BC_JDK17=/path/to/java17
export BC_JDK21=/path/to/java21
```



## Building

The project now uses ```gradlew``` which can be invoked for example:
Expand All @@ -44,6 +42,21 @@ The project now uses ```gradlew``` which can be invoked for example:
The gradle script will endeavour to verify their existence but not the correctness of their value.


## Multi-release jars and testing
Some subprojects produce multi-release jars and these jars are tested in different jvm versions.
Default testing on these projects is done on java 1.8 and there are specific test tasks for other versions.

1. test11 test on java 11 JVM
2. test17 test on java 17 JVM
3. test21 test on java 21 JVM

To run all of them:

```
./gradlew clean build test11 test17 test21
```


## Code Organisation

The clean room JCE, for use with JDK 1.1 to JDK 1.3 is in the jce/src/main/java directory. From JDK 1.4 and later the JCE ships with the JVM, the source for later JDKs follows the progress that was made in the later versions of the JCE. If you are using a later version of the JDK which comes with a JCE install please **do not** include the jce directory as a source file as it will clash with the JCE API installed with your JDK.
Expand Down
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ subprojects {
languageVersion = JavaLanguageVersion.of(8)
}

jvmArgs = ['-Dtest.java.version.prefix=1.8']


finalizedBy jacocoTestReport

filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Epoch(List<byte[]> proposals, byte[] commit, byte[] epoch_authenticator)
{
// try
// {
System.out.print("test case: " + count);
// System.out.print("test case: " + count);
short cipherSuite = Short.parseShort(buf.get("cipher_suite"));
byte[] key_package = Hex.decode(buf.get("key_package"));
byte[] signature_priv = Hex.decode(buf.get("signature_priv"));
Expand Down Expand Up @@ -189,7 +189,7 @@ public Epoch(List<byte[]> proposals, byte[] commit, byte[] epoch_authenticator)
epochs.clear();
buf.clear();
count++;
System.out.println(" PASSED");
// System.out.println(" PASSED");
// }
// catch (Exception e)
// {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void testMessageProtection()
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
cipher_suite = Short.parseShort(buf.get("cipher_suite"));
group_id = Hex.decode(buf.get("group_id"));
epoch = Long.parseLong(buf.get("epoch"));
Expand Down
22 changes: 11 additions & 11 deletions mls/src/test/java/org/bouncycastle/mls/test/VectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void testTreeMath()
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
long n_leaves = Long.parseLong((String)buf.get("n_leaves"));
long n_nodes = Long.parseLong((String)buf.get("n_nodes"));
long root = Long.parseLong((String)buf.get("root"));
Expand Down Expand Up @@ -159,7 +159,7 @@ public void testCryptoBasics()
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
short cipherSuite = Short.parseShort(buf.get("cipherSuite"));
String refHash_label = buf.get("refHash_label");
byte[] refHash_value = Hex.decode(buf.get("refHash_value"));
Expand Down Expand Up @@ -303,7 +303,7 @@ public LeafInfo(int generation, byte[] application_key, byte[] application_nonce
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
//System.out.println("test case: " + count);
short cipher_suite = Short.parseShort(buf.get("cipher_suite"));
byte[] encryption_secret = Hex.decode(buf.get("encryption_secret"));
byte[] sender_data_secret = Hex.decode(buf.get("sender_data_secret"));
Expand Down Expand Up @@ -420,7 +420,7 @@ public void testKeySchedule()
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
buf.clear();
bufEpoch.clear();
count++;
Expand Down Expand Up @@ -566,7 +566,7 @@ public PSK(byte[] psk_id, byte[] psk, byte[] psk_nonce)
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
short cipher_suite = Short.parseShort(buf.get("cipher_suite"));
byte[] psk_secret = Hex.decode(buf.get("psk_secret"));
MlsCipherSuite suite = MlsCipherSuite.getSuite(cipher_suite);
Expand Down Expand Up @@ -640,7 +640,7 @@ public void testTranscriptHashes()
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
short cipherSuite = Short.parseShort(buf.get("cipher_suite"));
byte[] confirmation_key = Hex.decode(buf.get("confirmation_key"));
byte[] authenticated_content = Hex.decode(buf.get("authenticated_content"));
Expand Down Expand Up @@ -686,7 +686,7 @@ public void testWelcome()
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
short cipherSuite = Short.parseShort(buf.get("cipher_suite"));
byte[] init_priv = Hex.decode(buf.get("init_priv"));
byte[] key_package = Hex.decode(buf.get("key_package"));
Expand Down Expand Up @@ -758,7 +758,7 @@ public void testTreeOperations() throws Exception
if (buf.size() > 0)
{

System.out.println("test case: " + count);
// System.out.println("test case: " + count);
byte[] tree_before = Hex.decode(buf.get("tree_before"));
byte[] proposal = Hex.decode(buf.get("proposal"));
int proposal_sender = Integer.parseInt(buf.get("proposal_sender"));
Expand Down Expand Up @@ -829,7 +829,7 @@ public void testTreeValidation()
{
if (buf.size() > 0)
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
short cipherSuite = Short.parseShort(buf.get("cipher_suite"));
byte[] treeBytes = Hex.decode(buf.get("tree"));
byte[] group_id = Hex.decode(buf.get("group_id"));
Expand Down Expand Up @@ -1015,7 +1015,7 @@ public UpdatePathInfo(LeafIndex sender, UpdatePath updatePath, List<PathSecret>
{
if (buf.size() > 0 && reading.equals(prevReading))
{
System.out.println("test case: " + count);
// System.out.println("test case: " + count);
short cipher_suite = Short.parseShort(buf.get("cipher_suite"));
byte[] confirmed_transcript_hash = Hex.decode(buf.get("confirmed_transcript_hash"));
long epoch = Long.parseLong(buf.get("epoch"));
Expand Down Expand Up @@ -1228,7 +1228,7 @@ public void testMessages() throws Exception
if (buf.size() > 0)
{

System.out.println("test case: " + count);
// System.out.println("test case: " + count);
byte[] mls_welcome = Hex.decode(buf.get("mls_welcome"));
byte[] mls_group_info = Hex.decode(buf.get("mls_group_info"));
byte[] mls_key_package = Hex.decode(buf.get("mls_key_package"));
Expand Down
66 changes: 35 additions & 31 deletions prov/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ artifacts {
archives sourcesJar
}

test {
forkEvery = 1;
maxParallelForks = 8;
}


sourceSets {
test11 {
Expand Down Expand Up @@ -204,6 +201,40 @@ dependencies {
}




compileTest11Java {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(17)
}
sourceCompatibility = 11
targetCompatibility = 11
options.sourcepath = files(['src/test/java', 'src/test/jdk1.11'])
}

compileTest17Java {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(17)
}
sourceCompatibility = 17
targetCompatibility = 17
options.sourcepath = files(['src/test/java', 'src/test/jdk1.15'])
}

compileTest21Java {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(21)
}
sourceCompatibility = 21
targetCompatibility = 21
options.sourcepath = files(['src/test/java', 'src/test/jdk21'])
}

test {
jvmArgs = ['-Dtest.java.version.prefix=1.8']
}


task test11(type: Test) {

dependsOn(jar)
Expand Down Expand Up @@ -268,33 +299,6 @@ task test17(type: Test) {
}
}

compileTest11Java {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(17)
}
sourceCompatibility = 11
targetCompatibility = 11
options.sourcepath = files(['src/test/java', 'src/test/jdk1.11'])
}

compileTest17Java {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(17)
}
sourceCompatibility = 17
targetCompatibility = 17
options.sourcepath = files(['src/test/java', 'src/test/jdk1.15'])
}

compileTest21Java {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(21)
}
sourceCompatibility = 21
targetCompatibility = 21
options.sourcepath = files(['src/test/java', 'src/test/jdk21'])
}

task test21(type: Test) {

// This is testing the 21 code base
Expand Down
47 changes: 47 additions & 0 deletions prov/src/test/java/org/bouncycastle/test/AllTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.bouncycastle.test;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;

public class AllTests
extends TestCase
{
public static void main(String[] args)
{

PrintTestResult.printResult(junit.textui.TestRunner.run(suite()));
}

public static Test suite()
{
TestSuite suite = new TestSuite("JVM Version Tests");
suite.addTestSuite(JVMVersionTest.class);


return new BCTestSetup(suite);
}

static class BCTestSetup
extends TestSetup
{
public BCTestSetup(Test test)
{
super(test);
}

protected void setUp()
{
Security.addProvider(new BouncyCastleProvider());
}

protected void tearDown()
{
Security.removeProvider("BC");
}
}
}
46 changes: 46 additions & 0 deletions prov/src/test/java/org/bouncycastle/test/JVMVersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.bouncycastle.test;

import junit.framework.TestCase;
import org.junit.Test;


/**
* This test asserts the java version running the tests starts with
* a property value passed in as part of test invocation.
*
* -Dtest.java.version.prefix must match the start of System.getProperty("java.version")
* So:
* if -Dtest.java.version.prefix=17 and System.getProperty("java.version") = 17.0.4.1
* Then this test will pass.
*/
public class JVMVersionTest extends TestCase
{

private static final String expectedVersionPropName = "test.java.version.prefix";

@Test
public void testAssertExpectedJVM() {

//
// This project produces a multi-release jar, and we need to test it on different jvm versions
// This test compares a property "test.java.version.prefix" with the start of the value reported by the JVM.
// eg:
// -Dtest.java.version.prefix=1.8
//
// It exists because we have had issues with build systems unexpectedly using a different JVM to one we need to test on.
// It is important for multi-release jars to be exercised on a representative JVM for each JVM they support.
//
//

String version = System.getProperty("java.version");
assertNotNull(String.format("property %s is not set, see comment in test for reason why.",expectedVersionPropName),System.getProperty(expectedVersionPropName));



String expectedPrefix = System.getProperty(expectedVersionPropName);

TestCase.assertTrue(String.format("JVM Version: '%s' did not start with '%s' see comment in test",version,expectedPrefix), version.startsWith(expectedPrefix));

}

}
47 changes: 47 additions & 0 deletions prov/src/test/jdk1.5/org/bouncycastle/test/AllTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.bouncycastle.test;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;

public class AllTests
extends TestCase
{
public static void main(String[] args)
{

PrintTestResult.printResult(junit.textui.TestRunner.run(suite()));
}

public static Test suite()
{
TestSuite suite = new TestSuite("JVM Version Tests");
suite.addTestSuite(JVMVersionTest.class);


return new BCTestSetup(suite);
}

static class BCTestSetup
extends TestSetup
{
public BCTestSetup(Test test)
{
super(test);
}

protected void setUp()
{
Security.addProvider(new BouncyCastleProvider());
}

protected void tearDown()
{
Security.removeProvider("BC");
}
}
}
Loading

0 comments on commit bec33f3

Please sign in to comment.