Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 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; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for IPv4 class. |
| 30 | */ |
| 31 | public class IPv4Test { |
| 32 | |
| 33 | private Deserializer<IPv4> deserializer; |
| 34 | |
| 35 | private byte version = 4; |
| 36 | private byte headerLength = 6; |
| 37 | private byte diffServ = 2; |
| 38 | private short totalLength = 20; |
| 39 | private short identification = 1; |
| 40 | private byte flags = 1; |
| 41 | private short fragmentOffset = 1; |
| 42 | private byte ttl = 60; |
| 43 | private byte protocol = 4; |
| 44 | private short checksum = 4; |
| 45 | private int sourceAddress = 1; |
| 46 | private int destinationAddress = 2; |
| 47 | private byte[] options = new byte[] {0x1, 0x2, 0x3, 0x4}; |
| 48 | |
| 49 | private byte[] headerBytes; |
| 50 | |
| 51 | @Before |
| 52 | public void setUp() throws Exception { |
| 53 | deserializer = IPv4.deserializer(); |
| 54 | |
| 55 | ByteBuffer bb = ByteBuffer.allocate(headerLength * 4); |
| 56 | |
| 57 | bb.put((byte) ((version & 0xf) << 4 | headerLength & 0xf)); |
| 58 | bb.put(diffServ); |
| 59 | bb.putShort(totalLength); |
| 60 | bb.putShort(identification); |
| 61 | bb.putShort((short) ((flags & 0x7) << 13 | fragmentOffset & 0x1fff)); |
| 62 | bb.put(ttl); |
| 63 | bb.put(protocol); |
| 64 | bb.putShort(checksum); |
| 65 | bb.putInt(sourceAddress); |
| 66 | bb.putInt(destinationAddress); |
| 67 | bb.put(options); |
| 68 | |
| 69 | headerBytes = bb.array(); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testDeserializeBadInput() throws Exception { |
| 74 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testDeserializeTruncated() throws Exception { |
| 79 | PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes); |
| 80 | } |
| 81 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Tests deserialize and getters. |
| 84 | */ |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 85 | @Test |
| 86 | public void testDeserialize() throws Exception { |
| 87 | IPv4 ipv4 = deserializer.deserialize(headerBytes, 0, headerBytes.length); |
| 88 | |
| 89 | assertEquals(version, ipv4.getVersion()); |
| 90 | assertEquals(headerLength, ipv4.getHeaderLength()); |
| 91 | assertEquals(diffServ, ipv4.getDiffServ()); |
| 92 | assertEquals(totalLength, ipv4.getTotalLength()); |
| 93 | assertEquals(identification, ipv4.getIdentification()); |
| 94 | assertEquals(flags, ipv4.getFlags()); |
| 95 | assertEquals(fragmentOffset, ipv4.getFragmentOffset()); |
| 96 | assertEquals(ttl, ipv4.getTtl()); |
| 97 | assertEquals(protocol, ipv4.getProtocol()); |
| 98 | assertEquals(checksum, ipv4.getChecksum()); |
| 99 | assertEquals(sourceAddress, ipv4.getSourceAddress()); |
| 100 | assertEquals(destinationAddress, ipv4.getDestinationAddress()); |
| 101 | assertTrue(ipv4.isTruncated()); |
| 102 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Tests toString. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testToStringIPv4() throws Exception { |
| 109 | IPv4 ipv4 = deserializer.deserialize(headerBytes, 0, headerBytes.length); |
| 110 | String str = ipv4.toString(); |
| 111 | |
| 112 | assertTrue(StringUtils.contains(str, "version=" + version)); |
| 113 | assertTrue(StringUtils.contains(str, "headerLength=" + headerLength)); |
| 114 | assertTrue(StringUtils.contains(str, "diffServ=" + diffServ)); |
| 115 | assertTrue(StringUtils.contains(str, "totalLength=" + totalLength)); |
| 116 | assertTrue(StringUtils.contains(str, "identification=" + identification)); |
| 117 | assertTrue(StringUtils.contains(str, "flags=" + flags)); |
| 118 | assertTrue(StringUtils.contains(str, "fragmentOffset=" + fragmentOffset)); |
| 119 | assertTrue(StringUtils.contains(str, "ttl=" + ttl)); |
| 120 | assertTrue(StringUtils.contains(str, "protocol=" + protocol)); |
| 121 | assertTrue(StringUtils.contains(str, "checksum=" + checksum)); |
| 122 | assertTrue(StringUtils.contains(str, "sourceAddress=" + sourceAddress)); |
| 123 | assertTrue(StringUtils.contains(str, "destinationAddress=" + destinationAddress)); |
| 124 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 125 | } |