Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 16 | package org.onosproject.routing; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 17 | |
| 18 | import org.hamcrest.Matchers; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.Ip4Prefix; |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 22 | import org.onlab.packet.Ip6Address; |
| 23 | import org.onlab.packet.Ip6Prefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 24 | |
| 25 | import static org.hamcrest.Matchers.is; |
| 26 | import static org.hamcrest.Matchers.not; |
| 27 | import static org.junit.Assert.assertThat; |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 28 | import static org.junit.Assert.assertTrue; |
| 29 | import java.util.regex.Matcher; |
| 30 | import java.util.regex.Pattern; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 31 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Unit tests for the RouteEntry class. |
| 34 | */ |
| 35 | public class RouteEntryTest { |
| 36 | /** |
| 37 | * Tests valid class constructor. |
| 38 | */ |
| 39 | @Test |
| 40 | public void testConstructor() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 41 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 42 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 43 | 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 Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 46 | |
| 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 Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests invalid class constructor for null IPv4 prefix. |
| 56 | */ |
| 57 | @Test(expected = NullPointerException.class) |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 58 | public void testInvalidConstructorNullIpv4Prefix() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 59 | Ip4Prefix prefix = null; |
| 60 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 61 | |
| 62 | new RouteEntry(prefix, nextHop); |
| 63 | } |
| 64 | |
| 65 | /** |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 66 | * 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 Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 77 | * Tests invalid class constructor for null IPv4 next-hop. |
| 78 | */ |
| 79 | @Test(expected = NullPointerException.class) |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 80 | public void testInvalidConstructorNullIpv4NextHop() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 81 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 82 | Ip4Address nextHop = null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 83 | |
| 84 | new RouteEntry(prefix, nextHop); |
| 85 | } |
| 86 | |
| 87 | /** |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 88 | * 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 Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 99 | * Tests getting the fields of a route entry. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testGetFields() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 103 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 104 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 105 | RouteEntry routeEntry = new RouteEntry(prefix, nextHop); |
| 106 | assertThat(routeEntry.prefix(), is(prefix)); |
| 107 | assertThat(routeEntry.nextHop(), is(nextHop)); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 108 | |
| 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 Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Tests creating a binary string from IPv4 prefix. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testCreateBinaryString() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 121 | Ip4Prefix prefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 122 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 123 | prefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 124 | assertThat(RouteEntry.createBinaryString(prefix), is("0")); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 125 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 126 | prefix = Ip4Prefix.valueOf("192.168.166.0/22"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 127 | assertThat(RouteEntry.createBinaryString(prefix), |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 128 | is("0" + "1100000010101000101001")); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 129 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 130 | prefix = Ip4Prefix.valueOf("192.168.166.0/23"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 131 | assertThat(RouteEntry.createBinaryString(prefix), |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 132 | is("0" + "11000000101010001010011")); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 133 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 134 | prefix = Ip4Prefix.valueOf("192.168.166.0/24"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 135 | assertThat(RouteEntry.createBinaryString(prefix), |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 136 | is("0" + "110000001010100010100110")); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 137 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 138 | prefix = Ip4Prefix.valueOf("130.162.10.1/25"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 139 | assertThat(RouteEntry.createBinaryString(prefix), |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 140 | is("0" + "1000001010100010000010100")); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 141 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 142 | prefix = Ip4Prefix.valueOf("255.255.255.255/32"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 143 | assertThat(RouteEntry.createBinaryString(prefix), |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 144 | is("0" + "11111111111111111111111111111111")); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 145 | |
| 146 | Ip6Prefix prefix6; |
| 147 | Pattern pattern; |
| 148 | Matcher matcher; |
| 149 | |
| 150 | prefix6 = Ip6Prefix.valueOf("::/0"); |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 151 | assertThat(RouteEntry.createBinaryString(prefix6), is("0")); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 152 | |
| 153 | prefix6 = Ip6Prefix.valueOf("2000::1000/112"); |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 154 | pattern = Pattern.compile("0" + "00100{108}"); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 155 | matcher = pattern.matcher(RouteEntry.createBinaryString(prefix6)); |
| 156 | assertTrue(matcher.matches()); |
| 157 | |
| 158 | prefix6 = Ip6Prefix.valueOf("2000::1000/116"); |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 159 | pattern = Pattern.compile("0" + "00100{108}0001"); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 160 | matcher = pattern.matcher(RouteEntry.createBinaryString(prefix6)); |
| 161 | assertTrue(matcher.matches()); |
| 162 | |
| 163 | prefix6 = Ip6Prefix.valueOf("2000::2000/116"); |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 164 | pattern = Pattern.compile("0" + "00100{108}0010"); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 165 | matcher = pattern.matcher(RouteEntry.createBinaryString(prefix6)); |
| 166 | assertTrue(matcher.matches()); |
| 167 | |
| 168 | prefix6 = Ip6Prefix.valueOf("2000::1234/128"); |
Pingping Lin | 92ca491 | 2015-11-19 16:41:54 -0800 | [diff] [blame] | 169 | pattern = Pattern.compile("0" + "00100{108}0001001000110100"); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 170 | matcher = pattern.matcher(RouteEntry.createBinaryString(prefix6)); |
| 171 | assertTrue(matcher.matches()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Tests equality of {@link RouteEntry}. |
| 176 | */ |
| 177 | @Test |
| 178 | public void testEquality() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 179 | Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 180 | Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 181 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 182 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 183 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 184 | Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 185 | RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2); |
| 186 | |
| 187 | assertThat(routeEntry1, is(routeEntry2)); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 188 | |
| 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 Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Tests non-equality of {@link RouteEntry}. |
| 202 | */ |
| 203 | @Test |
| 204 | public void testNonEquality() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 205 | Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 206 | Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 207 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 208 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 209 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different |
| 210 | Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 211 | RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2); |
| 212 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 213 | Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 214 | Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 215 | RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3); |
| 216 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 217 | assertThat(routeEntry1, Matchers.is(not(routeEntry2))); |
| 218 | assertThat(routeEntry1, Matchers.is(not(routeEntry3))); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 219 | |
| 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 Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Tests object string representation. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testToString() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 241 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 242 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 243 | 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 Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 247 | |
| 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 Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 254 | } |
| 255 | } |