blob: c18fe06ca4d8360f9b4b314f84ef5c496469d7ed [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
Pavlin Radoslavov276cd902014-10-24 16:28:01 -070016package org.onlab.onos.net.host;
17
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);
57 assertThat(toAddr.toString(),
58 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}"));
59
60 // Interface address with non-default broadcast address
61 fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
62 BROADCAST_ADDRESS);
63 toAddr = new InterfaceIpAddress(fromAddr);
64 assertThat(toAddr.toString(),
65 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}"));
66
67 // Point-to-point address with peer IP address
68 fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
69 PEER_ADDRESS);
70 toAddr = new InterfaceIpAddress(fromAddr);
71 assertThat(toAddr.toString(),
72 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}"));
73 }
74
75 /**
76 * Tests invalid class copy constructor for a null object to copy from.
77 */
78 @Test(expected = NullPointerException.class)
79 public void testInvalidConstructorNullObject() {
80 InterfaceIpAddress fromAddr = null;
81 InterfaceIpAddress toAddr = new InterfaceIpAddress(fromAddr);
82 }
83
84 /**
85 * Tests valid class constructor for regular interface address with
86 * default broadcast address.
87 */
88 @Test
89 public void testConstructorForDefaultBroadcastAddress() {
90 InterfaceIpAddress addr =
91 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
92 assertThat(addr.toString(),
93 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}"));
94 }
95
96 /**
97 * Tests valid class constructor for interface address with
98 * non-default broadcast address.
99 */
100 @Test
101 public void testConstructorForNonDefaultBroadcastAddress() {
102 InterfaceIpAddress addr =
103 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
104 BROADCAST_ADDRESS);
105 assertThat(addr.toString(),
106 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}"));
107 }
108
109 /**
110 * Tests valid class constructor for point-to-point interface address with
111 * peer address.
112 */
113 @Test
114 public void testConstructorForPointToPointAddress() {
115 InterfaceIpAddress addr =
116 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
117 PEER_ADDRESS);
118 assertThat(addr.toString(),
119 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}"));
120 }
121
122 /**
123 * Tests getting the fields of an interface address.
124 */
125 @Test
126 public void testGetFields() {
127 InterfaceIpAddress addr;
128
129 // Regular interface address with default broadcast address
130 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
131 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
132 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
133 assertThat(addr.broadcastAddress(), is(nullValue())); // TODO: Fix
134 assertThat(addr.peerAddress(), is(nullValue()));
135
136 // Interface address with non-default broadcast address
137 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
138 BROADCAST_ADDRESS);
139 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
140 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
141 assertThat(addr.broadcastAddress().toString(), is("1.2.0.255"));
142 assertThat(addr.peerAddress(), is(nullValue()));
143
144 // Point-to-point address with peer IP address
145 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
146 PEER_ADDRESS);
147 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
148 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
149 assertThat(addr.broadcastAddress(), is(nullValue()));
150 assertThat(addr.peerAddress().toString(), is("5.6.7.8"));
151 }
152
153 /**
154 * Tests equality of {@link InterfaceIpAddress}.
155 */
156 @Test
157 public void testEquality() {
158 InterfaceIpAddress addr1, addr2;
159
160 // Regular interface address with default broadcast address
161 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
162 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
163 assertThat(addr1, is(addr2));
164
165 // Interface address with non-default broadcast address
166 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
167 BROADCAST_ADDRESS);
168 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
169 BROADCAST_ADDRESS);
170 assertThat(addr1, is(addr2));
171
172 // Point-to-point address with peer IP address
173 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
174 PEER_ADDRESS);
175 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
176 PEER_ADDRESS);
177 assertThat(addr1, is(addr2));
178 }
179
180 /**
181 * Tests non-equality of {@link InterfaceIpAddress}.
182 */
183 @Test
184 public void testNonEquality() {
185 InterfaceIpAddress addr1, addr2, addr3, addr4;
186
187 // Regular interface address with default broadcast address
188 addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
189 // Interface address with non-default broadcast address
190 addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
191 BROADCAST_ADDRESS);
192 // Point-to-point address with peer IP address
193 addr3 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
194 PEER_ADDRESS);
195
196 // Test interface addresses with different properties:
197 // - default-broadcast vs non-default broadcast
198 // - regular vs point-to-point
199 assertThat(addr1, is(not(addr2)));
200 assertThat(addr1, is(not(addr3)));
201 assertThat(addr2, is(not(addr3)));
202
203 // Test regular interface address with default broadcast address
204 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS);
205 assertThat(addr1, is(not(addr4)));
206 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2);
207 assertThat(addr1, is(not(addr4)));
208
209 // Test interface address with non-default broadcast address
210 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS,
211 BROADCAST_ADDRESS);
212 assertThat(addr2, is(not(addr4)));
213 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2,
214 BROADCAST_ADDRESS);
215 assertThat(addr2, is(not(addr4)));
216 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
217 BROADCAST_ADDRESS2);
218 assertThat(addr2, is(not(addr4)));
219
220 // Test point-to-point address with peer IP address
221 addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS, null,
222 PEER_ADDRESS);
223 assertThat(addr3, is(not(addr4)));
224 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2, null,
225 PEER_ADDRESS);
226 assertThat(addr3, is(not(addr4)));
227 addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
228 PEER_ADDRESS2);
229 assertThat(addr3, is(not(addr4)));
230 }
231
232 /**
233 * Tests object string representation.
234 */
235 @Test
236 public void testToString() {
237 InterfaceIpAddress addr;
238
239 // Regular interface address with default broadcast address
240 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
241 assertThat(addr.toString(),
242 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}"));
243
244 // Interface address with non-default broadcast address
245 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS,
246 BROADCAST_ADDRESS);
247 assertThat(addr.toString(),
248 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}"));
249
250 // Point-to-point address with peer IP address
251 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null,
252 PEER_ADDRESS);
253 assertThat(addr.toString(),
254 is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}"));
255 }
256}