blob: c7bbe940db6e61e56e8fbe3267c06e6985948152 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart2da1e602015-02-18 19:09:24 -080016package org.onosproject.routing;
Jonathan Hart41349e92015-02-09 14:14:02 -080017
18import org.hamcrest.Matchers;
19import org.junit.Test;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.Ip4Prefix;
Charles Chand4db3ab2015-09-24 21:14:00 -070022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.Ip6Prefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070024
25import static org.hamcrest.Matchers.is;
26import static org.hamcrest.Matchers.not;
27import static org.junit.Assert.assertThat;
Charles Chand4db3ab2015-09-24 21:14:00 -070028import static org.junit.Assert.assertTrue;
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
Jonathan Hart335ef462014-10-16 08:20:46 -070031
Jonathan Hart335ef462014-10-16 08:20:46 -070032/**
33 * Unit tests for the RouteEntry class.
34 */
35public class RouteEntryTest {
36 /**
37 * Tests valid class constructor.
38 */
39 @Test
40 public void testConstructor() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080041 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
42 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070043 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
44 assertThat(routeEntry.toString(),
45 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
Charles Chand4db3ab2015-09-24 21:14:00 -070046
47 Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
48 Ip6Address nextHop6 = Ip6Address.valueOf("2000::1");
49 RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
50 assertThat(routeEntry6.toString(),
51 is("RouteEntry{prefix=1000::/64, nextHop=2000::1}"));
Jonathan Hart335ef462014-10-16 08:20:46 -070052 }
53
54 /**
55 * Tests invalid class constructor for null IPv4 prefix.
56 */
57 @Test(expected = NullPointerException.class)
Charles Chand4db3ab2015-09-24 21:14:00 -070058 public void testInvalidConstructorNullIpv4Prefix() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080059 Ip4Prefix prefix = null;
60 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070061
62 new RouteEntry(prefix, nextHop);
63 }
64
65 /**
Charles Chand4db3ab2015-09-24 21:14:00 -070066 * Tests invalid class constructor for null IPv6 prefix.
67 */
68 @Test(expected = NullPointerException.class)
69 public void testInvalidConstructorNullIpv6Prefix() {
70 Ip6Prefix prefix = null;
71 Ip6Address nextHop = Ip6Address.valueOf("2000::1");
72
73 new RouteEntry(prefix, nextHop);
74 }
75
76 /**
Jonathan Hart335ef462014-10-16 08:20:46 -070077 * Tests invalid class constructor for null IPv4 next-hop.
78 */
79 @Test(expected = NullPointerException.class)
Charles Chand4db3ab2015-09-24 21:14:00 -070080 public void testInvalidConstructorNullIpv4NextHop() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080081 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
82 Ip4Address nextHop = null;
Jonathan Hart335ef462014-10-16 08:20:46 -070083
84 new RouteEntry(prefix, nextHop);
85 }
86
87 /**
Charles Chand4db3ab2015-09-24 21:14:00 -070088 * Tests invalid class constructor for null IPv6 next-hop.
89 */
90 @Test(expected = NullPointerException.class)
91 public void testInvalidConstructorNullIpv6NextHop() {
92 Ip6Prefix prefix = Ip6Prefix.valueOf("1000::/64");
93 Ip6Address nextHop = null;
94
95 new RouteEntry(prefix, nextHop);
96 }
97
98 /**
Jonathan Hart335ef462014-10-16 08:20:46 -070099 * Tests getting the fields of a route entry.
100 */
101 @Test
102 public void testGetFields() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800103 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
104 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700105 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
106 assertThat(routeEntry.prefix(), is(prefix));
107 assertThat(routeEntry.nextHop(), is(nextHop));
Charles Chand4db3ab2015-09-24 21:14:00 -0700108
109 Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
110 Ip6Address nextHop6 = Ip6Address.valueOf("2000::1");
111 RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
112 assertThat(routeEntry6.prefix(), is(prefix6));
113 assertThat(routeEntry6.nextHop(), is(nextHop6));
Jonathan Hart335ef462014-10-16 08:20:46 -0700114 }
115
116 /**
117 * Tests creating a binary string from IPv4 prefix.
118 */
119 @Test
120 public void testCreateBinaryString() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800121 Ip4Prefix prefix;
Jonathan Hart335ef462014-10-16 08:20:46 -0700122
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800123 prefix = Ip4Prefix.valueOf("0.0.0.0/0");
Pingping Lin92ca4912015-11-19 16:41:54 -0800124 assertThat(RouteEntry.createBinaryString(prefix), is("0"));
Jonathan Hart335ef462014-10-16 08:20:46 -0700125
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800126 prefix = Ip4Prefix.valueOf("192.168.166.0/22");
Jonathan Hart335ef462014-10-16 08:20:46 -0700127 assertThat(RouteEntry.createBinaryString(prefix),
Pingping Lin92ca4912015-11-19 16:41:54 -0800128 is("0" + "1100000010101000101001"));
Jonathan Hart335ef462014-10-16 08:20:46 -0700129
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800130 prefix = Ip4Prefix.valueOf("192.168.166.0/23");
Jonathan Hart335ef462014-10-16 08:20:46 -0700131 assertThat(RouteEntry.createBinaryString(prefix),
Pingping Lin92ca4912015-11-19 16:41:54 -0800132 is("0" + "11000000101010001010011"));
Jonathan Hart335ef462014-10-16 08:20:46 -0700133
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800134 prefix = Ip4Prefix.valueOf("192.168.166.0/24");
Jonathan Hart335ef462014-10-16 08:20:46 -0700135 assertThat(RouteEntry.createBinaryString(prefix),
Pingping Lin92ca4912015-11-19 16:41:54 -0800136 is("0" + "110000001010100010100110"));
Jonathan Hart335ef462014-10-16 08:20:46 -0700137
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800138 prefix = Ip4Prefix.valueOf("130.162.10.1/25");
Jonathan Hart335ef462014-10-16 08:20:46 -0700139 assertThat(RouteEntry.createBinaryString(prefix),
Pingping Lin92ca4912015-11-19 16:41:54 -0800140 is("0" + "1000001010100010000010100"));
Jonathan Hart335ef462014-10-16 08:20:46 -0700141
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800142 prefix = Ip4Prefix.valueOf("255.255.255.255/32");
Jonathan Hart335ef462014-10-16 08:20:46 -0700143 assertThat(RouteEntry.createBinaryString(prefix),
Pingping Lin92ca4912015-11-19 16:41:54 -0800144 is("0" + "11111111111111111111111111111111"));
Charles Chand4db3ab2015-09-24 21:14:00 -0700145
146 Ip6Prefix prefix6;
147 Pattern pattern;
148 Matcher matcher;
149
150 prefix6 = Ip6Prefix.valueOf("::/0");
Pingping Lin92ca4912015-11-19 16:41:54 -0800151 assertThat(RouteEntry.createBinaryString(prefix6), is("0"));
Charles Chand4db3ab2015-09-24 21:14:00 -0700152
153 prefix6 = Ip6Prefix.valueOf("2000::1000/112");
Pingping Lin92ca4912015-11-19 16:41:54 -0800154 pattern = Pattern.compile("0" + "00100{108}");
Charles Chand4db3ab2015-09-24 21:14:00 -0700155 matcher = pattern.matcher(RouteEntry.createBinaryString(prefix6));
156 assertTrue(matcher.matches());
157
158 prefix6 = Ip6Prefix.valueOf("2000::1000/116");
Pingping Lin92ca4912015-11-19 16:41:54 -0800159 pattern = Pattern.compile("0" + "00100{108}0001");
Charles Chand4db3ab2015-09-24 21:14:00 -0700160 matcher = pattern.matcher(RouteEntry.createBinaryString(prefix6));
161 assertTrue(matcher.matches());
162
163 prefix6 = Ip6Prefix.valueOf("2000::2000/116");
Pingping Lin92ca4912015-11-19 16:41:54 -0800164 pattern = Pattern.compile("0" + "00100{108}0010");
Charles Chand4db3ab2015-09-24 21:14:00 -0700165 matcher = pattern.matcher(RouteEntry.createBinaryString(prefix6));
166 assertTrue(matcher.matches());
167
168 prefix6 = Ip6Prefix.valueOf("2000::1234/128");
Pingping Lin92ca4912015-11-19 16:41:54 -0800169 pattern = Pattern.compile("0" + "00100{108}0001001000110100");
Charles Chand4db3ab2015-09-24 21:14:00 -0700170 matcher = pattern.matcher(RouteEntry.createBinaryString(prefix6));
171 assertTrue(matcher.matches());
Jonathan Hart335ef462014-10-16 08:20:46 -0700172 }
173
174 /**
175 * Tests equality of {@link RouteEntry}.
176 */
177 @Test
178 public void testEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800179 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
180 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700181 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
182
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800183 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24");
184 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700185 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
186
187 assertThat(routeEntry1, is(routeEntry2));
Charles Chand4db3ab2015-09-24 21:14:00 -0700188
189 Ip6Prefix prefix3 = Ip6Prefix.valueOf("1000::/64");
190 Ip6Address nextHop3 = Ip6Address.valueOf("2000::2");
191 RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
192
193 Ip6Prefix prefix4 = Ip6Prefix.valueOf("1000::/64");
194 Ip6Address nextHop4 = Ip6Address.valueOf("2000::2");
195 RouteEntry routeEntry4 = new RouteEntry(prefix4, nextHop4);
196
197 assertThat(routeEntry3, is(routeEntry4));
Jonathan Hart335ef462014-10-16 08:20:46 -0700198 }
199
200 /**
201 * Tests non-equality of {@link RouteEntry}.
202 */
203 @Test
204 public void testNonEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800205 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
206 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700207 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
208
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800209 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different
210 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700211 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
212
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800213 Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24");
214 Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different
Jonathan Hart335ef462014-10-16 08:20:46 -0700215 RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
216
Jonathan Hart41349e92015-02-09 14:14:02 -0800217 assertThat(routeEntry1, Matchers.is(not(routeEntry2)));
218 assertThat(routeEntry1, Matchers.is(not(routeEntry3)));
Charles Chand4db3ab2015-09-24 21:14:00 -0700219
220 Ip6Prefix prefix4 = Ip6Prefix.valueOf("1000::/64");
221 Ip6Address nextHop4 = Ip6Address.valueOf("2000::1");
222 RouteEntry routeEntry4 = new RouteEntry(prefix4, nextHop4);
223
224 Ip6Prefix prefix5 = Ip6Prefix.valueOf("1000::/65");
225 Ip6Address nextHop5 = Ip6Address.valueOf("2000::1");
226 RouteEntry routeEntry5 = new RouteEntry(prefix5, nextHop5);
227
228 Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
229 Ip6Address nextHop6 = Ip6Address.valueOf("2000::2");
230 RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
231
232 assertThat(routeEntry4, Matchers.is(not(routeEntry5)));
233 assertThat(routeEntry4, Matchers.is(not(routeEntry6)));
Jonathan Hart335ef462014-10-16 08:20:46 -0700234 }
235
236 /**
237 * Tests object string representation.
238 */
239 @Test
240 public void testToString() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800241 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
242 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700243 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
244
245 assertThat(routeEntry.toString(),
246 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
Charles Chand4db3ab2015-09-24 21:14:00 -0700247
248 Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
249 Ip6Address nextHop6 = Ip6Address.valueOf("2000::1");
250 RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
251
252 assertThat(routeEntry6.toString(),
253 is("RouteEntry{prefix=1000::/64, nextHop=2000::1}"));
Jonathan Hart335ef462014-10-16 08:20:46 -0700254 }
255}