ctf: rename CTFReaderException to CTFException
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / EventHeaderDeclarationTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.tracecompass.ctf.core.tests.types;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15
16 import java.nio.ByteBuffer;
17 import java.nio.ByteOrder;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.eclipse.tracecompass.ctf.core.CTFException;
22 import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
23 import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
24 import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration;
25 import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
26 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
27 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
28 import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
29 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
30 import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration;
31 import org.eclipse.tracecompass.internal.ctf.core.event.types.composite.EventHeaderCompactDeclaration;
32 import org.eclipse.tracecompass.internal.ctf.core.event.types.composite.EventHeaderDefinition;
33 import org.eclipse.tracecompass.internal.ctf.core.event.types.composite.EventHeaderLargeDeclaration;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 /**
38 * Event header declaration tests
39 *
40 * @author Matthew Khouzam
41 *
42 */
43 public class EventHeaderDeclarationTest {
44
45 private static final int ID = 2222;
46 private static final int TIMESTAMP = 1000;
47 private static final int VALID_LARGE = 1;
48 private static final int VALID_COMPACT = 0;
49
50 private final List<StructDeclaration> declarations = new ArrayList<>();
51
52 /**
53 * Setup
54 */
55 @Before
56 public void init() {
57 declarations.clear();
58
59 /**
60 * do not reflow
61 *
62 * <pre>
63 * struct event_header_compact {
64 * enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
65 * variant <id> {
66 * struct {
67 * uint27_clock_monotonic_t timestamp;
68 * } compact;
69 * struct {
70 * uint32_t id;
71 * uint64_clock_monotonic_t timestamp;
72 * } extended;
73 * } v;
74 * } align(8);
75 * </pre>
76 */
77
78 StructDeclaration base = new StructDeclaration(8);
79 EnumDeclaration enumDec = new EnumDeclaration(IntegerDeclaration.createDeclaration(5, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1));
80 enumDec.add(0, 30, "compact");
81 enumDec.add(31, 31, "extended");
82 base.addField("id", enumDec);
83 VariantDeclaration variantV = new VariantDeclaration();
84 StructDeclaration compact = new StructDeclaration(1);
85 compact.addField("timestamp", IntegerDeclaration.createDeclaration(27, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1));
86 variantV.addField("compact", compact);
87 StructDeclaration large = new StructDeclaration(1);
88 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
89 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
90 variantV.addField("extended", large);
91 base.addField("v", variantV);
92 declarations.add(base);
93
94 /**
95 * Do not reflow
96 *
97 * <pre>
98 * struct event_header_large {
99 * enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;
100 * variant <id> {
101 * struct {
102 * uint32_clock_monotonic_t timestamp;
103 * } compact;
104 * struct {
105 * uint32_t id;
106 * uint64_clock_monotonic_t timestamp;
107 * } extended;
108 * } v;
109 * } align(8);
110 * </pre>
111 */
112
113 base = new StructDeclaration(8);
114 enumDec = new EnumDeclaration(IntegerDeclaration.createDeclaration(16, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1));
115 enumDec.add(0, 65534, "compact");
116 enumDec.add(65535, 65535, "extended");
117 base.addField("id", enumDec);
118 variantV = new VariantDeclaration();
119 compact = new StructDeclaration(8);
120 compact.addField("timestamp", IntegerDeclaration.UINT_32B_DECL);
121 variantV.addField("compact", compact);
122 large = new StructDeclaration(8);
123 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
124 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
125 variantV.addField("extended", large);
126 base.addField("v", variantV);
127 declarations.add(base);
128
129 // bad - misnamed enum
130 base = new StructDeclaration(8);
131 enumDec = new EnumDeclaration(IntegerDeclaration.createDeclaration(5, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1));
132 enumDec.add(0, 30, "compact");
133 enumDec.add(31, 31, "large");
134 base.addField("id", enumDec);
135 variantV = new VariantDeclaration();
136 compact = new StructDeclaration(1);
137 compact.addField("timestamp", IntegerDeclaration.createDeclaration(27, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1));
138 variantV.addField("compact", compact);
139 large = new StructDeclaration(1);
140 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
141 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
142 variantV.addField("extended", large);
143 base.addField("v", variantV);
144 declarations.add(base);
145
146 // bad - missing enum
147 base = new StructDeclaration(8);
148 enumDec = new EnumDeclaration(IntegerDeclaration.createDeclaration(5, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1));
149 enumDec.add(0, 30, "compact");
150 base.addField("id", enumDec);
151 variantV = new VariantDeclaration();
152 compact = new StructDeclaration(1);
153 compact.addField("timestamp", IntegerDeclaration.createDeclaration(27, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1));
154 variantV.addField("compact", compact);
155 large = new StructDeclaration(1);
156 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
157 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
158 variantV.addField("extended", large);
159 base.addField("v", variantV);
160 declarations.add(base);
161
162 // bad - int 5 alignment 8 bit
163 base = new StructDeclaration(8);
164 enumDec = new EnumDeclaration(IntegerDeclaration.createDeclaration(5, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8));
165 enumDec.add(0, 30, "compact");
166 enumDec.add(31, 31, "extended");
167 base.addField("id", enumDec);
168 variantV = new VariantDeclaration();
169 compact = new StructDeclaration(1);
170 compact.addField("timestamp", IntegerDeclaration.createDeclaration(27, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 1));
171 variantV.addField("compact", compact);
172 large = new StructDeclaration(1);
173 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
174 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
175 variantV.addField("extended", large);
176 base.addField("v", variantV);
177 declarations.add(base);
178
179 // bad - well, sounds nice though
180 base = new StructDeclaration(8);
181 base.addField("potato salad", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
182 base.addField("bbq ribs", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
183 declarations.add(base);
184 // bad
185 base = new StructDeclaration(8);
186 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_16B_DECL));
187 base.addField("v", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
188 declarations.add(base);
189 // bad
190 base = new StructDeclaration(8);
191 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
192 base.addField("v", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
193 declarations.add(base);
194 // bad
195 base = new StructDeclaration(8);
196 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
197 variantV = new VariantDeclaration();
198 compact = new StructDeclaration(8);
199 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
200 variantV.addField("compact1", compact);
201 large = new StructDeclaration(8);
202 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
203 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
204 variantV.addField("extended", large);
205 base.addField("v", variantV);
206 declarations.add(base);
207
208 // bad
209 base = new StructDeclaration(8);
210 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
211 variantV = new VariantDeclaration();
212 compact = new StructDeclaration(8);
213 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
214 variantV.addField("compact", compact);
215 large = new StructDeclaration(8);
216 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
217 large.addField("timestamp1", IntegerDeclaration.UINT_64B_DECL);
218 variantV.addField("extended", large);
219 base.addField("v", variantV);
220 declarations.add(base);
221
222 // bad
223 base = new StructDeclaration(8);
224 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
225 variantV = new VariantDeclaration();
226 compact = new StructDeclaration(8);
227 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
228 variantV.addField("compact", compact);
229 large = new StructDeclaration(8);
230 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
231 large.addField("timestamp", StringDeclaration.getStringDeclaration(Encoding.UTF8));
232 variantV.addField("extended", large);
233 base.addField("v", variantV);
234 declarations.add(base);
235
236 // bad
237 base = new StructDeclaration(8);
238 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
239 variantV = new VariantDeclaration();
240 compact = new StructDeclaration(8);
241 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
242 variantV.addField("compact", compact);
243 variantV.addField("surprise!", compact);
244 large = new StructDeclaration(8);
245 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
246 large.addField("timestamp", StringDeclaration.getStringDeclaration(Encoding.UTF8));
247 variantV.addField("extended", large);
248 base.addField("v", variantV);
249 declarations.add(base);
250
251 // bad
252 base = new StructDeclaration(8);
253 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_16B_DECL));
254 variantV = new VariantDeclaration();
255 compact = new StructDeclaration(8);
256 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
257 variantV.addField("compact", compact);
258 variantV.addField("surprise!", compact);
259 large = new StructDeclaration(8);
260 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
261 large.addField("timestamp", StringDeclaration.getStringDeclaration(Encoding.UTF8));
262 variantV.addField("extended", large);
263 base.addField("v", variantV);
264 declarations.add(base);
265 // bad
266 base = new StructDeclaration(8);
267 base.addField("id", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
268 base.addField("v", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
269 declarations.add(base);
270 // bad
271 base = new StructDeclaration(8);
272 base.addField("id", IntegerDeclaration.INT_32B_DECL);
273 base.addField("timestamp", IntegerDeclaration.INT_32B_DECL);
274 declarations.add(base);
275 // bad
276 base = new StructDeclaration(8);
277 base.addField("id", new EnumDeclaration(IntegerDeclaration.INT_8_DECL));
278 base.addField("timestamp", IntegerDeclaration.INT_32B_DECL);
279 declarations.add(base);
280 }
281
282 /**
283 * Validate a compact declaration
284 */
285 @Test
286 public void validateCompact() {
287 assertEquals(true, EventHeaderCompactDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN).isCompactEventHeader(declarations.get(VALID_COMPACT)));
288 }
289
290 /**
291 * Fail if it validates
292 */
293 @Test
294 public void validateCompactFail() {
295 for (int i = 0; i < declarations.size(); i++) {
296 if (i == VALID_COMPACT) {
297 continue;
298 }
299 assertEquals(false, EventHeaderCompactDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN).isCompactEventHeader(declarations.get(i)));
300 }
301 }
302
303 /**
304 * Validate a large declaration
305 */
306 @Test
307 public void validateLarge() {
308 assertEquals(true, EventHeaderLargeDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN).isLargeEventHeader(declarations.get(VALID_LARGE)));
309 }
310
311 /**
312 * Fail if it validates
313 */
314 @Test
315 public void validateLargeFail() {
316 for (int i = 0; i < declarations.size(); i++) {
317 if (i == VALID_LARGE) {
318 continue;
319 }
320 assertEquals(false, EventHeaderLargeDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN).isLargeEventHeader(declarations.get(i)));
321 }
322 }
323
324 /**
325 * Test an compact compact header
326 *
327 * @throws CTFException
328 * if {@link BitBuffer} is null
329 */
330 @Test
331 public void testCompactCompact() throws CTFException {
332 ByteBuffer buffer = ByteBuffer.allocate(16);
333 buffer.putInt(0x80000042);
334 byte[] validCompact1 = buffer.array();
335
336 EventHeaderCompactDeclaration decl = EventHeaderCompactDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN);
337 final ByteBuffer input = ByteBuffer.wrap(validCompact1);
338 assertNotNull(input);
339 EventHeaderDefinition def = decl.createDefinition(null, "bla", new BitBuffer(input));
340 assertNotNull(def);
341 assertEquals(16, def.getId());
342 assertEquals(0x42, def.getTimestamp());
343 }
344
345 /**
346 * Test an extended compact header
347 *
348 * @throws CTFException
349 * if {@link BitBuffer} is null
350 */
351 @Test
352 public void testCompactExtended() throws CTFException {
353 ByteBuffer buffer = ByteBuffer.allocate(16);
354 buffer.put((byte) 0xFF);
355 buffer.putInt(ID);
356 buffer.putLong(TIMESTAMP);
357 byte[] validCompact2 = buffer.array();
358
359 EventHeaderCompactDeclaration decl = EventHeaderCompactDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN);
360 final ByteBuffer input = ByteBuffer.wrap(validCompact2);
361 assertNotNull(input);
362 EventHeaderDefinition def = decl.createDefinition(null, "bla", new BitBuffer(input));
363 assertNotNull(def);
364 assertEquals(ID, def.getId());
365 assertEquals(TIMESTAMP, def.getTimestamp());
366 }
367
368 /**
369 * Test an compact large header
370 *
371 * @throws CTFException
372 * if {@link BitBuffer} is null
373 */
374 @Test
375 public void testLargeCompact() throws CTFException {
376 ByteBuffer buffer = ByteBuffer.allocate(16);
377 buffer.putShort((short) ID);
378 buffer.putInt(TIMESTAMP);
379 byte[] validLarge1 = buffer.array();
380
381 EventHeaderLargeDeclaration decl = EventHeaderLargeDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN);
382 final ByteBuffer input = ByteBuffer.wrap(validLarge1);
383 assertNotNull(input);
384 EventHeaderDefinition def = decl.createDefinition(null, "bla", new BitBuffer(input));
385 assertNotNull(def);
386 assertEquals(ID, def.getId());
387 assertEquals(TIMESTAMP, def.getTimestamp());
388 assertEquals(ID, ((IntegerDefinition) def.getDefinition("id")).getValue());
389 assertEquals(TIMESTAMP, ((IntegerDefinition) def.getDefinition("timestamp")).getValue());
390 }
391
392 /**
393 * Test an large large header
394 *
395 * @throws CTFException
396 * if {@link BitBuffer} is null
397 */
398 @Test
399 public void testLargeExtended() throws CTFException {
400 ByteBuffer buffer = ByteBuffer.allocate(16);
401 buffer.putShort((short) -1);
402 buffer.putInt(ID);
403 buffer.putLong(TIMESTAMP);
404 byte[] validLarge2 = buffer.array();
405
406 EventHeaderLargeDeclaration decl = EventHeaderLargeDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN);
407 final ByteBuffer input = ByteBuffer.wrap(validLarge2);
408 assertNotNull(input);
409 EventHeaderDefinition def = decl.createDefinition(null, "bla", new BitBuffer(input));
410 assertNotNull(def);
411 assertEquals(ID, def.getId());
412 assertEquals(TIMESTAMP, def.getTimestamp());
413 assertEquals(ID, ((IntegerDefinition) def.getDefinition("id")).getValue());
414 assertEquals(TIMESTAMP, ((IntegerDefinition) def.getDefinition("timestamp")).getValue());
415 }
416
417 /**
418 * Test maximum sizes, make sure they don't change unannounced
419 */
420 @Test
421 public void testMaxSizes() {
422 assertEquals(112, (EventHeaderLargeDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN)).getMaximumSize());
423 assertEquals(104, (EventHeaderCompactDeclaration.getEventHeader(ByteOrder.BIG_ENDIAN)).getMaximumSize());
424 }
425 }
This page took 0.042124 seconds and 5 git commands to generate.