1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.pcap
.core
.tests
.protocol
.pcap
;
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
;
21 import java
.io
.IOException
;
22 import java
.nio
.ByteBuffer
;
23 import java
.nio
.ByteOrder
;
24 import java
.text
.ParseException
;
25 import java
.text
.SimpleDateFormat
;
26 import java
.util
.Date
;
28 import java
.util
.TimeZone
;
30 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.BadPacketException
;
31 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.PcapProtocol
;
32 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.pcap
.PcapEndpoint
;
33 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.pcap
.PcapPacket
;
34 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.BadPcapFileException
;
35 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.PcapFile
;
36 import org
.eclipse
.tracecompass
.pcap
.core
.tests
.shared
.PcapTestTrace
;
37 import org
.junit
.Before
;
38 import org
.junit
.Test
;
40 import com
.google
.common
.collect
.ImmutableMap
;
43 * JUnit Class that tests the PcapPacket class and its method.
45 * @author Vincent Perot
47 public class PcapPacketTest
{
49 private static final String EXPECTED_DATE_FORMAT
= "yyyy-MM-dd HH:mm:ss.SSS";
50 private static final String EXPECTED_GMT_TIME
= "2005-07-04 09:33:52.829";
52 private static final Map
<String
, String
> EXPECTED_FIELDS
;
53 private static final String EXPECTED_TOSTRING
;
56 // Convert known GMT time to default (local) time zone. The local time
57 // is the expected value.
58 String captureTime
= "";
60 SimpleDateFormat gmtFormat
= new SimpleDateFormat(EXPECTED_DATE_FORMAT
);
61 gmtFormat
.setTimeZone(TimeZone
.getTimeZone("GMT"));
62 Date gmtDate
= gmtFormat
.parse(EXPECTED_GMT_TIME
);
64 SimpleDateFormat defaultFormat
= new SimpleDateFormat(EXPECTED_DATE_FORMAT
);
65 captureTime
= defaultFormat
.format(gmtDate
);
66 } catch (ParseException e
) {
67 fail("failed to parse date");
70 StringBuilder sb
= new StringBuilder();
71 sb
.append("Packet Capture 36: 75 bytes on wire, 75 bytes captured.\n");
72 sb
.append("Arrival time: " + captureTime
+ ".277.000\n");
73 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");
74 sb
.append("Internet Protocol Version 4, Source: 192.168.1.2, Destination: 192.168.1.1\n");
75 sb
.append("Version: 4, Identification: 0x69aa, Header Length: 20 bytes, Total Length: 61 bytes\n");
76 sb
.append("Differentiated Services Code Point: 0x00; Explicit Congestion Notification: 0x00\n");
77 sb
.append("Flags: 0x00 (Don't have more fragments), Fragment Offset: 0\n");
78 sb
.append("Time to live: 128\n");
79 sb
.append("Protocol: 17\n");
80 sb
.append("Header Checksum: 0x4db2\n");
81 sb
.append("User Datagram Protocol, Source Port: 2719, Destination Port: 53, Length: 41, Checksum: 19038\n");
82 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");
84 EXPECTED_TOSTRING
= sb
.toString();
85 EXPECTED_FIELDS
= ImmutableMap
.of(
87 "Frame Length", "75 bytes",
88 "Capture Length", "75 bytes",
89 "Capture Time", captureTime
+ ".277.000"
93 private ByteBuffer fPayload
;
96 * Initialize the payload.
99 public void initialize() {
100 fPayload
= ByteBuffer
.allocate(75);
101 fPayload
.order(ByteOrder
.BIG_ENDIAN
);
103 // Values copied from wireshark
106 fPayload
.put((byte) 0x00);
107 fPayload
.put((byte) 0x30);
108 fPayload
.put((byte) 0x54);
109 fPayload
.put((byte) 0x00);
110 fPayload
.put((byte) 0x34);
111 fPayload
.put((byte) 0x56);
112 fPayload
.put((byte) 0x00);
113 fPayload
.put((byte) 0xE0);
114 fPayload
.put((byte) 0xED);
115 fPayload
.put((byte) 0x01);
116 fPayload
.put((byte) 0x6E);
117 fPayload
.put((byte) 0xBD);
118 fPayload
.put((byte) 0x08);
119 fPayload
.put((byte) 0x00);
120 fPayload
.put((byte) 0x45);
121 fPayload
.put((byte) 0x00);
124 fPayload
.put((byte) 0x00);
125 fPayload
.put((byte) 0x3D);
126 fPayload
.put((byte) 0x69);
127 fPayload
.put((byte) 0xAA);
128 fPayload
.put((byte) 0x00);
129 fPayload
.put((byte) 0x00);
130 fPayload
.put((byte) 0x80);
131 fPayload
.put((byte) 0x11);
132 fPayload
.put((byte) 0x4D);
133 fPayload
.put((byte) 0xB2);
134 fPayload
.put((byte) 0xC0);
135 fPayload
.put((byte) 0xA8);
136 fPayload
.put((byte) 0x01);
137 fPayload
.put((byte) 0x02);
138 fPayload
.put((byte) 0xC0);
139 fPayload
.put((byte) 0xA8);
142 fPayload
.put((byte) 0x01);
143 fPayload
.put((byte) 0x01);
144 fPayload
.put((byte) 0x0A);
145 fPayload
.put((byte) 0x9F);
146 fPayload
.put((byte) 0x00);
147 fPayload
.put((byte) 0x35);
148 fPayload
.put((byte) 0x00);
149 fPayload
.put((byte) 0x29);
150 fPayload
.put((byte) 0x4A);
151 fPayload
.put((byte) 0x5E);
152 fPayload
.put((byte) 0xED);
153 fPayload
.put((byte) 0xd4);
154 fPayload
.put((byte) 0x01);
155 fPayload
.put((byte) 0x00);
156 fPayload
.put((byte) 0x00);
157 fPayload
.put((byte) 0x01);
160 fPayload
.put((byte) 0x00);
161 fPayload
.put((byte) 0x00);
162 fPayload
.put((byte) 0x00);
163 fPayload
.put((byte) 0x00);
164 fPayload
.put((byte) 0x00);
165 fPayload
.put((byte) 0x00);
166 fPayload
.put((byte) 0x03);
167 fPayload
.put((byte) 0x66);
168 fPayload
.put((byte) 0x74);
169 fPayload
.put((byte) 0x70);
170 fPayload
.put((byte) 0x07);
171 fPayload
.put((byte) 0x65);
172 fPayload
.put((byte) 0x63);
173 fPayload
.put((byte) 0x69);
174 fPayload
.put((byte) 0x74);
175 fPayload
.put((byte) 0x65);
178 fPayload
.put((byte) 0x6C);
179 fPayload
.put((byte) 0x65);
180 fPayload
.put((byte) 0x03);
181 fPayload
.put((byte) 0x63);
182 fPayload
.put((byte) 0x6F);
183 fPayload
.put((byte) 0x6D);
184 fPayload
.put((byte) 0x00);
185 fPayload
.put((byte) 0x00);
186 fPayload
.put((byte) 0x01);
187 fPayload
.put((byte) 0x00);
188 fPayload
.put((byte) 0x01);
194 * Test that verify the correctness of the PcapPacket's methods.
195 * @throws BadPcapFileException
196 * Thrown when the file is erroneous. Fails the test.
197 * @throws IOException
198 * Thrown when an IO error occurs. Fails the test.
199 * @throws BadPacketException
200 * Thrown when a packet is erroneous. Fails the test.
203 public void CompletePcapPacketTest() throws IOException
, BadPcapFileException
, BadPacketException
{
204 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_UDP
;
205 assumeTrue(trace
.exists());
206 try (PcapFile file
= new PcapFile(trace
.getPath());) {
209 PcapPacket packet
= file
.parseNextPacket();
210 if (packet
== null) {
211 fail("CompletePcapPacketTest has failed!");
215 assertEquals(PcapProtocol
.PCAP
, packet
.getProtocol());
216 assertTrue(packet
.hasProtocol(PcapProtocol
.PCAP
));
217 assertTrue(packet
.hasProtocol(PcapProtocol
.UNKNOWN
));
218 assertFalse(packet
.hasProtocol(PcapProtocol
.TCP
));
220 // Abstract methods Testing
221 assertTrue(packet
.validate());
222 assertEquals(86567859, packet
.hashCode());
223 assertFalse(packet
.equals(null));
224 assertFalse(packet
.equals(file
.parseNextPacket()));
226 assertEquals(EXPECTED_FIELDS
, packet
.getFields());
227 assertEquals(EXPECTED_TOSTRING
, packet
.toString());
228 assertEquals("Frame 36: 75 bytes on wire, 75 bytes captured", packet
.getLocalSummaryString());
229 assertEquals("Source Port: 2719, Destination Port: 53", packet
.getGlobalSummaryString());
231 assertEquals(new PcapEndpoint(packet
, true), packet
.getSourceEndpoint());
232 assertEquals(new PcapEndpoint(packet
, false), packet
.getDestinationEndpoint());
234 ByteBuffer payload
= packet
.getPayload();
235 if (payload
== null) {
236 fail("CompletePcapPacketTest has failed!");
239 assertEquals(fPayload
, payload
.flip());
241 // Packet-specific methods Testing
242 assertEquals(36, packet
.getIndex());
243 assertEquals(75, packet
.getOriginalLength());
244 assertEquals(75, packet
.getIncludedLength());
245 assertEquals(1120469632829277L, packet
.getTimestamp());
246 assertFalse(packet
.isTruncated());