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 | ||
13 | package org.eclipse.linuxtools.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.linuxtools.pcap.core.tests.shared.PcapTestTrace; | |
25 | import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException; | |
26 | import org.eclipse.linuxtools.pcap.core.trace.PcapFile; | |
27 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
28 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; | |
29 | import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation; | |
30 | import org.eclipse.linuxtools.tmf.pcap.core.event.PcapEvent; | |
31 | import org.eclipse.linuxtools.tmf.pcap.core.protocol.TmfProtocol; | |
32 | import org.eclipse.linuxtools.tmf.pcap.core.trace.PcapTrace; | |
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<TmfProtocol> 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 | String file = trace.getPath(); | |
64 | try (PcapFile pcap = new PcapFile(file); | |
65 | PcapTrace pcapTrace = new PcapTrace();) { | |
66 | pcapTrace.initTrace(null, trace.getPath(), PcapEvent.class); | |
67 | fEvent = pcapTrace.parseEvent(new TmfContext(new TmfLongLocation(3), 3)); | |
68 | } | |
69 | ||
70 | // Initialize protocol list. | |
71 | List<TmfProtocol> list = new ArrayList<>(); | |
72 | list.add(TmfProtocol.PCAP); | |
73 | list.add(TmfProtocol.ETHERNET_II); | |
74 | list.add(TmfProtocol.IPV4); | |
75 | list.add(TmfProtocol.TCP); | |
76 | list.add(TmfProtocol.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(TmfProtocol.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(TmfProtocol.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(TmfProtocol.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(TmfProtocol.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(TmfProtocol.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(TmfProtocol.IPV4)); | |
152 | } | |
153 | ||
154 | } |