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
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
13 package org.eclipse.tracecompass.tmf.pcap.core.tests.event;
14
15 import static org.junit.Assert.*;
16 import static org.junit.Assume.assumeTrue;
17
18 import java.io.IOException;
19 import java.nio.ByteBuffer;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException;
25 import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
26 import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent;
27 import org.eclipse.tracecompass.internal.tmf.pcap.core.protocol.TmfPcapProtocol;
28 import org.eclipse.tracecompass.internal.tmf.pcap.core.trace.PcapTrace;
29 import org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace;
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;
35
36 import com.google.common.collect.ImmutableList;
37
38 /**
39 * JUnit that test the PcapEvent class.
40 *
41 * @author Vincent Perot
42 */
43 public class PcapEventTest {
44
45 private static PcapEvent fEvent;
46 private static List<TmfPcapProtocol> fProtocolList;
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());
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));
67 pcapTrace.dispose();
68 }
69
70 // Initialize protocol list.
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);
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() {
93 assertEquals(TmfPcapProtocol.TCP, fEvent.getMostEncapsulatedProtocol());
94 }
95
96 /**
97 * Method that tests getFields of PcapEvent.
98 */
99 @Test
100 public void getFieldsTest() {
101 Map<String, String> map = fEvent.getFields(TmfPcapProtocol.IPV4);
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() {
114 ByteBuffer bb = fEvent.getPayload(TmfPcapProtocol.TCP);
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() {
127 assertEquals("00:00:01:00:00:00/145.254.160.237/3372", fEvent.getSourceEndpoint(TmfPcapProtocol.TCP));
128 }
129
130 /**
131 * Method that tests getDestinationEndpointTest of PcapEvent.
132 */
133 @Test
134 public void getDestinationEndpointTest() {
135 assertEquals("fe:ff:20:00:01:00", fEvent.getDestinationEndpoint(TmfPcapProtocol.ETHERNET_II));
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() {
151 assertEquals("Src: 145.254.160.237 , Dst: 65.208.228.223", fEvent.toString(TmfPcapProtocol.IPV4));
152 }
153
154 }
This page took 0.035823 seconds and 5 git commands to generate.