Daniel Park | d27dcda | 2018-08-07 01:40:40 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onlab.packet; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | import static org.onlab.packet.Ethernet.TYPE_IPV4; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for the ICMPEcho class. |
| 29 | */ |
| 30 | public class ICMPEchoTest { |
| 31 | private Deserializer<ICMPEcho> deserializer; |
| 32 | |
| 33 | protected byte icmpType = ICMP.TYPE_ECHO_REQUEST; |
| 34 | protected byte icmpCode = ICMP.CODE_ECHO_REQEUST; |
| 35 | protected short checksum = 870; |
| 36 | private short identifier = 1; |
| 37 | private short sequenceNum = 2; |
| 38 | private MacAddress srcMac = MacAddress.valueOf("11:22:33:44:55:66"); |
| 39 | private IpAddress srcIp = IpAddress.valueOf("10.0.0.3"); |
| 40 | private MacAddress dstMac = MacAddress.valueOf("66:55:44:33:22:11"); |
| 41 | private IpAddress dstIp = IpAddress.valueOf("10.0.0.1"); |
| 42 | |
| 43 | private byte[] headerBytes; |
| 44 | |
| 45 | /** |
| 46 | * Initial setup for this unit test. |
| 47 | */ |
| 48 | @Before |
| 49 | public void setUp() throws Exception { |
| 50 | deserializer = ICMPEcho.deserializer(); |
| 51 | |
| 52 | ByteBuffer bb = ByteBuffer.allocate(ICMPEcho.ICMP_ECHO_HEADER_LENGTH); |
| 53 | |
| 54 | bb.putShort(identifier); |
| 55 | bb.putShort(sequenceNum); |
| 56 | |
| 57 | headerBytes = bb.array(); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Tests the deserializeBadInput. |
| 63 | * |
| 64 | * @throws Exception exception |
| 65 | */ |
| 66 | @Test |
| 67 | public void testDeserializeBadInput() throws Exception { |
| 68 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Tests the deserialzeTruncates. |
| 73 | * |
| 74 | * @throws Exception exception |
| 75 | */ |
| 76 | @Test |
| 77 | public void testDeserializeTruncated() throws Exception { |
| 78 | PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests deserialize, serialize, getters and setters. |
| 83 | * |
| 84 | * @throws Exception exception |
| 85 | */ |
| 86 | @Test |
| 87 | public void testIcmpEchoPacket() throws Exception { |
| 88 | ICMPEcho icmpEcho = new ICMPEcho(); |
| 89 | |
| 90 | icmpEcho.setIdentifier(identifier); |
| 91 | icmpEcho.setSequenceNum(sequenceNum); |
| 92 | |
| 93 | ByteBuffer bb = ByteBuffer.wrap(icmpEcho.serialize()); |
| 94 | |
| 95 | ICMPEcho deserializedIcmpEcho = deserializer.deserialize(bb.array(), 0, bb.array().length); |
| 96 | |
| 97 | assertTrue(deserializedIcmpEcho.equals(icmpEcho)); |
| 98 | |
| 99 | assertEquals(deserializedIcmpEcho.getIdentifier(), icmpEcho.getIdentifier()); |
| 100 | assertEquals(deserializedIcmpEcho.getSequenceNum(), icmpEcho.getSequenceNum()); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testDeserializeFromIpPacket() throws Exception { |
| 105 | ICMPEcho icmpEcho = new ICMPEcho(); |
| 106 | icmpEcho.setIdentifier(identifier) |
| 107 | .setSequenceNum(sequenceNum); |
| 108 | ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize()); |
| 109 | |
| 110 | ICMP icmp = new ICMP(); |
| 111 | icmp.setIcmpType(icmpType) |
| 112 | .setIcmpCode(icmpCode) |
| 113 | .setChecksum(checksum); |
| 114 | |
| 115 | icmp.setPayload(ICMPEcho.deserializer().deserialize(byteBufferIcmpEcho.array(), |
| 116 | 0, ICMPEcho.ICMP_ECHO_HEADER_LENGTH)); |
| 117 | ByteBuffer byteBufferIcmp = ByteBuffer.wrap(icmp.serialize()); |
| 118 | |
| 119 | |
| 120 | IPv4 iPacket = new IPv4(); |
| 121 | iPacket.setDestinationAddress(dstIp.toString()); |
| 122 | iPacket.setSourceAddress(srcIp.toString()); |
| 123 | iPacket.setTtl((byte) 64); |
| 124 | iPacket.setChecksum((short) 0); |
| 125 | iPacket.setDiffServ((byte) 0); |
| 126 | iPacket.setProtocol(IPv4.PROTOCOL_ICMP); |
| 127 | |
| 128 | iPacket.setPayload(ICMP.deserializer().deserialize(byteBufferIcmp.array(), |
| 129 | 0, |
| 130 | byteBufferIcmp.array().length)); |
| 131 | |
| 132 | Ethernet ethPacket = new Ethernet(); |
| 133 | |
| 134 | ethPacket.setEtherType(TYPE_IPV4); |
| 135 | ethPacket.setSourceMACAddress(srcMac); |
| 136 | ethPacket.setDestinationMACAddress(dstMac); |
| 137 | ethPacket.setPayload(iPacket); |
| 138 | |
| 139 | validatePacket(ethPacket); |
| 140 | } |
| 141 | |
| 142 | private void validatePacket(Ethernet ethPacket) { |
| 143 | ICMP icmp = (ICMP) ethPacket.getPayload().getPayload(); |
| 144 | ICMPEcho icmpEcho = (ICMPEcho) icmp.getPayload(); |
| 145 | |
| 146 | assertEquals(icmp.getIcmpType(), icmpType); |
| 147 | assertEquals(icmp.getIcmpCode(), icmpCode); |
| 148 | assertEquals(icmp.getChecksum(), checksum); |
| 149 | assertEquals(icmpEcho.getIdentifier(), identifier); |
| 150 | assertEquals(icmpEcho.getSequenceNum(), sequenceNum); |
| 151 | } |
| 152 | |
| 153 | } |