Pavlin Radoslavov | 276cd90 | 2014-10-24 16:28:01 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.host; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | import org.onlab.packet.IpAddress; |
| 5 | import org.onlab.packet.IpPrefix; |
| 6 | |
| 7 | import static org.hamcrest.Matchers.is; |
| 8 | import static org.hamcrest.Matchers.not; |
| 9 | import static org.hamcrest.Matchers.nullValue; |
| 10 | import static org.junit.Assert.assertThat; |
| 11 | |
| 12 | /** |
| 13 | * Tests for class {@link InterfaceIpAddress}. |
| 14 | */ |
| 15 | public class InterfaceIpAddressTest { |
| 16 | private static final IpAddress IP_ADDRESS = IpAddress.valueOf("1.2.3.4"); |
| 17 | private static final IpPrefix SUBNET_ADDRESS = |
| 18 | IpPrefix.valueOf("1.2.0.0/16"); |
| 19 | private static final IpAddress BROADCAST_ADDRESS = |
| 20 | IpAddress.valueOf("1.2.0.255"); // NOTE: non-default broadcast |
| 21 | private static final IpAddress PEER_ADDRESS = IpAddress.valueOf("5.6.7.8"); |
| 22 | |
| 23 | private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("10.2.3.4"); |
| 24 | private static final IpPrefix SUBNET_ADDRESS2 = |
| 25 | IpPrefix.valueOf("10.2.0.0/16"); |
| 26 | private static final IpAddress BROADCAST_ADDRESS2 = |
| 27 | IpAddress.valueOf("10.2.0.255"); // NOTE: non-default broadcast |
| 28 | private static final IpAddress PEER_ADDRESS2 = |
| 29 | IpAddress.valueOf("50.6.7.8"); |
| 30 | |
| 31 | /** |
| 32 | * Tests valid class copy constructor. |
| 33 | */ |
| 34 | @Test |
| 35 | public void testCopyConstructor() { |
| 36 | InterfaceIpAddress fromAddr; |
| 37 | InterfaceIpAddress toAddr; |
| 38 | |
| 39 | // Regular interface address with default broadcast address |
| 40 | fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); |
| 41 | toAddr = new InterfaceIpAddress(fromAddr); |
| 42 | assertThat(toAddr.toString(), |
| 43 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}")); |
| 44 | |
| 45 | // Interface address with non-default broadcast address |
| 46 | fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, |
| 47 | BROADCAST_ADDRESS); |
| 48 | toAddr = new InterfaceIpAddress(fromAddr); |
| 49 | assertThat(toAddr.toString(), |
| 50 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}")); |
| 51 | |
| 52 | // Point-to-point address with peer IP address |
| 53 | fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, |
| 54 | PEER_ADDRESS); |
| 55 | toAddr = new InterfaceIpAddress(fromAddr); |
| 56 | assertThat(toAddr.toString(), |
| 57 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}")); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Tests invalid class copy constructor for a null object to copy from. |
| 62 | */ |
| 63 | @Test(expected = NullPointerException.class) |
| 64 | public void testInvalidConstructorNullObject() { |
| 65 | InterfaceIpAddress fromAddr = null; |
| 66 | InterfaceIpAddress toAddr = new InterfaceIpAddress(fromAddr); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests valid class constructor for regular interface address with |
| 71 | * default broadcast address. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testConstructorForDefaultBroadcastAddress() { |
| 75 | InterfaceIpAddress addr = |
| 76 | new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); |
| 77 | assertThat(addr.toString(), |
| 78 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}")); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests valid class constructor for interface address with |
| 83 | * non-default broadcast address. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testConstructorForNonDefaultBroadcastAddress() { |
| 87 | InterfaceIpAddress addr = |
| 88 | new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, |
| 89 | BROADCAST_ADDRESS); |
| 90 | assertThat(addr.toString(), |
| 91 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}")); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests valid class constructor for point-to-point interface address with |
| 96 | * peer address. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testConstructorForPointToPointAddress() { |
| 100 | InterfaceIpAddress addr = |
| 101 | new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, |
| 102 | PEER_ADDRESS); |
| 103 | assertThat(addr.toString(), |
| 104 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}")); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Tests getting the fields of an interface address. |
| 109 | */ |
| 110 | @Test |
| 111 | public void testGetFields() { |
| 112 | InterfaceIpAddress addr; |
| 113 | |
| 114 | // Regular interface address with default broadcast address |
| 115 | addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); |
| 116 | assertThat(addr.ipAddress().toString(), is("1.2.3.4")); |
| 117 | assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16")); |
| 118 | assertThat(addr.broadcastAddress(), is(nullValue())); // TODO: Fix |
| 119 | assertThat(addr.peerAddress(), is(nullValue())); |
| 120 | |
| 121 | // Interface address with non-default broadcast address |
| 122 | addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, |
| 123 | BROADCAST_ADDRESS); |
| 124 | assertThat(addr.ipAddress().toString(), is("1.2.3.4")); |
| 125 | assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16")); |
| 126 | assertThat(addr.broadcastAddress().toString(), is("1.2.0.255")); |
| 127 | assertThat(addr.peerAddress(), is(nullValue())); |
| 128 | |
| 129 | // Point-to-point address with peer IP address |
| 130 | addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, |
| 131 | PEER_ADDRESS); |
| 132 | assertThat(addr.ipAddress().toString(), is("1.2.3.4")); |
| 133 | assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16")); |
| 134 | assertThat(addr.broadcastAddress(), is(nullValue())); |
| 135 | assertThat(addr.peerAddress().toString(), is("5.6.7.8")); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Tests equality of {@link InterfaceIpAddress}. |
| 140 | */ |
| 141 | @Test |
| 142 | public void testEquality() { |
| 143 | InterfaceIpAddress addr1, addr2; |
| 144 | |
| 145 | // Regular interface address with default broadcast address |
| 146 | addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); |
| 147 | addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); |
| 148 | assertThat(addr1, is(addr2)); |
| 149 | |
| 150 | // Interface address with non-default broadcast address |
| 151 | addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, |
| 152 | BROADCAST_ADDRESS); |
| 153 | addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, |
| 154 | BROADCAST_ADDRESS); |
| 155 | assertThat(addr1, is(addr2)); |
| 156 | |
| 157 | // Point-to-point address with peer IP address |
| 158 | addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, |
| 159 | PEER_ADDRESS); |
| 160 | addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, |
| 161 | PEER_ADDRESS); |
| 162 | assertThat(addr1, is(addr2)); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Tests non-equality of {@link InterfaceIpAddress}. |
| 167 | */ |
| 168 | @Test |
| 169 | public void testNonEquality() { |
| 170 | InterfaceIpAddress addr1, addr2, addr3, addr4; |
| 171 | |
| 172 | // Regular interface address with default broadcast address |
| 173 | addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); |
| 174 | // Interface address with non-default broadcast address |
| 175 | addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, |
| 176 | BROADCAST_ADDRESS); |
| 177 | // Point-to-point address with peer IP address |
| 178 | addr3 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, |
| 179 | PEER_ADDRESS); |
| 180 | |
| 181 | // Test interface addresses with different properties: |
| 182 | // - default-broadcast vs non-default broadcast |
| 183 | // - regular vs point-to-point |
| 184 | assertThat(addr1, is(not(addr2))); |
| 185 | assertThat(addr1, is(not(addr3))); |
| 186 | assertThat(addr2, is(not(addr3))); |
| 187 | |
| 188 | // Test regular interface address with default broadcast address |
| 189 | addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS); |
| 190 | assertThat(addr1, is(not(addr4))); |
| 191 | addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2); |
| 192 | assertThat(addr1, is(not(addr4))); |
| 193 | |
| 194 | // Test interface address with non-default broadcast address |
| 195 | addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS, |
| 196 | BROADCAST_ADDRESS); |
| 197 | assertThat(addr2, is(not(addr4))); |
| 198 | addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2, |
| 199 | BROADCAST_ADDRESS); |
| 200 | assertThat(addr2, is(not(addr4))); |
| 201 | addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, |
| 202 | BROADCAST_ADDRESS2); |
| 203 | assertThat(addr2, is(not(addr4))); |
| 204 | |
| 205 | // Test point-to-point address with peer IP address |
| 206 | addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS, null, |
| 207 | PEER_ADDRESS); |
| 208 | assertThat(addr3, is(not(addr4))); |
| 209 | addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2, null, |
| 210 | PEER_ADDRESS); |
| 211 | assertThat(addr3, is(not(addr4))); |
| 212 | addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, |
| 213 | PEER_ADDRESS2); |
| 214 | assertThat(addr3, is(not(addr4))); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Tests object string representation. |
| 219 | */ |
| 220 | @Test |
| 221 | public void testToString() { |
| 222 | InterfaceIpAddress addr; |
| 223 | |
| 224 | // Regular interface address with default broadcast address |
| 225 | addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); |
| 226 | assertThat(addr.toString(), |
| 227 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}")); |
| 228 | |
| 229 | // Interface address with non-default broadcast address |
| 230 | addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, |
| 231 | BROADCAST_ADDRESS); |
| 232 | assertThat(addr.toString(), |
| 233 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}")); |
| 234 | |
| 235 | // Point-to-point address with peer IP address |
| 236 | addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, |
| 237 | PEER_ADDRESS); |
| 238 | assertThat(addr.toString(), |
| 239 | is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}")); |
| 240 | } |
| 241 | } |