|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | +package org.apache.hc.core5.testing.extension; |
| 28 | + |
| 29 | +import java.security.Provider; |
| 30 | +import java.security.Security; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.HashSet; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +import org.conscrypt.Conscrypt; |
| 36 | +import org.junit.jupiter.api.Assertions; |
| 37 | +import org.junit.jupiter.api.Assumptions; |
| 38 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 39 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 40 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 41 | + |
| 42 | +public class SecurityProviderResource implements BeforeEachCallback, AfterEachCallback { |
| 43 | + |
| 44 | + private final String securityProviderName; |
| 45 | + |
| 46 | + private Provider securityProvider; |
| 47 | + |
| 48 | + public SecurityProviderResource(final String securityProviderName) { |
| 49 | + super(); |
| 50 | + this.securityProviderName = securityProviderName; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void beforeEach(final ExtensionContext context) throws Exception { |
| 55 | + if ("Conscrypt".equalsIgnoreCase(securityProviderName)) { |
| 56 | + final Set<String> supportedArchitectures = new HashSet<>(Arrays.asList("x86", "x86_64", |
| 57 | + "x86-64", "amd64", "aarch64", "armeabi-v7a", "arm64-v8a")); |
| 58 | + Assumptions.assumeTrue(supportedArchitectures.contains(System.getProperty("os.arch"))); |
| 59 | + try { |
| 60 | + securityProvider = Conscrypt.newProviderBuilder().provideTrustManager(true).build(); |
| 61 | + } catch (final UnsatisfiedLinkError e) { |
| 62 | + Assertions.fail("Conscrypt provider failed to be loaded: " + e.getMessage()); |
| 63 | + } |
| 64 | + } else if ("Oracle".equalsIgnoreCase(securityProviderName)) { |
| 65 | + securityProvider = null; |
| 66 | + } else if ("SUN".equalsIgnoreCase(securityProviderName)) { |
| 67 | + securityProvider = null; |
| 68 | + } else { |
| 69 | + throw new AssertionError("Unsupported security provider: " + securityProviderName); |
| 70 | + } |
| 71 | + if (securityProvider != null) { |
| 72 | + Security.insertProviderAt(securityProvider, 1); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void afterEach(final ExtensionContext context) throws Exception { |
| 78 | + if (securityProvider != null) { |
| 79 | + Security.removeProvider(securityProvider.getName()); |
| 80 | + securityProvider = null; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public Provider securityProvider() { |
| 85 | + return securityProvider; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments