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