Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 19 | package org.onlab.packet; |
| 20 | |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import static org.hamcrest.Matchers.equalTo; |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | import static org.hamcrest.Matchers.not; |
| 26 | import static org.junit.Assert.assertThat; |
| 27 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 28 | |
| 29 | /** |
| 30 | * Tests for class {@link Ip6Prefix}. |
| 31 | */ |
| 32 | public class Ip6PrefixTest { |
| 33 | /** |
| 34 | * Tests the immutability of {@link Ip6Prefix}. |
| 35 | */ |
| 36 | @Test |
| 37 | public void testImmutable() { |
| 38 | assertThatClassIsImmutable(Ip6Prefix.class); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Tests default class constructor. |
| 43 | */ |
| 44 | @Test |
| 45 | public void testDefaultConstructor() { |
| 46 | Ip6Prefix ip6prefix = new Ip6Prefix(); |
| 47 | assertThat(ip6prefix.toString(), is("::/0")); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Tests valid class copy constructor. |
| 52 | */ |
| 53 | @Test |
| 54 | public void testCopyConstructor() { |
| 55 | Ip6Prefix fromAddr = new Ip6Prefix("1100::/8"); |
| 56 | Ip6Prefix ip6prefix = new Ip6Prefix(fromAddr); |
| 57 | assertThat(ip6prefix.toString(), is("1100::/8")); |
| 58 | |
| 59 | fromAddr = new Ip6Prefix("::/0"); |
| 60 | ip6prefix = new Ip6Prefix(fromAddr); |
| 61 | assertThat(ip6prefix.toString(), is("::/0")); |
| 62 | |
| 63 | fromAddr = |
| 64 | new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 65 | ip6prefix = new Ip6Prefix(fromAddr); |
| 66 | assertThat(ip6prefix.toString(), |
| 67 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests invalid class copy constructor for a null object to copy from. |
| 72 | */ |
| 73 | @Test(expected = NullPointerException.class) |
| 74 | public void testInvalidConstructorNullObject() { |
| 75 | Ip6Prefix fromAddr = null; |
| 76 | Ip6Prefix ip6prefix = new Ip6Prefix(fromAddr); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Tests valid class constructor for an address and prefix length. |
| 81 | */ |
| 82 | @Test |
| 83 | public void testConstructorForAddressAndPrefixLength() { |
| 84 | Ip6Prefix ip6prefix = |
| 85 | new Ip6Prefix(new Ip6Address("1100::"), (short) 8); |
| 86 | assertThat(ip6prefix.toString(), is("1100::/8")); |
| 87 | |
| 88 | ip6prefix = |
| 89 | new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"), |
| 90 | (short) 8); |
| 91 | assertThat(ip6prefix.toString(), is("1100::/8")); |
| 92 | |
| 93 | ip6prefix = |
| 94 | new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8800"), |
| 95 | (short) 120); |
| 96 | assertThat(ip6prefix.toString(), |
| 97 | is("1111:2222:3333:4444:5555:6666:7777:8800/120")); |
| 98 | |
| 99 | ip6prefix = new Ip6Prefix(new Ip6Address("::"), (short) 0); |
| 100 | assertThat(ip6prefix.toString(), is("::/0")); |
| 101 | |
| 102 | ip6prefix = |
| 103 | new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"), |
| 104 | (short) 128); |
| 105 | assertThat(ip6prefix.toString(), |
| 106 | is("1111:2222:3333:4444:5555:6666:7777:8885/128")); |
| 107 | |
| 108 | ip6prefix = |
| 109 | new Ip6Prefix(new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), |
| 110 | (short) 128); |
| 111 | assertThat(ip6prefix.toString(), |
| 112 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 113 | |
| 114 | ip6prefix = |
| 115 | new Ip6Prefix(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"), |
| 116 | (short) 64); |
| 117 | assertThat(ip6prefix.toString(), is("1111:2222:3333:4444::/64")); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tests valid class constructor for a string. |
| 122 | */ |
| 123 | @Test |
| 124 | public void testConstructorForString() { |
| 125 | Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8"); |
| 126 | assertThat(ip6prefix.toString(), is("1100::/8")); |
| 127 | |
| 128 | ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8"); |
| 129 | assertThat(ip6prefix.toString(), is("1100::/8")); |
| 130 | |
| 131 | ip6prefix = |
| 132 | new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8800/120"); |
| 133 | assertThat(ip6prefix.toString(), |
| 134 | is("1111:2222:3333:4444:5555:6666:7777:8800/120")); |
| 135 | |
| 136 | ip6prefix = new Ip6Prefix("::/0"); |
| 137 | assertThat(ip6prefix.toString(), is("::/0")); |
| 138 | |
| 139 | ip6prefix = |
| 140 | new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/128"); |
| 141 | assertThat(ip6prefix.toString(), |
| 142 | is("1111:2222:3333:4444:5555:6666:7777:8885/128")); |
| 143 | |
| 144 | ip6prefix = new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 145 | assertThat(ip6prefix.toString(), |
| 146 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 147 | |
| 148 | ip6prefix = |
| 149 | new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/64"); |
| 150 | assertThat(ip6prefix.toString(), is("1111:2222:3333:4444::/64")); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Tests invalid class constructor for a null string. |
| 155 | */ |
| 156 | @Test(expected = NullPointerException.class) |
| 157 | public void testInvalidConstructorNullString() { |
| 158 | String fromString = null; |
| 159 | Ip6Prefix ip6prefix = new Ip6Prefix(fromString); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests invalid class constructor for an empty string. |
| 164 | */ |
| 165 | @Test(expected = IllegalArgumentException.class) |
| 166 | public void testInvalidConstructors() { |
| 167 | // Check constructor for invalid ID: empty string |
| 168 | Ip6Prefix ip6prefix = new Ip6Prefix(""); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Tests getting the value of an address. |
| 173 | */ |
| 174 | @Test |
| 175 | public void testGetValue() { |
| 176 | Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8"); |
| 177 | assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("1100::"))); |
| 178 | assertThat(ip6prefix.getPrefixLen(), is((short) 8)); |
| 179 | |
| 180 | ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8"); |
| 181 | assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("1100::"))); |
| 182 | assertThat(ip6prefix.getPrefixLen(), is((short) 8)); |
| 183 | |
| 184 | ip6prefix = |
| 185 | new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8800/120"); |
| 186 | assertThat(ip6prefix.getAddress(), |
| 187 | equalTo(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8800"))); |
| 188 | assertThat(ip6prefix.getPrefixLen(), is((short) 120)); |
| 189 | |
| 190 | ip6prefix = new Ip6Prefix("::/0"); |
| 191 | assertThat(ip6prefix.getAddress(), equalTo(new Ip6Address("::"))); |
| 192 | assertThat(ip6prefix.getPrefixLen(), is((short) 0)); |
| 193 | |
| 194 | ip6prefix = |
| 195 | new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/128"); |
| 196 | assertThat(ip6prefix.getAddress(), |
| 197 | equalTo(new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"))); |
| 198 | assertThat(ip6prefix.getPrefixLen(), is((short) 128)); |
| 199 | |
| 200 | ip6prefix = |
| 201 | new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 202 | assertThat(ip6prefix.getAddress(), |
| 203 | equalTo(new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); |
| 204 | assertThat(ip6prefix.getPrefixLen(), is((short) 128)); |
| 205 | |
| 206 | ip6prefix = |
| 207 | new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/64"); |
| 208 | assertThat(ip6prefix.getAddress(), |
| 209 | equalTo(new Ip6Address("1111:2222:3333:4444::"))); |
| 210 | assertThat(ip6prefix.getPrefixLen(), is((short) 64)); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Tests equality of {@link Ip6Address}. |
| 215 | */ |
| 216 | @Test |
| 217 | public void testEquality() { |
| 218 | Ip6Prefix addr1net = new Ip6Prefix("1100::/8"); |
| 219 | Ip6Prefix addr2net = new Ip6Prefix("1100::/8"); |
| 220 | assertThat(addr1net, is(addr2net)); |
| 221 | |
| 222 | addr1net = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8"); |
| 223 | addr2net = new Ip6Prefix("1100::/8"); |
| 224 | assertThat(addr1net, is(addr2net)); |
| 225 | |
| 226 | addr1net = new Ip6Prefix("::/0"); |
| 227 | addr2net = new Ip6Prefix("::/0"); |
| 228 | assertThat(addr1net, is(addr2net)); |
| 229 | |
| 230 | addr1net = |
| 231 | new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 232 | addr2net = |
| 233 | new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 234 | assertThat(addr1net, is(addr2net)); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Tests non-equality of {@link Ip6Address}. |
| 239 | */ |
| 240 | @Test |
| 241 | public void testNonEquality() { |
| 242 | Ip6Prefix addr1net = new Ip6Prefix("1100::/8"); |
| 243 | Ip6Prefix addr2net = new Ip6Prefix("1200::/8"); |
| 244 | Ip6Prefix addr3net = new Ip6Prefix("1200::/12"); |
| 245 | Ip6Prefix addr4net = new Ip6Prefix("::/0"); |
| 246 | Ip6Prefix addr5net = |
| 247 | new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 248 | assertThat(addr1net, is(not(addr2net))); |
| 249 | assertThat(addr3net, is(not(addr2net))); |
| 250 | assertThat(addr4net, is(not(addr2net))); |
| 251 | assertThat(addr5net, is(not(addr2net))); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Tests object string representation. |
| 256 | */ |
| 257 | @Test |
| 258 | public void testToString() { |
| 259 | Ip6Prefix ip6prefix = new Ip6Prefix("1100::/8"); |
| 260 | assertThat(ip6prefix.toString(), is("1100::/8")); |
| 261 | |
| 262 | ip6prefix = new Ip6Prefix("1111:2222:3333:4444:5555:6666:7777:8885/8"); |
| 263 | assertThat(ip6prefix.toString(), is("1100::/8")); |
| 264 | |
| 265 | ip6prefix = new Ip6Prefix("::/0"); |
| 266 | assertThat(ip6prefix.toString(), is("::/0")); |
| 267 | |
| 268 | ip6prefix = |
| 269 | new Ip6Prefix("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); |
| 270 | assertThat(ip6prefix.toString(), |
| 271 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); |
| 272 | } |
| 273 | } |