Ensured that we guard against negative ports

fixes #518
This commit is contained in:
Marcin Grzejszczak
2017-02-15 13:53:47 +01:00
parent ba128a24de
commit a266d8bc6c
4 changed files with 24 additions and 2 deletions

View File

@@ -89,7 +89,7 @@ public class ServerPropertiesHostLocator implements HostLocator {
return this.port;
}
Integer port;
if (this.serverProperties != null && this.serverProperties.getPort() != null) {
if (this.serverProperties != null && this.serverProperties.getPort() != null && this.serverProperties.getPort() > 0) {
port = this.serverProperties.getPort();
}
else {

View File

@@ -89,6 +89,17 @@ public class ServerPropertiesHostLocatorTests {
assertThat(locator.locate(this.span).getServiceName()).isEqualTo("foo");
}
@Test
public void negativePortFromServerProperties() throws UnknownHostException {
ServerProperties properties = new ServerProperties();
properties.setPort(-1);
ServerPropertiesHostLocator locator = new ServerPropertiesHostLocator(properties,
"unknown", new ZipkinProperties(),localAddress(ADR1234));
assertThat(locator.locate(this.span).getPort()).isEqualTo((short) 8080);
}
private InetUtils localAddress(byte[] address) throws UnknownHostException {
InetUtils mocked = Mockito.spy(new InetUtils(new InetUtilsProperties()));
Mockito.when(mocked.findFirstNonLoopbackAddress())

View File

@@ -94,7 +94,7 @@ public class ServerPropertiesEndpointLocator implements EndpointLocator {
return this.port;
}
Integer port;
if (this.serverProperties!=null && this.serverProperties.getPort() != null) {
if (this.serverProperties!=null && this.serverProperties.getPort() != null && this.serverProperties.getPort() > 0) {
port = this.serverProperties.getPort();
}
else {

View File

@@ -83,6 +83,17 @@ public class ServerPropertiesEndpointLocatorTests {
assertThat(locator.local().serviceName).isEqualTo("foo");
}
@Test
public void negativePortFromServerProperties() throws UnknownHostException {
ServerProperties properties = new ServerProperties();
properties.setPort(-1);
ServerPropertiesEndpointLocator locator = new ServerPropertiesEndpointLocator(
properties, "unknown", new ZipkinProperties(),localAddress(ADDRESS1234));
assertThat(locator.local().port).isEqualTo((short) 8080);
}
private InetUtils localAddress(byte[] address) throws UnknownHostException {
InetUtils mocked = Mockito.spy(new InetUtils(new InetUtilsProperties()));
Mockito.when(mocked.findFirstNonLoopbackAddress())