blob: 2dce173dadf24cf4dfd62e8ecbf34d1084310a09 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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
7 *
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.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host;
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070017
18import org.junit.Test;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
21
22import static org.hamcrest.Matchers.is;
23import static org.hamcrest.Matchers.not;
24import static org.hamcrest.Matchers.nullValue;
25import static org.junit.Assert.assertThat;
26
27/**
28 * Tests for class {@link InterfaceIpAddress}.
29 */
30public class InterfaceIpAddressTest {
31 private static final IpAddress IP_ADDRESS = IpAddress.valueOf("1.2.3.4");
32 private static final IpPrefix SUBNET_ADDRESS =
33 IpPrefix.valueOf("1.2.0.0/16");
34 private static final IpAddress BROADCAST_ADDRESS =
35 IpAddress.valueOf("1.2.0.255"); // NOTE: non-default broadcast
36 private static final IpAddress PEER_ADDRESS = IpAddress.valueOf("5.6.7.8");
Srinivas Bandibc38cd42016-08-05 13:46:10 +053037 private static final IpAddress DEF_BROADCAST_ADDRESS =
38 IpAddress.valueOf("1.2.255.255"); // NOTE: default broadcast
39 private static final IpPrefix V6_SUBNET_ADDRESS =
40 IpPrefix.valueOf("::/64");
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070041
42 private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("10.2.3.4");
43 private static final IpPrefix SUBNET_ADDRESS2 =
44 IpPrefix.valueOf("10.2.0.0/16");
45 private static final IpAddress BROADCAST_ADDRESS2 =
46 IpAddress.valueOf("10.2.0.255"); // NOTE: non-default broadcast
47 private static final IpAddress PEER_ADDRESS2 =
48 IpAddress.valueOf("50.6.7.8");
49
50 /**
51 * Tests valid class copy constructor.
52 */
53 @Test
54 public void testCopyConstructor() {
55 InterfaceIpAddress fromAddr;
56 InterfaceIpAddress toAddr;
57
58 // Regular interface address with default broadcast address
59 fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
60 toAddr = new InterfaceIpAddress(fromAddr);
Jonathan Hart111b42b2015-07-14 13:28:05 -070061 assertThat(toAddr.ipAddress(), is(fromAddr.ipAddress()));
62 assertThat(toAddr.subnetAddress(), is(fromAddr.subnetAddress()));
63 assertThat(toAddr.broadcastAddress(), is(fromAddr.broadcastAddress()));
64 assertThat(toAddr.peerAddress(), is(fromAddr.peerAddress()));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070065
66 // Interface address with non-default broadcast address
67 fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
68 BROADCAST_ADDRESS);
69 toAddr = new InterfaceIpAddress(fromAddr);
Jonathan Hart111b42b2015-07-14 13:28:05 -070070 assertThat(toAddr.ipAddress(), is(fromAddr.ipAddress()));
71 assertThat(toAddr.subnetAddress(), is(fromAddr.subnetAddress()));
72 assertThat(toAddr.broadcastAddress(), is(fromAddr.broadcastAddress()));
73 assertThat(toAddr.peerAddress(), is(fromAddr.peerAddress()));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070074
75 // Point-to-point address with peer IP address
76 fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
77 PEER_ADDRESS);
78 toAddr = new InterfaceIpAddress(fromAddr);
Jonathan Hart111b42b2015-07-14 13:28:05 -070079 assertThat(toAddr.ipAddress(), is(fromAddr.ipAddress()));
80 assertThat(toAddr.subnetAddress(), is(fromAddr.subnetAddress()));
81 assertThat(toAddr.broadcastAddress(), is(fromAddr.broadcastAddress()));
82 assertThat(toAddr.peerAddress(), is(fromAddr.peerAddress()));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070083 }
84
85 /**
86 * Tests invalid class copy constructor for a null object to copy from.
87 */
88 @Test(expected = NullPointerException.class)
89 public void testInvalidConstructorNullObject() {
90 InterfaceIpAddress fromAddr = null;
91 InterfaceIpAddress toAddr = new InterfaceIpAddress(fromAddr);
92 }
93
94 /**
95 * Tests valid class constructor for regular interface address with
96 * default broadcast address.
97 */
98 @Test
99 public void testConstructorForDefaultBroadcastAddress() {
100 InterfaceIpAddress addr =
101 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
Jonathan Hart111b42b2015-07-14 13:28:05 -0700102 assertThat(addr.ipAddress(), is(IP_ADDRESS));
103 assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS));
Srinivas Bandibc38cd42016-08-05 13:46:10 +0530104 assertThat(addr.broadcastAddress(), is(DEF_BROADCAST_ADDRESS));
Jonathan Hart111b42b2015-07-14 13:28:05 -0700105 assertThat(addr.peerAddress(), nullValue());
Srinivas Bandibc38cd42016-08-05 13:46:10 +0530106
107 IpPrefix subnetAddr = IpPrefix.valueOf("10.2.3.0/24");
108 InterfaceIpAddress addr1 = new InterfaceIpAddress(IP_ADDRESS2, subnetAddr);
109 assertThat(addr1.broadcastAddress().toString(), is("10.2.3.255"));
110
111 IpAddress ipAddress = IpAddress.valueOf("2001::4");
112 InterfaceIpAddress addr2 = new InterfaceIpAddress(ipAddress, V6_SUBNET_ADDRESS);
113 assertThat(addr2.broadcastAddress(), is(nullValue()));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700114 }
115
116 /**
117 * Tests valid class constructor for interface address with
118 * non-default broadcast address.
119 */
120 @Test
121 public void testConstructorForNonDefaultBroadcastAddress() {
122 InterfaceIpAddress addr =
123 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
124 BROADCAST_ADDRESS);
Jonathan Hart111b42b2015-07-14 13:28:05 -0700125
126 assertThat(addr.ipAddress(), is(IP_ADDRESS));
127 assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS));
128 assertThat(addr.broadcastAddress(), is(BROADCAST_ADDRESS));
129 assertThat(addr.peerAddress(), nullValue());
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700130 }
131
132 /**
133 * Tests valid class constructor for point-to-point interface address with
134 * peer address.
135 */
136 @Test
137 public void testConstructorForPointToPointAddress() {
138 InterfaceIpAddress addr =
139 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
140 PEER_ADDRESS);
Jonathan Hart111b42b2015-07-14 13:28:05 -0700141
142 assertThat(addr.ipAddress(), is(IP_ADDRESS));
143 assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS));
144 assertThat(addr.broadcastAddress(), nullValue());
145 assertThat(addr.peerAddress(), is(PEER_ADDRESS));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700146 }
147
148 /**
149 * Tests getting the fields of an interface address.
150 */
151 @Test
152 public void testGetFields() {
153 InterfaceIpAddress addr;
154
155 // Regular interface address with default broadcast address
156 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
157 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
158 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
Srinivas Bandibc38cd42016-08-05 13:46:10 +0530159 assertThat(addr.broadcastAddress(), is(DEF_BROADCAST_ADDRESS));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700160 assertThat(addr.peerAddress(), is(nullValue()));
161
162 // Interface address with non-default broadcast address
163 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
164 BROADCAST_ADDRESS);
165 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
166 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
167 assertThat(addr.broadcastAddress().toString(), is("1.2.0.255"));
168 assertThat(addr.peerAddress(), is(nullValue()));
169
170 // Point-to-point address with peer IP address
171 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
172 PEER_ADDRESS);
173 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
174 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
175 assertThat(addr.broadcastAddress(), is(nullValue()));
176 assertThat(addr.peerAddress().toString(), is("5.6.7.8"));
177 }
178
179 /**
180 * Tests equality of {@link InterfaceIpAddress}.
181 */
182 @Test
183 public void testEquality() {
184 InterfaceIpAddress addr1, addr2;
185
186 // Regular interface address with default broadcast address
187 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
188 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
189 assertThat(addr1, is(addr2));
190
191 // Interface address with non-default broadcast address
192 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
193 BROADCAST_ADDRESS);
194 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
195 BROADCAST_ADDRESS);
196 assertThat(addr1, is(addr2));
197
198 // Point-to-point address with peer IP address
199 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
200 PEER_ADDRESS);
201 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
202 PEER_ADDRESS);
203 assertThat(addr1, is(addr2));
204 }
205
206 /**
207 * Tests non-equality of {@link InterfaceIpAddress}.
208 */
209 @Test
210 public void testNonEquality() {
211 InterfaceIpAddress addr1, addr2, addr3, addr4;
212
213 // Regular interface address with default broadcast address
214 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
215 // Interface address with non-default broadcast address
216 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
217 BROADCAST_ADDRESS);
218 // Point-to-point address with peer IP address
219 addr3 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
220 PEER_ADDRESS);
221
222 // Test interface addresses with different properties:
223 // - default-broadcast vs non-default broadcast
224 // - regular vs point-to-point
225 assertThat(addr1, is(not(addr2)));
226 assertThat(addr1, is(not(addr3)));
227 assertThat(addr2, is(not(addr3)));
228
229 // Test regular interface address with default broadcast address
230 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS);
231 assertThat(addr1, is(not(addr4)));
232 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2);
233 assertThat(addr1, is(not(addr4)));
234
235 // Test interface address with non-default broadcast address
236 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS,
237 BROADCAST_ADDRESS);
238 assertThat(addr2, is(not(addr4)));
239 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2,
240 BROADCAST_ADDRESS);
241 assertThat(addr2, is(not(addr4)));
242 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
243 BROADCAST_ADDRESS2);
244 assertThat(addr2, is(not(addr4)));
245
246 // Test point-to-point address with peer IP address
247 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS, null,
248 PEER_ADDRESS);
249 assertThat(addr3, is(not(addr4)));
250 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2, null,
251 PEER_ADDRESS);
252 assertThat(addr3, is(not(addr4)));
253 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
254 PEER_ADDRESS2);
255 assertThat(addr3, is(not(addr4)));
256 }
257
Srinivas Bandibc38cd42016-08-05 13:46:10 +0530258 /**
259 * Tests invalid class copy constructor for a null object to copy from.
260 */
261 @Test(expected = IllegalArgumentException.class)
262 public void testIllegalConstructorArgument() {
263 InterfaceIpAddress toAddr = new InterfaceIpAddress(IP_ADDRESS, V6_SUBNET_ADDRESS);
264 }
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700265}