blob: 2873e6fd3c7232eb393405dda2636d79b906e432 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
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 Hartf4bd0482017-01-27 15:11:18 -080016
Jonathan Hartc9e36c52017-01-05 09:53:33 +130017package org.onosproject.routing.bgp;
Jonathan Hart41349e92015-02-09 14:14:02 -080018
19import org.hamcrest.Matchers;
20import org.junit.Test;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.Ip4Prefix;
Charles Chand4db3ab2015-09-24 21:14:00 -070023import org.onlab.packet.Ip6Address;
24import org.onlab.packet.Ip6Prefix;
Jonathan Hart335ef462014-10-16 08:20:46 -070025
26import static org.hamcrest.Matchers.is;
Jonathan Hart335ef462014-10-16 08:20:46 -070027import static org.junit.Assert.assertThat;
28
Jonathan Hart335ef462014-10-16 08:20:46 -070029/**
30 * Unit tests for the RouteEntry class.
31 */
32public class RouteEntryTest {
Jonathan Hart335ef462014-10-16 08:20:46 -070033
34 /**
35 * Tests invalid class constructor for null IPv4 prefix.
36 */
37 @Test(expected = NullPointerException.class)
Charles Chand4db3ab2015-09-24 21:14:00 -070038 public void testInvalidConstructorNullIpv4Prefix() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080039 Ip4Prefix prefix = null;
40 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070041
42 new RouteEntry(prefix, nextHop);
43 }
44
45 /**
Charles Chand4db3ab2015-09-24 21:14:00 -070046 * 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 Hart335ef462014-10-16 08:20:46 -070057 * Tests invalid class constructor for null IPv4 next-hop.
58 */
59 @Test(expected = NullPointerException.class)
Charles Chand4db3ab2015-09-24 21:14:00 -070060 public void testInvalidConstructorNullIpv4NextHop() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080061 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
62 Ip4Address nextHop = null;
Jonathan Hart335ef462014-10-16 08:20:46 -070063
64 new RouteEntry(prefix, nextHop);
65 }
66
67 /**
Charles Chand4db3ab2015-09-24 21:14:00 -070068 * 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 Hart335ef462014-10-16 08:20:46 -070079 * Tests getting the fields of a route entry.
80 */
81 @Test
82 public void testGetFields() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080083 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
84 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -070085 RouteEntry routeEntry = new RouteEntry(prefix, nextHop);
86 assertThat(routeEntry.prefix(), is(prefix));
87 assertThat(routeEntry.nextHop(), is(nextHop));
Charles Chand4db3ab2015-09-24 21:14:00 -070088
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 Hart335ef462014-10-16 08:20:46 -070094 }
95
96 /**
Jonathan Hart335ef462014-10-16 08:20:46 -070097 * Tests equality of {@link RouteEntry}.
98 */
99 @Test
100 public void testEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800101 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
102 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700103 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
104
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800105 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/24");
106 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700107 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
108
109 assertThat(routeEntry1, is(routeEntry2));
Charles Chand4db3ab2015-09-24 21:14:00 -0700110
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 Hart335ef462014-10-16 08:20:46 -0700120 }
121
122 /**
123 * Tests non-equality of {@link RouteEntry}.
124 */
125 @Test
126 public void testNonEquality() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800127 Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
128 Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700129 RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
130
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800131 Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25"); // Different
132 Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700133 RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
134
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800135 Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24");
136 Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9"); // Different
Jonathan Hart335ef462014-10-16 08:20:46 -0700137 RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
138
Jonathan Hartc9e36c52017-01-05 09:53:33 +1300139 assertThat(routeEntry1, Matchers.is(Matchers.not(routeEntry2)));
140 assertThat(routeEntry1, Matchers.is(Matchers.not(routeEntry3)));
Charles Chand4db3ab2015-09-24 21:14:00 -0700141
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 Hartc9e36c52017-01-05 09:53:33 +1300154 assertThat(routeEntry4, Matchers.is(Matchers.not(routeEntry5)));
155 assertThat(routeEntry4, Matchers.is(Matchers.not(routeEntry6)));
Jonathan Hart335ef462014-10-16 08:20:46 -0700156 }
157
158 /**
159 * Tests object string representation.
160 */
161 @Test
162 public void testToString() {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800163 Ip4Prefix prefix = Ip4Prefix.valueOf("1.2.3.0/24");
164 Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
Jonathan Hart335ef462014-10-16 08:20:46 -0700165 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 Chand4db3ab2015-09-24 21:14:00 -0700169
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 Hart335ef462014-10-16 08:20:46 -0700176 }
177}