Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 17 | package org.onlab.packet; |
| 18 | |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for the ICMP class. |
| 28 | */ |
| 29 | public class ICMPTest { |
| 30 | |
| 31 | private Deserializer<ICMP> deserializer; |
| 32 | |
| 33 | private byte icmpType = ICMP.TYPE_ECHO_REQUEST; |
| 34 | private byte icmpCode = 4; |
| 35 | private short checksum = 870; |
| 36 | |
| 37 | private byte[] headerBytes; |
| 38 | |
| 39 | @Before |
| 40 | public void setUp() throws Exception { |
| 41 | deserializer = ICMP.deserializer(); |
| 42 | |
| 43 | ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH); |
| 44 | |
| 45 | bb.put(icmpType); |
| 46 | bb.put(icmpCode); |
| 47 | bb.putShort(checksum); |
| 48 | |
| 49 | headerBytes = bb.array(); |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testDeserializeBadInput() throws Exception { |
| 54 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testDeserializeTruncated() throws Exception { |
| 59 | PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testDeserialize() throws Exception { |
| 64 | ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length); |
| 65 | |
| 66 | assertEquals(icmpType, icmp.getIcmpType()); |
| 67 | assertEquals(icmpCode, icmp.getIcmpCode()); |
| 68 | assertEquals(checksum, icmp.getChecksum()); |
| 69 | } |
| 70 | } |