diff options
author | CNlucius <lukai1@huawei.com> | 2016-09-13 11:40:12 +0800 |
---|---|---|
committer | CNlucius <lukai1@huawei.com> | 2016-09-13 11:41:53 +0800 |
commit | b731e2f1dd0972409b136aebc7b463dd72c9cfad (patch) | |
tree | 5107d7d80c19ad8076c2c97c2b5ef8d1cf3ab903 /framework/src/onos/utils/misc/src/test/java/org/onlab/packet | |
parent | ee93993458266114c29271a481ef9ce7ce621b2a (diff) |
ONOSFW-171
O/S-SFC-ONOS scenario documentation
Change-Id: I51ae1cf736ea24ab6680f8edca1b2bf5dd598365
Signed-off-by: CNlucius <lukai1@huawei.com>
Diffstat (limited to 'framework/src/onos/utils/misc/src/test/java/org/onlab/packet')
34 files changed, 0 insertions, 7085 deletions
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ArpTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ArpTest.java deleted file mode 100644 index 62246245..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ArpTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Unit tests for the ARP class. - */ -public class ArpTest { - - private Deserializer<ARP> deserializer = ARP.deserializer(); - - private final byte hwAddressLength = 6; - private final byte protoAddressLength = 4; - - private MacAddress srcMac = MacAddress.valueOf(1); - private MacAddress targetMac = MacAddress.valueOf(2); - private Ip4Address srcIp = Ip4Address.valueOf(1); - private Ip4Address targetIp = Ip4Address.valueOf(2); - - private byte[] byteHeader; - - @Before - public void setUp() { - ByteBuffer bb = ByteBuffer.allocate(ARP.INITIAL_HEADER_LENGTH + - 2 * hwAddressLength + 2 * protoAddressLength); - bb.putShort(ARP.HW_TYPE_ETHERNET); - bb.putShort(ARP.PROTO_TYPE_IP); - bb.put(hwAddressLength); - bb.put(protoAddressLength); - bb.putShort(ARP.OP_REPLY); - - bb.put(srcMac.toBytes()); - bb.put(srcIp.toOctets()); - bb.put(targetMac.toBytes()); - bb.put(targetIp.toOctets()); - - byteHeader = bb.array(); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader); - } - - @Test - public void testDeserialize() throws Exception { - ARP arp = deserializer.deserialize(byteHeader, 0, byteHeader.length); - - assertEquals(ARP.HW_TYPE_ETHERNET, arp.getHardwareType()); - assertEquals(ARP.PROTO_TYPE_IP, arp.getProtocolType()); - assertEquals(hwAddressLength, arp.getHardwareAddressLength()); - assertEquals(protoAddressLength, arp.getProtocolAddressLength()); - assertEquals(ARP.OP_REPLY, arp.getOpCode()); - - assertTrue(Arrays.equals(srcMac.toBytes(), arp.getSenderHardwareAddress())); - assertTrue(Arrays.equals(srcIp.toOctets(), arp.getSenderProtocolAddress())); - assertTrue(Arrays.equals(targetMac.toBytes(), arp.getTargetHardwareAddress())); - assertTrue(Arrays.equals(targetIp.toOctets(), arp.getTargetProtocolAddress())); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/DhcpTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/DhcpTest.java deleted file mode 100644 index aac81412..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/DhcpTest.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import com.google.common.base.Charsets; -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Unit tests for DHCP class. - */ -public class DhcpTest { - - private Deserializer<DHCP> deserializer = DHCP.deserializer(); - - private byte opCode = 1; - private byte hardwareType = 1; - private byte hardwareAddressLength = Ethernet.DATALAYER_ADDRESS_LENGTH; - private byte hops = 0; - private int transactionId = 0x2ed4eb50; - private short seconds = 0; - private short flags = 0; - private int clientIpAddress = 1; - private int yourIpAddress = 2; - private int serverIpAddress = 3; - private int gatewayIpAddress = 4; - private byte[] clientHardwareAddress = MacAddress.valueOf(500).toBytes(); - private String serverName = "test-server"; - private String bootFileName = "test-file"; - - private String hostName = "test-host"; - private DHCPOption hostNameOption = new DHCPOption(); - - private byte[] byteHeader; - - @Before - public void setUp() { - hostNameOption.setCode((byte) 55); - hostNameOption.setLength((byte) hostName.length()); - hostNameOption.setData(hostName.getBytes(Charsets.US_ASCII)); - - // Packet length is the fixed DHCP header plus option length plus an - // extra byte to indicate 'end of options'. - ByteBuffer bb = ByteBuffer.allocate(DHCP.MIN_HEADER_LENGTH + - 2 + hostNameOption.getLength() + 1); - - bb.put(opCode); - bb.put(hardwareType); - bb.put(hardwareAddressLength); - bb.put(hops); - bb.putInt(transactionId); - bb.putShort(seconds); - bb.putShort(flags); - bb.putInt(clientIpAddress); - bb.putInt(yourIpAddress); - bb.putInt(serverIpAddress); - bb.putInt(gatewayIpAddress); - bb.put(clientHardwareAddress); - - // need 16 bytes of zeros to pad out the client hardware address field - bb.put(new byte[16 - hardwareAddressLength]); - - // Put server name and pad out to 64 bytes - bb.put(serverName.getBytes(Charsets.US_ASCII)); - bb.put(new byte[64 - serverName.length()]); - - // Put boot file name and pad out to 128 bytes - bb.put(bootFileName.getBytes(Charsets.US_ASCII)); - bb.put(new byte[128 - bootFileName.length()]); - - // Magic cookie - bb.put("DHCP".getBytes(Charsets.US_ASCII)); - - bb.put(hostNameOption.getCode()); - bb.put(hostNameOption.getLength()); - bb.put(hostNameOption.getData()); - - // End of options marker - bb.put((byte) (0xff & 255)); - - byteHeader = bb.array(); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader); - } - - @Test - public void testDeserialize() throws Exception { - DHCP dhcp = deserializer.deserialize(byteHeader, 0, byteHeader.length); - - assertEquals(opCode, dhcp.opCode); - assertEquals(hardwareType, dhcp.hardwareType); - assertEquals(hardwareAddressLength, dhcp.hardwareAddressLength); - assertEquals(hops, dhcp.hops); - assertEquals(transactionId, dhcp.transactionId); - assertEquals(seconds, dhcp.seconds); - assertEquals(flags, dhcp.flags); - assertEquals(clientIpAddress, dhcp.clientIPAddress); - assertEquals(yourIpAddress, dhcp.yourIPAddress); - assertEquals(serverIpAddress, dhcp.serverIPAddress); - assertEquals(gatewayIpAddress, dhcp.gatewayIPAddress); - assertTrue(Arrays.equals(clientHardwareAddress, dhcp.clientHardwareAddress)); - - assertEquals(serverName, dhcp.serverName); - assertEquals(bootFileName, dhcp.bootFileName); - assertEquals(2, dhcp.options.size()); - assertEquals(hostNameOption, dhcp.options.get(0)); - } - -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/EthernetTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/EthernetTest.java deleted file mode 100644 index 15a01fc3..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/EthernetTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.junit.Assert.assertEquals; - -/** - * Unit tests for the Ethernet class. - */ -public class EthernetTest { - - private MacAddress dstMac; - private MacAddress srcMac; - private short ethertype = 6; - private short vlan = 5; - - private Deserializer<Ethernet> deserializer; - - private byte[] byteHeader; - private byte[] vlanByteHeader; - - @Before - public void setUp() { - deserializer = Ethernet.deserializer(); - - byte[] dstMacBytes = { - (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88, - (byte) 0x88 }; - dstMac = MacAddress.valueOf(dstMacBytes); - byte[] srcMacBytes = { - (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, - (byte) 0xaa }; - srcMac = MacAddress.valueOf(srcMacBytes); - - // Create Ethernet byte array with no VLAN header - ByteBuffer bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH); - bb.put(dstMacBytes); - bb.put(srcMacBytes); - bb.putShort(ethertype); - - byteHeader = bb.array(); - - // Create Ethernet byte array with a VLAN header - bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH + Ethernet.VLAN_HEADER_LENGTH); - bb.put(dstMacBytes); - bb.put(srcMacBytes); - bb.putShort(Ethernet.TYPE_VLAN); - bb.putShort(vlan); - bb.putShort(ethertype); - - vlanByteHeader = bb.array(); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws DeserializationException { - PacketTestUtils.testDeserializeTruncated(deserializer, vlanByteHeader); - } - - @Test - public void testDeserializeNoVlan() throws Exception { - Ethernet eth = deserializer.deserialize(byteHeader, 0, byteHeader.length); - - assertEquals(dstMac, eth.getDestinationMAC()); - assertEquals(srcMac, eth.getSourceMAC()); - assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID()); - assertEquals(ethertype, eth.getEtherType()); - } - - @Test - public void testDeserializeWithVlan() throws Exception { - Ethernet eth = deserializer.deserialize(vlanByteHeader, 0, vlanByteHeader.length); - - assertEquals(dstMac, eth.getDestinationMAC()); - assertEquals(srcMac, eth.getSourceMAC()); - assertEquals(vlan, eth.getVlanID()); - assertEquals(ethertype, eth.getEtherType()); - } - -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMP6Test.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMP6Test.java deleted file mode 100644 index 39ddc24c..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMP6Test.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - - -package org.onlab.packet; - -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link ICMP6}. - */ -public class ICMP6Test { - private static final byte[] IPV6_SOURCE_ADDRESS = { - (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 - }; - private static final byte[] IPV6_DESTINATION_ADDRESS = { - (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02 - }; - - private static IPv6 ipv6 = new IPv6(); - private static byte[] bytePacket = { - ICMP6.ECHO_REQUEST, // type - (byte) 0x00, // code - (byte) 0x82, (byte) 0xbc, // checksum - }; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS); - ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS); - ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - ICMP6 icmp6 = new ICMP6(); - icmp6.setIcmpType(ICMP6.ECHO_REQUEST); - icmp6.setIcmpCode((byte) 0); - icmp6.setParent(ipv6); - - assertArrayEquals(bytePacket, icmp6.serialize()); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(ICMP6.deserializer()); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(ICMP6.deserializer(), bytePacket); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws Exception { - ICMP6 icmp6 = ICMP6.deserializer().deserialize(bytePacket, 0, bytePacket.length); - - assertThat(icmp6.getIcmpType(), is(ICMP6.ECHO_REQUEST)); - assertThat(icmp6.getIcmpCode(), is((byte) 0x00)); - assertThat(icmp6.getChecksum(), is((short) 0x82bc)); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - ICMP6 icmp61 = new ICMP6(); - icmp61.setIcmpType(ICMP6.ECHO_REQUEST); - icmp61.setIcmpCode((byte) 0); - icmp61.setChecksum((short) 0); - - ICMP6 icmp62 = new ICMP6(); - icmp62.setIcmpType(ICMP6.ECHO_REPLY); - icmp62.setIcmpCode((byte) 0); - icmp62.setChecksum((short) 0); - - assertTrue(icmp61.equals(icmp61)); - assertFalse(icmp61.equals(icmp62)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java deleted file mode 100644 index 8514aa59..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.junit.Assert.assertEquals; - -/** - * Unit tests for the ICMP class. - */ -public class ICMPTest { - - private Deserializer<ICMP> deserializer; - - private byte icmpType = ICMP.TYPE_ECHO_REQUEST; - private byte icmpCode = 4; - private short checksum = 870; - - private byte[] headerBytes; - - @Before - public void setUp() throws Exception { - deserializer = ICMP.deserializer(); - - ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH); - - bb.put(icmpType); - bb.put(icmpCode); - bb.putShort(checksum); - - headerBytes = bb.array(); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes); - } - - @Test - public void testDeserialize() throws Exception { - ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length); - - assertEquals(icmpType, icmp.getIcmpType()); - assertEquals(icmpCode, icmp.getIcmpCode()); - assertEquals(checksum, icmp.getChecksum()); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IGMPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IGMPTest.java deleted file mode 100644 index a25f7217..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IGMPTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.Assert.assertTrue; - -/** - * Unit tests for IGMP class. - */ -public class IGMPTest { - private Deserializer<IGMP> deserializer; - - private IGMP igmpQuery; - private IGMP igmpMembership; - - private Ip4Address gaddr1; - private Ip4Address gaddr2; - private Ip4Address saddr1; - private Ip4Address saddr2; - - @Before - public void setUp() throws Exception { - gaddr1 = Ip4Address.valueOf(0xe1010101); - gaddr2 = Ip4Address.valueOf(0xe2020202); - saddr1 = Ip4Address.valueOf(0x0a010101); - saddr2 = Ip4Address.valueOf(0x0b020202); - - deserializer = IGMP.deserializer(); - - // Create an IGMP Query object - igmpQuery = new IGMP(); - igmpQuery.setIgmpType(IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY); - igmpQuery.setMaxRespCode((byte) 0x7f); - IGMPQuery q = new IGMPQuery(gaddr1, (byte) 0x7f); - q.addSource(saddr1); - q.addSource(saddr2); - q.setSbit(false); - igmpQuery.groups.add(q); - - // Create an IGMP Membership Object - igmpMembership = new IGMP(); - igmpMembership.setIgmpType(IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT); - IGMPMembership g1 = new IGMPMembership(gaddr1); - g1.addSource(saddr1); - g1.addSource(saddr2); - igmpMembership.groups.add(g1); - IGMPMembership g2 = new IGMPMembership(gaddr2); - g2.addSource(saddr1); - g2.addSource(saddr2); - igmpMembership.groups.add(g2); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - byte [] bits = igmpQuery.serialize(); - PacketTestUtils.testDeserializeTruncated(deserializer, bits); - - bits = igmpMembership.serialize(); - PacketTestUtils.testDeserializeTruncated(deserializer, bits); - } - - @Test - public void testDeserializeQuery() throws Exception { - byte [] data = igmpQuery.serialize(); - IGMP igmp = deserializer.deserialize(data, 0, data.length); - assertTrue(igmp.equals(igmpQuery)); - } - - @Test - public void testDeserializeMembership() throws Exception { - byte [] data = igmpMembership.serialize(); - IGMP igmp = deserializer.deserialize(data, 0, data.length); - assertTrue(igmp.equals(igmpMembership)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv4Test.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv4Test.java deleted file mode 100644 index 1bacf2a2..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv4Test.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Unit tests for IPv4 class. - */ -public class IPv4Test { - - private Deserializer<IPv4> deserializer; - - private byte version = 4; - private byte headerLength = 6; - private byte diffServ = 2; - private short totalLength = 20; - private short identification = 1; - private byte flags = 1; - private short fragmentOffset = 1; - private byte ttl = 60; - private byte protocol = 4; - private short checksum = 4; - private int sourceAddress = 1; - private int destinationAddress = 2; - private byte[] options = new byte[] {0x1, 0x2, 0x3, 0x4}; - - private byte[] headerBytes; - - @Before - public void setUp() throws Exception { - deserializer = IPv4.deserializer(); - - ByteBuffer bb = ByteBuffer.allocate(headerLength * 4); - - bb.put((byte) ((version & 0xf) << 4 | headerLength & 0xf)); - bb.put(diffServ); - bb.putShort(totalLength); - bb.putShort(identification); - bb.putShort((short) ((flags & 0x7) << 13 | fragmentOffset & 0x1fff)); - bb.put(ttl); - bb.put(protocol); - bb.putShort(checksum); - bb.putInt(sourceAddress); - bb.putInt(destinationAddress); - bb.put(options); - - headerBytes = bb.array(); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes); - } - - @Test - public void testDeserialize() throws Exception { - IPv4 ipv4 = deserializer.deserialize(headerBytes, 0, headerBytes.length); - - assertEquals(version, ipv4.getVersion()); - assertEquals(headerLength, ipv4.getHeaderLength()); - assertEquals(diffServ, ipv4.getDiffServ()); - assertEquals(totalLength, ipv4.getTotalLength()); - assertEquals(identification, ipv4.getIdentification()); - assertEquals(flags, ipv4.getFlags()); - assertEquals(fragmentOffset, ipv4.getFragmentOffset()); - assertEquals(ttl, ipv4.getTtl()); - assertEquals(protocol, ipv4.getProtocol()); - assertEquals(checksum, ipv4.getChecksum()); - assertEquals(sourceAddress, ipv4.getSourceAddress()); - assertEquals(destinationAddress, ipv4.getDestinationAddress()); - assertTrue(ipv4.isTruncated()); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv6Test.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv6Test.java deleted file mode 100644 index 720a4d20..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IPv6Test.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - - -package org.onlab.packet; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link IPv6}. - */ -public class IPv6Test { - private static final byte[] SOURCE_ADDRESS = { - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce - }; - private static final byte[] DESTINATION_ADDRESS = { - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 - }; - private static Data data; - private static UDP udp; - private static byte[] bytePacket; - - private Deserializer<IPv6> deserializer; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - data = new Data(); - data.setData("testSerialize".getBytes()); - udp = new UDP(); - udp.setPayload(data); - - byte[] bytePayload = udp.serialize(); - byte[] byteHeader = { - (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79, - (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff), - (byte) 0x11, (byte) 0x20, - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8, - }; - bytePacket = new byte[byteHeader.length + bytePayload.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); - } - - @Before - public void setUp() { - deserializer = IPv6.deserializer(); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - IPv6 ipv6 = new IPv6(); - ipv6.setPayload(udp); - ipv6.setVersion((byte) 6); - ipv6.setTrafficClass((byte) 0x93); - ipv6.setFlowLabel(0x13579); - ipv6.setNextHeader(IPv6.PROTOCOL_UDP); - ipv6.setHopLimit((byte) 32); - ipv6.setSourceAddress(SOURCE_ADDRESS); - ipv6.setDestinationAddress(DESTINATION_ADDRESS); - - assertArrayEquals(ipv6.serialize(), bytePacket); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - // Run the truncation test only on the IPv6 header - byte[] ipv6Header = new byte[IPv6.FIXED_HEADER_LENGTH]; - ByteBuffer.wrap(bytePacket).get(ipv6Header); - - PacketTestUtils.testDeserializeTruncated(deserializer, ipv6Header); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws DeserializationException { - IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertThat(ipv6.getVersion(), is((byte) 6)); - assertThat(ipv6.getTrafficClass(), is((byte) 0x93)); - assertThat(ipv6.getFlowLabel(), is(0x13579)); - assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP)); - assertThat(ipv6.getHopLimit(), is((byte) 32)); - assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS); - assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - IPv6 packet1 = new IPv6(); - packet1.setPayload(udp); - packet1.setVersion((byte) 6); - packet1.setTrafficClass((byte) 0x93); - packet1.setFlowLabel(0x13579); - packet1.setNextHeader(IPv6.PROTOCOL_UDP); - packet1.setHopLimit((byte) 32); - packet1.setSourceAddress(SOURCE_ADDRESS); - packet1.setDestinationAddress(DESTINATION_ADDRESS); - - IPv6 packet2 = new IPv6(); - packet2.setPayload(udp); - packet2.setVersion((byte) 6); - packet2.setTrafficClass((byte) 0x93); - packet2.setFlowLabel(0x13579); - packet2.setNextHeader(IPv6.PROTOCOL_UDP); - packet2.setHopLimit((byte) 32); - packet2.setSourceAddress(DESTINATION_ADDRESS); - packet2.setDestinationAddress(SOURCE_ADDRESS); - - assertTrue(packet1.equals(packet1)); - assertFalse(packet1.equals(packet2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4AddressTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4AddressTest.java deleted file mode 100644 index 3bbf0009..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4AddressTest.java +++ /dev/null @@ -1,432 +0,0 @@ -/* - * Copyright 2014 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import com.google.common.net.InetAddresses; -import com.google.common.testing.EqualsTester; -import org.junit.Test; - -import java.net.InetAddress; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; - -/** - * Tests for class {@link Ip4Address}. - */ -public class Ip4AddressTest { - /** - * Tests the immutability of {@link Ip4Address}. - */ - @Test - public void testImmutable() { - assertThatClassIsImmutable(Ip4Address.class); - } - - /** - * Tests the IPv4 address version constant. - */ - @Test - public void testAddressVersion() { - assertThat(Ip4Address.VERSION, is(IpAddress.Version.INET)); - } - - /** - * Tests the length of the address in bytes (octets). - */ - @Test - public void testAddrByteLength() { - assertThat(Ip4Address.BYTE_LENGTH, is(4)); - } - - /** - * Tests the length of the address in bits. - */ - @Test - public void testAddrBitLength() { - assertThat(Ip4Address.BIT_LENGTH, is(32)); - } - - /** - * Tests returning the IP address version. - */ - @Test - public void testVersion() { - Ip4Address ipAddress; - - // IPv4 - ipAddress = Ip4Address.valueOf("0.0.0.0"); - assertThat(ipAddress.version(), is(IpAddress.Version.INET)); - } - - /** - * Tests returning an IPv4 address as a byte array. - */ - @Test - public void testAddressToOctetsIPv4() { - Ip4Address ipAddress; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipAddress = Ip4Address.valueOf("1.2.3.4"); - assertThat(ipAddress.toOctets(), is(value)); - - value = new byte[] {0, 0, 0, 0}; - ipAddress = Ip4Address.valueOf("0.0.0.0"); - assertThat(ipAddress.toOctets(), is(value)); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipAddress = Ip4Address.valueOf("255.255.255.255"); - assertThat(ipAddress.toOctets(), is(value)); - } - - /** - * Tests returning an IPv4 address as an integer. - */ - @Test - public void testToInt() { - Ip4Address ipAddress; - - ipAddress = Ip4Address.valueOf("1.2.3.4"); - assertThat(ipAddress.toInt(), is(0x01020304)); - - ipAddress = Ip4Address.valueOf("0.0.0.0"); - assertThat(ipAddress.toInt(), is(0)); - - ipAddress = Ip4Address.valueOf("255.255.255.255"); - assertThat(ipAddress.toInt(), is(-1)); - } - - /** - * Tests valueOf() converter for IPv4 integer value. - */ - @Test - public void testValueOfForIntegerIPv4() { - Ip4Address ipAddress; - - ipAddress = Ip4Address.valueOf(0x01020304); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - ipAddress = Ip4Address.valueOf(0); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - ipAddress = Ip4Address.valueOf(0xffffffff); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests valueOf() converter for IPv4 byte array. - */ - @Test - public void testValueOfByteArrayIPv4() { - Ip4Address ipAddress; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipAddress = Ip4Address.valueOf(value); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - value = new byte[] {0, 0, 0, 0}; - ipAddress = Ip4Address.valueOf(value); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipAddress = Ip4Address.valueOf(value); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests invalid valueOf() converter for a null array for IPv4. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullArrayIPv4() { - Ip4Address ipAddress; - byte[] value; - - value = null; - ipAddress = Ip4Address.valueOf(value); - } - - /** - * Tests invalid valueOf() converger for an array that is too short for - * IPv4. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfShortArrayIPv4() { - Ip4Address ipAddress; - byte[] value; - - value = new byte[] {1, 2, 3}; - ipAddress = Ip4Address.valueOf(value); - } - - /** - * Tests valueOf() converter for IPv4 byte array and an offset. - */ - @Test - public void testValueOfByteArrayOffsetIPv4() { - Ip4Address ipAddress; - byte[] value; - - value = new byte[] {11, 22, 33, // Preamble - 1, 2, 3, 4, - 44, 55}; // Extra bytes - ipAddress = Ip4Address.valueOf(value, 3); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - value = new byte[] {11, 22, // Preamble - 0, 0, 0, 0, - 33}; // Extra bytes - ipAddress = Ip4Address.valueOf(value, 2); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - value = new byte[] {11, 22, // Preamble - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - 33}; // Extra bytes - ipAddress = Ip4Address.valueOf(value, 2); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests invalid valueOf() converger for an array and an invalid offset - * for IPv4. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfArrayInvalidOffsetIPv4() { - Ip4Address ipAddress; - byte[] value; - - value = new byte[] {11, 22, 33, // Preamble - 1, 2, 3, 4, - 44, 55}; // Extra bytes - ipAddress = Ip4Address.valueOf(value, 6); - } - - /** - * Tests valueOf() converter for IPv4 InetAddress. - */ - @Test - public void testValueOfInetAddressIPv4() { - Ip4Address ipAddress; - InetAddress inetAddress; - - inetAddress = InetAddresses.forString("1.2.3.4"); - ipAddress = Ip4Address.valueOf(inetAddress); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - inetAddress = InetAddresses.forString("0.0.0.0"); - ipAddress = Ip4Address.valueOf(inetAddress); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - inetAddress = InetAddresses.forString("255.255.255.255"); - ipAddress = Ip4Address.valueOf(inetAddress); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests valueOf() converter for IPv4 string. - */ - @Test - public void testValueOfStringIPv4() { - Ip4Address ipAddress; - - ipAddress = Ip4Address.valueOf("1.2.3.4"); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - ipAddress = Ip4Address.valueOf("0.0.0.0"); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - ipAddress = Ip4Address.valueOf("255.255.255.255"); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests invalid valueOf() converter for a null string. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullString() { - Ip4Address ipAddress; - - String fromString = null; - ipAddress = Ip4Address.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an empty string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfEmptyString() { - Ip4Address ipAddress; - - String fromString = ""; - ipAddress = Ip4Address.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an incorrect string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIncorrectString() { - Ip4Address ipAddress; - - String fromString = "NoSuchIpAddress"; - ipAddress = Ip4Address.valueOf(fromString); - } - - /** - * Tests making a mask prefix for a given prefix length for IPv4. - */ - @Test - public void testMakeMaskPrefixIPv4() { - Ip4Address ipAddress; - - ipAddress = Ip4Address.makeMaskPrefix(25); - assertThat(ipAddress.toString(), is("255.255.255.128")); - - ipAddress = Ip4Address.makeMaskPrefix(0); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - ipAddress = Ip4Address.makeMaskPrefix(32); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests making a mask prefix for an invalid prefix length for IPv4: - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeNegativeMaskPrefixIPv4() { - Ip4Address ipAddress; - - ipAddress = Ip4Address.makeMaskPrefix(-1); - } - - /** - * Tests making a mask prefix for an invalid prefix length for IPv4: - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeTooLongMaskPrefixIPv4() { - Ip4Address ipAddress; - - ipAddress = Ip4Address.makeMaskPrefix(33); - } - - /** - * Tests making of a masked address for IPv4. - */ - @Test - public void testMakeMaskedAddressIPv4() { - Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5"); - Ip4Address ipAddressMasked; - - ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 24); - assertThat(ipAddressMasked.toString(), is("1.2.3.0")); - - ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 0); - assertThat(ipAddressMasked.toString(), is("0.0.0.0")); - - ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 32); - assertThat(ipAddressMasked.toString(), is("1.2.3.5")); - } - - /** - * Tests making of a masked address for invalid prefix length for IPv4: - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeNegativeMaskedAddressIPv4() { - Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5"); - Ip4Address ipAddressMasked; - - ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, -1); - } - - /** - * Tests making of a masked address for an invalid prefix length for IPv4: - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeTooLongMaskedAddressIPv4() { - Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5"); - Ip4Address ipAddressMasked; - - ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 33); - } - - /** - * Tests comparison of {@link Ip4Address} for IPv4. - */ - @Test - public void testComparisonIPv4() { - Ip4Address addr1, addr2, addr3, addr4; - - addr1 = Ip4Address.valueOf("1.2.3.4"); - addr2 = Ip4Address.valueOf("1.2.3.4"); - addr3 = Ip4Address.valueOf("1.2.3.3"); - addr4 = Ip4Address.valueOf("1.2.3.5"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - - addr1 = Ip4Address.valueOf("255.2.3.4"); - addr2 = Ip4Address.valueOf("255.2.3.4"); - addr3 = Ip4Address.valueOf("255.2.3.3"); - addr4 = Ip4Address.valueOf("255.2.3.5"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - } - - /** - * Tests equality of {@link Ip4Address} for IPv4. - */ - @Test - public void testEqualityIPv4() { - new EqualsTester() - .addEqualityGroup(Ip4Address.valueOf("1.2.3.4"), - Ip4Address.valueOf("1.2.3.4")) - .addEqualityGroup(Ip4Address.valueOf("1.2.3.5"), - Ip4Address.valueOf("1.2.3.5")) - .addEqualityGroup(Ip4Address.valueOf("0.0.0.0"), - Ip4Address.valueOf("0.0.0.0")) - .addEqualityGroup(Ip4Address.valueOf("255.255.255.255"), - Ip4Address.valueOf("255.255.255.255")) - .testEquals(); - } - - /** - * Tests object string representation for IPv4. - */ - @Test - public void testToStringIPv4() { - Ip4Address ipAddress; - - ipAddress = Ip4Address.valueOf("1.2.3.4"); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - ipAddress = Ip4Address.valueOf("0.0.0.0"); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - ipAddress = Ip4Address.valueOf("255.255.255.255"); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4PrefixTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4PrefixTest.java deleted file mode 100644 index c731ed66..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip4PrefixTest.java +++ /dev/null @@ -1,534 +0,0 @@ -/* - * Copyright 2014 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import com.google.common.testing.EqualsTester; -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; - -/** - * Tests for class {@link Ip4Prefix}. - */ -public class Ip4PrefixTest { - /** - * Tests the immutability of {@link Ip4Prefix}. - */ - @Test - public void testImmutable() { - assertThatClassIsImmutable(Ip4Prefix.class); - } - - /** - * Tests the IPv4 prefix address version constant. - */ - @Test - public void testAddressVersion() { - assertThat(Ip4Prefix.VERSION, is(IpAddress.Version.INET)); - } - - /** - * Tests the maximum mask length. - */ - @Test - public void testMaxMaskLength() { - assertThat(Ip4Prefix.MAX_MASK_LENGTH, is(32)); - } - - /** - * Tests returning the IP version of the prefix. - */ - @Test - public void testVersion() { - Ip4Prefix ipPrefix; - - // IPv4 - ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); - assertThat(ipPrefix.version(), is(IpAddress.Version.INET)); - } - - /** - * Tests returning the IP address value and IP address prefix length of - * an IPv4 prefix. - */ - @Test - public void testAddressAndPrefixLengthIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24"); - assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0"))); - assertThat(ipPrefix.prefixLength(), is(24)); - - ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24"); - assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0"))); - assertThat(ipPrefix.prefixLength(), is(24)); - - ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32"); - assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.4"))); - assertThat(ipPrefix.prefixLength(), is(32)); - - ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32"); - assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.5"))); - assertThat(ipPrefix.prefixLength(), is(32)); - - ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); - assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("0.0.0.0"))); - assertThat(ipPrefix.prefixLength(), is(0)); - - ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); - assertThat(ipPrefix.address(), - equalTo(Ip4Address.valueOf("255.255.255.255"))); - assertThat(ipPrefix.prefixLength(), is(32)); - } - - /** - * Tests valueOf() converter for IPv4 integer value. - */ - @Test - public void testValueOfForIntegerIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf(0x01020304, 24); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = Ip4Prefix.valueOf(0x01020304, 32); - assertThat(ipPrefix.toString(), is("1.2.3.4/32")); - - ipPrefix = Ip4Prefix.valueOf(0x01020305, 32); - assertThat(ipPrefix.toString(), is("1.2.3.5/32")); - - ipPrefix = Ip4Prefix.valueOf(0, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf(0, 32); - assertThat(ipPrefix.toString(), is("0.0.0.0/32")); - - ipPrefix = Ip4Prefix.valueOf(0xffffffff, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf(0xffffffff, 16); - assertThat(ipPrefix.toString(), is("255.255.0.0/16")); - - ipPrefix = Ip4Prefix.valueOf(0xffffffff, 32); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests invalid valueOf() converter for IPv4 integer value and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf(0x01020304, -1); - } - - /** - * Tests invalid valueOf() converter for IPv4 integer value and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf(0x01020304, 33); - } - - /** - * Tests valueOf() converter for IPv4 byte array. - */ - @Test - public void testValueOfByteArrayIPv4() { - Ip4Prefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipPrefix = Ip4Prefix.valueOf(value, 24); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = Ip4Prefix.valueOf(value, 32); - assertThat(ipPrefix.toString(), is("1.2.3.4/32")); - - value = new byte[] {1, 2, 3, 5}; - ipPrefix = Ip4Prefix.valueOf(value, 32); - assertThat(ipPrefix.toString(), is("1.2.3.5/32")); - - value = new byte[] {0, 0, 0, 0}; - ipPrefix = Ip4Prefix.valueOf(value, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf(value, 32); - assertThat(ipPrefix.toString(), is("0.0.0.0/32")); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipPrefix = Ip4Prefix.valueOf(value, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf(value, 16); - assertThat(ipPrefix.toString(), is("255.255.0.0/16")); - - ipPrefix = Ip4Prefix.valueOf(value, 32); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests invalid valueOf() converter for a null array for IPv4. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullArrayIPv4() { - Ip4Prefix ipPrefix; - byte[] value; - - value = null; - ipPrefix = Ip4Prefix.valueOf(value, 24); - } - - /** - * Tests invalid valueOf() converter for a short array for IPv4. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfShortArrayIPv4() { - Ip4Prefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3}; - ipPrefix = Ip4Prefix.valueOf(value, 24); - } - - /** - * Tests invalid valueOf() converter for IPv4 byte array and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() { - Ip4Prefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipPrefix = Ip4Prefix.valueOf(value, -1); - } - - /** - * Tests invalid valueOf() converter for IPv4 byte array and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() { - Ip4Prefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipPrefix = Ip4Prefix.valueOf(value, 33); - } - - /** - * Tests valueOf() converter for IPv4 address. - */ - @Test - public void testValueOfAddressIPv4() { - Ip4Address ipAddress; - Ip4Prefix ipPrefix; - - ipAddress = Ip4Address.valueOf("1.2.3.4"); - ipPrefix = Ip4Prefix.valueOf(ipAddress, 24); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = Ip4Prefix.valueOf(ipAddress, 32); - assertThat(ipPrefix.toString(), is("1.2.3.4/32")); - - ipAddress = Ip4Address.valueOf("1.2.3.5"); - ipPrefix = Ip4Prefix.valueOf(ipAddress, 32); - assertThat(ipPrefix.toString(), is("1.2.3.5/32")); - - ipAddress = Ip4Address.valueOf("0.0.0.0"); - ipPrefix = Ip4Prefix.valueOf(ipAddress, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf(ipAddress, 32); - assertThat(ipPrefix.toString(), is("0.0.0.0/32")); - - ipAddress = Ip4Address.valueOf("255.255.255.255"); - ipPrefix = Ip4Prefix.valueOf(ipAddress, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf(ipAddress, 16); - assertThat(ipPrefix.toString(), is("255.255.0.0/16")); - - ipPrefix = Ip4Prefix.valueOf(ipAddress, 32); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests invalid valueOf() converter for a null IP address. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullAddress() { - Ip4Address ipAddress; - Ip4Prefix ipPrefix; - - ipAddress = null; - ipPrefix = Ip4Prefix.valueOf(ipAddress, 24); - } - - /** - * Tests invalid valueOf() converter for IPv4 address and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfAddressNegativePrefixLengthIPv4() { - Ip4Address ipAddress; - Ip4Prefix ipPrefix; - - ipAddress = Ip4Address.valueOf("1.2.3.4"); - ipPrefix = Ip4Prefix.valueOf(ipAddress, -1); - } - - /** - * Tests invalid valueOf() converter for IPv4 address and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() { - Ip4Address ipAddress; - Ip4Prefix ipPrefix; - - ipAddress = Ip4Address.valueOf("1.2.3.4"); - ipPrefix = Ip4Prefix.valueOf(ipAddress, 33); - } - - /** - * Tests valueOf() converter for IPv4 string. - */ - @Test - public void testValueOfStringIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24"); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32"); - assertThat(ipPrefix.toString(), is("1.2.3.4/32")); - - ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32"); - assertThat(ipPrefix.toString(), is("1.2.3.5/32")); - - ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf("0.0.0.0/32"); - assertThat(ipPrefix.toString(), is("0.0.0.0/32")); - - ipPrefix = Ip4Prefix.valueOf("255.255.255.255/0"); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf("255.255.255.255/16"); - assertThat(ipPrefix.toString(), is("255.255.0.0/16")); - - ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests invalid valueOf() converter for a null string. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullString() { - Ip4Prefix ipPrefix; - String fromString; - - fromString = null; - ipPrefix = Ip4Prefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an empty string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfEmptyString() { - Ip4Prefix ipPrefix; - String fromString; - - fromString = ""; - ipPrefix = Ip4Prefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an incorrect string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIncorrectString() { - Ip4Prefix ipPrefix; - String fromString; - - fromString = "NoSuchIpPrefix"; - ipPrefix = Ip4Prefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for IPv4 string and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfStringNegativePrefixLengthIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf("1.2.3.4/-1"); - } - - /** - * Tests invalid valueOf() converter for IPv4 string and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfStringTooLongPrefixLengthIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf("1.2.3.4/33"); - } - - /** - * Tests IP prefix contains another IP prefix for IPv4. - */ - @Test - public void testContainsIpPrefixIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24"); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32"))); - - ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32"); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32"))); - - ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32"))); - - ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16"))); - assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0"))); - assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32"))); - } - - /** - * Tests IP prefix contains IP address for IPv4. - */ - @Test - public void testContainsIpAddressIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24"); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0"))); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255"))); - - ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32"); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255"))); - - ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0"))); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4"))); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0"))); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0"))); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255"))); - - ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0"))); - assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0"))); - assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255"))); - } - - /** - * Tests equality of {@link Ip4Prefix} for IPv4. - */ - @Test - public void testEqualityIPv4() { - new EqualsTester() - .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/24"), - Ip4Prefix.valueOf("1.2.0.0/24"), - Ip4Prefix.valueOf("1.2.0.4/24")) - .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/16"), - Ip4Prefix.valueOf("1.2.0.0/16")) - .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/32"), - Ip4Prefix.valueOf("1.2.0.0/32")) - .addEqualityGroup(Ip4Prefix.valueOf("1.3.0.0/24"), - Ip4Prefix.valueOf("1.3.0.0/24")) - .addEqualityGroup(Ip4Prefix.valueOf("0.0.0.0/0"), - Ip4Prefix.valueOf("0.0.0.0/0")) - .addEqualityGroup(Ip4Prefix.valueOf("255.255.255.255/32"), - Ip4Prefix.valueOf("255.255.255.255/32")) - .testEquals(); - } - - /** - * Tests object string representation for IPv4. - */ - @Test - public void testToStringIPv4() { - Ip4Prefix ipPrefix; - - ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24"); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24"); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6AddressTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6AddressTest.java deleted file mode 100644 index e7d017d1..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6AddressTest.java +++ /dev/null @@ -1,499 +0,0 @@ -/* - * Copyright 2014 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import com.google.common.net.InetAddresses; -import com.google.common.testing.EqualsTester; -import org.junit.Test; - -import java.net.InetAddress; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; - -/** - * Tests for class {@link Ip6Address}. - */ -public class Ip6AddressTest { - /** - * Tests the immutability of {@link Ip6Address}. - */ - @Test - public void testImmutable() { - assertThatClassIsImmutable(Ip6Address.class); - } - - /** - * Tests the IPv4 address version constant. - */ - @Test - public void testAddressVersion() { - assertThat(Ip6Address.VERSION, is(IpAddress.Version.INET6)); - } - - /** - * Tests the length of the address in bytes (octets). - */ - @Test - public void testAddrByteLength() { - assertThat(Ip6Address.BYTE_LENGTH, is(16)); - } - - /** - * Tests the length of the address in bits. - */ - @Test - public void testAddrBitLength() { - assertThat(Ip6Address.BIT_LENGTH, is(128)); - } - - /** - * Tests returning the IP address version. - */ - @Test - public void testVersion() { - IpAddress ipAddress; - - // IPv6 - ipAddress = IpAddress.valueOf("::"); - assertThat(ipAddress.version(), is(IpAddress.Version.INET6)); - } - - /** - * Tests returning an IPv6 address as a byte array. - */ - @Test - public void testAddressToOctetsIPv6() { - Ip6Address ipAddress; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, - (byte) 0x88, (byte) 0x88}; - ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - assertThat(ipAddress.toOctets(), is(value)); - - value = new byte[] {0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - ipAddress = Ip6Address.valueOf("::"); - assertThat(ipAddress.toOctets(), is(value)); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipAddress = - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - assertThat(ipAddress.toOctets(), is(value)); - } - - /** - * Tests valueOf() converter for IPv6 byte array. - */ - @Test - public void testValueOfByteArrayIPv6() { - Ip6Address ipAddress; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, - (byte) 0x88, (byte) 0x88}; - ipAddress = Ip6Address.valueOf(value); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - value = new byte[] {0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - ipAddress = Ip6Address.valueOf(value); - assertThat(ipAddress.toString(), is("::")); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipAddress = Ip6Address.valueOf(value); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } - - /** - * Tests invalid valueOf() converter for a null array for IPv6. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullArrayIPv6() { - Ip6Address ipAddress; - byte[] value; - - value = null; - ipAddress = Ip6Address.valueOf(value); - } - - /** - * Tests invalid valueOf() converger for an array that is too short for - * IPv6. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfShortArrayIPv6() { - Ip6Address ipAddress; - byte[] value; - - value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; - ipAddress = Ip6Address.valueOf(value); - } - - /** - * Tests valueOf() converter for IPv6 byte array and an offset. - */ - @Test - public void testValueOfByteArrayOffsetIPv6() { - Ip6Address ipAddress; - byte[] value; - - value = new byte[] {11, 22, 33, // Preamble - 0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, - (byte) 0x88, (byte) 0x88, - 44, 55}; // Extra bytes - ipAddress = Ip6Address.valueOf(value, 3); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - value = new byte[] {11, 22, // Preamble - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 33}; // Extra bytes - ipAddress = Ip6Address.valueOf(value, 2); - assertThat(ipAddress.toString(), is("::")); - - value = new byte[] {11, 22, // Preamble - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - 33}; // Extra bytes - ipAddress = Ip6Address.valueOf(value, 2); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } - - /** - * Tests invalid valueOf() converger for an array and an invalid offset - * for IPv6. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfArrayInvalidOffsetIPv6() { - Ip6Address ipAddress; - byte[] value; - - value = new byte[] {11, 22, 33, // Preamble - 0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, - (byte) 0x88, (byte) 0x88, - 44, 55}; // Extra bytes - ipAddress = Ip6Address.valueOf(value, 6); - } - - /** - * Tests valueOf() converter for IPv6 InetAddress. - */ - @Test - public void testValueOfInetAddressIPv6() { - Ip6Address ipAddress; - InetAddress inetAddress; - - inetAddress = - InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888"); - ipAddress = Ip6Address.valueOf(inetAddress); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - inetAddress = InetAddresses.forString("::"); - ipAddress = Ip6Address.valueOf(inetAddress); - assertThat(ipAddress.toString(), is("::")); - - inetAddress = - InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - ipAddress = Ip6Address.valueOf(inetAddress); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } - - /** - * Tests valueOf() converter for IPv6 string. - */ - @Test - public void testValueOfStringIPv6() { - Ip6Address ipAddress; - - ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - ipAddress = Ip6Address.valueOf("::"); - assertThat(ipAddress.toString(), is("::")); - - ipAddress = - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } - - /** - * Tests invalid valueOf() converter for a null string. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullString() { - Ip6Address ipAddress; - - String fromString = null; - ipAddress = Ip6Address.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an empty string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfEmptyString() { - Ip6Address ipAddress; - - String fromString = ""; - ipAddress = Ip6Address.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an incorrect string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIncorrectString() { - Ip6Address ipAddress; - - String fromString = "NoSuchIpAddress"; - ipAddress = Ip6Address.valueOf(fromString); - } - - /** - * Tests making a mask prefix for a given prefix length for IPv6. - */ - @Test - public void testMakeMaskPrefixIPv6() { - Ip6Address ipAddress; - - ipAddress = Ip6Address.makeMaskPrefix(8); - assertThat(ipAddress.toString(), is("ff00::")); - - ipAddress = Ip6Address.makeMaskPrefix(120); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00")); - - ipAddress = Ip6Address.makeMaskPrefix(0); - assertThat(ipAddress.toString(), is("::")); - - ipAddress = Ip6Address.makeMaskPrefix(128); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - - ipAddress = Ip6Address.makeMaskPrefix(64); - assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::")); - } - - /** - * Tests making a mask prefix for an invalid prefix length for IPv6: - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeNegativeMaskPrefixIPv6() { - Ip6Address ipAddress; - - ipAddress = Ip6Address.makeMaskPrefix(-1); - } - - /** - * Tests making a mask prefix for an invalid prefix length for IPv6: - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeTooLongMaskPrefixIPv6() { - Ip6Address ipAddress; - - ipAddress = Ip6Address.makeMaskPrefix(129); - } - - /** - * Tests making of a masked address for IPv6. - */ - @Test - public void testMakeMaskedAddressIPv6() { - Ip6Address ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); - Ip6Address ipAddressMasked; - - ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 8); - assertThat(ipAddressMasked.toString(), is("1100::")); - - ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 120); - assertThat(ipAddressMasked.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8800")); - - ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 0); - assertThat(ipAddressMasked.toString(), is("::")); - - ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 128); - assertThat(ipAddressMasked.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8885")); - - ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 64); - assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::")); - } - - /** - * Tests making of a masked address for invalid prefix length for IPv6: - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeNegativeMaskedAddressIPv6() { - Ip6Address ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); - Ip6Address ipAddressMasked; - - ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, -1); - } - - /** - * Tests making of a masked address for an invalid prefix length for IPv6: - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeTooLongMaskedAddressIPv6() { - Ip6Address ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); - Ip6Address ipAddressMasked; - - ipAddressMasked = Ip6Address.makeMaskedAddress(ipAddress, 129); - } - - /** - * Tests comparison of {@link Ip6Address} for IPv6. - */ - @Test - public void testComparisonIPv6() { - Ip6Address addr1, addr2, addr3, addr4; - - addr1 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - addr2 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - addr3 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8887"); - addr4 = Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8889"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - - addr1 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); - addr2 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); - addr3 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887"); - addr4 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - - addr1 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); - addr2 = Ip6Address.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); - addr3 = Ip6Address.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888"); - addr4 = Ip6Address.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - } - - /** - * Tests equality of {@link Ip6Address} for IPv6. - */ - @Test - public void testEqualityIPv6() { - new EqualsTester() - .addEqualityGroup( - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"), - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888")) - .addEqualityGroup( - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"), - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:888a")) - .addEqualityGroup( - Ip6Address.valueOf("::"), - Ip6Address.valueOf("::")) - .addEqualityGroup( - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) - .testEquals(); - } - - /** - * Tests object string representation for IPv6. - */ - @Test - public void testToStringIPv6() { - Ip6Address ipAddress; - - ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - ipAddress = Ip6Address.valueOf("1111::8888"); - assertThat(ipAddress.toString(), is("1111::8888")); - - ipAddress = Ip6Address.valueOf("1111::"); - assertThat(ipAddress.toString(), is("1111::")); - - ipAddress = Ip6Address.valueOf("::8888"); - assertThat(ipAddress.toString(), is("::8888")); - - ipAddress = Ip6Address.valueOf("::"); - assertThat(ipAddress.toString(), is("::")); - - ipAddress = - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6PrefixTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6PrefixTest.java deleted file mode 100644 index dceeb840..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/Ip6PrefixTest.java +++ /dev/null @@ -1,568 +0,0 @@ -/* - * Copyright 2014 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import com.google.common.testing.EqualsTester; -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; - -/** - * Tests for class {@link Ip6Prefix}. - */ -public class Ip6PrefixTest { - /** - * Tests the immutability of {@link Ip6Prefix}. - */ - @Test - public void testImmutable() { - assertThatClassIsImmutable(Ip6Prefix.class); - } - - /** - * Tests the IPv4 prefix address version constant. - */ - @Test - public void testAddressVersion() { - assertThat(Ip6Prefix.VERSION, is(IpAddress.Version.INET6)); - } - - /** - * Tests the maximum mask length. - */ - @Test - public void testMaxMaskLength() { - assertThat(Ip6Prefix.MAX_MASK_LENGTH, is(128)); - } - - /** - * Tests returning the IP version of the prefix. - */ - @Test - public void testVersion() { - Ip6Prefix ipPrefix; - - // IPv6 - ipPrefix = Ip6Prefix.valueOf("::/0"); - assertThat(ipPrefix.version(), is(IpAddress.Version.INET6)); - } - - /** - * Tests returning the IP address value and IP address prefix length of - * an IPv6 prefix. - */ - @Test - public void testAddressAndPrefixLengthIPv6() { - Ip6Prefix ipPrefix; - - ipPrefix = Ip6Prefix.valueOf("1100::/8"); - assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::"))); - assertThat(ipPrefix.prefixLength(), is(8)); - - ipPrefix = - Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8"); - assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("1100::"))); - assertThat(ipPrefix.prefixLength(), is(8)); - - ipPrefix = - Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120"); - assertThat(ipPrefix.address(), - equalTo(Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8800"))); - assertThat(ipPrefix.prefixLength(), is(120)); - - ipPrefix = - Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128"); - assertThat(ipPrefix.address(), - equalTo(Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"))); - assertThat(ipPrefix.prefixLength(), is(128)); - - ipPrefix = Ip6Prefix.valueOf("::/0"); - assertThat(ipPrefix.address(), equalTo(Ip6Address.valueOf("::"))); - assertThat(ipPrefix.prefixLength(), is(0)); - - ipPrefix = - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertThat(ipPrefix.address(), - equalTo(Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - assertThat(ipPrefix.prefixLength(), is(128)); - - ipPrefix = - Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64"); - assertThat(ipPrefix.address(), - equalTo(Ip6Address.valueOf("1111:2222:3333:4444::"))); - assertThat(ipPrefix.prefixLength(), is(64)); - } - - /** - * Tests valueOf() converter for IPv6 byte array. - */ - @Test - public void testValueOfByteArrayIPv6() { - Ip6Prefix ipPrefix; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, (byte) 0x88, (byte) 0x88}; - ipPrefix = Ip6Prefix.valueOf(value, 120); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8800/120")); - - ipPrefix = Ip6Prefix.valueOf(value, 128); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888/128")); - - value = new byte[] {0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - ipPrefix = Ip6Prefix.valueOf(value, 0); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = Ip6Prefix.valueOf(value, 128); - assertThat(ipPrefix.toString(), is("::/128")); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipPrefix = Ip6Prefix.valueOf(value, 0); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = Ip6Prefix.valueOf(value, 64); - assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); - - ipPrefix = Ip6Prefix.valueOf(value, 128); - assertThat(ipPrefix.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); - } - - /** - * Tests invalid valueOf() converter for a null array for IPv6. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullArrayIPv6() { - Ip6Prefix ipPrefix; - byte[] value; - - value = null; - ipPrefix = Ip6Prefix.valueOf(value, 120); - } - - /** - * Tests invalid valueOf() converter for a short array for IPv6. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfShortArrayIPv6() { - Ip6Prefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; - ipPrefix = Ip6Prefix.valueOf(value, 120); - } - - /** - * Tests invalid valueOf() converter for IPv6 byte array and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() { - Ip6Prefix ipPrefix; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, (byte) 0x88, (byte) 0x88}; - ipPrefix = Ip6Prefix.valueOf(value, -1); - } - - /** - * Tests invalid valueOf() converter for IPv6 byte array and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() { - Ip6Prefix ipPrefix; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, (byte) 0x88, (byte) 0x88}; - ipPrefix = Ip6Prefix.valueOf(value, 129); - } - - /** - * Tests valueOf() converter for IPv6 address. - */ - @Test - public void testValueOfAddressIPv6() { - Ip6Address ipAddress; - Ip6Prefix ipPrefix; - - ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - ipPrefix = Ip6Prefix.valueOf(ipAddress, 120); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8800/120")); - - ipPrefix = Ip6Prefix.valueOf(ipAddress, 128); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888/128")); - - ipAddress = Ip6Address.valueOf("::"); - ipPrefix = Ip6Prefix.valueOf(ipAddress, 0); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = Ip6Prefix.valueOf(ipAddress, 128); - assertThat(ipPrefix.toString(), is("::/128")); - - ipAddress = - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - ipPrefix = Ip6Prefix.valueOf(ipAddress, 0); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = Ip6Prefix.valueOf(ipAddress, 64); - assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); - - ipPrefix = Ip6Prefix.valueOf(ipAddress, 128); - assertThat(ipPrefix.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); - } - - /** - * Tests invalid valueOf() converter for a null IP address. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullAddress() { - Ip6Address ipAddress; - Ip6Prefix ipPrefix; - - ipAddress = null; - ipPrefix = Ip6Prefix.valueOf(ipAddress, 24); - } - - /** - * Tests invalid valueOf() converter for IPv6 address and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfAddressNegativePrefixLengthIPv6() { - Ip6Address ipAddress; - Ip6Prefix ipPrefix; - - ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - ipPrefix = Ip6Prefix.valueOf(ipAddress, -1); - } - - /** - * Tests invalid valueOf() converter for IPv6 address and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() { - Ip6Address ipAddress; - Ip6Prefix ipPrefix; - - ipAddress = - Ip6Address.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - ipPrefix = Ip6Prefix.valueOf(ipAddress, 129); - } - - /** - * Tests valueOf() converter for IPv6 string. - */ - @Test - public void testValueOfStringIPv6() { - Ip6Prefix ipPrefix; - - ipPrefix = - Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120"); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8800/120")); - - ipPrefix = - Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128"); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888/128")); - - ipPrefix = Ip6Prefix.valueOf("::/0"); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = Ip6Prefix.valueOf("::/128"); - assertThat(ipPrefix.toString(), is("::/128")); - - ipPrefix = - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"); - assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); - - ipPrefix = - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertThat(ipPrefix.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); - } - - /** - * Tests invalid valueOf() converter for a null string. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullString() { - Ip6Prefix ipPrefix; - String fromString; - - fromString = null; - ipPrefix = Ip6Prefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an empty string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfEmptyString() { - Ip6Prefix ipPrefix; - String fromString; - - fromString = ""; - ipPrefix = Ip6Prefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an incorrect string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIncorrectString() { - Ip6Prefix ipPrefix; - String fromString; - - fromString = "NoSuchIpPrefix"; - ipPrefix = Ip6Prefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for IPv6 string and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfStringNegativePrefixLengthIPv6() { - Ip6Prefix ipPrefix; - - ipPrefix = - Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1"); - } - - /** - * Tests invalid valueOf() converter for IPv6 string and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfStringTooLongPrefixLengthIPv6() { - Ip6Prefix ipPrefix; - - ipPrefix = - Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129"); - } - - /** - * Tests IP prefix contains another IP prefix for IPv6. - */ - @Test - public void testContainsIpPrefixIPv6() { - Ip6Prefix ipPrefix; - - ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/120"); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::1/128"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4445::/120"))); - assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64"))); - assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - - ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/128"); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::1/128"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4445::/120"))); - assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64"))); - assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - - ipPrefix = Ip6Prefix.valueOf("::/0"); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::1/128"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4445::/120"))); - assertTrue(ipPrefix.contains(Ip6Prefix.valueOf("::/64"))); - assertTrue(ipPrefix.contains(Ip6Prefix.valueOf("::/0"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - - ipPrefix = - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/120"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/128"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::1/128"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4444::/64"))); - assertFalse(ipPrefix.contains( - Ip6Prefix.valueOf("1111:2222:3333:4445::/120"))); - assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/64"))); - assertFalse(ipPrefix.contains(Ip6Prefix.valueOf("::/0"))); - assertTrue(ipPrefix.contains( - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - } - - /** - * Tests IP prefix contains IP address for IPv6. - */ - @Test - public void testContainsIpAddressIPv6() { - Ip6Prefix ipPrefix; - - ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/120"); - assertTrue(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4444::"))); - assertTrue(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4444::1"))); - assertFalse(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4445::"))); - assertFalse(ipPrefix.contains(Ip6Address.valueOf("::"))); - assertFalse(ipPrefix.contains( - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - - ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444::/128"); - assertTrue(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4444::"))); - assertFalse(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4444::1"))); - assertFalse(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4445::"))); - assertFalse(ipPrefix.contains(Ip6Address.valueOf("::"))); - assertFalse(ipPrefix.contains( - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - - ipPrefix = Ip6Prefix.valueOf("::/0"); - assertTrue(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4444::"))); - assertTrue(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4444::1"))); - assertTrue(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4445::"))); - assertTrue(ipPrefix.contains(Ip6Address.valueOf("::"))); - assertTrue(ipPrefix.contains( - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - - ipPrefix = - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertFalse(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4444::"))); - assertFalse(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4444::1"))); - assertFalse(ipPrefix.contains( - Ip6Address.valueOf("1111:2222:3333:4445::"))); - assertFalse(ipPrefix.contains(Ip6Address.valueOf("::"))); - assertTrue(ipPrefix.contains( - Ip6Address.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - } - - /** - * Tests equality of {@link Ip6Prefix} for IPv6. - */ - @Test - public void testEqualityIPv6() { - new EqualsTester() - .addEqualityGroup( - Ip6Prefix.valueOf("1111:2222:3333:4444::/120"), - Ip6Prefix.valueOf("1111:2222:3333:4444::1/120"), - Ip6Prefix.valueOf("1111:2222:3333:4444::/120")) - .addEqualityGroup( - Ip6Prefix.valueOf("1111:2222:3333:4444::/64"), - Ip6Prefix.valueOf("1111:2222:3333:4444::/64")) - .addEqualityGroup( - Ip6Prefix.valueOf("1111:2222:3333:4444::/128"), - Ip6Prefix.valueOf("1111:2222:3333:4444::/128")) - .addEqualityGroup( - Ip6Prefix.valueOf("1111:2222:3333:4445::/64"), - Ip6Prefix.valueOf("1111:2222:3333:4445::/64")) - .addEqualityGroup( - Ip6Prefix.valueOf("::/0"), - Ip6Prefix.valueOf("::/0")) - .addEqualityGroup( - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"), - Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")) - .testEquals(); - } - - /** - * Tests object string representation for IPv6. - */ - @Test - public void testToStringIPv6() { - Ip6Prefix ipPrefix; - - ipPrefix = Ip6Prefix.valueOf("1100::/8"); - assertThat(ipPrefix.toString(), is("1100::/8")); - - ipPrefix = Ip6Prefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8"); - assertThat(ipPrefix.toString(), is("1100::/8")); - - ipPrefix = Ip6Prefix.valueOf("::/0"); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = Ip6Prefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertThat(ipPrefix.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java deleted file mode 100644 index 8618c466..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpAddressTest.java +++ /dev/null @@ -1,946 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import java.net.InetAddress; - -import org.junit.Test; - -import com.google.common.net.InetAddresses; -import com.google.common.testing.EqualsTester; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; -import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; - -/** - * Tests for class {@link IpAddress}. - */ -public class IpAddressTest { - /** - * Tests the immutability of {@link IpAddress}. - */ - @Test - public void testImmutable() { - assertThatClassIsImmutableBaseClass(IpAddress.class); - } - - /** - * Tests the length of the address in bytes (octets). - */ - @Test - public void testAddrByteLength() { - assertThat(IpAddress.INET_BYTE_LENGTH, is(4)); - assertThat(IpAddress.INET6_BYTE_LENGTH, is(16)); - assertThat(IpAddress.byteLength(IpAddress.Version.INET), is(4)); - assertThat(IpAddress.byteLength(IpAddress.Version.INET6), is(16)); - } - - /** - * Tests the length of the address in bits. - */ - @Test - public void testAddrBitLength() { - assertThat(IpAddress.INET_BIT_LENGTH, is(32)); - assertThat(IpAddress.INET6_BIT_LENGTH, is(128)); - } - - /** - * Tests returning the IP address version. - */ - @Test - public void testVersion() { - IpAddress ipAddress; - - // IPv4 - ipAddress = IpAddress.valueOf("0.0.0.0"); - assertThat(ipAddress.version(), is(IpAddress.Version.INET)); - - // IPv6 - ipAddress = IpAddress.valueOf("::"); - assertThat(ipAddress.version(), is(IpAddress.Version.INET6)); - } - - /** - * Tests whether the IP version of an address is IPv4. - */ - @Test - public void testIsIp4() { - IpAddress ipAddress; - - // IPv4 - ipAddress = IpAddress.valueOf("0.0.0.0"); - assertTrue(ipAddress.isIp4()); - - // IPv6 - ipAddress = IpAddress.valueOf("::"); - assertFalse(ipAddress.isIp4()); - } - - /** - * Tests whether the IP version of an address is IPv6. - */ - @Test - public void testIsIp6() { - IpAddress ipAddress; - - // IPv4 - ipAddress = IpAddress.valueOf("0.0.0.0"); - assertFalse(ipAddress.isIp6()); - - // IPv6 - ipAddress = IpAddress.valueOf("::"); - assertTrue(ipAddress.isIp6()); - } - - /** - * Tests getting the Ip4Address and Ip6Address view of the IP address. - */ - @Test - public void testGetIp4AndIp6AddressView() { - IpAddress ipAddress; - Ip4Address ip4Address; - Ip6Address ip6Address; - - // Pure IPv4 IpAddress - ipAddress = IpAddress.valueOf("1.2.3.4"); - ip4Address = ipAddress.getIp4Address(); - ip6Address = ipAddress.getIp6Address(); - assertThat(ip4Address.toString(), is("1.2.3.4")); - assertNull(ip6Address); - - // IPv4 IpAddress that is Ip4Address - ipAddress = Ip4Address.valueOf("1.2.3.4"); - ip4Address = ipAddress.getIp4Address(); - ip6Address = ipAddress.getIp6Address(); - assertThat(ip4Address.toString(), is("1.2.3.4")); - assertNull(ip6Address); - - // Pure IPv6 IpAddress - ipAddress = IpAddress.valueOf("1111:2222::"); - ip4Address = ipAddress.getIp4Address(); - ip6Address = ipAddress.getIp6Address(); - assertNull(ip4Address); - assertThat(ip6Address.toString(), is("1111:2222::")); - - // IPv6 IpAddress that is Ip6Address - ipAddress = Ip6Address.valueOf("1111:2222::"); - ip4Address = ipAddress.getIp4Address(); - ip6Address = ipAddress.getIp6Address(); - assertNull(ip4Address); - assertThat(ip6Address.toString(), is("1111:2222::")); - } - - /** - * Tests returning an IPv4 address as a byte array. - */ - @Test - public void testAddressToOctetsIPv4() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipAddress = IpAddress.valueOf("1.2.3.4"); - assertThat(ipAddress.toOctets(), is(value)); - - value = new byte[] {0, 0, 0, 0}; - ipAddress = IpAddress.valueOf("0.0.0.0"); - assertThat(ipAddress.toOctets(), is(value)); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipAddress = IpAddress.valueOf("255.255.255.255"); - assertThat(ipAddress.toOctets(), is(value)); - } - - /** - * Tests returning an IPv6 address as a byte array. - */ - @Test - public void testAddressToOctetsIPv6() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, - (byte) 0x88, (byte) 0x88}; - ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - assertThat(ipAddress.toOctets(), is(value)); - - value = new byte[] {0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - ipAddress = IpAddress.valueOf("::"); - assertThat(ipAddress.toOctets(), is(value)); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipAddress = - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - assertThat(ipAddress.toOctets(), is(value)); - } - - /** - * Tests valueOf() converter for IPv4 integer value. - */ - @Test - public void testValueOfForIntegerIPv4() { - IpAddress ipAddress; - - ipAddress = IpAddress.valueOf(0x01020304); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - ipAddress = IpAddress.valueOf(0); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - ipAddress = IpAddress.valueOf(0xffffffff); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests valueOf() converter for IPv4 byte array. - */ - @Test - public void testValueOfByteArrayIPv4() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - value = new byte[] {0, 0, 0, 0}; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests valueOf() converter for IPv6 byte array. - */ - @Test - public void testValueOfByteArrayIPv6() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, - (byte) 0x88, (byte) 0x88}; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - value = new byte[] {0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); - assertThat(ipAddress.toString(), is("::")); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } - - /** - * Tests invalid valueOf() converter for a null array for IPv4. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullArrayIPv4() { - IpAddress ipAddress; - byte[] value; - - value = null; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); - } - - /** - * Tests invalid valueOf() converter for a null array for IPv6. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullArrayIPv6() { - IpAddress ipAddress; - byte[] value; - - value = null; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); - } - - /** - * Tests invalid valueOf() converger for an array that is too short for - * IPv4. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfShortArrayIPv4() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {1, 2, 3}; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value); - } - - /** - * Tests invalid valueOf() converger for an array that is too short for - * IPv6. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfShortArrayIPv6() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value); - } - - /** - * Tests valueOf() converter for IPv4 byte array and an offset. - */ - @Test - public void testValueOfByteArrayOffsetIPv4() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {11, 22, 33, // Preamble - 1, 2, 3, 4, - 44, 55}; // Extra bytes - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 3); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - value = new byte[] {11, 22, // Preamble - 0, 0, 0, 0, - 33}; // Extra bytes - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - value = new byte[] {11, 22, // Preamble - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - 33}; // Extra bytes - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests valueOf() converter for IPv6 byte array and an offset. - */ - @Test - public void testValueOfByteArrayOffsetIPv6() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {11, 22, 33, // Preamble - 0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, - (byte) 0x88, (byte) 0x88, - 44, 55}; // Extra bytes - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 3); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - value = new byte[] {11, 22, // Preamble - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 33}; // Extra bytes - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2); - assertThat(ipAddress.toString(), is("::")); - - value = new byte[] {11, 22, // Preamble - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - 33}; // Extra bytes - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } - - /** - * Tests invalid valueOf() converger for an array and an invalid offset - * for IPv4. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfArrayInvalidOffsetIPv4() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {11, 22, 33, // Preamble - 1, 2, 3, 4, - 44, 55}; // Extra bytes - ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 6); - } - - /** - * Tests invalid valueOf() converger for an array and an invalid offset - * for IPv6. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfArrayInvalidOffsetIPv6() { - IpAddress ipAddress; - byte[] value; - - value = new byte[] {11, 22, 33, // Preamble - 0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, - (byte) 0x88, (byte) 0x88, - 44, 55}; // Extra bytes - ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 6); - } - - /** - * Tests valueOf() converter for IPv4 InetAddress. - */ - @Test - public void testValueOfInetAddressIPv4() { - IpAddress ipAddress; - InetAddress inetAddress; - - inetAddress = InetAddresses.forString("1.2.3.4"); - ipAddress = IpAddress.valueOf(inetAddress); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - inetAddress = InetAddresses.forString("0.0.0.0"); - ipAddress = IpAddress.valueOf(inetAddress); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - inetAddress = InetAddresses.forString("255.255.255.255"); - ipAddress = IpAddress.valueOf(inetAddress); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests valueOf() converter for IPv6 InetAddress. - */ - @Test - public void testValueOfInetAddressIPv6() { - IpAddress ipAddress; - InetAddress inetAddress; - - inetAddress = - InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888"); - ipAddress = IpAddress.valueOf(inetAddress); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - inetAddress = InetAddresses.forString("::"); - ipAddress = IpAddress.valueOf(inetAddress); - assertThat(ipAddress.toString(), is("::")); - - inetAddress = - InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - ipAddress = IpAddress.valueOf(inetAddress); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } - - /** - * Tests valueOf() converter for IPv4 string. - */ - @Test - public void testValueOfStringIPv4() { - IpAddress ipAddress; - - ipAddress = IpAddress.valueOf("1.2.3.4"); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - ipAddress = IpAddress.valueOf("0.0.0.0"); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - ipAddress = IpAddress.valueOf("255.255.255.255"); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests valueOf() converter for IPv6 string. - */ - @Test - public void testValueOfStringIPv6() { - IpAddress ipAddress; - - ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - ipAddress = IpAddress.valueOf("::"); - assertThat(ipAddress.toString(), is("::")); - - ipAddress = - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - } - - /** - * Tests invalid valueOf() converter for a null string. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullString() { - IpAddress ipAddress; - - String fromString = null; - ipAddress = IpAddress.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an empty string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfEmptyString() { - IpAddress ipAddress; - - String fromString = ""; - ipAddress = IpAddress.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an incorrect string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIncorrectString() { - IpAddress ipAddress; - - String fromString = "NoSuchIpAddress"; - ipAddress = IpAddress.valueOf(fromString); - } - - /** - * Tests making a mask prefix for a given prefix length for IPv4. - */ - @Test - public void testMakeMaskPrefixIPv4() { - IpAddress ipAddress; - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 25); - assertThat(ipAddress.toString(), is("255.255.255.128")); - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 0); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 32); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests making a mask prefix for a given prefix length for IPv6. - */ - @Test - public void testMakeMaskPrefixIPv6() { - IpAddress ipAddress; - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 8); - assertThat(ipAddress.toString(), is("ff00::")); - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 120); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00")); - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 0); - assertThat(ipAddress.toString(), is("::")); - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 128); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 64); - assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::")); - } - - /** - * Tests making a mask prefix for an invalid prefix length for IPv4: - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeNegativeMaskPrefixIPv4() { - IpAddress ipAddress; - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, -1); - } - - /** - * Tests making a mask prefix for an invalid prefix length for IPv6: - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeNegativeMaskPrefixIPv6() { - IpAddress ipAddress; - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, -1); - } - - /** - * Tests making a mask prefix for an invalid prefix length for IPv4: - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeTooLongMaskPrefixIPv4() { - IpAddress ipAddress; - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 33); - } - - /** - * Tests making a mask prefix for an invalid prefix length for IPv6: - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeTooLongMaskPrefixIPv6() { - IpAddress ipAddress; - - ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 129); - } - - /** - * Tests making of a masked address for IPv4. - */ - @Test - public void testMakeMaskedAddressIPv4() { - IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); - IpAddress ipAddressMasked; - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 24); - assertThat(ipAddressMasked.toString(), is("1.2.3.0")); - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0); - assertThat(ipAddressMasked.toString(), is("0.0.0.0")); - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 32); - assertThat(ipAddressMasked.toString(), is("1.2.3.5")); - } - - /** - * Tests making of a masked address for IPv6. - */ - @Test - public void testMakeMaskedAddressIPv6() { - IpAddress ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); - IpAddress ipAddressMasked; - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 8); - assertThat(ipAddressMasked.toString(), is("1100::")); - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 120); - assertThat(ipAddressMasked.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8800")); - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0); - assertThat(ipAddressMasked.toString(), is("::")); - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 128); - assertThat(ipAddressMasked.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8885")); - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 64); - assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::")); - } - - /** - * Tests making of a masked address for invalid prefix length for IPv4: - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeNegativeMaskedAddressIPv4() { - IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); - IpAddress ipAddressMasked; - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1); - } - - /** - * Tests making of a masked address for invalid prefix length for IPv6: - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeNegativeMaskedAddressIPv6() { - IpAddress ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); - IpAddress ipAddressMasked; - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1); - } - - /** - * Tests making of a masked address for an invalid prefix length for IPv4: - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeTooLongMaskedAddressIPv4() { - IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); - IpAddress ipAddressMasked; - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 33); - } - - /** - * Tests making of a masked address for an invalid prefix length for IPv6: - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidMakeTooLongMaskedAddressIPv6() { - IpAddress ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); - IpAddress ipAddressMasked; - - ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 129); - } - - /** - * Tests if address is zero for IPv4. - */ - @Test - public void testIsZeroIPv4() { - IpAddress normalIP = IpAddress.valueOf("10.0.0.1"); - IpAddress zeroIP = IpAddress.valueOf("0.0.0.0"); - assertFalse(normalIP.isZero()); - assertTrue(zeroIP.isZero()); - } - - /** - * Tests if address is zero for IPv6. - */ - @Test - public void testIsZeroIPv6() { - IpAddress normalIP = IpAddress.valueOf("fe80::1"); - IpAddress zeroIP = IpAddress.valueOf("::"); - assertFalse(normalIP.isZero()); - assertTrue(zeroIP.isZero()); - } - - /** - * Tests if address is self-assigned for IPv4. - */ - @Test - public void testIsSelfAssignedIpv4() { - IpAddress normalIP = IpAddress.valueOf("10.0.0.1"); - IpAddress selfAssignedIP = IpAddress.valueOf("169.1.2.3"); - assertFalse(normalIP.isSelfAssigned()); - assertTrue(selfAssignedIP.isSelfAssigned()); - } - - /** - * Tests comparison of {@link IpAddress} for IPv4. - */ - @Test - public void testComparisonIPv4() { - IpAddress addr1, addr2, addr3, addr4; - - addr1 = IpAddress.valueOf("1.2.3.4"); - addr2 = IpAddress.valueOf("1.2.3.4"); - addr3 = IpAddress.valueOf("1.2.3.3"); - addr4 = IpAddress.valueOf("1.2.3.5"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - - addr1 = IpAddress.valueOf("255.2.3.4"); - addr2 = IpAddress.valueOf("255.2.3.4"); - addr3 = IpAddress.valueOf("255.2.3.3"); - addr4 = IpAddress.valueOf("255.2.3.5"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - } - - /** - * Tests comparison of {@link IpAddress} for IPv6. - */ - @Test - public void testComparisonIPv6() { - IpAddress addr1, addr2, addr3, addr4; - - addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887"); - addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - - addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); - addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); - addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887"); - addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - - addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); - addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); - addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888"); - addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888"); - assertTrue(addr1.compareTo(addr2) == 0); - assertTrue(addr1.compareTo(addr3) > 0); - assertTrue(addr1.compareTo(addr4) < 0); - } - - /** - * Tests equality of {@link IpAddress} for IPv4. - */ - @Test - public void testEqualityIPv4() { - new EqualsTester() - .addEqualityGroup(IpAddress.valueOf("1.2.3.4"), - IpAddress.valueOf("1.2.3.4")) - .addEqualityGroup(IpAddress.valueOf("1.2.3.5"), - IpAddress.valueOf("1.2.3.5")) - .addEqualityGroup(IpAddress.valueOf("0.0.0.0"), - IpAddress.valueOf("0.0.0.0")) - .addEqualityGroup(IpAddress.valueOf("255.255.255.255"), - IpAddress.valueOf("255.255.255.255")) - .testEquals(); - } - - /** - * Tests equality of {@link IpAddress} for IPv6. - */ - @Test - public void testEqualityIPv6() { - new EqualsTester() - .addEqualityGroup( - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"), - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888")) - .addEqualityGroup( - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"), - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a")) - .addEqualityGroup( - IpAddress.valueOf("::"), - IpAddress.valueOf("::")) - .addEqualityGroup( - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) - .testEquals(); - } - - /** - * Tests object string representation for IPv4. - */ - @Test - public void testToStringIPv4() { - IpAddress ipAddress; - - ipAddress = IpAddress.valueOf("1.2.3.4"); - assertThat(ipAddress.toString(), is("1.2.3.4")); - - ipAddress = IpAddress.valueOf("0.0.0.0"); - assertThat(ipAddress.toString(), is("0.0.0.0")); - - ipAddress = IpAddress.valueOf("255.255.255.255"); - assertThat(ipAddress.toString(), is("255.255.255.255")); - } - - /** - * Tests object string representation for IPv6. - */ - @Test - public void testToStringIPv6() { - IpAddress ipAddress; - - ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - assertThat(ipAddress.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888")); - - ipAddress = IpAddress.valueOf("1111::8888"); - assertThat(ipAddress.toString(), is("1111::8888")); - - ipAddress = IpAddress.valueOf("1111::"); - assertThat(ipAddress.toString(), is("1111::")); - - ipAddress = IpAddress.valueOf("::8888"); - assertThat(ipAddress.toString(), is("::8888")); - - ipAddress = IpAddress.valueOf("::"); - assertThat(ipAddress.toString(), is("::")); - - ipAddress = - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - assertThat(ipAddress.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - - ipAddress = - IpAddress.valueOf("::1111:2222"); - assertThat(ipAddress.toString(), - is("::1111:2222")); - - ipAddress = - IpAddress.valueOf("1:0:0:1:0:0:2:3"); - assertThat(ipAddress.toString(), - is("1::1:0:0:2:3")); - - ipAddress = - IpAddress.valueOf("::0123:0004"); - assertThat(ipAddress.toString(), - is("::123:4")); - - ipAddress = - IpAddress.valueOf("0:0:1:1:0:0:1:1"); - assertThat(ipAddress.toString(), - is("::1:1:0:0:1:1")); - - ipAddress = - IpAddress.valueOf("1:1a2b::"); - assertThat(ipAddress.toString(), - is("1:1a2b::")); - - ipAddress = - IpAddress.valueOf("0:0:00:00:0000:00:00:000"); - assertThat(ipAddress.toString(), - is("::")); - - ipAddress = - IpAddress.valueOf("0:0:0:1:0:0:0:0"); - assertThat(ipAddress.toString(), - is("0:0:0:1::")); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java deleted file mode 100644 index 052a4cbb..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/IpPrefixTest.java +++ /dev/null @@ -1,1080 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import org.junit.Test; - -import com.google.common.testing.EqualsTester; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; - -/** - * Tests for class {@link IpPrefix}. - */ -public class IpPrefixTest { - /** - * Tests the immutability of {@link IpPrefix}. - */ - @Test - public void testImmutable() { - assertThatClassIsImmutableBaseClass(IpPrefix.class); - } - - /** - * Tests the maximum mask length. - */ - @Test - public void testMaxMaskLength() { - assertThat(IpPrefix.MAX_INET_MASK_LENGTH, is(32)); - assertThat(IpPrefix.MAX_INET6_MASK_LENGTH, is(128)); - } - - /** - * Tests returning the IP version of the prefix. - */ - @Test - public void testVersion() { - IpPrefix ipPrefix; - - // IPv4 - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertThat(ipPrefix.version(), is(IpAddress.Version.INET)); - - // IPv6 - ipPrefix = IpPrefix.valueOf("::/0"); - assertThat(ipPrefix.version(), is(IpAddress.Version.INET6)); - } - - /** - * Tests whether the IP version of a prefix is IPv4. - */ - @Test - public void testIsIp4() { - IpPrefix ipPrefix; - - // IPv4 - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertTrue(ipPrefix.isIp4()); - - // IPv6 - ipPrefix = IpPrefix.valueOf("::/0"); - assertFalse(ipPrefix.isIp4()); - } - - /** - * Tests whether the IP version of a prefix is IPv6. - */ - @Test - public void testIsIp6() { - IpPrefix ipPrefix; - - // IPv4 - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertFalse(ipPrefix.isIp6()); - - // IPv6 - ipPrefix = IpPrefix.valueOf("::/0"); - assertTrue(ipPrefix.isIp6()); - } - - /** - * Tests returning the IP address value and IP address prefix length of - * an IPv4 prefix. - */ - @Test - public void testAddressAndPrefixLengthIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1.2.3.0/24"); - assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0"))); - assertThat(ipPrefix.prefixLength(), is(24)); - - ipPrefix = IpPrefix.valueOf("1.2.3.4/24"); - assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0"))); - assertThat(ipPrefix.prefixLength(), is(24)); - - ipPrefix = IpPrefix.valueOf("1.2.3.4/32"); - assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.4"))); - assertThat(ipPrefix.prefixLength(), is(32)); - - ipPrefix = IpPrefix.valueOf("1.2.3.5/32"); - assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.5"))); - assertThat(ipPrefix.prefixLength(), is(32)); - - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("0.0.0.0"))); - assertThat(ipPrefix.prefixLength(), is(0)); - - ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); - assertThat(ipPrefix.address(), - equalTo(IpAddress.valueOf("255.255.255.255"))); - assertThat(ipPrefix.prefixLength(), is(32)); - } - - /** - * Tests returning the IP address value and IP address prefix length of - * an IPv6 prefix. - */ - @Test - public void testAddressAndPrefixLengthIPv6() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1100::/8"); - assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::"))); - assertThat(ipPrefix.prefixLength(), is(8)); - - ipPrefix = - IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8"); - assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::"))); - assertThat(ipPrefix.prefixLength(), is(8)); - - ipPrefix = - IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120"); - assertThat(ipPrefix.address(), - equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8800"))); - assertThat(ipPrefix.prefixLength(), is(120)); - - ipPrefix = - IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128"); - assertThat(ipPrefix.address(), - equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"))); - assertThat(ipPrefix.prefixLength(), is(128)); - - ipPrefix = IpPrefix.valueOf("::/0"); - assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("::"))); - assertThat(ipPrefix.prefixLength(), is(0)); - - ipPrefix = - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertThat(ipPrefix.address(), - equalTo(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - assertThat(ipPrefix.prefixLength(), is(128)); - - ipPrefix = - IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64"); - assertThat(ipPrefix.address(), - equalTo(IpAddress.valueOf("1111:2222:3333:4444::"))); - assertThat(ipPrefix.prefixLength(), is(64)); - } - - /** - * Tests getting the Ip4Prefix and Ip6Prefix view of the IP prefix. - */ - @Test - public void testGetIp4AndIp6PrefixView() { - IpPrefix ipPrefix; - Ip4Prefix ip4Prefix; - Ip6Prefix ip6Prefix; - - // Pure IPv4 IpPrefix - ipPrefix = IpPrefix.valueOf("1.2.3.0/24"); - ip4Prefix = ipPrefix.getIp4Prefix(); - ip6Prefix = ipPrefix.getIp6Prefix(); - assertThat(ip4Prefix.toString(), is("1.2.3.0/24")); - assertNull(ip6Prefix); - - // IPv4 IpPrefix that is Ip4Prefix - ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24"); - ip4Prefix = ipPrefix.getIp4Prefix(); - ip6Prefix = ipPrefix.getIp6Prefix(); - assertThat(ip4Prefix.toString(), is("1.2.3.0/24")); - assertNull(ip6Prefix); - - // Pure IPv6 IpPrefix - ipPrefix = IpPrefix.valueOf("1111:2222::/64"); - ip4Prefix = ipPrefix.getIp4Prefix(); - ip6Prefix = ipPrefix.getIp6Prefix(); - assertNull(ip4Prefix); - assertThat(ip6Prefix.toString(), is("1111:2222::/64")); - - // IPv6 IpPrefix that is Ip6Prefix - ipPrefix = Ip6Prefix.valueOf("1111:2222::/64"); - ip4Prefix = ipPrefix.getIp4Prefix(); - ip6Prefix = ipPrefix.getIp6Prefix(); - assertNull(ip4Prefix); - assertThat(ip6Prefix.toString(), is("1111:2222::/64")); - } - - /** - * Tests valueOf() converter for IPv4 integer value. - */ - @Test - public void testValueOfForIntegerIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf(0x01020304, 24); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = IpPrefix.valueOf(0x01020304, 32); - assertThat(ipPrefix.toString(), is("1.2.3.4/32")); - - ipPrefix = IpPrefix.valueOf(0x01020305, 32); - assertThat(ipPrefix.toString(), is("1.2.3.5/32")); - - ipPrefix = IpPrefix.valueOf(0, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf(0, 32); - assertThat(ipPrefix.toString(), is("0.0.0.0/32")); - - ipPrefix = IpPrefix.valueOf(0xffffffff, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf(0xffffffff, 16); - assertThat(ipPrefix.toString(), is("255.255.0.0/16")); - - ipPrefix = IpPrefix.valueOf(0xffffffff, 32); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests invalid valueOf() converter for IPv4 integer value and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf(0x01020304, -1); - } - - /** - * Tests invalid valueOf() converter for IPv4 integer value and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf(0x01020304, 33); - } - - /** - * Tests valueOf() converter for IPv4 byte array. - */ - @Test - public void testValueOfByteArrayIPv4() { - IpPrefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32); - assertThat(ipPrefix.toString(), is("1.2.3.4/32")); - - value = new byte[] {1, 2, 3, 5}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32); - assertThat(ipPrefix.toString(), is("1.2.3.5/32")); - - value = new byte[] {0, 0, 0, 0}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32); - assertThat(ipPrefix.toString(), is("0.0.0.0/32")); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 16); - assertThat(ipPrefix.toString(), is("255.255.0.0/16")); - - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests valueOf() converter for IPv6 byte array. - */ - @Test - public void testValueOfByteArrayIPv6() { - IpPrefix ipPrefix; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, (byte) 0x88, (byte) 0x88}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8800/120")); - - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888/128")); - - value = new byte[] {0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128); - assertThat(ipPrefix.toString(), is("::/128")); - - value = new byte[] {(byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 64); - assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); - - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128); - assertThat(ipPrefix.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); - } - - /** - * Tests invalid valueOf() converter for a null array for IPv4. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullArrayIPv4() { - IpPrefix ipPrefix; - byte[] value; - - value = null; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24); - } - - /** - * Tests invalid valueOf() converter for a null array for IPv6. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullArrayIPv6() { - IpPrefix ipPrefix; - byte[] value; - - value = null; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120); - } - - /** - * Tests invalid valueOf() converter for a short array for IPv4. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfShortArrayIPv4() { - IpPrefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24); - } - - /** - * Tests invalid valueOf() converter for a short array for IPv6. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfShortArrayIPv6() { - IpPrefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120); - } - - /** - * Tests invalid valueOf() converter for IPv4 byte array and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() { - IpPrefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, -1); - } - - /** - * Tests invalid valueOf() converter for IPv6 byte array and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() { - IpPrefix ipPrefix; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, (byte) 0x88, (byte) 0x88}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, -1); - } - - /** - * Tests invalid valueOf() converter for IPv4 byte array and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() { - IpPrefix ipPrefix; - byte[] value; - - value = new byte[] {1, 2, 3, 4}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 33); - } - - /** - * Tests invalid valueOf() converter for IPv6 byte array and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() { - IpPrefix ipPrefix; - byte[] value; - - value = new byte[] {0x11, 0x11, 0x22, 0x22, - 0x33, 0x33, 0x44, 0x44, - 0x55, 0x55, 0x66, 0x66, - 0x77, 0x77, (byte) 0x88, (byte) 0x88}; - ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 129); - } - - /** - * Tests valueOf() converter for IPv4 address. - */ - @Test - public void testValueOfAddressIPv4() { - IpAddress ipAddress; - IpPrefix ipPrefix; - - ipAddress = IpAddress.valueOf("1.2.3.4"); - ipPrefix = IpPrefix.valueOf(ipAddress, 24); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = IpPrefix.valueOf(ipAddress, 32); - assertThat(ipPrefix.toString(), is("1.2.3.4/32")); - - ipAddress = IpAddress.valueOf("1.2.3.5"); - ipPrefix = IpPrefix.valueOf(ipAddress, 32); - assertThat(ipPrefix.toString(), is("1.2.3.5/32")); - - ipAddress = IpAddress.valueOf("0.0.0.0"); - ipPrefix = IpPrefix.valueOf(ipAddress, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf(ipAddress, 32); - assertThat(ipPrefix.toString(), is("0.0.0.0/32")); - - ipAddress = IpAddress.valueOf("255.255.255.255"); - ipPrefix = IpPrefix.valueOf(ipAddress, 0); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf(ipAddress, 16); - assertThat(ipPrefix.toString(), is("255.255.0.0/16")); - - ipPrefix = IpPrefix.valueOf(ipAddress, 32); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests valueOf() converter for IPv6 address. - */ - @Test - public void testValueOfAddressIPv6() { - IpAddress ipAddress; - IpPrefix ipPrefix; - - ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - ipPrefix = IpPrefix.valueOf(ipAddress, 120); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8800/120")); - - ipPrefix = IpPrefix.valueOf(ipAddress, 128); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888/128")); - - ipAddress = IpAddress.valueOf("::"); - ipPrefix = IpPrefix.valueOf(ipAddress, 0); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = IpPrefix.valueOf(ipAddress, 128); - assertThat(ipPrefix.toString(), is("::/128")); - - ipAddress = - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - ipPrefix = IpPrefix.valueOf(ipAddress, 0); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = IpPrefix.valueOf(ipAddress, 64); - assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); - - ipPrefix = IpPrefix.valueOf(ipAddress, 128); - assertThat(ipPrefix.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); - } - - /** - * Tests invalid valueOf() converter for a null IP address. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullAddress() { - IpAddress ipAddress; - IpPrefix ipPrefix; - - ipAddress = null; - ipPrefix = IpPrefix.valueOf(ipAddress, 24); - } - - /** - * Tests invalid valueOf() converter for IPv4 address and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfAddressNegativePrefixLengthIPv4() { - IpAddress ipAddress; - IpPrefix ipPrefix; - - ipAddress = IpAddress.valueOf("1.2.3.4"); - ipPrefix = IpPrefix.valueOf(ipAddress, -1); - } - - /** - * Tests invalid valueOf() converter for IPv6 address and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfAddressNegativePrefixLengthIPv6() { - IpAddress ipAddress; - IpPrefix ipPrefix; - - ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - ipPrefix = IpPrefix.valueOf(ipAddress, -1); - } - - /** - * Tests invalid valueOf() converter for IPv4 address and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() { - IpAddress ipAddress; - IpPrefix ipPrefix; - - ipAddress = IpAddress.valueOf("1.2.3.4"); - ipPrefix = IpPrefix.valueOf(ipAddress, 33); - } - - /** - * Tests invalid valueOf() converter for IPv6 address and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() { - IpAddress ipAddress; - IpPrefix ipPrefix; - - ipAddress = - IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); - ipPrefix = IpPrefix.valueOf(ipAddress, 129); - } - - /** - * Tests valueOf() converter for IPv4 string. - */ - @Test - public void testValueOfStringIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1.2.3.4/24"); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = IpPrefix.valueOf("1.2.3.4/32"); - assertThat(ipPrefix.toString(), is("1.2.3.4/32")); - - ipPrefix = IpPrefix.valueOf("1.2.3.5/32"); - assertThat(ipPrefix.toString(), is("1.2.3.5/32")); - - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf("0.0.0.0/32"); - assertThat(ipPrefix.toString(), is("0.0.0.0/32")); - - ipPrefix = IpPrefix.valueOf("255.255.255.255/0"); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf("255.255.255.255/16"); - assertThat(ipPrefix.toString(), is("255.255.0.0/16")); - - ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests valueOf() converter for IPv6 string. - */ - @Test - public void testValueOfStringIPv6() { - IpPrefix ipPrefix; - - ipPrefix = - IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120"); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8800/120")); - - ipPrefix = - IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128"); - assertThat(ipPrefix.toString(), - is("1111:2222:3333:4444:5555:6666:7777:8888/128")); - - ipPrefix = IpPrefix.valueOf("::/0"); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = IpPrefix.valueOf("::/128"); - assertThat(ipPrefix.toString(), is("::/128")); - - ipPrefix = - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"); - assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64")); - - ipPrefix = - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertThat(ipPrefix.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); - } - - /** - * Tests invalid valueOf() converter for a null string. - */ - @Test(expected = NullPointerException.class) - public void testInvalidValueOfNullString() { - IpPrefix ipPrefix; - String fromString; - - fromString = null; - ipPrefix = IpPrefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an empty string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfEmptyString() { - IpPrefix ipPrefix; - String fromString; - - fromString = ""; - ipPrefix = IpPrefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for an incorrect string. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfIncorrectString() { - IpPrefix ipPrefix; - String fromString; - - fromString = "NoSuchIpPrefix"; - ipPrefix = IpPrefix.valueOf(fromString); - } - - /** - * Tests invalid valueOf() converter for IPv4 string and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfStringNegativePrefixLengthIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1.2.3.4/-1"); - } - - /** - * Tests invalid valueOf() converter for IPv6 string and - * negative prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfStringNegativePrefixLengthIPv6() { - IpPrefix ipPrefix; - - ipPrefix = - IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1"); - } - - /** - * Tests invalid valueOf() converter for IPv4 string and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfStringTooLongPrefixLengthIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1.2.3.4/33"); - } - - /** - * Tests invalid valueOf() converter for IPv6 string and - * too long prefix length. - */ - @Test(expected = IllegalArgumentException.class) - public void testInvalidValueOfStringTooLongPrefixLengthIPv6() { - IpPrefix ipPrefix; - - ipPrefix = - IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129"); - } - - /** - * Tests IP prefix contains another IP prefix for IPv4. - */ - @Test - public void testContainsIpPrefixIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1.2.0.0/24"); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); - - ipPrefix = IpPrefix.valueOf("1.2.0.0/32"); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); - - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); - - ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); - - // Test when there is a mistmatch in the compared IP address families - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1111:2222:3333:4444::/120"))); - ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - } - - /** - * Tests IP prefix contains another IP prefix for IPv6. - */ - @Test - public void testContainsIpPrefixIPv6() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120"); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/120"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/128"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::1/128"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/64"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4445::/120"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - - ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128"); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/120"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/128"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::1/128"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/64"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4445::/120"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - - ipPrefix = IpPrefix.valueOf("::/0"); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/120"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/128"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::1/128"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/64"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4445::/120"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/64"))); - assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/0"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - - ipPrefix = - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/120"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/128"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::1/128"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4444::/64"))); - assertFalse(ipPrefix.contains( - IpPrefix.valueOf("1111:2222:3333:4445::/120"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64"))); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0"))); - assertTrue(ipPrefix.contains( - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))); - - // Test when there is a mistmatch in the compared IP address families - ipPrefix = IpPrefix.valueOf("::/0"); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24"))); - ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32"))); - } - - /** - * Tests IP prefix contains IP address for IPv4. - */ - @Test - public void testContainsIpAddressIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1.2.0.0/24"); - assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); - assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); - - ipPrefix = IpPrefix.valueOf("1.2.0.0/32"); - assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); - - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); - assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4"))); - assertTrue(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); - assertTrue(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); - assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); - - ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); - assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0"))); - assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); - - // Test when there is a mistmatch in the compared IP address families - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertFalse(ipPrefix.contains(IpAddress.valueOf("1111:2222:3333:4444::"))); - ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); - assertFalse(ipPrefix.contains(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - } - - /** - * Tests IP prefix contains IP address for IPv6. - */ - @Test - public void testContainsIpAddressIPv6() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120"); - assertTrue(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4444::"))); - assertTrue(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4444::1"))); - assertFalse(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4445::"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("::"))); - assertFalse(ipPrefix.contains( - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - - ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128"); - assertTrue(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4444::"))); - assertFalse(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4444::1"))); - assertFalse(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4445::"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("::"))); - assertFalse(ipPrefix.contains( - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - - ipPrefix = IpPrefix.valueOf("::/0"); - assertTrue(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4444::"))); - assertTrue(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4444::1"))); - assertTrue(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4445::"))); - assertTrue(ipPrefix.contains(IpAddress.valueOf("::"))); - assertTrue(ipPrefix.contains( - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - - ipPrefix = - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertFalse(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4444::"))); - assertFalse(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4444::1"))); - assertFalse(ipPrefix.contains( - IpAddress.valueOf("1111:2222:3333:4445::"))); - assertFalse(ipPrefix.contains(IpAddress.valueOf("::"))); - assertTrue(ipPrefix.contains( - IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))); - - // Test when there is a mistmatch in the compared IP address families - ipPrefix = IpPrefix.valueOf("::/0"); - assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0"))); - ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255"))); - } - - /** - * Tests equality of {@link IpPrefix} for IPv4. - */ - @Test - public void testEqualityIPv4() { - new EqualsTester() - .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/24"), - IpPrefix.valueOf("1.2.0.0/24"), - IpPrefix.valueOf("1.2.0.4/24")) - .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/16"), - IpPrefix.valueOf("1.2.0.0/16")) - .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/32"), - IpPrefix.valueOf("1.2.0.0/32")) - .addEqualityGroup(IpPrefix.valueOf("1.3.0.0/24"), - IpPrefix.valueOf("1.3.0.0/24")) - .addEqualityGroup(IpPrefix.valueOf("0.0.0.0/0"), - IpPrefix.valueOf("0.0.0.0/0")) - .addEqualityGroup(IpPrefix.valueOf("255.255.255.255/32"), - IpPrefix.valueOf("255.255.255.255/32")) - .testEquals(); - } - - /** - * Tests equality of {@link IpPrefix} for IPv6. - */ - @Test - public void testEqualityIPv6() { - new EqualsTester() - .addEqualityGroup( - IpPrefix.valueOf("1111:2222:3333:4444::/120"), - IpPrefix.valueOf("1111:2222:3333:4444::1/120"), - IpPrefix.valueOf("1111:2222:3333:4444::/120")) - .addEqualityGroup( - IpPrefix.valueOf("1111:2222:3333:4444::/64"), - IpPrefix.valueOf("1111:2222:3333:4444::/64")) - .addEqualityGroup( - IpPrefix.valueOf("1111:2222:3333:4444::/128"), - IpPrefix.valueOf("1111:2222:3333:4444::/128")) - .addEqualityGroup( - IpPrefix.valueOf("1111:2222:3333:4445::/64"), - IpPrefix.valueOf("1111:2222:3333:4445::/64")) - .addEqualityGroup( - IpPrefix.valueOf("::/0"), - IpPrefix.valueOf("::/0")) - .addEqualityGroup( - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"), - IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")) - .testEquals(); - } - - /** - * Tests object string representation for IPv4. - */ - @Test - public void testToStringIPv4() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1.2.3.0/24"); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = IpPrefix.valueOf("1.2.3.4/24"); - assertThat(ipPrefix.toString(), is("1.2.3.0/24")); - - ipPrefix = IpPrefix.valueOf("0.0.0.0/0"); - assertThat(ipPrefix.toString(), is("0.0.0.0/0")); - - ipPrefix = IpPrefix.valueOf("255.255.255.255/32"); - assertThat(ipPrefix.toString(), is("255.255.255.255/32")); - } - - /** - * Tests object string representation for IPv6. - */ - @Test - public void testToStringIPv6() { - IpPrefix ipPrefix; - - ipPrefix = IpPrefix.valueOf("1100::/8"); - assertThat(ipPrefix.toString(), is("1100::/8")); - - ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8"); - assertThat(ipPrefix.toString(), is("1100::/8")); - - ipPrefix = IpPrefix.valueOf("::/0"); - assertThat(ipPrefix.toString(), is("::/0")); - - ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); - assertThat(ipPrefix.toString(), - is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLCTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLCTest.java deleted file mode 100644 index 39bb72f4..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLCTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.junit.Assert.assertEquals; - -/** - * Unit tests for LLC class. - */ -public class LLCTest { - - private Deserializer<LLC> deserializer; - - private byte dsap = 10; - private byte ssap = 20; - private byte ctrl = 30; - - private byte[] bytes; - - @Before - public void setUp() throws Exception { - deserializer = LLC.deserializer(); - - ByteBuffer bb = ByteBuffer.allocate(LLC.LLC_HEADER_LENGTH); - - bb.put(dsap); - bb.put(ssap); - bb.put(ctrl); - - bytes = bb.array(); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, bytes); - } - - @Test - public void testDeserialize() throws Exception { - LLC llc = deserializer.deserialize(bytes, 0, bytes.length); - - assertEquals(dsap, llc.getDsap()); - assertEquals(ssap, llc.getSsap()); - assertEquals(ctrl, llc.getCtrl()); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLDPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLDPTest.java deleted file mode 100644 index 95b0b04a..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/LLDPTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Unit tests for the LLDP class. - */ -public class LLDPTest { - - private Deserializer<LLDP> deserializer; - - private byte[] chassisValue = new byte[] {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7}; - private byte[] portValue = new byte[] {0x1, 0x2, 0x3, 0x4, 0x5}; - private byte[] ttlValue = new byte[] {0x0, 0x20}; - - private short optionalTlvSize = 6; - private byte[] optionalTlvValue = new byte[] {0x6, 0x5, 0x4, 0x3, 0x2, 0x1}; - - private byte[] bytes; - - @Before - public void setUp() throws Exception { - deserializer = LLDP.deserializer(); - - // Each TLV is 2 bytes for the type+length, plus the size of the value - // There are 2 zero-bytes at the end - ByteBuffer bb = ByteBuffer.allocate(2 + LLDP.CHASSIS_TLV_SIZE + - 2 + LLDP.PORT_TLV_SIZE + - 2 + LLDP.TTL_TLV_SIZE + - 2 + optionalTlvSize + - 2); - - // Chassis TLV - bb.putShort(getTypeLength(LLDP.CHASSIS_TLV_TYPE, LLDP.CHASSIS_TLV_SIZE)); - bb.put(chassisValue); - - // Port TLV - bb.putShort(getTypeLength(LLDP.PORT_TLV_TYPE, LLDP.PORT_TLV_SIZE)); - bb.put(portValue); - - // TTL TLV - bb.putShort(getTypeLength(LLDP.TTL_TLV_TYPE, LLDP.TTL_TLV_SIZE)); - bb.put(ttlValue); - - // Optional TLV - bb.putShort(getTypeLength(LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE, optionalTlvSize)); - bb.put(optionalTlvValue); - - bb.putShort((short) 0); - - bytes = bb.array(); - - } - - private short getTypeLength(byte type, short length) { - return (short) ((0x7f & type) << 9 | 0x1ff & length); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, bytes); - } - - @Test - public void testDeserialize() throws Exception { - LLDP lldp = deserializer.deserialize(bytes, 0, bytes.length); - - assertEquals(LLDP.CHASSIS_TLV_TYPE, lldp.getChassisId().getType()); - assertEquals(LLDP.CHASSIS_TLV_SIZE, lldp.getChassisId().getLength()); - assertTrue(Arrays.equals(chassisValue, lldp.getChassisId().getValue())); - - assertEquals(LLDP.PORT_TLV_TYPE, lldp.getPortId().getType()); - assertEquals(LLDP.PORT_TLV_SIZE, lldp.getPortId().getLength()); - assertTrue(Arrays.equals(portValue, lldp.getPortId().getValue())); - - assertEquals(LLDP.TTL_TLV_TYPE, lldp.getTtl().getType()); - assertEquals(LLDP.TTL_TLV_SIZE, lldp.getTtl().getLength()); - assertTrue(Arrays.equals(ttlValue, lldp.getTtl().getValue())); - - assertEquals(1, lldp.getOptionalTLVList().size()); - LLDPTLV optionalTlv = lldp.getOptionalTLVList().get(0); - - assertEquals(LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE, optionalTlv.getType()); - assertEquals(optionalTlvSize, optionalTlv.getLength()); - assertTrue(Arrays.equals(optionalTlvValue, optionalTlv.getValue())); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/MplsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/MplsTest.java deleted file mode 100644 index 2ab8ff98..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/MplsTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; - -import java.nio.ByteBuffer; -import java.util.HashMap; - -import static org.junit.Assert.assertEquals; - -/** - * Unit tests for MPLS class. - */ -public class MplsTest { - - private Deserializer<MPLS> deserializer; - - private int label = 1048575; - private byte bos = 1; - private byte ttl = 20; - private byte protocol = MPLS.PROTOCOL_IPV4; - - private byte[] bytes; - - @Before - public void setUp() throws Exception { - // Replace normal deserializer map with an empty map. This will cause - // the DataDeserializer to be used which will silently handle 0-byte input. - MPLS.protocolDeserializerMap = new HashMap<>(); - - deserializer = MPLS.deserializer(); - - ByteBuffer bb = ByteBuffer.allocate(MPLS.HEADER_LENGTH); - bb.putInt(((label & 0x000fffff) << 12) | ((bos & 0x1) << 8 | (ttl & 0xff))); - - bytes = bb.array(); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, bytes); - } - - @Test - public void testDeserialize() throws Exception { - MPLS mpls = deserializer.deserialize(bytes, 0, bytes.length); - - assertEquals(label, mpls.label); - assertEquals(bos, mpls.bos); - assertEquals(ttl, mpls.ttl); - assertEquals(protocol, mpls.protocol); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PIMTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PIMTest.java deleted file mode 100644 index 7fba3cd7..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PIMTest.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import org.junit.Before; -import org.junit.Test; -import org.onlab.packet.pim.PIMAddrUnicast; -import org.onlab.packet.pim.PIMHello; -import org.onlab.packet.pim.PIMJoinPrune; - -import static junit.framework.Assert.assertTrue; - -public final class PIMTest { - - public static final String SADDR = "10.2.1.2"; - public static final String PIMADDR = "224.0.0.13"; - public static final String PIMUADDR = "10.23.3.5"; - - public static final String SADDR1 = "10.1.1.1/32"; - public static final String SADDR2 = "10.1.2.1/32"; - public static final String GADDR1 = "232.1.1.1/32"; - public static final String GADDR2 = "232.1.2.1/32"; - - public static final String CPSTR1 = "of:deadbeefball/8"; - public static final String CPSTR2 = "of:deadbeefcafe/3"; - public static final String CPSTR3 = "of:2badcafef00d/3"; - - private Deserializer<PIM> deserializer; - - private PIM pimHello; - private PIMHello hello; - - private PIM pimJoinPrune; - private PIMJoinPrune joinPrune; - - /** - * Create PIM Hello and Join/Prune packets to be used in testing. - * - * @throws Exception if packet creation fails - */ - @Before - public void setUp() throws Exception { - - // Create a PIM Hello - pimHello = new PIM(); - pimHello.setVersion((byte) 2); - pimHello.setPIMType((byte) PIM.TYPE_HELLO); - pimHello.setChecksum((short) 0); - - hello = new PIMHello(); - hello.createDefaultOptions(); - pimHello.setPayload(hello); - hello.setParent(pimHello); - - // Create PIM Join Prune - pimJoinPrune = new PIM(); - pimJoinPrune.setVersion((byte) 2); - pimJoinPrune.setPIMType((byte) PIM.TYPE_JOIN_PRUNE_REQUEST); - pimJoinPrune.setChecksum((short) 0); - - joinPrune = new PIMJoinPrune(); - joinPrune.setUpstreamAddr(new PIMAddrUnicast(SADDR)); - joinPrune.addJoin(GADDR1, SADDR1); - joinPrune.addJoin(GADDR2, SADDR2); - joinPrune.addPrune(GADDR1, SADDR2); - joinPrune.addPrune(GADDR2, SADDR1); - - pimJoinPrune.setPayload(joinPrune); - joinPrune.setParent(pimJoinPrune); - - deserializer = PIM.deserializer(); - } - - /** - * Make sure our deserializer throws an exception if we recieve bad input. - * - * @throws Exception if we are given bad input. - */ - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - /** - * Verify we throw an exception if we receive a truncated Join/Prune message. - * - * @throws Exception if we receive a truncated Join/Prune message. - */ - @Test - public void testDeserializeTruncated() throws Exception { - byte [] bits = pimJoinPrune.serialize(); - PacketTestUtils.testDeserializeTruncated(deserializer, bits); - } - - /** - * Verify that we correctly deserialize hello messages. - * - * @throws Exception if our input is bad or truncated. - */ - @Test - public void testDeserializeHello() throws Exception { - byte [] data = pimHello.serialize(); - PIM pim = deserializer.deserialize(data, 0, data.length); - assertTrue(pim.equals(pimHello)); - } - - /** - * Verify that we correctly deserialize Join/Prune messages. - * - * @throws Exception if our input is bad or truncated. - */ - @Test - public void testDeserializeJoinPrune() throws Exception { - byte [] data = pimJoinPrune.serialize(); - PIM pim = deserializer.deserialize(data, 0, data.length); - assertTrue(pim.equals(pimJoinPrune)); - } - -}
\ No newline at end of file diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PacketTestUtils.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PacketTestUtils.java deleted file mode 100644 index 209b1d27..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/PacketTestUtils.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet; - -import java.nio.ByteBuffer; - -import static junit.framework.TestCase.fail; -import static org.junit.Assert.assertTrue; - -/** - * Utilities for testing packet methods. - */ -public final class PacketTestUtils { - - private PacketTestUtils() { - } - - /** - * Tests that the Deserializer function is resilient to bad input parameters - * such as null input, negative offset and length, etc. - * - * @param deserializer deserializer function to test - */ - public static void testDeserializeBadInput(Deserializer deserializer) { - byte[] bytes = ByteBuffer.allocate(4).array(); - - try { - deserializer.deserialize(null, 0, 4); - fail("NullPointerException was not thrown"); - } catch (NullPointerException e) { - assertTrue(true); - } catch (DeserializationException e) { - fail("NullPointerException was not thrown"); - } - - // input byte array length, offset and length don't make sense - expectDeserializationException(deserializer, bytes, -1, 0); - expectDeserializationException(deserializer, bytes, 0, -1); - expectDeserializationException(deserializer, bytes, 0, 5); - expectDeserializationException(deserializer, bytes, 2, 3); - expectDeserializationException(deserializer, bytes, 5, 0); - } - - /** - * Tests that the Deserializer function is resilient to truncated input, or - * cases where the input byte array does not contain enough bytes to - * deserialize the packet. - * - * @param deserializer deserializer function to test - * @param header byte array of a full-size packet - */ - public static void testDeserializeTruncated(Deserializer deserializer, - byte[] header) { - byte[] truncated; - - for (int i = 0; i < header.length; i++) { - truncated = new byte[i]; - - ByteBuffer.wrap(header).get(truncated); - - expectDeserializationException(deserializer, truncated, 0, truncated.length); - } - } - - /** - * Run the given deserializer function against the given inputs and verify - * that a DeserializationException is thrown. The the test will fail if a - * DeserializationException is not thrown by the deserializer function. - * - * @param deserializer deserializer function to test - * @param bytes input byte array - * @param offset input offset - * @param length input length - */ - public static void expectDeserializationException(Deserializer deserializer, - byte[] bytes, int offset, int length) { - try { - deserializer.deserialize(bytes, offset, length); - fail("DeserializationException was not thrown"); - } catch (DeserializationException e) { - assertTrue(true); - } - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/TCPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/TCPTest.java deleted file mode 100644 index faab5c2c..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/TCPTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - - -package org.onlab.packet; - -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link TCP}. - */ -public class TCPTest { - private static final byte[] IPV4_SOURCE_ADDRESS = { - (byte) 192, (byte) 168, (byte) 1, (byte) 1 - }; - private static final byte[] IPV4_DESTINATION_ADDRESS = { - (byte) 192, (byte) 168, (byte) 1, (byte) 2 - }; - private static final byte[] IPV6_SOURCE_ADDRESS = { - (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 - }; - private static final byte[] IPV6_DESTINATION_ADDRESS = { - (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02 - }; - - private static IPv4 ipv4 = new IPv4(); - private static IPv6 ipv6 = new IPv6(); - private static byte[] bytePacketTCP4 = { - (byte) 0x00, (byte) 0x50, (byte) 0x00, (byte) 0x60, // src,dst port - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, // seq - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, // ack - (byte) 0x50, (byte) 0x02, // offset,flag - (byte) 0x10, (byte) 0x00, // window - (byte) 0x1b, (byte) 0xae, // checksum - (byte) 0x00, (byte) 0x01 // urgent - }; - private static byte[] bytePacketTCP6 = { - (byte) 0x00, (byte) 0x50, (byte) 0x00, (byte) 0x60, // src,dst port - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, // seq - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, // ack - (byte) 0x50, (byte) 0x02, // offset,flag - (byte) 0x10, (byte) 0x00, // window - (byte) 0xa1, (byte) 0xfd, // checksum - (byte) 0x00, (byte) 0x01 // urgent - }; - - private static Deserializer<TCP> deserializer; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - deserializer = TCP.deserializer(); - - ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS)); - ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS)); - ipv4.setProtocol(IPv4.PROTOCOL_TCP); - - ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS); - ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS); - ipv6.setNextHeader(IPv6.PROTOCOL_TCP); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - TCP tcp = new TCP(); - tcp.setSourcePort(0x50); - tcp.setDestinationPort(0x60); - tcp.setSequence(0x10); - tcp.setAcknowledge(0x20); - tcp.setDataOffset((byte) 0x5); - tcp.setFlags((short) 0x2); - tcp.setWindowSize((short) 0x1000); - tcp.setUrgentPointer((short) 0x1); - - tcp.setParent(ipv4); - assertArrayEquals(bytePacketTCP4, tcp.serialize()); - tcp.resetChecksum(); - tcp.setParent(ipv6); - assertArrayEquals(bytePacketTCP6, tcp.serialize()); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, bytePacketTCP4); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws Exception { - TCP tcp = deserializer.deserialize(bytePacketTCP4, 0, bytePacketTCP4.length); - - assertThat(tcp.getSourcePort(), is(0x50)); - assertThat(tcp.getDestinationPort(), is(0x60)); - assertThat(tcp.getSequence(), is(0x10)); - assertThat(tcp.getAcknowledge(), is(0x20)); - assertThat(tcp.getDataOffset(), is((byte) 0x5)); - assertThat(tcp.getFlags(), is((short) 0x2)); - assertThat(tcp.getWindowSize(), is((short) 0x1000)); - assertThat(tcp.getUrgentPointer(), is((short) 0x1)); - assertThat(tcp.getChecksum(), is((short) 0x1bae)); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - TCP tcp1 = new TCP(); - tcp1.setSourcePort(0x50); - tcp1.setDestinationPort(0x60); - tcp1.setSequence(0x10); - tcp1.setAcknowledge(0x20); - tcp1.setDataOffset((byte) 0x5); - tcp1.setFlags((short) 0x2); - tcp1.setWindowSize((short) 0x1000); - tcp1.setUrgentPointer((short) 0x1); - - TCP tcp2 = new TCP(); - tcp2.setSourcePort(0x70); - tcp2.setDestinationPort(0x60); - tcp2.setSequence(0x10); - tcp2.setAcknowledge(0x20); - tcp2.setDataOffset((byte) 0x5); - tcp2.setFlags((short) 0x2); - tcp2.setWindowSize((short) 0x1000); - tcp2.setUrgentPointer((short) 0x1); - - assertTrue(tcp1.equals(tcp1)); - assertFalse(tcp1.equals(tcp2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/UDPTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/UDPTest.java deleted file mode 100644 index ba453f61..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/UDPTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - - -package org.onlab.packet; - -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link UDP}. - */ -public class UDPTest { - private static final byte[] IPV4_SOURCE_ADDRESS = { - (byte) 192, (byte) 168, (byte) 1, (byte) 1 - }; - private static final byte[] IPV4_DESTINATION_ADDRESS = { - (byte) 192, (byte) 168, (byte) 1, (byte) 2 - }; - private static final byte[] IPV6_SOURCE_ADDRESS = { - (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 - }; - private static final byte[] IPV6_DESTINATION_ADDRESS = { - (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02 - }; - - private static IPv4 ipv4 = new IPv4(); - private static IPv6 ipv6 = new IPv6(); - private static byte[] bytePacketUDP4 = { - (byte) 0x00, (byte) 0x50, // src port - (byte) 0x00, (byte) 0x60, // dst port - (byte) 0x00, (byte) 0x08, // length - (byte) 0x7b, (byte) 0xda, // checksum - }; - private static byte[] bytePacketUDP6 = { - (byte) 0x00, (byte) 0x50, // src port - (byte) 0x00, (byte) 0x60, // dst port - (byte) 0x00, (byte) 0x08, // length - (byte) 0x02, (byte) 0x2a, // checksum - }; - - private static Deserializer<UDP> deserializer; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - deserializer = UDP.deserializer(); - - ipv4.setSourceAddress(IPv4.toIPv4Address(IPV4_SOURCE_ADDRESS)); - ipv4.setDestinationAddress(IPv4.toIPv4Address(IPV4_DESTINATION_ADDRESS)); - ipv4.setProtocol(IPv4.PROTOCOL_UDP); - - ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS); - ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS); - ipv6.setNextHeader(IPv6.PROTOCOL_UDP); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - UDP udp = new UDP(); - udp.setSourcePort(0x50); - udp.setDestinationPort(0x60); - - udp.setParent(ipv4); - assertArrayEquals(bytePacketUDP4, udp.serialize()); - udp.resetChecksum(); - udp.setParent(ipv6); - assertArrayEquals(bytePacketUDP6, udp.serialize()); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(deserializer); - } - - @Test - public void testDeserializeTruncated() throws Exception { - PacketTestUtils.testDeserializeTruncated(deserializer, bytePacketUDP4); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws Exception { - UDP udp = deserializer.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length); - - assertThat(udp.getSourcePort(), is(0x50)); - assertThat(udp.getDestinationPort(), is(0x60)); - assertThat(udp.getLength(), is((short) 8)); - assertThat(udp.getChecksum(), is((short) 0x7bda)); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - UDP udp1 = new UDP(); - udp1.setSourcePort(0x50); - udp1.setDestinationPort(0x60); - - UDP udp2 = new UDP(); - udp2.setSourcePort(0x70); - udp2.setDestinationPort(0x60); - - assertTrue(udp1.equals(udp1)); - assertFalse(udp1.equals(udp2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/VlanIdTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/VlanIdTest.java deleted file mode 100644 index 9ec8ddda..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/VlanIdTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2014 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import com.google.common.testing.EqualsTester; - -public class VlanIdTest { - - @Test - public void testEquality() { - - VlanId vlan1 = VlanId.vlanId((short) -1); - VlanId vlan2 = VlanId.vlanId((short) 100); - VlanId vlan3 = VlanId.vlanId((short) 100); - - new EqualsTester().addEqualityGroup(VlanId.vlanId(), vlan1) - .addEqualityGroup(vlan2, vlan3) - .addEqualityGroup(VlanId.vlanId((short) 10)); - - } - - @Test - public void basics() { - // purposefully create UNTAGGED VLAN - VlanId vlan1 = VlanId.vlanId((short) 10); - VlanId vlan2 = VlanId.vlanId((short) -1); - - assertEquals("incorrect VLAN value", 10, vlan1.toShort()); - assertEquals("invalid untagged value", VlanId.UNTAGGED, vlan2.toShort()); - } - - @Test(expected = IllegalArgumentException.class) - public void testIllicitVLAN() { - VlanId.vlanId((short) 5000); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/AuthenticationTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/AuthenticationTest.java deleted file mode 100644 index 39ab9104..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/AuthenticationTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet.ipv6; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.Data; -import org.onlab.packet.Deserializer; -import org.onlab.packet.UDP; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link Authentication}. - */ -public class AuthenticationTest { - private static Data data; - private static UDP udp; - private static byte[] icv = { - (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 - }; - private static byte[] bytePacket; - - private Deserializer<Authentication> deserializer; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - data = new Data(); - data.setData("testSerialize".getBytes()); - udp = new UDP(); - udp.setPayload(data); - - byte[] bytePayload = udp.serialize(); - byte[] byteHeader = { - (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x00, - (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68, - (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00, - (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 - }; - bytePacket = new byte[byteHeader.length + bytePayload.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); - } - - @Before - public void setUp() { - deserializer = Authentication.deserializer(); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - Authentication auth = new Authentication(); - auth.setNextHeader((byte) 0x11); - auth.setPayloadLength((byte) 0x02); - auth.setSecurityParamIndex(0x13572468); - auth.setSequence(0xffff00); - auth.setIngegrityCheck(icv); - auth.setPayload(udp); - - assertArrayEquals(auth.serialize(), bytePacket); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws Exception { - Authentication auth = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertThat(auth.getNextHeader(), is((byte) 0x11)); - assertThat(auth.getPayloadLength(), is((byte) 0x02)); - assertThat(auth.getSecurityParamIndex(), is(0x13572468)); - assertThat(auth.getSequence(), is(0xffff00)); - assertArrayEquals(auth.getIntegrityCheck(), icv); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - Authentication auth1 = new Authentication(); - auth1.setNextHeader((byte) 0x11); - auth1.setPayloadLength((byte) 0x02); - auth1.setSecurityParamIndex(0x13572468); - auth1.setSequence(0xffff00); - auth1.setIngegrityCheck(icv); - - Authentication auth2 = new Authentication(); - auth2.setNextHeader((byte) 0x11); - auth2.setPayloadLength((byte) 0x02); - auth2.setSecurityParamIndex(0x13572467); - auth2.setSequence(0xffff00); - auth2.setIngegrityCheck(icv); - - assertTrue(auth1.equals(auth1)); - assertFalse(auth1.equals(auth2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/BaseOptionsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/BaseOptionsTest.java deleted file mode 100644 index bb91e9e7..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/BaseOptionsTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet.ipv6; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.Data; -import org.onlab.packet.Deserializer; -import org.onlab.packet.IPv6; -import org.onlab.packet.UDP; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link BaseOptions}. - */ -public class BaseOptionsTest { - private static Data data; - private static UDP udp; - private static byte[] options = { - (byte) 0x00, (byte) 0x03, - (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00 - }; - private static byte[] bytePacket; - - private Deserializer<BaseOptions> deserializer; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - data = new Data(); - data.setData("testSerialize".getBytes()); - udp = new UDP(); - udp.setPayload(data); - - byte[] bytePayload = udp.serialize(); - byte[] byteHeader = { - (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x03, - (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00 - }; - bytePacket = new byte[byteHeader.length + bytePayload.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); - } - - @Before - public void setUp() { - deserializer = BaseOptions.deserializer(); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - BaseOptions baseopt = new BaseOptions(); - baseopt.setNextHeader((byte) 0x11); - baseopt.setHeaderExtLength((byte) 0x00); - baseopt.setOptions(options); - baseopt.setPayload(udp); - - assertArrayEquals(baseopt.serialize(), bytePacket); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws Exception { - BaseOptions baseopt = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertThat(baseopt.getNextHeader(), is((byte) 0x11)); - assertThat(baseopt.getHeaderExtLength(), is((byte) 0x00)); - assertArrayEquals(baseopt.getOptions(), options); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - BaseOptions baseopt1 = new BaseOptions(); - baseopt1.setNextHeader((byte) 0x11); - baseopt1.setHeaderExtLength((byte) 0x00); - baseopt1.setOptions(options); - baseopt1.setType(IPv6.PROTOCOL_HOPOPT); - - BaseOptions baseopt2 = new BaseOptions(); - baseopt2.setNextHeader((byte) 0x11); - baseopt2.setHeaderExtLength((byte) 0x00); - baseopt2.setOptions(options); - baseopt1.setType(IPv6.PROTOCOL_DSTOPT); - - assertTrue(baseopt1.equals(baseopt1)); - assertFalse(baseopt1.equals(baseopt2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/DestinationOptionsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/DestinationOptionsTest.java deleted file mode 100644 index 29dd1266..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/DestinationOptionsTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet.ipv6; - -import org.junit.Test; -import org.onlab.packet.IPv6; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -/** - * Tests for class {@link DestinationOptions}. - */ -public class DestinationOptionsTest { - /** - * Tests constructor. - */ - @Test - public void testConstructor() { - DestinationOptions dstopt = new DestinationOptions(); - assertThat(dstopt.getType(), is(IPv6.PROTOCOL_DSTOPT)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/EncapSecurityPayloadTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/EncapSecurityPayloadTest.java deleted file mode 100644 index e0e99191..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/EncapSecurityPayloadTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet.ipv6; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.Data; -import org.onlab.packet.DeserializationException; -import org.onlab.packet.Deserializer; - -import java.util.Arrays; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link EncapSecurityPayload}. - */ -public class EncapSecurityPayloadTest { - private static Data data; - private static byte[] dataByte = new byte[32]; - private static byte[] bytePacket; - - private Deserializer<EncapSecurityPayload> deserializer; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - Arrays.fill(dataByte, (byte) 0xff); - data = new Data().setData(dataByte); - - byte[] bytePayload = data.serialize(); - byte[] byteHeader = { - (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68, - (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00 - }; - bytePacket = new byte[byteHeader.length + bytePayload.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); - } - - @Before - public void setUp() { - deserializer = EncapSecurityPayload.deserializer(); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - EncapSecurityPayload esp = new EncapSecurityPayload(); - esp.setSecurityParamIndex(0x13572468); - esp.setSequence(0xffff00); - esp.setPayload(data); - - assertArrayEquals(esp.serialize(), bytePacket); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws DeserializationException { - EncapSecurityPayload esp = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertThat(esp.getSecurityParamIndex(), is(0x13572468)); - assertThat(esp.getSequence(), is(0xffff00)); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - EncapSecurityPayload esp1 = new EncapSecurityPayload(); - esp1.setSecurityParamIndex(0x13572468); - esp1.setSequence(0xffff00); - - EncapSecurityPayload esp2 = new EncapSecurityPayload(); - esp2.setSecurityParamIndex(0x13572468); - esp2.setSequence(0xfffff0); - - assertTrue(esp1.equals(esp1)); - assertFalse(esp1.equals(esp2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/FragmentTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/FragmentTest.java deleted file mode 100644 index f2d5e48e..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/FragmentTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet.ipv6; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.Data; -import org.onlab.packet.DeserializationException; -import org.onlab.packet.Deserializer; -import org.onlab.packet.UDP; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link Fragment}. - */ -public class FragmentTest { - private static Data data; - private static UDP udp; - private static byte[] bytePacket; - - private Deserializer<Fragment> deserializer; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - data = new Data(); - data.setData("testSerialize".getBytes()); - udp = new UDP(); - udp.setPayload(data); - - byte[] bytePayload = udp.serialize(); - byte[] byteHeader = { - (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0xf9, - (byte) 0x00, (byte) 0x00, (byte) 0x13, (byte) 0x57 - }; - bytePacket = new byte[byteHeader.length + bytePayload.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); - } - - @Before - public void setUp() { - deserializer = Fragment.deserializer(); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - Fragment frag = new Fragment(); - frag.setNextHeader((byte) 0x11); - frag.setFragmentOffset((short) 0x1f); - frag.setMoreFragment((byte) 1); - frag.setIdentification(0x1357); - frag.setPayload(udp); - - assertArrayEquals(frag.serialize(), bytePacket); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws DeserializationException { - Fragment frag = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertThat(frag.getNextHeader(), is((byte) 0x11)); - assertThat(frag.getFragmentOffset(), is((short) 0x1f)); - assertThat(frag.getMoreFragment(), is((byte) 1)); - assertThat(frag.getIdentification(), is(0x1357)); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - Fragment frag1 = new Fragment(); - frag1.setNextHeader((byte) 0x11); - frag1.setFragmentOffset((short) 0x1f); - frag1.setMoreFragment((byte) 1); - frag1.setIdentification(0x1357); - - Fragment frag2 = new Fragment(); - frag2.setNextHeader((byte) 0x11); - frag2.setFragmentOffset((short) 0x1f); - frag2.setMoreFragment((byte) 1); - frag2.setIdentification(0x1358); - - assertTrue(frag1.equals(frag1)); - assertFalse(frag1.equals(frag2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/HopByHopOptionsTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/HopByHopOptionsTest.java deleted file mode 100644 index 1e9be2f6..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/HopByHopOptionsTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet.ipv6; - -import org.junit.Test; -import org.onlab.packet.IPv6; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -/** - * Tests for class {@link HopByHopOptions}. - */ -public class HopByHopOptionsTest { - /** - * Tests constructor. - */ - @Test - public void testConstructor() { - HopByHopOptions hopopt = new HopByHopOptions(); - assertThat(hopopt.getType(), is(IPv6.PROTOCOL_HOPOPT)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/RoutingTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/RoutingTest.java deleted file mode 100644 index a03bacc9..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ipv6/RoutingTest.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onlab.packet.ipv6; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.Data; -import org.onlab.packet.DeserializationException; -import org.onlab.packet.Deserializer; -import org.onlab.packet.UDP; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link Routing}. - */ -public class RoutingTest { - private static Data data; - private static UDP udp; - private static byte[] routingData = { - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, - (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce - }; - private static byte[] bytePacket; - - private Deserializer<Routing> deserializer; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - data = new Data(); - data.setData("testSerialize".getBytes()); - udp = new UDP(); - udp.setPayload(data); - - byte[] bytePayload = udp.serialize(); - byte[] byteHeader = { - (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x03, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, - (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce - }; - bytePacket = new byte[byteHeader.length + bytePayload.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); - } - - @Before - public void setUp() { - deserializer = Routing.deserializer(); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - Routing routing = new Routing(); - routing.setNextHeader((byte) 0x11); - routing.setHeaderExtLength((byte) 0x02); - routing.setRoutingType((byte) 0x00); - routing.setSegmntsLeft((byte) 0x03); - routing.setRoutingData(routingData); - routing.setPayload(udp); - - assertArrayEquals(routing.serialize(), bytePacket); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws DeserializationException { - Routing routing = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertThat(routing.getNextHeader(), is((byte) 0x11)); - assertThat(routing.getHeaderExtLength(), is((byte) 0x02)); - assertThat(routing.getRoutingType(), is((byte) 0x00)); - assertThat(routing.getSegmentsLeft(), is((byte) 0x03)); - assertArrayEquals(routing.getRoutingData(), routingData); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - Routing routing1 = new Routing(); - routing1.setNextHeader((byte) 0x11); - routing1.setHeaderExtLength((byte) 0x02); - routing1.setRoutingType((byte) 0x00); - routing1.setSegmntsLeft((byte) 0x03); - routing1.setRoutingData(routingData); - - Routing routing2 = new Routing(); - routing2.setNextHeader((byte) 0x11); - routing2.setHeaderExtLength((byte) 0x02); - routing2.setRoutingType((byte) 0x00); - routing2.setSegmntsLeft((byte) 0x02); - routing2.setRoutingData(routingData); - - assertTrue(routing1.equals(routing1)); - assertFalse(routing1.equals(routing2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborAdvertisementTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborAdvertisementTest.java deleted file mode 100644 index d537251c..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborAdvertisementTest.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet.ndp; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.DeserializationException; -import org.onlab.packet.Deserializer; -import org.onlab.packet.MacAddress; -import org.onlab.packet.PacketTestUtils; - -import java.nio.ByteBuffer; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link NeighborAdvertisement}. - */ -public class NeighborAdvertisementTest { - private static final byte[] TARGET_ADDRESS = { - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, - (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce - }; - private static final MacAddress MAC_ADDRESS = - MacAddress.valueOf("11:22:33:44:55:66"); - - private static byte[] bytePacket; - - private Deserializer<NeighborAdvertisement> deserializer - = NeighborAdvertisement.deserializer(); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - byte[] byteHeader = { - (byte) 0xe0, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, - (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, - (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, - (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 - }; - bytePacket = new byte[byteHeader.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - NeighborAdvertisement na = new NeighborAdvertisement(); - na.setRouterFlag((byte) 1); - na.setSolicitedFlag((byte) 1); - na.setOverrideFlag((byte) 1); - na.setTargetAddress(TARGET_ADDRESS); - na.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - assertArrayEquals(na.serialize(), bytePacket); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(NeighborAdvertisement.deserializer()); - } - - @Test - public void testDeserializeTruncated() throws Exception { - // Run the truncation test only on the NeighborAdvertisement header - byte[] naHeader = new byte[NeighborAdvertisement.HEADER_LENGTH]; - ByteBuffer.wrap(bytePacket).get(naHeader); - - PacketTestUtils.testDeserializeTruncated(NeighborAdvertisement.deserializer(), naHeader); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws DeserializationException { - NeighborAdvertisement na = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertThat(na.getRouterFlag(), is((byte) 1)); - assertThat(na.getSolicitedFlag(), is((byte) 1)); - assertThat(na.getOverrideFlag(), is((byte) 1)); - assertArrayEquals(na.getTargetAddress(), TARGET_ADDRESS); - - // Check the option(s) - assertThat(na.getOptions().size(), is(1)); - NeighborDiscoveryOptions.Option option = na.getOptions().get(0); - assertThat(option.type(), - is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); - assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - NeighborAdvertisement na1 = new NeighborAdvertisement(); - na1.setRouterFlag((byte) 1); - na1.setSolicitedFlag((byte) 1); - na1.setOverrideFlag((byte) 1); - na1.setTargetAddress(TARGET_ADDRESS); - na1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - NeighborAdvertisement na2 = new NeighborAdvertisement(); - na2.setRouterFlag((byte) 1); - na2.setSolicitedFlag((byte) 1); - na2.setOverrideFlag((byte) 0); - na2.setTargetAddress(TARGET_ADDRESS); - na2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - assertTrue(na1.equals(na1)); - assertFalse(na1.equals(na2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborSolicitationTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborSolicitationTest.java deleted file mode 100644 index 03e57f5e..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/NeighborSolicitationTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet.ndp; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.DeserializationException; -import org.onlab.packet.Deserializer; -import org.onlab.packet.MacAddress; -import org.onlab.packet.PacketTestUtils; - -import java.nio.ByteBuffer; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link NeighborSolicitation}. - */ -public class NeighborSolicitationTest { - private static final byte[] TARGET_ADDRESS = { - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, - (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce - }; - private static final byte[] TARGET_ADDRESS2 = { - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, - (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 - }; - private static final MacAddress MAC_ADDRESS = - MacAddress.valueOf("11:22:33:44:55:66"); - - private static byte[] bytePacket; - - private Deserializer<NeighborSolicitation> deserializer - = NeighborSolicitation.deserializer(); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - byte[] byteHeader = { - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, - (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, - (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, - (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 - }; - bytePacket = new byte[byteHeader.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - NeighborSolicitation ns = new NeighborSolicitation(); - ns.setTargetAddress(TARGET_ADDRESS); - ns.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - assertArrayEquals(ns.serialize(), bytePacket); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(NeighborSolicitation.deserializer()); - } - - @Test - public void testDeserializeTruncated() throws Exception { - // Run the truncation test only on the NeighborSolicitation header - byte[] nsHeader = new byte[NeighborSolicitation.HEADER_LENGTH]; - ByteBuffer.wrap(bytePacket).get(nsHeader); - - PacketTestUtils.testDeserializeTruncated(NeighborSolicitation.deserializer(), nsHeader); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws DeserializationException { - NeighborSolicitation ns = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS); - - // Check the option(s) - assertThat(ns.getOptions().size(), is(1)); - NeighborDiscoveryOptions.Option option = ns.getOptions().get(0); - assertThat(option.type(), - is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); - assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - NeighborSolicitation ns1 = new NeighborSolicitation(); - ns1.setTargetAddress(TARGET_ADDRESS); - ns1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - NeighborSolicitation ns2 = new NeighborSolicitation(); - ns2.setTargetAddress(TARGET_ADDRESS2); - ns2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - assertTrue(ns1.equals(ns1)); - assertFalse(ns1.equals(ns2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RedirectTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RedirectTest.java deleted file mode 100644 index 31157145..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RedirectTest.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet.ndp; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.DeserializationException; -import org.onlab.packet.Deserializer; -import org.onlab.packet.MacAddress; -import org.onlab.packet.PacketTestUtils; - -import java.nio.ByteBuffer; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link Redirect}. - */ -public class RedirectTest { - private static final byte[] TARGET_ADDRESS = { - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 - }; - private static final byte[] DESTINATION_ADDRESS = { - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, - (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce - }; - private static final byte[] DESTINATION_ADDRESS2 = { - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, - (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 - }; - private static final MacAddress MAC_ADDRESS = - MacAddress.valueOf("11:22:33:44:55:66"); - - private static byte[] bytePacket; - - private Deserializer<Redirect> deserializer = Redirect.deserializer(); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - byte[] byteHeader = { - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, - (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, - (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, - (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, - (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, - (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 - }; - bytePacket = new byte[byteHeader.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - Redirect rd = new Redirect(); - rd.setTargetAddress(TARGET_ADDRESS); - rd.setDestinationAddress(DESTINATION_ADDRESS); - rd.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - assertArrayEquals(rd.serialize(), bytePacket); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(Redirect.deserializer()); - } - - @Test - public void testDeserializeTruncated() throws Exception { - // Run the truncation test only on the Redirect header - byte[] rdHeader = new byte[Redirect.HEADER_LENGTH]; - ByteBuffer.wrap(bytePacket).get(rdHeader); - - PacketTestUtils.testDeserializeTruncated(Redirect.deserializer(), rdHeader); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws DeserializationException { - Redirect rd = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertArrayEquals(rd.getTargetAddress(), TARGET_ADDRESS); - assertArrayEquals(rd.getDestinationAddress(), DESTINATION_ADDRESS); - - // Check the option(s) - assertThat(rd.getOptions().size(), is(1)); - NeighborDiscoveryOptions.Option option = rd.getOptions().get(0); - assertThat(option.type(), - is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); - assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - Redirect rd1 = new Redirect(); - rd1.setTargetAddress(TARGET_ADDRESS); - rd1.setDestinationAddress(DESTINATION_ADDRESS); - rd1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - Redirect rd2 = new Redirect(); - rd2.setTargetAddress(TARGET_ADDRESS); - rd2.setDestinationAddress(DESTINATION_ADDRESS2); - rd2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - assertTrue(rd1.equals(rd1)); - assertFalse(rd1.equals(rd2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterAdvertisementTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterAdvertisementTest.java deleted file mode 100644 index ede15747..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterAdvertisementTest.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet.ndp; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.DeserializationException; -import org.onlab.packet.Deserializer; -import org.onlab.packet.MacAddress; -import org.onlab.packet.PacketTestUtils; - -import java.nio.ByteBuffer; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link RouterAdvertisement}. - */ -public class RouterAdvertisementTest { - private static final MacAddress MAC_ADDRESS = - MacAddress.valueOf("11:22:33:44:55:66"); - - private static byte[] bytePacket; - - private Deserializer<RouterAdvertisement> deserializer - = RouterAdvertisement.deserializer(); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - byte[] byteHeader = { - (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58, - (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8, - (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4, - (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, - (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 - }; - bytePacket = new byte[byteHeader.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - RouterAdvertisement ra = new RouterAdvertisement(); - ra.setCurrentHopLimit((byte) 3); - ra.setMFlag((byte) 1); - ra.setOFlag((byte) 1); - ra.setRouterLifetime((short) 0x258); - ra.setReachableTime(0x3e8); - ra.setRetransmitTimer(0x1f4); - ra.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - assertArrayEquals(ra.serialize(), bytePacket); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(RouterAdvertisement.deserializer()); - } - - @Test - public void testDeserializeTruncated() throws Exception { - // Run the truncation test only on the RouterAdvertisement header - byte[] raHeader = new byte[RouterAdvertisement.HEADER_LENGTH]; - ByteBuffer.wrap(bytePacket).get(raHeader); - - PacketTestUtils.testDeserializeTruncated(RouterAdvertisement.deserializer(), raHeader); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws DeserializationException { - RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - assertThat(ra.getCurrentHopLimit(), is((byte) 3)); - assertThat(ra.getMFlag(), is((byte) 1)); - assertThat(ra.getOFlag(), is((byte) 1)); - assertThat(ra.getRouterLifetime(), is((short) 0x258)); - assertThat(ra.getReachableTime(), is(0x3e8)); - assertThat(ra.getRetransmitTimer(), is(0x1f4)); - - // Check the option(s) - assertThat(ra.getOptions().size(), is(1)); - NeighborDiscoveryOptions.Option option = ra.getOptions().get(0); - assertThat(option.type(), - is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); - assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - RouterAdvertisement ra1 = new RouterAdvertisement(); - ra1.setCurrentHopLimit((byte) 3); - ra1.setMFlag((byte) 1); - ra1.setOFlag((byte) 1); - ra1.setRouterLifetime((short) 0x258); - ra1.setReachableTime(0x3e8); - ra1.setRetransmitTimer(0x1f4); - ra1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - RouterAdvertisement ra2 = new RouterAdvertisement(); - ra2.setCurrentHopLimit((byte) 3); - ra2.setMFlag((byte) 0); - ra2.setOFlag((byte) 0); - ra2.setRouterLifetime((short) 0x1f4); - ra2.setReachableTime(0x3e8); - ra2.setRetransmitTimer(0x1f4); - ra2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS.toBytes()); - - assertTrue(ra1.equals(ra1)); - assertFalse(ra1.equals(ra2)); - } -} diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterSolicitationTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterSolicitationTest.java deleted file mode 100644 index e3b5686c..00000000 --- a/framework/src/onos/utils/misc/src/test/java/org/onlab/packet/ndp/RouterSolicitationTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onlab.packet.ndp; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.onlab.packet.Deserializer; -import org.onlab.packet.MacAddress; -import org.onlab.packet.PacketTestUtils; - -import java.nio.ByteBuffer; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * Tests for class {@link RouterSolicitation}. - */ -public class RouterSolicitationTest { - private static final MacAddress MAC_ADDRESS1 = - MacAddress.valueOf("11:22:33:44:55:66"); - private static final MacAddress MAC_ADDRESS2 = - MacAddress.valueOf("11:22:33:44:55:00"); - - private static byte[] bytePacket; - - private Deserializer<RouterSolicitation> deserializer - = RouterSolicitation.deserializer(); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - byte[] byteHeader = { - (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, - (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, - (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 - }; - bytePacket = new byte[byteHeader.length]; - System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); - } - - /** - * Tests serialize and setters. - */ - @Test - public void testSerialize() { - RouterSolicitation rs = new RouterSolicitation(); - rs.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS1.toBytes()); - - assertArrayEquals(rs.serialize(), bytePacket); - } - - @Test - public void testDeserializeBadInput() throws Exception { - PacketTestUtils.testDeserializeBadInput(RouterSolicitation.deserializer()); - } - - @Test - public void testDeserializeTruncated() throws Exception { - // Run the truncation test only on the RouterSolicitation header - byte[] rsHeader = new byte[RouterSolicitation.HEADER_LENGTH]; - ByteBuffer.wrap(bytePacket).get(rsHeader); - - PacketTestUtils.testDeserializeTruncated(RouterSolicitation.deserializer(), rsHeader); - } - - /** - * Tests deserialize and getters. - */ - @Test - public void testDeserialize() throws Exception { - RouterSolicitation rs = deserializer.deserialize(bytePacket, 0, bytePacket.length); - - // Check the option(s) - assertThat(rs.getOptions().size(), is(1)); - NeighborDiscoveryOptions.Option option = rs.getOptions().get(0); - assertThat(option.type(), - is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); - assertArrayEquals(option.data(), MAC_ADDRESS1.toBytes()); - } - - /** - * Tests comparator. - */ - @Test - public void testEqual() { - RouterSolicitation rs1 = new RouterSolicitation(); - rs1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS1.toBytes()); - - RouterSolicitation rs2 = new RouterSolicitation(); - rs2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, - MAC_ADDRESS2.toBytes()); - - assertTrue(rs1.equals(rs1)); - assertFalse(rs1.equals(rs2)); - } -} |