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.assertEquals; | |
16 | import static org.junit.Assert.fail; | |
17 | import static org.junit.Assume.assumeTrue; | |
18 | ||
19 | import java.io.IOException; | |
20 | import java.nio.ByteBuffer; | |
21 | import java.nio.ByteOrder; | |
22 | import java.util.ArrayList; | |
23 | import java.util.List; | |
24 | import java.util.Map; | |
25 | ||
26 | import org.eclipse.jdt.annotation.NonNull; | |
71f2817f AM |
27 | import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException; |
28 | import org.eclipse.tracecompass.internal.pcap.core.packet.Packet; | |
29 | import org.eclipse.tracecompass.internal.pcap.core.protocol.ipv4.IPv4Packet; | |
30 | import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException; | |
31 | import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile; | |
2bdf0193 AM |
32 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventField; |
33 | import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapRootEventField; | |
71f2817f | 34 | import org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace; |
2bdf0193 AM |
35 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
36 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; | |
527c3a79 VP |
37 | import org.junit.BeforeClass; |
38 | import org.junit.Test; | |
39 | ||
40 | /** | |
41 | * JUnit that test the PcapEventField class. | |
42 | * | |
43 | * @author Vincent Perot | |
44 | */ | |
45 | public class PcapEventFieldTest { | |
46 | ||
47 | private static final @NonNull String EMPTY_STRING = ""; | |
48 | private static PcapEventField fRegularField; | |
49 | private static PcapRootEventField fRootField; | |
50 | ||
51 | /** | |
52 | * Initialize the Packet and the Event. | |
53 | * | |
54 | * @throws BadPcapFileException | |
55 | * Thrown when the pcap file is erroneous. | |
56 | * @throws IOException | |
57 | * Thrown when an IO error occurs. | |
58 | * @throws BadPacketException | |
59 | * Thrown when the packet is erroneous. | |
60 | */ | |
61 | @BeforeClass | |
62 | public static void setUp() throws IOException, BadPcapFileException, BadPacketException { | |
63 | ByteBuffer bb = ByteBuffer.allocate(25); | |
64 | bb.order(ByteOrder.BIG_ENDIAN); | |
65 | ||
66 | // Version + IHL | |
67 | bb.put((byte) 0x46); | |
68 | ||
69 | // DSCP + ECN | |
70 | bb.put((byte) 0x9A); | |
71 | ||
72 | // Total length - this is randomly chosen so that we verify that the | |
73 | // packet handles wrong total length. | |
74 | bb.put((byte) 0x00); | |
75 | bb.put((byte) 0xFF); | |
76 | ||
77 | // Identification | |
78 | bb.put((byte) 0x0F); | |
79 | bb.put((byte) 0xF0); | |
80 | ||
81 | // Flags + Fragment Offset | |
82 | bb.put((byte) 0x1E); | |
83 | bb.put((byte) 0xE1); | |
84 | ||
85 | // Time to live | |
86 | bb.put((byte) 0xA0); | |
87 | ||
88 | // Protocol - Unknown | |
89 | bb.put((byte) 0xFE); | |
90 | ||
91 | // Header checksum - chosen randomly | |
92 | bb.put((byte) 0x33); | |
93 | bb.put((byte) 0x44); | |
94 | ||
95 | // Source IP - 4 bytes | |
96 | bb.put((byte) 192); | |
97 | bb.put((byte) 168); | |
98 | bb.put((byte) 1); | |
99 | bb.put((byte) 0); | |
100 | ||
101 | // Destination IP - 4 bytes | |
102 | bb.put((byte) 193); | |
103 | bb.put((byte) 169); | |
104 | bb.put((byte) 2); | |
105 | bb.put((byte) 1); | |
106 | ||
107 | // Options - 4 bytes | |
108 | bb.put((byte) 0xA2); | |
109 | bb.put((byte) 0x56); | |
110 | bb.put((byte) 0xA2); | |
111 | bb.put((byte) 0x56); | |
112 | ||
113 | // Payload - 1 byte | |
114 | bb.put((byte) 0xA6); | |
115 | ||
116 | bb.flip(); | |
117 | ||
118 | PcapTestTrace trace = PcapTestTrace.MOSTLY_TCP; | |
119 | assumeTrue(trace.exists()); | |
333a2acb | 120 | try (PcapFile dummy = new PcapFile(trace.getPath())) { |
527c3a79 VP |
121 | IPv4Packet packet = new IPv4Packet(dummy, null, bb); |
122 | ITmfEventField[] fieldArray = generatePacketFields(packet); | |
123 | fRegularField = new PcapEventField("Regular Field", EMPTY_STRING, fieldArray, packet); | |
40dfafb3 | 124 | fRootField = new PcapRootEventField(fieldArray, packet); |
527c3a79 VP |
125 | } |
126 | ||
127 | } | |
128 | ||
129 | /** | |
130 | * Method that tests the copy constructor. | |
131 | */ | |
132 | @Test | |
133 | public void copyConstructorTest() { | |
134 | PcapEventField oldField = fRegularField; | |
135 | if (oldField == null) { | |
136 | fail("The field has not been initialized!"); | |
137 | return; | |
138 | } | |
139 | PcapEventField newField = new PcapEventField(oldField); | |
140 | assertEquals(fRegularField.hashCode(), newField.hashCode()); | |
141 | assertEquals(fRegularField, newField); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Method that tests a standard field value request. | |
146 | */ | |
147 | @Test | |
148 | public void regularFieldValueRequestTest() { | |
149 | ITmfEventField field = fRootField.getField("Internet Protocol Version 4"); | |
150 | if (field == null) { | |
151 | fail("The field is null!"); | |
152 | return; | |
153 | } | |
154 | ||
155 | ITmfEventField subfield = field.getField("Source IP Address"); | |
156 | if (subfield == null) { | |
157 | fail("The subfield is null!"); | |
158 | return; | |
159 | } | |
160 | ||
161 | String string = subfield.getValue().toString(); | |
162 | assertEquals("192.168.1.0", string); | |
163 | } | |
164 | ||
165 | /** | |
166 | * Method that tests a custom field value request. | |
167 | */ | |
168 | @Test | |
169 | public void customFieldValueRequestTest() { | |
170 | ITmfEventField field = fRootField.getField(":protocol:"); | |
171 | if (field == null) { | |
172 | fail("The field is null!"); | |
173 | return; | |
174 | } | |
175 | String string = field.getValue().toString(); | |
176 | assertEquals("IPV4", string); | |
177 | ||
178 | field = fRootField.getField(":packetsource:"); | |
179 | if (field == null) { | |
180 | fail("The field is null!"); | |
181 | return; | |
182 | } | |
183 | string = field.getValue().toString(); | |
184 | assertEquals("192.168.1.0", string); | |
185 | ||
186 | field = fRootField.getField(":packetdestination:"); | |
187 | if (field == null) { | |
188 | fail("The field is null!"); | |
189 | return; | |
190 | } | |
191 | string = field.getValue().toString(); | |
192 | assertEquals("193.169.2.1", string); | |
193 | ||
194 | } | |
195 | ||
196 | /** | |
197 | * Method that teststhe toString() method for a non-root field. | |
198 | */ | |
199 | @Test | |
200 | public void regularToStringTest() { | |
201 | assertEquals("Src: 192.168.1.0 , Dst: 193.169.2.1", fRegularField.toString()); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Method that teststhe toString() method for a root field. | |
206 | */ | |
207 | @Test | |
208 | public void rootToStringTest() { | |
209 | assertEquals("192.168.1.0 > 193.169.2.1 Id=4080 Len=1", fRootField.toString()); | |
210 | } | |
211 | ||
212 | // Convenience method | |
213 | private static ITmfEventField[] generatePacketFields(Packet packet) { | |
214 | List<ITmfEventField> fieldList = new ArrayList<>(); | |
215 | List<ITmfEventField> subfieldList = new ArrayList<>(); | |
216 | Packet localPacket = packet; | |
217 | ||
218 | while (localPacket != null) { | |
219 | subfieldList.clear(); | |
220 | for (Map.Entry<String, String> entry : localPacket.getFields().entrySet()) { | |
221 | ||
222 | @SuppressWarnings("null") | |
223 | @NonNull | |
224 | String key = entry.getKey(); | |
225 | ||
226 | @SuppressWarnings("null") | |
227 | @NonNull | |
228 | String value = entry.getValue(); | |
229 | subfieldList.add(new TmfEventField(key, value, null)); | |
230 | } | |
231 | ITmfEventField[] subfieldArray = subfieldList.toArray(new ITmfEventField[subfieldList.size()]); | |
232 | fieldList.add(new PcapEventField(localPacket.getProtocol().getName(), EMPTY_STRING, subfieldArray, localPacket)); | |
233 | localPacket = localPacket.getChildPacket(); | |
234 | } | |
235 | ||
236 | ITmfEventField[] fieldArray = fieldList.toArray(new ITmfEventField[fieldList.size()]); | |
237 | if (fieldArray == null) { | |
238 | return new ITmfEventField[0]; | |
239 | } | |
240 | return fieldArray; | |
241 | } | |
242 | ||
243 | } |