diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdRequest.java b/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdRequest.java index 6411cb75a9..dfcdad8768 100644 --- a/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdRequest.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdRequest.java @@ -63,7 +63,7 @@ public final class SocksCmdRequest extends SocksRequest { case UNKNOWN: break; } - if (port < 0 && port >= 65536) { + if (port <= 0 || port >= 65536) { throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536"); } this.cmdType = cmdType; diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponse.java b/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponse.java index 567ceb63ff..b38db62121 100644 --- a/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponse.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponse.java @@ -90,7 +90,7 @@ public final class SocksCmdResponse extends SocksResponse { } host = IDN.toASCII(host); } - if (port < 0 && port >= 65535) { + if (port <= 0 && port >= 65536) { throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536"); } this.cmdStatus = cmdStatus; diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksAuthResponseTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksAuthResponseTest.java index 0c15941327..cde19e2c34 100644 --- a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksAuthResponseTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksAuthResponseTest.java @@ -27,4 +27,5 @@ public class SocksAuthResponseTest { assertTrue(e instanceof NullPointerException); } } + } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java index 350461bd26..0672453cd7 100644 --- a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestDecoderTest.java @@ -51,7 +51,7 @@ public class SocksCmdRequestDecoderTest { @Test public void testCmdRequestDecoderIPv4() { String[] hosts = {"127.0.0.1", }; - int[] ports = {0, 32769, 65535 }; + int[] ports = {1, 32769, 65535 }; for (SocksCmdType cmdType : SocksCmdType.values()) { for (String host : hosts) { for (int port : ports) { @@ -64,7 +64,7 @@ public class SocksCmdRequestDecoderTest { @Test public void testCmdRequestDecoderIPv6() { String[] hosts = {SocksCommonUtils.ipv6toStr(IPAddressUtil.textToNumericFormatV6("::1"))}; - int[] ports = {0, 32769, 65535}; + int[] ports = {1, 32769, 65535}; for (SocksCmdType cmdType : SocksCmdType.values()) { for (String host : hosts) { for (int port : ports) { @@ -88,7 +88,7 @@ public class SocksCmdRequestDecoderTest { "例え.テスト", "실례.테스트", "உதாரணம்.பரிட்சை"}; - int[] ports = {0, 32769, 65535}; + int[] ports = {1, 32769, 65535}; for (SocksCmdType cmdType : SocksCmdType.values()) { for (String host : hosts) { for (int port : ports) { diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestTest.java index 5eb163c333..1fc0d9c199 100644 --- a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdRequestTest.java @@ -23,19 +23,19 @@ public class SocksCmdRequestTest { @Test public void testConstructorParamsAreNotNull() { try { - new SocksCmdRequest(null, SocksAddressType.UNKNOWN, "", 0); + new SocksCmdRequest(null, SocksAddressType.UNKNOWN, "", 1); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } try { - new SocksCmdRequest(SocksCmdType.UNKNOWN, null, "", 0); + new SocksCmdRequest(SocksCmdType.UNKNOWN, null, "", 1); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } try { - new SocksCmdRequest(SocksCmdType.UNKNOWN, SocksAddressType.UNKNOWN, null, 0); + new SocksCmdRequest(SocksCmdType.UNKNOWN, SocksAddressType.UNKNOWN, null, 1); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } @@ -44,7 +44,7 @@ public class SocksCmdRequestTest { @Test public void testIPv4CorrectAddress() { try { - new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.IPv4, "54.54.1111.253", 0); + new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.IPv4, "54.54.1111.253", 1); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } @@ -53,7 +53,7 @@ public class SocksCmdRequestTest { @Test public void testIPv6CorrectAddress() { try { - new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.IPv6, "xxx:xxx:xxx", 0); + new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.IPv6, "xxx:xxx:xxx", 1); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } @@ -66,7 +66,7 @@ public class SocksCmdRequestTest { "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + - "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή", 0); + "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή", 1); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } @@ -76,7 +76,7 @@ public class SocksCmdRequestTest { public void testValidPortRange() { try { new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.DOMAIN, - "παράδειγμα.δοκιμήπαράδει", -1); + "παράδειγμα.δοκιμήπαράδει", 0); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdResponseTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdResponseTest.java index f8d69f138f..420c5108f7 100644 --- a/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdResponseTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socks/SocksCmdResponseTest.java @@ -123,4 +123,18 @@ public class SocksCmdResponseTest { assertArrayEquals("Generated response differs from expected", expected, actualBytes); } + @Test + public void testValidPortRange() { + try { + new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "127.0.0", 0); + } catch (Exception e) { + assertTrue(e instanceof IllegalArgumentException); + } + + try { + new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "127.0.0", 65536); + } catch (Exception e) { + assertTrue(e instanceof IllegalArgumentException); + } + } }