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
;
26 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.BadPacketException
;
27 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.PcapProtocol
;
28 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.pcap
.PcapEndpoint
;
29 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.pcap
.PcapPacket
;
30 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.BadPcapFileException
;
31 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.PcapFile
;
32 import org
.eclipse
.tracecompass
.pcap
.core
.tests
.shared
.PcapTestTrace
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
36 import com
.google
.common
.collect
.ImmutableMap
;
39 * JUnit Class that tests the PcapPacket class and its method.
41 * @author Vincent Perot
43 public class PcapPacketTest
{
45 private static final Map
<String
, String
> EXPECTED_FIELDS
= ImmutableMap
.of(
47 "Frame Length", "75 bytes",
48 "Capture Length", "75 bytes",
49 "Capture Time", "2005-07-04 05:33:52.829.277.000"
52 private static final String EXPECTED_TOSTRING
;
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");
68 EXPECTED_TOSTRING
= sb
.toString();
71 private ByteBuffer fPayload
;
74 * Initialize the payload.
77 public void initialize() {
78 fPayload
= ByteBuffer
.allocate(75);
79 fPayload
.order(ByteOrder
.BIG_ENDIAN
);
81 // Values copied from wireshark
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);
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);
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);
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);
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);
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.
181 public void CompletePcapPacketTest() throws IOException
, BadPcapFileException
, BadPacketException
{
182 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_UDP
;
183 assumeTrue(trace
.exists());
184 try (PcapFile file
= new PcapFile(trace
.getPath());) {
187 PcapPacket packet
= file
.parseNextPacket();
188 if (packet
== null) {
189 fail("CompletePcapPacketTest has failed!");
193 assertEquals(PcapProtocol
.PCAP
, packet
.getProtocol());
194 assertTrue(packet
.hasProtocol(PcapProtocol
.PCAP
));
195 assertTrue(packet
.hasProtocol(PcapProtocol
.UNKNOWN
));
196 assertFalse(packet
.hasProtocol(PcapProtocol
.TCP
));
198 // Abstract methods Testing
199 assertTrue(packet
.validate());
200 assertEquals(86567859, packet
.hashCode());
201 assertFalse(packet
.equals(null));
202 assertFalse(packet
.equals(file
.parseNextPacket()));
204 assertEquals(EXPECTED_FIELDS
, packet
.getFields());
205 assertEquals(EXPECTED_TOSTRING
, packet
.toString());
206 assertEquals("Frame 36: 75 bytes on wire, 75 bytes captured", packet
.getLocalSummaryString());
207 assertEquals("Source Port: 2719, Destination Port: 53", packet
.getGlobalSummaryString());
209 assertEquals(new PcapEndpoint(packet
, true), packet
.getSourceEndpoint());
210 assertEquals(new PcapEndpoint(packet
, false), packet
.getDestinationEndpoint());
212 ByteBuffer payload
= packet
.getPayload();
213 if (payload
== null) {
214 fail("CompletePcapPacketTest has failed!");
217 assertEquals(fPayload
, payload
.flip());
219 // Packet-specific methods Testing
220 assertEquals(36, packet
.getIndex());
221 assertEquals(75, packet
.getOriginalLength());
222 assertEquals(75, packet
.getIncludedLength());
223 assertEquals(1120469632829277L, packet
.getTimestamp());
224 assertFalse(packet
.isTruncated());
This page took 0.036938 seconds and 6 git commands to generate.