[#2428] Proper fix of incorrect port range check

Motivation:

In the Internet Protocol, the valid port number range is from 1 to 65535
(inclusive on the both side.)  However, SocksCmdRequest and SocksCmdResponse
refuses to construct itself when the port number 65535 is specified. Beside
this it excepts 0 as port number which should not allowed.

    Modification:

* Not raise an exception when the specified port number is 65535.
* Raise an exception when the specified port number is 0

Result:

Fixes #2428
This commit is contained in:
Norman Maurer 2014-04-30 06:56:22 +02:00
parent 01ef614e77
commit 80f1dfaec0
6 changed files with 27 additions and 12 deletions

View File

@ -63,7 +63,7 @@ public final class SocksCmdRequest extends SocksRequest {
case UNKNOWN: case UNKNOWN:
break; break;
} }
if (port < 0 && port >= 65536) { if (port <= 0 || port >= 65536) {
throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536"); throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536");
} }
this.cmdType = cmdType; this.cmdType = cmdType;

View File

@ -90,7 +90,7 @@ public final class SocksCmdResponse extends SocksResponse {
} }
host = IDN.toASCII(host); 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"); throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536");
} }
this.cmdStatus = cmdStatus; this.cmdStatus = cmdStatus;

View File

@ -27,4 +27,5 @@ public class SocksAuthResponseTest {
assertTrue(e instanceof NullPointerException); assertTrue(e instanceof NullPointerException);
} }
} }
} }

View File

@ -51,7 +51,7 @@ public class SocksCmdRequestDecoderTest {
@Test @Test
public void testCmdRequestDecoderIPv4() { public void testCmdRequestDecoderIPv4() {
String[] hosts = {"127.0.0.1", }; String[] hosts = {"127.0.0.1", };
int[] ports = {0, 32769, 65535 }; int[] ports = {1, 32769, 65535 };
for (SocksCmdType cmdType : SocksCmdType.values()) { for (SocksCmdType cmdType : SocksCmdType.values()) {
for (String host : hosts) { for (String host : hosts) {
for (int port : ports) { for (int port : ports) {
@ -64,7 +64,7 @@ public class SocksCmdRequestDecoderTest {
@Test @Test
public void testCmdRequestDecoderIPv6() { public void testCmdRequestDecoderIPv6() {
String[] hosts = {SocksCommonUtils.ipv6toStr(IPAddressUtil.textToNumericFormatV6("::1"))}; String[] hosts = {SocksCommonUtils.ipv6toStr(IPAddressUtil.textToNumericFormatV6("::1"))};
int[] ports = {0, 32769, 65535}; int[] ports = {1, 32769, 65535};
for (SocksCmdType cmdType : SocksCmdType.values()) { for (SocksCmdType cmdType : SocksCmdType.values()) {
for (String host : hosts) { for (String host : hosts) {
for (int port : ports) { 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 (SocksCmdType cmdType : SocksCmdType.values()) {
for (String host : hosts) { for (String host : hosts) {
for (int port : ports) { for (int port : ports) {

View File

@ -23,19 +23,19 @@ public class SocksCmdRequestTest {
@Test @Test
public void testConstructorParamsAreNotNull() { public void testConstructorParamsAreNotNull() {
try { try {
new SocksCmdRequest(null, SocksAddressType.UNKNOWN, "", 0); new SocksCmdRequest(null, SocksAddressType.UNKNOWN, "", 1);
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof NullPointerException); assertTrue(e instanceof NullPointerException);
} }
try { try {
new SocksCmdRequest(SocksCmdType.UNKNOWN, null, "", 0); new SocksCmdRequest(SocksCmdType.UNKNOWN, null, "", 1);
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof NullPointerException); assertTrue(e instanceof NullPointerException);
} }
try { try {
new SocksCmdRequest(SocksCmdType.UNKNOWN, SocksAddressType.UNKNOWN, null, 0); new SocksCmdRequest(SocksCmdType.UNKNOWN, SocksAddressType.UNKNOWN, null, 1);
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof NullPointerException); assertTrue(e instanceof NullPointerException);
} }
@ -44,7 +44,7 @@ public class SocksCmdRequestTest {
@Test @Test
public void testIPv4CorrectAddress() { public void testIPv4CorrectAddress() {
try { 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) { } catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException); assertTrue(e instanceof IllegalArgumentException);
} }
@ -53,7 +53,7 @@ public class SocksCmdRequestTest {
@Test @Test
public void testIPv6CorrectAddress() { public void testIPv6CorrectAddress() {
try { try {
new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.IPv6, "xxx:xxx:xxx", 0); new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.IPv6, "xxx:xxx:xxx", 1);
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException); assertTrue(e instanceof IllegalArgumentException);
} }
@ -66,7 +66,7 @@ public class SocksCmdRequestTest {
"παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" +
"παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" +
"παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" +
"παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή", 0); "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή", 1);
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException); assertTrue(e instanceof IllegalArgumentException);
} }
@ -76,7 +76,7 @@ public class SocksCmdRequestTest {
public void testValidPortRange() { public void testValidPortRange() {
try { try {
new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.DOMAIN, new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.DOMAIN,
"παράδειγμα.δοκιμήπαράδει", -1); "παράδειγμα.δοκιμήπαράδει", 0);
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException); assertTrue(e instanceof IllegalArgumentException);
} }

View File

@ -123,4 +123,18 @@ public class SocksCmdResponseTest {
assertArrayEquals("Generated response differs from expected", expected, actualBytes); 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);
}
}
} }