This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-net.git
commit e389ada11ce11fe27547e566eb2d105979ef2d97 Author: Gary Gregory <[email protected]> AuthorDate: Fri Jan 30 09:41:09 2026 -0500 Sort members --- .../java/org/apache/commons/net/SocketClient.java | 16 +++--- .../java/org/apache/commons/net/tftp/TFTP.java | 58 +++++++++++----------- .../apache/commons/net/tftp/TFTPRequestPacket.java | 32 ++++++------ .../apache/commons/net/ftp/AbstractFtpsTest.java | 8 +-- .../org/apache/commons/net/ftp/FTPSClientTest.java | 8 +-- .../commons/net/ftp/ListingFunctionalTest.java | 18 +++---- .../net/ftp/parser/UnixFTPEntryParserTest.java | 40 +++++++-------- .../commons/net/tftp/TFTPRequestPacketTest.java | 46 ++++++++--------- .../java/org/apache/commons/net/tftp/TFTPTest.java | 18 +++---- 9 files changed, 122 insertions(+), 122 deletions(-) diff --git a/src/main/java/org/apache/commons/net/SocketClient.java b/src/main/java/org/apache/commons/net/SocketClient.java index 50b7612e..8cba0038 100644 --- a/src/main/java/org/apache/commons/net/SocketClient.java +++ b/src/main/java/org/apache/commons/net/SocketClient.java @@ -61,25 +61,25 @@ public abstract class SocketClient { private static final int DEFAULT_CONNECT_TIMEOUT = 60000; /** - * Gets the IP address string of the given Socket in textual presentation. + * Gets the IP address string of the given InetAddress in textual presentation. * - * @param socket the socket to query. + * @param inetAddress Internet Protocol (IP) address to query. * @return the raw IP address in a string format. * @since 3.12.0 */ - protected static String getHostAddress(final Socket socket) { - return getHostAddress(socket.getInetAddress()); + protected static String getHostAddress(final InetAddress inetAddress) { + return inetAddress != null ? inetAddress.getHostAddress() : null; } /** - * Gets the IP address string of the given InetAddress in textual presentation. + * Gets the IP address string of the given Socket in textual presentation. * - * @param inetAddress Internet Protocol (IP) address to query. + * @param socket the socket to query. * @return the raw IP address in a string format. * @since 3.12.0 */ - protected static String getHostAddress(final InetAddress inetAddress) { - return inetAddress != null ? inetAddress.getHostAddress() : null; + protected static String getHostAddress(final Socket socket) { + return getHostAddress(socket.getInetAddress()); } /** diff --git a/src/main/java/org/apache/commons/net/tftp/TFTP.java b/src/main/java/org/apache/commons/net/tftp/TFTP.java index 0eceead8..8b3dca58 100644 --- a/src/main/java/org/apache/commons/net/tftp/TFTP.java +++ b/src/main/java/org/apache/commons/net/tftp/TFTP.java @@ -223,6 +223,16 @@ public class TFTP extends DatagramSocketClient { buffersInitialized = false; } + /** + * Gets the buffer size of the buffered used by {@link #bufferedSend(TFTPPacket)} and {@link #bufferedReceive}. + * + * @return current buffer size + * @since 3.12.0 + */ + public int getPacketSize() { + return packetSize; + } + /** * Receives a TFTPPacket. * @@ -246,30 +256,6 @@ public class TFTP extends DatagramSocketClient { return newTFTPPacket; } - /** - * Sends a TFTP packet to its destination. - * - * @param packet The TFTP packet to send. - * @throws IOException If some I/O error occurs. - */ - public final void send(final TFTPPacket packet) throws IOException { - trace(">", packet); - checkOpen().send(packet.newDatagram()); - } - - /** - * Trace facility; this implementation does nothing. - * <p> - * Override it to trace the data, for example:<br> - * {@code System.out.println(direction + " " + packet.toString());} - * - * @param direction {@code >} or {@code <} - * @param packet the packet to be sent or that has been received respectively - * @since 3.6 - */ - protected void trace(final String direction, final TFTPPacket packet) { - } - /** * Sets the size of the buffers used to receive and send packets. * This method can be used to increase the size of the buffer to support `blksize`. @@ -290,12 +276,26 @@ public class TFTP extends DatagramSocketClient { } /** - * Gets the buffer size of the buffered used by {@link #bufferedSend(TFTPPacket)} and {@link #bufferedReceive}. + * Sends a TFTP packet to its destination. * - * @return current buffer size - * @since 3.12.0 + * @param packet The TFTP packet to send. + * @throws IOException If some I/O error occurs. */ - public int getPacketSize() { - return packetSize; + public final void send(final TFTPPacket packet) throws IOException { + trace(">", packet); + checkOpen().send(packet.newDatagram()); + } + + /** + * Trace facility; this implementation does nothing. + * <p> + * Override it to trace the data, for example:<br> + * {@code System.out.println(direction + " " + packet.toString());} + * + * @param direction {@code >} or {@code <} + * @param packet the packet to be sent or that has been received respectively + * @since 3.6 + */ + protected void trace(final String direction, final TFTPPacket packet) { } } diff --git a/src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java b/src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java index eeeacc01..9a7383b4 100644 --- a/src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java +++ b/src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java @@ -192,6 +192,22 @@ public abstract class TFTPRequestPacket extends TFTPPacket { return options; } + private void handleOptions(final byte[] data, final int fileLength, final int modeLength) { + int index = fileLength + modeLength + 2; + for (final Map.Entry<String, String> entry : options.entrySet()) { + data[index] = 0; + final String key = entry.getKey(); + final String value = entry.getValue(); + + System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, data, ++index, key.length()); + index += key.length(); + data[index++] = 0; + + System.arraycopy(value.getBytes(StandardCharsets.US_ASCII), 0, data, index, value.length()); + index += value.length(); + } + } + /** * Creates a UDP datagram containing all the TFTP request packet data in the proper format. This is a method exposed to the programmer in case he wants to * implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have @@ -257,20 +273,4 @@ public abstract class TFTPRequestPacket extends TFTPPacket { return datagram; } - - private void handleOptions(final byte[] data, final int fileLength, final int modeLength) { - int index = fileLength + modeLength + 2; - for (final Map.Entry<String, String> entry : options.entrySet()) { - data[index] = 0; - final String key = entry.getKey(); - final String value = entry.getValue(); - - System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, data, ++index, key.length()); - index += key.length(); - data[index++] = 0; - - System.arraycopy(value.getBytes(StandardCharsets.US_ASCII), 0, data, index, value.length()); - index += value.length(); - } - } } diff --git a/src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java b/src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java index d159f88f..2816528e 100644 --- a/src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java +++ b/src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java @@ -74,10 +74,6 @@ public abstract class AbstractFtpsTest { return System.getProperty("test.basedir", defaultHome); } - public void setEndpointCheckingEnabled(final boolean value) { - this.endpointCheckingEnabled = value; - } - /** * Creates and starts an embedded Apache MINA FTP Server. * @@ -207,4 +203,8 @@ public abstract class AbstractFtpsTest { client.disconnect(); } } + + public void setEndpointCheckingEnabled(final boolean value) { + this.endpointCheckingEnabled = value; + } } diff --git a/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java b/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java index d7c8cb42..5569d166 100644 --- a/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java +++ b/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java @@ -51,15 +51,15 @@ public class FTPSClientTest extends AbstractFtpsTest { private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks"; + private static Stream<Boolean> endpointCheckingEnabledSource() { + return Stream.of(Boolean.FALSE, Boolean.TRUE); + } + @BeforeAll public static void setupServer() throws Exception { setupServer(IMPLICIT, USER_PROPS_RES, SERVER_JKS_RES, "target/test-classes/org/apache/commons/net/test-data"); } - private static Stream<Boolean> endpointCheckingEnabledSource() { - return Stream.of(Boolean.FALSE, Boolean.TRUE); - } - @ParameterizedTest(name = "endpointCheckingEnabled={0}") @MethodSource("endpointCheckingEnabledSource") @Timeout(TEST_TIMEOUT) diff --git a/src/test/java/org/apache/commons/net/ftp/ListingFunctionalTest.java b/src/test/java/org/apache/commons/net/ftp/ListingFunctionalTest.java index 328d93e6..82c2c6cc 100644 --- a/src/test/java/org/apache/commons/net/ftp/ListingFunctionalTest.java +++ b/src/test/java/org/apache/commons/net/ftp/ListingFunctionalTest.java @@ -40,15 +40,6 @@ import org.junit.jupiter.params.provider.MethodSource; * A functional test suite for checking that site listings work. */ public class ListingFunctionalTest { - // Offsets within testData below - static final int HOSTNAME = 0; - static final int VALID_PARSERKEY = 1; - static final int INVALID_PARSERKEY = 2; - static final int INVALID_PATH = 3; - static final int VALID_FILENAME = 4; - static final int VALID_PATH = 5; - static final int PATH_PWD = 6; // response to PWD - public static final class TestCase { private final String hostName; private final String invalidParserKey; @@ -73,6 +64,15 @@ public class ListingFunctionalTest { return validParserKey + " @ " + hostName; } } + // Offsets within testData below + static final int HOSTNAME = 0; + static final int VALID_PARSERKEY = 1; + static final int INVALID_PARSERKEY = 2; + static final int INVALID_PATH = 3; + static final int VALID_FILENAME = 4; + static final int VALID_PATH = 5; + + static final int PATH_PWD = 6; // response to PWD private static Stream<TestCase> testCases() { final String[][] testData = { { "ftp.ibiblio.org", "unix", "vms", "HA!", "javaio.jar", "pub/languages/java/javafaq", "/pub/languages/java/javafaq", }, diff --git a/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java b/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java index 366d2ddc..022f403b 100644 --- a/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java +++ b/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java @@ -339,10 +339,9 @@ public class UnixFTPEntryParserTest extends AbstractFTPParseTest { assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime())); } - // https://mail-archives.apache.org/mod_mbox/commons-dev/200408.mbox/%[email protected]%3e @Test - public void testParseFieldsOnFileJapaneseTime() { - final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x 2 user group 4096 3\u6708 2\u65e5 15:13 zxbox"); + public void testParseFieldsOnFileChineseTime() { + final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x 2 user group 4096 3\u6708 2 15:13 zxbox"); assertNotNull(f, "Could not parse entry."); assertTrue(f.isFile(), "Should have been a file."); checkPermissions(f); @@ -369,60 +368,61 @@ public class UnixFTPEntryParserTest extends AbstractFTPParseTest { } @Test - public void testParseFieldsOnFileChineseTime() { - final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x 2 user group 4096 3\u6708 2 15:13 zxbox"); + public void testParseFieldsOnFileChineseYear() { + final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x 2 user group 4096 3\u6708 2 2003 \u8a66\u9a13\u30d5\u30a1\u30a4\u30eb.csv"); assertNotNull(f, "Could not parse entry."); assertTrue(f.isFile(), "Should have been a file."); checkPermissions(f); assertEquals(2, f.getHardLinkCount()); assertEquals("user", f.getUser()); assertEquals("group", f.getGroup()); - assertEquals("zxbox", f.getName()); + assertEquals("\u8a66\u9a13\u30d5\u30a1\u30a4\u30eb.csv", f.getName()); assertEquals(4096, f.getSize()); assertNotNull(f.getTimestamp(), "Timestamp not null"); final Calendar cal = Calendar.getInstance(); + cal.set(Calendar.YEAR, 2003); cal.set(Calendar.MONTH, Calendar.MARCH); - cal.set(Calendar.DATE, 1); + cal.set(Calendar.DATE, 2); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); - if (f.getTimestamp().getTime().before(cal.getTime())) { - cal.add(Calendar.YEAR, -1); - } - cal.set(Calendar.DATE, 2); - cal.set(Calendar.HOUR_OF_DAY, 15); - cal.set(Calendar.MINUTE, 13); assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime())); } // https://mail-archives.apache.org/mod_mbox/commons-dev/200408.mbox/%[email protected]%3e @Test - public void testParseFieldsOnFileJapaneseYear() { - final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x 2 user group 4096 3\u6708 2\u65e5 2003\u5e74 \u8a66\u9a13\u30d5\u30a1\u30a4\u30eb.csv"); + public void testParseFieldsOnFileJapaneseTime() { + final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x 2 user group 4096 3\u6708 2\u65e5 15:13 zxbox"); assertNotNull(f, "Could not parse entry."); assertTrue(f.isFile(), "Should have been a file."); checkPermissions(f); assertEquals(2, f.getHardLinkCount()); assertEquals("user", f.getUser()); assertEquals("group", f.getGroup()); - assertEquals("\u8a66\u9a13\u30d5\u30a1\u30a4\u30eb.csv", f.getName()); + assertEquals("zxbox", f.getName()); assertEquals(4096, f.getSize()); assertNotNull(f.getTimestamp(), "Timestamp not null"); final Calendar cal = Calendar.getInstance(); - cal.set(Calendar.YEAR, 2003); cal.set(Calendar.MONTH, Calendar.MARCH); - cal.set(Calendar.DATE, 2); + cal.set(Calendar.DATE, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); + if (f.getTimestamp().getTime().before(cal.getTime())) { + cal.add(Calendar.YEAR, -1); + } + cal.set(Calendar.DATE, 2); + cal.set(Calendar.HOUR_OF_DAY, 15); + cal.set(Calendar.MINUTE, 13); assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime())); } + // https://mail-archives.apache.org/mod_mbox/commons-dev/200408.mbox/%[email protected]%3e @Test - public void testParseFieldsOnFileChineseYear() { - final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x 2 user group 4096 3\u6708 2 2003 \u8a66\u9a13\u30d5\u30a1\u30a4\u30eb.csv"); + public void testParseFieldsOnFileJapaneseYear() { + final FTPFile f = getParser().parseFTPEntry("-rwxr-xr-x 2 user group 4096 3\u6708 2\u65e5 2003\u5e74 \u8a66\u9a13\u30d5\u30a1\u30a4\u30eb.csv"); assertNotNull(f, "Could not parse entry."); assertTrue(f.isFile(), "Should have been a file."); checkPermissions(f); diff --git a/src/test/java/org/apache/commons/net/tftp/TFTPRequestPacketTest.java b/src/test/java/org/apache/commons/net/tftp/TFTPRequestPacketTest.java index 9308be5c..bbb94b48 100644 --- a/src/test/java/org/apache/commons/net/tftp/TFTPRequestPacketTest.java +++ b/src/test/java/org/apache/commons/net/tftp/TFTPRequestPacketTest.java @@ -36,6 +36,29 @@ import org.junit.jupiter.api.Test; */ class TFTPRequestPacketTest { + private static DatagramPacket getDatagramPacket() throws UnknownHostException { + final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + byteStream.write(0); + byteStream.write(1); + + try { + byteStream.write("fileName".getBytes(StandardCharsets.US_ASCII)); + byteStream.write(0); + byteStream.write("octet".getBytes(StandardCharsets.US_ASCII)); + byteStream.write(0); + + byteStream.write("blksize".getBytes(StandardCharsets.US_ASCII)); + byteStream.write(0); + byteStream.write("1024".getBytes(StandardCharsets.US_ASCII)); + byteStream.write(0); + } catch (final IOException e) { + throw new RuntimeException("Error creating TFTP request packet", e); + } + + final byte[] data = byteStream.toByteArray(); + return new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 0); + } + @Test public void testGetOptions() throws UnknownHostException, TFTPPacketException { final DatagramPacket datagramPacket = getDatagramPacket(); @@ -67,27 +90,4 @@ class TFTPRequestPacketTest { assertEquals(datagramPacket.getPort(), newDatagram2.getPort()); assertArrayEquals(datagramPacket.getData(), data); } - - private static DatagramPacket getDatagramPacket() throws UnknownHostException { - final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); - byteStream.write(0); - byteStream.write(1); - - try { - byteStream.write("fileName".getBytes(StandardCharsets.US_ASCII)); - byteStream.write(0); - byteStream.write("octet".getBytes(StandardCharsets.US_ASCII)); - byteStream.write(0); - - byteStream.write("blksize".getBytes(StandardCharsets.US_ASCII)); - byteStream.write(0); - byteStream.write("1024".getBytes(StandardCharsets.US_ASCII)); - byteStream.write(0); - } catch (final IOException e) { - throw new RuntimeException("Error creating TFTP request packet", e); - } - - final byte[] data = byteStream.toByteArray(); - return new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 0); - } } diff --git a/src/test/java/org/apache/commons/net/tftp/TFTPTest.java b/src/test/java/org/apache/commons/net/tftp/TFTPTest.java index 2cb38455..26faa699 100644 --- a/src/test/java/org/apache/commons/net/tftp/TFTPTest.java +++ b/src/test/java/org/apache/commons/net/tftp/TFTPTest.java @@ -174,6 +174,15 @@ public class TFTPTest { } } + @Test + public void testResizeBuffer() { + try (TFTPClient tftp = new TFTPClient()) { + final int bufferSize = 1024; + tftp.resetBuffersToSize(bufferSize); + assertEquals(bufferSize + 4, tftp.getPacketSize(), "Packet size should be 1028"); + } + } + @Test public void testSend() throws IOException { try (TFTP tftp = new TFTP()) { @@ -198,15 +207,6 @@ public class TFTPTest { } } - @Test - public void testResizeBuffer() { - try (TFTPClient tftp = new TFTPClient()) { - final int bufferSize = 1024; - tftp.resetBuffersToSize(bufferSize); - assertEquals(bufferSize + 4, tftp.getPacketSize(), "Packet size should be 1028"); - } - } - private void testUpload(final int mode, final File file) throws Exception { // Create our TFTP instance to handle the file transfer. try (TFTPClient tftp = new TFTPClient()) {
