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 Ip4Prefix}. |
| 31 | */ |
| 32 | public class Ip4PrefixTest { |
| 33 | /** |
| 34 | * Tests the immutability of {@link Ip4Prefix}. |
| 35 | */ |
| 36 | @Test |
| 37 | public void testImmutable() { |
| 38 | assertThatClassIsImmutable(Ip4Prefix.class); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Tests default class constructor. |
| 43 | */ |
| 44 | @Test |
| 45 | public void testDefaultConstructor() { |
| 46 | Ip4Prefix ip4prefix = new Ip4Prefix(); |
| 47 | assertThat(ip4prefix.toString(), is("0.0.0.0/0")); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Tests valid class copy constructor. |
| 52 | */ |
| 53 | @Test |
| 54 | public void testCopyConstructor() { |
| 55 | Ip4Prefix fromAddr = new Ip4Prefix("1.2.3.0/24"); |
| 56 | Ip4Prefix ip4prefix = new Ip4Prefix(fromAddr); |
| 57 | assertThat(ip4prefix.toString(), is("1.2.3.0/24")); |
| 58 | |
| 59 | fromAddr = new Ip4Prefix("0.0.0.0/0"); |
| 60 | ip4prefix = new Ip4Prefix(fromAddr); |
| 61 | assertThat(ip4prefix.toString(), is("0.0.0.0/0")); |
| 62 | |
| 63 | fromAddr = new Ip4Prefix("255.255.255.255/32"); |
| 64 | ip4prefix = new Ip4Prefix(fromAddr); |
| 65 | assertThat(ip4prefix.toString(), is("255.255.255.255/32")); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Tests invalid class copy constructor for a null object to copy from. |
| 70 | */ |
| 71 | @Test(expected = NullPointerException.class) |
| 72 | public void testInvalidConstructorNullObject() { |
| 73 | Ip4Prefix fromAddr = null; |
| 74 | Ip4Prefix ip4prefix = new Ip4Prefix(fromAddr); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Tests valid class constructor for an address and prefix length. |
| 79 | */ |
| 80 | @Test |
| 81 | public void testConstructorForAddressAndPrefixLength() { |
| 82 | Ip4Prefix ip4prefix = |
| 83 | new Ip4Prefix(new Ip4Address("1.2.3.0"), (short) 24); |
| 84 | assertThat(ip4prefix.toString(), is("1.2.3.0/24")); |
| 85 | |
| 86 | ip4prefix = new Ip4Prefix(new Ip4Address("1.2.3.4"), (short) 24); |
| 87 | assertThat(ip4prefix.toString(), is("1.2.3.0/24")); |
| 88 | |
| 89 | ip4prefix = new Ip4Prefix(new Ip4Address("1.2.3.5"), (short) 32); |
| 90 | assertThat(ip4prefix.toString(), is("1.2.3.5/32")); |
| 91 | |
| 92 | ip4prefix = new Ip4Prefix(new Ip4Address("0.0.0.0"), (short) 0); |
| 93 | assertThat(ip4prefix.toString(), is("0.0.0.0/0")); |
| 94 | |
| 95 | ip4prefix = |
| 96 | new Ip4Prefix(new Ip4Address("255.255.255.255"), (short) 32); |
| 97 | assertThat(ip4prefix.toString(), is("255.255.255.255/32")); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests valid class constructor for a string. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testConstructorForString() { |
| 105 | Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24"); |
| 106 | assertThat(ip4prefix.toString(), is("1.2.3.0/24")); |
| 107 | |
| 108 | ip4prefix = new Ip4Prefix("1.2.3.4/24"); |
| 109 | assertThat(ip4prefix.toString(), is("1.2.3.0/24")); |
| 110 | |
| 111 | ip4prefix = new Ip4Prefix("1.2.3.5/32"); |
| 112 | assertThat(ip4prefix.toString(), is("1.2.3.5/32")); |
| 113 | |
| 114 | ip4prefix = new Ip4Prefix("0.0.0.0/0"); |
| 115 | assertThat(ip4prefix.toString(), is("0.0.0.0/0")); |
| 116 | |
| 117 | ip4prefix = new Ip4Prefix("255.255.255.255/32"); |
| 118 | assertThat(ip4prefix.toString(), is("255.255.255.255/32")); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Tests invalid class constructor for a null string. |
| 123 | */ |
| 124 | @Test(expected = NullPointerException.class) |
| 125 | public void testInvalidConstructorNullString() { |
| 126 | String fromString = null; |
| 127 | Ip4Prefix ip4prefix = new Ip4Prefix(fromString); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests invalid class constructor for an empty string. |
| 132 | */ |
| 133 | @Test(expected = IllegalArgumentException.class) |
| 134 | public void testInvalidConstructors() { |
| 135 | // Check constructor for invalid ID: empty string |
| 136 | Ip4Prefix ip4prefix = new Ip4Prefix(""); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Tests getting the value of an address. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testGetValue() { |
| 144 | Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24"); |
| 145 | assertThat(ip4prefix.getAddress(), equalTo(new Ip4Address("1.2.3.0"))); |
| 146 | assertThat(ip4prefix.getPrefixLen(), is((short) 24)); |
| 147 | |
| 148 | ip4prefix = new Ip4Prefix("0.0.0.0/0"); |
| 149 | assertThat(ip4prefix.getAddress(), equalTo(new Ip4Address("0.0.0.0"))); |
| 150 | assertThat(ip4prefix.getPrefixLen(), is((short) 0)); |
| 151 | |
| 152 | ip4prefix = new Ip4Prefix("255.255.255.255/32"); |
| 153 | assertThat(ip4prefix.getAddress(), |
| 154 | equalTo(new Ip4Address("255.255.255.255"))); |
| 155 | assertThat(ip4prefix.getPrefixLen(), is((short) 32)); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Tests equality of {@link Ip4Address}. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testEquality() { |
| 163 | Ip4Prefix addr1net = new Ip4Prefix("1.2.3.0/24"); |
| 164 | Ip4Prefix addr2net = new Ip4Prefix("1.2.3.0/24"); |
| 165 | assertThat(addr1net, is(addr2net)); |
| 166 | |
| 167 | addr1net = new Ip4Prefix("1.2.3.0/24"); |
| 168 | addr2net = new Ip4Prefix("1.2.3.4/24"); |
| 169 | assertThat(addr1net, is(addr2net)); |
| 170 | |
| 171 | addr1net = new Ip4Prefix("0.0.0.0/0"); |
| 172 | addr2net = new Ip4Prefix("0.0.0.0/0"); |
| 173 | assertThat(addr1net, is(addr2net)); |
| 174 | |
| 175 | addr1net = new Ip4Prefix("255.255.255.255/32"); |
| 176 | addr2net = new Ip4Prefix("255.255.255.255/32"); |
| 177 | assertThat(addr1net, is(addr2net)); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests non-equality of {@link Ip4Address}. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testNonEquality() { |
| 185 | Ip4Prefix addr1net = new Ip4Prefix("1.2.0.0/16"); |
| 186 | Ip4Prefix addr2net = new Ip4Prefix("1.3.0.0/16"); |
| 187 | Ip4Prefix addr3net = new Ip4Prefix("1.3.0.0/24"); |
| 188 | Ip4Prefix addr4net = new Ip4Prefix("0.0.0.0/0"); |
| 189 | Ip4Prefix addr5net = new Ip4Prefix("255.255.255.255/32"); |
| 190 | assertThat(addr1net, is(not(addr2net))); |
| 191 | assertThat(addr3net, is(not(addr2net))); |
| 192 | assertThat(addr4net, is(not(addr2net))); |
| 193 | assertThat(addr5net, is(not(addr2net))); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Tests object string representation. |
| 198 | */ |
| 199 | @Test |
| 200 | public void testToString() { |
| 201 | Ip4Prefix ip4prefix = new Ip4Prefix("1.2.3.0/24"); |
| 202 | assertThat(ip4prefix.toString(), is("1.2.3.0/24")); |
| 203 | |
| 204 | ip4prefix = new Ip4Prefix("1.2.3.4/24"); |
| 205 | assertThat(ip4prefix.toString(), is("1.2.3.0/24")); |
| 206 | |
| 207 | ip4prefix = new Ip4Prefix("0.0.0.0/0"); |
| 208 | assertThat(ip4prefix.toString(), is("0.0.0.0/0")); |
| 209 | |
| 210 | ip4prefix = new Ip4Prefix("255.255.255.255/32"); |
| 211 | assertThat(ip4prefix.toString(), is("255.255.255.255/32")); |
| 212 | } |
| 213 | } |