blob: cf80982ca5b0c67bdc4525f1fff4b6a2fae44632 [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");
37
38 private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("10.2.3.4");
39 private static final IpPrefix SUBNET_ADDRESS2 =
40 IpPrefix.valueOf("10.2.0.0/16");
41 private static final IpAddress BROADCAST_ADDRESS2 =
42 IpAddress.valueOf("10.2.0.255"); // NOTE: non-default broadcast
43 private static final IpAddress PEER_ADDRESS2 =
44 IpAddress.valueOf("50.6.7.8");
45
46 /**
47 * Tests valid class copy constructor.
48 */
49 @Test
50 public void testCopyConstructor() {
51 InterfaceIpAddress fromAddr;
52 InterfaceIpAddress toAddr;
53
54 // Regular interface address with default broadcast address
55 fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
56 toAddr = new InterfaceIpAddress(fromAddr);
Jonathan Hart111b42b2015-07-14 13:28:05 -070057 assertThat(toAddr.ipAddress(), is(fromAddr.ipAddress()));
58 assertThat(toAddr.subnetAddress(), is(fromAddr.subnetAddress()));
59 assertThat(toAddr.broadcastAddress(), is(fromAddr.broadcastAddress()));
60 assertThat(toAddr.peerAddress(), is(fromAddr.peerAddress()));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070061
62 // Interface address with non-default broadcast address
63 fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
64 BROADCAST_ADDRESS);
65 toAddr = new InterfaceIpAddress(fromAddr);
Jonathan Hart111b42b2015-07-14 13:28:05 -070066 assertThat(toAddr.ipAddress(), is(fromAddr.ipAddress()));
67 assertThat(toAddr.subnetAddress(), is(fromAddr.subnetAddress()));
68 assertThat(toAddr.broadcastAddress(), is(fromAddr.broadcastAddress()));
69 assertThat(toAddr.peerAddress(), is(fromAddr.peerAddress()));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070070
71 // Point-to-point address with peer IP address
72 fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
73 PEER_ADDRESS);
74 toAddr = new InterfaceIpAddress(fromAddr);
Jonathan Hart111b42b2015-07-14 13:28:05 -070075 assertThat(toAddr.ipAddress(), is(fromAddr.ipAddress()));
76 assertThat(toAddr.subnetAddress(), is(fromAddr.subnetAddress()));
77 assertThat(toAddr.broadcastAddress(), is(fromAddr.broadcastAddress()));
78 assertThat(toAddr.peerAddress(), is(fromAddr.peerAddress()));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070079 }
80
81 /**
82 * Tests invalid class copy constructor for a null object to copy from.
83 */
84 @Test(expected = NullPointerException.class)
85 public void testInvalidConstructorNullObject() {
86 InterfaceIpAddress fromAddr = null;
87 InterfaceIpAddress toAddr = new InterfaceIpAddress(fromAddr);
88 }
89
90 /**
91 * Tests valid class constructor for regular interface address with
92 * default broadcast address.
93 */
94 @Test
95 public void testConstructorForDefaultBroadcastAddress() {
96 InterfaceIpAddress addr =
97 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
Jonathan Hart111b42b2015-07-14 13:28:05 -070098 assertThat(addr.ipAddress(), is(IP_ADDRESS));
99 assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS));
100 assertThat(addr.broadcastAddress(), nullValue());
101 assertThat(addr.peerAddress(), nullValue());
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700102 }
103
104 /**
105 * Tests valid class constructor for interface address with
106 * non-default broadcast address.
107 */
108 @Test
109 public void testConstructorForNonDefaultBroadcastAddress() {
110 InterfaceIpAddress addr =
111 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
112 BROADCAST_ADDRESS);
Jonathan Hart111b42b2015-07-14 13:28:05 -0700113
114 assertThat(addr.ipAddress(), is(IP_ADDRESS));
115 assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS));
116 assertThat(addr.broadcastAddress(), is(BROADCAST_ADDRESS));
117 assertThat(addr.peerAddress(), nullValue());
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700118 }
119
120 /**
121 * Tests valid class constructor for point-to-point interface address with
122 * peer address.
123 */
124 @Test
125 public void testConstructorForPointToPointAddress() {
126 InterfaceIpAddress addr =
127 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
128 PEER_ADDRESS);
Jonathan Hart111b42b2015-07-14 13:28:05 -0700129
130 assertThat(addr.ipAddress(), is(IP_ADDRESS));
131 assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS));
132 assertThat(addr.broadcastAddress(), nullValue());
133 assertThat(addr.peerAddress(), is(PEER_ADDRESS));
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700134 }
135
136 /**
137 * Tests getting the fields of an interface address.
138 */
139 @Test
140 public void testGetFields() {
141 InterfaceIpAddress addr;
142
143 // Regular interface address with default broadcast address
144 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
145 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
146 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
147 assertThat(addr.broadcastAddress(), is(nullValue())); // TODO: Fix
148 assertThat(addr.peerAddress(), is(nullValue()));
149
150 // Interface address with non-default broadcast address
151 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
152 BROADCAST_ADDRESS);
153 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
154 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
155 assertThat(addr.broadcastAddress().toString(), is("1.2.0.255"));
156 assertThat(addr.peerAddress(), is(nullValue()));
157
158 // Point-to-point address with peer IP address
159 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
160 PEER_ADDRESS);
161 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
162 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
163 assertThat(addr.broadcastAddress(), is(nullValue()));
164 assertThat(addr.peerAddress().toString(), is("5.6.7.8"));
165 }
166
167 /**
168 * Tests equality of {@link InterfaceIpAddress}.
169 */
170 @Test
171 public void testEquality() {
172 InterfaceIpAddress addr1, addr2;
173
174 // Regular interface address with default broadcast address
175 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
176 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
177 assertThat(addr1, is(addr2));
178
179 // Interface address with non-default broadcast address
180 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
181 BROADCAST_ADDRESS);
182 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
183 BROADCAST_ADDRESS);
184 assertThat(addr1, is(addr2));
185
186 // Point-to-point address with peer IP address
187 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
188 PEER_ADDRESS);
189 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
190 PEER_ADDRESS);
191 assertThat(addr1, is(addr2));
192 }
193
194 /**
195 * Tests non-equality of {@link InterfaceIpAddress}.
196 */
197 @Test
198 public void testNonEquality() {
199 InterfaceIpAddress addr1, addr2, addr3, addr4;
200
201 // Regular interface address with default broadcast address
202 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
203 // Interface address with non-default broadcast address
204 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
205 BROADCAST_ADDRESS);
206 // Point-to-point address with peer IP address
207 addr3 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
208 PEER_ADDRESS);
209
210 // Test interface addresses with different properties:
211 // - default-broadcast vs non-default broadcast
212 // - regular vs point-to-point
213 assertThat(addr1, is(not(addr2)));
214 assertThat(addr1, is(not(addr3)));
215 assertThat(addr2, is(not(addr3)));
216
217 // Test regular interface address with default broadcast address
218 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS);
219 assertThat(addr1, is(not(addr4)));
220 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2);
221 assertThat(addr1, is(not(addr4)));
222
223 // Test interface address with non-default broadcast address
224 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS,
225 BROADCAST_ADDRESS);
226 assertThat(addr2, is(not(addr4)));
227 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2,
228 BROADCAST_ADDRESS);
229 assertThat(addr2, is(not(addr4)));
230 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
231 BROADCAST_ADDRESS2);
232 assertThat(addr2, is(not(addr4)));
233
234 // Test point-to-point address with peer IP address
235 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS, null,
236 PEER_ADDRESS);
237 assertThat(addr3, is(not(addr4)));
238 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2, null,
239 PEER_ADDRESS);
240 assertThat(addr3, is(not(addr4)));
241 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
242 PEER_ADDRESS2);
243 assertThat(addr3, is(not(addr4)));
244 }
245
Pavlin Radoslavov276cd902014-10-24 16:28:01 -0700246}