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
.*;
16 import static org
.junit
.Assume
.assumeTrue
;
18 import java
.io
.IOException
;
19 import java
.nio
.ByteBuffer
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
24 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.BadPcapFileException
;
25 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.PcapFile
;
26 import org
.eclipse
.linuxtools
.pcap
.core
.tests
.shared
.PcapTestTrace
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.pcap
.core
.event
.PcapEvent
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.pcap
.core
.protocol
.TmfPcapProtocol
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.pcap
.core
.trace
.PcapTrace
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLongLocation
;
33 import org
.junit
.BeforeClass
;
34 import org
.junit
.Test
;
36 import com
.google
.common
.collect
.ImmutableList
;
39 * JUnit that test the PcapEvent class.
41 * @author Vincent Perot
43 public class PcapEventTest
{
45 private static PcapEvent fEvent
;
46 private static List
<TmfPcapProtocol
> fProtocolList
;
49 * Initialize the Packet and the EventField.
51 * @throws BadPcapFileException
52 * Thrown when the pcap file is erroneous.
54 * Thrown when an IO error occurs.
55 * @throws TmfTraceException
56 * Thrown when the trace is not valid.
59 public static void setUp() throws IOException
, BadPcapFileException
, TmfTraceException
{
61 PcapTestTrace trace
= PcapTestTrace
.MOSTLY_TCP
;
62 assumeTrue(trace
.exists());
63 try (PcapFile pcap
= new PcapFile(trace
.getPath());
64 PcapTrace pcapTrace
= new PcapTrace();) {
65 pcapTrace
.initTrace(null, trace
.getPath().toString(), PcapEvent
.class);
66 fEvent
= pcapTrace
.parseEvent(new TmfContext(new TmfLongLocation(3), 3));
69 // Initialize protocol list.
70 List
<TmfPcapProtocol
> list
= new ArrayList
<>();
71 list
.add(TmfPcapProtocol
.PCAP
);
72 list
.add(TmfPcapProtocol
.ETHERNET_II
);
73 list
.add(TmfPcapProtocol
.IPV4
);
74 list
.add(TmfPcapProtocol
.TCP
);
75 list
.add(TmfPcapProtocol
.UNKNOWN
);
76 fProtocolList
= ImmutableList
.copyOf(list
);
80 * Method that tests getProtocols of PcapEvent.
83 public void getProtocolsTest() {
84 assertEquals(fProtocolList
, fEvent
.getProtocols());
88 * Method that tests getMostEncapsulatedProtocol of PcapEvent.
91 public void getMostEncapsulatedProtocolTest() {
92 assertEquals(TmfPcapProtocol
.TCP
, fEvent
.getMostEncapsulatedProtocol());
96 * Method that tests getFields of PcapEvent.
99 public void getFieldsTest() {
100 Map
<String
, String
> map
= fEvent
.getFields(TmfPcapProtocol
.IPV4
);
102 fail("getFieldsTest() failed because map is null!");
105 assertEquals("145.254.160.237", map
.get("Source IP Address"));
109 * Method that tests getPayload of PcapEvent.
112 public void getPayloadTest() {
113 ByteBuffer bb
= fEvent
.getPayload(TmfPcapProtocol
.TCP
);
115 fail("getPayloadTest() failed because bb is null!");
118 assertEquals((byte) 0x47, bb
.get());
122 * Method that tests getSourceEndpoint of PcapEvent.
125 public void getSourceEndpointTest() {
126 assertEquals("00:00:01:00:00:00/145.254.160.237/3372", fEvent
.getSourceEndpoint(TmfPcapProtocol
.TCP
));
130 * Method that tests getDestinationEndpointTest of PcapEvent.
133 public void getDestinationEndpointTest() {
134 assertEquals("fe:ff:20:00:01:00", fEvent
.getDestinationEndpoint(TmfPcapProtocol
.ETHERNET_II
));
138 * Method that tests toString() of PcapEvent.
141 public void toStringTest() {
142 assertEquals("3372 > 80 [ACK, PSH] Seq=951057940 Ack=290218380 Len=20", fEvent
.toString());
146 * Method that tests toString(protocol) of PcapEvent.
149 public void toStringAtSpecificProtocolTest() {
150 assertEquals("Src: 145.254.160.237 , Dst: 65.208.228.223", fEvent
.toString(TmfPcapProtocol
.IPV4
));
This page took 0.03546 seconds and 6 git commands to generate.