blob: 95982a80149637f586fdf3b475d1d121ee864050 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Jonathan Hartc9e36c52017-01-05 09:53:33 +13002 * Copyright 2017-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 Hartc9e36c52017-01-05 09:53:33 +130016package org.onosproject.routing.bgp;
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;
Jonathan Hart335ef462014-10-16 08:20:46 -070026import static org.junit.Assert.assertThat;
27
Jonathan Hart335ef462014-10-16 08:20:46 -070028/**
29 * Unit tests for the RouteEntry class.
30 */
31public class RouteEntryTest {
Jonathan Hart335ef462014-10-16 08:20:46 -070032
33 /**
34 * Tests invalid class constructor for null IPv4 prefix.
35 */
36 @Test(expected = NullPointerException.class)
Charles Chand4db3ab2015-09-24 21:14:00 -070037 public void testInvalidConstructorNullIpv4Prefix() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080038 Ip4Prefix prefix = null;
39 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070040
41 new RouteEntry(prefix, nextHop);
42 }
43
44 /**
Charles Chand4db3ab2015-09-24 21:14:00 -070045 * Tests invalid class constructor for null IPv6 prefix.
46 */
47 @Test(expected = NullPointerException.class)
48 public void testInvalidConstructorNullIpv6Prefix() {
49 Ip6Prefix prefix = null;
50 Ip6Address nextHop = Ip6Address.valueOf("2000::1");
51
52 new RouteEntry(prefix, nextHop);
53 }
54
55 /**
Jonathan Hart335ef462014-10-16 08:20:46 -070056 * Tests invalid class constructor for null IPv4 next-hop.
57 */
58 @Test(expected = NullPointerException.class)
Charles Chand4db3ab2015-09-24 21:14:00 -070059 public void testInvalidConstructorNullIpv4NextHop() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080060 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
61 Ip4Address nextHop = null;
Jonathan Hart335ef462014-10-16 08:20:46 -070062
63 new RouteEntry(prefix, nextHop);
64 }
65
66 /**
Charles Chand4db3ab2015-09-24 21:14:00 -070067 * Tests invalid class constructor for null IPv6 next-hop.
68 */
69 @Test(expected = NullPointerException.class)
70 public void testInvalidConstructorNullIpv6NextHop() {
71 Ip6Prefix prefix = Ip6Prefix.valueOf("1000::/64");
72 Ip6Address nextHop = null;
73
74 new RouteEntry(prefix, nextHop);
75 }
76
77 /**
Jonathan Hart335ef462014-10-16 08:20:46 -070078 * Tests getting the fields of a route entry.
79 */
80 @Test
81 public void testGetFields() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080082 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
83 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070084 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
85 assertThat(routeEntry.prefix(), is(prefix));
86 assertThat(routeEntry.nextHop(), is(nextHop));
Charles Chand4db3ab2015-09-24 21:14:00 -070087
88 Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
89 Ip6Address nextHop6 = Ip6Address.valueOf("2000::1");
90 RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
91 assertThat(routeEntry6.prefix(), is(prefix6));
92 assertThat(routeEntry6.nextHop(), is(nextHop6));
Jonathan Hart335ef462014-10-16 08:20:46 -070093 }
94
95 /**
Jonathan Hart335ef462014-10-16 08:20:46 -070096 * Tests equality of {@link RouteEntry}.
97 */
98 @Test
99 public void testEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800100 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
101 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700102 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
103
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800104 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24");
105 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700106 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
107
108 assertThat(routeEntry1, is(routeEntry2));
Charles Chand4db3ab2015-09-24 21:14:00 -0700109
110 Ip6Prefix prefix3 = Ip6Prefix.valueOf("1000::/64");
111 Ip6Address nextHop3 = Ip6Address.valueOf("2000::2");
112 RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
113
114 Ip6Prefix prefix4 = Ip6Prefix.valueOf("1000::/64");
115 Ip6Address nextHop4 = Ip6Address.valueOf("2000::2");
116 RouteEntry routeEntry4 = new RouteEntry(prefix4, nextHop4);
117
118 assertThat(routeEntry3, is(routeEntry4));
Jonathan Hart335ef462014-10-16 08:20:46 -0700119 }
120
121 /**
122 * Tests non-equality of {@link RouteEntry}.
123 */
124 @Test
125 public void testNonEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800126 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
127 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700128 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
129
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800130 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different
131 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700132 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
133
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800134 Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24");
135 Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different
Jonathan Hart335ef462014-10-16 08:20:46 -0700136 RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
137
Jonathan Hartc9e36c52017-01-05 09:53:33 +1300138 assertThat(routeEntry1, Matchers.is(Matchers.not(routeEntry2)));
139 assertThat(routeEntry1, Matchers.is(Matchers.not(routeEntry3)));
Charles Chand4db3ab2015-09-24 21:14:00 -0700140
141 Ip6Prefix prefix4 = Ip6Prefix.valueOf("1000::/64");
142 Ip6Address nextHop4 = Ip6Address.valueOf("2000::1");
143 RouteEntry routeEntry4 = new RouteEntry(prefix4, nextHop4);
144
145 Ip6Prefix prefix5 = Ip6Prefix.valueOf("1000::/65");
146 Ip6Address nextHop5 = Ip6Address.valueOf("2000::1");
147 RouteEntry routeEntry5 = new RouteEntry(prefix5, nextHop5);
148
149 Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
150 Ip6Address nextHop6 = Ip6Address.valueOf("2000::2");
151 RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
152
Jonathan Hartc9e36c52017-01-05 09:53:33 +1300153 assertThat(routeEntry4, Matchers.is(Matchers.not(routeEntry5)));
154 assertThat(routeEntry4, Matchers.is(Matchers.not(routeEntry6)));
Jonathan Hart335ef462014-10-16 08:20:46 -0700155 }
156
157 /**
158 * Tests object string representation.
159 */
160 @Test
161 public void testToString() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800162 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
163 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700164 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
165
166 assertThat(routeEntry.toString(),
167 is("RouteEntry{prefix=1.2.3.0/24, nextHop=5.6.7.8}"));
Charles Chand4db3ab2015-09-24 21:14:00 -0700168
169 Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
170 Ip6Address nextHop6 = Ip6Address.valueOf("2000::1");
171 RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
172
173 assertThat(routeEntry6.toString(),
174 is("RouteEntry{prefix=1000::/64, nextHop=2000::1}"));
Jonathan Hart335ef462014-10-16 08:20:46 -0700175 }
176}