Fix some null warnings
[deliverable/tracecompass.git] / pcap / org.eclipse.tracecompass.tmf.pcap.core.tests / src / org / eclipse / tracecompass / tmf / pcap / core / tests / event / PcapEventFieldTest.java
CommitLineData
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 13package org.eclipse.tracecompass.tmf.pcap.core.tests.event;
527c3a79
VP
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.fail;
17import static org.junit.Assume.assumeTrue;
18
19import java.io.IOException;
20import java.nio.ByteBuffer;
21import java.nio.ByteOrder;
22import java.util.ArrayList;
23import java.util.List;
24import java.util.Map;
25
26import org.eclipse.jdt.annotation.NonNull;
71f2817f
AM
27import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException;
28import org.eclipse.tracecompass.internal.pcap.core.packet.Packet;
29import org.eclipse.tracecompass.internal.pcap.core.protocol.ipv4.IPv4Packet;
30import org.eclipse.tracecompass.internal.pcap.core.trace.BadPcapFileException;
31import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
2bdf0193
AM
32import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventField;
33import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapRootEventField;
71f2817f 34import org.eclipse.tracecompass.pcap.core.tests.shared.PcapTestTrace;
2bdf0193
AM
35import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
36import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
527c3a79
VP
37import org.junit.BeforeClass;
38import org.junit.Test;
39
40/**
41 * JUnit that test the PcapEventField class.
42 *
43 * @author Vincent Perot
44 */
45public 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
aa353506 213 private static ITmfEventField @NonNull [] generatePacketFields(Packet packet) {
527c3a79
VP
214 List<ITmfEventField> fieldList = new ArrayList<>();
215 List<ITmfEventField> subfieldList = new ArrayList<>();
216 Packet localPacket = packet;
217
218 while (localPacket != null) {
219 subfieldList.clear();
aa353506 220 for (Map.Entry<@NonNull String, @NonNull String> entry : localPacket.getFields().entrySet()) {
527c3a79 221 String key = entry.getKey();
527c3a79
VP
222 String value = entry.getValue();
223 subfieldList.add(new TmfEventField(key, value, null));
224 }
225 ITmfEventField[] subfieldArray = subfieldList.toArray(new ITmfEventField[subfieldList.size()]);
226 fieldList.add(new PcapEventField(localPacket.getProtocol().getName(), EMPTY_STRING, subfieldArray, localPacket));
227 localPacket = localPacket.getChildPacket();
228 }
229
95bcb6c4 230 return fieldList.toArray(new ITmfEventField[fieldList.size()]);
527c3a79
VP
231 }
232
233}
This page took 0.058617 seconds and 5 git commands to generate.