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