blob: ae3b4426c60655fe68fcbd411bb0243ef2e2a8b1 [file] [log] [blame]
Jonathan Hart8f6dc092014-04-18 15:56:43 -07001package net.onrc.onos.apps.sdnip;
Jonathan Hart8f5f4682013-08-07 22:13:39 +12002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
Jonathan Hartf6978ce2014-06-23 11:20:04 -07005import static org.junit.Assert.assertNotNull;
Jonathan Harta88fd242014-04-03 11:24:54 -07006import static org.junit.Assert.assertTrue;
Jonathan Hart8f5f4682013-08-07 22:13:39 +12007
8import java.util.Arrays;
Jonathan Hartf6978ce2014-06-23 11:20:04 -07009import java.util.LinkedList;
10import java.util.List;
Jonathan Hart8f5f4682013-08-07 22:13:39 +120011
12import org.junit.After;
13import org.junit.Before;
14import org.junit.Test;
15
16public class PrefixTest {
17
Jonathan Hartf6978ce2014-06-23 11:20:04 -070018 private List<PrefixTestData> testPrefixes;
19
20 private class PrefixTestData {
21 public final String dotNotation;
22 public final String binaryNotation;
23 public final int prefixLength;
24
25 public PrefixTestData(String dotNotation, int prefixLength,
26 String binaryNotation) {
27 this.dotNotation = dotNotation;
28 this.prefixLength = prefixLength;
29 this.binaryNotation = binaryNotation;
30 }
31 }
32
Ray Milkey269ffb92014-04-03 14:43:30 -070033 @Before
34 public void setUp() throws Exception {
Jonathan Hartf6978ce2014-06-23 11:20:04 -070035
36 testPrefixes = new LinkedList<>();
37
38 testPrefixes.add(new PrefixTestData("0.0.0.0", 0, ""));
39
40 testPrefixes.add(new PrefixTestData("192.168.166.0", 22,
41 "1100000010101000101001"));
42
43 testPrefixes.add(new PrefixTestData("192.168.166.0", 23,
44 "11000000101010001010011"));
45
46 testPrefixes.add(new PrefixTestData("192.168.166.0", 24,
47 "110000001010100010100110"));
48
49 testPrefixes.add(new PrefixTestData("130.162.10.1", 25,
50 "1000001010100010000010100"));
51
52 testPrefixes.add(new PrefixTestData("255.255.255.255", 32,
53 "11111111111111111111111111111111"));
Ray Milkey269ffb92014-04-03 14:43:30 -070054 }
Jonathan Hart8f5f4682013-08-07 22:13:39 +120055
Ray Milkey269ffb92014-04-03 14:43:30 -070056 @After
57 public void tearDown() throws Exception {
58 }
Jonathan Hart8f5f4682013-08-07 22:13:39 +120059
Ray Milkey269ffb92014-04-03 14:43:30 -070060 @Test
Jonathan Hartf6978ce2014-06-23 11:20:04 -070061 public void testNewPrefixFromByteArray() {
Ray Milkey269ffb92014-04-03 14:43:30 -070062 byte[] b1 = new byte[]{(byte) 0x8f, (byte) 0xa0, (byte) 0x00, (byte) 0x00};
63 byte[] b2 = new byte[]{(byte) 0x8f, (byte) 0xa0, (byte) 0xff, (byte) 0xff};
64 byte[] b3 = new byte[]{(byte) 0x8f, (byte) 0xac, (byte) 0x00, (byte) 0x00};
65 byte[] b4 = new byte[]{(byte) 0x8f, (byte) 0xa0, (byte) 0x00, (byte) 0x00};
Jonathan Hart8f5f4682013-08-07 22:13:39 +120066
Ray Milkey269ffb92014-04-03 14:43:30 -070067 Prefix p1 = new Prefix(b1, 12);
68 Prefix p2 = new Prefix(b2, 12);
69 Prefix p3 = new Prefix(b3, 12);
70 Prefix p4 = new Prefix(b4, 11);
Jonathan Hart8f5f4682013-08-07 22:13:39 +120071
Jonathan Hartf6978ce2014-06-23 11:20:04 -070072 //Have different input byte arrays, but should be equal after construction
Ray Milkey269ffb92014-04-03 14:43:30 -070073 assertTrue(p1.equals(p2));
74 assertTrue(p2.equals(p3));
75
Jonathan Hartf6978ce2014-06-23 11:20:04 -070076 //Same input byte array, but should be false
Ray Milkey269ffb92014-04-03 14:43:30 -070077 assertFalse(p1.equals(p4));
78
79 assertTrue(Arrays.equals(p1.getAddress(), p3.getAddress()));
80 assertTrue(p1.toString().equals(p2.toString()));
81 assertTrue(Arrays.equals(p1.getAddress(), p4.getAddress()));
82 assertFalse(p1.toString().equals(p4.toString()));
83 }
84
85 @Test
86 public void testPrefixString() {
87 Prefix p1 = new Prefix("192.168.166.0", 24);
88 Prefix p2 = new Prefix("192.168.166.0", 23);
89 Prefix p3 = new Prefix("192.168.166.128", 24);
90 Prefix p4 = new Prefix("192.168.166.128", 25);
91
92 assertFalse(p1.equals(p2));
93 assertTrue(Arrays.equals(p1.getAddress(), p2.getAddress()));
94
95 assertTrue(p1.equals(p3));
96 assertTrue(Arrays.equals(p1.getAddress(), p2.getAddress()));
97
98 assertFalse(p3.equals(p4));
99 assertFalse(Arrays.equals(p3.getAddress(), p4.getAddress()));
100
101 assertTrue(p1.toString().equals(p3.toString()));
102 assertEquals(p1.hashCode(), p3.hashCode());
103 }
104
105 @Test
106 public void testPrefixReturnsSame() {
Jonathan Hartf6978ce2014-06-23 11:20:04 -0700107 //Create a prefix of all ones for each prefix length.
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 //Check that Prefix doesn't mangle it
Jonathan Hartf6978ce2014-06-23 11:20:04 -0700109 for (int prefixLength = 1; prefixLength <= Prefix.MAX_PREFIX_LENGTH; prefixLength++) {
110 byte[] address = new byte[Prefix.ADDRESS_LENGTH_BYTES];
Ray Milkey269ffb92014-04-03 14:43:30 -0700111
112 int lastByte = (prefixLength - 1) / Byte.SIZE;
113 int lastBit = (prefixLength - 1) % Byte.SIZE;
114
115 for (int j = 0; j < address.length; j++) {
116 if (j < lastByte) {
117 address[j] = (byte) 0xff;
118 } else if (j == lastByte) {
119 byte b = 0;
120 byte msb = (byte) 0x80;
121 for (int k = 0; k < Byte.SIZE; k++) {
122 if (k <= lastBit) {
123 b |= (msb >> k);
124 }
125 }
126 address[j] = b;
127 } else {
128 address[j] = 0;
129 }
130 }
131
132 Prefix p = new Prefix(address, prefixLength);
Jonathan Hartf6978ce2014-06-23 11:20:04 -0700133
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 assertTrue(Arrays.equals(address, p.getAddress()));
135 }
136 }
Jonathan Hartf6978ce2014-06-23 11:20:04 -0700137
138 @Test
139 public void testToBinaryString() {
140 for (PrefixTestData testPrefix : testPrefixes) {
141 Prefix p = new Prefix(testPrefix.dotNotation, testPrefix.prefixLength);
142 assertEquals(testPrefix.binaryNotation, p.toBinaryString());
143 assertEquals(p.getPrefixLength(), p.toBinaryString().length());
144 }
145 }
146
147 @Test(expected = IllegalArgumentException.class)
148 public void testStringConstructorPrefixLengthTooSmall() {
149 String s = "255.255.255.255";
150
151 // Check the first valid prefix length works
152 Prefix p = new Prefix(s, Prefix.MIN_PREFIX_LENGTH);
153 assertNotNull(p);
154
155 // Should throw IllegalArgumentException
156 new Prefix(s, Prefix.MIN_PREFIX_LENGTH - 1);
157 }
158
159 @Test(expected = IllegalArgumentException.class)
160 public void testStringConstructorPrefixLengthTooLarge() {
161 String s = "255.255.255.255";
162
163 // Check the last valid prefix length works
164 Prefix p = new Prefix(s, Prefix.MAX_PREFIX_LENGTH);
165 assertNotNull(p);
166
167 // Should throw IllegalArgumentException
168 new Prefix(s, Prefix.MAX_PREFIX_LENGTH + 1);
169 }
170
171 @Test(expected = IllegalArgumentException.class)
172 public void testByteConstructorPrefixLengthTooSmall() {
173 byte[] b = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
174
175 // Check the first valid prefix length works
176 Prefix p = new Prefix(b, Prefix.MIN_PREFIX_LENGTH);
177 assertNotNull(p);
178
179 // Should throw IllegalArgumentException
180 new Prefix(b, Prefix.MIN_PREFIX_LENGTH - 1);
181 }
182
183 @Test(expected = IllegalArgumentException.class)
184 public void testByteConstructorPrefixLengthTooLarge() {
185 byte[] b = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
186
187 // Check the last valid prefix length works
188 Prefix p = new Prefix(b, Prefix.MAX_PREFIX_LENGTH);
189 assertNotNull(p);
190
191 // Should throw IllegalArgumentException
192 new Prefix(b, Prefix.MAX_PREFIX_LENGTH + 1);
193 }
Jonathan Hart8f5f4682013-08-07 22:13:39 +1200194}