VedaKadam commented on a change in pull request #4670: URL: https://github.com/apache/nifi/pull/4670#discussion_r527002762
########## File path: nifi-toolkit/nifi-toolkit-tls/src/test/groovy/org/apache/nifi/toolkit/tls/diagnosis/TlsToolkitGetDiagnosisStandaloneTest.groovy ########## @@ -0,0 +1,660 @@ +/* + * 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.toolkit.tls.diagnosis + +import org.apache.commons.lang3.SystemUtils +import org.apache.nifi.security.util.CertificateUtils +import org.apache.nifi.security.util.KeyStoreUtils +import org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException +import org.apache.nifi.toolkit.tls.util.TlsHelper +import org.apache.nifi.util.NiFiProperties +import org.bouncycastle.asn1.x500.X500Name +import org.bouncycastle.asn1.x509.ExtendedKeyUsage +import org.bouncycastle.asn1.x509.Extension +import org.bouncycastle.asn1.x509.Extensions +import org.bouncycastle.asn1.x509.KeyPurposeId +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo +import org.bouncycastle.cert.X509v3CertificateBuilder +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter +import org.bouncycastle.jce.provider.BouncyCastleProvider +import org.bouncycastle.operator.ContentSigner +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder +import org.junit.Assume +import org.junit.BeforeClass +import org.junit.Ignore +import org.junit.Rule +import org.junit.Test +import org.junit.contrib.java.lang.system.ExpectedSystemExit +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.slf4j.Logger +import org.slf4j.LoggerFactory + +import javax.security.auth.x500.X500Principal +import java.security.KeyPair +import java.security.KeyStore +import java.security.Security +import java.security.cert.X509Certificate +import java.util.concurrent.TimeUnit + + +@RunWith(JUnit4.class) +class TlsToolkitGetDiagnosisStandaloneTest extends GroovyTestCase { + private static final Logger logger = LoggerFactory.getLogger(TlsToolkitGetDiagnosisCommandLineTest.class) + public static final String DEFAULT_SIGNING_ALGORITHM = "SHA256WITHRSA" + + private static final KeyPair keyPair = TlsHelper.generateKeyPair("RSA", 2048) + + @Rule + public final ExpectedSystemExit exit = ExpectedSystemExit.none() + + @BeforeClass + static void setUpOnce() throws Exception { + Assume.assumeTrue("Test only runs on *nix", !SystemUtils.IS_OS_WINDOWS) + Security.addProvider(new BouncyCastleProvider()) + + logger.metaClass.methodMissing = { String name, args -> + logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") + } + //setupTmpDir() ??? + } + + static X509Certificate signAndBuildCert(String dn, String signingAlgorithm, KeyPair keyPair) { + ContentSigner sigGen = new JcaContentSignerBuilder(signingAlgorithm).setProvider(BouncyCastleProvider.PROVIDER_NAME).build(keyPair.getPrivate()) + X509v3CertificateBuilder certBuilder = certBuilder(new Date(), dn, keyPair, 365 * 24) + X509Certificate cert = new JcaX509CertificateConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME).getCertificate(certBuilder.build(sigGen)) + return cert + } + + static X509v3CertificateBuilder certBuilder(Date startDate, String dn, KeyPair keyPair, int hours) { + Date endDate = new Date(startDate.getTime() + TimeUnit.HOURS.toMillis(hours)); + + SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()) + X509v3CertificateBuilder certBuilder = new X509v3CertificateBuilder( + CertificateUtils.reverseX500Name(new X500Name(dn)), + CertificateUtils.getUniqueSerialNumber(), + startDate, endDate, + CertificateUtils.reverseX500Name(new X500Name(dn)), + subPubKeyInfo) + return certBuilder + } + + void setUp() { + super.setUp() + } + + void tearDown() { + } + + @Ignore("No assertions to make here") Review comment: For future addition of command-line arguments, it will be helpful to test out printing the help message to check for any dimension changes of helpFormatter. ---------------------------------------------------------------- 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]
