Repository: nifi
Updated Branches:
  refs/heads/master fd0dd51ff -> fa5da543e


http://git-wip-us.apache.org/repos/asf/nifi/blob/fa5da543/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneTest.java
----------------------------------------------------------------------
diff --git 
a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneTest.java
 
b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneTest.java
index 6746c40..af81eae 100644
--- 
a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneTest.java
+++ 
b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneTest.java
@@ -18,27 +18,36 @@
 package org.apache.nifi.toolkit.tls.standalone;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.nifi.security.util.CertificateUtils;
 import org.apache.nifi.toolkit.tls.commandLine.BaseCommandLine;
 import org.apache.nifi.toolkit.tls.commandLine.ExitCode;
 import org.apache.nifi.toolkit.tls.configuration.TlsConfig;
+import org.apache.nifi.toolkit.tls.manager.BaseTlsManager;
+import org.apache.nifi.toolkit.tls.service.TlsCertificateAuthorityTest;
 import org.apache.nifi.toolkit.tls.util.TlsHelperTest;
 import org.apache.nifi.util.NiFiProperties;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.KeyPair;
 import java.security.KeyStore;
 import java.security.NoSuchAlgorithmException;
 import java.security.Permission;
+import java.security.PrivateKey;
+import java.security.PublicKey;
 import java.security.cert.Certificate;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
 import java.security.spec.InvalidKeySpecException;
+import java.util.List;
 import java.util.Properties;
 import java.util.UUID;
 
@@ -95,18 +104,18 @@ public class TlsToolkitStandaloneTest {
 
     @Test
     public void testBadParse() {
-        runAndAssertExitCode(ExitCode.ERROR_PARSING_COMMAND_LINE.ordinal(), 
"--unknownArgument");
+        runAndAssertExitCode(ExitCode.ERROR_PARSING_COMMAND_LINE, 
"--unknownArgument");
     }
 
     @Test
     public void testHelp() {
-        runAndAssertExitCode(ExitCode.HELP.ordinal(), "-h");
-        runAndAssertExitCode(ExitCode.HELP.ordinal(), "--help");
+        runAndAssertExitCode(ExitCode.HELP, "-h");
+        runAndAssertExitCode(ExitCode.HELP, "--help");
     }
 
     @Test
     public void testDirOutput() throws Exception {
-        runAndAssertExitCode(0, "-o", tempDir.getAbsolutePath());
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-n", TlsConfig.DEFAULT_HOSTNAME);
         X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
 
         Properties nifiProperties = 
checkHostDirAndReturnNifiProperties(TlsConfig.DEFAULT_HOSTNAME, 
x509Certificate);
@@ -116,7 +125,7 @@ public class TlsToolkitStandaloneTest {
 
     @Test
     public void testDifferentArg() throws Exception {
-        runAndAssertExitCode(0, "-o", tempDir.getAbsolutePath(), "-g");
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-g", "-n", TlsConfig.DEFAULT_HOSTNAME);
         X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
 
         Properties nifiProperties = 
checkHostDirAndReturnNifiProperties(TlsConfig.DEFAULT_HOSTNAME, 
x509Certificate);
@@ -126,7 +135,7 @@ public class TlsToolkitStandaloneTest {
 
     @Test
     public void testFileArg() throws Exception {
-        runAndAssertExitCode(0, "-o", tempDir.getAbsolutePath(), "-f", 
TEST_NIFI_PROPERTIES);
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-f", TEST_NIFI_PROPERTIES, "-n", 
TlsConfig.DEFAULT_HOSTNAME);
         X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
 
         Properties nifiProperties = 
checkHostDirAndReturnNifiProperties(TlsConfig.DEFAULT_HOSTNAME, 
x509Certificate);
@@ -134,23 +143,36 @@ public class TlsToolkitStandaloneTest {
     }
 
     @Test
-    public void testHostnamesArgument() throws Exception {
+    public void testHostnamesArgumentOverwrite() throws Exception {
         String nifi1 = "nifi1";
         String nifi2 = "nifi2";
         String nifi3 = "nifi3";
 
-        runAndAssertExitCode(0, "-o", tempDir.getAbsolutePath(), "-n", nifi1 + 
"," + nifi2 + "," + nifi3);
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-n", nifi1 + "," + nifi2);
         X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-n", nifi3);
 
         checkHostDirAndReturnNifiProperties(nifi1, x509Certificate);
         checkHostDirAndReturnNifiProperties(nifi2, x509Certificate);
         checkHostDirAndReturnNifiProperties(nifi3, x509Certificate);
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-O", "-n", nifi3);
+        checkHostDirAndReturnNifiProperties(nifi3, x509Certificate);
+    }
+
+    @Test
+    public void testHostnamesArgumentNoOverwrite() throws Exception {
+        String nifi = "nifi";
+
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-n", nifi);
+        X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
+        checkHostDirAndReturnNifiProperties(nifi, x509Certificate);
+        runAndAssertExitCode(ExitCode.ERROR_GENERATING_CONFIG, "-o", 
tempDir.getAbsolutePath(), "-n", nifi);
     }
 
     @Test
     public void testKeyPasswordArg() throws Exception {
         String testKey = "testKey";
-        runAndAssertExitCode(0, "-o", tempDir.getAbsolutePath(), "-K", 
testKey);
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-K", testKey, "-n", TlsConfig.DEFAULT_HOSTNAME);
         X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
 
         Properties nifiProperties = 
checkHostDirAndReturnNifiProperties(TlsConfig.DEFAULT_HOSTNAME, 
x509Certificate);
@@ -160,7 +182,7 @@ public class TlsToolkitStandaloneTest {
     @Test
     public void testKeyStorePasswordArg() throws Exception {
         String testKeyStore = "testKeyStore";
-        runAndAssertExitCode(0, "-o", tempDir.getAbsolutePath(), "-S", 
testKeyStore);
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-S", testKeyStore, "-n", 
TlsConfig.DEFAULT_HOSTNAME);
         X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
 
         Properties nifiProperties = 
checkHostDirAndReturnNifiProperties(TlsConfig.DEFAULT_HOSTNAME, 
x509Certificate);
@@ -170,13 +192,38 @@ public class TlsToolkitStandaloneTest {
     @Test
     public void testTrustStorePasswordArg() throws Exception {
         String testTrustStore = "testTrustStore";
-        runAndAssertExitCode(0, "-o", tempDir.getAbsolutePath(), "-P", 
testTrustStore);
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-P", testTrustStore, "-n", 
TlsConfig.DEFAULT_HOSTNAME);
         X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
 
         Properties nifiProperties = 
checkHostDirAndReturnNifiProperties(TlsConfig.DEFAULT_HOSTNAME, 
x509Certificate);
         assertEquals(testTrustStore, 
nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_PASSWD));
     }
 
+    @Test
+    public void testClientDnsArg() throws Exception {
+        String clientDn = "OU=NIFI,CN=testuser";
+        String clientDn2 = "OU=NIFI,CN=testuser2";
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-C", clientDn, "-C", clientDn2, "-B", "pass1", 
"-P", "pass2");
+        X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
+
+        checkClientCert(clientDn, x509Certificate);
+        checkClientCert(clientDn2, x509Certificate);
+
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-O", "-C", clientDn2, "-B", "pass3");
+        checkClientCert(clientDn2, x509Certificate);
+    }
+
+    @Test
+    public void testClientDnsArgNoOverwrite() throws Exception {
+        String clientDn = "OU=NIFI,CN=testuser";
+        runAndAssertExitCode(ExitCode.SUCCESS, "-o", 
tempDir.getAbsolutePath(), "-C", clientDn, "-B", "passwor");
+        X509Certificate x509Certificate = 
checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
+
+        checkClientCert(clientDn, x509Certificate);
+
+        runAndAssertExitCode(ExitCode.ERROR_GENERATING_CONFIG, "-o", 
tempDir.getAbsolutePath(), "-C", clientDn);
+    }
+
     private X509Certificate checkLoadCertPrivateKey(String algorithm) throws 
IOException, NoSuchAlgorithmException, InvalidKeySpecException, 
CertificateException {
         KeyPair keyPair = TlsHelperTest.loadKeyPair(new File(tempDir, 
TlsToolkitStandalone.NIFI_KEY + ".key"));
 
@@ -227,15 +274,40 @@ public class TlsToolkitStandaloneTest {
         assertEquals(rootCert, certificateChain[1]);
         certificateChain[1].verify(rootCert.getPublicKey());
         certificateChain[0].verify(rootCert.getPublicKey());
+        
TlsCertificateAuthorityTest.assertPrivateAndPublicKeyMatch(privateKeyEntry.getPrivateKey(),
 certificateChain[0].getPublicKey());
         return nifiProperties;
     }
 
-    private void runAndAssertExitCode(int exitCode, String... args) {
+    private void checkClientCert(String clientDn, X509Certificate rootCert) 
throws Exception {
+        String clientDnFile = 
TlsToolkitStandalone.getClientDnFile(CertificateUtils.reorderDn(clientDn));
+        String password;
+        try (FileReader fileReader = new FileReader(new File(tempDir, 
clientDnFile + ".password"))) {
+            List<String> lines = IOUtils.readLines(fileReader);
+            assertEquals(1, lines.size());
+            password = lines.get(0);
+        }
+
+        KeyStore keyStore = KeyStore.getInstance(BaseTlsManager.PKCS_12, 
BouncyCastleProvider.PROVIDER_NAME);
+        try (FileInputStream fileInputStream = new FileInputStream(new 
File(tempDir, clientDnFile + ".p12"))) {
+            keyStore.load(fileInputStream, password.toCharArray());
+        }
+        PrivateKey privateKey = (PrivateKey) 
keyStore.getKey(TlsToolkitStandalone.NIFI_KEY, new char[0]);
+        Certificate[] certificateChain = 
keyStore.getCertificateChain(TlsToolkitStandalone.NIFI_KEY);
+        assertEquals(2, certificateChain.length);
+        assertEquals(rootCert, certificateChain[1]);
+        certificateChain[1].verify(rootCert.getPublicKey());
+        certificateChain[0].verify(rootCert.getPublicKey());
+        PublicKey publicKey = certificateChain[0].getPublicKey();
+        TlsCertificateAuthorityTest.assertPrivateAndPublicKeyMatch(privateKey, 
publicKey);
+
+    }
+
+    private void runAndAssertExitCode(ExitCode exitCode, String... args) {
         try {
             TlsToolkitStandaloneCommandLine.main(args);
             fail("Expecting exit code: " + exitCode);
         } catch (ExitException e) {
-            assertEquals(exitCode, e.getExitCode());
+            assertEquals(exitCode, ExitCode.values()[e.getExitCode()]);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/fa5da543/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/util/TlsHelperTest.java
----------------------------------------------------------------------
diff --git 
a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/util/TlsHelperTest.java
 
b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/util/TlsHelperTest.java
index a900a4f..66ec890 100644
--- 
a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/util/TlsHelperTest.java
+++ 
b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/util/TlsHelperTest.java
@@ -25,23 +25,32 @@ import org.bouncycastle.openssl.PEMKeyPair;
 import org.bouncycastle.openssl.PEMParser;
 import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
 import org.bouncycastle.operator.OperatorCreationException;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.AdditionalMatchers;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
 import java.security.GeneralSecurityException;
 import java.security.InvalidKeyException;
 import java.security.KeyPair;
 import java.security.KeyPairGenerator;
 import java.security.KeyStore;
+import java.security.KeyStoreSpi;
 import java.security.NoSuchAlgorithmException;
 import java.security.NoSuchProviderException;
-import java.security.SecureRandom;
+import java.security.Provider;
 import java.security.Security;
 import java.security.SignatureException;
 import java.security.cert.CertificateException;
@@ -51,9 +60,17 @@ import java.util.concurrent.TimeUnit;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+@RunWith(MockitoJUnitRunner.class)
 public class TlsHelperTest {
+    private static final boolean originalUnlimitedCrypto = 
TlsHelper.isUnlimitedStrengthCryptographyEnabled();
+
     private int days;
 
     private int keySize;
@@ -62,11 +79,32 @@ public class TlsHelperTest {
 
     private String signingAlgorithm;
 
-    private String keyStoreType;
+    private KeyPairGenerator keyPairGenerator;
 
-    private SecureRandom secureRandom;
+    private KeyStore keyStore;
 
-    private KeyPairGenerator keyPairGenerator;
+    @Mock
+    KeyStoreSpi keyStoreSpi;
+
+    @Mock
+    Provider keyStoreProvider;
+
+    @Mock
+    OutputStreamFactory outputStreamFactory;
+
+    private ByteArrayOutputStream tmpFileOutputStream;
+
+    private File file;
+
+    private static void setUnlimitedCrypto(boolean value) {
+        try {
+            Field isUnlimitedStrengthCryptographyEnabled = 
TlsHelper.class.getDeclaredField("isUnlimitedStrengthCryptographyEnabled");
+            isUnlimitedStrengthCryptographyEnabled.setAccessible(true);
+            isUnlimitedStrengthCryptographyEnabled.set(null, value);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
 
     public static KeyPair loadKeyPair(Reader reader) throws IOException {
         try (PEMParser pemParser = new PEMParser(reader)) {
@@ -98,15 +136,25 @@ public class TlsHelperTest {
     }
 
     @Before
-    public void setup() throws NoSuchAlgorithmException {
+    public void setup() throws Exception {
         days = 360;
         keySize = 2048;
         keyPairAlgorithm = "RSA";
         signingAlgorithm = "SHA1WITHRSA";
-        keyStoreType = KeyStore.getDefaultType();
-        secureRandom = mock(SecureRandom.class);
         keyPairGenerator = KeyPairGenerator.getInstance(keyPairAlgorithm);
         keyPairGenerator.initialize(keySize);
+        Constructor<KeyStore> keyStoreConstructor = 
KeyStore.class.getDeclaredConstructor(KeyStoreSpi.class, Provider.class, 
String.class);
+        keyStoreConstructor.setAccessible(true);
+        keyStore = keyStoreConstructor.newInstance(keyStoreSpi, 
keyStoreProvider, "faketype");
+        keyStore.load(null, null);
+        file = File.createTempFile("keystore", "file");
+        when(outputStreamFactory.create(file)).thenReturn(tmpFileOutputStream);
+    }
+
+    @After
+    public void tearDown() {
+        setUnlimitedCrypto(originalUnlimitedCrypto);
+        file.delete();
     }
 
     private Date inFuture(int days) {
@@ -127,7 +175,7 @@ public class TlsHelperTest {
         assertTrue(notBefore.after(inFuture(-1)));
         assertTrue(notBefore.before(inFuture(1)));
 
-        assertEquals(dn, x509Certificate.getIssuerDN().getName());
+        assertEquals(dn, x509Certificate.getIssuerX500Principal().getName());
         assertEquals(signingAlgorithm, x509Certificate.getSigAlgName());
         assertEquals(keyPairAlgorithm, 
x509Certificate.getPublicKey().getAlgorithm());
 
@@ -139,12 +187,12 @@ public class TlsHelperTest {
         X509Certificate issuer = loadCertificate(new 
InputStreamReader(getClass().getClassLoader().getResourceAsStream("rootCert.crt")));
         KeyPair issuerKeyPair = loadKeyPair(new 
InputStreamReader(getClass().getClassLoader().getResourceAsStream("rootCert.key")));
 
-        String dn = "CN=testIssued,O=testOrg";
+        String dn = "CN=testIssued, O=testOrg";
 
         KeyPair keyPair = TlsHelper.generateKeyPair(keyPairAlgorithm, keySize);
         X509Certificate x509Certificate = 
CertificateUtils.generateIssuedCertificate(dn, keyPair.getPublic(), issuer, 
issuerKeyPair, signingAlgorithm, days);
-        assertEquals(dn, x509Certificate.getSubjectDN().toString());
-        assertEquals(issuer.getSubjectDN().toString(), 
x509Certificate.getIssuerDN().toString());
+        assertEquals(dn, x509Certificate.getSubjectX500Principal().toString());
+        assertEquals(issuer.getSubjectX500Principal().toString(), 
x509Certificate.getIssuerX500Principal().toString());
         assertEquals(keyPair.getPublic(), x509Certificate.getPublicKey());
 
         Date notAfter = x509Certificate.getNotAfter();
@@ -160,4 +208,83 @@ public class TlsHelperTest {
 
         x509Certificate.verify(issuerKeyPair.getPublic());
     }
+
+    @Test
+    public void testWriteKeyStoreSuccess() throws IOException, 
GeneralSecurityException {
+        setUnlimitedCrypto(false);
+        String testPassword = "testPassword";
+        assertEquals(testPassword, TlsHelper.writeKeyStore(keyStore, 
outputStreamFactory, file, testPassword, false));
+        verify(keyStoreSpi, times(1)).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(testPassword.toCharArray()));
+    }
+
+    @Test
+    public void testWriteKeyStoreFailure() throws IOException, 
GeneralSecurityException {
+        setUnlimitedCrypto(false);
+        String testPassword = "testPassword";
+        IOException ioException = new IOException("Fail");
+        
doThrow(ioException).when(keyStoreSpi).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(testPassword.toCharArray()));
+        try {
+            TlsHelper.writeKeyStore(keyStore, outputStreamFactory, file, 
testPassword, true);
+            fail("Expected " + ioException);
+        } catch (IOException e) {
+            assertEquals(ioException, e);
+        }
+    }
+
+    @Test
+    public void testWriteKeyStoreTruncate() throws IOException, 
GeneralSecurityException {
+        setUnlimitedCrypto(false);
+        String testPassword = "testPassword";
+        String truncatedPassword = testPassword.substring(0, 7);
+        IOException ioException = new IOException(TlsHelper.ILLEGAL_KEY_SIZE);
+        
doThrow(ioException).when(keyStoreSpi).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(testPassword.toCharArray()));
+        assertEquals(truncatedPassword, TlsHelper.writeKeyStore(keyStore, 
outputStreamFactory, file, testPassword, true));
+        verify(keyStoreSpi, times(1)).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(testPassword.toCharArray()));
+        verify(keyStoreSpi, times(1)).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(truncatedPassword.toCharArray()));
+    }
+
+    @Test
+    public void testWriteKeyStoreUnlimitedWontTruncate() throws 
GeneralSecurityException, IOException {
+        setUnlimitedCrypto(true);
+        String testPassword = "testPassword";
+        IOException ioException = new IOException(TlsHelper.ILLEGAL_KEY_SIZE);
+        
doThrow(ioException).when(keyStoreSpi).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(testPassword.toCharArray()));
+        try {
+            TlsHelper.writeKeyStore(keyStore, outputStreamFactory, file, 
testPassword, true);
+            fail("Expected " + ioException);
+        } catch (IOException e) {
+            assertEquals(ioException, e);
+        }
+    }
+
+    @Test
+    public void testWriteKeyStoreNoTruncate() throws IOException, 
GeneralSecurityException {
+        setUnlimitedCrypto(false);
+        String testPassword = "testPassword";
+        IOException ioException = new IOException(TlsHelper.ILLEGAL_KEY_SIZE);
+        
doThrow(ioException).when(keyStoreSpi).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(testPassword.toCharArray()));
+        try {
+            TlsHelper.writeKeyStore(keyStore, outputStreamFactory, file, 
testPassword, false);
+            fail("Expected " + GeneralSecurityException.class);
+        } catch (GeneralSecurityException e) {
+            assertTrue("Expected exception to contain " + TlsHelper.JCE_URL, 
e.getMessage().contains(TlsHelper.JCE_URL));
+        }
+    }
+
+    @Test
+    public void testWriteKeyStoreTruncateFailure() throws IOException, 
GeneralSecurityException {
+        setUnlimitedCrypto(false);
+        String testPassword = "testPassword";
+        String truncatedPassword = testPassword.substring(0, 7);
+        IOException ioException = new IOException(TlsHelper.ILLEGAL_KEY_SIZE);
+        IOException ioException2 = new IOException(TlsHelper.ILLEGAL_KEY_SIZE);
+        
doThrow(ioException).when(keyStoreSpi).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(testPassword.toCharArray()));
+        
doThrow(ioException2).when(keyStoreSpi).engineStore(eq(tmpFileOutputStream), 
AdditionalMatchers.aryEq(truncatedPassword.toCharArray()));
+        try {
+            TlsHelper.writeKeyStore(keyStore, outputStreamFactory, file, 
testPassword, true);
+            fail("Expected " + ioException2);
+        } catch (IOException e) {
+            assertEquals(ioException2, e);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/nifi/blob/fa5da543/nifi-toolkit/nifi-toolkit-tls/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/nifi-toolkit/nifi-toolkit-tls/src/test/resources/log4j.properties 
b/nifi-toolkit/nifi-toolkit-tls/src/test/resources/log4j.properties
new file mode 100644
index 0000000..fc2aaf1
--- /dev/null
+++ b/nifi-toolkit/nifi-toolkit-tls/src/test/resources/log4j.properties
@@ -0,0 +1,22 @@
+#
+# 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.
+#
+
+log4j.rootLogger=INFO,console
+
+log4j.appender.console=org.apache.log4j.ConsoleAppender
+log4j.appender.console.layout=org.apache.log4j.PatternLayout
+log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p 
%c{2}: %m%n
\ No newline at end of file

Reply via email to