KeyStoreManagerImpl extends ComponentSupport implements KeyStoreManager { @Override public void generateAndStoreKeyPair(final String commonName, final String organizationalUnit, final String organization, final String locality, final String state, final String country) throws KeystoreException { privateKeyStoreLock.writeLock().lock(); try { privateKeyStore.generateKeyPair(PRIVATE_KEY_ALIAS, config.getPrivateKeyStorePassword(), config.getPrivateKeyPassword(), config.getKeyAlgorithm(), config.getKeyAlgorithmSize(), config.getSignatureAlgorithm(), config.getCertificateValidity().toDaysI(), commonName, organizationalUnit, organization, locality, state, country); getKeyManagers(); } finally { privateKeyStoreLock.writeLock().unlock(); } } KeyStoreManagerImpl(final CryptoHelper crypto,
                             final KeyStoreStorageManager storageManager,
                             final KeyStoreManagerConfiguration config); @VisibleForTesting  KeyStoreManagerImpl(final CryptoHelper crypto,
                      final KeyStoreManagerConfiguration config,
                      final KeystoreInstance privateKeyStore,
                      final KeystoreInstance trustedKeyStore); @Override TrustManager[] getTrustManagers(); @Override KeyManager[] getKeyManagers(); @Override void importTrustCertificate(Certificate certificate, String alias); @Override void importTrustCertificate(String certificateInPEM, String alias); @Override Certificate getTrustedCertificate(String alias); @Override Collection<Certificate> getTrustedCertificates(); @Override void removeTrustCertificate(String alias); @Override void generateAndStoreKeyPair(final String commonName,
                                      final String organizationalUnit,
                                      final String organization,
                                      final String locality,
                                      final String state,
                                      final String country); @Override boolean isKeyPairInitialized(); @Override Certificate getCertificate(); @Override PrivateKey getPrivateKey(); @Override void removePrivateKey();  }
@Test public void testConcurrentGenerateAndStoreKeyPair() throws Exception { KeyStoreManagerConfiguration configuration = createMockConfiguration(); KeystoreInstance privateStore = mock(KeystoreInstance.class); CountDownLatch block = new CountDownLatch(1); KeyStoreManagerImpl manager = new KeyStoreManagerImpl(crypto, configuration, privateStore, null); doAnswer(blockingAnswer(block)) .when(privateStore) .generateKeyPair(any(String.class), any(char[].class), any(char[].class), any(String.class), anyInt(), any(String.class), anyInt(), any(String.class), any(String.class), any(String.class), any(String.class), any(String.class), any(String.class)); ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1)); List<ListenableFuture<String>> futures = new ArrayList<>(); try { futures.add(service.submit(() -> { manager.generateAndStoreKeyPair("concurrency-1", "ou", "o", "l", "st", "c"); return "concurrency-1"; })); futures.add(service.submit(() -> { manager.generateAndStoreKeyPair("concurrency-2", "ou", "o", "l", "st", "c"); return "concurrency-2"; })); List<String> results = Futures.successfulAsList(futures).get(100, TimeUnit.MILLISECONDS); assertEquals(0, results.size()); } catch (TimeoutException e) { } finally { block.countDown(); service.shutdownNow(); } verify(privateStore, times(1)) .generateKeyPair(any(String.class), any(char[].class), any(char[].class), any(String.class), anyInt(), any(String.class), anyInt(), any(String.class), any(String.class), any(String.class), any(String.class), any(String.class), any(String.class)); }