gnodet-bot commented on code in PR #28106:
URL: https://github.com/apache/camel/pull/28106#discussion_r4312881608


##########
components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCMaterialConsistencyTest.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.pqc;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Guards the consistency rules the sibling key material classes are meant to 
follow but drifted away from, so the next
+ * clone-and-tweak does not reintroduce the divergence.
+ */
+class PQCMaterialConsistencyTest {
+
+    private static Path materialSourceRoot() throws URISyntaxException {
+        // the tests run from the module directory, so the sources sit at a 
fixed relative path
+        Path root = 
Paths.get("src/main/java/org/apache/camel/component/pqc/crypto");
+        assertTrue(Files.isDirectory(root), "cannot locate the key material 
sources at " + root.toAbsolutePath());
+        return root;
+    }
+
+    private static List<Path> materialSources() throws Exception {
+        List<Path> sources = new ArrayList<>();
+        try (Stream<Path> files = Files.walk(materialSourceRoot())) {
+            files.filter(p -> 
p.getFileName().toString().startsWith("PQCDefault"))
+                    .filter(p -> 
p.getFileName().toString().endsWith("Material.java"))
+                    .forEach(sources::add);
+        }
+        assertFalse(sources.isEmpty(), "no key material sources found");
+        return sources;
+    }
+
+    @Test
+    void everyKeyMaterialSeedsItsGeneratorWithSecureRandomHelper() throws 
Exception {
+        List<String> offenders = new ArrayList<>();
+        for (Path source : materialSources()) {
+            String text = Files.readString(source, StandardCharsets.UTF_8);
+            if (!text.contains("SecureRandomHelper.getSecureRandom()")) {
+                offenders.add(source.getFileName().toString());
+            }
+        }
+
+        assertTrue(offenders.isEmpty(),
+                "these key material classes fall back to the provider default 
RNG instead of the shared"
+                                        + " SecureRandomHelper: " + offenders);
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "MAYO", "SNOVA" })
+    void mayoAndSnovaAcceptANamedParameterSet(String algorithm) {
+        assertTrue(PQCParameterSpecResolver.isSupported(algorithm),
+                algorithm + " has a name-addressable BouncyCastle parameter 
spec and must accept parameterSpec");
+    }
+
+    @Test
+    void mayoResolvesEveryDocumentedLevel() {
+        for (String name : new String[] { "mayo1", "mayo2", "mayo3", "mayo5" 
}) {
+            assertTrue(PQCParameterSpecResolver.resolve("MAYO", 
name).toString().toLowerCase().contains("mayo"),
+                    "MAYO must resolve " + name);

Review Comment:
   ⚠️ **Fragile assertion + latent NPE risk.** This assertion works today only 
by accident: `MayoParameterSpec` has no `toString()` override, so 
`Object.toString()` returns 
`org.bouncycastle.pqc.jcajce.spec.MayoParameterSpec@<hash>` — the class name 
happens to contain `"mayo"` after `.toLowerCase()`. If BouncyCastle ever adds a 
`toString()` that doesn't include the word `"mayo"`, this test silently breaks.
   
   More critically: `MayoParameterSpec.fromName()` is a map lookup that returns 
**`null`** for unknown parameter-set names (it does not throw). If 
`doResolve()` returns null (e.g. because a new MAYO level name isn't in the map 
yet), `.toString()` will NPE instead of failing with a meaningful assertion 
message.
   
   `MayoParameterSpec` exposes `getName()` which returns the canonical level 
name (`"mayo1"`, `"mayo2"`, etc.). Use that:
   
   ```suggestion
           for (String name : new String[] { "mayo1", "mayo2", "mayo3", "mayo5" 
}) {
               AlgorithmParameterSpec spec = 
PQCParameterSpecResolver.resolve("MAYO", name);
               assertNotNull(spec, "MAYO must resolve " + name);
               assertInstanceOf(MayoParameterSpec.class, spec,
                       "MAYO must resolve to MayoParameterSpec for " + name);
               assertEquals(name, ((MayoParameterSpec) spec).getName(),
                       "resolved MAYO parameter set name must match requested " 
+ name);
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to