pcap: Add unit tests to pcap.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core.tests / src / org / eclipse / linuxtools / pcap / core / tests / protocol / ipv4 / IPv4PacketTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.pcap.core.tests.protocol.ipv4;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19 import static org.junit.Assume.assumeTrue;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder;
24 import java.util.Arrays;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27
28 import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
29 import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
30 import org.eclipse.linuxtools.pcap.core.protocol.ipv4.IPv4Endpoint;
31 import org.eclipse.linuxtools.pcap.core.protocol.ipv4.IPv4Packet;
32 import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
33 import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
34 import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 /**
39 * JUnit Class that tests the IPv4Packet class and its method.
40 *
41 * @author Vincent Perot
42 */
43 public class IPv4PacketTest {
44
45
46 private static final Map<String, String> EXPECTED_FIELDS;
47 static {
48 EXPECTED_FIELDS = new LinkedHashMap<>();
49 EXPECTED_FIELDS.put("Version", "4");
50 EXPECTED_FIELDS.put("Header Length", "24 bytes");
51 EXPECTED_FIELDS.put("Differentiated Services Field", "0x26");
52 EXPECTED_FIELDS.put("Explicit Congestion Notification", "0x02");
53 EXPECTED_FIELDS.put("Total Length", "255 bytes");
54 EXPECTED_FIELDS.put("Identification", "0x0ff0");
55 EXPECTED_FIELDS.put("Don't Fragment Flag", "false");
56 EXPECTED_FIELDS.put("More Fragment Flag", "false");
57 EXPECTED_FIELDS.put("Fragment Offset", "-31");
58 EXPECTED_FIELDS.put("Time to live", "160");
59 EXPECTED_FIELDS.put("Protocol", "Unknown (254)");
60 EXPECTED_FIELDS.put("Checksum", "0x3344");
61 EXPECTED_FIELDS.put("Source IP Address", "192.168.1.0");
62 EXPECTED_FIELDS.put("Destination IP Address", "193.169.2.1");
63 EXPECTED_FIELDS.put("Options", "a2 56 a2 56");
64 }
65
66 private static final String EXPECTED_TOSTRING;
67 static {
68 StringBuilder sb = new StringBuilder();
69 sb.append("Internet Protocol Version 4, Source: 192.168.1.0, Destination: 193.169.2.1\n");
70 sb.append("Version: 4, Identification: 0x0ff0, Header Length: 24 bytes, Total Length: 255 bytes\n");
71 sb.append("Differentiated Services Code Point: 0x26; Explicit Congestion Notification: 0x02\n");
72 sb.append("Flags: 0x00 (Don't have more fragments), Fragment Offset: -31\n");
73 sb.append("Time to live: 160\n");
74 sb.append("Protocol: 254\n");
75 sb.append("Header Checksum: 0x3344\n");
76 sb.append("Payload: a6");
77
78 EXPECTED_TOSTRING = sb.toString();
79 }
80
81 private ByteBuffer fPacket;
82
83 /**
84 * Initialize the packet.
85 */
86 @Before
87 public void initialize() {
88 fPacket = ByteBuffer.allocate(25);
89 fPacket.order(ByteOrder.BIG_ENDIAN);
90
91 // Version + IHL
92 fPacket.put((byte) 0x46);
93
94 // DSCP + ECN
95 fPacket.put((byte) 0x9A);
96
97 // Total length - this is randomly chosen so that we verify that the
98 // packet handles wrong total length.
99 fPacket.put((byte) 0x00);
100 fPacket.put((byte) 0xFF);
101
102 // Identification
103 fPacket.put((byte) 0x0F);
104 fPacket.put((byte) 0xF0);
105
106 // Flags + Fragment Offset
107 fPacket.put((byte) 0x1E);
108 fPacket.put((byte) 0xE1);
109
110 // Time to live
111 fPacket.put((byte) 0xA0);
112
113 // Protocol - Unknown
114 fPacket.put((byte) 0xFE);
115
116 // Header checksum - chosen randomly
117 fPacket.put((byte) 0x33);
118 fPacket.put((byte) 0x44);
119
120 // Source IP - 4 bytes
121 fPacket.put((byte) 192);
122 fPacket.put((byte) 168);
123 fPacket.put((byte) 1);
124 fPacket.put((byte) 0);
125
126 // Destination IP - 4 bytes
127 fPacket.put((byte) 193);
128 fPacket.put((byte) 169);
129 fPacket.put((byte) 2);
130 fPacket.put((byte) 1);
131
132 // Options - 4 bytes
133 fPacket.put((byte) 0xA2);
134 fPacket.put((byte) 0x56);
135 fPacket.put((byte) 0xA2);
136 fPacket.put((byte) 0x56);
137
138 // Payload - 1 byte
139 fPacket.put((byte) 0xA6);
140
141 fPacket.flip();
142 }
143
144 /**
145 * Test that verify the correctness of the IPv4Packet's methods.
146 * @throws BadPcapFileException
147 * Thrown when the file is erroneous. Fails the test.
148 * @throws IOException
149 * Thrown when an IO error occurs. Fails the test.
150 * @throws BadPacketException
151 * Thrown when a packet is erroneous. Fails the test.
152 */
153 @Test
154 public void CompleteIPv4PacketTest() throws IOException, BadPcapFileException, BadPacketException {
155 PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
156 assumeTrue(trace.exists());
157 String file = trace.getPath();
158 try (PcapFile dummy = new PcapFile(file)) {
159 ByteBuffer byteBuffer = fPacket;
160 if (byteBuffer == null) {
161 fail("CompleteIPv4PacketTest has failed!");
162 return;
163 }
164 IPv4Packet packet = new IPv4Packet(dummy, null, byteBuffer);
165
166 // Protocol Testing
167 assertEquals(Protocol.IPV4, packet.getProtocol());
168 assertTrue(packet.hasProtocol(Protocol.IPV4));
169 assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
170 assertFalse(packet.hasProtocol(Protocol.TCP));
171
172 // Abstract methods Testing
173 assertTrue(packet.validate());
174 assertEquals(1910842322, packet.hashCode());
175 assertFalse(packet.equals(null));
176 assertEquals(new IPv4Packet(dummy, null, byteBuffer), packet);
177
178 assertEquals(EXPECTED_FIELDS, packet.getFields());
179 assertEquals(EXPECTED_TOSTRING, packet.toString());
180 assertEquals("Src: 192.168.1.0 , Dst: 193.169.2.1", packet.getLocalSummaryString());
181 assertEquals("192.168.1.0 > 193.169.2.1 Id=4080 Len=1", packet.getGlobalSummaryString());
182
183 assertEquals(new IPv4Endpoint(packet, true), packet.getSourceEndpoint());
184 assertEquals(new IPv4Endpoint(packet, false), packet.getDestinationEndpoint());
185
186 fPacket.position(24);
187 byte[] payload = new byte[1];
188 fPacket.get(payload);
189 assertEquals(ByteBuffer.wrap(payload), packet.getPayload());
190
191 // Packet-specific methods Testing
192 assertTrue(Arrays.equals(packet.getSourceIpAddress(), Arrays.copyOfRange(fPacket.array(), 12, 16)));
193 assertTrue(Arrays.equals(packet.getDestinationIpAddress(), Arrays.copyOfRange(fPacket.array(), 16, 20)));
194 assertTrue(Arrays.equals(packet.getOptions(), Arrays.copyOfRange(fPacket.array(), 20, 24)));
195 assertEquals(4, packet.getVersion());
196 assertEquals(24, packet.getHeaderLength());
197 assertEquals(0x26, packet.getDSCP());
198 assertEquals(0x02, packet.getExplicitCongestionNotification());
199 assertEquals(255, packet.getTotalLength());
200 assertEquals(0x0FF0, packet.getIdentification());
201 assertFalse(packet.getReservedFlag());
202 assertFalse(packet.getDontFragmentFlag());
203 assertFalse(packet.getHasMoreFragment());
204 assertEquals(-31, packet.getFragmentOffset());
205 assertEquals(160, packet.getTimeToLive());
206 assertEquals(0xFE, packet.getIpDatagramProtocol());
207 assertEquals(0x3344, packet.getHeaderChecksum());
208
209 }
210 }
211 }
This page took 0.038954 seconds and 5 git commands to generate.