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 | import static org.junit.Assert.assertTrue; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for IPv4 class. |
| 29 | */ |
| 30 | public class IPv4Test { |
| 31 | |
| 32 | private Deserializer<IPv4> deserializer; |
| 33 | |
| 34 | private byte version = 4; |
| 35 | private byte headerLength = 6; |
| 36 | private byte diffServ = 2; |
| 37 | private short totalLength = 20; |
| 38 | private short identification = 1; |
| 39 | private byte flags = 1; |
| 40 | private short fragmentOffset = 1; |
| 41 | private byte ttl = 60; |
| 42 | private byte protocol = 4; |
| 43 | private short checksum = 4; |
| 44 | private int sourceAddress = 1; |
| 45 | private int destinationAddress = 2; |
| 46 | private byte[] options = new byte[] {0x1, 0x2, 0x3, 0x4}; |
| 47 | |
| 48 | private byte[] headerBytes; |
| 49 | |
| 50 | @Before |
| 51 | public void setUp() throws Exception { |
| 52 | deserializer = IPv4.deserializer(); |
| 53 | |
| 54 | ByteBuffer bb = ByteBuffer.allocate(headerLength * 4); |
| 55 | |
| 56 | bb.put((byte) ((version & 0xf) << 4 | headerLength & 0xf)); |
| 57 | bb.put(diffServ); |
| 58 | bb.putShort(totalLength); |
| 59 | bb.putShort(identification); |
| 60 | bb.putShort((short) ((flags & 0x7) << 13 | fragmentOffset & 0x1fff)); |
| 61 | bb.put(ttl); |
| 62 | bb.put(protocol); |
| 63 | bb.putShort(checksum); |
| 64 | bb.putInt(sourceAddress); |
| 65 | bb.putInt(destinationAddress); |
| 66 | bb.put(options); |
| 67 | |
| 68 | headerBytes = bb.array(); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testDeserializeBadInput() throws Exception { |
| 73 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testDeserializeTruncated() throws Exception { |
| 78 | PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testDeserialize() throws Exception { |
| 83 | IPv4 ipv4 = deserializer.deserialize(headerBytes, 0, headerBytes.length); |
| 84 | |
| 85 | assertEquals(version, ipv4.getVersion()); |
| 86 | assertEquals(headerLength, ipv4.getHeaderLength()); |
| 87 | assertEquals(diffServ, ipv4.getDiffServ()); |
| 88 | assertEquals(totalLength, ipv4.getTotalLength()); |
| 89 | assertEquals(identification, ipv4.getIdentification()); |
| 90 | assertEquals(flags, ipv4.getFlags()); |
| 91 | assertEquals(fragmentOffset, ipv4.getFragmentOffset()); |
| 92 | assertEquals(ttl, ipv4.getTtl()); |
| 93 | assertEquals(protocol, ipv4.getProtocol()); |
| 94 | assertEquals(checksum, ipv4.getChecksum()); |
| 95 | assertEquals(sourceAddress, ipv4.getSourceAddress()); |
| 96 | assertEquals(destinationAddress, ipv4.getDestinationAddress()); |
| 97 | assertTrue(ipv4.isTruncated()); |
| 98 | } |
| 99 | } |