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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.types
;
13 import static org
.junit
.Assert
.assertEquals
;
14 import static org
.junit
.Assert
.assertNotNull
;
16 import java
.nio
.ByteBuffer
;
17 import java
.nio
.ByteOrder
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
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
;
38 * Event header declaration tests
40 * @author Matthew Khouzam
43 public class EventHeaderDeclarationTest
{
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;
50 private final List
<StructDeclaration
> declarations
= new ArrayList
<>();
63 * struct event_header_compact {
64 * enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
67 * uint27_clock_monotonic_t timestamp;
71 * uint64_clock_monotonic_t timestamp;
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
);
98 * struct event_header_large {
99 * enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;
102 * uint32_clock_monotonic_t timestamp;
106 * uint64_clock_monotonic_t timestamp;
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
283 * Validate a compact declaration
286 public void validateCompact() {
287 assertEquals(true, EventHeaderCompactDeclaration
.getEventHeader(ByteOrder
.BIG_ENDIAN
).isCompactEventHeader(declarations
.get(VALID_COMPACT
)));
291 * Fail if it validates
294 public void validateCompactFail() {
295 for (int i
= 0; i
< declarations
.size(); i
++) {
296 if (i
== VALID_COMPACT
) {
299 assertEquals(false, EventHeaderCompactDeclaration
.getEventHeader(ByteOrder
.BIG_ENDIAN
).isCompactEventHeader(declarations
.get(i
)));
304 * Validate a large declaration
307 public void validateLarge() {
308 assertEquals(true, EventHeaderLargeDeclaration
.getEventHeader(ByteOrder
.BIG_ENDIAN
).isLargeEventHeader(declarations
.get(VALID_LARGE
)));
312 * Fail if it validates
315 public void validateLargeFail() {
316 for (int i
= 0; i
< declarations
.size(); i
++) {
317 if (i
== VALID_LARGE
) {
320 assertEquals(false, EventHeaderLargeDeclaration
.getEventHeader(ByteOrder
.BIG_ENDIAN
).isLargeEventHeader(declarations
.get(i
)));
325 * Test an compact compact header
327 * @throws CTFException
328 * if {@link BitBuffer} is null
331 public void testCompactCompact() throws CTFException
{
332 ByteBuffer buffer
= ByteBuffer
.allocate(16);
333 buffer
.putInt(0x80000042);
334 byte[] validCompact1
= buffer
.array();
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
));
341 assertEquals(16, def
.getId());
342 assertEquals(0x42, def
.getTimestamp());
346 * Test an extended compact header
348 * @throws CTFException
349 * if {@link BitBuffer} is null
352 public void testCompactExtended() throws CTFException
{
353 ByteBuffer buffer
= ByteBuffer
.allocate(16);
354 buffer
.put((byte) 0xFF);
356 buffer
.putLong(TIMESTAMP
);
357 byte[] validCompact2
= buffer
.array();
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
));
364 assertEquals(ID
, def
.getId());
365 assertEquals(TIMESTAMP
, def
.getTimestamp());
369 * Test an compact large header
371 * @throws CTFException
372 * if {@link BitBuffer} is null
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();
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
));
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());
393 * Test an large large header
395 * @throws CTFException
396 * if {@link BitBuffer} is null
399 public void testLargeExtended() throws CTFException
{
400 ByteBuffer buffer
= ByteBuffer
.allocate(16);
401 buffer
.putShort((short) -1);
403 buffer
.putLong(TIMESTAMP
);
404 byte[] validLarge2
= buffer
.array();
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
));
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());
418 * Test maximum sizes, make sure they don't change unannounced
421 public void testMaxSizes() {
422 assertEquals(112, (EventHeaderLargeDeclaration
.getEventHeader(ByteOrder
.BIG_ENDIAN
)).getMaximumSize());
423 assertEquals(104, (EventHeaderCompactDeclaration
.getEventHeader(ByteOrder
.BIG_ENDIAN
)).getMaximumSize());