|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package com.amazon.corretto.crypto.provider.test; |
| 4 | + |
| 5 | +import static com.amazon.corretto.crypto.provider.test.TestUtil.NATIVE_PROVIDER; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import java.security.KeyPair; |
| 9 | +import java.security.KeyPairGenerator; |
| 10 | +import java.security.Provider; |
| 11 | +import java.security.Security; |
| 12 | +import java.security.Signature; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import org.junit.jupiter.api.AfterAll; |
| 16 | +import org.junit.jupiter.api.BeforeAll; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.junit.jupiter.api.parallel.Execution; |
| 20 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 21 | +import org.junit.jupiter.api.parallel.ResourceAccessMode; |
| 22 | +import org.junit.jupiter.api.parallel.ResourceLock; |
| 23 | + |
| 24 | +@Execution(ExecutionMode.CONCURRENT) |
| 25 | +@ExtendWith(TestResultLogger.class) |
| 26 | +@ResourceLock(value = TestUtil.RESOURCE_GLOBAL, mode = ResourceAccessMode.READ_WRITE) |
| 27 | +// This test is used to prove that functionality tested within it is independent of |
| 28 | +// default-registered JDK providers (SunEC, SunJCE, etc.). We take a global resource |
| 29 | +// lock because other tests rely on default providers and JCA state is global. |
| 30 | +public class RemoveDefaultProvidersTest { |
| 31 | + private static List<Provider> defaultProviders; |
| 32 | + |
| 33 | + @BeforeAll |
| 34 | + static void removeAndStoreProviders() { |
| 35 | + defaultProviders = new ArrayList<>(); |
| 36 | + for (Provider provider : Security.getProviders()) { |
| 37 | + defaultProviders.add(provider); |
| 38 | + Security.removeProvider(provider.getName()); |
| 39 | + } |
| 40 | + assertTrue(Security.getProviders().length == 0); |
| 41 | + } |
| 42 | + |
| 43 | + @AfterAll |
| 44 | + static void restoreProviders() { |
| 45 | + assertTrue(Security.getProviders().length == 0); |
| 46 | + for (Provider provider : defaultProviders) { |
| 47 | + Security.addProvider(provider); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testEdDSASignature() throws Exception { |
| 53 | + final byte[] message = new byte[0]; |
| 54 | + final KeyPair keyPair = |
| 55 | + KeyPairGenerator.getInstance("EdDSA", NATIVE_PROVIDER).generateKeyPair(); |
| 56 | + final Signature signature = Signature.getInstance("EdDSA", NATIVE_PROVIDER); |
| 57 | + signature.initSign(keyPair.getPrivate()); |
| 58 | + signature.update(message); |
| 59 | + final byte[] signatureBytes = signature.sign(); |
| 60 | + signature.initVerify(keyPair.getPublic()); |
| 61 | + signature.update(message); |
| 62 | + assertTrue(signature.verify(signatureBytes)); |
| 63 | + } |
| 64 | +} |
0 commit comments