ctf: preserve byte order if explicitly set in a typedef
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / MetadataTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 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
12 package org.eclipse.tracecompass.ctf.core.tests.trace;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.fail;
18
19 import java.nio.ByteOrder;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.UUID;
23
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.tracecompass.ctf.core.CTFException;
26 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
27 import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
28 import org.eclipse.tracecompass.ctf.core.event.types.ISimpleDatatypeDeclaration;
29 import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils;
30 import org.eclipse.tracecompass.ctf.core.trace.CTFStream;
31 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
32 import org.eclipse.tracecompass.ctf.core.trace.Metadata;
33 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 import com.google.common.collect.Iterables;
38
39 /**
40 * The class <code>MetadataTest</code> contains tests for the class
41 * <code>{@link Metadata}</code>.
42 *
43 * @author ematkho
44 * @version $Revision: 1.0 $
45 */
46 @SuppressWarnings("javadoc")
47 public class MetadataTest {
48
49 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
50 private static final String mdStart = "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n" +
51 " typealias integer { size = 16; align = 8; signed = false; } := uint16_t;\n" +
52 " typealias integer { size = 32; align = 8; signed = false; } := uint32_t;\n" +
53 " typealias integer { size = 64; align = 8; signed = false; } := uint64_t;\n" +
54 " typealias integer { size = 64; align = 8; signed = false; } := unsigned long;\n" +
55 " typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" +
56 " typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" +
57 "" +
58 " trace {\n" +
59 " major = 1;\n" +
60 " minor = 8;\n" +
61 " uuid = \"8b1258ba-effb-554b-b779-fbd676746000\";\n" +
62 " byte_order = le;\n" +
63 " packet.header := struct {\n" +
64 " uint32_t magic;\n" +
65 " uint8_t uuid[16];\n" +
66 " uint32_t stream_id;\n" +
67 " };\n" +
68 " };\n" +
69 "" +
70 " env {\n" +
71 " hostname = \"computer\";\n" +
72 " domain = \"kernel\";\n" +
73 " sysname = \"BeOS\";\n" +
74 " kernel_release = \"95\";\n" +
75 " kernel_version = \"BeWare 95\";\n" +
76 " tracer_name = \"BeOS Tracer\";\n" +
77 " tracer_major = 2;\n" +
78 " tracer_minor = 3;\n" +
79 " tracer_patchlevel = 0;\n" +
80 " };\n" +
81 " clock {\n" +
82 " name = monotonic;\n" +
83 " uuid = \"4d737a79-e3f1-4f4d-a649-42015266baf5\";\n" +
84 " description = \"Monotonic Clock\";\n" +
85 " freq = 1000000000; /* Frequency, in Hz */\n" +
86 " /* clock value offset from Epoch is: offset * (1/freq) */\n" +
87 " offset = 1383600210829415521;\n" +
88 " };\n" +
89
90 " typealias integer {\n" +
91 "size = 27; align = 1; signed = false;\n" +
92 " map = clock.monotonic.value;\n" +
93 " } := uint27_clock_monotonic_t;\n" +
94 " \n" +
95 " typealias integer {\n" +
96 " size = 32; align = 8; signed = false;\n" +
97 " map = clock.monotonic.value;\n" +
98 " } := uint32_clock_monotonic_t;\n" +
99 " \n" +
100 " typealias integer {\n" +
101 " size = 64; align = 8; signed = false;\n" +
102 " map = clock.monotonic.value;\n" +
103 " } := uint64_clock_monotonic_t;\n" +
104 " \n" +
105 " struct packet_context {\n" +
106 " uint64_clock_monotonic_t timestamp_begin;\n" +
107 " uint64_clock_monotonic_t timestamp_end;\n" +
108 " uint64_t content_size;\n" +
109 " uint64_t packet_size;\n" +
110 " unsigned long events_discarded;\n" +
111 " uint32_t cpu_id;\n" +
112 " };\n" +
113 " \n" +
114 " struct event_header_compact {\n" +
115 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" +
116 " variant <id> {\n" +
117 " struct {\n" +
118 " uint27_clock_monotonic_t timestamp;\n" +
119 " } compact;\n" +
120 " struct {\n" +
121 " uint32_t id;\n" +
122 " uint64_clock_monotonic_t timestamp;\n" +
123 " } extended;\n" +
124 " } v;\n" +
125 " } align(8);\n" +
126 " \n" +
127 " struct event_header_large {\n" +
128 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" +
129 " variant <id> {\n" +
130 " struct {\n" +
131 " uint32_clock_monotonic_t timestamp;\n" +
132 " } compact;\n" +
133 " struct {\n" +
134 " uint32_t id;\n" +
135 " uint64_clock_monotonic_t timestamp;\n" +
136 " } extended;\n" +
137 " } v;\n" +
138 " } align(8);\n" +
139 " \n" +
140 " stream {\n" +
141 " id = 0;\n" +
142 " event.header := struct event_header_compact;\n" +
143 " packet.context := struct packet_context;\n" +
144 " };\n" +
145 " \n" +
146 " event {\n" +
147 " name = sched_switch;\n" +
148 " id = 0;\n" +
149 " stream_id = 0;\n" +
150 " fields := struct {\n" +
151 " integer { size = 8; align = 8; signed = 1; encoding = UTF8; base = 10; } _prev_comm[16];\n" +
152 " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _prev_tid;\n" +
153 " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _prev_prio;\n" +
154 " integer { size = 64; align = 8; signed = 1; encoding = none; base = 10; } _prev_state;\n" +
155 " integer { size = 8; align = 8; signed = 1; encoding = UTF8; base = 10; } _next_comm[16];\n" +
156 " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _next_tid;\n" +
157 " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _next_prio;\n" +
158 " };\n" +
159 " };";
160
161 private static final String mdSecond = " event {\n" +
162 " name = bozo_the_clown;\n" +
163 " id = 1;\n" +
164 " stream_id = 0;\n" +
165 " fields := struct {\n" +
166 " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } clown_nose;\n" +
167 " };\n" +
168 " };";
169
170 private static final String ENDIAN_CHANGE_L_B =
171 "/* ctf 1.8 */"
172 + "typealias integer { size = 32; align = 16; byte_order = be; signed = true; base = dec; } := INT;"
173 + "trace { byte_order = le; };"
174 + "event { "
175 + " name = \"bob\"; "
176 + " fields := struct field { INT data ; };"
177 + "};";
178
179 private static final String ENDIAN_CHANGE_L_N =
180 "/* ctf 1.8 */"
181 + "typealias integer { size = 32; align = 16; signed = true; base = dec; } := INT;"
182 + "trace { byte_order = le; };"
183 + "event { "
184 + " name = \"bob\"; "
185 + " fields := struct field { INT data ; };"
186 + "};";
187
188 private static final String ENDIAN_CHANGE_L_L =
189 "/* ctf 1.8 */"
190 + "typealias integer { size = 32; align = 16; byte_order = le; signed = true; base = dec; } := INT;"
191 + "trace { byte_order = le; };"
192 + "event { "
193 + " name = \"bob\"; "
194 + " fields := struct field { INT data ; };"
195 + "};";
196
197
198 private static final String ENDIAN_CHANGE_B_L =
199 "/* ctf 1.8 */"
200 + "typealias integer { size = 32; align = 16; byte_order = le; signed = true; base = dec; } := INT;"
201 + "trace { byte_order = be; };"
202 + "event { "
203 + " name = \"bob\"; "
204 + " fields := struct field { INT data ; };"
205 + "};";
206
207 private static final String ENDIAN_CHANGE_B_N =
208 "/* ctf 1.8 */"
209 + "typealias integer { size = 32; align = 16; signed = true; base = dec; } := INT;"
210 + "trace { byte_order = be; };"
211 + "event { "
212 + " name = \"bob\"; "
213 + " fields := struct field { INT data ; };"
214 + "};";
215
216 private static final String ENDIAN_CHANGE_B_B =
217 "/* ctf 1.8 */"
218 + "typealias integer { size = 32; align = 16; byte_order = be; signed = true; base = dec; } := INT;"
219 + "trace { byte_order = be; };"
220 + "event { "
221 + " name = \"bob\"; "
222 + " fields := struct field { INT data ; };"
223 + "};";
224
225
226 private Metadata fixture;
227
228 /**
229 * Perform pre-test initialization.
230 *
231 * @throws CTFException
232 */
233 @Before
234 public void setUp() throws CTFException {
235 fixture = new Metadata(CtfTestTraceUtils.getTrace(testTrace));
236 }
237
238 /**
239 * Run the Metadata(CTFTrace) constructor test.
240 */
241 @Test
242 public void testMetadata() {
243 assertNotNull(fixture);
244 }
245
246 @Test
247 public void testTextMD() throws CTFException {
248 testSingleFragment();
249 }
250
251 protected CTFTrace testSingleFragment() throws CTFException {
252 fixture = new Metadata();
253 CTFTrace trace = fixture.getTrace();
254 for (CTFStream s : trace.getStreams()) {
255 fail("This should be empty, has" + s.toString());
256 }
257 fixture.parseText(mdStart);
258 int count = 0;
259 for (CTFStream s : trace.getStreams()) {
260 count++;
261 assertNotNull(s);
262 }
263 assertEquals(1, count);
264 assertEquals(1, trace.getEventDeclarations(0L).size());
265 return trace;
266 }
267
268 @Test
269 public void testStreamTextMD() throws CTFException {
270 CTFTrace trace = testSingleFragment();
271 fixture.parseTextFragment(mdSecond);
272 final List<IEventDeclaration> eventDeclarations = new ArrayList<>(trace.getEventDeclarations(0L));
273 assertEquals(2, eventDeclarations.size());
274 assertEquals("bozo_the_clown", eventDeclarations.get(1).getName());
275 }
276
277 /**
278 * Run the ByteOrder getDetectedByteOrder() method test.
279 *
280 * @throws CTFException
281 */
282 @Test
283 public void testGetDetectedByteOrder() throws CTFException {
284 setUp();
285 ByteOrder result = fixture.getDetectedByteOrder();
286 assertNull(result);
287 }
288
289 /**
290 * Test toString
291 *
292 * @throws CTFException
293 */
294 @Test
295 public void testToString() throws CTFException {
296 setUp();
297 String result = fixture.toString();
298 assertNotNull(result);
299 }
300
301 /**
302 * Test a changing endian event field
303 *
304 * @throws CTFException
305 * won't happen
306 */
307 @Test
308 public void testEndian() throws CTFException {
309 testEndianess(ENDIAN_CHANGE_L_B, ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN);
310 testEndianess(ENDIAN_CHANGE_L_N, ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN);
311 testEndianess(ENDIAN_CHANGE_L_L, ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN);
312 testEndianess(ENDIAN_CHANGE_B_L, ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN);
313 testEndianess(ENDIAN_CHANGE_B_N, ByteOrder.BIG_ENDIAN, ByteOrder.BIG_ENDIAN);
314 testEndianess(ENDIAN_CHANGE_B_B, ByteOrder.BIG_ENDIAN, ByteOrder.BIG_ENDIAN);
315 }
316
317 private void testEndianess(String tsdl, ByteOrder traceEndian, ByteOrder fieldEndian) throws CTFException {
318 fixture = new Metadata();
319 CTFTrace trace = fixture.getTrace();
320 fixture.parseText(tsdl);
321 assertEquals(traceEndian, trace.getByteOrder());
322 final Iterable<IEventDeclaration> eventDeclarations = trace.getEventDeclarations(0L);
323 assertNotNull(eventDeclarations);
324 IEventDeclaration event = Iterables.getFirst(eventDeclarations, null);
325 assertNotNull(event);
326 assertNotNull(event.getFields());
327 final @Nullable IDeclaration field = event.getFields().getField("data");
328 assertNotNull(field);
329 if (field instanceof ISimpleDatatypeDeclaration) {
330 ISimpleDatatypeDeclaration declaration = (ISimpleDatatypeDeclaration) field;
331 assertEquals(fieldEndian, declaration.getByteOrder());
332 } else {
333 fail("data is not the right type");
334 }
335 }
336
337 /**
338 * Run the void parse() method test.
339 *
340 * @throws CTFException
341 */
342 @Test
343 public void testParse() throws CTFException {
344 setUp();
345 assertEquals(new UUID(0xd18e637435a1cd42L, 0x8e70a9cffa712793L), CtfTestTraceUtils.getTrace(testTrace).getUUID());
346 assertEquals(1332166405241713920.0, CtfTestTraceUtils.getTrace(testTrace).getClock().getClockOffset(), 200.0);
347 assertEquals(8, CtfTestTraceUtils.getTrace(testTrace).getEnvironment().size());
348 }
349 }
This page took 0.054026 seconds and 5 git commands to generate.