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