IPAddressTest.java
2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package org.onlab.packet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import org.junit.Test;
import org.onlab.packet.IpAddress.Version;
import com.google.common.testing.EqualsTester;
public class IPAddressTest {
private static final byte [] BYTES1 = new byte [] {0xa, 0x0, 0x0, 0xa};
private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb};
private static final int INTVAL1 = 167772170;
private static final int INTVAL2 = 167772171;
private static final String STRVAL = "10.0.0.12";
private static final int MASK = 16;
@Test
public void testEquality() {
IpAddress ip1 = IpAddress.valueOf(BYTES1);
IpAddress ip2 = IpAddress.valueOf(INTVAL1);
IpAddress ip3 = IpAddress.valueOf(BYTES2);
IpAddress ip4 = IpAddress.valueOf(INTVAL2);
IpAddress ip5 = IpAddress.valueOf(STRVAL);
new EqualsTester().addEqualityGroup(ip1, ip2)
.addEqualityGroup(ip3, ip4)
.addEqualityGroup(ip5)
.testEquals();
// string conversions
IpAddress ip6 = IpAddress.valueOf(BYTES1, MASK);
IpAddress ip7 = IpAddress.valueOf("10.0.0.10/16");
IpAddress ip8 = IpAddress.valueOf(new byte [] {0xa, 0x0, 0x0, 0xc});
assertEquals("incorrect address conversion", ip6, ip7);
assertEquals("incorrect address conversion", ip5, ip8);
}
@Test
public void basics() {
IpAddress ip1 = IpAddress.valueOf(BYTES1, MASK);
final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0xa};
//check fields
assertEquals("incorrect IP Version", Version.INET, ip1.version());
assertEquals("incorrect netmask", 16, ip1.netmask);
assertTrue("faulty toOctets()", Arrays.equals(bytes, ip1.toOctets()));
assertEquals("faulty toInt()", INTVAL1, ip1.toInt());
assertEquals("faulty toString()", "10.0.0.10/16", ip1.toString());
}
@Test
public void netmasks() {
// masked
IpAddress ip1 = IpAddress.valueOf(BYTES1, MASK);
IpAddress host = IpAddress.valueOf("0.0.0.10/16");
IpAddress network = IpAddress.valueOf("10.0.0.0/16");
assertEquals("incorrect host address", host, ip1.host());
assertEquals("incorrect network address", network, ip1.network());
assertEquals("incorrect netmask", "255.255.0.0", ip1.netmask().toString());
//unmasked
IpAddress ip2 = IpAddress.valueOf(BYTES1);
IpAddress umhost = IpAddress.valueOf("10.0.0.10/0");
IpAddress umnet = IpAddress.valueOf("0.0.0.0/0");
assertEquals("incorrect host address", umhost, ip2.host());
assertEquals("incorrect host address", umnet, ip2.network());
assertTrue("incorrect netmask",
Arrays.equals(IpAddress.ANY, ip2.netmask().toOctets()));
}
}