blob: 84127018fab1c83dd61db0c0b8d85186642989b2 [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
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
17package org.onlab.packet;
18
Jian Li5fc14292015-12-04 11:30:46 -080019import org.apache.commons.lang3.StringUtils;
Jonathan Hart2a655752015-04-07 16:46:33 -070020import org.junit.Before;
21import org.junit.Test;
22
23import java.nio.ByteBuffer;
24
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertTrue;
27
28/**
29 * Unit tests for IPv4 class.
30 */
31public 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 Li5fc14292015-12-04 11:30:46 -080082 /**
83 * Tests deserialize and getters.
84 */
Jonathan Hart2a655752015-04-07 16:46:33 -070085 @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 Li5fc14292015-12-04 11:30:46 -0800103
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 Hart2a655752015-04-07 16:46:33 -0700125}