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
.tmf
.pcap
.core
.tests
.event
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.fail
;
17 import static org
.junit
.Assume
.assumeTrue
;
19 import java
.io
.IOException
;
20 import java
.nio
.ByteBuffer
;
21 import java
.nio
.ByteOrder
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.BadPacketException
;
28 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.Packet
;
29 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.ipv4
.IPv4Packet
;
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
.internal
.tmf
.pcap
.core
.event
.PcapEventField
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.pcap
.core
.event
.PcapRootEventField
;
34 import org
.eclipse
.tracecompass
.pcap
.core
.tests
.shared
.PcapTestTrace
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
37 import org
.junit
.BeforeClass
;
38 import org
.junit
.Test
;
41 * JUnit that test the PcapEventField class.
43 * @author Vincent Perot
45 public class PcapEventFieldTest
{
47 private static final @NonNull String EMPTY_STRING
= "";
48 private static PcapEventField fRegularField
;
49 private static PcapRootEventField fRootField
;
52 * Initialize the Packet and the Event.
54 * @throws BadPcapFileException
55 * Thrown when the pcap file is erroneous.
57 * Thrown when an IO error occurs.
58 * @throws BadPacketException
59 * Thrown when the packet is erroneous.
62 public static void setUp() throws IOException
, BadPcapFileException
, BadPacketException
{
63 ByteBuffer bb
= ByteBuffer
.allocate(25);
64 bb
.order(ByteOrder
.BIG_ENDIAN
);
72 // Total length - this is randomly chosen so that we verify that the
73 // packet handles wrong total length.
81 // Flags + Fragment Offset
91 // Header checksum - chosen randomly
95 // Source IP - 4 bytes
101 // Destination IP - 4 bytes
118 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
119 assumeTrue(trace
.exists());
120 try (PcapFile dummy
= new PcapFile(trace
.getPath())) {
121 IPv4Packet packet
= new IPv4Packet(dummy
, null, bb
);
122 ITmfEventField
[] fieldArray
= generatePacketFields(packet
);
123 fRegularField
= new PcapEventField("Regular Field", EMPTY_STRING
, fieldArray
, packet
);
124 fRootField
= new PcapRootEventField(EMPTY_STRING
, fieldArray
, packet
);
130 * Method that tests the copy constructor.
133 public void copyConstructorTest() {
134 PcapEventField oldField
= fRegularField
;
135 if (oldField
== null) {
136 fail("The field has not been initialized!");
139 PcapEventField newField
= new PcapEventField(oldField
);
140 assertEquals(fRegularField
.hashCode(), newField
.hashCode());
141 assertEquals(fRegularField
, newField
);
145 * Method that tests a standard field value request.
148 public void regularFieldValueRequestTest() {
149 ITmfEventField field
= fRootField
.getField("Internet Protocol Version 4");
151 fail("The field is null!");
155 ITmfEventField subfield
= field
.getField("Source IP Address");
156 if (subfield
== null) {
157 fail("The subfield is null!");
161 String string
= subfield
.getValue().toString();
162 assertEquals("192.168.1.0", string
);
166 * Method that tests a custom field value request.
169 public void customFieldValueRequestTest() {
170 ITmfEventField field
= fRootField
.getField(":protocol:");
172 fail("The field is null!");
175 String string
= field
.getValue().toString();
176 assertEquals("IPV4", string
);
178 field
= fRootField
.getField(":packetsource:");
180 fail("The field is null!");
183 string
= field
.getValue().toString();
184 assertEquals("192.168.1.0", string
);
186 field
= fRootField
.getField(":packetdestination:");
188 fail("The field is null!");
191 string
= field
.getValue().toString();
192 assertEquals("193.169.2.1", string
);
197 * Method that teststhe toString() method for a non-root field.
200 public void regularToStringTest() {
201 assertEquals("Src: 192.168.1.0 , Dst: 193.169.2.1", fRegularField
.toString());
205 * Method that teststhe toString() method for a root field.
208 public void rootToStringTest() {
209 assertEquals("192.168.1.0 > 193.169.2.1 Id=4080 Len=1", fRootField
.toString());
212 // Convenience method
213 private static ITmfEventField
[] generatePacketFields(Packet packet
) {
214 List
<ITmfEventField
> fieldList
= new ArrayList
<>();
215 List
<ITmfEventField
> subfieldList
= new ArrayList
<>();
216 Packet localPacket
= packet
;
218 while (localPacket
!= null) {
219 subfieldList
.clear();
220 for (Map
.Entry
<String
, String
> entry
: localPacket
.getFields().entrySet()) {
222 @SuppressWarnings("null")
224 String key
= entry
.getKey();
226 @SuppressWarnings("null")
228 String value
= entry
.getValue();
229 subfieldList
.add(new TmfEventField(key
, value
, null));
231 ITmfEventField
[] subfieldArray
= subfieldList
.toArray(new ITmfEventField
[subfieldList
.size()]);
232 fieldList
.add(new PcapEventField(localPacket
.getProtocol().getName(), EMPTY_STRING
, subfieldArray
, localPacket
));
233 localPacket
= localPacket
.getChildPacket();
236 ITmfEventField
[] fieldArray
= fieldList
.toArray(new ITmfEventField
[fieldList
.size()]);
237 if (fieldArray
== null) {
238 return new ITmfEventField
[0];