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.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; | |
27 | import org.eclipse.linuxtools.pcap.core.packet.BadPacketException; | |
28 | import org.eclipse.linuxtools.pcap.core.packet.Packet; | |
29 | import org.eclipse.linuxtools.pcap.core.protocol.ipv4.IPv4Packet; | |
30 | import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace; | |
31 | import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException; | |
32 | import org.eclipse.linuxtools.pcap.core.trace.PcapFile; | |
33 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
34 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; | |
35 | import org.eclipse.linuxtools.tmf.pcap.core.event.PcapEventField; | |
36 | import org.eclipse.linuxtools.tmf.pcap.core.event.PcapRootEventField; | |
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()); | |
120 | String file = trace.getPath(); | |
121 | try (PcapFile dummy = new PcapFile(file)) { | |
122 | IPv4Packet packet = new IPv4Packet(dummy, null, bb); | |
123 | ITmfEventField[] fieldArray = generatePacketFields(packet); | |
124 | fRegularField = new PcapEventField("Regular Field", EMPTY_STRING, fieldArray, packet); | |
125 | fRootField = new PcapRootEventField(EMPTY_STRING, fieldArray, packet); | |
126 | } | |
127 | ||
128 | } | |
129 | ||
130 | /** | |
131 | * Method that tests the copy constructor. | |
132 | */ | |
133 | @Test | |
134 | public void copyConstructorTest() { | |
135 | PcapEventField oldField = fRegularField; | |
136 | if (oldField == null) { | |
137 | fail("The field has not been initialized!"); | |
138 | return; | |
139 | } | |
140 | PcapEventField newField = new PcapEventField(oldField); | |
141 | assertEquals(fRegularField.hashCode(), newField.hashCode()); | |
142 | assertEquals(fRegularField, newField); | |
143 | } | |
144 | ||
145 | /** | |
146 | * Method that tests a standard field value request. | |
147 | */ | |
148 | @Test | |
149 | public void regularFieldValueRequestTest() { | |
150 | ITmfEventField field = fRootField.getField("Internet Protocol Version 4"); | |
151 | if (field == null) { | |
152 | fail("The field is null!"); | |
153 | return; | |
154 | } | |
155 | ||
156 | ITmfEventField subfield = field.getField("Source IP Address"); | |
157 | if (subfield == null) { | |
158 | fail("The subfield is null!"); | |
159 | return; | |
160 | } | |
161 | ||
162 | String string = subfield.getValue().toString(); | |
163 | assertEquals("192.168.1.0", string); | |
164 | } | |
165 | ||
166 | /** | |
167 | * Method that tests a custom field value request. | |
168 | */ | |
169 | @Test | |
170 | public void customFieldValueRequestTest() { | |
171 | ITmfEventField field = fRootField.getField(":protocol:"); | |
172 | if (field == null) { | |
173 | fail("The field is null!"); | |
174 | return; | |
175 | } | |
176 | String string = field.getValue().toString(); | |
177 | assertEquals("IPV4", string); | |
178 | ||
179 | field = fRootField.getField(":packetsource:"); | |
180 | if (field == null) { | |
181 | fail("The field is null!"); | |
182 | return; | |
183 | } | |
184 | string = field.getValue().toString(); | |
185 | assertEquals("192.168.1.0", string); | |
186 | ||
187 | field = fRootField.getField(":packetdestination:"); | |
188 | if (field == null) { | |
189 | fail("The field is null!"); | |
190 | return; | |
191 | } | |
192 | string = field.getValue().toString(); | |
193 | assertEquals("193.169.2.1", string); | |
194 | ||
195 | } | |
196 | ||
197 | /** | |
198 | * Method that teststhe toString() method for a non-root field. | |
199 | */ | |
200 | @Test | |
201 | public void regularToStringTest() { | |
202 | assertEquals("Src: 192.168.1.0 , Dst: 193.169.2.1", fRegularField.toString()); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Method that teststhe toString() method for a root field. | |
207 | */ | |
208 | @Test | |
209 | public void rootToStringTest() { | |
210 | assertEquals("192.168.1.0 > 193.169.2.1 Id=4080 Len=1", fRootField.toString()); | |
211 | } | |
212 | ||
213 | // Convenience method | |
214 | private static ITmfEventField[] generatePacketFields(Packet packet) { | |
215 | List<ITmfEventField> fieldList = new ArrayList<>(); | |
216 | List<ITmfEventField> subfieldList = new ArrayList<>(); | |
217 | Packet localPacket = packet; | |
218 | ||
219 | while (localPacket != null) { | |
220 | subfieldList.clear(); | |
221 | for (Map.Entry<String, String> entry : localPacket.getFields().entrySet()) { | |
222 | ||
223 | @SuppressWarnings("null") | |
224 | @NonNull | |
225 | String key = entry.getKey(); | |
226 | ||
227 | @SuppressWarnings("null") | |
228 | @NonNull | |
229 | String value = entry.getValue(); | |
230 | subfieldList.add(new TmfEventField(key, value, null)); | |
231 | } | |
232 | ITmfEventField[] subfieldArray = subfieldList.toArray(new ITmfEventField[subfieldList.size()]); | |
233 | fieldList.add(new PcapEventField(localPacket.getProtocol().getName(), EMPTY_STRING, subfieldArray, localPacket)); | |
234 | localPacket = localPacket.getChildPacket(); | |
235 | } | |
236 | ||
237 | ITmfEventField[] fieldArray = fieldList.toArray(new ITmfEventField[fieldList.size()]); | |
238 | if (fieldArray == null) { | |
239 | return new ITmfEventField[0]; | |
240 | } | |
241 | return fieldArray; | |
242 | } | |
243 | ||
244 | } |