Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Fix conversion of ip address to int (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Biegan authored and yurishkuro committed Jun 20, 2017
1 parent 74faa98 commit a4ce2b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 19 additions & 4 deletions jaeger-core/src/test/java/com/uber/jaeger/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}

0 comments on commit a4ce2b0

Please sign in to comment.