Commit | Line | Data |
---|---|---|
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 | 13 | package org.eclipse.tracecompass.tmf.pcap.core.tests.event; |
527c3a79 VP |
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 | ||
93d1d135 AM |
24 | import org.eclipse.linuxtools.internal.pcap.core.trace.BadPcapFileException; |
25 | import org.eclipse.linuxtools.internal.pcap.core.trace.PcapFile; | |
527c3a79 | 26 | import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace; |
2bdf0193 AM |
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; | |
527c3a79 VP |
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; | |
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()); | |
333a2acb | 63 | try (PcapFile pcap = new PcapFile(trace.getPath()); |
527c3a79 | 64 | PcapTrace pcapTrace = new PcapTrace();) { |
333a2acb | 65 | pcapTrace.initTrace(null, trace.getPath().toString(), PcapEvent.class); |
527c3a79 VP |
66 | fEvent = pcapTrace.parseEvent(new TmfContext(new TmfLongLocation(3), 3)); |
67 | } | |
68 | ||
69 | // Initialize protocol list. | |
c88feda9 AM |
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); | |
527c3a79 VP |
76 | fProtocolList = ImmutableList.copyOf(list); |
77 | } | |
78 | ||
79 | /** | |
80 | * Method that tests getProtocols of PcapEvent. | |
81 | */ | |
82 | @Test | |
83 | public void getProtocolsTest() { | |
84 | assertEquals(fProtocolList, fEvent.getProtocols()); | |
85 | } | |
86 | ||
87 | /** | |
88 | * Method that tests getMostEncapsulatedProtocol of PcapEvent. | |
89 | */ | |
90 | @Test | |
91 | public void getMostEncapsulatedProtocolTest() { | |
c88feda9 | 92 | assertEquals(TmfPcapProtocol.TCP, fEvent.getMostEncapsulatedProtocol()); |
527c3a79 VP |
93 | } |
94 | ||
95 | /** | |
96 | * Method that tests getFields of PcapEvent. | |
97 | */ | |
98 | @Test | |
99 | public void getFieldsTest() { | |
c88feda9 | 100 | Map<String, String> map = fEvent.getFields(TmfPcapProtocol.IPV4); |
527c3a79 VP |
101 | if (map == null) { |
102 | fail("getFieldsTest() failed because map is null!"); | |
103 | return; | |
104 | } | |
105 | assertEquals("145.254.160.237", map.get("Source IP Address")); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Method that tests getPayload of PcapEvent. | |
110 | */ | |
111 | @Test | |
112 | public void getPayloadTest() { | |
c88feda9 | 113 | ByteBuffer bb = fEvent.getPayload(TmfPcapProtocol.TCP); |
527c3a79 VP |
114 | if (bb == null) { |
115 | fail("getPayloadTest() failed because bb is null!"); | |
116 | return; | |
117 | } | |
118 | assertEquals((byte) 0x47, bb.get()); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Method that tests getSourceEndpoint of PcapEvent. | |
123 | */ | |
124 | @Test | |
125 | public void getSourceEndpointTest() { | |
c88feda9 | 126 | assertEquals("00:00:01:00:00:00/145.254.160.237/3372", fEvent.getSourceEndpoint(TmfPcapProtocol.TCP)); |
527c3a79 VP |
127 | } |
128 | ||
129 | /** | |
130 | * Method that tests getDestinationEndpointTest of PcapEvent. | |
131 | */ | |
132 | @Test | |
133 | public void getDestinationEndpointTest() { | |
c88feda9 | 134 | assertEquals("fe:ff:20:00:01:00", fEvent.getDestinationEndpoint(TmfPcapProtocol.ETHERNET_II)); |
527c3a79 VP |
135 | } |
136 | ||
137 | /** | |
138 | * Method that tests toString() of PcapEvent. | |
139 | */ | |
140 | @Test | |
141 | public void toStringTest() { | |
142 | assertEquals("3372 > 80 [ACK, PSH] Seq=951057940 Ack=290218380 Len=20", fEvent.toString()); | |
143 | } | |
144 | ||
145 | /** | |
146 | * Method that tests toString(protocol) of PcapEvent. | |
147 | */ | |
148 | @Test | |
149 | public void toStringAtSpecificProtocolTest() { | |
c88feda9 | 150 | assertEquals("Src: 145.254.160.237 , Dst: 65.208.228.223", fEvent.toString(TmfPcapProtocol.IPV4)); |
527c3a79 VP |
151 | } |
152 | ||
153 | } |