Move MockPkcs11Security to spring-boot-testsupport

Closes gh-44260
This commit is contained in:
Andy Wilkinson
2025-02-13 14:48:16 +00:00
parent 874ee9936a
commit 2e00552130
9 changed files with 35 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.io.ApplicationResourceLoader;
import org.springframework.boot.web.embedded.test.MockPkcs11Security;
import org.springframework.boot.testsupport.ssl.MockPkcs11Security;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,8 +31,8 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.springframework.boot.testsupport.junit.DisabledOnOs;
import org.springframework.boot.web.embedded.test.MockPkcs11Security;
import org.springframework.boot.web.embedded.test.MockPkcs11SecurityProvider;
import org.springframework.boot.testsupport.ssl.MockPkcs11Security;
import org.springframework.boot.testsupport.ssl.MockPkcs11SecurityProvider;
import org.springframework.boot.web.server.Http2;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.server.WebServerSslBundle;

View File

@@ -1,135 +0,0 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.web.embedded.test;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStoreSpi;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* Mock Security Provider for testing purposes.
*
* @author Cyril Dangerville
*/
public class MockKeyStoreSpi extends KeyStoreSpi {
private static final KeyPairGenerator KEYGEN;
static {
try {
KEYGEN = KeyPairGenerator.getInstance("RSA");
KEYGEN.initialize(2048);
}
catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException(ex);
}
}
private final Map<String, KeyPair> aliases = new HashMap<>();
@Override
public Key engineGetKey(String alias, char[] password) {
final KeyPair keyPair = this.aliases.get(alias);
return (keyPair != null) ? keyPair.getPrivate() : null;
}
@Override
public Certificate[] engineGetCertificateChain(String alias) {
return new Certificate[0];
}
@Override
public Certificate engineGetCertificate(String alias) {
throw new UnsupportedOperationException();
}
@Override
public Date engineGetCreationDate(String alias) {
throw new UnsupportedOperationException();
}
@Override
public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain) {
throw new UnsupportedOperationException();
}
@Override
public void engineSetKeyEntry(String alias, byte[] key, Certificate[] chain) {
throw new UnsupportedOperationException();
}
@Override
public void engineSetCertificateEntry(String alias, Certificate cert) {
throw new UnsupportedOperationException();
}
@Override
public void engineDeleteEntry(String alias) {
throw new UnsupportedOperationException();
}
@Override
public Enumeration<String> engineAliases() {
return Collections.enumeration(this.aliases.keySet());
}
@Override
public boolean engineContainsAlias(String alias) {
this.aliases.put(alias, KEYGEN.generateKeyPair());
return true;
}
@Override
public int engineSize() {
throw new UnsupportedOperationException();
}
@Override
public boolean engineIsKeyEntry(String alias) {
return this.aliases.containsKey(alias);
}
@Override
public boolean engineIsCertificateEntry(String alias) {
return false;
}
@Override
public String engineGetCertificateAlias(Certificate cert) {
throw new UnsupportedOperationException();
}
@Override
public void engineStore(OutputStream stream, char[] password) {
throw new UnsupportedOperationException();
}
@Override
public void engineLoad(InputStream stream, char[] password) {
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.web.embedded.test;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* JUnit {@link ExtendWith @ExtendWith} annotation to support
* {@link MockPkcs11SecurityProvider}.
*
* @author Phillip Webb
*/
@ExtendWith(MockPkcs11SecurityProviderExtension.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface MockPkcs11Security {
}

View File

@@ -1,40 +0,0 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.web.embedded.test;
import java.security.Provider;
/**
* Mock PKCS#11 Security Provider for testing purposes.
*
* @author Cyril Dangerville
*/
public class MockPkcs11SecurityProvider extends Provider {
/**
* The name of the mock provider.
*/
public static final String NAME = "Mock-PKCS11";
static final MockPkcs11SecurityProvider INSTANCE = new MockPkcs11SecurityProvider();
MockPkcs11SecurityProvider() {
super(NAME, "0.1", "Mock PKCS11 Provider");
putService(new Service(this, "KeyStore", "PKCS11", MockKeyStoreSpi.class.getName(), null, null));
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.web.embedded.test;
import java.security.Security;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* {@link Extension} to support {@link MockPkcs11SecurityProvider}.
*
* @author Phillip Webb
* @see MockPkcs11Security
*/
class MockPkcs11SecurityProviderExtension implements BeforeAllCallback, AfterAllCallback {
@Override
public void beforeAll(ExtensionContext context) throws Exception {
Security.addProvider(MockPkcs11SecurityProvider.INSTANCE);
}
@Override
public void afterAll(ExtensionContext context) throws Exception {
Security.removeProvider(MockPkcs11SecurityProvider.NAME);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,10 +28,10 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.testsupport.ssl.MockPkcs11Security;
import org.springframework.boot.testsupport.ssl.MockPkcs11SecurityProvider;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
import org.springframework.boot.web.embedded.test.MockPkcs11Security;
import org.springframework.boot.web.embedded.test.MockPkcs11SecurityProvider;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.server.WebServerSslBundle;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,8 +22,8 @@ import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.ssl.SslBundleKey;
import org.springframework.boot.ssl.SslOptions;
import org.springframework.boot.ssl.SslStoreBundle;
import org.springframework.boot.web.embedded.test.MockPkcs11Security;
import org.springframework.boot.web.embedded.test.MockPkcs11SecurityProvider;
import org.springframework.boot.testsupport.ssl.MockPkcs11Security;
import org.springframework.boot.testsupport.ssl.MockPkcs11SecurityProvider;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;