exceptionfactory commented on a change in pull request #4842: URL: https://github.com/apache/nifi/pull/4842#discussion_r607374814
########## File path: nifi-nar-bundles/nifi-pgp-bundle/nifi-pgp-processors/src/test/java/org/apache/nifi/processors/pgp/DecryptContentPGPTest.java ########## @@ -0,0 +1,391 @@ +/* + * 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.PGPPrivateKeyService; +import org.apache.nifi.pgp.util.PGPSecretKeyGenerator; +import org.apache.nifi.processors.pgp.exception.PGPDecryptionException; +import org.apache.nifi.processors.pgp.exception.PGPProcessException; +import org.apache.nifi.reporting.InitializationException; +import org.apache.nifi.util.LogMessage; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; +import org.bouncycastle.openpgp.PGPCompressedData; +import org.bouncycastle.openpgp.PGPCompressedDataGenerator; +import org.bouncycastle.openpgp.PGPEncryptedDataGenerator; +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPLiteralDataGenerator; +import org.bouncycastle.openpgp.PGPPrivateKey; +import org.bouncycastle.openpgp.PGPPublicKey; +import org.bouncycastle.openpgp.PGPSecretKey; +import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.PGPSignature; +import org.bouncycastle.openpgp.PGPSignatureGenerator; +import org.bouncycastle.openpgp.PGPUtil; +import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; +import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder; +import org.bouncycastle.openpgp.operator.bc.BcPGPDataEncryptorBuilder; +import org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator; +import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; +import org.bouncycastle.openpgp.operator.jcajce.JcePBEKeyEncryptionMethodGenerator; +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.OutputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.isA; +import static org.junit.Assert.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class DecryptContentPGPTest { + private static final int ENCRYPTION_ALGORITHM = SymmetricKeyAlgorithmTags.AES_256; + + private static final boolean INTEGRITY_ENABLED = true; + + private static final boolean INTEGRITY_DISABLED = false; + + private static final String PASSPHRASE = UUID.randomUUID().toString(); + + private static final String FILE_NAME = String.class.getSimpleName(); + + private static final char FILE_TYPE = PGPLiteralDataGenerator.TEXT; + + private static final long MODIFIED_MILLISECONDS = 86400000; + + private static final Date MODIFIED = new Date(MODIFIED_MILLISECONDS); + + private static final String DATA = String.class.getName(); + + private static final Charset DATA_CHARSET = StandardCharsets.UTF_8; + + private static final int BUFFER_SIZE = 128; + + private static final boolean NESTED_SIGNATURE_DISABLED = false; + + private static final String SERVICE_ID = PGPPrivateKeyService.class.getSimpleName(); + + private static PGPSecretKey rsaSecretKey; + + private static PGPPrivateKey rsaPrivateKey; + + private static PGPPublicKey elGamalPublicKey; + + private static PGPPrivateKey elGamalPrivateKey; + + private TestRunner runner; + + @Mock + private PGPPrivateKeyService privateKeyService; + + @BeforeClass + public static void setKeys() throws Exception { + rsaSecretKey = PGPSecretKeyGenerator.generateRsaSecretKey(PASSPHRASE.toCharArray()); + + final PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder().build(PASSPHRASE.toCharArray()); + rsaPrivateKey = rsaSecretKey.extractPrivateKey(decryptor); + final PGPSecretKeyRing dsaElGamalSecretKeyRing = PGPSecretKeyGenerator.generateDsaElGamalSecretKeyRing(PASSPHRASE.toCharArray()); + 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 DecryptContentPGP()); + } + + @Test + public void testMissingProperties() { + runner.assertNotValid(); + } + + @Test + public void testFailureEncryptedDataNotFound() { + runner.setProperty(DecryptContentPGP.PASSPHRASE, PASSPHRASE); + runner.enqueue(new byte[]{}); + runner.run(); + + assertFailureExceptionLogged(PGPProcessException.class); + } + Review comment: Great question! RFC 4880 Section 5.3 describes this as a valid scenario, so I will include additional unit tests. -- 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]
