jrihtarsic commented on code in PR #234: URL: https://github.com/apache/santuario-xml-security-java/pull/234#discussion_r1384737390
########## src/test/java/org/apache/xml/security/utils/KeyUtilsTest.java: ########## @@ -0,0 +1,93 @@ +/** + * 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.xml.security.utils; + +import org.apache.xml.security.Init; +import org.apache.xml.security.testutils.JDKTestUtils; +import org.apache.xml.security.testutils.KeyTestUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import java.security.KeyPair; +import java.security.Provider; +import java.security.PublicKey; + +/** + * Unit test for {@link KeyUtils} + */ +class KeyUtilsTest { + static { + Init.init(); + } + + /** + * Test if the ephemeral key is generated with the same algorithm as the original key + * @param testKey the enumeration with exiting keys in resource folder + * @throws Exception if the key cannot be loaded + */ + @ParameterizedTest + @EnumSource(KeyTestUtils.TestKeys.class) + void generateEphemeralDHKeyPair(KeyTestUtils.TestKeys testKey) throws Exception { + String keyAlgorithm = testKey.getAlgorithm(); + Assumptions.assumeTrue(JDKTestUtils.isAlgorithmSupported(keyAlgorithm, true), "Algorithm [" + + keyAlgorithm + "] not supported by JDK or auxiliary provider! Skipping test."); + + PublicKey publicKey = KeyTestUtils.loadPublicKey(testKey.getFilename(), testKey.getAlgorithm()); + // when + KeyPair ephenmeralKeyPair = KeyUtils.generateEphemeralDHKeyPair(publicKey, JDKTestUtils.isAlgorithmSupportedByJDK(testKey.getAlgorithm()) ? + null : JDKTestUtils.getAuxiliaryProvider()); + // then + Assertions.assertNotNull(ephenmeralKeyPair); + Assertions.assertNotEquals(publicKey, ephenmeralKeyPair.getPublic()); + Assertions.assertEquals(publicKey.getAlgorithm(), ephenmeralKeyPair.getPublic().getAlgorithm()); + } + + /** + * Test if the ephemeral key is generated with the same algorithm as the original key. The initial keys are generated + * @param keyType the enumeration with most common EC and XEC keys + * @throws Exception if the key cannot be loaded + */ + @ParameterizedTest + @EnumSource(value = KeyUtils.KeyType.class, + names = "^(?!C2TNB).*", mode = EnumSource.Mode.MATCH_ALL // exclude C2TNB keys Review Comment: I have now removed C2TNB from the enum because I put them there for legacy reasons. -- 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: dev-unsubscr...@santuario.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org