From a4ce2b046082dcfd462119a49314ed2ce008ffa9 Mon Sep 17 00:00:00 2001 From: Marcin Biegan Date: Tue, 20 Jun 2017 16:37:46 +0200 Subject: [PATCH] Fix conversion of ip address to int (#199) --- .../java/com/uber/jaeger/utils/Utils.java | 2 +- .../test/java/com/uber/jaeger/UtilsTest.java | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java b/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java index 24e5a5fc1..1643cb72c 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java @@ -50,7 +50,7 @@ public static int ipToInt(String ip) throws EmptyIpException, NotFourOctetsExcep int intIp = 0; for (byte octet : octets.getAddress()) { - intIp = (intIp << 8) | (octet); + intIp = (intIp << 8) | (octet & 0xFF); } return intIp; } diff --git a/jaeger-core/src/test/java/com/uber/jaeger/UtilsTest.java b/jaeger-core/src/test/java/com/uber/jaeger/UtilsTest.java index 8bfcd7cf6..39bf56816 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/UtilsTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/UtilsTest.java @@ -22,7 +22,9 @@ package com.uber.jaeger; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import com.uber.jaeger.exceptions.EmptyIpException; import com.uber.jaeger.exceptions.NotFourOctetsException; @@ -49,9 +51,22 @@ public void testIpToInt32EmptyIpException() { } @Test - public void testIpToInt32() { - int expectedIp = (127 << 24) | 1; - int actualIp = Utils.ipToInt("127.0.0.1"); - assertEquals(expectedIp, actualIp); + public void testIpToInt32_localhost() { + assertThat(Utils.ipToInt("127.0.0.1"), equalTo((127 << 24) | 1)); + } + + @Test + public void testIpToInt32_above127() { + assertThat(Utils.ipToInt("10.137.1.2"), equalTo((10 << 24) | (137 << 16) | (1 << 8) | 2)); + } + + @Test + public void testIpToInt32_zeros() { + assertThat(Utils.ipToInt("0.0.0.0"), equalTo(0)); + } + + @Test + public void testIpToInt32_broadcast() { + assertThat(Utils.ipToInt("255.255.255.255"), equalTo(-1)); } }