exceptionfactory commented on code in PR #11784:
URL: https://github.com/apache/nifi/pull/11784#discussion_r4353641393


##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ftp/NifiFtpServer.java:
##########
@@ -176,10 +182,16 @@ private ConnectionConfig createConnectionConfig(boolean 
anonymousLoginEnabled) {
             return connectionConfigFactory.createConnectionConfig();
         }
 
-        private Listener createListener(String bindAddress, int port, 
SSLContextProvider sslContextProvider) throws FtpServerConfigurationException {
+        private Listener createListener(String bindAddress, int port, 
SSLContextProvider sslContextProvider, String passivePortRange) throws 
FtpServerConfigurationException {
             ListenerFactory listenerFactory = new ListenerFactory();
             listenerFactory.setServerAddress(bindAddress);
             listenerFactory.setPort(port);
+
+            DataConnectionConfigurationFactory 
dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
+            if (passivePortRange != null) {
+                
dataConnectionConfigurationFactory.setPassivePorts(passivePortRange);
+            }

Review Comment:
   The `null` check can be removed now that the property is required



##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestListenFTP.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.standard;
+
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPReply;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class TestListenFTP {
+
+    private static final String LOCALHOST = "127.0.0.1";
+
+    private static final Pattern PASSIVE_REPLY_PATTERN = 
Pattern.compile("\\((\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+)\\)");
+
+    private TestRunner runner;
+
+    @BeforeEach
+    void setRunner() {
+        runner = TestRunners.newTestRunner(ListenFTP.class);
+    }
+
+    @AfterEach
+    void shutdownRunner() {
+        runner.shutdown();
+    }
+
+    @Test
+    void testPassivePortRangeDefaultValid() {
+        runner.assertValid();
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"50000-50099", "50000-50000", "1-65535", 
"${PASSIVE_PORT_RANGE}"})
+    void testPassivePortRangeValid(final String passivePortRange) {
+        runner.setProperty(ListenFTP.PASSIVE_PORT_RANGE, passivePortRange);
+
+        runner.assertValid();
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"50000", "50099-50000", "0-100", "50000-65536", 
"50000-50099,50200", "start-end", " "})
+    void testPassivePortRangeInvalid(final String passivePortRange) {
+        runner.setProperty(ListenFTP.PASSIVE_PORT_RANGE, passivePortRange);
+
+        runner.assertNotValid();
+    }
+
+    @Test
+    void testPassiveModeUsesConfiguredPortRange() throws IOException {
+        final int port = getAvailablePort();
+        final int passivePort = getAvailablePort();
+        runner.setProperty(ListenFTP.ADDRESS, LOCALHOST);
+        runner.setProperty(ListenFTP.PORT, Integer.toString(port));
+        runner.setProperty(ListenFTP.PASSIVE_PORT_RANGE, passivePort + "-" + 
passivePort);
+
+        runner.run(1, false);
+
+        final FTPClient client = new FTPClient();
+        try {
+            client.connect(LOCALHOST, port);
+            assertTrue(client.login("anonymous", ""));
+
+            assertEquals(FTPReply.ENTERING_PASSIVE_MODE, client.pasv());
+            assertEquals(passivePort, getPassivePort(client.getReplyString()));
+        } finally {
+            client.disconnect();
+            runner.stop();
+        }
+    }
+
+    private int getPassivePort(final String passiveReply) {
+        final Matcher matcher = PASSIVE_REPLY_PATTERN.matcher(passiveReply);
+        assertTrue(matcher.find(), "Unexpected passive mode reply: " + 
passiveReply);
+        return Integer.parseInt(matcher.group(5)) * 256 + 
Integer.parseInt(matcher.group(6));
+    }
+
+    private int getAvailablePort() throws IOException {

Review Comment:
   This approach can be unstable based on timing. The value of `0` can be 
configured at the lower level, but may require adding a method to the Processor 
to retrieve the value determined.



##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestListenFTP.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.standard;
+
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPReply;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class TestListenFTP {
+
+    private static final String LOCALHOST = "127.0.0.1";
+
+    private static final Pattern PASSIVE_REPLY_PATTERN = 
Pattern.compile("\\((\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+)\\)");
+
+    private TestRunner runner;
+
+    @BeforeEach
+    void setRunner() {
+        runner = TestRunners.newTestRunner(ListenFTP.class);
+    }
+
+    @AfterEach
+    void shutdownRunner() {
+        runner.shutdown();
+    }
+
+    @Test
+    void testPassivePortRangeDefaultValid() {
+        runner.assertValid();
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"50000-50099", "50000-50000", "1-65535", 
"${PASSIVE_PORT_RANGE}"})
+    void testPassivePortRangeValid(final String passivePortRange) {
+        runner.setProperty(ListenFTP.PASSIVE_PORT_RANGE, passivePortRange);
+
+        runner.assertValid();
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"50000", "50099-50000", "0-100", "50000-65536", 
"50000-50099,50200", "start-end", " "})
+    void testPassivePortRangeInvalid(final String passivePortRange) {
+        runner.setProperty(ListenFTP.PASSIVE_PORT_RANGE, passivePortRange);
+
+        runner.assertNotValid();
+    }
+
+    @Test
+    void testPassiveModeUsesConfiguredPortRange() throws IOException {
+        final int port = getAvailablePort();
+        final int passivePort = getAvailablePort();
+        runner.setProperty(ListenFTP.ADDRESS, LOCALHOST);
+        runner.setProperty(ListenFTP.PORT, Integer.toString(port));
+        runner.setProperty(ListenFTP.PASSIVE_PORT_RANGE, passivePort + "-" + 
passivePort);
+
+        runner.run(1, false);
+
+        final FTPClient client = new FTPClient();
+        try {
+            client.connect(LOCALHOST, port);
+            assertTrue(client.login("anonymous", ""));
+
+            assertEquals(FTPReply.ENTERING_PASSIVE_MODE, client.pasv());
+            assertEquals(passivePort, getPassivePort(client.getReplyString()));
+        } finally {
+            client.disconnect();
+            runner.stop();
+        }
+    }
+
+    private int getPassivePort(final String passiveReply) {
+        final Matcher matcher = PASSIVE_REPLY_PATTERN.matcher(passiveReply);
+        assertTrue(matcher.find(), "Unexpected passive mode reply: " + 
passiveReply);
+        return Integer.parseInt(matcher.group(5)) * 256 + 
Integer.parseInt(matcher.group(6));

Review Comment:
   I would be helpful to declare the intermediate variables for the parsed 
integers



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to