11/*
22 * ===========================================================================
3- * (c) Copyright IBM Corp. 2024, 2025 All Rights Reserved
3+ * (c) Copyright IBM Corp. 2024, 2026 All Rights Reserved
44 * ===========================================================================
55 *
66 * This code is free software; you can redistribute it and/or modify it
@@ -202,6 +202,71 @@ private static Stream<Arguments> patternMatches_propertiesList() {
202202 return tests .build ();
203203 }
204204
205+ private static Stream <Arguments > patternMatches_strongAlgorithms () {
206+ Stream .Builder <Arguments > tests = Stream .builder ();
207+
208+ if (isProviderPresent ("OpenJCEPlusFIPS" )) {
209+ // 1 - Test property - base profile with securerandom.strongAlgorithms loads successfully.
210+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms" ,
211+ System .getProperty ("test.src" ) + "/property-java.security" ,
212+ "(?s)(?=.*OpenJCEPlusFIPS)(?=.*SUN)(?=.*SunJSSE)" ,
213+ 0 ));
214+ // 2 - Test property - securerandom.strongAlgorithms property with multiple algorithms.
215+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-MultipleEntries" ,
216+ System .getProperty ("test.src" ) + "/property-java.security" ,
217+ "securerandom\\ .strongAlgorithms: SHA512DRBG:OpenJCEPlusFIPS, SHA256DRBG:OpenJCEPlusFIPS" ,
218+ 0 ));
219+ // 3 - Test property - securerandom.strongAlgorithms append algorithm in extended profile.
220+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-Extension_1" ,
221+ System .getProperty ("test.src" ) + "/property-java.security" ,
222+ "securerandom\\ .strongAlgorithms: SHA512DRBG:OpenJCEPlusFIPS, SHA256DRBG:OpenJCEPlusFIPS" ,
223+ 0 ));
224+ // 4 - Test property - securerandom.strongAlgorithms remove algorithm in extended profile.
225+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-Extension_2" ,
226+ System .getProperty ("test.src" ) + "/property-java.security" ,
227+ "securerandom\\ .strongAlgorithms: (?=.*NativePRNGBlocking:SUN)(?=.*DRBG:SUN)" ,
228+ 0 ));
229+ // 5 - Test property - securerandom.strongAlgorithms invalid algorithm.
230+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-InvalidFormat" ,
231+ System .getProperty ("test.src" ) + "/property-java.security" ,
232+ "FAILED: No strong SecureRandom impls available: .*" ,
233+ 0 ));
234+ // 6 - Test property - securerandom.strongAlgorithms missing algorithm.
235+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-MissingAlgo" ,
236+ System .getProperty ("test.src" ) + "/property-java.security" ,
237+ "FAILED: No strong SecureRandom impls available: .*" ,
238+ 0 ));
239+ // 7 - Test property - securerandom.strongAlgorithms missing provider.
240+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-MissingProvider" ,
241+ System .getProperty ("test.src" ) + "/property-java.security" ,
242+ "FAILED: missing provider" ,
243+ 0 ));
244+ // 8 - Test property - set invalid provider.
245+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-InvalidProvider" ,
246+ System .getProperty ("test.src" ) + "/property-java.security" ,
247+ "FAILED: No strong SecureRandom impls available: .*" ,
248+ 0 ));
249+ // 9 - Test property - securerandom.strongAlgorithms when only algorithm is present.
250+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-Specify-Algo-Only" ,
251+ System .getProperty ("test.src" ) + "/property-java.security" ,
252+ "securerandom\\ .strongAlgorithms: SHA(256|512)DRBG$" ,
253+ 0 ));
254+ // 10 - Test property - invalid algorithm.
255+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-InvalidAlgorithm" ,
256+ System .getProperty ("test.src" ) + "/property-java.security" ,
257+ "FAILED: No strong SecureRandom impls available: .*" ,
258+ 0 ));
259+ // 11 - Test property - securerandom.strongAlgorithms misspelled property name.
260+ tests .add (Arguments .of ("Test-Profile-strongAlgorithms-MisspelledPropertyName" ,
261+ System .getProperty ("test.src" ) + "/property-java.security" ,
262+ "The property names: RestrictedSecurity.Test-Profile-strongAlgorithms-MisspelledPropertyName.securerandom.strongAlgorithmsWrong "
263+ + "in profile RestrictedSecurity.Test-Profile-strongAlgorithms-MisspelledPropertyName \\ (or a base profile\\ ) are not recognized" ,
264+ 1 ));
265+ }
266+
267+ return tests .build ();
268+ }
269+
205270 @ ParameterizedTest
206271 @ MethodSource ("patternMatches_expectedExitValue0" )
207272 public void shouldContain_expectedExitValue0 (String customprofile , String securityPropertyFile , String expected ) throws Exception {
@@ -241,6 +306,19 @@ public void shouldContain_propertiesList(String customprofile, String securityPr
241306 outputAnalyzer .shouldHaveExitValue (exitValue ).shouldMatch (expected );
242307 }
243308
309+ @ ParameterizedTest
310+ @ MethodSource ("patternMatches_strongAlgorithms" )
311+ public void shouldContain_strongAlgorithms (String customprofile , String securityPropertyFile , String expected , int exitValue ) throws Exception {
312+ OutputAnalyzer outputAnalyzer = ProcessTools .executeTestJava (
313+ "-Dsemeru.fips=true" ,
314+ "-Dsemeru.customprofile=" + customprofile ,
315+ "-Djava.security.properties=" + securityPropertyFile ,
316+ "TestProperties"
317+ );
318+ outputAnalyzer .reportDiagnosticSummary ();
319+ outputAnalyzer .shouldHaveExitValue (exitValue ).shouldMatch (expected );
320+ }
321+
244322 private static boolean isProviderPresent (String providerName ) {
245323 for (Provider provider : Security .getProviders ()) {
246324 if (provider .getName ().equalsIgnoreCase (providerName )) {
@@ -250,13 +328,28 @@ private static boolean isProviderPresent(String providerName) {
250328 return false ;
251329 }
252330
331+ private static void testStrongAlgorithms () {
332+ if (isProviderPresent ("OpenJCEPlusFIPS" )) {
333+ String strongAlgorithms = Security .getProperty ("securerandom.strongAlgorithms" );
334+ if ((strongAlgorithms != null ) && !strongAlgorithms .isEmpty ()) {
335+ try {
336+ java .security .SecureRandom .getInstanceStrong ();
337+ System .out .println ("securerandom.strongAlgorithms: " + strongAlgorithms );
338+ } catch (java .security .NoSuchAlgorithmException | IllegalArgumentException e ) {
339+ System .out .println ("FAILED: " + e .getMessage ());
340+ }
341+ }
342+ }
343+ }
344+
253345 public static void main (String [] args ) {
254346 // Something to trigger "properties" debug output.
255347 try {
256348 for (Provider provider : Security .getProviders ()) {
257349 System .out .println ("Provider Name: " + provider .getName ());
258350 System .out .println ("Provider Version: " + provider .getVersionStr ());
259351 }
352+ testStrongAlgorithms ();
260353 } catch (Exception e ) {
261354 System .out .println (e );
262355 }
0 commit comments