exceptionfactory commented on a change in pull request #4842:
URL: https://github.com/apache/nifi/pull/4842#discussion_r607291909



##########
File path: 
nifi-nar-bundles/nifi-pgp-bundle/nifi-pgp-processors/src/test/java/org/apache/nifi/processors/pgp/EncryptContentPGPTest.java
##########
@@ -0,0 +1,293 @@
+/*
+ * 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.
+ */
+package org.apache.nifi.processors.pgp;
+
+import org.apache.nifi.pgp.service.api.PGPPublicKeyService;
+import org.apache.nifi.processors.pgp.attributes.CompressionAlgorithm;
+import org.apache.nifi.processors.pgp.attributes.FileEncoding;
+import org.apache.nifi.processors.pgp.attributes.SymmetricKeyAlgorithm;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.stream.io.StreamUtils;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.apache.nifi.pgp.util.PGPSecretKeyGenerator;
+
+import org.bouncycastle.bcpg.ArmoredInputStream;
+import org.bouncycastle.openpgp.PGPCompressedData;
+import org.bouncycastle.openpgp.PGPEncryptedData;
+import org.bouncycastle.openpgp.PGPEncryptedDataList;
+import org.bouncycastle.openpgp.PGPException;
+import org.bouncycastle.openpgp.PGPLiteralData;
+import org.bouncycastle.openpgp.PGPObjectFactory;
+import org.bouncycastle.openpgp.PGPPBEEncryptedData;
+import org.bouncycastle.openpgp.PGPPrivateKey;
+import org.bouncycastle.openpgp.PGPPublicKey;
+import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
+import org.bouncycastle.openpgp.PGPSecretKey;
+import org.bouncycastle.openpgp.PGPSecretKeyRing;
+import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
+import org.bouncycastle.openpgp.operator.PBEDataDecryptorFactory;
+import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
+import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
+import org.bouncycastle.openpgp.operator.bc.BcPBEDataDecryptorFactory;
+import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider;
+import org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory;
+import 
org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Optional;
+import java.util.UUID;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class EncryptContentPGPTest {
+    private static final String PASSPHRASE = UUID.randomUUID().toString();
+
+    private static final String DATA = String.class.getName();
+
+    private static final SymmetricKeyAlgorithm DEFAULT_SYMMETRIC_KEY_ALGORITHM 
= 
SymmetricKeyAlgorithm.valueOf(EncryptContentPGP.SYMMETRIC_KEY_ALGORITHM.getDefaultValue());
+
+    private static final String SERVICE_ID = 
PGPPublicKeyService.class.getName();
+
+    private static PGPSecretKey rsaSecretKey;
+
+    private static PGPPrivateKey rsaPrivateKey;
+
+    private static PGPPublicKey elGamalPublicKey;
+
+    private static PGPPrivateKey elGamalPrivateKey;
+
+    private TestRunner runner;
+
+    @Mock
+    private PGPPublicKeyService publicKeyService;
+
+    @BeforeClass
+    public static void setKeys() throws Exception {
+        rsaSecretKey = 
PGPSecretKeyGenerator.generateRsaSecretKey(PASSPHRASE.toCharArray());
+        final PGPSecretKeyRing dsaElGamalSecretKeyRing = 
PGPSecretKeyGenerator.generateDsaElGamalSecretKeyRing(PASSPHRASE.toCharArray());
+
+        final PBESecretKeyDecryptor decryptor = new 
JcePBESecretKeyDecryptorBuilder().build(PASSPHRASE.toCharArray());
+        rsaPrivateKey = rsaSecretKey.extractPrivateKey(decryptor);
+        for (final PGPSecretKey secretKey : dsaElGamalSecretKeyRing) {
+            final PGPPublicKey publicKey = secretKey.getPublicKey();
+            if (PGPPublicKey.ELGAMAL_ENCRYPT == publicKey.getAlgorithm()) {
+                elGamalPrivateKey = secretKey.extractPrivateKey(decryptor);
+                elGamalPublicKey = publicKey;
+            }
+        }
+    }
+
+    @Before
+    public void setRunner() {
+        runner = TestRunners.newTestRunner(new EncryptContentPGP());
+    }
+
+    @Test
+    public void testMissingProperties() {
+        runner.assertNotValid();
+    }
+
+    @Test
+    public void testSuccessPasswordBasedEncryptionDefaultProperties() throws 
IOException, PGPException {
+        runner.setProperty(EncryptContentPGP.PASSPHRASE, PASSPHRASE);
+        runner.enqueue(DATA);
+        runner.run();
+
+        assertSuccess(DEFAULT_SYMMETRIC_KEY_ALGORITHM, 
PASSPHRASE.toCharArray());
+    }
+
+    @Test
+    public void testSuccessPasswordBasedEncryptionSymmetricKeyAlgorithms() 
throws IOException, PGPException {
+        for (final SymmetricKeyAlgorithm symmetricKeyAlgorithm : 
SymmetricKeyAlgorithm.values()) {
+            runner = TestRunners.newTestRunner(new EncryptContentPGP());
+            runner.setProperty(EncryptContentPGP.PASSPHRASE, PASSPHRASE);
+            runner.setProperty(EncryptContentPGP.SYMMETRIC_KEY_ALGORITHM, 
symmetricKeyAlgorithm.toString());
+            runner.enqueue(DATA);
+            runner.run();
+            assertSuccess(symmetricKeyAlgorithm, PASSPHRASE.toCharArray());
+        }
+    }
+
+    @Test
+    public void testSuccessPasswordBasedEncryptionCompressionAlgorithms() 
throws IOException, PGPException {
+        for (final CompressionAlgorithm compressionAlgorithm : 
CompressionAlgorithm.values()) {
+            runner = TestRunners.newTestRunner(new EncryptContentPGP());
+            runner.setProperty(EncryptContentPGP.PASSPHRASE, PASSPHRASE);
+            runner.setProperty(EncryptContentPGP.COMPRESSION_ALGORITHM, 
compressionAlgorithm.toString());
+            runner.enqueue(DATA);
+            runner.run();
+            assertSuccess(DEFAULT_SYMMETRIC_KEY_ALGORITHM, 
PASSPHRASE.toCharArray());
+        }
+    }
+
+    @Test
+    public void testSuccessPasswordBasedEncryptionFileEncodingAscii() throws 
IOException, PGPException {
+        runner.setProperty(EncryptContentPGP.PASSPHRASE, PASSPHRASE);
+        runner.setProperty(EncryptContentPGP.FILE_ENCODING, 
FileEncoding.ASCII.toString());
+        runner.enqueue(DATA);
+        runner.run();
+        assertSuccess(DEFAULT_SYMMETRIC_KEY_ALGORITHM, 
PASSPHRASE.toCharArray());
+    }
+
+    @Test
+    public void testSuccessPublicKeyEncryptionRsaPublicKey() throws 
IOException, InitializationException, PGPException {
+        final PGPPublicKey publicKey = rsaSecretKey.getPublicKey();
+        setPublicKeyService(publicKey);
+        final String publicKeyIdSearch = 
Long.toHexString(publicKey.getKeyID()).toUpperCase();
+        
when(publicKeyService.findPublicKey(eq(publicKeyIdSearch))).thenReturn(Optional.of(publicKey));
+
+        runner.enqueue(DATA);
+        runner.run();
+        assertSuccess(rsaPrivateKey);
+    }
+

Review comment:
       Thanks for the suggestion, will add a new unit test method for this use 
case.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to