Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.packet; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | import org.junit.Before; |
| 5 | import org.junit.Test; |
| 6 | |
| 7 | public class PacketTest { |
| 8 | protected IPacket pkt1, pkt2, pkt3, pkt4; |
| 9 | protected IPacket dummyPkt; |
| 10 | protected IPacket[] packets; |
| 11 | |
| 12 | @Before |
| 13 | public void setUp() { |
| 14 | this.pkt1 = new Ethernet() |
| 15 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 16 | .setSourceMACAddress("00:44:33:22:11:00") |
| 17 | .setEtherType(Ethernet.TYPE_IPv4) |
| 18 | .setPayload( |
| 19 | new IPv4() |
| 20 | .setTtl((byte) 128) |
| 21 | .setSourceAddress("192.168.1.1") |
| 22 | .setDestinationAddress("192.168.1.2") |
| 23 | .setPayload(new UDP() |
| 24 | .setSourcePort((short) 5000) |
| 25 | .setDestinationPort((short) 5001) |
| 26 | .setPayload(new Data(new byte[] {0x01})))); |
| 27 | |
| 28 | this.pkt2 = new Ethernet() |
| 29 | .setSourceMACAddress("00:44:33:22:11:01") |
| 30 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 31 | .setEtherType(Ethernet.TYPE_ARP) |
| 32 | .setVlanID((short)5) |
| 33 | .setPayload( |
| 34 | new ARP() |
| 35 | .setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 36 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 37 | .setHardwareAddressLength((byte) 6) |
| 38 | .setProtocolAddressLength((byte) 4) |
| 39 | .setOpCode(ARP.OP_REPLY) |
| 40 | .setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:01")) |
| 41 | .setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1")) |
| 42 | .setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55")) |
| 43 | .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2"))); |
| 44 | |
| 45 | |
| 46 | this.pkt3 = new Ethernet() |
| 47 | .setSourceMACAddress("00:44:33:22:11:01") |
| 48 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 49 | .setEtherType(Ethernet.TYPE_ARP) |
| 50 | .setPayload( |
| 51 | new ARP() |
| 52 | .setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 53 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 54 | .setHardwareAddressLength((byte) 6) |
| 55 | .setProtocolAddressLength((byte) 4) |
| 56 | .setOpCode(ARP.OP_REPLY) |
| 57 | .setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:01")) |
| 58 | .setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1")) |
| 59 | .setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55")) |
| 60 | .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2"))); |
| 61 | |
| 62 | this.pkt4 = new Ethernet() |
| 63 | .setDestinationMACAddress("FF:FF:FF:FF:FF:FF") |
| 64 | .setSourceMACAddress("00:11:33:55:77:01") |
| 65 | .setEtherType(Ethernet.TYPE_IPv4) |
| 66 | .setPayload( |
| 67 | new IPv4() |
| 68 | .setTtl((byte) 128) |
| 69 | .setSourceAddress("192.168.10.1") |
| 70 | .setDestinationAddress("192.168.255.255") |
| 71 | .setPayload(new UDP() |
| 72 | .setSourcePort((short) 5000) |
| 73 | .setDestinationPort((short) 5001) |
| 74 | .setPayload(new Data(new byte[] {0x01})))); |
| 75 | |
| 76 | this.dummyPkt = new IPv4() |
| 77 | .setTtl((byte) 32) |
| 78 | .setSourceAddress("1.2.3.4") |
| 79 | .setDestinationAddress("5.6.7.8"); |
| 80 | |
| 81 | this.packets = new IPacket[] { pkt1, pkt2, pkt3, pkt4 }; |
| 82 | } |
| 83 | |
| 84 | protected void doTestClone(IPacket pkt) { |
| 85 | if (pkt.getPayload() != null) |
| 86 | doTestClone(pkt.getPayload()); |
| 87 | IPacket newPkt = (IPacket)pkt.clone(); |
| 88 | assertSame(pkt.getClass(), newPkt.getClass()); |
| 89 | assertNotSame(pkt, newPkt); |
| 90 | assertSame(pkt.getParent(), newPkt.getParent()); |
| 91 | assertEquals(pkt, newPkt); |
| 92 | assertEquals(pkt.getPayload(), newPkt.getPayload()); |
| 93 | if (pkt.getPayload() != null) |
| 94 | assertNotSame(pkt.getPayload(), newPkt.getPayload()); |
| 95 | |
| 96 | if (pkt instanceof Ethernet) { |
| 97 | Ethernet eth = (Ethernet)pkt; |
| 98 | Ethernet newEth = (Ethernet)newPkt; |
| 99 | newEth.setDestinationMACAddress(new byte[] { 1,2,3,4,5,6}); |
| 100 | assertEquals(false, newEth.getDestinationMAC() |
| 101 | .equals(eth.getDestinationMAC())); |
| 102 | assertEquals(false, newPkt.equals(pkt)); |
| 103 | } |
| 104 | if (pkt instanceof ARP) { |
| 105 | ARP arp = (ARP)pkt; |
| 106 | ARP newArp = (ARP)newPkt; |
| 107 | newArp.setSenderProtocolAddress(new byte[] {1,2,3,4}); |
| 108 | assertEquals(false, newArp.getSenderProtocolAddress() |
| 109 | .equals(arp.getSenderProtocolAddress())); |
| 110 | assertEquals(false, newPkt.equals(pkt)); |
| 111 | } |
| 112 | |
| 113 | byte[] dummyData = dummyPkt.serialize().clone(); |
| 114 | newPkt = (IPacket)pkt.clone(); |
| 115 | newPkt.deserialize(dummyData, 0, dummyData.length); |
| 116 | assertEquals(false, newPkt.equals(pkt)); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testClone() { |
| 121 | for (IPacket pkt: packets) { |
| 122 | doTestClone(pkt); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |