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 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 26 | import static org.junit.Assert.assertTrue; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Unit tests for the ICMP class. |
| 30 | */ |
| 31 | public class ICMPTest { |
| 32 | |
| 33 | private Deserializer<ICMP> deserializer; |
| 34 | |
| 35 | private byte icmpType = ICMP.TYPE_ECHO_REQUEST; |
| 36 | private byte icmpCode = 4; |
| 37 | private short checksum = 870; |
| 38 | |
| 39 | private byte[] headerBytes; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() throws Exception { |
| 43 | deserializer = ICMP.deserializer(); |
| 44 | |
| 45 | ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH); |
| 46 | |
| 47 | bb.put(icmpType); |
| 48 | bb.put(icmpCode); |
| 49 | bb.putShort(checksum); |
| 50 | |
| 51 | headerBytes = bb.array(); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void testDeserializeBadInput() throws Exception { |
| 56 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testDeserializeTruncated() throws Exception { |
| 61 | PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes); |
| 62 | } |
| 63 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Tests deserialize and getters. |
| 66 | */ |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 67 | @Test |
| 68 | public void testDeserialize() throws Exception { |
| 69 | ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length); |
| 70 | |
| 71 | assertEquals(icmpType, icmp.getIcmpType()); |
| 72 | assertEquals(icmpCode, icmp.getIcmpCode()); |
| 73 | assertEquals(checksum, icmp.getChecksum()); |
| 74 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Tests toString. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testToStringIcmp() throws Exception { |
| 81 | ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length); |
| 82 | String str = icmp.toString(); |
| 83 | |
| 84 | assertTrue(StringUtils.contains(str, "icmpType=" + icmpType)); |
| 85 | assertTrue(StringUtils.contains(str, "icmpCode=" + icmpCode)); |
| 86 | assertTrue(StringUtils.contains(str, "checksum=" + checksum)); |
| 87 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 88 | } |