IpPrefixTest.java
3.63 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.onlab.packet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import org.junit.Test;
import org.onlab.packet.IpPrefix.Version;
import com.google.common.testing.EqualsTester;
public class IpPrefixTest {
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() {
IpPrefix ip1 = IpPrefix.valueOf(BYTES1);
IpPrefix ip2 = IpPrefix.valueOf(INTVAL1);
IpPrefix ip3 = IpPrefix.valueOf(BYTES2);
IpPrefix ip4 = IpPrefix.valueOf(INTVAL2);
IpPrefix ip5 = IpPrefix.valueOf(STRVAL);
new EqualsTester().addEqualityGroup(ip1, ip2)
.addEqualityGroup(ip3, ip4)
.addEqualityGroup(ip5)
.testEquals();
// string conversions
IpPrefix ip6 = IpPrefix.valueOf(BYTES1, MASK);
IpPrefix ip7 = IpPrefix.valueOf("10.0.0.10/16");
IpPrefix ip8 = IpPrefix.valueOf(new byte [] {0xa, 0x0, 0x0, 0xc});
assertEquals("incorrect address conversion", ip6, ip7);
assertEquals("incorrect address conversion", ip5, ip8);
}
@Test
public void basics() {
IpPrefix ip1 = IpPrefix.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
IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK);
IpPrefix host = IpPrefix.valueOf("0.0.0.10/16");
IpPrefix network = IpPrefix.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
IpPrefix ip2 = IpPrefix.valueOf(BYTES1);
IpPrefix umhost = IpPrefix.valueOf("10.0.0.10/0");
IpPrefix umnet = IpPrefix.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(IpPrefix.ANY, ip2.netmask().toOctets()));
}
@Test
public void testContains() {
IpPrefix slash31 = IpPrefix.valueOf(BYTES1, 31);
IpPrefix slash32 = IpPrefix.valueOf(BYTES1, 32);
IpPrefix differentSlash32 = IpPrefix.valueOf(BYTES2, 32);
assertTrue(slash31.contains(differentSlash32));
assertFalse(differentSlash32.contains(slash31));
assertTrue(slash31.contains(slash32));
assertFalse(slash32.contains(differentSlash32));
assertFalse(differentSlash32.contains(slash32));
IpPrefix zero = IpPrefix.valueOf("0.0.0.0/0");
assertTrue(zero.contains(differentSlash32));
assertFalse(differentSlash32.contains(zero));
IpPrefix slash8 = IpPrefix.valueOf("10.0.0.0/8");
assertTrue(slash8.contains(slash31));
assertFalse(slash31.contains(slash8));
}
}