gnodet commented on code in PR #26486:
URL: https://github.com/apache/camel/pull/26486#discussion_r4059739668


##########
components/camel-xmlsecurity/src/test/java/org/apache/camel/dataformat/xmlsecurity/XmlSecurityConstantNameTest.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.dataformat.xmlsecurity;
+
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.converter.jaxp.XmlConverter;
+import org.apache.camel.support.jsse.KeyStoreParameters;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.xml.security.encryption.XMLCipher;
+import org.apache.xml.security.utils.EncryptionConstants;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Verifies that the Java constant names exposed by the model's {@code 
@Metadata(enums=...)} annotations (e.g.
+ * {@code "AES_256_GCM"}, {@code "RSA_OAEP"}, {@code "SHA256"}, {@code 
"MGF1_SHA256"}) are accepted by
+ * {@link XMLSecurityDataFormat} at runtime as aliases for the corresponding 
W3C URIs.
+ *
+ * <p>
+ * Before the fix, passing a constant name to {@link 
XMLSecurityDataFormat#setXmlCipherAlgorithm(String)} would store it
+ * verbatim and then cause {@code XMLCipher.getInstance("AES_256_GCM")} to 
throw
+ * {@code XMLEncryptionException: Null or empty transformation}, because 
XMLCipher only understands URIs.
+ */
+class XmlSecurityConstantNameTest extends CamelTestSupport {
+
+    TestHelper xmlsecTestHelper = new TestHelper();
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    public void doPostSetup() {
+        context.getGlobalOptions().put(XmlConverter.OUTPUT_PROPERTIES_PREFIX + 
javax.xml.transform.OutputKeys.ENCODING,
+                "UTF-8");
+    }
+
+    // -- resolveAlgorithm unit tests 
-------------------------------------------------------
+
+    @Test
+    void resolveAlgorithmReturnsMappedUri() {
+        assertEquals(XMLCipher.AES_256_GCM, 
XMLSecurityDataFormat.resolveAlgorithm("AES_256_GCM"));
+        assertEquals(XMLCipher.AES_128_GCM, 
XMLSecurityDataFormat.resolveAlgorithm("AES_128_GCM"));
+        assertEquals(XMLCipher.AES_128, 
XMLSecurityDataFormat.resolveAlgorithm("AES_128"));
+        assertEquals(XMLCipher.AES_192, 
XMLSecurityDataFormat.resolveAlgorithm("AES_192"));
+        assertEquals(XMLCipher.TRIPLEDES, 
XMLSecurityDataFormat.resolveAlgorithm("TRIPLEDES"));
+        assertEquals(XMLCipher.SEED_128, 
XMLSecurityDataFormat.resolveAlgorithm("SEED_128"));
+        assertEquals(XMLCipher.CAMELLIA_128, 
XMLSecurityDataFormat.resolveAlgorithm("CAMELLIA_128"));
+        assertEquals(XMLCipher.RSA_v1dot5, 
XMLSecurityDataFormat.resolveAlgorithm("RSA_v1dot5"));
+        assertEquals(XMLCipher.RSA_OAEP, 
XMLSecurityDataFormat.resolveAlgorithm("RSA_OAEP"));
+        assertEquals(XMLCipher.RSA_OAEP_11, 
XMLSecurityDataFormat.resolveAlgorithm("RSA_OAEP_11"));
+        assertEquals(XMLCipher.SHA1, 
XMLSecurityDataFormat.resolveAlgorithm("SHA1"));
+        assertEquals(XMLCipher.SHA256, 
XMLSecurityDataFormat.resolveAlgorithm("SHA256"));
+        assertEquals(XMLCipher.SHA512, 
XMLSecurityDataFormat.resolveAlgorithm("SHA512"));
+        assertEquals(EncryptionConstants.MGF1_SHA1, 
XMLSecurityDataFormat.resolveAlgorithm("MGF1_SHA1"));
+        assertEquals(EncryptionConstants.MGF1_SHA256, 
XMLSecurityDataFormat.resolveAlgorithm("MGF1_SHA256"));
+        assertEquals(EncryptionConstants.MGF1_SHA512, 
XMLSecurityDataFormat.resolveAlgorithm("MGF1_SHA512"));
+    }
+
+    @Test
+    void resolveAlgorithmPassesThroughUri() {
+        // raw W3C URIs must pass through unchanged so existing routes keep 
working
+        assertEquals(XMLCipher.AES_256_GCM, 
XMLSecurityDataFormat.resolveAlgorithm(XMLCipher.AES_256_GCM));
+        assertEquals(XMLCipher.RSA_OAEP, 
XMLSecurityDataFormat.resolveAlgorithm(XMLCipher.RSA_OAEP));
+        assertEquals(XMLCipher.SHA256, 
XMLSecurityDataFormat.resolveAlgorithm(XMLCipher.SHA256));
+    }
+
+    @Test
+    void resolveAlgorithmHandlesNull() {
+        assertEquals(null, XMLSecurityDataFormat.resolveAlgorithm(null));
+    }
+
+    // -- end-to-end encrypt/decrypt tests using constant names 
----------------------------
+
+    /**
+     * Symmetric AES-256-GCM using the constant name "AES_256_GCM" (the YAML 
DSL schema value).
+     */
+    @Test
+    void testAES256GCMByConstantName() throws Exception {
+        KeyGenerator keygen = KeyGenerator.getInstance("AES");
+        keygen.init(256);
+        SecretKey key = keygen.generateKey();
+
+        XMLSecurityDataFormat df = new XMLSecurityDataFormat();
+        df.setPassPhrase(key.getEncoded());
+        df.setSecureTagContents(true);
+        df.setSecureTag("//cheesesites/italy/cheese");
+        // Use the constant name, not the URI
+        df.setXmlCipherAlgorithm("AES_256_GCM");
+
+        // verify the setter resolved it to the URI
+        assertEquals(XMLCipher.AES_256_GCM, df.getXmlCipherAlgorithm());
+

Review Comment:
   Fixed in 25baab161d47e4d14d83cd36cffd1fe7b9ed1d14: removed the stale getter 
assertions (Option A from the review). The setters remain plain assignments in 
this backport; resolution happens lazily at crypto call-sites. The 
encrypt/decrypt tests cover the functional behaviour end-to-end.



-- 
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