pcap: Add unit tests to pcap.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core.tests / src / org / eclipse / linuxtools / pcap / core / tests / protocol / pcap / PcapPacketTest.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.pcap;
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.Map;
25
26 import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
27 import org.eclipse.linuxtools.pcap.core.protocol.Protocol;
28 import org.eclipse.linuxtools.pcap.core.protocol.pcap.PcapEndpoint;
29 import org.eclipse.linuxtools.pcap.core.protocol.pcap.PcapPacket;
30 import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
31 import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
32 import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 import com.google.common.collect.ImmutableMap;
37
38 /**
39 * JUnit Class that tests the PcapPacket class and its method.
40 *
41 * @author Vincent Perot
42 */
43 public class PcapPacketTest {
44
45 private static final Map<String, String> EXPECTED_FIELDS = ImmutableMap.of(
46 "Frame", "36",
47 "Frame Length", "75 bytes",
48 "Capture Length", "75 bytes",
49 "Capture Time", "2005-07-04 05:33:52.829.277.000"
50 );
51
52 private static final String EXPECTED_TOSTRING;
53 static {
54 StringBuilder sb = new StringBuilder();
55 sb.append("Packet Capture 36: 75 bytes on wire, 75 bytes captured.\n");
56 sb.append("Arrival time: 2005-07-04 05:33:52.829.277.000\n");
57 sb.append("Ethernet II, Source: 00:e0:ed:01:6e:bd, Destination: 00:30:54:00:34:56, Type: Internet Protocol Version 4 (0x0800)\n");
58 sb.append("Internet Protocol Version 4, Source: 192.168.1.2, Destination: 192.168.1.1\n");
59 sb.append("Version: 4, Identification: 0x69aa, Header Length: 20 bytes, Total Length: 61 bytes\n");
60 sb.append("Differentiated Services Code Point: 0x00; Explicit Congestion Notification: 0x00\n");
61 sb.append("Flags: 0x00 (Don't have more fragments), Fragment Offset: 0\n");
62 sb.append("Time to live: 128\n");
63 sb.append("Protocol: 17\n");
64 sb.append("Header Checksum: 0x4db2\n");
65 sb.append("User Datagram Protocol, Source Port: 2719, Destination Port: 53, Length: 41, Checksum: 19038\n");
66 sb.append("Payload: ed d4 01 00 00 01 00 00 00 00 00 00 03 66 74 70 07 65 63 69 74 65 6c 65 03 63 6f 6d 00 00 01 00 01");
67
68 EXPECTED_TOSTRING = sb.toString();
69 }
70
71 private ByteBuffer fPayload;
72
73 /**
74 * Initialize the payload.
75 */
76 @Before
77 public void initialize() {
78 fPayload = ByteBuffer.allocate(75);
79 fPayload.order(ByteOrder.BIG_ENDIAN);
80
81 // Values copied from wireshark
82
83 // Bytes 0x01-0x10
84 fPayload.put((byte) 0x00);
85 fPayload.put((byte) 0x30);
86 fPayload.put((byte) 0x54);
87 fPayload.put((byte) 0x00);
88 fPayload.put((byte) 0x34);
89 fPayload.put((byte) 0x56);
90 fPayload.put((byte) 0x00);
91 fPayload.put((byte) 0xE0);
92 fPayload.put((byte) 0xED);
93 fPayload.put((byte) 0x01);
94 fPayload.put((byte) 0x6E);
95 fPayload.put((byte) 0xBD);
96 fPayload.put((byte) 0x08);
97 fPayload.put((byte) 0x00);
98 fPayload.put((byte) 0x45);
99 fPayload.put((byte) 0x00);
100
101 // Bytes 0x11-0x20
102 fPayload.put((byte) 0x00);
103 fPayload.put((byte) 0x3D);
104 fPayload.put((byte) 0x69);
105 fPayload.put((byte) 0xAA);
106 fPayload.put((byte) 0x00);
107 fPayload.put((byte) 0x00);
108 fPayload.put((byte) 0x80);
109 fPayload.put((byte) 0x11);
110 fPayload.put((byte) 0x4D);
111 fPayload.put((byte) 0xB2);
112 fPayload.put((byte) 0xC0);
113 fPayload.put((byte) 0xA8);
114 fPayload.put((byte) 0x01);
115 fPayload.put((byte) 0x02);
116 fPayload.put((byte) 0xC0);
117 fPayload.put((byte) 0xA8);
118
119 // Bytes 0x21-0x30
120 fPayload.put((byte) 0x01);
121 fPayload.put((byte) 0x01);
122 fPayload.put((byte) 0x0A);
123 fPayload.put((byte) 0x9F);
124 fPayload.put((byte) 0x00);
125 fPayload.put((byte) 0x35);
126 fPayload.put((byte) 0x00);
127 fPayload.put((byte) 0x29);
128 fPayload.put((byte) 0x4A);
129 fPayload.put((byte) 0x5E);
130 fPayload.put((byte) 0xED);
131 fPayload.put((byte) 0xd4);
132 fPayload.put((byte) 0x01);
133 fPayload.put((byte) 0x00);
134 fPayload.put((byte) 0x00);
135 fPayload.put((byte) 0x01);
136
137 // Bytes 0x31-0x40
138 fPayload.put((byte) 0x00);
139 fPayload.put((byte) 0x00);
140 fPayload.put((byte) 0x00);
141 fPayload.put((byte) 0x00);
142 fPayload.put((byte) 0x00);
143 fPayload.put((byte) 0x00);
144 fPayload.put((byte) 0x03);
145 fPayload.put((byte) 0x66);
146 fPayload.put((byte) 0x74);
147 fPayload.put((byte) 0x70);
148 fPayload.put((byte) 0x07);
149 fPayload.put((byte) 0x65);
150 fPayload.put((byte) 0x63);
151 fPayload.put((byte) 0x69);
152 fPayload.put((byte) 0x74);
153 fPayload.put((byte) 0x65);
154
155 // Bytes 0x41-0x4B
156 fPayload.put((byte) 0x6C);
157 fPayload.put((byte) 0x65);
158 fPayload.put((byte) 0x03);
159 fPayload.put((byte) 0x63);
160 fPayload.put((byte) 0x6F);
161 fPayload.put((byte) 0x6D);
162 fPayload.put((byte) 0x00);
163 fPayload.put((byte) 0x00);
164 fPayload.put((byte) 0x01);
165 fPayload.put((byte) 0x00);
166 fPayload.put((byte) 0x01);
167
168 fPayload.flip();
169 }
170
171 /**
172 * Test that verify the correctness of the PcapPacket's methods.
173 * @throws BadPcapFileException
174 * Thrown when the file is erroneous. Fails the test.
175 * @throws IOException
176 * Thrown when an IO error occurs. Fails the test.
177 * @throws BadPacketException
178 * Thrown when a packet is erroneous. Fails the test.
179 */
180 @Test
181 public void CompletePcapPacketTest() throws IOException, BadPcapFileException, BadPacketException {
182 PcapTestTrace trace = PcapTestTrace.MOSTLY_UDP;
183 assumeTrue(trace.exists());
184 String path = trace.getPath();
185 try (PcapFile file = new PcapFile(path);) {
186
187 file.seekPacket(36);
188 PcapPacket packet = file.parseNextPacket();
189 if (packet == null) {
190 fail("CompletePcapPacketTest has failed!");
191 return;
192 }
193 // Protocol Testing
194 assertEquals(Protocol.PCAP, packet.getProtocol());
195 assertTrue(packet.hasProtocol(Protocol.PCAP));
196 assertTrue(packet.hasProtocol(Protocol.UNKNOWN));
197 assertFalse(packet.hasProtocol(Protocol.TCP));
198
199 // Abstract methods Testing
200 assertTrue(packet.validate());
201 assertEquals(842182851, packet.hashCode());
202 assertFalse(packet.equals(null));
203 assertFalse(packet.equals(file.parseNextPacket()));
204
205 assertEquals(EXPECTED_FIELDS, packet.getFields());
206 assertEquals(EXPECTED_TOSTRING, packet.toString());
207 assertEquals("Frame 36: 75 bytes on wire, 75 bytes captured", packet.getLocalSummaryString());
208 assertEquals("Source Port: 2719, Destination Port: 53", packet.getGlobalSummaryString());
209
210 assertEquals(new PcapEndpoint(packet, true), packet.getSourceEndpoint());
211 assertEquals(new PcapEndpoint(packet, false), packet.getDestinationEndpoint());
212
213 ByteBuffer payload = packet.getPayload();
214 if (payload == null) {
215 fail("CompletePcapPacketTest has failed!");
216 return;
217 }
218 assertEquals(fPayload, payload.flip());
219
220 // Packet-specific methods Testing
221 assertEquals(36, packet.getIndex());
222 assertEquals(75, packet.getOriginalLength());
223 assertEquals(75, packet.getIncludedLength());
224 assertEquals(1120469632829277L, packet.getTimestamp());
225 assertFalse(packet.isTruncated());
226 }
227 }
228 }
This page took 0.043347 seconds and 5 git commands to generate.