pcap: Remove AutoCloseable from PcapTrace
[deliverable/tracecompass.git] / pcap / org.eclipse.tracecompass.tmf.pcap.core.tests / src / org / eclipse / tracecompass / tmf / pcap / core / tests / event / PcapEventTest.java
CommitLineData
527c3a79
VP
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
2bdf0193 13package org.eclipse.tracecompass.tmf.pcap.core.tests.event;
527c3a79
VP
14
15import static org.junit.Assert.*;
16import static org.junit.Assume.assumeTrue;
17
18import java.io.IOException;
19import java.nio.ByteBuffer;
20import java.util.ArrayList;
21import java.util.List;
22import java.util.Map;
23
71f2817f
AM
24import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException;
25import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
2bdf0193
AM
26import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent;
27import org.eclipse.tracecompass.internal.tmf.pcap.core.protocol.TmfPcapProtocol;
28import org.eclipse.tracecompass.internal.tmf.pcap.core.trace.PcapTrace;
71f2817f 29import org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace;
2bdf0193
AM
30import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
31import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
32import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
527c3a79
VP
33import org.junit.BeforeClass;
34import org.junit.Test;
35
36import com.google.common.collect.ImmutableList;
37
38/**
39 * JUnit that test the PcapEvent class.
40 *
41 * @author Vincent Perot
42 */
43public class PcapEventTest {
44
45 private static PcapEvent fEvent;
c88feda9 46 private static List<TmfPcapProtocol> fProtocolList;
527c3a79
VP
47
48 /**
49 * Initialize the Packet and the EventField.
50 *
51 * @throws BadPcapFileException
52 * Thrown when the pcap file is erroneous.
53 * @throws IOException
54 * Thrown when an IO error occurs.
55 * @throws TmfTraceException
56 * Thrown when the trace is not valid.
57 */
58 @BeforeClass
59 public static void setUp() throws IOException, BadPcapFileException, TmfTraceException {
60
61 PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP;
62 assumeTrue(trace.exists());
61cf93ec
AM
63 try (PcapFile pcap = new PcapFile(trace.getPath());) {
64 PcapTrace pcapTrace = new PcapTrace();
333a2acb 65 pcapTrace.initTrace(null, trace.getPath().toString(), PcapEvent.class);
527c3a79 66 fEvent = pcapTrace.parseEvent(new TmfContext(new TmfLongLocation(3), 3));
61cf93ec 67 pcapTrace.dispose();
527c3a79
VP
68 }
69
70 // Initialize protocol list.
c88feda9
AM
71 List<TmfPcapProtocol> list = new ArrayList<>();
72 list.add(TmfPcapProtocol.PCAP);
73 list.add(TmfPcapProtocol.ETHERNET_II);
74 list.add(TmfPcapProtocol.IPV4);
75 list.add(TmfPcapProtocol.TCP);
76 list.add(TmfPcapProtocol.UNKNOWN);
527c3a79
VP
77 fProtocolList = ImmutableList.copyOf(list);
78 }
79
80 /**
81 * Method that tests getProtocols of PcapEvent.
82 */
83 @Test
84 public void getProtocolsTest() {
85 assertEquals(fProtocolList, fEvent.getProtocols());
86 }
87
88 /**
89 * Method that tests getMostEncapsulatedProtocol of PcapEvent.
90 */
91 @Test
92 public void getMostEncapsulatedProtocolTest() {
c88feda9 93 assertEquals(TmfPcapProtocol.TCP, fEvent.getMostEncapsulatedProtocol());
527c3a79
VP
94 }
95
96 /**
97 * Method that tests getFields of PcapEvent.
98 */
99 @Test
100 public void getFieldsTest() {
c88feda9 101 Map<String, String> map = fEvent.getFields(TmfPcapProtocol.IPV4);
527c3a79
VP
102 if (map == null) {
103 fail("getFieldsTest() failed because map is null!");
104 return;
105 }
106 assertEquals("145.254.160.237", map.get("Source IP Address"));
107 }
108
109 /**
110 * Method that tests getPayload of PcapEvent.
111 */
112 @Test
113 public void getPayloadTest() {
c88feda9 114 ByteBuffer bb = fEvent.getPayload(TmfPcapProtocol.TCP);
527c3a79
VP
115 if (bb == null) {
116 fail("getPayloadTest() failed because bb is null!");
117 return;
118 }
119 assertEquals((byte) 0x47, bb.get());
120 }
121
122 /**
123 * Method that tests getSourceEndpoint of PcapEvent.
124 */
125 @Test
126 public void getSourceEndpointTest() {
c88feda9 127 assertEquals("00:00:01:00:00:00/145.254.160.237/3372", fEvent.getSourceEndpoint(TmfPcapProtocol.TCP));
527c3a79
VP
128 }
129
130 /**
131 * Method that tests getDestinationEndpointTest of PcapEvent.
132 */
133 @Test
134 public void getDestinationEndpointTest() {
c88feda9 135 assertEquals("fe:ff:20:00:01:00", fEvent.getDestinationEndpoint(TmfPcapProtocol.ETHERNET_II));
527c3a79
VP
136 }
137
138 /**
139 * Method that tests toString() of PcapEvent.
140 */
141 @Test
142 public void toStringTest() {
143 assertEquals("3372 > 80 [ACK, PSH] Seq=951057940 Ack=290218380 Len=20", fEvent.toString());
144 }
145
146 /**
147 * Method that tests toString(protocol) of PcapEvent.
148 */
149 @Test
150 public void toStringAtSpecificProtocolTest() {
c88feda9 151 assertEquals("Src: 145.254.160.237 , Dst: 65.208.228.223", fEvent.toString(TmfPcapProtocol.IPV4));
527c3a79
VP
152 }
153
154}
This page took 0.082494 seconds and 5 git commands to generate.