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