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