Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions commons-ip-math/src/main/java/com/github/jgonian/ipmath/Ipv4.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.github.jgonian.ipmath;

import java.math.BigInteger;
import java.net.Inet4Address;
import java.util.regex.Pattern;

public final class Ipv4 extends AbstractIp<Ipv4, Ipv4Range> {
Expand Down Expand Up @@ -74,6 +75,20 @@ public static Ipv4 of(String value) {
return parse(value);
}

public static Ipv4 of(byte[] octets) {
Validate.isTrue(octets.length == TOTAL_OCTETS, "exactly " + TOTAL_OCTETS + " octets are required");
long result = 0;
for (int i = 0; i < octets.length; i++) {
result = addOctet(result, Byte.toUnsignedInt(octets[i]));
}
return new Ipv4(result);
}

public static Ipv4 of(Inet4Address value) {
Validate.notNull(value, "value is required");
return of(value.getAddress());
}

public static Ipv4 parse(String ipv4Address) {
try {
String ipv4String = Validate.notNull(ipv4Address).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.github.jgonian.ipmath;

import java.math.BigInteger;
import java.net.Inet4Address;

public final class Ipv4Range extends AbstractIpRange<Ipv4, Ipv4Range> {

Expand Down Expand Up @@ -55,6 +56,14 @@ public static Ipv4RangeBuilder from(Long from) {
return new Ipv4RangeBuilder(Ipv4.of(from));
}

public static Ipv4RangeBuilder from(byte[] from) {
return new Ipv4RangeBuilder(Ipv4.of(from));
}

public static Ipv4RangeBuilder from(Inet4Address from) {
return new Ipv4RangeBuilder(Ipv4.of(from));
}

public static Ipv4RangeBuilder from(String from) {
return new Ipv4RangeBuilder(Ipv4.parse(from));
}
Expand Down Expand Up @@ -125,6 +134,14 @@ public Ipv4Range to(Long end) {
return to(Ipv4.of(end));
}

public Ipv4Range to(byte[] end) {
return to(Ipv4.of(end));
}

public Ipv4Range to(Inet4Address end) {
return to(Ipv4.of(end));
}

public Ipv4Range to(String end) {
return to(Ipv4.parse(end));
}
Expand Down
23 changes: 20 additions & 3 deletions commons-ip-math/src/main/java/com/github/jgonian/ipmath/Ipv6.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.github.jgonian.ipmath;

import java.math.BigInteger;
import java.net.Inet6Address;
import java.util.regex.Pattern;

import static java.math.BigInteger.ONE;
Expand All @@ -47,8 +48,10 @@ public final class Ipv6 extends AbstractIp<Ipv6, Ipv6Range> {
private static final String COLON = ":";
private static final String ZERO = "0";
private static final int BITS_PER_PART = 16;
private static final int TOTAL_OCTETS = 8;
private static final int TOTAL_PARTS = 8;
private static final int COLON_COUNT_IPV6 = 7;
private static final int BITS_PER_OCTET = 8;
private static final int TOTAL_OCTETS = 16;
private static final BigInteger MINUS_ONE = BigInteger.valueOf(-1);

private final BigInteger value;
Expand All @@ -71,6 +74,20 @@ public static Ipv6 of(String value) {
return parse(value);
}

public static Ipv6 of(byte[] octets) {
Validate.isTrue(octets.length == TOTAL_OCTETS, "exactly " + TOTAL_OCTETS + " octets are required");
BigInteger result = BigInteger.ZERO;
for (int i = 0; i < octets.length; i++) {
result = result.shiftLeft(BITS_PER_OCTET).add(BigInteger.valueOf(Byte.toUnsignedInt(octets[i])));
}
return new Ipv6(result);
}

public static Ipv6 of(Inet6Address value) {
Validate.notNull(value, "value is required");
return of(value.getAddress());
}

@Override
public int compareTo(Ipv6 other) {
return value.compareTo(other.value);
Expand Down Expand Up @@ -174,8 +191,8 @@ public static Ipv6 parse(final String ipv6Address) {
ipv6String = expandMissingColons(ipv6String, indexOfDoubleColons);
}

final String[] split = ipv6String.split(COLON, TOTAL_OCTETS);
Validate.isTrue(split.length == TOTAL_OCTETS);
final String[] split = ipv6String.split(COLON, TOTAL_PARTS);
Validate.isTrue(split.length == TOTAL_PARTS);
BigInteger ipv6value = BigInteger.ZERO;
for (String part : split) {
Validate.isTrue(part.length() <= MAX_PART_LENGTH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static java.math.BigInteger.*;
import java.math.BigInteger;
import java.net.Inet6Address;

public final class Ipv6Range extends AbstractIpRange<Ipv6, Ipv6Range> {

Expand Down Expand Up @@ -52,6 +53,14 @@ public static Ipv6RangeBuilder from(BigInteger from) {
return new Ipv6RangeBuilder(Ipv6.of(from));
}

public static Ipv6RangeBuilder from(byte[] from) {
return new Ipv6RangeBuilder(Ipv6.of(from));
}

public static Ipv6RangeBuilder from(Inet6Address from) {
return new Ipv6RangeBuilder(Ipv6.of(from));
}

public static Ipv6RangeBuilder from(String from) {
return new Ipv6RangeBuilder(Ipv6.parse(from));
}
Expand Down Expand Up @@ -119,6 +128,14 @@ public Ipv6Range to(BigInteger end) {
return to(Ipv6.of(end));
}

public Ipv6Range to(byte[] end) {
return to(Ipv6.of(end));
}

public Ipv6Range to(Inet6Address end) {
return to(Ipv6.of(end));
}

public Ipv6Range to(String end) {
return to(Ipv6.parse(end));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import org.junit.Test;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -226,6 +229,21 @@ public void testBuilderWithLongs() {
assertEquals(ip3, range.end());
}

@Test
public void testBuilderWithByteArrays() {
Ipv4Range range = Ipv4Range.from(new byte[] {0, 0, 0, 1}).to(new byte[] {0, 0, 0, 3});
assertEquals(ip1, range.start());
assertEquals(ip3, range.end());
}

@Test
public void testBuilderWithInetAddresses() throws UnknownHostException
{
Ipv4Range range = Ipv4Range.from((Inet4Address) InetAddress.getByAddress(new byte[]{0, 0, 0, 1})).to((Inet4Address) InetAddress.getByAddress(new byte[]{0, 0, 0, 3}));
assertEquals(ip1, range.start());
assertEquals(ip3, range.end());
}

@Test
public void testBuilderWithStrings() {
Ipv4Range range = Ipv4Range.from("0.0.0.1").to("0.0.0.3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
Expand All @@ -45,11 +48,21 @@ public void testEqualsContract() {
}

@Test
public void testBuilderMethods() {
public void testBuilderMethods() throws UnknownHostException {
Ipv4 sample = new Ipv4(1l);
assertEquals(sample, Ipv4.of(BigInteger.ONE));
assertEquals(sample, Ipv4.of(1l));
assertEquals(sample, Ipv4.of("0.0.0.1"));
assertEquals(sample, Ipv4.of(new byte[] {0, 0, 0, 1}));
assertEquals(sample, Ipv4.of((Inet4Address) InetAddress.getByAddress(new byte[] {0, 0, 0, 1})));
assertEquals(InetAddress.getByAddress(new byte[] {0, 0, 0, 1}), InetAddress.getByName("0.0.0.1"));
}

@Test
public void testByteArrayBuilder() throws UnknownHostException {
Ipv4 sample = new Ipv4(3325256709l);
// Explicitly test for cases that involve Java's unsigned byte usage.
assertEquals(sample, Ipv4.of(InetAddress.getByName("198.51.100.5").getAddress()));
}

@Test
Expand All @@ -59,6 +72,27 @@ public void testBuilderWithNull() {
Ipv4.of((BigInteger) null);
}

@Test
public void testBuilderWithNullInetAddress() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("value is required");
Ipv4.of((Inet4Address) null);
}

@Test
public void testBuilderNotEnoughBytes() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("exactly 4 octets are required");
Ipv4.of(new byte[3]);
}

@Test
public void testBuilderToManyBytes() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("exactly 4 octets are required");
Ipv4.of(new byte[5]);
}

@Test
public void testUpperBound() {
thrown.expect(IllegalArgumentException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.junit.Test;

import java.math.BigInteger;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;

import static com.github.jgonian.ipmath.Ipv4.LAST_IPV4_ADDRESS;
Expand Down Expand Up @@ -213,6 +216,20 @@ public void testBuilderWithBigIntegers() {
assertEquals(ip3, range.end());
}

@Test
public void testBuilderWithByteArrays() {
Ipv6Range range = Ipv6Range.from(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}).to(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3});
assertEquals(ip1, range.start());
assertEquals(ip3, range.end());
}

@Test
public void testBuilderWithInetAddresses() throws UnknownHostException {
Ipv6Range range = Ipv6Range.from((Inet6Address) InetAddress.getByAddress(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})).to((Inet6Address) InetAddress.getByAddress(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}));
assertEquals(ip1, range.start());
assertEquals(ip3, range.end());
}

@Test
public void testBuilderWithStrings() {
Ipv6Range range = Ipv6Range.from("::1").to("::3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.junit.Test;

import java.math.BigInteger;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import static junit.framework.Assert.assertEquals;
Expand All @@ -50,6 +53,17 @@ public void testFactoryMethodWithString() {
assertEquals(new Ipv6(BigInteger.ZERO), Ipv6.of("::"));
}

@Test
public void testFactoryMethodWithByteArray() {
assertEquals(new Ipv6(BigInteger.ZERO), Ipv6.of(new byte[16])); // Java initializes byte array to all zero.
}

@Test
public void testByteArrayBuilder() throws UnknownHostException {
// Explicitly test for cases that involve Java's unsigned byte usage.
assertEquals(Ipv6.LAST_IPV6_ADDRESS, Ipv6.of(InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff").getAddress()));
}

// Representing IPv6 Addresses

@Test
Expand Down Expand Up @@ -182,11 +196,26 @@ public void shouldNotParseNull() {
Ipv6.parse(null);
}

@Test(expected = IllegalArgumentException.class)
public void shouldNotParseNullInetAddress() {
Ipv6.of((Inet6Address) null);
}

@Test(expected = IllegalArgumentException.class)
public void shouldFailOnEmptyString() {
Ipv6.parse("");
}

@Test(expected = IllegalArgumentException.class)
public void testBuilderNotEnoughBytes() {
Ipv4.of(new byte[15]);
}

@Test(expected = IllegalArgumentException.class)
public void testBuilderToManyBytes() {
Ipv4.of(new byte[17]);
}

@Test
public void shouldParseFullAddressesCaseInsensitively() {
assertEquals(Ipv6.parse("abcd:ef01:2345:6789:abcd:ef01:2345:6789"), Ipv6.parse("ABCD:EF01:2345:6789:ABCD:EF01:2345:6789"));
Expand Down