Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
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 | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 16 | |
Jonathan Hart | c9e36c5 | 2017-01-05 09:53:33 +1300 | [diff] [blame] | 17 | package org.onosproject.routing.bgp; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 18 | |
| 19 | import org.hamcrest.Matchers; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.Ip4Prefix; |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ip6Address; |
| 24 | import org.onlab.packet.Ip6Prefix; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 25 | |
| 26 | import static org.hamcrest.Matchers.is; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertThat; |
| 28 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Unit tests for the RouteEntry class. |
| 31 | */ |
| 32 | public class RouteEntryTest { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Tests invalid class constructor for null IPv4 prefix. |
| 36 | */ |
| 37 | @Test(expected = NullPointerException.class) |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 38 | public void testInvalidConstructorNullIpv4Prefix() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 39 | Ip4Prefix prefix = null; |
| 40 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 41 | |
| 42 | new RouteEntry(prefix, nextHop); |
| 43 | } |
| 44 | |
| 45 | /** |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 46 | * Tests invalid class constructor for null IPv6 prefix. |
| 47 | */ |
| 48 | @Test(expected = NullPointerException.class) |
| 49 | public void testInvalidConstructorNullIpv6Prefix() { |
| 50 | Ip6Prefix prefix = null; |
| 51 | Ip6Address nextHop = Ip6Address.valueOf("2000::1"); |
| 52 | |
| 53 | new RouteEntry(prefix, nextHop); |
| 54 | } |
| 55 | |
| 56 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 57 | * Tests invalid class constructor for null IPv4 next-hop. |
| 58 | */ |
| 59 | @Test(expected = NullPointerException.class) |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 60 | public void testInvalidConstructorNullIpv4NextHop() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 61 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 62 | Ip4Address nextHop = null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 63 | |
| 64 | new RouteEntry(prefix, nextHop); |
| 65 | } |
| 66 | |
| 67 | /** |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 68 | * Tests invalid class constructor for null IPv6 next-hop. |
| 69 | */ |
| 70 | @Test(expected = NullPointerException.class) |
| 71 | public void testInvalidConstructorNullIpv6NextHop() { |
| 72 | Ip6Prefix prefix = Ip6Prefix.valueOf("1000::/64"); |
| 73 | Ip6Address nextHop = null; |
| 74 | |
| 75 | new RouteEntry(prefix, nextHop); |
| 76 | } |
| 77 | |
| 78 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 79 | * Tests getting the fields of a route entry. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testGetFields() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 83 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 84 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 85 | RouteEntry routeEntry = new RouteEntry(prefix, nextHop); |
| 86 | assertThat(routeEntry.prefix(), is(prefix)); |
| 87 | assertThat(routeEntry.nextHop(), is(nextHop)); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 88 | |
| 89 | Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64"); |
| 90 | Ip6Address nextHop6 = Ip6Address.valueOf("2000::1"); |
| 91 | RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6); |
| 92 | assertThat(routeEntry6.prefix(), is(prefix6)); |
| 93 | assertThat(routeEntry6.nextHop(), is(nextHop6)); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 97 | * Tests equality of {@link RouteEntry}. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testEquality() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 101 | Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 102 | Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 103 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 104 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 105 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 106 | Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 107 | RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2); |
| 108 | |
| 109 | assertThat(routeEntry1, is(routeEntry2)); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 110 | |
| 111 | Ip6Prefix prefix3 = Ip6Prefix.valueOf("1000::/64"); |
| 112 | Ip6Address nextHop3 = Ip6Address.valueOf("2000::2"); |
| 113 | RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3); |
| 114 | |
| 115 | Ip6Prefix prefix4 = Ip6Prefix.valueOf("1000::/64"); |
| 116 | Ip6Address nextHop4 = Ip6Address.valueOf("2000::2"); |
| 117 | RouteEntry routeEntry4 = new RouteEntry(prefix4, nextHop4); |
| 118 | |
| 119 | assertThat(routeEntry3, is(routeEntry4)); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Tests non-equality of {@link RouteEntry}. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testNonEquality() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 127 | Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 128 | Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 129 | RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1); |
| 130 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 131 | Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different |
| 132 | Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 133 | RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2); |
| 134 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 135 | Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 136 | Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 137 | RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3); |
| 138 | |
Jonathan Hart | c9e36c5 | 2017-01-05 09:53:33 +1300 | [diff] [blame] | 139 | assertThat(routeEntry1, Matchers.is(Matchers.not(routeEntry2))); |
| 140 | assertThat(routeEntry1, Matchers.is(Matchers.not(routeEntry3))); |
Charles Chan | d4db3ab | 2015-09-24 21:14:00 -0700 | [diff] [blame] | 141 | |
| 142 | Ip6Prefix prefix4 = Ip6Prefix.valueOf("1000::/64"); |
| 143 | Ip6Address nextHop4 = Ip6Address.valueOf("2000::1"); |
| 144 | RouteEntry routeEntry4 = new RouteEntry(prefix4, nextHop4); |
| 145 | |
| 146 | Ip6Prefix prefix5 = Ip6Prefix.valueOf("1000::/65"); |
| 147 | Ip6Address nextHop5 = Ip6Address.valueOf("2000::1"); |
| 148 | RouteEntry routeEntry5 = new RouteEntry(prefix5, nextHop5); |
| 149 | |
| 150 | Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64"); |
| 151 | Ip6Address nextHop6 = Ip6Address.valueOf("2000::2"); |
| 152 | RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6); |
| 153 | |
Jonathan Hart | c9e36c5 | 2017-01-05 09:53:33 +1300 | [diff] [blame] | 154 | assertThat(routeEntry4, Matchers.is(Matchers.not(routeEntry5))); |
| 155 | assertThat(routeEntry4, Matchers.is(Matchers.not(routeEntry6))); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Tests object string representation. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testToString() { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 163 | Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 164 | Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 165 | RouteEntry routeEntry = new RouteEntry(prefix, nextHop); |
| 166 | |
| 167 | assertThat(routeEntry.toString(), |
| 168 | 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] | 169 | |
| 170 | Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64"); |
| 171 | Ip6Address nextHop6 = Ip6Address.valueOf("2000::1"); |
| 172 | RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6); |
| 173 | |
| 174 | assertThat(routeEntry6.toString(), |
| 175 | is("RouteEntry{prefix=1000::/64, nextHop=2000::1}")); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 176 | } |
| 177 | } |