Pavlin Radoslavov

Reimplementation of classes Ip4Address/Ip6Address/Ip4Prefix/Ip6Prefix

and the corresponding unit tests.

* Reimplemented classes Ip4Address and Ip6Address by inheriting from
  class IpAddress
* Reimplemented classes Ip4Prefix and Ip6Prefix by inheriting from
  class IpPrefix
* Reimplemented the unit tests Ip4AddressTest and Ip6AddressTest to
  match the corresponding IpAddressTest unit tests
* Reimplemented the unit tests Ip4PrefixTest and Ip6PrefixTest to
  match the corresponding IpPrefixTest unit tests
* Minor refactoring/cleanup of classes IpAddress and IpPrefix
......@@ -15,203 +15,160 @@
*/
package org.onlab.packet;
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.nio.ByteBuffer;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Arrays;
import com.google.common.net.InetAddresses;
/**
* The class representing an IPv4 address.
* A class representing an IPv4 address.
* This class is immutable.
*/
public final class Ip4Address implements Comparable<Ip4Address> {
private final int value;
/** The length of the address in bytes (octets). */
public static final int BYTE_LENGTH = 4;
/** The length of the address in bits. */
public static final int BIT_LENGTH = BYTE_LENGTH * Byte.SIZE;
public final class Ip4Address extends IpAddress {
public static final IpAddress.Version VERSION = IpAddress.Version.INET;
public static final int BYTE_LENGTH = IpAddress.INET_BYTE_LENGTH;
public static final int BIT_LENGTH = IpAddress.INET_BIT_LENGTH;
/**
* Default constructor.
* Constructor for given IP address version and address octets.
*
* @param value the IP address value stored in network byte order
* (i.e., the most significant byte first)
* @throws IllegalArgumentException if the arguments are invalid
*/
public Ip4Address() {
this.value = 0;
private Ip4Address(byte[] value) {
super(VERSION, value);
}
/**
* Copy constructor.
* Returns the integer value of this IPv4 address.
*
* @param other the object to copy from
* @return the IPv4 address's value as an integer
*/
public Ip4Address(Ip4Address other) {
this.value = other.value;
public int toInt() {
ByteBuffer bb = ByteBuffer.wrap(super.toOctets());
return bb.getInt();
}
/**
* Constructor from an integer value.
* Converts an integer into an IPv4 address.
*
* @param value the value to use
* @param value an integer representing an IPv4 address value
* @return an IPv4 address
*/
public Ip4Address(int value) {
this.value = value;
public static Ip4Address valueOf(int value) {
byte[] bytes =
ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array();
return new Ip4Address(bytes);
}
/**
* Constructor from a byte array with the IPv4 address stored in network
* byte order (i.e., the most significant byte first).
* Converts a byte array into an IPv4 address.
*
* @param value the value to use
* @param value the IPv4 address value stored in network byte order
* (i.e., the most significant byte first)
* @return an IPv4 address
* @throws IllegalArgumentException if the argument is invalid
*/
public Ip4Address(byte[] value) {
this(value, 0);
public static Ip4Address valueOf(byte[] value) {
return new Ip4Address(value);
}
/**
* Constructor from a byte array with the IPv4 address stored in network
* byte order (i.e., the most significant byte first), and a given offset
* from the beginning of the byte array.
*
* Converts a byte array and a given offset from the beginning of the
* array into an IPv4 address.
* <p>
* The IP address is stored in network byte order (i.e., the most
* significant byte first).
* </p>
* @param value the value to use
* @param offset the offset in bytes from the beginning of the byte array
* @return an IPv4 address
* @throws IllegalArgumentException if the arguments are invalid
*/
public Ip4Address(byte[] value, int offset) {
checkNotNull(value);
// Verify the arguments
if ((offset < 0) || (offset + BYTE_LENGTH > value.length)) {
String msg;
if (value.length < BYTE_LENGTH) {
msg = "Invalid IPv4 address array: array length: " +
value.length + ". Must be at least " + BYTE_LENGTH;
} else {
msg = "Invalid IPv4 address array: array offset: " +
offset + ". Must be in the interval [0, " +
(value.length - BYTE_LENGTH) + "]";
}
throw new IllegalArgumentException(msg);
}
// Read the address
ByteBuffer bb = ByteBuffer.wrap(value);
this.value = bb.getInt(offset);
public static Ip4Address valueOf(byte[] value, int offset) {
IpAddress.checkArguments(VERSION, value, offset);
byte[] bc = Arrays.copyOfRange(value, offset, value.length);
return Ip4Address.valueOf(bc);
}
/**
* Constructs an IPv4 address from a string representation of the address.
*<p>
* Example: "1.2.3.4"
* Converts an InetAddress into an IPv4 address.
*
* @param value the value to use
* @param inetAddress the InetAddress value to use. It must contain an IPv4
* address
* @return an IPv4 address
* @throws IllegalArgumentException if the argument is invalid
*/
public Ip4Address(String value) {
checkNotNull(value);
String[] splits = value.split("\\.");
if (splits.length != 4) {
final String msg = "Invalid IPv4 address string: " + value;
public static Ip4Address valueOf(InetAddress inetAddress) {
byte[] bytes = inetAddress.getAddress();
if (inetAddress instanceof Inet4Address) {
return new Ip4Address(bytes);
}
if ((inetAddress instanceof Inet6Address) ||
(bytes.length == INET6_BYTE_LENGTH)) {
final String msg = "Invalid IPv4 version address string: " +
inetAddress.toString();
throw new IllegalArgumentException(msg);
}
int result = 0;
for (int i = 0; i < BYTE_LENGTH; i++) {
result |= Integer.parseInt(splits[i]) <<
((BYTE_LENGTH - (i + 1)) * Byte.SIZE);
// Use the number of bytes as a hint
if (bytes.length == INET_BYTE_LENGTH) {
return new Ip4Address(bytes);
}
this.value = result;
final String msg = "Unrecognized IP version address string: " +
inetAddress.toString();
throw new IllegalArgumentException(msg);
}
/**
* Gets the IPv4 address as a byte array.
* Converts an IPv4 string literal (e.g., "10.2.3.4") into an IP address.
*
* @return a byte array with the IPv4 address stored in network byte order
* (i.e., the most significant byte first).
* @param value an IPv4 address value in string form
* @return an IPv4 address
* @throws IllegalArgumentException if the argument is invalid
*/
public byte[] toOctets() {
return ByteBuffer.allocate(BYTE_LENGTH).putInt(value).array();
public static Ip4Address valueOf(String value) {
InetAddress inetAddress = null;
try {
inetAddress = InetAddresses.forString(value);
} catch (IllegalArgumentException e) {
final String msg = "Invalid IP address string: " + value;
throw new IllegalArgumentException(msg);
}
return valueOf(inetAddress);
}
/**
* Creates an IPv4 network mask prefix.
*
* @param prefixLen the length of the mask prefix. Must be in the interval
* [0, 32].
* @param prefixLength the length of the mask prefix. Must be in the
* interval [0, 32]
* @return a new IPv4 address that contains a mask prefix of the
* specified length
* @throws IllegalArgumentException if the argument is invalid
*/
public static Ip4Address makeMaskPrefix(int prefixLen) {
// Verify the prefix length
if ((prefixLen < 0) || (prefixLen > Ip4Address.BIT_LENGTH)) {
final String msg = "Invalid IPv4 prefix length: " + prefixLen +
". Must be in the interval [0, 32].";
throw new IllegalArgumentException(msg);
}
long v =
(0xffffffffL << (Ip4Address.BIT_LENGTH - prefixLen)) & 0xffffffffL;
return new Ip4Address((int) v);
public static Ip4Address makeMaskPrefix(int prefixLength) {
byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength);
return new Ip4Address(mask);
}
/**
* Creates an IPv4 address by masking it with a network mask of given
* mask length.
*
* @param addr the address to mask
* @param prefixLen the length of the mask prefix. Must be in the interval
* [0, 32].
* @param address the address to mask
* @param prefixLength the length of the mask prefix. Must be in the
* interval [0, 32]
* @return a new IPv4 address that is masked with a mask prefix of the
* specified length
* @throws IllegalArgumentException if the prefix length is invalid
*/
public static Ip4Address makeMaskedAddress(final Ip4Address addr,
int prefixLen) {
Ip4Address mask = Ip4Address.makeMaskPrefix(prefixLen);
long v = addr.value & mask.value;
return new Ip4Address((int) v);
}
/**
* Gets the value of the IPv4 address.
*
* @return the value of the IPv4 address
*/
public int getValue() {
return value;
}
/**
* Converts the IPv4 value to a '.' separated string.
*
* @return the IPv4 value as a '.' separated string
*/
@Override
public String toString() {
return ((this.value >> 24) & 0xff) + "." +
((this.value >> 16) & 0xff) + "." +
((this.value >> 8) & 0xff) + "." +
(this.value & 0xff);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Ip4Address)) {
return false;
}
Ip4Address other = (Ip4Address) o;
if (this.value != other.value) {
return false;
}
return true;
}
@Override
public int hashCode() {
return this.value;
}
@Override
public int compareTo(Ip4Address o) {
Long lv = ((long) this.value) & 0xffffffffL;
Long rv = ((long) o.value) & 0xffffffffL;
return lv.compareTo(rv);
public static Ip4Address makeMaskedAddress(final Ip4Address address,
int prefixLength) {
byte[] net = makeMaskedAddressArray(address, prefixLength);
return Ip4Address.valueOf(net);
}
}
......
......@@ -15,110 +15,90 @@
*/
package org.onlab.packet;
import java.util.Objects;
/**
* The class representing an IPv4 network address.
* This class is immutable.
*/
public final class Ip4Prefix {
private final Ip4Address address; // The IPv4 address
private final short prefixLen; // The prefix length
public final class Ip4Prefix extends IpPrefix {
public static final IpAddress.Version VERSION = IpAddress.Version.INET;
// Maximum network mask length
public static final int MAX_MASK_LENGTH = IpPrefix.MAX_INET_MASK_LENGTH;
/**
* Default constructor.
*/
public Ip4Prefix() {
this.address = new Ip4Address();
this.prefixLen = 0;
}
/**
* Copy constructor.
* Constructor for given IPv4 address, and a prefix length.
*
* @param other the object to copy from
* @param address the IPv4 address
* @param prefixLength the prefix length
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public Ip4Prefix(Ip4Prefix other) {
this.address = new Ip4Address(other.address);
this.prefixLen = other.prefixLen;
private Ip4Prefix(Ip4Address address, int prefixLength) {
super(address, prefixLength);
}
/**
* Constructor for a given address and prefix length.
* Returns the IPv4 address value of the prefix.
*
* @param address the address to use
* @param prefixLen the prefix length to use
* @return the IPv4 address value of the prefix
*/
public Ip4Prefix(Ip4Address address, short prefixLen) {
this.address = Ip4Address.makeMaskedAddress(address, prefixLen);
this.prefixLen = prefixLen;
public Ip4Address address() {
IpAddress a = super.address();
return (Ip4Address) a;
}
/**
* Constructs an IPv4 prefix from a string representation of the
* prefix.
*<p>
* Example: "1.2.0.0/16"
* Converts an integer and a prefix length into an IPv4 prefix.
*
* @param value the value to use
* @param address an integer representing the IPv4 address
* @param prefixLength the prefix length
* @return an IPv4 prefix
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public Ip4Prefix(String value) {
String[] splits = value.split("/");
if (splits.length != 2) {
throw new IllegalArgumentException("Specified IPv4 prefix must contain an IPv4 " +
"address and a prefix length separated by '/'");
}
this.prefixLen = Short.decode(splits[1]);
this.address = Ip4Address.makeMaskedAddress(new Ip4Address(splits[0]),
this.prefixLen);
public static Ip4Prefix valueOf(int address, int prefixLength) {
return new Ip4Prefix(Ip4Address.valueOf(address), prefixLength);
}
/**
* Gets the address value of the IPv4 prefix.
* Converts a byte array and a prefix length into an IPv4 prefix.
*
* @return the address value of the IPv4 prefix
* @param address the IPv4 address value stored in network byte order
* @param prefixLength the prefix length
* @return an IPv4 prefix
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public Ip4Address getAddress() {
return address;
public static Ip4Prefix valueOf(byte[] address, int prefixLength) {
return new Ip4Prefix(Ip4Address.valueOf(address), prefixLength);
}
/**
* Gets the prefix length value of the IPv4 prefix.
* Converts an IPv4 address and a prefix length into an IPv4 prefix.
*
* @return the prefix length value of the IPv4 prefix
* @param address the IPv4 address
* @param prefixLength the prefix length
* @return an IPv4 prefix
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public short getPrefixLen() {
return prefixLen;
public static Ip4Prefix valueOf(Ip4Address address, int prefixLength) {
return new Ip4Prefix(address, prefixLength);
}
/**
* Converts the IPv4 prefix value to an "address/prefixLen" string.
* Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16")
* into an IPv4 prefix.
*
* @return the IPv4 prefix value as an "address/prefixLen" string
* @param address an IP prefix in string form (e.g., "10.1.0.0/16")
* @return an IPv4 prefix
* @throws IllegalArgumentException if the arguments are invalid
*/
@Override
public String toString() {
return this.address.toString() + "/" + this.prefixLen;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Ip4Prefix)) {
return false;
}
Ip4Prefix otherIp4Prefix = (Ip4Prefix) other;
return Objects.equals(this.address, otherIp4Prefix.address)
&& this.prefixLen == otherIp4Prefix.prefixLen;
public static Ip4Prefix valueOf(String address) {
final String[] parts = address.split("/");
if (parts.length != 2) {
String msg = "Malformed IPv4 prefix string: " + address + "." +
"Address must take form \"x.x.x.x/y\"";
throw new IllegalArgumentException(msg);
}
Ip4Address ipAddress = Ip4Address.valueOf(parts[0]);
int prefixLength = Integer.parseInt(parts[1]);
@Override
public int hashCode() {
return Objects.hash(address, prefixLen);
return new Ip4Prefix(ipAddress, prefixLength);
}
}
......
......@@ -16,260 +16,137 @@
package org.onlab.packet;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.util.Arrays;
import com.google.common.net.InetAddresses;
import com.google.common.primitives.UnsignedLongs;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
/**
* The class representing an IPv6 address.
* A class representing an IPv6 address.
* This class is immutable.
*/
public final class Ip6Address implements Comparable<Ip6Address> {
private final long valueHigh; // The higher (more significant) 64 bits
private final long valueLow; // The lower (less significant) 64 bits
/** The length of the address in bytes (octets). */
public static final int BYTE_LENGTH = 16;
/** The length of the address in bits. */
public static final int BIT_LENGTH = BYTE_LENGTH * Byte.SIZE;
/**
* Default constructor.
*/
public Ip6Address() {
this.valueHigh = 0;
this.valueLow = 0;
}
public final class Ip6Address extends IpAddress {
public static final IpAddress.Version VERSION = IpAddress.Version.INET6;
public static final int BYTE_LENGTH = IpAddress.INET6_BYTE_LENGTH;
public static final int BIT_LENGTH = IpAddress.INET6_BIT_LENGTH;
/**
* Copy constructor.
* Constructor for given IP address version and address octets.
*
* @param other the object to copy from
* @param value the IP address value stored in network byte order
* (i.e., the most significant byte first)
* @throws IllegalArgumentException if the arguments are invalid
*/
public Ip6Address(Ip6Address other) {
this.valueHigh = other.valueHigh;
this.valueLow = other.valueLow;
private Ip6Address(byte[] value) {
super(VERSION, value);
}
/**
* Constructor from integer values.
* Converts a byte array into an IPv6 address.
*
* @param valueHigh the higher (more significant) 64 bits of the address
* @param valueLow the lower (less significant) 64 bits of the address
* @param value the IPv6 address value stored in network byte order
* (i.e., the most significant byte first)
* @return an IPv6 address
* @throws IllegalArgumentException if the argument is invalid
*/
public Ip6Address(long valueHigh, long valueLow) {
this.valueHigh = valueHigh;
this.valueLow = valueLow;
public static Ip6Address valueOf(byte[] value) {
return new Ip6Address(value);
}
/**
* Constructor from a byte array with the IPv6 address stored in network
* byte order (i.e., the most significant byte first).
*
* Converts a byte array and a given offset from the beginning of the
* array into an IPv6 address.
* <p>
* The IP address is stored in network byte order (i.e., the most
* significant byte first).
* </p>
* @param value the value to use
* @param offset the offset in bytes from the beginning of the byte array
* @return an IPv6 address
* @throws IllegalArgumentException if the arguments are invalid
*/
public Ip6Address(byte[] value) {
this(value, 0);
public static Ip6Address valueOf(byte[] value, int offset) {
IpAddress.checkArguments(VERSION, value, offset);
byte[] bc = Arrays.copyOfRange(value, offset, value.length);
return Ip6Address.valueOf(bc);
}
/**
* Constructor from a byte array with the IPv6 address stored in network
* byte order (i.e., the most significant byte first), and a given offset
* from the beginning of the byte array.
* Converts an InetAddress into an IPv6 address.
*
* @param value the value to use
* @param offset the offset in bytes from the beginning of the byte array
* @param inetAddress the InetAddress value to use. It must contain an IPv6
* address
* @return an IPv6 address
* @throws IllegalArgumentException if the argument is invalid
*/
public Ip6Address(byte[] value, int offset) {
checkNotNull(value);
// Verify the arguments
if ((offset < 0) || (offset + BYTE_LENGTH > value.length)) {
String msg;
if (value.length < BYTE_LENGTH) {
msg = "Invalid IPv6 address array: array length: " +
value.length + ". Must be at least " + BYTE_LENGTH;
} else {
msg = "Invalid IPv6 address array: array offset: " +
offset + ". Must be in the interval [0, " +
(value.length - BYTE_LENGTH) + "]";
}
public static Ip6Address valueOf(InetAddress inetAddress) {
byte[] bytes = inetAddress.getAddress();
if (inetAddress instanceof Inet6Address) {
return new Ip6Address(bytes);
}
if ((inetAddress instanceof Inet4Address) ||
(bytes.length == INET_BYTE_LENGTH)) {
final String msg = "Invalid IPv6 version address string: " +
inetAddress.toString();
throw new IllegalArgumentException(msg);
}
// Read the address
ByteBuffer bb = ByteBuffer.wrap(value);
bb.position(offset);
this.valueHigh = bb.getLong();
this.valueLow = bb.getLong();
// Use the number of bytes as a hint
if (bytes.length == INET6_BYTE_LENGTH) {
return new Ip6Address(bytes);
}
final String msg = "Unrecognized IP version address string: " +
inetAddress.toString();
throw new IllegalArgumentException(msg);
}
/**
* Constructs an IPv6 address from a string representation of the address.
*<p>
* Example: "1111:2222::8888"
* Converts an IPv6 string literal (e.g., "1111:2222::8888") into an IP
* address.
*
* @param value the value to use
* @param value an IPv6 address value in string form
* @return an IPv6 address
* @throws IllegalArgumentException if the argument is invalid
*/
public Ip6Address(String value) {
checkNotNull(value);
if (value.isEmpty()) {
final String msg = "Specified IPv6 cannot be an empty string";
throw new IllegalArgumentException(msg);
}
InetAddress addr = null;
public static Ip6Address valueOf(String value) {
InetAddress inetAddress = null;
try {
addr = InetAddresses.forString(value);
inetAddress = InetAddresses.forString(value);
} catch (IllegalArgumentException e) {
final String msg = "Invalid IPv6 address string: " + value;
final String msg = "Invalid IP address string: " + value;
throw new IllegalArgumentException(msg);
}
byte[] bytes = addr.getAddress();
ByteBuffer bb = ByteBuffer.wrap(bytes);
this.valueHigh = bb.getLong();
this.valueLow = bb.getLong();
}
/**
* Gets the IPv6 address as a byte array.
*
* @return a byte array with the IPv6 address stored in network byte order
* (i.e., the most significant byte first).
*/
public byte[] toOctets() {
return ByteBuffer.allocate(BYTE_LENGTH)
.putLong(valueHigh).putLong(valueLow).array();
return valueOf(inetAddress);
}
/**
* Creates an IPv6 network mask prefix.
*
* @param prefixLen the length of the mask prefix. Must be in the interval
* [0, 128].
* @param prefixLength the length of the mask prefix. Must be in the
* interval [0, 128]
* @return a new IPv6 address that contains a mask prefix of the
* specified length
* @throws IllegalArgumentException if the arguments are invalid
*/
public static Ip6Address makeMaskPrefix(int prefixLen) {
long vh, vl;
// Verify the prefix length
if ((prefixLen < 0) || (prefixLen > Ip6Address.BIT_LENGTH)) {
final String msg = "Invalid IPv6 prefix length: " + prefixLen +
". Must be in the interval [0, 128].";
throw new IllegalArgumentException(msg);
}
if (prefixLen == 0) {
//
// NOTE: Apparently, the result of "<< 64" shifting to the left
// results in all 1s instead of all 0s, hence we handle it as
// a special case.
//
vh = 0;
vl = 0;
} else if (prefixLen <= 64) {
vh = (0xffffffffffffffffL << (64 - prefixLen)) & 0xffffffffffffffffL;
vl = 0;
} else {
vh = -1L; // All 1s
vl = (0xffffffffffffffffL << (128 - prefixLen)) & 0xffffffffffffffffL;
}
return new Ip6Address(vh, vl);
public static Ip6Address makeMaskPrefix(int prefixLength) {
byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength);
return new Ip6Address(mask);
}
/**
* Creates an IPv6 address by masking it with a network mask of given
* mask length.
*
* @param addr the address to mask
* @param prefixLen the length of the mask prefix. Must be in the interval
* [0, 128].
* @param address the address to mask
* @param prefixLength the length of the mask prefix. Must be in the
* interval [0, 128]
* @return a new IPv6 address that is masked with a mask prefix of the
* specified length
* @throws IllegalArgumentException if the prefix length is invalid
*/
public static Ip6Address makeMaskedAddress(final Ip6Address addr,
int prefixLen) {
Ip6Address mask = Ip6Address.makeMaskPrefix(prefixLen);
long vh = addr.valueHigh & mask.valueHigh;
long vl = addr.valueLow & mask.valueLow;
return new Ip6Address(vh, vl);
}
/**
* Gets the value of the higher (more significant) 64 bits of the address.
*
* @return the value of the higher (more significant) 64 bits of the
* address
*/
public long getValueHigh() {
return valueHigh;
}
/**
* Gets the value of the lower (less significant) 64 bits of the address.
*
* @return the value of the lower (less significant) 64 bits of the
* address
*/
public long getValueLow() {
return valueLow;
}
/**
* Converts the IPv6 value to a ':' separated string.
*
* @return the IPv6 value as a ':' separated string
*/
@Override
public String toString() {
ByteBuffer bb = ByteBuffer.allocate(Ip6Address.BYTE_LENGTH);
bb.putLong(valueHigh);
bb.putLong(valueLow);
InetAddress inetAddr = null;
try {
inetAddr = InetAddress.getByAddress(bb.array());
} catch (UnknownHostException e) {
// Should never happen
checkState(false, "Internal error: Ip6Address.toString()");
return "::";
}
return InetAddresses.toAddrString(inetAddr);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Ip6Address)) {
return false;
}
Ip6Address other = (Ip6Address) o;
return this.valueHigh == other.valueHigh
&& this.valueLow == other.valueLow;
}
@Override
public int hashCode() {
return Objects.hash(valueHigh, valueLow);
}
@Override
public int compareTo(Ip6Address o) {
// Compare the high-order 64-bit value
if (this.valueHigh != o.valueHigh) {
return UnsignedLongs.compare(this.valueHigh, o.valueHigh);
}
// Compare the low-order 64-bit value
if (this.valueLow != o.valueLow) {
return UnsignedLongs.compare(this.valueLow, o.valueLow);
}
return 0;
public static Ip6Address makeMaskedAddress(final Ip6Address address,
int prefixLength) {
byte[] net = makeMaskedAddressArray(address, prefixLength);
return Ip6Address.valueOf(net);
}
}
......
......@@ -15,110 +15,79 @@
*/
package org.onlab.packet;
import java.util.Objects;
/**
* The class representing an IPv6 network address.
* This class is immutable.
*/
public final class Ip6Prefix {
private final Ip6Address address; // The IPv6 address
private final short prefixLen; // The prefix length
/**
* Default constructor.
*/
public Ip6Prefix() {
this.address = new Ip6Address();
this.prefixLen = 0;
}
/**
* Copy constructor.
*
* @param other the object to copy from
*/
public Ip6Prefix(Ip6Prefix other) {
this.address = new Ip6Address(other.address);
this.prefixLen = other.prefixLen;
}
public final class Ip6Prefix extends IpPrefix {
public static final IpAddress.Version VERSION = IpAddress.Version.INET6;
// Maximum network mask length
public static final int MAX_MASK_LENGTH = IpPrefix.MAX_INET6_MASK_LENGTH;
/**
* Constructor for a given address and prefix length.
* Constructor for given IPv6 address, and a prefix length.
*
* @param address the address to use
* @param prefixLen the prefix length to use
* @param address the IPv6 address
* @param prefixLength the prefix length
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public Ip6Prefix(Ip6Address address, short prefixLen) {
this.address = Ip6Address.makeMaskedAddress(address, prefixLen);
this.prefixLen = prefixLen;
private Ip6Prefix(Ip6Address address, int prefixLength) {
super(address, prefixLength);
}
/**
* Constructs an IPv6 prefix from a string representation of the
* prefix.
*<p>
* Example: "1111:2222::/32"
* Returns the IPv6 address value of the prefix.
*
* @param value the value to use
* @return the IPv6 address value of the prefix
*/
public Ip6Prefix(String value) {
String[] splits = value.split("/");
if (splits.length != 2) {
throw new IllegalArgumentException("Specified IPv6 prefix must contain an IPv6 " +
"address and a prefix length separated by '/'");
}
this.prefixLen = Short.decode(splits[1]);
this.address = Ip6Address.makeMaskedAddress(new Ip6Address(splits[0]),
this.prefixLen);
public Ip6Address address() {
IpAddress a = super.address();
return (Ip6Address) a;
}
/**
* Gets the address value of the IPv6 prefix.
* Converts a byte array and a prefix length into an IPv6 prefix.
*
* @return the address value of the IPv6 prefix
* @param address the IPv6 address value stored in network byte order
* @param prefixLength the prefix length
* @return an IPv6 prefix
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public Ip6Address getAddress() {
return address;
public static Ip6Prefix valueOf(byte[] address, int prefixLength) {
return new Ip6Prefix(Ip6Address.valueOf(address), prefixLength);
}
/**
* Gets the prefix length value of the IPv6 prefix.
* Converts an IPv6 address and a prefix length into an IPv6 prefix.
*
* @return the prefix length value of the IPv6 prefix
* @param address the IPv6 address
* @param prefixLength the prefix length
* @return an IPv6 prefix
* @throws IllegalArgumentException if the prefix length value is invalid
*/
public short getPrefixLen() {
return prefixLen;
public static Ip6Prefix valueOf(Ip6Address address, int prefixLength) {
return new Ip6Prefix(address, prefixLength);
}
/**
* Converts the IPv6 prefix value to an "address/prefixLen" string.
* Converts a CIDR (slash) notation string (e.g., "1111:2222::/64")
* into an IPv6 prefix.
*
* @return the IPv6 prefix value as an "address/prefixLen" string
* @param address an IP prefix in string form (e.g.,"1111:2222::/64")
* @return an IPv6 prefix
* @throws IllegalArgumentException if the arguments are invalid
*/
@Override
public String toString() {
return this.address.toString() + "/" + this.prefixLen;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Ip6Prefix)) {
return false;
}
Ip6Prefix otherIp6Prefix = (Ip6Prefix) other;
return Objects.equals(this.address, otherIp6Prefix.address)
&& this.prefixLen == otherIp6Prefix.prefixLen;
public static Ip6Prefix valueOf(String address) {
final String[] parts = address.split("/");
if (parts.length != 2) {
String msg = "Malformed IPv6 prefix string: " + address + "." +
"Address must take form " +
"\"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/y\"";
throw new IllegalArgumentException(msg);
}
Ip6Address ipAddress = Ip6Address.valueOf(parts[0]);
int prefixLength = Integer.parseInt(parts[1]);
@Override
public int hashCode() {
return Objects.hash(address, prefixLen);
return new Ip6Prefix(ipAddress, prefixLength);
}
}
......
......@@ -30,8 +30,9 @@ import static com.google.common.base.Preconditions.checkState;
/**
* A class representing an IP address.
* This class is immutable.
*/
public final class IpAddress implements Comparable<IpAddress> {
public class IpAddress implements Comparable<IpAddress> {
// IP Versions
public enum Version { INET, INET6 };
......@@ -52,7 +53,7 @@ public final class IpAddress implements Comparable<IpAddress> {
* (i.e., the most significant byte first)
* @throws IllegalArgumentException if the arguments are invalid
*/
private IpAddress(Version version, byte[] value) {
protected IpAddress(Version version, byte[] value) {
checkArguments(version, value, 0);
this.version = version;
switch (version) {
......@@ -88,7 +89,7 @@ public final class IpAddress implements Comparable<IpAddress> {
}
/**
* Returns the integral value of this IP address.
* Returns the integer value of this IP address.
* TODO: This method should be moved to Ip4Address.
*
* @return the IP address's value as an integer
......@@ -219,31 +220,7 @@ public final class IpAddress implements Comparable<IpAddress> {
* @throws IllegalArgumentException if the arguments are invalid
*/
public static IpAddress makeMaskPrefix(Version version, int prefixLength) {
int addrByteLength = byteLength(version);
int addrBitLength = addrByteLength * Byte.SIZE;
// Verify the prefix length
if ((prefixLength < 0) || (prefixLength > addrBitLength)) {
final String msg = "Invalid IP prefix length: " + prefixLength +
". Must be in the interval [0, " + addrBitLength + "].";
throw new IllegalArgumentException(msg);
}
// Number of bytes and extra bits that should be all 1s
int maskBytes = prefixLength / Byte.SIZE;
int maskBits = prefixLength % Byte.SIZE;
byte[] mask = new byte[addrByteLength];
// Set the bytes and extra bits to 1s
for (int i = 0; i < maskBytes; i++) {
mask[i] = (byte) 0xff; // Set mask bytes to 1s
}
for (int i = maskBytes; i < addrByteLength; i++) {
mask[i] = 0; // Set remaining bytes to 0s
}
if (maskBits > 0) {
mask[maskBytes] = (byte) (0xff << (Byte.SIZE - maskBits));
}
byte[] mask = makeMaskPrefixArray(version, prefixLength);
return new IpAddress(version, mask);
}
......@@ -251,24 +228,26 @@ public final class IpAddress implements Comparable<IpAddress> {
* Creates an IP address by masking it with a network mask of given
* mask length.
*
* @param addr the address to mask
* @param address the address to mask
* @param prefixLength the length of the mask prefix. Must be in the
* interval [0, 32] for IPv4, or [0, 128] for IPv6
* @return a new IP address that is masked with a mask prefix of the
* specified length
* @throws IllegalArgumentException if the prefix length is invalid
*/
public static IpAddress makeMaskedAddress(final IpAddress addr,
public static IpAddress makeMaskedAddress(final IpAddress address,
int prefixLength) {
IpAddress mask = IpAddress.makeMaskPrefix(addr.version(),
prefixLength);
byte[] net = new byte[mask.octets.length];
// Mask each byte
for (int i = 0; i < net.length; i++) {
net[i] = (byte) (addr.octets[i] & mask.octets[i]);
// TODO: The code below should go away and replaced with generics
if (address instanceof Ip4Address) {
Ip4Address ip4a = (Ip4Address) address;
return Ip4Address.makeMaskedAddress(ip4a, prefixLength);
} else if (address instanceof Ip6Address) {
Ip6Address ip6a = (Ip6Address) address;
return Ip6Address.makeMaskedAddress(ip6a, prefixLength);
} else {
byte[] net = makeMaskedAddressArray(address, prefixLength);
return IpAddress.valueOf(address.version(), net);
}
return IpAddress.valueOf(addr.version(), net);
}
@Override
......@@ -352,8 +331,7 @@ public final class IpAddress implements Comparable<IpAddress> {
* array with the address
* @throws IllegalArgumentException if any of the arguments is invalid
*/
private static void checkArguments(Version version, byte[] value,
int offset) {
static void checkArguments(Version version, byte[] value, int offset) {
// Check the offset and byte array length
int addrByteLength = byteLength(version);
if ((offset < 0) || (offset + addrByteLength > value.length)) {
......@@ -371,4 +349,67 @@ public final class IpAddress implements Comparable<IpAddress> {
throw new IllegalArgumentException(msg);
}
}
/**
* Creates a byte array for IP network mask prefix.
*
* @param version the IP address version
* @param prefixLength the length of the mask prefix. Must be in the
* interval [0, 32] for IPv4, or [0, 128] for IPv6
* @return a byte array that contains a mask prefix of the
* specified length
* @throws IllegalArgumentException if the arguments are invalid
*/
static byte[] makeMaskPrefixArray(Version version, int prefixLength) {
int addrByteLength = byteLength(version);
int addrBitLength = addrByteLength * Byte.SIZE;
// Verify the prefix length
if ((prefixLength < 0) || (prefixLength > addrBitLength)) {
final String msg = "Invalid IP prefix length: " + prefixLength +
". Must be in the interval [0, " + addrBitLength + "].";
throw new IllegalArgumentException(msg);
}
// Number of bytes and extra bits that should be all 1s
int maskBytes = prefixLength / Byte.SIZE;
int maskBits = prefixLength % Byte.SIZE;
byte[] mask = new byte[addrByteLength];
// Set the bytes and extra bits to 1s
for (int i = 0; i < maskBytes; i++) {
mask[i] = (byte) 0xff; // Set mask bytes to 1s
}
for (int i = maskBytes; i < addrByteLength; i++) {
mask[i] = 0; // Set remaining bytes to 0s
}
if (maskBits > 0) {
mask[maskBytes] = (byte) (0xff << (Byte.SIZE - maskBits));
}
return mask;
}
/**
* Creates a byte array that represents an IP address masked with
* a network mask of given mask length.
*
* @param addr the address to mask
* @param prefixLength the length of the mask prefix. Must be in the
* interval [0, 32] for IPv4, or [0, 128] for IPv6
* @return a byte array that represents the IP address masked with
* a mask prefix of the specified length
* @throws IllegalArgumentException if the prefix length is invalid
*/
static byte[] makeMaskedAddressArray(final IpAddress addr,
int prefixLength) {
byte[] mask = IpAddress.makeMaskPrefixArray(addr.version(),
prefixLength);
byte[] net = new byte[mask.length];
// Mask each byte
for (int i = 0; i < net.length; i++) {
net[i] = (byte) (addr.octets[i] & mask[i]);
}
return net;
}
}
......
......@@ -20,12 +20,13 @@ import java.util.Objects;
/**
* A class representing an IP prefix. A prefix consists of an IP address and
* a subnet mask.
* This class is immutable.
* <p>
* NOTE: The stored IP address in the result IP prefix is masked to
* contain zeroes in all bits after the prefix length.
* </p>
*/
public final class IpPrefix {
public class IpPrefix {
// Maximum network mask length
public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH;
public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH;
......@@ -40,7 +41,7 @@ public final class IpPrefix {
* @param prefixLength the prefix length
* @throws IllegalArgumentException if the prefix length value is invalid
*/
private IpPrefix(IpAddress address, int prefixLength) {
protected IpPrefix(IpAddress address, int prefixLength) {
checkPrefixLength(address.version(), prefixLength);
this.address = IpAddress.makeMaskedAddress(address, prefixLength);
this.prefixLength = (short) prefixLength;
......@@ -100,7 +101,7 @@ public final class IpPrefix {
}
/**
* Converts an IP address and a prefix length into IP prefix.
* Converts an IP address and a prefix length into an IP prefix.
*
* @param address the IP address
* @param prefixLength the prefix length
......@@ -112,10 +113,11 @@ public final class IpPrefix {
}
/**
* Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16") into an
* IP prefix.
* Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16" or
* "1111:2222::/64") into an IP prefix.
*
* @param address an IP prefix in string form, e.g. "10.1.0.0/16"
* @param address an IP prefix in string form (e.g. "10.1.0.0/16" or
* "1111:2222::/64")
* @return an IP prefix
* @throws IllegalArgumentException if the arguments are invalid
*/
......@@ -123,7 +125,8 @@ public final class IpPrefix {
final String[] parts = address.split("/");
if (parts.length != 2) {
String msg = "Malformed IP prefix string: " + address + "." +
"Address must take form \"x.x.x.x/y\"";
"Address must take form \"x.x.x.x/y\" or " +
"\"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/y\"";
throw new IllegalArgumentException(msg);
}
IpAddress ipAddress = IpAddress.valueOf(parts[0]);
......
......@@ -15,10 +15,13 @@
*/
package org.onlab.packet;
import com.google.common.net.InetAddresses;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import java.net.InetAddress;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
......@@ -36,10 +39,18 @@ public class Ip4AddressTest {
}
/**
* Tests the IPv4 address version constant.
*/
@Test
public void testAddressVersion() {
assertThat(Ip4Address.VERSION, is(IpAddress.Version.INET));
}
/**
* Tests the length of the address in bytes (octets).
*/
@Test
public void testAddrBytelen() {
public void testAddrByteLength() {
assertThat(Ip4Address.BYTE_LENGTH, is(4));
}
......@@ -47,299 +58,372 @@ public class Ip4AddressTest {
* Tests the length of the address in bits.
*/
@Test
public void testAddrBitlen() {
public void testAddrBitLength() {
assertThat(Ip4Address.BIT_LENGTH, is(32));
}
/**
* Tests default class constructor.
* Tests returning the IP address version.
*/
@Test
public void testDefaultConstructor() {
Ip4Address ip4Address = new Ip4Address();
assertThat(ip4Address.toString(), is("0.0.0.0"));
public void testVersion() {
Ip4Address ipAddress;
// IPv4
ipAddress = Ip4Address.valueOf("0.0.0.0");
assertThat(ipAddress.version(), is(IpAddress.Version.INET));
}
/**
* Tests valid class copy constructor.
* Tests returning an IPv4 address as a byte array.
*/
@Test
public void testCopyConstructor() {
Ip4Address fromAddr = new Ip4Address("1.2.3.4");
Ip4Address ip4Address = new Ip4Address(fromAddr);
assertThat(ip4Address.toString(), is("1.2.3.4"));
fromAddr = new Ip4Address("0.0.0.0");
ip4Address = new Ip4Address(fromAddr);
assertThat(ip4Address.toString(), is("0.0.0.0"));
fromAddr = new Ip4Address("255.255.255.255");
ip4Address = new Ip4Address(fromAddr);
assertThat(ip4Address.toString(), is("255.255.255.255"));
public void testAddressToOctetsIPv4() {
Ip4Address ipAddress;
byte[] value;
value = new byte[] {1, 2, 3, 4};
ipAddress = Ip4Address.valueOf("1.2.3.4");
assertThat(ipAddress.toOctets(), is(value));
value = new byte[] {0, 0, 0, 0};
ipAddress = Ip4Address.valueOf("0.0.0.0");
assertThat(ipAddress.toOctets(), is(value));
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress = Ip4Address.valueOf("255.255.255.255");
assertThat(ipAddress.toOctets(), is(value));
}
/**
* Tests invalid class copy constructor for a null object to copy from.
* Tests returning an IPv4 address as an integer.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullObject() {
Ip4Address fromAddr = null;
Ip4Address ip4Address = new Ip4Address(fromAddr);
@Test
public void testToInt() {
Ip4Address ipAddress;
ipAddress = Ip4Address.valueOf("1.2.3.4");
assertThat(ipAddress.toInt(), is(0x01020304));
ipAddress = Ip4Address.valueOf("0.0.0.0");
assertThat(ipAddress.toInt(), is(0));
ipAddress = Ip4Address.valueOf("255.255.255.255");
assertThat(ipAddress.toInt(), is(-1));
}
/**
* Tests valid class constructor for an integer value.
* Tests valueOf() converter for IPv4 integer value.
*/
@Test
public void testConstructorForInteger() {
Ip4Address ip4Address = new Ip4Address(0x01020304);
assertThat(ip4Address.toString(), is("1.2.3.4"));
public void testValueOfForIntegerIPv4() {
Ip4Address ipAddress;
ip4Address = new Ip4Address(0);
assertThat(ip4Address.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.valueOf(0x01020304);
assertThat(ipAddress.toString(), is("1.2.3.4"));
ip4Address = new Ip4Address(0xffffffff);
assertThat(ip4Address.toString(), is("255.255.255.255"));
ipAddress = Ip4Address.valueOf(0);
assertThat(ipAddress.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.valueOf(0xffffffff);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valid class constructor for an array value.
* Tests valueOf() converter for IPv4 byte array.
*/
@Test
public void testConstructorForArray() {
public void testValueOfByteArrayIPv4() {
Ip4Address ipAddress;
final byte[] value1 = new byte[] {1, 2, 3, 4};
Ip4Address ip4Address = new Ip4Address(value1);
assertThat(ip4Address.toString(), is("1.2.3.4"));
ipAddress = Ip4Address.valueOf(value1);
assertThat(ipAddress.toString(), is("1.2.3.4"));
final byte[] value2 = new byte[] {0, 0, 0, 0};
ip4Address = new Ip4Address(value2);
assertThat(ip4Address.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.valueOf(value2);
assertThat(ipAddress.toString(), is("0.0.0.0"));
final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ip4Address = new Ip4Address(value3);
assertThat(ip4Address.toString(), is("255.255.255.255"));
ipAddress = Ip4Address.valueOf(value3);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valid class constructor for an array value and an offset.
* Tests invalid valueOf() converter for a null array for IPv4.
*/
@Test(expected = NullPointerException.class)
public void testInvalidValueOfNullArrayIPv4() {
Ip4Address ipAddress;
final byte[] fromArray = null;
ipAddress = Ip4Address.valueOf(fromArray);
}
/**
* Tests invalid valueOf() converger for an array that is too short for
* IPv4.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfShortArrayIPv4() {
Ip4Address ipAddress;
final byte[] fromArray = new byte[] {1, 2, 3};
ipAddress = Ip4Address.valueOf(fromArray);
}
/**
* Tests valueOf() converter for IPv4 byte array and an offset.
*/
@Test
public void testConstructorForArrayAndOffset() {
final byte[] value1 = new byte[] {11, 22, 33, // Preamble
public void testValueOfByteArrayOffsetIPv4() {
Ip4Address ipAddress;
byte[] value;
value = new byte[] {11, 22, 33, // Preamble
1, 2, 3, 4,
44, 55}; // Extra bytes
Ip4Address ip4Address = new Ip4Address(value1, 3);
assertThat(ip4Address.toString(), is("1.2.3.4"));
ipAddress = Ip4Address.valueOf(value, 3);
assertThat(ipAddress.toString(), is("1.2.3.4"));
final byte[] value2 = new byte[] {11, 22, // Preamble
value = new byte[] {11, 22, // Preamble
0, 0, 0, 0,
33}; // Extra bytes
ip4Address = new Ip4Address(value2, 2);
assertThat(ip4Address.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.valueOf(value, 2);
assertThat(ipAddress.toString(), is("0.0.0.0"));
final byte[] value3 = new byte[] {11, 22, // Preamble
value = new byte[] {11, 22, // Preamble
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
33}; // Extra bytes
ip4Address = new Ip4Address(value3, 2);
assertThat(ip4Address.toString(), is("255.255.255.255"));
}
/**
* Tests invalid class constructor for a null array.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullArray() {
final byte[] fromArray = null;
Ip4Address ip4Address = new Ip4Address(fromArray);
ipAddress = Ip4Address.valueOf(value, 2);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests invalid class constructor for an array that is too short.
* Tests invalid valueOf() converger for an array and an invalid offset
* for IPv4.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidConstructorShortArray() {
final byte[] fromArray = new byte[] {1, 2, 3};
Ip4Address ip4Address = new Ip4Address(fromArray);
public void testInvalidValueOfArrayInvalidOffsetIPv4() {
Ip4Address ipAddress;
byte[] value;
value = new byte[] {11, 22, 33, // Preamble
1, 2, 3, 4,
44, 55}; // Extra bytes
ipAddress = Ip4Address.valueOf(value, 6);
}
/**
* Tests invalid class constructor for an array and an invalid offset.
* Tests valueOf() converter for IPv4 InetAddress.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidConstructorArrayInvalidOffset() {
final byte[] value1 = new byte[] {11, 22, 33, // Preamble
1, 2, 3, 4,
44, 55}; // Extra bytes
Ip4Address ip4Address = new Ip4Address(value1, 6);
@Test
public void testValueOfInetAddressIPv4() {
Ip4Address ipAddress;
InetAddress inetAddress;
inetAddress = InetAddresses.forString("1.2.3.4");
ipAddress = Ip4Address.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("1.2.3.4"));
inetAddress = InetAddresses.forString("0.0.0.0");
ipAddress = Ip4Address.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("0.0.0.0"));
inetAddress = InetAddresses.forString("255.255.255.255");
ipAddress = Ip4Address.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valid class constructor for a string.
* Tests valueOf() converter for IPv4 string.
*/
@Test
public void testConstructorForString() {
Ip4Address ip4Address = new Ip4Address("1.2.3.4");
assertThat(ip4Address.toString(), is("1.2.3.4"));
public void testValueOfStringIPv4() {
Ip4Address ipAddress;
ip4Address = new Ip4Address("0.0.0.0");
assertThat(ip4Address.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.valueOf("1.2.3.4");
assertThat(ipAddress.toString(), is("1.2.3.4"));
ip4Address = new Ip4Address("255.255.255.255");
assertThat(ip4Address.toString(), is("255.255.255.255"));
ipAddress = Ip4Address.valueOf("0.0.0.0");
assertThat(ipAddress.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.valueOf("255.255.255.255");
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests invalid class constructor for a null string.
* Tests invalid valueOf() converter for a null string.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullString() {
public void testInvalidValueOfNullString() {
Ip4Address ipAddress;
String fromString = null;
Ip4Address ip4Address = new Ip4Address(fromString);
ipAddress = Ip4Address.valueOf(fromString);
}
/**
* Tests invalid class constructor for an empty string.
* Tests invalid valueOf() converter for an empty string.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidConstructors() {
// Check constructor for invalid ID: empty string
Ip4Address ip4Address = new Ip4Address("");
public void testInvalidValueOfEmptyString() {
Ip4Address ipAddress;
String fromString = "";
ipAddress = Ip4Address.valueOf(fromString);
}
/**
* Tests returning the address as a byte array.
* Tests invalid valueOf() converter for an incorrect string.
*/
@Test
public void testAddressToOctets() {
final byte[] value1 = new byte[] {1, 2, 3, 4};
Ip4Address ip4Address = new Ip4Address("1.2.3.4");
assertThat(ip4Address.toOctets(), is(value1));
final byte[] value2 = new byte[] {0, 0, 0, 0};
ip4Address = new Ip4Address("0.0.0.0");
assertThat(ip4Address.toOctets(), is(value2));
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfIncorrectString() {
Ip4Address ipAddress;
final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ip4Address = new Ip4Address("255.255.255.255");
assertThat(ip4Address.toOctets(), is(value3));
String fromString = "NoSuchIpAddress";
ipAddress = Ip4Address.valueOf(fromString);
}
/**
* Tests making a mask prefix for a given prefix length.
* Tests making a mask prefix for a given prefix length for IPv4.
*/
@Test
public void testMakeMaskPrefix() {
Ip4Address ip4Address = Ip4Address.makeMaskPrefix(25);
assertThat(ip4Address.toString(), is("255.255.255.128"));
public void testMakeMaskPrefixIPv4() {
Ip4Address ipAddress;
ip4Address = Ip4Address.makeMaskPrefix(0);
assertThat(ip4Address.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.makeMaskPrefix(25);
assertThat(ipAddress.toString(), is("255.255.255.128"));
ip4Address = Ip4Address.makeMaskPrefix(32);
assertThat(ip4Address.toString(), is("255.255.255.255"));
ipAddress = Ip4Address.makeMaskPrefix(0);
assertThat(ipAddress.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.makeMaskPrefix(32);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests making of a masked address.
* Tests making a mask prefix for an invalid prefix length for IPv4:
* negative prefix length.
*/
@Test
public void testMakeMaskedAddress() {
Ip4Address ip4Address = new Ip4Address("1.2.3.5");
Ip4Address ip4AddressMasked =
Ip4Address.makeMaskedAddress(ip4Address, 24);
assertThat(ip4AddressMasked.toString(), is("1.2.3.0"));
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeNegativeMaskPrefixIPv4() {
Ip4Address ipAddress;
ipAddress = Ip4Address.makeMaskPrefix(-1);
}
ip4AddressMasked = Ip4Address.makeMaskedAddress(ip4Address, 0);
assertThat(ip4AddressMasked.toString(), is("0.0.0.0"));
/**
* Tests making a mask prefix for an invalid prefix length for IPv4:
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeTooLongMaskPrefixIPv4() {
Ip4Address ipAddress;
ip4AddressMasked = Ip4Address.makeMaskedAddress(ip4Address, 32);
assertThat(ip4AddressMasked.toString(), is("1.2.3.5"));
ipAddress = Ip4Address.makeMaskPrefix(33);
}
/**
* Tests getting the value of an address.
* Tests making of a masked address for IPv4.
*/
@Test
public void testGetValue() {
Ip4Address ip4Address = new Ip4Address("1.2.3.4");
assertThat(ip4Address.getValue(), is(0x01020304));
public void testMakeMaskedAddressIPv4() {
Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5");
Ip4Address ipAddressMasked;
ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 24);
assertThat(ipAddressMasked.toString(), is("1.2.3.0"));
ip4Address = new Ip4Address("0.0.0.0");
assertThat(ip4Address.getValue(), is(0));
ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 0);
assertThat(ipAddressMasked.toString(), is("0.0.0.0"));
ip4Address = new Ip4Address("255.255.255.255");
assertThat(ip4Address.getValue(), is(-1));
ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 32);
assertThat(ipAddressMasked.toString(), is("1.2.3.5"));
}
/**
* Tests equality of {@link Ip4Address}.
* Tests making of a masked address for invalid prefix length for IPv4:
* negative prefix length.
*/
@Test
public void testEquality() {
Ip4Address addr1 = new Ip4Address("1.2.3.4");
Ip4Address addr2 = new Ip4Address("1.2.3.4");
assertThat(addr1, is(addr2));
addr1 = new Ip4Address("0.0.0.0");
addr2 = new Ip4Address("0.0.0.0");
assertThat(addr1, is(addr2));
addr1 = new Ip4Address("255.255.255.255");
addr2 = new Ip4Address("255.255.255.255");
assertThat(addr1, is(addr2));
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeNegativeMaskedAddressIPv4() {
Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5");
Ip4Address ipAddressMasked;
ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, -1);
}
/**
* Tests non-equality of {@link Ip4Address}.
* Tests making of a masked address for an invalid prefix length for IPv4:
* too long prefix length.
*/
@Test
public void testNonEquality() {
Ip4Address addr1 = new Ip4Address("1.2.3.4");
Ip4Address addr2 = new Ip4Address("1.2.3.5");
Ip4Address addr3 = new Ip4Address("0.0.0.0");
Ip4Address addr4 = new Ip4Address("255.255.255.255");
assertThat(addr1, is(not(addr2)));
assertThat(addr3, is(not(addr2)));
assertThat(addr4, is(not(addr2)));
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeTooLongMaskedAddressIPv4() {
Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5");
Ip4Address ipAddressMasked;
ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 33);
}
/**
* Tests comparison of {@link Ip4Address}.
* Tests comparison of {@link Ip4Address} for IPv4.
*/
@Test
public void testComparison() {
Ip4Address addr1 = new Ip4Address("1.2.3.4");
Ip4Address addr2 = new Ip4Address("1.2.3.4");
Ip4Address addr3 = new Ip4Address("1.2.3.3");
Ip4Address addr4 = new Ip4Address("1.2.3.5");
public void testComparisonIPv4() {
Ip4Address addr1, addr2, addr3, addr4;
addr1 = Ip4Address.valueOf("1.2.3.4");
addr2 = Ip4Address.valueOf("1.2.3.4");
addr3 = Ip4Address.valueOf("1.2.3.3");
addr4 = Ip4Address.valueOf("1.2.3.5");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
addr1 = new Ip4Address("255.2.3.4");
addr2 = new Ip4Address("255.2.3.4");
addr3 = new Ip4Address("255.2.3.3");
addr4 = new Ip4Address("255.2.3.5");
addr1 = Ip4Address.valueOf("255.2.3.4");
addr2 = Ip4Address.valueOf("255.2.3.4");
addr3 = Ip4Address.valueOf("255.2.3.3");
addr4 = Ip4Address.valueOf("255.2.3.5");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
}
/**
* Tests object string representation.
* Tests equality of {@link Ip4Address} for IPv4.
*/
@Test
public void testEqualityIPv4() {
new EqualsTester()
.addEqualityGroup(Ip4Address.valueOf("1.2.3.4"),
Ip4Address.valueOf("1.2.3.4"))
.addEqualityGroup(Ip4Address.valueOf("1.2.3.5"),
Ip4Address.valueOf("1.2.3.5"))
.addEqualityGroup(Ip4Address.valueOf("0.0.0.0"),
Ip4Address.valueOf("0.0.0.0"))
.addEqualityGroup(Ip4Address.valueOf("255.255.255.255"),
Ip4Address.valueOf("255.255.255.255"))
.testEquals();
}
/**
* Tests object string representation for IPv4.
*/
@Test
public void testToString() {
Ip4Address ip4Address = new Ip4Address("1.2.3.4");
assertThat(ip4Address.toString(), is("1.2.3.4"));
public void testToStringIPv4() {
Ip4Address ipAddress;
ipAddress = Ip4Address.valueOf("1.2.3.4");
assertThat(ipAddress.toString(), is("1.2.3.4"));
ip4Address = new Ip4Address("0.0.0.0");
assertThat(ip4Address.toString(), is("0.0.0.0"));
ipAddress = Ip4Address.valueOf("0.0.0.0");
assertThat(ipAddress.toString(), is("0.0.0.0"));
ip4Address = new Ip4Address("255.255.255.255");
assertThat(ip4Address.toString(), is("255.255.255.255"));
ipAddress = Ip4Address.valueOf("255.255.255.255");
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
}
......
......@@ -15,12 +15,14 @@
*/
package org.onlab.packet;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
......@@ -36,175 +38,497 @@ public class Ip4PrefixTest {
}
/**
* Tests default class constructor.
* Tests the IPv4 prefix address version constant.
*/
@Test
public void testDefaultConstructor() {
Ip4Prefix ip4prefix = new Ip4Prefix();
assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
public void testAddressVersion() {
assertThat(Ip4Prefix.VERSION, is(IpAddress.Version.INET));
}
/**
* Tests valid class copy constructor.
* Tests the maximum mask length.
*/
@Test
public void testCopyConstructor() {
Ip4Prefix fromAddr = new Ip4Prefix("1.2.3.0/24");
Ip4Prefix ip4prefix = new Ip4Prefix(fromAddr);
assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
public void testMaxMaskLength() {
assertThat(Ip4Prefix.MAX_MASK_LENGTH, is(32));
}
fromAddr = new Ip4Prefix("0.0.0.0/0");
ip4prefix = new Ip4Prefix(fromAddr);
assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
/**
* Tests returning the IP version of the prefix.
*/
@Test
public void testVersion() {
Ip4Prefix ipPrefix;
fromAddr = new Ip4Prefix("255.255.255.255/32");
ip4prefix = new Ip4Prefix(fromAddr);
assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
// IPv4
ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
assertThat(ipPrefix.version(), is(IpAddress.Version.INET));
}
/**
* Tests invalid class copy constructor for a null object to copy from.
* Tests returning the IP address value and IP address prefix length of
* an IPv4 prefix.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullObject() {
Ip4Prefix fromAddr = null;
Ip4Prefix ip4prefix = new Ip4Prefix(fromAddr);
@Test
public void testAddressAndPrefixLengthIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0")));
assertThat(ipPrefix.prefixLength(), is(24));
ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0")));
assertThat(ipPrefix.prefixLength(), is(24));
ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32");
assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.4")));
assertThat(ipPrefix.prefixLength(), is(32));
ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32");
assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.5")));
assertThat(ipPrefix.prefixLength(), is(32));
ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("0.0.0.0")));
assertThat(ipPrefix.prefixLength(), is(0));
ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
assertThat(ipPrefix.address(),
equalTo(Ip4Address.valueOf("255.255.255.255")));
assertThat(ipPrefix.prefixLength(), is(32));
}
/**
* Tests valid class constructor for an address and prefix length.
* Tests valueOf() converter for IPv4 integer value.
*/
@Test
public void testConstructorForAddressAndPrefixLength() {
Ip4Prefix ip4prefix =
new Ip4Prefix(new Ip4Address("1.2.3.0"), (short) 24);
assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
public void testValueOfForIntegerIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf(0x01020304, 24);
assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
ipPrefix = Ip4Prefix.valueOf(0x01020304, 32);
assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
ipPrefix = Ip4Prefix.valueOf(0x01020305, 32);
assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
ip4prefix = new Ip4Prefix(new Ip4Address("1.2.3.4"), (short) 24);
assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
ipPrefix = Ip4Prefix.valueOf(0, 0);
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ip4prefix = new Ip4Prefix(new Ip4Address("1.2.3.5"), (short) 32);
assertThat(ip4prefix.toString(), is("1.2.3.5/32"));
ipPrefix = Ip4Prefix.valueOf(0, 32);
assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
ip4prefix = new Ip4Prefix(new Ip4Address("0.0.0.0"), (short) 0);
assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
ipPrefix = Ip4Prefix.valueOf(0xffffffff, 0);
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ip4prefix =
new Ip4Prefix(new Ip4Address("255.255.255.255"), (short) 32);
assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
ipPrefix = Ip4Prefix.valueOf(0xffffffff, 16);
assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
ipPrefix = Ip4Prefix.valueOf(0xffffffff, 32);
assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
}
/**
* Tests invalid valueOf() converter for IPv4 integer value and
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf(0x01020304, -1);
}
/**
* Tests valid class constructor for a string.
* Tests invalid valueOf() converter for IPv4 integer value and
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf(0x01020304, 33);
}
/**
* Tests valueOf() converter for IPv4 byte array.
*/
@Test
public void testConstructorForString() {
Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
public void testValueOfByteArrayIPv4() {
Ip4Prefix ipPrefix;
byte[] value;
value = new byte[] {1, 2, 3, 4};
ipPrefix = Ip4Prefix.valueOf(value, 24);
assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
ip4prefix = new Ip4Prefix("1.2.3.4/24");
assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
ipPrefix = Ip4Prefix.valueOf(value, 32);
assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
ip4prefix = new Ip4Prefix("1.2.3.5/32");
assertThat(ip4prefix.toString(), is("1.2.3.5/32"));
value = new byte[] {1, 2, 3, 5};
ipPrefix = Ip4Prefix.valueOf(value, 32);
assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
ip4prefix = new Ip4Prefix("0.0.0.0/0");
assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
value = new byte[] {0, 0, 0, 0};
ipPrefix = Ip4Prefix.valueOf(value, 0);
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ip4prefix = new Ip4Prefix("255.255.255.255/32");
assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
ipPrefix = Ip4Prefix.valueOf(value, 32);
assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipPrefix = Ip4Prefix.valueOf(value, 0);
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ipPrefix = Ip4Prefix.valueOf(value, 16);
assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
ipPrefix = Ip4Prefix.valueOf(value, 32);
assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
}
/**
* Tests invalid class constructor for a null string.
* Tests invalid valueOf() converter for a null array for IPv4.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullString() {
String fromString = null;
Ip4Prefix ip4prefix = new Ip4Prefix(fromString);
public void testInvalidValueOfNullArrayIPv4() {
Ip4Prefix ipPrefix;
byte[] value;
value = null;
ipPrefix = Ip4Prefix.valueOf(value, 24);
}
/**
* Tests invalid class constructor for an empty string.
* Tests invalid valueOf() converter for a short array for IPv4.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidConstructors() {
// Check constructor for invalid ID: empty string
Ip4Prefix ip4prefix = new Ip4Prefix("");
public void testInvalidValueOfShortArrayIPv4() {
Ip4Prefix ipPrefix;
byte[] value;
value = new byte[] {1, 2, 3};
ipPrefix = Ip4Prefix.valueOf(value, 24);
}
/**
* Tests getting the value of an address.
* Tests invalid valueOf() converter for IPv4 byte array and
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() {
Ip4Prefix ipPrefix;
byte[] value;
value = new byte[] {1, 2, 3, 4};
ipPrefix = Ip4Prefix.valueOf(value, -1);
}
/**
* Tests invalid valueOf() converter for IPv4 byte array and
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() {
Ip4Prefix ipPrefix;
byte[] value;
value = new byte[] {1, 2, 3, 4};
ipPrefix = Ip4Prefix.valueOf(value, 33);
}
/**
* Tests valueOf() converter for IPv4 address.
*/
@Test
public void testGetValue() {
Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
assertThat(ip4prefix.getAddress(), equalTo(new Ip4Address("1.2.3.0")));
assertThat(ip4prefix.getPrefixLen(), is((short) 24));
public void testValueOfAddressIPv4() {
Ip4Address ipAddress;
Ip4Prefix ipPrefix;
ipAddress = Ip4Address.valueOf("1.2.3.4");
ipPrefix = Ip4Prefix.valueOf(ipAddress, 24);
assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
ipAddress = Ip4Address.valueOf("1.2.3.5");
ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
ipAddress = Ip4Address.valueOf("0.0.0.0");
ipPrefix = Ip4Prefix.valueOf(ipAddress, 0);
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
ipAddress = Ip4Address.valueOf("255.255.255.255");
ipPrefix = Ip4Prefix.valueOf(ipAddress, 0);
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ipPrefix = Ip4Prefix.valueOf(ipAddress, 16);
assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
ipPrefix = Ip4Prefix.valueOf(ipAddress, 32);
assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
}
/**
* Tests invalid valueOf() converter for a null IP address.
*/
@Test(expected = NullPointerException.class)
public void testInvalidValueOfNullAddress() {
Ip4Address ipAddress;
Ip4Prefix ipPrefix;
ipAddress = null;
ipPrefix = Ip4Prefix.valueOf(ipAddress, 24);
}
ip4prefix = new Ip4Prefix("0.0.0.0/0");
assertThat(ip4prefix.getAddress(), equalTo(new Ip4Address("0.0.0.0")));
assertThat(ip4prefix.getPrefixLen(), is((short) 0));
/**
* Tests invalid valueOf() converter for IPv4 address and
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfAddressNegativePrefixLengthIPv4() {
Ip4Address ipAddress;
Ip4Prefix ipPrefix;
ip4prefix = new Ip4Prefix("255.255.255.255/32");
assertThat(ip4prefix.getAddress(),
equalTo(new Ip4Address("255.255.255.255")));
assertThat(ip4prefix.getPrefixLen(), is((short) 32));
ipAddress = Ip4Address.valueOf("1.2.3.4");
ipPrefix = Ip4Prefix.valueOf(ipAddress, -1);
}
/**
* Tests equality of {@link Ip4Address}.
* Tests invalid valueOf() converter for IPv4 address and
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() {
Ip4Address ipAddress;
Ip4Prefix ipPrefix;
ipAddress = Ip4Address.valueOf("1.2.3.4");
ipPrefix = Ip4Prefix.valueOf(ipAddress, 33);
}
/**
* Tests valueOf() converter for IPv4 string.
*/
@Test
public void testEquality() {
Ip4Prefix addr1net = new Ip4Prefix("1.2.3.0/24");
Ip4Prefix addr2net = new Ip4Prefix("1.2.3.0/24");
assertThat(addr1net, is(addr2net));
public void testValueOfStringIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32");
assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32");
assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ipPrefix = Ip4Prefix.valueOf("0.0.0.0/32");
assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
ipPrefix = Ip4Prefix.valueOf("255.255.255.255/0");
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ipPrefix = Ip4Prefix.valueOf("255.255.255.255/16");
assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
}
/**
* Tests invalid valueOf() converter for a null string.
*/
@Test(expected = NullPointerException.class)
public void testInvalidValueOfNullString() {
Ip4Prefix ipPrefix;
String fromString;
fromString = null;
ipPrefix = Ip4Prefix.valueOf(fromString);
}
/**
* Tests invalid valueOf() converter for an empty string.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfEmptyString() {
Ip4Prefix ipPrefix;
String fromString;
addr1net = new Ip4Prefix("1.2.3.0/24");
addr2net = new Ip4Prefix("1.2.3.4/24");
assertThat(addr1net, is(addr2net));
fromString = "";
ipPrefix = Ip4Prefix.valueOf(fromString);
}
addr1net = new Ip4Prefix("0.0.0.0/0");
addr2net = new Ip4Prefix("0.0.0.0/0");
assertThat(addr1net, is(addr2net));
/**
* Tests invalid valueOf() converter for an incorrect string.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfIncorrectString() {
Ip4Prefix ipPrefix;
String fromString;
addr1net = new Ip4Prefix("255.255.255.255/32");
addr2net = new Ip4Prefix("255.255.255.255/32");
assertThat(addr1net, is(addr2net));
fromString = "NoSuchIpPrefix";
ipPrefix = Ip4Prefix.valueOf(fromString);
}
/**
* Tests non-equality of {@link Ip4Address}.
* Tests invalid valueOf() converter for IPv4 string and
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfStringNegativePrefixLengthIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf("1.2.3.4/-1");
}
/**
* Tests invalid valueOf() converter for IPv4 string and
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfStringTooLongPrefixLengthIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf("1.2.3.4/33");
}
/**
* Tests IP prefix contains another IP prefix for IPv4.
*/
@Test
public void testContainsIpPrefixIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24");
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32");
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16")));
assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0")));
assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32")));
}
/**
* Tests IP prefix contains IP address for IPv4.
*/
@Test
public void testNonEquality() {
Ip4Prefix addr1net = new Ip4Prefix("1.2.0.0/16");
Ip4Prefix addr2net = new Ip4Prefix("1.3.0.0/16");
Ip4Prefix addr3net = new Ip4Prefix("1.3.0.0/24");
Ip4Prefix addr4net = new Ip4Prefix("0.0.0.0/0");
Ip4Prefix addr5net = new Ip4Prefix("255.255.255.255/32");
assertThat(addr1net, is(not(addr2net)));
assertThat(addr3net, is(not(addr2net)));
assertThat(addr4net, is(not(addr2net)));
assertThat(addr5net, is(not(addr2net)));
public void testContainsIpAddressIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24");
assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32");
assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
assertTrue(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0")));
assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0")));
assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255")));
}
/**
* Tests object string representation.
* Tests equality of {@link Ip4Prefix} for IPv4.
*/
@Test
public void testToString() {
Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
public void testEqualityIPv4() {
new EqualsTester()
.addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/24"),
Ip4Prefix.valueOf("1.2.0.0/24"),
Ip4Prefix.valueOf("1.2.0.4/24"))
.addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/16"),
Ip4Prefix.valueOf("1.2.0.0/16"))
.addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/32"),
Ip4Prefix.valueOf("1.2.0.0/32"))
.addEqualityGroup(Ip4Prefix.valueOf("1.3.0.0/24"),
Ip4Prefix.valueOf("1.3.0.0/24"))
.addEqualityGroup(Ip4Prefix.valueOf("0.0.0.0/0"),
Ip4Prefix.valueOf("0.0.0.0/0"))
.addEqualityGroup(Ip4Prefix.valueOf("255.255.255.255/32"),
Ip4Prefix.valueOf("255.255.255.255/32"))
.testEquals();
}
/**
* Tests object string representation for IPv4.
*/
@Test
public void testToStringIPv4() {
Ip4Prefix ipPrefix;
ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24");
assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
ip4prefix = new Ip4Prefix("1.2.3.4/24");
assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24");
assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
ip4prefix = new Ip4Prefix("0.0.0.0/0");
assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0");
assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
ip4prefix = new Ip4Prefix("255.255.255.255/32");
assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32");
assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
}
}
......
......@@ -15,10 +15,13 @@
*/
package org.onlab.packet;
import com.google.common.net.InetAddresses;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import java.net.InetAddress;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
......@@ -36,100 +39,103 @@ public class Ip6AddressTest {
}
/**
* Tests the length of the address in bytes (octets).
* Tests the IPv4 address version constant.
*/
@Test
public void testAddrBytelen() {
assertThat(Ip6Address.BYTE_LENGTH, is(16));
public void testAddressVersion() {
assertThat(Ip6Address.VERSION, is(IpAddress.Version.INET6));
}
/**
* Tests the length of the address in bits.
* Tests the length of the address in bytes (octets).
*/
@Test
public void testAddrBitlen() {
assertThat(Ip6Address.BIT_LENGTH, is(128));
public void testAddrByteLength() {
assertThat(Ip6Address.BYTE_LENGTH, is(16));
}
/**
* Tests default class constructor.
* Tests the length of the address in bits.
*/
@Test
public void testDefaultConstructor() {
Ip6Address ip6Address = new Ip6Address();
assertThat(ip6Address.toString(), is("::"));
public void testAddrBitLength() {
assertThat(Ip6Address.BIT_LENGTH, is(128));
}
/**
* Tests valid class copy constructor.
* Tests returning the IP address version.
*/
@Test
public void testCopyConstructor() {
Ip6Address fromAddr =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
Ip6Address ip6Address = new Ip6Address(fromAddr);
assertThat(ip6Address.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
public void testVersion() {
IpAddress ipAddress;
fromAddr = new Ip6Address("::");
ip6Address = new Ip6Address(fromAddr);
assertThat(ip6Address.toString(), is("::"));
fromAddr = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
ip6Address = new Ip6Address(fromAddr);
assertThat(ip6Address.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests invalid class copy constructor for a null object to copy from.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullObject() {
Ip6Address fromAddr = null;
Ip6Address ip6Address = new Ip6Address(fromAddr);
// IPv6
ipAddress = IpAddress.valueOf("::");
assertThat(ipAddress.version(), is(IpAddress.Version.INET6));
}
/**
* Tests valid class constructor for integer values.
* Tests returning an IPv6 address as a byte array.
*/
@Test
public void testConstructorForInteger() {
Ip6Address ip6Address =
new Ip6Address(0x1111222233334444L, 0x5555666677778888L);
assertThat(ip6Address.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
public void testAddressToOctetsIPv6() {
Ip6Address ipAddress;
byte[] value;
ip6Address = new Ip6Address(0L, 0L);
assertThat(ip6Address.toString(), is("::"));
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88};
ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ipAddress.toOctets(), is(value));
ip6Address = new Ip6Address(-1L, -1L);
assertThat(ip6Address.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
value = new byte[] {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
ipAddress = Ip6Address.valueOf("::");
assertThat(ipAddress.toOctets(), is(value));
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress =
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ipAddress.toOctets(), is(value));
}
/**
* Tests valid class constructor for an array value.
* Tests valueOf() converter for IPv6 byte array.
*/
@Test
public void testConstructorForArray() {
final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22,
public void testValueOfByteArrayIPv6() {
Ip6Address ipAddress;
byte[] value;
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88};
Ip6Address ip6Address = new Ip6Address(value1);
assertThat(ip6Address.toString(),
ipAddress = Ip6Address.valueOf(value);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00,
value = new byte[] {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
ip6Address = new Ip6Address(value2);
assertThat(ip6Address.toString(), is("::"));
ipAddress = Ip6Address.valueOf(value);
assertThat(ipAddress.toString(), is("::"));
final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
......@@ -137,37 +143,63 @@ public class Ip6AddressTest {
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ip6Address = new Ip6Address(value3);
assertThat(ip6Address.toString(),
ipAddress = Ip6Address.valueOf(value);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests valid class constructor for an array value and an offset.
* Tests invalid valueOf() converter for a null array for IPv6.
*/
@Test(expected = NullPointerException.class)
public void testInvalidValueOfNullArrayIPv6() {
Ip6Address ipAddress;
final byte[] fromArray = null;
ipAddress = Ip6Address.valueOf(fromArray);
}
/**
* Tests invalid valueOf() converger for an array that is too short for
* IPv6.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfShortArrayIPv6() {
Ip6Address ipAddress;
final byte[] fromArray = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
ipAddress = Ip6Address.valueOf(fromArray);
}
/**
* Tests valueOf() converter for IPv6 byte array and an offset.
*/
@Test
public void testConstructorForArrayAndOffset() {
final byte[] value1 = new byte[] {11, 22, 33, // Preamble
public void testValueOfByteArrayOffsetIPv6() {
Ip6Address ipAddress;
byte[] value;
value = new byte[] {11, 22, 33, // Preamble
0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88,
44, 55}; // Extra bytes
Ip6Address ip6Address = new Ip6Address(value1, 3);
assertThat(ip6Address.toString(),
ipAddress = Ip6Address.valueOf(value, 3);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
final byte[] value2 = new byte[] {11, 22, // Preamble
value = new byte[] {11, 22, // Preamble
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
33}; // Extra bytes
ip6Address = new Ip6Address(value2, 2);
assertThat(ip6Address.toString(), is("::"));
ipAddress = Ip6Address.valueOf(value, 2);
assertThat(ipAddress.toString(), is("::"));
final byte[] value3 = new byte[] {11, 22, // Preamble
value = new byte[] {11, 22, // Preamble
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
......@@ -177,276 +209,289 @@ public class Ip6AddressTest {
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
33}; // Extra bytes
ip6Address = new Ip6Address(value3, 2);
assertThat(ip6Address.toString(),
ipAddress = Ip6Address.valueOf(value, 2);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests invalid class constructor for a null array.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullArray() {
final byte[] fromArray = null;
Ip6Address ip6Address = new Ip6Address(fromArray);
}
/**
* Tests invalid class constructor for an array that is too short.
* Tests invalid valueOf() converger for an array and an invalid offset
* for IPv6.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidConstructorShortArray() {
final byte[] fromArray = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
Ip6Address ip6Address = new Ip6Address(fromArray);
}
public void testInvalidValueOfArrayInvalidOffsetIPv6() {
Ip6Address ipAddress;
byte[] value;
/**
* Tests invalid class constructor for an array and an invalid offset.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidConstructorArrayInvalidOffset() {
final byte[] value1 = new byte[] {11, 22, 33, // Preamble
value = new byte[] {11, 22, 33, // Preamble
0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88,
44, 55}; // Extra bytes
Ip6Address ip6Address = new Ip6Address(value1, 6);
ipAddress = Ip6Address.valueOf(value, 6);
}
/**
* Tests valid class constructor for a string.
* Tests valueOf() converter for IPv6 InetAddress.
*/
@Test
public void testConstructorForString() {
Ip6Address ip6Address =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ip6Address.toString(),
public void testValueOfInetAddressIPv6() {
Ip6Address ipAddress;
InetAddress inetAddress;
inetAddress =
InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
ipAddress = Ip6Address.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
ip6Address = new Ip6Address("::");
assertThat(ip6Address.toString(), is("::"));
inetAddress = InetAddresses.forString("::");
ipAddress = Ip6Address.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("::"));
ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ip6Address.toString(),
inetAddress =
InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
ipAddress = Ip6Address.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests invalid class constructor for a null string.
* Tests valueOf() converter for IPv6 string.
*/
@Test
public void testValueOfStringIPv6() {
Ip6Address ipAddress;
ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
ipAddress = Ip6Address.valueOf("::");
assertThat(ipAddress.toString(), is("::"));
ipAddress =
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests invalid valueOf() converter for a null string.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullString() {
public void testInvalidValueOfNullString() {
Ip6Address ipAddress;
String fromString = null;
Ip6Address ip6Address = new Ip6Address(fromString);
ipAddress = Ip6Address.valueOf(fromString);
}
/**
* Tests invalid class constructor for an empty string.
* Tests invalid valueOf() converter for an empty string.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidConstructors() {
// Check constructor for invalid ID: empty string
Ip6Address ip6Address = new Ip6Address("");
public void testInvalidValueOfEmptyString() {
Ip6Address ipAddress;
String fromString = "";
ipAddress = Ip6Address.valueOf(fromString);
}
/**
* Tests returning the address as a byte array.
* Tests invalid valueOf() converter for an incorrect string.
*/
@Test
public void testAddressToOctets() {
final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88};
Ip6Address ip6Address =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ip6Address.toOctets(), is(value1));
final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
ip6Address = new Ip6Address("::");
assertThat(ip6Address.toOctets(), is(value2));
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfIncorrectString() {
Ip6Address ipAddress;
final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ip6Address.toOctets(), is(value3));
String fromString = "NoSuchIpAddress";
ipAddress = Ip6Address.valueOf(fromString);
}
/**
* Tests making a mask prefix for a given prefix length.
* Tests making a mask prefix for a given prefix length for IPv6.
*/
@Test
public void testMakeMaskPrefix() {
Ip6Address ip6Address = Ip6Address.makeMaskPrefix(8);
assertThat(ip6Address.toString(), is("ff00::"));
public void testMakeMaskPrefixIPv6() {
Ip6Address ipAddress;
ip6Address = Ip6Address.makeMaskPrefix(120);
assertThat(ip6Address.toString(),
ipAddress = Ip6Address.makeMaskPrefix(8);
assertThat(ipAddress.toString(), is("ff00::"));
ipAddress = Ip6Address.makeMaskPrefix(120);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00"));
ip6Address = Ip6Address.makeMaskPrefix(0);
assertThat(ip6Address.toString(), is("::"));
ipAddress = Ip6Address.makeMaskPrefix(0);
assertThat(ipAddress.toString(), is("::"));
ip6Address = Ip6Address.makeMaskPrefix(128);
assertThat(ip6Address.toString(),
ipAddress = Ip6Address.makeMaskPrefix(128);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
ip6Address = Ip6Address.makeMaskPrefix(64);
assertThat(ip6Address.toString(), is("ffff:ffff:ffff:ffff::"));
ipAddress = Ip6Address.makeMaskPrefix(64);
assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::"));
}
/**
* Tests making of a masked address.
* Tests making a mask prefix for an invalid prefix length for IPv6:
* negative prefix length.
*/
@Test
public void testMakeMaskedAddress() {
Ip6Address ip6Address =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885");
Ip6Address ip6AddressMasked =
Ip6Address.makeMaskedAddress(ip6Address, 8);
assertThat(ip6AddressMasked.toString(), is("1100::"));
ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 120);
assertThat(ip6AddressMasked.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8800"));
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeNegativeMaskPrefixIPv6() {
Ip6Address ipAddress;
ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 0);
assertThat(ip6AddressMasked.toString(), is("::"));
ipAddress = Ip6Address.makeMaskPrefix(-1);
}
ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 128);
assertThat(ip6AddressMasked.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8885"));
/**
* Tests making a mask prefix for an invalid prefix length for IPv6:
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeTooLongMaskPrefixIPv6() {
Ip6Address ipAddress;
ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 64);
assertThat(ip6AddressMasked.toString(), is("1111:2222:3333:4444::"));
ipAddress = Ip6Address.makeMaskPrefix(129);
}
/**
* Tests getting the value of an address.
* Tests making of a masked address for IPv6.
*/
@Test
public void testGetValue() {
Ip6Address ip6Address =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ip6Address.getValueHigh(), is(0x1111222233334444L));
assertThat(ip6Address.getValueLow(), is(0x5555666677778888L));
ip6Address = new Ip6Address(0, 0);
assertThat(ip6Address.getValueHigh(), is(0L));
assertThat(ip6Address.getValueLow(), is(0L));
ip6Address = new Ip6Address(-1L, -1L);
assertThat(ip6Address.getValueHigh(), is(-1L));
assertThat(ip6Address.getValueLow(), is(-1L));
public void testMakeMaskedAddressIPv6() {
Ip6Address ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
Ip6Address ipAddressMasked;
ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 8);
assertThat(ipAddressMasked.toString(), is("1100::"));
ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 120);
assertThat(ipAddressMasked.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8800"));
ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 0);
assertThat(ipAddressMasked.toString(), is("::"));
ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 128);
assertThat(ipAddressMasked.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8885"));
ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 64);
assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::"));
}
/**
* Tests equality of {@link Ip6Address}.
* Tests making of a masked address for invalid prefix length for IPv6:
* negative prefix length.
*/
@Test
public void testEquality() {
Ip6Address addr1 =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
Ip6Address addr2 =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(addr1, is(addr2));
addr1 = new Ip6Address("::");
addr2 = new Ip6Address("::");
assertThat(addr1, is(addr2));
addr1 = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
addr2 = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(addr1, is(addr2));
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeNegativeMaskedAddressIPv6() {
Ip6Address ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
Ip6Address ipAddressMasked;
ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, -1);
}
/**
* Tests non-equality of {@link Ip6Address}.
* Tests making of a masked address for an invalid prefix length for IPv6:
* too long prefix length.
*/
@Test
public void testNonEquality() {
Ip6Address addr1 =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
Ip6Address addr2 =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:888A");
Ip6Address addr3 = new Ip6Address("::");
Ip6Address addr4 =
new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(addr1, is(not(addr2)));
assertThat(addr3, is(not(addr2)));
assertThat(addr4, is(not(addr2)));
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeTooLongMaskedAddressIPv6() {
Ip6Address ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
Ip6Address ipAddressMasked;
ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 129);
}
/**
* Tests comparison of {@link Ip6Address}.
* Tests comparison of {@link Ip6Address} for IPv6.
*/
@Test
public void testComparison() {
Ip6Address addr1 =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
Ip6Address addr2 =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
Ip6Address addr3 =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8887");
Ip6Address addr4 =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8889");
public void testComparisonIPv6() {
Ip6Address addr1, addr2, addr3, addr4;
addr1 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
addr2 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
addr3 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8887");
addr4 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8889");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
addr1 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888");
addr2 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888");
addr3 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8887");
addr4 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8889");
addr1 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
addr2 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
addr3 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887");
addr4 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
addr1 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888");
addr2 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888");
addr3 = new Ip6Address("ffff:2222:3333:4443:5555:6666:7777:8888");
addr4 = new Ip6Address("ffff:2222:3333:4445:5555:6666:7777:8888");
addr1 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
addr2 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
addr3 = Ip6Address.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888");
addr4 = Ip6Address.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
}
/**
* Tests object string representation.
* Tests equality of {@link Ip6Address} for IPv6.
*/
@Test
public void testToString() {
Ip6Address ip6Address =
new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ip6Address.toString(),
public void testEqualityIPv6() {
new EqualsTester()
.addEqualityGroup(
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"),
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"))
.addEqualityGroup(
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"),
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"))
.addEqualityGroup(
Ip6Address.valueOf("::"),
Ip6Address.valueOf("::"))
.addEqualityGroup(
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))
.testEquals();
}
/**
* Tests object string representation for IPv6.
*/
@Test
public void testToStringIPv6() {
Ip6Address ipAddress;
ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
ip6Address = new Ip6Address("1111::8888");
assertThat(ip6Address.toString(), is("1111::8888"));
ipAddress = Ip6Address.valueOf("1111::8888");
assertThat(ipAddress.toString(), is("1111::8888"));
ip6Address = new Ip6Address("1111::");
assertThat(ip6Address.toString(), is("1111::"));
ipAddress = Ip6Address.valueOf("1111::");
assertThat(ipAddress.toString(), is("1111::"));
ip6Address = new Ip6Address("::8888");
assertThat(ip6Address.toString(), is("::8888"));
ipAddress = Ip6Address.valueOf("::8888");
assertThat(ipAddress.toString(), is("::8888"));
ip6Address = new Ip6Address("::");
assertThat(ip6Address.toString(), is("::"));
ipAddress = Ip6Address.valueOf("::");
assertThat(ipAddress.toString(), is("::"));
ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ip6Address.toString(),
ipAddress =
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
}
......
......@@ -15,12 +15,14 @@
*/
package org.onlab.packet;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
......@@ -36,235 +38,531 @@ public class Ip6PrefixTest {
}
/**
* Tests default class constructor.
* Tests the IPv4 prefix address version constant.
*/
@Test
public void testDefaultConstructor() {
Ip6Prefix ip6prefix = new Ip6Prefix();
assertThat(ip6prefix.toString(), is("::/0"));
public void testAddressVersion() {
assertThat(Ip6Prefix.VERSION, is(IpAddress.Version.INET6));
}
/**
* Tests valid class copy constructor.
* Tests the maximum mask length.
*/
@Test
public void testCopyConstructor() {
Ip6Prefix fromAddr = new Ip6Prefix("1100::/8");
Ip6Prefix ip6prefix = new Ip6Prefix(fromAddr);
assertThat(ip6prefix.toString(), is("1100::/8"));
fromAddr = new Ip6Prefix("::/0");
ip6prefix = new Ip6Prefix(fromAddr);
assertThat(ip6prefix.toString(), is("::/0"));
fromAddr =
new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
ip6prefix = new Ip6Prefix(fromAddr);
assertThat(ip6prefix.toString(),
public void testMaxMaskLength() {
assertThat(Ip6Prefix.MAX_MASK_LENGTH, is(128));
}
/**
* Tests returning the IP version of the prefix.
*/
@Test
public void testVersion() {
Ip6Prefix ipPrefix;
// IPv6
ipPrefix = Ip6Prefix.valueOf("::/0");
assertThat(ipPrefix.version(), is(IpAddress.Version.INET6));
}
/**
* Tests returning the IP address value and IP address prefix length of
* an IPv6 prefix.
*/
@Test
public void testAddressAndPrefixLengthIPv6() {
Ip6Prefix ipPrefix;
ipPrefix = Ip6Prefix.valueOf("1100::/8");
assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::")));
assertThat(ipPrefix.prefixLength(), is(8));
ipPrefix =
Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::")));
assertThat(ipPrefix.prefixLength(), is(8));
ipPrefix =
Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120");
assertThat(ipPrefix.address(),
equalTo(Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8800")));
assertThat(ipPrefix.prefixLength(), is(120));
ipPrefix =
Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128");
assertThat(ipPrefix.address(),
equalTo(Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")));
assertThat(ipPrefix.prefixLength(), is(128));
ipPrefix = Ip6Prefix.valueOf("::/0");
assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("::")));
assertThat(ipPrefix.prefixLength(), is(0));
ipPrefix =
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertThat(ipPrefix.address(),
equalTo(Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
assertThat(ipPrefix.prefixLength(), is(128));
ipPrefix =
Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64");
assertThat(ipPrefix.address(),
equalTo(Ip6Address.valueOf("1111:2222:3333:4444::")));
assertThat(ipPrefix.prefixLength(), is(64));
}
/**
* Tests valueOf() converter for IPv6 byte array.
*/
@Test
public void testValueOfByteArrayIPv6() {
Ip6Prefix ipPrefix;
byte[] value;
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77, (byte) 0x88, (byte) 0x88};
ipPrefix = Ip6Prefix.valueOf(value, 120);
assertThat(ipPrefix.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
ipPrefix = Ip6Prefix.valueOf(value, 128);
assertThat(ipPrefix.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
value = new byte[] {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
ipPrefix = Ip6Prefix.valueOf(value, 0);
assertThat(ipPrefix.toString(), is("::/0"));
ipPrefix = Ip6Prefix.valueOf(value, 128);
assertThat(ipPrefix.toString(), is("::/128"));
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipPrefix = Ip6Prefix.valueOf(value, 0);
assertThat(ipPrefix.toString(), is("::/0"));
ipPrefix = Ip6Prefix.valueOf(value, 64);
assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
ipPrefix = Ip6Prefix.valueOf(value, 128);
assertThat(ipPrefix.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
}
/**
* Tests invalid class copy constructor for a null object to copy from.
* Tests invalid valueOf() converter for a null array for IPv6.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullObject() {
Ip6Prefix fromAddr = null;
Ip6Prefix ip6prefix = new Ip6Prefix(fromAddr);
public void testInvalidValueOfNullArrayIPv6() {
Ip6Prefix ipPrefix;
byte[] value;
value = null;
ipPrefix = Ip6Prefix.valueOf(value, 120);
}
/**
* Tests valid class constructor for an address and prefix length.
* Tests invalid valueOf() converter for a short array for IPv6.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfShortArrayIPv6() {
Ip6Prefix ipPrefix;
byte[] value;
value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
ipPrefix = Ip6Prefix.valueOf(value, 120);
}
/**
* Tests invalid valueOf() converter for IPv6 byte array and
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() {
Ip6Prefix ipPrefix;
byte[] value;
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77, (byte) 0x88, (byte) 0x88};
ipPrefix = Ip6Prefix.valueOf(value, -1);
}
/**
* Tests invalid valueOf() converter for IPv6 byte array and
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() {
Ip6Prefix ipPrefix;
byte[] value;
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77, (byte) 0x88, (byte) 0x88};
ipPrefix = Ip6Prefix.valueOf(value, 129);
}
/**
* Tests valueOf() converter for IPv6 address.
*/
@Test
public void testConstructorForAddressAndPrefixLength() {
Ip6Prefix ip6prefix =
new Ip6Prefix(new Ip6Address("1100::"), (short) 8);
assertThat(ip6prefix.toString(), is("1100::/8"));
ip6prefix =
new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"),
(short) 8);
assertThat(ip6prefix.toString(), is("1100::/8"));
ip6prefix =
new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8800"),
(short) 120);
assertThat(ip6prefix.toString(),
public void testValueOfAddressIPv6() {
Ip6Address ipAddress;
Ip6Prefix ipPrefix;
ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
ipPrefix = Ip6Prefix.valueOf(ipAddress, 120);
assertThat(ipPrefix.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
ip6prefix = new Ip6Prefix(new Ip6Address("::"), (short) 0);
assertThat(ip6prefix.toString(), is("::/0"));
ipPrefix = Ip6Prefix.valueOf(ipAddress, 128);
assertThat(ipPrefix.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
ipAddress = Ip6Address.valueOf("::");
ipPrefix = Ip6Prefix.valueOf(ipAddress, 0);
assertThat(ipPrefix.toString(), is("::/0"));
ip6prefix =
new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"),
(short) 128);
assertThat(ip6prefix.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8885/128"));
ipPrefix = Ip6Prefix.valueOf(ipAddress, 128);
assertThat(ipPrefix.toString(), is("::/128"));
ip6prefix =
new Ip6Prefix(new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
(short) 128);
assertThat(ip6prefix.toString(),
ipAddress =
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
ipPrefix = Ip6Prefix.valueOf(ipAddress, 0);
assertThat(ipPrefix.toString(), is("::/0"));
ipPrefix = Ip6Prefix.valueOf(ipAddress, 64);
assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
ipPrefix = Ip6Prefix.valueOf(ipAddress, 128);
assertThat(ipPrefix.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
}
ip6prefix =
new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"),
(short) 64);
assertThat(ip6prefix.toString(), is("1111:2222:3333:4444::/64"));
/**
* Tests invalid valueOf() converter for a null IP address.
*/
@Test(expected = NullPointerException.class)
public void testInvalidValueOfNullAddress() {
Ip6Address ipAddress;
Ip6Prefix ipPrefix;
ipAddress = null;
ipPrefix = Ip6Prefix.valueOf(ipAddress, 24);
}
/**
* Tests valid class constructor for a string.
* Tests invalid valueOf() converter for IPv6 address and
* negative prefix length.
*/
@Test
public void testConstructorForString() {
Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8");
assertThat(ip6prefix.toString(), is("1100::/8"));
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfAddressNegativePrefixLengthIPv6() {
Ip6Address ipAddress;
Ip6Prefix ipPrefix;
ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
ipPrefix = Ip6Prefix.valueOf(ipAddress, -1);
}
/**
* Tests invalid valueOf() converter for IPv6 address and
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() {
Ip6Address ipAddress;
Ip6Prefix ipPrefix;
ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8");
assertThat(ip6prefix.toString(), is("1100::/8"));
ipAddress =
Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
ipPrefix = Ip6Prefix.valueOf(ipAddress, 129);
}
/**
* Tests valueOf() converter for IPv6 string.
*/
@Test
public void testValueOfStringIPv6() {
Ip6Prefix ipPrefix;
ip6prefix =
new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8800/120");
assertThat(ip6prefix.toString(),
ipPrefix =
Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120");
assertThat(ipPrefix.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
ip6prefix = new Ip6Prefix("::/0");
assertThat(ip6prefix.toString(), is("::/0"));
ipPrefix =
Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128");
assertThat(ipPrefix.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
ip6prefix =
new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/128");
assertThat(ip6prefix.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8885/128"));
ipPrefix = Ip6Prefix.valueOf("::/0");
assertThat(ipPrefix.toString(), is("::/0"));
ip6prefix = new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertThat(ip6prefix.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
ipPrefix = Ip6Prefix.valueOf("::/128");
assertThat(ipPrefix.toString(), is("::/128"));
ipPrefix =
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0");
assertThat(ipPrefix.toString(), is("::/0"));
ipPrefix =
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64");
assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
ip6prefix =
new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/64");
assertThat(ip6prefix.toString(), is("1111:2222:3333:4444::/64"));
ipPrefix =
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertThat(ipPrefix.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
}
/**
* Tests invalid class constructor for a null string.
* Tests invalid valueOf() converter for a null string.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullString() {
String fromString = null;
Ip6Prefix ip6prefix = new Ip6Prefix(fromString);
public void testInvalidValueOfNullString() {
Ip6Prefix ipPrefix;
String fromString;
fromString = null;
ipPrefix = Ip6Prefix.valueOf(fromString);
}
/**
* Tests invalid class constructor for an empty string.
* Tests invalid valueOf() converter for an empty string.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidConstructors() {
// Check constructor for invalid ID: empty string
Ip6Prefix ip6prefix = new Ip6Prefix("");
public void testInvalidValueOfEmptyString() {
Ip6Prefix ipPrefix;
String fromString;
fromString = "";
ipPrefix = Ip6Prefix.valueOf(fromString);
}
/**
* Tests invalid valueOf() converter for an incorrect string.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfIncorrectString() {
Ip6Prefix ipPrefix;
String fromString;
fromString = "NoSuchIpPrefix";
ipPrefix = Ip6Prefix.valueOf(fromString);
}
/**
* Tests invalid valueOf() converter for IPv6 string and
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfStringNegativePrefixLengthIPv6() {
Ip6Prefix ipPrefix;
ipPrefix =
Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1");
}
/**
* Tests invalid valueOf() converter for IPv6 string and
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfStringTooLongPrefixLengthIPv6() {
Ip6Prefix ipPrefix;
ipPrefix =
Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129");
}
/**
* Tests getting the value of an address.
* Tests IP prefix contains another IP prefix for IPv6.
*/
@Test
public void testGetValue() {
Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8");
assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("1100::")));
assertThat(ip6prefix.getPrefixLen(), is((short) 8));
ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8");
assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("1100::")));
assertThat(ip6prefix.getPrefixLen(), is((short) 8));
ip6prefix =
new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8800/120");
assertThat(ip6prefix.getAddress(),
equalTo(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8800")));
assertThat(ip6prefix.getPrefixLen(), is((short) 120));
ip6prefix = new Ip6Prefix("::/0");
assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("::")));
assertThat(ip6prefix.getPrefixLen(), is((short) 0));
ip6prefix =
new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/128");
assertThat(ip6prefix.getAddress(),
equalTo(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885")));
assertThat(ip6prefix.getPrefixLen(), is((short) 128));
ip6prefix =
new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertThat(ip6prefix.getAddress(),
equalTo(new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
assertThat(ip6prefix.getPrefixLen(), is((short) 128));
ip6prefix =
new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/64");
assertThat(ip6prefix.getAddress(),
equalTo(new Ip6Address("1111:2222:3333:4444::")));
assertThat(ip6prefix.getPrefixLen(), is((short) 64));
public void testContainsIpPrefixIPv6() {
Ip6Prefix ipPrefix;
ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/120");
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/120")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/128")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::1/128")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/64")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4445::/120")));
assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64")));
assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/128");
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/120")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/128")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::1/128")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/64")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4445::/120")));
assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64")));
assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
ipPrefix = Ip6Prefix.valueOf("::/0");
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/120")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/128")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::1/128")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/64")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4445::/120")));
assertTrue(ipPrefix.contains(Ip6Prefix.valueOf("::/64")));
assertTrue(ipPrefix.contains(Ip6Prefix.valueOf("::/0")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
ipPrefix =
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/120")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/128")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::1/128")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4444::/64")));
assertFalse(ipPrefix.contains(
Ip6Prefix.valueOf("1111:2222:3333:4445::/120")));
assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64")));
assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0")));
assertTrue(ipPrefix.contains(
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
}
/**
* Tests equality of {@link Ip6Address}.
* Tests IP prefix contains IP address for IPv6.
*/
@Test
public void testEquality() {
Ip6Prefix addr1net = new Ip6Prefix("1100::/8");
Ip6Prefix addr2net = new Ip6Prefix("1100::/8");
assertThat(addr1net, is(addr2net));
addr1net = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8");
addr2net = new Ip6Prefix("1100::/8");
assertThat(addr1net, is(addr2net));
addr1net = new Ip6Prefix("::/0");
addr2net = new Ip6Prefix("::/0");
assertThat(addr1net, is(addr2net));
addr1net =
new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
addr2net =
new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertThat(addr1net, is(addr2net));
public void testContainsIpAddressIPv6() {
Ip6Prefix ipPrefix;
ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/120");
assertTrue(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4444::")));
assertTrue(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4444::1")));
assertFalse(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4445::")));
assertFalse(ipPrefix.contains(Ip6Address.valueOf("::")));
assertFalse(ipPrefix.contains(
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/128");
assertTrue(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4444::")));
assertFalse(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4444::1")));
assertFalse(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4445::")));
assertFalse(ipPrefix.contains(Ip6Address.valueOf("::")));
assertFalse(ipPrefix.contains(
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
ipPrefix = Ip6Prefix.valueOf("::/0");
assertTrue(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4444::")));
assertTrue(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4444::1")));
assertTrue(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4445::")));
assertTrue(ipPrefix.contains(Ip6Address.valueOf("::")));
assertTrue(ipPrefix.contains(
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
ipPrefix =
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertFalse(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4444::")));
assertFalse(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4444::1")));
assertFalse(ipPrefix.contains(
Ip6Address.valueOf("1111:2222:3333:4445::")));
assertFalse(ipPrefix.contains(Ip6Address.valueOf("::")));
assertTrue(ipPrefix.contains(
Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
}
/**
* Tests non-equality of {@link Ip6Address}.
* Tests equality of {@link Ip6Prefix} for IPv6.
*/
@Test
public void testNonEquality() {
Ip6Prefix addr1net = new Ip6Prefix("1100::/8");
Ip6Prefix addr2net = new Ip6Prefix("1200::/8");
Ip6Prefix addr3net = new Ip6Prefix("1200::/12");
Ip6Prefix addr4net = new Ip6Prefix("::/0");
Ip6Prefix addr5net =
new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertThat(addr1net, is(not(addr2net)));
assertThat(addr3net, is(not(addr2net)));
assertThat(addr4net, is(not(addr2net)));
assertThat(addr5net, is(not(addr2net)));
public void testEqualityIPv6() {
new EqualsTester()
.addEqualityGroup(
Ip6Prefix.valueOf("1111:2222:3333:4444::/120"),
Ip6Prefix.valueOf("1111:2222:3333:4444::1/120"),
Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))
.addEqualityGroup(
Ip6Prefix.valueOf("1111:2222:3333:4444::/64"),
Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))
.addEqualityGroup(
Ip6Prefix.valueOf("1111:2222:3333:4444::/128"),
Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))
.addEqualityGroup(
Ip6Prefix.valueOf("1111:2222:3333:4445::/64"),
Ip6Prefix.valueOf("1111:2222:3333:4445::/64"))
.addEqualityGroup(
Ip6Prefix.valueOf("::/0"),
Ip6Prefix.valueOf("::/0"))
.addEqualityGroup(
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))
.testEquals();
}
/**
* Tests object string representation.
* Tests object string representation for IPv6.
*/
@Test
public void testToString() {
Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8");
assertThat(ip6prefix.toString(), is("1100::/8"));
public void testToStringIPv6() {
Ip6Prefix ipPrefix;
ipPrefix = Ip6Prefix.valueOf("1100::/8");
assertThat(ipPrefix.toString(), is("1100::/8"));
ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8");
assertThat(ip6prefix.toString(), is("1100::/8"));
ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
assertThat(ipPrefix.toString(), is("1100::/8"));
ip6prefix = new Ip6Prefix("::/0");
assertThat(ip6prefix.toString(), is("::/0"));
ipPrefix = Ip6Prefix.valueOf("::/0");
assertThat(ipPrefix.toString(), is("::/0"));
ip6prefix =
new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertThat(ip6prefix.toString(),
ipPrefix = Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
assertThat(ipPrefix.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
}
}
......
......@@ -17,6 +17,7 @@ package org.onlab.packet;
import com.google.common.net.InetAddresses;
import com.google.common.testing.EqualsTester;
import org.junit.Ignore;
import org.junit.Test;
import java.net.InetAddress;
......@@ -33,6 +34,7 @@ public class IpAddressTest {
/**
* Tests the immutability of {@link IpAddress}.
*/
@Ignore("The class is not pure immutable, because it is not 'final'")
@Test
public void testImmutable() {
assertThatClassIsImmutable(IpAddress.class);
......@@ -80,19 +82,20 @@ public class IpAddressTest {
@Test
public void testAddressToOctetsIPv4() {
IpAddress ipAddress;
byte[] value;
final byte[] value1 = new byte[] {1, 2, 3, 4};
value = new byte[] {1, 2, 3, 4};
ipAddress = IpAddress.valueOf("1.2.3.4");
assertThat(ipAddress.toOctets(), is(value1));
assertThat(ipAddress.toOctets(), is(value));
final byte[] value2 = new byte[] {0, 0, 0, 0};
value = new byte[] {0, 0, 0, 0};
ipAddress = IpAddress.valueOf("0.0.0.0");
assertThat(ipAddress.toOctets(), is(value2));
assertThat(ipAddress.toOctets(), is(value));
final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress = IpAddress.valueOf("255.255.255.255");
assertThat(ipAddress.toOctets(), is(value3));
assertThat(ipAddress.toOctets(), is(value));
}
/**
......@@ -101,24 +104,25 @@ public class IpAddressTest {
@Test
public void testAddressToOctetsIPv6() {
IpAddress ipAddress;
byte[] value;
final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22,
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88};
ipAddress =
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ipAddress.toOctets(), is(value1));
assertThat(ipAddress.toOctets(), is(value));
final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00,
value = new byte[] {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
ipAddress = IpAddress.valueOf("::");
assertThat(ipAddress.toOctets(), is(value2));
assertThat(ipAddress.toOctets(), is(value));
final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
......@@ -128,11 +132,11 @@ public class IpAddressTest {
(byte) 0xff, (byte) 0xff};
ipAddress =
IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ipAddress.toOctets(), is(value3));
assertThat(ipAddress.toOctets(), is(value));
}
/**
* Tests returning an IPv4 address asn an integer.
* Tests returning an IPv4 address as an integer.
*/
@Test
public void testToInt() {
......@@ -171,18 +175,19 @@ public class IpAddressTest {
@Test
public void testValueOfByteArrayIPv4() {
IpAddress ipAddress;
byte[] value;
final byte[] value1 = new byte[] {1, 2, 3, 4};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value1);
value = new byte[] {1, 2, 3, 4};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
assertThat(ipAddress.toString(), is("1.2.3.4"));
final byte[] value2 = new byte[] {0, 0, 0, 0};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value2);
value = new byte[] {0, 0, 0, 0};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
assertThat(ipAddress.toString(), is("0.0.0.0"));
final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value3);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
......@@ -192,24 +197,25 @@ public class IpAddressTest {
@Test
public void testValueOfByteArrayIPv6() {
IpAddress ipAddress;
byte[] value;
final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22,
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value1);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00,
value = new byte[] {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value2);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
assertThat(ipAddress.toString(), is("::"));
final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
......@@ -217,7 +223,7 @@ public class IpAddressTest {
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value3);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
......@@ -274,24 +280,25 @@ public class IpAddressTest {
@Test
public void testValueOfByteArrayOffsetIPv4() {
IpAddress ipAddress;
byte[] value;
final byte[] value1 = new byte[] {11, 22, 33, // Preamble
value = new byte[] {11, 22, 33, // Preamble
1, 2, 3, 4,
44, 55}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value1, 3);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 3);
assertThat(ipAddress.toString(), is("1.2.3.4"));
final byte[] value2 = new byte[] {11, 22, // Preamble
value = new byte[] {11, 22, // Preamble
0, 0, 0, 0,
33}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value2, 2);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
assertThat(ipAddress.toString(), is("0.0.0.0"));
final byte[] value3 = new byte[] {11, 22, // Preamble
value = new byte[] {11, 22, // Preamble
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
33}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value3, 2);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
......@@ -301,28 +308,29 @@ public class IpAddressTest {
@Test
public void testValueOfByteArrayOffsetIPv6() {
IpAddress ipAddress;
byte[] value;
final byte[] value1 = new byte[] {11, 22, 33, // Preamble
value = new byte[] {11, 22, 33, // Preamble
0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88,
44, 55}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value1, 3);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 3);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
final byte[] value2 = new byte[] {11, 22, // Preamble
value = new byte[] {11, 22, // Preamble
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
33}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value2, 2);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
assertThat(ipAddress.toString(), is("::"));
final byte[] value3 = new byte[] {11, 22, // Preamble
value = new byte[] {11, 22, // Preamble
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
......@@ -332,7 +340,7 @@ public class IpAddressTest {
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
33}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value3, 2);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
......@@ -344,11 +352,12 @@ public class IpAddressTest {
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfArrayInvalidOffsetIPv4() {
IpAddress ipAddress;
byte[] value;
final byte[] value1 = new byte[] {11, 22, 33, // Preamble
value = new byte[] {11, 22, 33, // Preamble
1, 2, 3, 4,
44, 55}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value1, 6);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 6);
}
/**
......@@ -358,15 +367,16 @@ public class IpAddressTest {
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfArrayInvalidOffsetIPv6() {
IpAddress ipAddress;
byte[] value;
final byte[] value1 = new byte[] {11, 22, 33, // Preamble
value = new byte[] {11, 22, 33, // Preamble
0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88,
44, 55}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value1, 6);
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 6);
}
/**
......
......@@ -16,6 +16,7 @@
package org.onlab.packet;
import com.google.common.testing.EqualsTester;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
......@@ -32,6 +33,7 @@ public class IpPrefixTest {
/**
* Tests the immutability of {@link IpPrefix}.
*/
@Ignore("The class is not pure immutable, because it is not 'final'")
@Test
public void testImmutable() {
assertThatClassIsImmutable(IpPrefix.class);
......