Pavlin Radoslavov

Work toward common IP address classes.

Ported the following IP address classes from the older codebase:
Ip4Address, Ip6Address, Ip4Prefix, Ip6Prefix (and the corresponding
unit tests).
NOTE: Those classes are not ready to be used yet.

Change-Id: I234875abbc9df8daa2f8ae28706af591dd2c5f2d
1 +package org.onlab.packet;
2 +
3 +import java.nio.ByteBuffer;
4 +import static com.google.common.base.Preconditions.checkNotNull;
5 +
6 +/**
7 + * The class representing an IPv4 address.
8 + * This class is immutable.
9 + */
10 +public final class Ip4Address implements Comparable<Ip4Address> {
11 + private final int value;
12 +
13 + /** The length of the address in bytes (octets). */
14 + public static final int BYTE_LENGTH = 4;
15 +
16 + /** The length of the address in bits. */
17 + public static final int BIT_LENGTH = BYTE_LENGTH * Byte.SIZE;
18 +
19 + /**
20 + * Default constructor.
21 + */
22 + public Ip4Address() {
23 + this.value = 0;
24 + }
25 +
26 + /**
27 + * Copy constructor.
28 + *
29 + * @param other the object to copy from
30 + */
31 + public Ip4Address(Ip4Address other) {
32 + this.value = other.value;
33 + }
34 +
35 + /**
36 + * Constructor from an integer value.
37 + *
38 + * @param value the value to use
39 + */
40 + public Ip4Address(int value) {
41 + this.value = value;
42 + }
43 +
44 + /**
45 + * Constructor from a byte array with the IPv4 address stored in network
46 + * byte order (i.e., the most significant byte first).
47 + *
48 + * @param value the value to use
49 + */
50 + public Ip4Address(byte[] value) {
51 + this(value, 0);
52 + }
53 +
54 + /**
55 + * Constructor from a byte array with the IPv4 address stored in network
56 + * byte order (i.e., the most significant byte first), and a given offset
57 + * from the beginning of the byte array.
58 + *
59 + * @param value the value to use
60 + * @param offset the offset in bytes from the beginning of the byte array
61 + */
62 + public Ip4Address(byte[] value, int offset) {
63 + checkNotNull(value);
64 +
65 + // Verify the arguments
66 + if ((offset < 0) || (offset + BYTE_LENGTH > value.length)) {
67 + String msg;
68 + if (value.length < BYTE_LENGTH) {
69 + msg = "Invalid IPv4 address array: array length: " +
70 + value.length + ". Must be at least " + BYTE_LENGTH;
71 + } else {
72 + msg = "Invalid IPv4 address array: array offset: " +
73 + offset + ". Must be in the interval [0, " +
74 + (value.length - BYTE_LENGTH) + "]";
75 + }
76 + throw new IllegalArgumentException(msg);
77 + }
78 +
79 + // Read the address
80 + ByteBuffer bb = ByteBuffer.wrap(value);
81 + this.value = bb.getInt(offset);
82 + }
83 +
84 + /**
85 + * Constructs an IPv4 address from a string representation of the address.
86 + *<p>
87 + * Example: "1.2.3.4"
88 + *
89 + * @param value the value to use
90 + */
91 + public Ip4Address(String value) {
92 + checkNotNull(value);
93 +
94 + String[] splits = value.split("\\.");
95 + if (splits.length != 4) {
96 + final String msg = "Invalid IPv4 address string: " + value;
97 + throw new IllegalArgumentException(msg);
98 + }
99 +
100 + int result = 0;
101 + for (int i = 0; i < BYTE_LENGTH; i++) {
102 + result |= Integer.parseInt(splits[i]) <<
103 + ((BYTE_LENGTH - (i + 1)) * Byte.SIZE);
104 + }
105 + this.value = result;
106 + }
107 +
108 + /**
109 + * Gets the IPv4 address as a byte array.
110 + *
111 + * @return a byte array with the IPv4 address stored in network byte order
112 + * (i.e., the most significant byte first).
113 + */
114 + public byte[] toOctets() {
115 + return ByteBuffer.allocate(BYTE_LENGTH).putInt(value).array();
116 + }
117 +
118 + /**
119 + * Creates an IPv4 network mask prefix.
120 + *
121 + * @param prefixLen the length of the mask prefix. Must be in the interval
122 + * [0, 32].
123 + * @return a new IPv4 address that contains a mask prefix of the
124 + * specified length
125 + */
126 + public static Ip4Address makeMaskPrefix(int prefixLen) {
127 + // Verify the prefix length
128 + if ((prefixLen < 0) || (prefixLen > Ip4Address.BIT_LENGTH)) {
129 + final String msg = "Invalid IPv4 prefix length: " + prefixLen +
130 + ". Must be in the interval [0, 32].";
131 + throw new IllegalArgumentException(msg);
132 + }
133 +
134 + long v =
135 + (0xffffffffL << (Ip4Address.BIT_LENGTH - prefixLen)) & 0xffffffffL;
136 + return new Ip4Address((int) v);
137 + }
138 +
139 + /**
140 + * Creates an IPv4 address by masking it with a network mask of given
141 + * mask length.
142 + *
143 + * @param addr the address to mask
144 + * @param prefixLen the length of the mask prefix. Must be in the interval
145 + * [0, 32].
146 + * @return a new IPv4 address that is masked with a mask prefix of the
147 + * specified length
148 + */
149 + public static Ip4Address makeMaskedAddress(final Ip4Address addr,
150 + int prefixLen) {
151 + Ip4Address mask = Ip4Address.makeMaskPrefix(prefixLen);
152 + long v = addr.value & mask.value;
153 +
154 + return new Ip4Address((int) v);
155 + }
156 +
157 + /**
158 + * Gets the value of the IPv4 address.
159 + *
160 + * @return the value of the IPv4 address
161 + */
162 + public int getValue() {
163 + return value;
164 + }
165 +
166 + /**
167 + * Converts the IPv4 value to a '.' separated string.
168 + *
169 + * @return the IPv4 value as a '.' separated string
170 + */
171 + @Override
172 + public String toString() {
173 + return ((this.value >> 24) & 0xff) + "." +
174 + ((this.value >> 16) & 0xff) + "." +
175 + ((this.value >> 8) & 0xff) + "." +
176 + (this.value & 0xff);
177 + }
178 +
179 + @Override
180 + public boolean equals(Object o) {
181 + if (!(o instanceof Ip4Address)) {
182 + return false;
183 + }
184 + Ip4Address other = (Ip4Address) o;
185 + if (this.value != other.value) {
186 + return false;
187 + }
188 + return true;
189 + }
190 +
191 + @Override
192 + public int hashCode() {
193 + return this.value;
194 + }
195 +
196 + @Override
197 + public int compareTo(Ip4Address o) {
198 + Long lv = ((long) this.value) & 0xffffffffL;
199 + Long rv = ((long) o.value) & 0xffffffffL;
200 + return lv.compareTo(rv);
201 + }
202 +}
1 +package org.onlab.packet;
2 +
3 +import java.util.Objects;
4 +
5 +/**
6 + * The class representing an IPv4 network address.
7 + * This class is immutable.
8 + */
9 +public final class Ip4Prefix {
10 + private final Ip4Address address; // The IPv4 address
11 + private final short prefixLen; // The prefix length
12 +
13 + /**
14 + * Default constructor.
15 + */
16 + public Ip4Prefix() {
17 + this.address = new Ip4Address();
18 + this.prefixLen = 0;
19 + }
20 +
21 + /**
22 + * Copy constructor.
23 + *
24 + * @param other the object to copy from
25 + */
26 + public Ip4Prefix(Ip4Prefix other) {
27 + this.address = new Ip4Address(other.address);
28 + this.prefixLen = other.prefixLen;
29 + }
30 +
31 + /**
32 + * Constructor for a given address and prefix length.
33 + *
34 + * @param address the address to use
35 + * @param prefixLen the prefix length to use
36 + */
37 + public Ip4Prefix(Ip4Address address, short prefixLen) {
38 + this.address = Ip4Address.makeMaskedAddress(address, prefixLen);
39 + this.prefixLen = prefixLen;
40 + }
41 +
42 + /**
43 + * Constructs an IPv4 prefix from a string representation of the
44 + * prefix.
45 + *<p>
46 + * Example: "1.2.0.0/16"
47 + *
48 + * @param value the value to use
49 + */
50 + public Ip4Prefix(String value) {
51 + String[] splits = value.split("/");
52 + if (splits.length != 2) {
53 + throw new IllegalArgumentException("Specified IPv4 prefix must contain an IPv4 " +
54 + "address and a prefix length separated by '/'");
55 + }
56 + this.prefixLen = Short.decode(splits[1]);
57 + this.address = Ip4Address.makeMaskedAddress(new Ip4Address(splits[0]),
58 + this.prefixLen);
59 + }
60 +
61 + /**
62 + * Gets the address value of the IPv4 prefix.
63 + *
64 + * @return the address value of the IPv4 prefix
65 + */
66 + public Ip4Address getAddress() {
67 + return address;
68 + }
69 +
70 + /**
71 + * Gets the prefix length value of the IPv4 prefix.
72 + *
73 + * @return the prefix length value of the IPv4 prefix
74 + */
75 + public short getPrefixLen() {
76 + return prefixLen;
77 + }
78 +
79 + /**
80 + * Converts the IPv4 prefix value to an "address/prefixLen" string.
81 + *
82 + * @return the IPv4 prefix value as an "address/prefixLen" string
83 + */
84 + @Override
85 + public String toString() {
86 + return this.address.toString() + "/" + this.prefixLen;
87 + }
88 +
89 + /**
90 + * Compares the value of two Ip4Prefix objects.
91 + * <p/>
92 + * Note the value of the IPv4 address is compared directly between the
93 + * objects, and must match exactly for the objects to be considered equal.
94 + * This may result in objects which represent the same IP prefix being
95 + * classified as unequal, because the unsignificant bits of the address
96 + * field don't match (the bits to the right of the prefix length).
97 + * <p/>
98 + * TODO Change this behavior so that objects that represent the same prefix
99 + * are classified as equal according to this equals method.
100 + *
101 + * @see Object#equals(Object)
102 + */
103 + @Override
104 + public boolean equals(Object other) {
105 + if (other == this) {
106 + return true;
107 + }
108 +
109 + if (!(other instanceof Ip4Prefix)) {
110 + return false;
111 + }
112 +
113 + Ip4Prefix otherIp4Prefix = (Ip4Prefix) other;
114 +
115 + return Objects.equals(this.address, otherIp4Prefix.address)
116 + && this.prefixLen == otherIp4Prefix.prefixLen;
117 + }
118 +
119 + @Override
120 + public int hashCode() {
121 + return Objects.hash(address, prefixLen);
122 + }
123 +}
1 +package org.onlab.packet;
2 +
3 +import java.net.InetAddress;
4 +import java.net.UnknownHostException;
5 +import java.nio.ByteBuffer;
6 +import java.util.Objects;
7 +
8 +import com.google.common.net.InetAddresses;
9 +import com.google.common.primitives.UnsignedLongs;
10 +
11 +import static com.google.common.base.Preconditions.checkNotNull;
12 +import static com.google.common.base.Preconditions.checkState;
13 +
14 +/**
15 + * The class representing an IPv6 address.
16 + * This class is immutable.
17 + */
18 +public final class Ip6Address implements Comparable<Ip6Address> {
19 + private final long valueHigh; // The higher (more significant) 64 bits
20 + private final long valueLow; // The lower (less significant) 64 bits
21 +
22 + /** The length of the address in bytes (octets). */
23 + public static final int BYTE_LENGTH = 16;
24 +
25 + /** The length of the address in bits. */
26 + public static final int BIT_LENGTH = BYTE_LENGTH * Byte.SIZE;
27 +
28 + /**
29 + * Default constructor.
30 + */
31 + public Ip6Address() {
32 + this.valueHigh = 0;
33 + this.valueLow = 0;
34 + }
35 +
36 + /**
37 + * Copy constructor.
38 + *
39 + * @param other the object to copy from
40 + */
41 + public Ip6Address(Ip6Address other) {
42 + this.valueHigh = other.valueHigh;
43 + this.valueLow = other.valueLow;
44 + }
45 +
46 + /**
47 + * Constructor from integer values.
48 + *
49 + * @param valueHigh the higher (more significant) 64 bits of the address
50 + * @param valueLow the lower (less significant) 64 bits of the address
51 + */
52 + public Ip6Address(long valueHigh, long valueLow) {
53 + this.valueHigh = valueHigh;
54 + this.valueLow = valueLow;
55 + }
56 +
57 + /**
58 + * Constructor from a byte array with the IPv6 address stored in network
59 + * byte order (i.e., the most significant byte first).
60 + *
61 + * @param value the value to use
62 + */
63 + public Ip6Address(byte[] value) {
64 + this(value, 0);
65 + }
66 +
67 + /**
68 + * Constructor from a byte array with the IPv6 address stored in network
69 + * byte order (i.e., the most significant byte first), and a given offset
70 + * from the beginning of the byte array.
71 + *
72 + * @param value the value to use
73 + * @param offset the offset in bytes from the beginning of the byte array
74 + */
75 + public Ip6Address(byte[] value, int offset) {
76 + checkNotNull(value);
77 +
78 + // Verify the arguments
79 + if ((offset < 0) || (offset + BYTE_LENGTH > value.length)) {
80 + String msg;
81 + if (value.length < BYTE_LENGTH) {
82 + msg = "Invalid IPv6 address array: array length: " +
83 + value.length + ". Must be at least " + BYTE_LENGTH;
84 + } else {
85 + msg = "Invalid IPv6 address array: array offset: " +
86 + offset + ". Must be in the interval [0, " +
87 + (value.length - BYTE_LENGTH) + "]";
88 + }
89 + throw new IllegalArgumentException(msg);
90 + }
91 +
92 + // Read the address
93 + ByteBuffer bb = ByteBuffer.wrap(value);
94 + bb.position(offset);
95 + this.valueHigh = bb.getLong();
96 + this.valueLow = bb.getLong();
97 + }
98 +
99 + /**
100 + * Constructs an IPv6 address from a string representation of the address.
101 + *<p>
102 + * Example: "1111:2222::8888"
103 + *
104 + * @param value the value to use
105 + */
106 + public Ip6Address(String value) {
107 + checkNotNull(value);
108 +
109 + if (value.isEmpty()) {
110 + final String msg = "Specified IPv6 cannot be an empty string";
111 + throw new IllegalArgumentException(msg);
112 + }
113 + InetAddress addr = null;
114 + try {
115 + addr = InetAddresses.forString(value);
116 + } catch (IllegalArgumentException e) {
117 + final String msg = "Invalid IPv6 address string: " + value;
118 + throw new IllegalArgumentException(msg);
119 + }
120 + byte[] bytes = addr.getAddress();
121 + ByteBuffer bb = ByteBuffer.wrap(bytes);
122 + this.valueHigh = bb.getLong();
123 + this.valueLow = bb.getLong();
124 + }
125 +
126 + /**
127 + * Gets the IPv6 address as a byte array.
128 + *
129 + * @return a byte array with the IPv6 address stored in network byte order
130 + * (i.e., the most significant byte first).
131 + */
132 + public byte[] toOctets() {
133 + return ByteBuffer.allocate(BYTE_LENGTH)
134 + .putLong(valueHigh).putLong(valueLow).array();
135 + }
136 +
137 + /**
138 + * Creates an IPv6 network mask prefix.
139 + *
140 + * @param prefixLen the length of the mask prefix. Must be in the interval
141 + * [0, 128].
142 + * @return a new IPv6 address that contains a mask prefix of the
143 + * specified length
144 + */
145 + public static Ip6Address makeMaskPrefix(int prefixLen) {
146 + long vh, vl;
147 +
148 + // Verify the prefix length
149 + if ((prefixLen < 0) || (prefixLen > Ip6Address.BIT_LENGTH)) {
150 + final String msg = "Invalid IPv6 prefix length: " + prefixLen +
151 + ". Must be in the interval [0, 128].";
152 + throw new IllegalArgumentException(msg);
153 + }
154 +
155 + if (prefixLen == 0) {
156 + //
157 + // NOTE: Apparently, the result of "<< 64" shifting to the left
158 + // results in all 1s instead of all 0s, hence we handle it as
159 + // a special case.
160 + //
161 + vh = 0;
162 + vl = 0;
163 + } else if (prefixLen <= 64) {
164 + vh = (0xffffffffffffffffL << (64 - prefixLen)) & 0xffffffffffffffffL;
165 + vl = 0;
166 + } else {
167 + vh = -1L; // All 1s
168 + vl = (0xffffffffffffffffL << (128 - prefixLen)) & 0xffffffffffffffffL;
169 + }
170 + return new Ip6Address(vh, vl);
171 + }
172 +
173 + /**
174 + * Creates an IPv6 address by masking it with a network mask of given
175 + * mask length.
176 + *
177 + * @param addr the address to mask
178 + * @param prefixLen the length of the mask prefix. Must be in the interval
179 + * [0, 128].
180 + * @return a new IPv6 address that is masked with a mask prefix of the
181 + * specified length
182 + */
183 + public static Ip6Address makeMaskedAddress(final Ip6Address addr,
184 + int prefixLen) {
185 + Ip6Address mask = Ip6Address.makeMaskPrefix(prefixLen);
186 + long vh = addr.valueHigh & mask.valueHigh;
187 + long vl = addr.valueLow & mask.valueLow;
188 +
189 + return new Ip6Address(vh, vl);
190 + }
191 +
192 + /**
193 + * Gets the value of the higher (more significant) 64 bits of the address.
194 + *
195 + * @return the value of the higher (more significant) 64 bits of the
196 + * address
197 + */
198 + public long getValueHigh() {
199 + return valueHigh;
200 + }
201 +
202 + /**
203 + * Gets the value of the lower (less significant) 64 bits of the address.
204 + *
205 + * @return the value of the lower (less significant) 64 bits of the
206 + * address
207 + */
208 + public long getValueLow() {
209 + return valueLow;
210 + }
211 +
212 + /**
213 + * Converts the IPv6 value to a ':' separated string.
214 + *
215 + * @return the IPv6 value as a ':' separated string
216 + */
217 + @Override
218 + public String toString() {
219 + ByteBuffer bb = ByteBuffer.allocate(Ip6Address.BYTE_LENGTH);
220 + bb.putLong(valueHigh);
221 + bb.putLong(valueLow);
222 + InetAddress inetAddr = null;
223 + try {
224 + inetAddr = InetAddress.getByAddress(bb.array());
225 + } catch (UnknownHostException e) {
226 + // Should never happen
227 + checkState(false, "Internal error: Ip6Address.toString()");
228 + return "::";
229 + }
230 + return InetAddresses.toAddrString(inetAddr);
231 + }
232 +
233 + @Override
234 + public boolean equals(Object o) {
235 + if (!(o instanceof Ip6Address)) {
236 + return false;
237 + }
238 + Ip6Address other = (Ip6Address) o;
239 + return this.valueHigh == other.valueHigh
240 + && this.valueLow == other.valueLow;
241 + }
242 +
243 + @Override
244 + public int hashCode() {
245 + return Objects.hash(valueHigh, valueLow);
246 + }
247 +
248 + @Override
249 + public int compareTo(Ip6Address o) {
250 + // Compare the high-order 64-bit value
251 + if (this.valueHigh != o.valueHigh) {
252 + return UnsignedLongs.compare(this.valueHigh, o.valueHigh);
253 + }
254 + // Compare the low-order 64-bit value
255 + if (this.valueLow != o.valueLow) {
256 + return UnsignedLongs.compare(this.valueLow, o.valueLow);
257 + }
258 + return 0;
259 + }
260 +}
1 +package org.onlab.packet;
2 +
3 +import java.util.Objects;
4 +
5 +/**
6 + * The class representing an IPv6 network address.
7 + * This class is immutable.
8 + */
9 +public final class Ip6Prefix {
10 + private final Ip6Address address; // The IPv6 address
11 + private final short prefixLen; // The prefix length
12 +
13 + /**
14 + * Default constructor.
15 + */
16 + public Ip6Prefix() {
17 + this.address = new Ip6Address();
18 + this.prefixLen = 0;
19 + }
20 +
21 + /**
22 + * Copy constructor.
23 + *
24 + * @param other the object to copy from
25 + */
26 + public Ip6Prefix(Ip6Prefix other) {
27 + this.address = new Ip6Address(other.address);
28 + this.prefixLen = other.prefixLen;
29 + }
30 +
31 + /**
32 + * Constructor for a given address and prefix length.
33 + *
34 + * @param address the address to use
35 + * @param prefixLen the prefix length to use
36 + */
37 + public Ip6Prefix(Ip6Address address, short prefixLen) {
38 + this.address = Ip6Address.makeMaskedAddress(address, prefixLen);
39 + this.prefixLen = prefixLen;
40 + }
41 +
42 + /**
43 + * Constructs an IPv6 prefix from a string representation of the
44 + * prefix.
45 + *<p>
46 + * Example: "1111:2222::/32"
47 + *
48 + * @param value the value to use
49 + */
50 + public Ip6Prefix(String value) {
51 + String[] splits = value.split("/");
52 + if (splits.length != 2) {
53 + throw new IllegalArgumentException("Specified IPv6 prefix must contain an IPv6 " +
54 + "address and a prefix length separated by '/'");
55 + }
56 + this.prefixLen = Short.decode(splits[1]);
57 + this.address = Ip6Address.makeMaskedAddress(new Ip6Address(splits[0]),
58 + this.prefixLen);
59 + }
60 +
61 + /**
62 + * Gets the address value of the IPv6 prefix.
63 + *
64 + * @return the address value of the IPv6 prefix
65 + */
66 + public Ip6Address getAddress() {
67 + return address;
68 + }
69 +
70 + /**
71 + * Gets the prefix length value of the IPv6 prefix.
72 + *
73 + * @return the prefix length value of the IPv6 prefix
74 + */
75 + public short getPrefixLen() {
76 + return prefixLen;
77 + }
78 +
79 + /**
80 + * Converts the IPv6 prefix value to an "address/prefixLen" string.
81 + *
82 + * @return the IPv6 prefix value as an "address/prefixLen" string
83 + */
84 + @Override
85 + public String toString() {
86 + return this.address.toString() + "/" + this.prefixLen;
87 + }
88 +
89 + /**
90 + * Compares the value of two Ip6Prefix objects.
91 + * <p/>
92 + * Note the value of the IPv6 address is compared directly between the
93 + * objects, and must match exactly for the objects to be considered equal.
94 + * This may result in objects which represent the same IP prefix being
95 + * classified as unequal, because the unsignificant bits of the address
96 + * field don't match (the bits to the right of the prefix length).
97 + * <p/>
98 + * TODO Change this behavior so that objects that represent the same prefix
99 + * are classified as equal according to this equals method.
100 + *
101 + * @see Object#equals(Object)
102 + */
103 + @Override
104 + public boolean equals(Object other) {
105 + if (other == this) {
106 + return true;
107 + }
108 +
109 + if (!(other instanceof Ip6Prefix)) {
110 + return false;
111 + }
112 +
113 + Ip6Prefix otherIp6Prefix = (Ip6Prefix) other;
114 +
115 + return Objects.equals(this.address, otherIp6Prefix.address)
116 + && this.prefixLen == otherIp6Prefix.prefixLen;
117 + }
118 +
119 + @Override
120 + public int hashCode() {
121 + return Objects.hash(address, prefixLen);
122 + }
123 +}
This diff is collapsed. Click to expand it.
1 +package org.onlab.packet;
2 +
3 +import org.junit.Test;
4 +
5 +import static org.hamcrest.Matchers.equalTo;
6 +import static org.hamcrest.Matchers.is;
7 +import static org.hamcrest.Matchers.not;
8 +import static org.junit.Assert.assertThat;
9 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
10 +
11 +/**
12 + * Tests for class {@link Ip4Prefix}.
13 + */
14 +public class Ip4PrefixTest {
15 + /**
16 + * Tests the immutability of {@link Ip4Prefix}.
17 + */
18 + @Test
19 + public void testImmutable() {
20 + assertThatClassIsImmutable(Ip4Prefix.class);
21 + }
22 +
23 + /**
24 + * Tests default class constructor.
25 + */
26 + @Test
27 + public void testDefaultConstructor() {
28 + Ip4Prefix ip4prefix = new Ip4Prefix();
29 + assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
30 + }
31 +
32 + /**
33 + * Tests valid class copy constructor.
34 + */
35 + @Test
36 + public void testCopyConstructor() {
37 + Ip4Prefix fromAddr = new Ip4Prefix("1.2.3.0/24");
38 + Ip4Prefix ip4prefix = new Ip4Prefix(fromAddr);
39 + assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
40 +
41 + fromAddr = new Ip4Prefix("0.0.0.0/0");
42 + ip4prefix = new Ip4Prefix(fromAddr);
43 + assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
44 +
45 + fromAddr = new Ip4Prefix("255.255.255.255/32");
46 + ip4prefix = new Ip4Prefix(fromAddr);
47 + assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
48 + }
49 +
50 + /**
51 + * Tests invalid class copy constructor for a null object to copy from.
52 + */
53 + @Test(expected = NullPointerException.class)
54 + public void testInvalidConstructorNullObject() {
55 + Ip4Prefix fromAddr = null;
56 + Ip4Prefix ip4prefix = new Ip4Prefix(fromAddr);
57 + }
58 +
59 + /**
60 + * Tests valid class constructor for an address and prefix length.
61 + */
62 + @Test
63 + public void testConstructorForAddressAndPrefixLength() {
64 + Ip4Prefix ip4prefix =
65 + new Ip4Prefix(new Ip4Address("1.2.3.0"), (short) 24);
66 + assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
67 +
68 + ip4prefix = new Ip4Prefix(new Ip4Address("1.2.3.4"), (short) 24);
69 + assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
70 +
71 + ip4prefix = new Ip4Prefix(new Ip4Address("1.2.3.5"), (short) 32);
72 + assertThat(ip4prefix.toString(), is("1.2.3.5/32"));
73 +
74 + ip4prefix = new Ip4Prefix(new Ip4Address("0.0.0.0"), (short) 0);
75 + assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
76 +
77 + ip4prefix =
78 + new Ip4Prefix(new Ip4Address("255.255.255.255"), (short) 32);
79 + assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
80 + }
81 +
82 + /**
83 + * Tests valid class constructor for a string.
84 + */
85 + @Test
86 + public void testConstructorForString() {
87 + Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
88 + assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
89 +
90 + ip4prefix = new Ip4Prefix("1.2.3.4/24");
91 + assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
92 +
93 + ip4prefix = new Ip4Prefix("1.2.3.5/32");
94 + assertThat(ip4prefix.toString(), is("1.2.3.5/32"));
95 +
96 + ip4prefix = new Ip4Prefix("0.0.0.0/0");
97 + assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
98 +
99 + ip4prefix = new Ip4Prefix("255.255.255.255/32");
100 + assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
101 + }
102 +
103 + /**
104 + * Tests invalid class constructor for a null string.
105 + */
106 + @Test(expected = NullPointerException.class)
107 + public void testInvalidConstructorNullString() {
108 + String fromString = null;
109 + Ip4Prefix ip4prefix = new Ip4Prefix(fromString);
110 + }
111 +
112 + /**
113 + * Tests invalid class constructor for an empty string.
114 + */
115 + @Test(expected = IllegalArgumentException.class)
116 + public void testInvalidConstructors() {
117 + // Check constructor for invalid ID: empty string
118 + Ip4Prefix ip4prefix = new Ip4Prefix("");
119 + }
120 +
121 + /**
122 + * Tests getting the value of an address.
123 + */
124 + @Test
125 + public void testGetValue() {
126 + Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
127 + assertThat(ip4prefix.getAddress(), equalTo(new Ip4Address("1.2.3.0")));
128 + assertThat(ip4prefix.getPrefixLen(), is((short) 24));
129 +
130 + ip4prefix = new Ip4Prefix("0.0.0.0/0");
131 + assertThat(ip4prefix.getAddress(), equalTo(new Ip4Address("0.0.0.0")));
132 + assertThat(ip4prefix.getPrefixLen(), is((short) 0));
133 +
134 + ip4prefix = new Ip4Prefix("255.255.255.255/32");
135 + assertThat(ip4prefix.getAddress(),
136 + equalTo(new Ip4Address("255.255.255.255")));
137 + assertThat(ip4prefix.getPrefixLen(), is((short) 32));
138 + }
139 +
140 + /**
141 + * Tests equality of {@link Ip4Address}.
142 + */
143 + @Test
144 + public void testEquality() {
145 + Ip4Prefix addr1net = new Ip4Prefix("1.2.3.0/24");
146 + Ip4Prefix addr2net = new Ip4Prefix("1.2.3.0/24");
147 + assertThat(addr1net, is(addr2net));
148 +
149 + addr1net = new Ip4Prefix("1.2.3.0/24");
150 + addr2net = new Ip4Prefix("1.2.3.4/24");
151 + assertThat(addr1net, is(addr2net));
152 +
153 + addr1net = new Ip4Prefix("0.0.0.0/0");
154 + addr2net = new Ip4Prefix("0.0.0.0/0");
155 + assertThat(addr1net, is(addr2net));
156 +
157 + addr1net = new Ip4Prefix("255.255.255.255/32");
158 + addr2net = new Ip4Prefix("255.255.255.255/32");
159 + assertThat(addr1net, is(addr2net));
160 + }
161 +
162 + /**
163 + * Tests non-equality of {@link Ip4Address}.
164 + */
165 + @Test
166 + public void testNonEquality() {
167 + Ip4Prefix addr1net = new Ip4Prefix("1.2.0.0/16");
168 + Ip4Prefix addr2net = new Ip4Prefix("1.3.0.0/16");
169 + Ip4Prefix addr3net = new Ip4Prefix("1.3.0.0/24");
170 + Ip4Prefix addr4net = new Ip4Prefix("0.0.0.0/0");
171 + Ip4Prefix addr5net = new Ip4Prefix("255.255.255.255/32");
172 + assertThat(addr1net, is(not(addr2net)));
173 + assertThat(addr3net, is(not(addr2net)));
174 + assertThat(addr4net, is(not(addr2net)));
175 + assertThat(addr5net, is(not(addr2net)));
176 + }
177 +
178 + /**
179 + * Tests object string representation.
180 + */
181 + @Test
182 + public void testToString() {
183 + Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24");
184 + assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
185 +
186 + ip4prefix = new Ip4Prefix("1.2.3.4/24");
187 + assertThat(ip4prefix.toString(), is("1.2.3.0/24"));
188 +
189 + ip4prefix = new Ip4Prefix("0.0.0.0/0");
190 + assertThat(ip4prefix.toString(), is("0.0.0.0/0"));
191 +
192 + ip4prefix = new Ip4Prefix("255.255.255.255/32");
193 + assertThat(ip4prefix.toString(), is("255.255.255.255/32"));
194 + }
195 +}
This diff is collapsed. Click to expand it.
1 +package org.onlab.packet;
2 +
3 +import org.junit.Test;
4 +
5 +import static org.hamcrest.Matchers.equalTo;
6 +import static org.hamcrest.Matchers.is;
7 +import static org.hamcrest.Matchers.not;
8 +import static org.junit.Assert.assertThat;
9 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
10 +
11 +/**
12 + * Tests for class {@link Ip6Prefix}.
13 + */
14 +public class Ip6PrefixTest {
15 + /**
16 + * Tests the immutability of {@link Ip6Prefix}.
17 + */
18 + @Test
19 + public void testImmutable() {
20 + assertThatClassIsImmutable(Ip6Prefix.class);
21 + }
22 +
23 + /**
24 + * Tests default class constructor.
25 + */
26 + @Test
27 + public void testDefaultConstructor() {
28 + Ip6Prefix ip6prefix = new Ip6Prefix();
29 + assertThat(ip6prefix.toString(), is("::/0"));
30 + }
31 +
32 + /**
33 + * Tests valid class copy constructor.
34 + */
35 + @Test
36 + public void testCopyConstructor() {
37 + Ip6Prefix fromAddr = new Ip6Prefix("1100::/8");
38 + Ip6Prefix ip6prefix = new Ip6Prefix(fromAddr);
39 + assertThat(ip6prefix.toString(), is("1100::/8"));
40 +
41 + fromAddr = new Ip6Prefix("::/0");
42 + ip6prefix = new Ip6Prefix(fromAddr);
43 + assertThat(ip6prefix.toString(), is("::/0"));
44 +
45 + fromAddr =
46 + new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
47 + ip6prefix = new Ip6Prefix(fromAddr);
48 + assertThat(ip6prefix.toString(),
49 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
50 + }
51 +
52 + /**
53 + * Tests invalid class copy constructor for a null object to copy from.
54 + */
55 + @Test(expected = NullPointerException.class)
56 + public void testInvalidConstructorNullObject() {
57 + Ip6Prefix fromAddr = null;
58 + Ip6Prefix ip6prefix = new Ip6Prefix(fromAddr);
59 + }
60 +
61 + /**
62 + * Tests valid class constructor for an address and prefix length.
63 + */
64 + @Test
65 + public void testConstructorForAddressAndPrefixLength() {
66 + Ip6Prefix ip6prefix =
67 + new Ip6Prefix(new Ip6Address("1100::"), (short) 8);
68 + assertThat(ip6prefix.toString(), is("1100::/8"));
69 +
70 + ip6prefix =
71 + new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"),
72 + (short) 8);
73 + assertThat(ip6prefix.toString(), is("1100::/8"));
74 +
75 + ip6prefix =
76 + new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8800"),
77 + (short) 120);
78 + assertThat(ip6prefix.toString(),
79 + is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
80 +
81 + ip6prefix = new Ip6Prefix(new Ip6Address("::"), (short) 0);
82 + assertThat(ip6prefix.toString(), is("::/0"));
83 +
84 + ip6prefix =
85 + new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"),
86 + (short) 128);
87 + assertThat(ip6prefix.toString(),
88 + is("1111:2222:3333:4444:5555:6666:7777:8885/128"));
89 +
90 + ip6prefix =
91 + new Ip6Prefix(new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
92 + (short) 128);
93 + assertThat(ip6prefix.toString(),
94 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
95 +
96 + ip6prefix =
97 + new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"),
98 + (short) 64);
99 + assertThat(ip6prefix.toString(), is("1111:2222:3333:4444::/64"));
100 + }
101 +
102 + /**
103 + * Tests valid class constructor for a string.
104 + */
105 + @Test
106 + public void testConstructorForString() {
107 + Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8");
108 + assertThat(ip6prefix.toString(), is("1100::/8"));
109 +
110 + ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8");
111 + assertThat(ip6prefix.toString(), is("1100::/8"));
112 +
113 + ip6prefix =
114 + new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8800/120");
115 + assertThat(ip6prefix.toString(),
116 + is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
117 +
118 + ip6prefix = new Ip6Prefix("::/0");
119 + assertThat(ip6prefix.toString(), is("::/0"));
120 +
121 + ip6prefix =
122 + new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/128");
123 + assertThat(ip6prefix.toString(),
124 + is("1111:2222:3333:4444:5555:6666:7777:8885/128"));
125 +
126 + ip6prefix = new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
127 + assertThat(ip6prefix.toString(),
128 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
129 +
130 + ip6prefix =
131 + new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/64");
132 + assertThat(ip6prefix.toString(), is("1111:2222:3333:4444::/64"));
133 + }
134 +
135 + /**
136 + * Tests invalid class constructor for a null string.
137 + */
138 + @Test(expected = NullPointerException.class)
139 + public void testInvalidConstructorNullString() {
140 + String fromString = null;
141 + Ip6Prefix ip6prefix = new Ip6Prefix(fromString);
142 + }
143 +
144 + /**
145 + * Tests invalid class constructor for an empty string.
146 + */
147 + @Test(expected = IllegalArgumentException.class)
148 + public void testInvalidConstructors() {
149 + // Check constructor for invalid ID: empty string
150 + Ip6Prefix ip6prefix = new Ip6Prefix("");
151 + }
152 +
153 + /**
154 + * Tests getting the value of an address.
155 + */
156 + @Test
157 + public void testGetValue() {
158 + Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8");
159 + assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("1100::")));
160 + assertThat(ip6prefix.getPrefixLen(), is((short) 8));
161 +
162 + ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8");
163 + assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("1100::")));
164 + assertThat(ip6prefix.getPrefixLen(), is((short) 8));
165 +
166 + ip6prefix =
167 + new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8800/120");
168 + assertThat(ip6prefix.getAddress(),
169 + equalTo(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8800")));
170 + assertThat(ip6prefix.getPrefixLen(), is((short) 120));
171 +
172 + ip6prefix = new Ip6Prefix("::/0");
173 + assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("::")));
174 + assertThat(ip6prefix.getPrefixLen(), is((short) 0));
175 +
176 + ip6prefix =
177 + new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/128");
178 + assertThat(ip6prefix.getAddress(),
179 + equalTo(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885")));
180 + assertThat(ip6prefix.getPrefixLen(), is((short) 128));
181 +
182 + ip6prefix =
183 + new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
184 + assertThat(ip6prefix.getAddress(),
185 + equalTo(new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
186 + assertThat(ip6prefix.getPrefixLen(), is((short) 128));
187 +
188 + ip6prefix =
189 + new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/64");
190 + assertThat(ip6prefix.getAddress(),
191 + equalTo(new Ip6Address("1111:2222:3333:4444::")));
192 + assertThat(ip6prefix.getPrefixLen(), is((short) 64));
193 + }
194 +
195 + /**
196 + * Tests equality of {@link Ip6Address}.
197 + */
198 + @Test
199 + public void testEquality() {
200 + Ip6Prefix addr1net = new Ip6Prefix("1100::/8");
201 + Ip6Prefix addr2net = new Ip6Prefix("1100::/8");
202 + assertThat(addr1net, is(addr2net));
203 +
204 + addr1net = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8");
205 + addr2net = new Ip6Prefix("1100::/8");
206 + assertThat(addr1net, is(addr2net));
207 +
208 + addr1net = new Ip6Prefix("::/0");
209 + addr2net = new Ip6Prefix("::/0");
210 + assertThat(addr1net, is(addr2net));
211 +
212 + addr1net =
213 + new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
214 + addr2net =
215 + new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
216 + assertThat(addr1net, is(addr2net));
217 + }
218 +
219 + /**
220 + * Tests non-equality of {@link Ip6Address}.
221 + */
222 + @Test
223 + public void testNonEquality() {
224 + Ip6Prefix addr1net = new Ip6Prefix("1100::/8");
225 + Ip6Prefix addr2net = new Ip6Prefix("1200::/8");
226 + Ip6Prefix addr3net = new Ip6Prefix("1200::/12");
227 + Ip6Prefix addr4net = new Ip6Prefix("::/0");
228 + Ip6Prefix addr5net =
229 + new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
230 + assertThat(addr1net, is(not(addr2net)));
231 + assertThat(addr3net, is(not(addr2net)));
232 + assertThat(addr4net, is(not(addr2net)));
233 + assertThat(addr5net, is(not(addr2net)));
234 + }
235 +
236 + /**
237 + * Tests object string representation.
238 + */
239 + @Test
240 + public void testToString() {
241 + Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8");
242 + assertThat(ip6prefix.toString(), is("1100::/8"));
243 +
244 + ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8");
245 + assertThat(ip6prefix.toString(), is("1100::/8"));
246 +
247 + ip6prefix = new Ip6Prefix("::/0");
248 + assertThat(ip6prefix.toString(), is("::/0"));
249 +
250 + ip6prefix =
251 + new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
252 + assertThat(ip6prefix.toString(),
253 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
254 + }
255 +}