Skip to content

Commit de76533

Browse files
committed
updates to test resource finding, relates to github #2248
1 parent e6ba201 commit de76533

19 files changed

Lines changed: 255 additions & 547 deletions

File tree

CLAUDE.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ The build is Gradle multi-module. JDK 21+ is required to drive Gradle. Optional
1313
./gradlew -PexcludeTests=<glob> :prov:test # exclude pattern
1414
```
1515

16-
`bc-test-data` (separate repo `bcgit/bc-test-data`) must be checked out as a sibling of `bc-java` for the full suite to pass; the Gradle property `bcTestDataHome` defaults to `core/src/test/data`.
16+
`bc-test-data` (separate repo `bcgit/bc-test-data`) must be checked out for the full suite to pass. `TestResourceFinder.findTestResource(homeDir, fileName)` (six per-module copies under `<module>/src/test/java/org/bouncycastle/test/`) resolves the bc-test-data root in this order:
17+
18+
1. The system property `bc.test.data.home`, if set.
19+
2. The environment variable `BC_TEST_DATA_HOME`, if set.
20+
3. Walk up from the working directory looking for a directory literally named `bc-test-data` — the default that makes `./gradlew :prov:test` work when bc-test-data is checked out as a sibling of `bc-java`.
21+
22+
When the property or environment variable is supplied, the named path is required to exist; a mistyped value fails fast with a `FileNotFoundException` naming both the source (`-Dbc.test.data.home` or `$BC_TEST_DATA_HOME`) and the bad path, rather than silently falling through. The Gradle build no longer sets the property itself; supply `-Dbc.test.data.home=/path/to/bc-test-data` (or export `BC_TEST_DATA_HOME` once in your shell) only when the sibling-checkout convention doesn't fit your layout. Direct `java -cp ... junit.textui.TestRunner ...` invocations follow the same rule.
1723

1824
### Running an individual test fast
1925

@@ -36,10 +42,11 @@ java -cp pkix/build/classes/java/main:pkix/build/classes/java/test:pkix/src/test
3642
util/build/classes/java/main:\
3743
$(find ~/.gradle -name 'junit-*.jar' | head -1):\
3844
$(find ~/.gradle -name 'hamcrest-core-1*.jar' | head -1) \
39-
-Dbc.test.data.home=core/src/test/data \
4045
org.bouncycastle.openssl.test.ParserTest
4146
```
4247

48+
If your bc-test-data checkout isn't a sibling of `bc-java`, add `-Dbc.test.data.home=/abs/path/to/bc-test-data` to the command. Otherwise the walk-up search picks it up automatically.
49+
4350
Common gotchas:
4451
- `*/build/resources/main` directories are required — some tests pull resource files (e.g. `lowmcL1.bin.properties` for Picnic, GOST tables) that fail with cryptic `NullPointerException` if missing.
4552
- `prov/src/test/resources` and `core/src/test/resources` carry test fixtures referenced by `TestResourceFinder` and direct classpath lookups.

CONTRIBUTORS.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@
580580
<li>Sunwoo Lee &lt;https://github.com/programsurf&gt; - Reported missing validation for PQC certificates in JcaTlsCertificate.</li>
581581
<li>Stefan Santesson &lt;https://github.com/Razumain&gt; - Initial implementation of hashing to elliptic curves (RFC 9380) and hashing to scalars (RFC 9497).</li>
582582
<li>Steve Hawkins &lt;https://github.com/shawkins&gt; - Initial patch adding configurable BlockPool support to Argon2BytesGenerator.</li>
583-
<li>Hendrik Ebbers &lt;https://github.com/hendrikebbers&gt; - Patch propagating exception causes when re-throwing as IllegalArgumentException in ASN.1 parsing utilities.</li>
583+
<li>Hendrik Ebbers &lt;https://github.com/hendrikebbers&gt; - Patch propagating exception causes when re-throwing as IllegalArgumentException in ASN.1 parsing utilities; primary author of the first cut of the TestResourceFinder update to honour the bc.test.data.home system property and BC_TEST_DATA_HOME environment variable for locating bc-test-data.</li>
584584
<li>Gray j256 &lt;https://github.com/j256&gt; - Addition of license ID to licenses files.</li>
585585
<li>Johannes Leupold from KOBIL GmbH; XChaCha20 and XChaCha20Poly1305 initial implementation.</li>
586586
<li>Sean Gilligan &lt;https://github.com/msgilligan&gt; - README updates for JDK25 build / BC_JDK21 environment variable.</li>

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ This project can now be built and tested with JDK25.
3333
If the build script detects BC_JDK8, BC_JDK11, BC_JDK17, BC_JDK21 it will add to the usual test task a dependency on test tasks
3434
that specifically use the JVMs addressed by those environmental variables. The script relies on JAVA_HOME for picking up Java 25 if it is use.
3535

36-
To run the tests of the project as part of the build test data is needed. Our test data can be found at the [bc-test-data](https://github.com/bcgit/bc-test-data) repository.
36+
To run the tests of the project as part of the build test data is needed. Our test data can be found at the [bc-test-data](https://github.com/bcgit/bc-test-data) repository. The tests locate the bc-test-data tree using, in order:
37+
38+
1. The system property ```bc.test.data.home```, if set.
39+
2. The environment variable ```BC_TEST_DATA_HOME```, if set.
40+
3. Otherwise the tests walk up from the working directory looking for a directory literally named ```bc-test-data```. The simplest configuration is therefore to check ```bc-test-data``` out as a sibling of ```bc-java``` and no further setup is required.
41+
42+
When the property or environment variable is supplied, the named path is required to exist; a mistyped value fails fast with a ```FileNotFoundException``` naming whichever source supplied it, rather than silently falling through to the walk-up.
3743

3844
We support testing on specific JVMs as it is the only way to be certain the library is compatible.
3945

@@ -48,6 +54,12 @@ export BC_JDK17=/path/to/java17
4854
export BC_JDK21=/path/to/java21
4955
```
5056

57+
If your ```bc-test-data``` checkout is not a sibling of ```bc-java```, set ```BC_TEST_DATA_HOME``` (or pass ```-Dbc.test.data.home=...``` on the command line) so the tests can find it:
58+
59+
```
60+
export BC_TEST_DATA_HOME=/path/to/bc-test-data
61+
```
62+
5163
## Building
5264

5365
The project now uses ```gradlew``` which can be invoked for example:

ant/bc+-build.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,6 @@
10401040
<exclude name="**/bcjmail-jdk*.jar" />
10411041
</fileset>
10421042
</classpath>
1043-
<sysproperty key="bc.test.data.home" value="../../core/src/test/data" />
10441043
<sysproperty key="test.java.version.prefix" value="${env.JAVA_VERSION_PREFIX}" />
10451044

10461045
<formatter type="xml" />

build.gradle

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,7 @@ configure(allprojects.findAll {it.name != 'bom'}) {
165165

166166
}
167167

168-
task printProperties {
169-
doLast {
170-
println bcTestDataHome
171-
}
172-
}
173-
174168
ext {
175-
bcTestDataHome = file('core/src/test/data').absolutePath
176-
177169
JavaVersion current = JavaVersion.current();
178170
if (current.compareTo(JavaVersion.VERSION_1_8) <= 0) {
179171
ext.vmrange = 'jdk15to18'
@@ -205,10 +197,8 @@ configure(subprojects.findAll {it.name != 'bom'}) {
205197

206198
task testFull(type: Test) {
207199
systemProperties = [
208-
'bc.test.data.home': bcTestDataHome,
209200
'test.full' : 'true'
210201
]
211-
systemProperty 'bc.test.data.home', bcTestDataHome
212202
maxHeapSize = "1536m"
213203

214204
finalizedBy jacocoTestReport
@@ -221,7 +211,6 @@ configure(subprojects.findAll {it.name != 'bom'}) {
221211
test {
222212
forkEvery = 1;
223213
maxParallelForks = 1;
224-
systemProperty 'bc.test.data.home', bcTestDataHome
225214
maxHeapSize = "1536m"
226215
testLogging.showStandardStreams = false
227216

build1-1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ javac -J-mx768m -d ../../../classes -classpath ../../../classes:../../../src:$JD
441441
javac -J-mx768m -d ../../../classes -classpath ../../../classes:../../../src:$JDK11PATH/lib/classes.zip */u*/*.java )
442442

443443
echo "lightweight regression test"
444-
java -mx868m -classpath classes:$JDK11PATH/lib/classes.zip -Dbc.test.data.home=/home/dgh/bc/java/crypto/test/data org.bouncycastle.crypto.test.RegressionTest
445-
java -classpath classes:$JDK11PATH/lib/classes.zip -Dbc.test.data.home=/home/dgh/bc/java/crypto/test/data org.bouncycastle.asn1.test.RegressionTest
444+
java -mx868m -classpath classes:$JDK11PATH/lib/classes.zip org.bouncycastle.crypto.test.RegressionTest
445+
java -classpath classes:$JDK11PATH/lib/classes.zip org.bouncycastle.asn1.test.RegressionTest
446446
)
447447
(2>&1 find $artifacts/lcrypto-jdk11-$base -name CVS -exec rm -rf \{\} \; ) > /dev/null
448448
fi

build1-2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ then
351351

352352
cp ../../../../core/src/test/resources/org/bouncycastle/asn1/test/*.data classes/org/bouncycastle/asn1/test
353353

354-
java -classpath classes -Dbc.test.data.home=../core/src/test/data org.bouncycastle.crypto.test.RegressionTest
354+
java -classpath classes org.bouncycastle.crypto.test.RegressionTest
355355
java -classpath classes org.bouncycastle.asn1.test.RegressionTest
356356
)
357357
(2>&1 find $artifacts/lcrypto-jdk12-$base -name CVS -exec rm -rf \{\} \; ) > /dev/null

core/src/test/java/org/bouncycastle/test/TestResourceFinder.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,53 @@
99

1010
public class TestResourceFinder
1111
{
12+
private static final String DATA_HOME_PROPERTY = "bc.test.data.home";
13+
private static final String DATA_HOME_ENV = "BC_TEST_DATA_HOME";
1214
private static final String dataDirName = "bc-test-data";
1315

1416
/**
15-
* We search starting at the working directory looking for the bc-test-data directory.
17+
* Resolve a test fixture from the bc-test-data tree.
18+
* <p>
19+
* Resolution order for the bc-test-data root:
20+
* <ol>
21+
* <li>The {@code bc.test.data.home} system property, if set.</li>
22+
* <li>The {@code BC_TEST_DATA_HOME} environment variable, if set.</li>
23+
* <li>Walk up from the working directory looking for a directory literally named
24+
* {@code bc-test-data} (the legacy resolution path, for direct test
25+
* invocations that don't set either).</li>
26+
* </ol>
27+
* When the property or environment variable is supplied, the named path is
28+
* required to exist; a mistyped value fails fast rather than silently falling
29+
* through to the walk-up.
1630
*
17-
* @throws FileNotFoundException
31+
* @throws FileNotFoundException if no lookup locates the bc-test-data root.
1832
*/
1933
public static InputStream findTestResource(String homeDir, String fileName)
2034
throws FileNotFoundException
2135
{
22-
String wrkDirName = System.getProperty("user.dir");
2336
String separator = System.getProperty("file.separator");
37+
38+
String configured = System.getProperty(DATA_HOME_PROPERTY);
39+
String configuredSource = "-D" + DATA_HOME_PROPERTY;
40+
if (configured == null || configured.length() == 0)
41+
{
42+
configured = System.getenv(DATA_HOME_ENV);
43+
configuredSource = "$" + DATA_HOME_ENV;
44+
}
45+
if (configured != null && configured.length() > 0)
46+
{
47+
File dataDir = new File(configured);
48+
if (!dataDir.exists())
49+
{
50+
String ln = Strings.lineSeparator();
51+
throw new FileNotFoundException("Test data directory '" + configured
52+
+ "' from " + configuredSource + " not found." + ln
53+
+ "Test data available from: https://github.com/bcgit/bc-test-data.git");
54+
}
55+
return new FileInputStream(new File(dataDir, homeDir + separator + fileName));
56+
}
57+
58+
String wrkDirName = System.getProperty("user.dir");
2459
File wrkDir = new File(wrkDirName);
2560
File dataDir = new File(wrkDir, dataDirName);
2661
while (!dataDir.exists() && wrkDirName.length() > 1)

docs/releasenotes.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ <h3>2.2.3 Additional Features and Functionality</h3>
8787
<li>SignerInformation now offers a three-argument addCounterSigners(SignerInformation outer, SignerId targetCounterSigner, SignerInformationStore counterSigners) overload that nests the supplied counter-signers underneath the counter-signer in <code>outer</code>'s subtree whose SID matches <code>targetCounterSigner</code>, rebuilding the containing SignerInfos on the way back up. The existing two-argument form remains unchanged and still attaches its counter-signers as peers of any existing counter-signers (an additional counterSignature attribute in <code>outer</code>'s unsignedAttributes, per RFC 5652 sec. 11.4) — callers who wanted to build a counter-counter-signature tree previously had no way to do so. Counter-signatures live in unsignedAttributes, which is not covered by the enclosing signer's signature, so the rewrite preserves all existing signatures (issue #769).</li>
8888
<li>The Gradle build now exposes a top-level <code>copyJars</code> task (in the <code>distribution</code> group) that gathers the produced jars (main, sources and javadoc) for <code>bccore</code>, <code>bcutil</code>, <code>bcprov</code>, <code>bcpkix</code>, <code>bcpg</code>, <code>bctls</code>, <code>bcmls</code>, <code>bcmail</code> and <code>bcjmail</code> into a single <code>dist/</code> directory at the project root, providing a "dist"-style aggregate output for consumers who don't want to scrape each module's <code>build/libs</code> directory. The directory is cleared at the start of each invocation so stale version artifacts don't accumulate (issue #2301).</li>
8989
<li>FAEST post-quantum digital signature scheme per the FAEST v2.0 algorithm specification (NIST PQC additional digital signatures process). Lightweight implementation in org.bouncycastle.pqc.crypto.faest covering all twelve parameter sets — base FAEST (AES one-way function): faest_128s/f, faest_192s/f, faest_256s/f; and FAEST-EM (Even-Mansour one-way function): faest_em_128s/f, faest_em_192s/f, faest_em_256s/f — via FaestKeyPairGenerator, FaestSigner and the matching FaestPublicKeyParameters / FaestPrivateKeyParameters. JCE provider plumbing is registered through BCPQC: KeyPairGenerator.Faest, Signature.Faest, KeyFactory.Faest, FaestParameterSpec (twelve constants + fromName lookup), FaestKey interface and BCFaestPublicKey / BCFaestPrivateKey. Twelve BC-arc OIDs in BCObjectIdentifiers (faest.{1..12}) cover the SubjectPublicKeyInfo / PrivateKeyInfo wire form via the PublicKeyFactory / PrivateKeyFactory / SubjectPublicKeyInfoFactory / PrivateKeyInfoFactory converter plumbing, and BouncyCastleProvider.loadPQCKeys() registers a FaestKeyFactorySpi against each OID so the standard BC provider can decode FAEST-bearing certificates and PKCS#8 keys without BCPQC in the lookup chain.</li>
90+
<li>TestResourceFinder (the six per-module copies under &lt;module&gt;/src/test/java/org/bouncycastle/test/) now picks the bc-test-data root via, in order, the bc.test.data.home system property, the BC_TEST_DATA_HOME environment variable, and finally the existing walk-up-from-working-directory search. When the property or environment variable is set its value is used directly, and a mistyped path now fails fast with a FileNotFoundException naming the source (-Dbc.test.data.home or $BC_TEST_DATA_HOME) and the bad path rather than silently falling through. The walk-up fallback preserves the default sibling-checkout convention (bc-test-data alongside bc-java) so existing setups keep working without configuration. The Gradle build no longer sets the property itself; supply -Dbc.test.data.home=... or export BC_TEST_DATA_HOME=... only when the layout differs from the sibling convention.</li>
9091
</ul>
9192

9293
<a id="r1rv84"><h3>2.2.1 Version</h3></a>

kmip/src/test/java/org/bouncycastle/test/TestResourceFinder.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,53 @@
77

88
public class TestResourceFinder
99
{
10+
private static final String DATA_HOME_PROPERTY = "bc.test.data.home";
11+
private static final String DATA_HOME_ENV = "BC_TEST_DATA_HOME";
1012
private static final String dataDirName = "bc-test-data";
1113

1214
/**
13-
* We search starting at the working directory looking for the bc-test-data directory.
15+
* Resolve a test fixture from the bc-test-data tree.
16+
* <p>
17+
* Resolution order for the bc-test-data root:
18+
* <ol>
19+
* <li>The {@code bc.test.data.home} system property, if set.</li>
20+
* <li>The {@code BC_TEST_DATA_HOME} environment variable, if set.</li>
21+
* <li>Walk up from the working directory looking for a directory literally named
22+
* {@code bc-test-data} (the legacy resolution path, for direct test
23+
* invocations that don't set either).</li>
24+
* </ol>
25+
* When the property or environment variable is supplied, the named path is
26+
* required to exist; a mistyped value fails fast rather than silently falling
27+
* through to the walk-up.
1428
*
15-
* @throws FileNotFoundException
29+
* @throws FileNotFoundException if no lookup locates the bc-test-data root.
1630
*/
1731
public static InputStream findTestResource(String homeDir, String fileName)
1832
throws FileNotFoundException
1933
{
20-
String wrkDirName = System.getProperty("user.dir");
2134
String separator = System.getProperty("file.separator");
35+
36+
String configured = System.getProperty(DATA_HOME_PROPERTY);
37+
String configuredSource = "-D" + DATA_HOME_PROPERTY;
38+
if (configured == null || configured.length() == 0)
39+
{
40+
configured = System.getenv(DATA_HOME_ENV);
41+
configuredSource = "$" + DATA_HOME_ENV;
42+
}
43+
if (configured != null && configured.length() > 0)
44+
{
45+
File dataDir = new File(configured);
46+
if (!dataDir.exists())
47+
{
48+
String ln = System.getProperty("line.separator");
49+
throw new FileNotFoundException("Test data directory '" + configured
50+
+ "' from " + configuredSource + " not found." + ln
51+
+ "Test data available from: https://github.com/bcgit/bc-test-data.git");
52+
}
53+
return new FileInputStream(new File(dataDir, homeDir + separator + fileName));
54+
}
55+
56+
String wrkDirName = System.getProperty("user.dir");
2257
File wrkDir = new File(wrkDirName);
2358
File dataDir = new File(wrkDir, dataDirName);
2459
while (!dataDir.exists() && wrkDirName.length() > 1)

0 commit comments

Comments
 (0)