lib: force user to include `<babeltrace2/babeltrace.h>`
[babeltrace.git] / tests / ctf-writer / ctf_writer.c
1 /*
2 * test_ctf_writer.c
3 *
4 * CTF Writer test
5 *
6 * Copyright 2013 - 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include <babeltrace2/ctf-writer/writer.h>
23 #include <babeltrace2/ctf-writer/clock.h>
24 #include <babeltrace2/ctf-writer/clock-class.h>
25 #include <babeltrace2/ctf-writer/stream.h>
26 #include <babeltrace2/ctf-writer/event.h>
27 #include <babeltrace2/ctf-writer/event-types.h>
28 #include <babeltrace2/ctf-writer/event-fields.h>
29 #include <babeltrace2/ctf-writer/stream-class.h>
30 #include <babeltrace2/ctf-writer/trace.h>
31 #include <babeltrace2/ctf/events.h>
32 #include <babeltrace2/babeltrace.h>
33 #include <glib.h>
34 #include <unistd.h>
35 #include "compat/stdlib.h"
36 #include <stdio.h>
37 #include "compat/limits.h"
38 #include "compat/stdio.h"
39 #include <string.h>
40 #include "common/assert.h"
41 #include "common/uuid.h"
42 #include <fcntl.h>
43 #include "tap/tap.h"
44 #include <math.h>
45 #include <float.h>
46 #include "common.h"
47
48 #define METADATA_LINE_SIZE 512
49 #define SEQUENCE_TEST_LENGTH 10
50 #define ARRAY_TEST_LENGTH 5
51 #define PACKET_RESIZE_TEST_DEF_LENGTH 100000
52
53 #define DEFAULT_CLOCK_FREQ 1000000000
54 #define DEFAULT_CLOCK_PRECISION 1
55 #define DEFAULT_CLOCK_OFFSET 0
56 #define DEFAULT_CLOCK_OFFSET_S 0
57 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
58 #define DEFAULT_CLOCK_TIME 0
59 #define DEFAULT_CLOCK_VALUE 0
60
61 #define NR_TESTS 325
62
63 struct bt_utsname {
64 char sysname[BABELTRACE_HOST_NAME_MAX];
65 char nodename[BABELTRACE_HOST_NAME_MAX];
66 char release[BABELTRACE_HOST_NAME_MAX];
67 char version[BABELTRACE_HOST_NAME_MAX];
68 char machine[BABELTRACE_HOST_NAME_MAX];
69 };
70
71 static int64_t current_time = 42;
72 static unsigned int packet_resize_test_length = PACKET_RESIZE_TEST_DEF_LENGTH;
73
74 /* Return 1 if uuids match, zero if different. */
75 static
76 int uuid_match(const uint8_t *uuid_a, const uint8_t *uuid_b)
77 {
78 int ret = 0;
79 int i;
80
81 if (!uuid_a || !uuid_b) {
82 goto end;
83 }
84
85 for (i = 0; i < 16; i++) {
86 if (uuid_a[i] != uuid_b[i]) {
87 goto end;
88 }
89 }
90
91 ret = 1;
92 end:
93 return ret;
94 }
95
96 static
97 void validate_trace(char *parser_path, char *trace_path)
98 {
99 int ret = 0;
100 gint exit_status;
101 char *argv[] = {parser_path, trace_path, "-o", "dummy", NULL};
102
103 if (!parser_path || !trace_path) {
104 ret = -1;
105 goto result;
106 }
107
108 if (!g_spawn_sync(NULL,
109 argv,
110 NULL,
111 G_SPAWN_STDOUT_TO_DEV_NULL,
112 NULL,
113 NULL,
114 NULL,
115 NULL,
116 &exit_status,
117 NULL)) {
118 diag("Failed to spawn babeltrace.");
119 ret = -1;
120 goto result;
121 }
122
123 /* Replace by g_spawn_check_exit_status when we require glib >= 2.34 */
124 #ifdef G_OS_UNIX
125 ret = WIFEXITED(exit_status) ? WEXITSTATUS(exit_status) : -1;
126 #else
127 ret = exit_status;
128 #endif
129
130 if (ret != 0) {
131 diag("Babeltrace returned an error.");
132 goto result;
133 }
134
135 result:
136 ok(ret == 0, "Babeltrace could read the resulting trace");
137 }
138
139 static
140 void append_simple_event(struct bt_ctf_stream_class *stream_class,
141 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
142 {
143 /* Create and add a simple event class */
144 struct bt_ctf_event_class *simple_event_class =
145 bt_ctf_event_class_create("Simple Event");
146 struct bt_ctf_field_type *uint_12_type =
147 bt_ctf_field_type_integer_create(12);
148 struct bt_ctf_field_type *int_64_type =
149 bt_ctf_field_type_integer_create(64);
150 struct bt_ctf_field_type *float_type =
151 bt_ctf_field_type_floating_point_create();
152 struct bt_ctf_field_type *enum_type;
153 struct bt_ctf_field_type *enum_type_unsigned =
154 bt_ctf_field_type_enumeration_create(uint_12_type);
155 struct bt_ctf_field_type *event_context_type =
156 bt_ctf_field_type_structure_create();
157 struct bt_ctf_field_type *event_payload_type = NULL;
158 struct bt_ctf_field_type *returned_type;
159 struct bt_ctf_event *simple_event;
160 struct bt_ctf_field *integer_field;
161 struct bt_ctf_field *float_field;
162 struct bt_ctf_field *enum_field;
163 struct bt_ctf_field *enum_field_unsigned;
164 struct bt_ctf_field *enum_container_field;
165 const char *mapping_name_test = "truie";
166 const double double_test_value = 3.1415;
167 struct bt_ctf_field *enum_container_field_unsigned;
168 const char *mapping_name_negative_test = "negative_value";
169 const char *ret_char;
170 double ret_double;
171 int64_t ret_range_start_int64_t, ret_range_end_int64_t;
172 uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
173 struct bt_ctf_event_class *ret_event_class;
174 struct bt_ctf_field *packet_context;
175 struct bt_ctf_field *packet_context_field;
176 struct bt_ctf_field *stream_event_context;
177 struct bt_ctf_field *stream_event_context_field;
178 struct bt_ctf_field *event_context;
179 struct bt_ctf_field *event_context_field;
180 struct bt_ctf_field_type *ep_integer_field_type = NULL;
181 struct bt_ctf_field_type *ep_enum_field_type = NULL;
182 struct bt_ctf_field_type *ep_enum_field_unsigned_type = NULL;
183 struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
184 int ret;
185
186 ok(uint_12_type, "Create an unsigned integer type");
187
188 ok(!bt_ctf_field_type_integer_set_signed(int_64_type, 1),
189 "Set signed 64 bit integer signedness to true");
190 ok(int_64_type, "Create a signed integer type");
191 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
192
193 returned_type = bt_ctf_field_type_enumeration_get_container_field_type(enum_type);
194 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_field_type returns the right type");
195 ok(!bt_ctf_field_type_enumeration_create(enum_type),
196 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
197 bt_ctf_object_put_ref(returned_type);
198
199 bt_ctf_field_type_set_alignment(float_type, 32);
200 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
201 "bt_ctf_field_type_get_alignment returns a correct value");
202
203 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
204 "Set a floating point type's exponent digit count");
205 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
206 "Set a floating point type's mantissa digit count");
207
208 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
209 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
210 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
211 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
212
213 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
214 mapping_name_negative_test, -12345, 0) == 0,
215 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
216 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
217 "escaping; \"test\"", 1, 1) == 0,
218 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
219 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
220 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
221 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
222 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
223 "event clock int float", 5, 22) == 0,
224 "Accept enumeration mapping strings containing reserved keywords");
225 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
226 42, 42);
227 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
228 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
229 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
230 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
231 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
232 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
233 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
234
235 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
236 "enum_field") == 0, "Add signed enumeration field to event");
237
238 ok(bt_ctf_field_type_enumeration_signed_get_mapping_by_index(enum_type, 0, NULL,
239 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
240 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index handles a NULL string correctly");
241 ok(bt_ctf_field_type_enumeration_signed_get_mapping_by_index(enum_type, 0, &ret_char,
242 NULL, &ret_range_end_int64_t) == 0,
243 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index handles a NULL start correctly");
244 ok(bt_ctf_field_type_enumeration_signed_get_mapping_by_index(enum_type, 0, &ret_char,
245 &ret_range_start_int64_t, NULL) == 0,
246 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index handles a NULL end correctly");
247 /* Assumes entries are sorted by range_start values. */
248 ok(bt_ctf_field_type_enumeration_signed_get_mapping_by_index(enum_type, 6, &ret_char,
249 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
250 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index returns a value");
251 ok(!strcmp(ret_char, mapping_name_test),
252 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index returns a correct mapping name");
253 ok(ret_range_start_int64_t == 42,
254 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index returns a correct mapping start");
255 ok(ret_range_end_int64_t == 42,
256 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index returns a correct mapping end");
257
258 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned,
259 "escaping; \"test\"", 0, 0) == 0,
260 "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts enumeration mapping strings containing quotes");
261 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned,
262 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
263 "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts enumeration mapping strings containing special characters");
264 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned,
265 "event clock int float", 5, 22) == 0,
266 "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts enumeration mapping strings containing reserved keywords");
267 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned, mapping_name_test,
268 42, 42) == 0, "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts single-value ranges");
269 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned, mapping_name_test,
270 43, 51) == 0, "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts duplicate mapping names");
271 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned, "something",
272 7, 8) == 0, "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts overlapping enum entries");
273 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned, mapping_name_test,
274 55, 54), "bt_ctf_field_type_enumeration_unsigned_add_mapping rejects mapping where end < start");
275 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
276 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
277
278 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 6,
279 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
280
281 ok(bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index(enum_type_unsigned, 0, NULL,
282 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
283 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index handles a NULL string correctly");
284 ok(bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index(enum_type_unsigned, 0, &ret_char,
285 NULL, &ret_range_end_uint64_t) == 0,
286 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index handles a NULL start correctly");
287 ok(bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index(enum_type_unsigned, 0, &ret_char,
288 &ret_range_start_uint64_t, NULL) == 0,
289 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index handles a NULL end correctly");
290 ok(bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index(enum_type_unsigned, 4, &ret_char,
291 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
292 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index returns a value");
293 ok(!strcmp(ret_char, mapping_name_test),
294 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index returns a correct mapping name");
295 ok(ret_range_start_uint64_t == 42,
296 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index returns a correct mapping start");
297 ok(ret_range_end_uint64_t == 42,
298 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index returns a correct mapping end");
299
300 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
301 "integer_field");
302 bt_ctf_event_class_add_field(simple_event_class, float_type,
303 "float_field");
304
305 ret = bt_ctf_event_class_set_id(simple_event_class, 13);
306 BT_ASSERT(ret == 0);
307
308 /* Set an event context type which will contain a single integer. */
309 ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
310 "event_specific_context"),
311 "Add event specific context field");
312
313 ok(bt_ctf_event_class_set_context_field_type(NULL, event_context_type) < 0,
314 "bt_ctf_event_class_set_context_field_type handles a NULL event class correctly");
315 ok(!bt_ctf_event_class_set_context_field_type(simple_event_class, event_context_type),
316 "Set an event class' context type successfully");
317 returned_type = bt_ctf_event_class_get_context_field_type(simple_event_class);
318 ok(returned_type == event_context_type,
319 "bt_ctf_event_class_get_context_field_type returns the appropriate type");
320 bt_ctf_object_put_ref(returned_type);
321
322 ok(!bt_ctf_stream_class_add_event_class(stream_class, simple_event_class),
323 "Adding simple event class to stream class");
324
325 /*
326 * bt_ctf_stream_class_add_event_class() copies the field types
327 * of simple_event_class, so we retrieve the new ones to create
328 * the appropriate fields.
329 */
330 BT_CTF_OBJECT_PUT_REF_AND_RESET(event_context_type);
331 BT_CTF_OBJECT_PUT_REF_AND_RESET(event_payload_type);
332 event_payload_type = bt_ctf_event_class_get_payload_field_type(
333 simple_event_class);
334 BT_ASSERT(event_payload_type);
335 event_context_type = bt_ctf_event_class_get_context_field_type(
336 simple_event_class);
337 BT_ASSERT(event_context_type);
338 ep_integer_field_type =
339 bt_ctf_field_type_structure_get_field_type_by_name(
340 event_payload_type, "integer_field");
341 BT_ASSERT(ep_integer_field_type);
342 ep_enum_field_type =
343 bt_ctf_field_type_structure_get_field_type_by_name(
344 event_payload_type, "enum_field");
345 BT_ASSERT(ep_enum_field_type);
346 ep_enum_field_unsigned_type =
347 bt_ctf_field_type_structure_get_field_type_by_name(
348 event_payload_type, "enum_field_unsigned");
349 BT_ASSERT(ep_enum_field_unsigned_type);
350
351 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
352 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
353 ret_event_class = bt_ctf_stream_class_get_event_class_by_index(stream_class, 0);
354 ok(ret_event_class == simple_event_class,
355 "bt_ctf_stream_class_get_event_class returns the correct event class");
356 bt_ctf_object_put_ref(ret_event_class);
357 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class, 2),
358 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
359 ret_event_class =
360 bt_ctf_stream_class_get_event_class_by_id(stream_class, 13);
361 ok(ret_event_class == simple_event_class,
362 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
363 bt_ctf_object_put_ref(ret_event_class);
364
365 simple_event = bt_ctf_event_create(simple_event_class);
366 ok(simple_event,
367 "Instantiate an event containing a single integer field");
368
369 integer_field = bt_ctf_field_create(ep_integer_field_type);
370 bt_ctf_field_integer_unsigned_set_value(integer_field, 42);
371 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
372 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
373
374 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
375 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
376 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
377 "bt_ctf_field_floating_point_get_value returns a double value");
378 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
379 "bt_ctf_field_floating_point_get_value returns a correct value");
380
381 enum_field = bt_ctf_field_create(ep_enum_field_type);
382 BT_ASSERT(enum_field);
383
384 enum_container_field = bt_ctf_field_enumeration_get_container(enum_field);
385 ok(bt_ctf_field_integer_signed_set_value(
386 enum_container_field, -42) == 0,
387 "Set signed enumeration container value");
388 ret = bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
389 BT_ASSERT(!ret);
390 BT_CTF_OBJECT_PUT_REF_AND_RESET(iter);
391
392 enum_field_unsigned = bt_ctf_field_create(ep_enum_field_unsigned_type);
393 BT_ASSERT(enum_field_unsigned);
394 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
395 enum_field_unsigned);
396 ok(bt_ctf_field_integer_unsigned_set_value(
397 enum_container_field_unsigned, 42) == 0,
398 "Set unsigned enumeration container value");
399 ret = bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
400 enum_field_unsigned);
401 BT_ASSERT(!ret);
402
403 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
404
405 /* Populate stream event context */
406 stream_event_context =
407 bt_ctf_event_get_stream_event_context(simple_event);
408 BT_ASSERT(stream_event_context);
409 stream_event_context_field = bt_ctf_field_structure_get_field_by_name(
410 stream_event_context, "common_event_context");
411 bt_ctf_field_integer_unsigned_set_value(stream_event_context_field, 42);
412
413 /* Populate the event's context */
414 event_context = bt_ctf_event_get_context(simple_event);
415 ok(event_context,
416 "bt_ctf_event_get_context returns a field");
417 returned_type = bt_ctf_field_get_type(event_context);
418 ok(returned_type == event_context_type,
419 "bt_ctf_event_get_context returns a field of the appropriate type");
420 event_context_field = bt_ctf_field_structure_get_field_by_name(event_context,
421 "event_specific_context");
422 ok(!bt_ctf_field_integer_unsigned_set_value(event_context_field, 1234),
423 "Successfully set an event context's value");
424 ok(!bt_ctf_event_set_context(simple_event, event_context),
425 "Set an event context successfully");
426
427 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
428 "Append simple event to trace stream");
429
430 packet_context = bt_ctf_stream_get_packet_context(stream);
431 ok(packet_context,
432 "bt_ctf_stream_get_packet_context returns a packet context");
433
434 packet_context_field = bt_ctf_field_structure_get_field_by_name(packet_context,
435 "packet_size");
436 ok(packet_context_field,
437 "Packet context contains the default packet_size field.");
438 bt_ctf_object_put_ref(packet_context_field);
439 packet_context_field = bt_ctf_field_structure_get_field_by_name(packet_context,
440 "custom_packet_context_field");
441 ok(bt_ctf_field_integer_unsigned_set_value(packet_context_field, 8) == 0,
442 "Custom packet context field value successfully set.");
443
444 ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0,
445 "Successfully set a stream's packet context");
446
447 ok(bt_ctf_stream_flush(stream) == 0,
448 "Flush trace stream with one event");
449
450 bt_ctf_object_put_ref(simple_event_class);
451 bt_ctf_object_put_ref(simple_event);
452 bt_ctf_object_put_ref(uint_12_type);
453 bt_ctf_object_put_ref(int_64_type);
454 bt_ctf_object_put_ref(float_type);
455 bt_ctf_object_put_ref(enum_type);
456 bt_ctf_object_put_ref(enum_type_unsigned);
457 bt_ctf_object_put_ref(returned_type);
458 bt_ctf_object_put_ref(event_context_type);
459 bt_ctf_object_put_ref(integer_field);
460 bt_ctf_object_put_ref(float_field);
461 bt_ctf_object_put_ref(enum_field);
462 bt_ctf_object_put_ref(enum_field_unsigned);
463 bt_ctf_object_put_ref(enum_container_field);
464 bt_ctf_object_put_ref(enum_container_field_unsigned);
465 bt_ctf_object_put_ref(packet_context);
466 bt_ctf_object_put_ref(packet_context_field);
467 bt_ctf_object_put_ref(stream_event_context);
468 bt_ctf_object_put_ref(stream_event_context_field);
469 bt_ctf_object_put_ref(event_context);
470 bt_ctf_object_put_ref(event_context_field);
471 bt_ctf_object_put_ref(event_payload_type);
472 bt_ctf_object_put_ref(ep_integer_field_type);
473 bt_ctf_object_put_ref(ep_enum_field_type);
474 bt_ctf_object_put_ref(ep_enum_field_unsigned_type);
475 bt_ctf_object_put_ref(iter);
476 }
477
478 static
479 void append_complex_event(struct bt_ctf_stream_class *stream_class,
480 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
481 {
482 int i;
483 struct event_class_attrs_counts ;
484 const char *complex_test_event_string = "Complex Test Event";
485 const char *test_string_1 = "Test ";
486 const char *test_string_2 = "string ";
487 const char *test_string_3 = "abcdefghi";
488 const char *test_string_4 = "abcd\0efg\0hi";
489 const char *test_string_cat = "Test string abcdeefg";
490 struct bt_ctf_field_type *uint_35_type =
491 bt_ctf_field_type_integer_create(35);
492 struct bt_ctf_field_type *int_16_type =
493 bt_ctf_field_type_integer_create(16);
494 struct bt_ctf_field_type *uint_3_type =
495 bt_ctf_field_type_integer_create(3);
496 struct bt_ctf_field_type *enum_variant_type =
497 bt_ctf_field_type_enumeration_create(uint_3_type);
498 struct bt_ctf_field_type *variant_type =
499 bt_ctf_field_type_variant_create(enum_variant_type,
500 "variant_selector");
501 struct bt_ctf_field_type *string_type =
502 bt_ctf_field_type_string_create();
503 struct bt_ctf_field_type *sequence_type;
504 struct bt_ctf_field_type *array_type;
505 struct bt_ctf_field_type *inner_structure_type =
506 bt_ctf_field_type_structure_create();
507 struct bt_ctf_field_type *complex_structure_type =
508 bt_ctf_field_type_structure_create();
509 struct bt_ctf_field_type *ret_field_type;
510 struct bt_ctf_event_class *event_class;
511 struct bt_ctf_event *event;
512 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
513 *inner_structure_field, *complex_structure_field,
514 *a_sequence_field, *enum_variant_field, *enum_container_field,
515 *variant_field, *an_array_field, *stream_event_ctx_field,
516 *stream_event_ctx_int_field;
517 uint64_t ret_unsigned_int;
518 int64_t ret_signed_int;
519 const char *ret_string;
520 struct bt_ctf_stream_class *ret_stream_class;
521 struct bt_ctf_event_class *ret_event_class;
522 struct bt_ctf_field *packet_context, *packet_context_field;
523
524 ok(bt_ctf_field_type_set_alignment(int_16_type, 0),
525 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
526 ok(bt_ctf_field_type_set_alignment(int_16_type, 3),
527 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
528 ok(bt_ctf_field_type_set_alignment(int_16_type, 24),
529 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
530 ok(!bt_ctf_field_type_set_alignment(int_16_type, 4),
531 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
532 ok(!bt_ctf_field_type_set_alignment(int_16_type, 32),
533 "Set alignment of signed 16 bit integer to 32");
534 ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1),
535 "Set integer signedness to true");
536 ok(!bt_ctf_field_type_integer_set_base(uint_35_type,
537 BT_CTF_INTEGER_BASE_HEXADECIMAL),
538 "Set signed 16 bit integer base to hexadecimal");
539
540 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
541 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
542 "seq_len");
543
544 ret_field_type = bt_ctf_field_type_array_get_element_field_type(
545 array_type);
546 ok(ret_field_type == int_16_type,
547 "bt_ctf_field_type_array_get_element_field_type returns the correct type");
548 bt_ctf_object_put_ref(ret_field_type);
549
550 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
551 "bt_ctf_field_type_array_get_length returns the correct length");
552
553 ok(bt_ctf_field_type_structure_add_field(inner_structure_type,
554 inner_structure_type, "yes"), "Cannot add self to structure");
555 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type,
556 uint_35_type, "seq_len"), "Add seq_len field to inner structure");
557 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type,
558 sequence_type, "a_sequence"), "Add a_sequence field to inner structure");
559 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type,
560 array_type, "an_array"), "Add an_array field to inner structure");
561
562 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
563 "UINT3_TYPE", 0, 0);
564 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
565 "INT16_TYPE", 1, 1);
566 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
567 "UINT35_TYPE", 2, 7);
568
569 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
570 "An unknown entry"), "Reject a variant field based on an unknown tag value");
571 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
572 "UINT3_TYPE") == 0, "Add a field to a variant");
573 ok(!bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
574 "INT16_TYPE"), "Add INT16_TYPE field to variant");
575 ok(!bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
576 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
577
578 ret_field_type = bt_ctf_field_type_variant_get_tag_field_type(variant_type);
579 ok(ret_field_type == enum_variant_type,
580 "bt_ctf_field_type_variant_get_tag_field_type returns a correct tag type");
581 bt_ctf_object_put_ref(ret_field_type);
582
583 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
584 ok(ret_string ? !strcmp(ret_string, "variant_selector") : 0,
585 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
586 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
587 variant_type, "INT16_TYPE");
588 ok(ret_field_type == int_16_type,
589 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
590 bt_ctf_object_put_ref(ret_field_type);
591
592 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
593 "bt_ctf_field_type_variant_get_field_count returns the correct count");
594
595 ok(bt_ctf_field_type_variant_get_field_by_index(variant_type, NULL, &ret_field_type, 0) == 0,
596 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
597 bt_ctf_object_put_ref(ret_field_type);
598 ok(bt_ctf_field_type_variant_get_field_by_index(variant_type, &ret_string, NULL, 0) == 0,
599 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
600 ok(bt_ctf_field_type_variant_get_field_by_index(variant_type, &ret_string, &ret_field_type, 1) == 0,
601 "bt_ctf_field_type_variant_get_field returns a field");
602 ok(!strcmp("INT16_TYPE", ret_string),
603 "bt_ctf_field_type_variant_get_field returns a correct field name");
604 ok(ret_field_type == int_16_type,
605 "bt_ctf_field_type_variant_get_field returns a correct field type");
606 bt_ctf_object_put_ref(ret_field_type);
607
608 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type,
609 enum_variant_type, "variant_selector"),
610 "Add variant_selector field to complex structure");
611 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type,
612 string_type, "string"), "Add `string` field to complex structure");
613 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type,
614 variant_type, "variant_value"),
615 "Add variant_value field to complex structure");
616 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type,
617 inner_structure_type, "inner_structure"),
618 "Add inner_structure field to complex structure");
619
620 event_class = bt_ctf_event_class_create(complex_test_event_string);
621 ok(event_class, "Create an event class");
622 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
623 "Reject addition of a field with an empty name to an event");
624 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
625 "Reject addition of a field with a NULL type to an event");
626 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
627 "int"),
628 "Reject addition of a type with an illegal name to an event");
629 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
630 "uint_35") == 0,
631 "Add field of type unsigned integer to an event");
632 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
633 "int_16") == 0, "Add field of type signed integer to an event");
634 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
635 "complex_structure") == 0,
636 "Add composite structure to an event");
637
638 ret_string = bt_ctf_event_class_get_name(event_class);
639 ok(!strcmp(ret_string, complex_test_event_string),
640 "bt_ctf_event_class_get_name returns a correct name");
641 ok(bt_ctf_event_class_get_id(event_class) < 0,
642 "bt_ctf_event_class_get_id returns a negative value when not set");
643 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
644 "bt_ctf_event_class_set_id handles NULL correctly");
645 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
646 "Set an event class' id");
647 ok(bt_ctf_event_class_get_id(event_class) == 42,
648 "bt_ctf_event_class_get_id returns the correct value");
649
650 /* Test event class attributes */
651 ok(bt_ctf_event_class_get_log_level(event_class) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED,
652 "event class has the expected initial log level");
653 ok(!bt_ctf_event_class_get_emf_uri(event_class),
654 "as expected, event class has no initial EMF URI");
655 ok(bt_ctf_event_class_set_log_level(NULL, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO),
656 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
657 ok(bt_ctf_event_class_set_log_level(event_class, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN),
658 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
659 ok(!bt_ctf_event_class_set_log_level(event_class, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO),
660 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
661 ok(bt_ctf_event_class_get_log_level(event_class) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO,
662 "bt_ctf_event_class_get_log_level returns the expected log level");
663 ok(bt_ctf_event_class_set_emf_uri(NULL, "http://diamon.org/babeltrace/"),
664 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
665 ok(!bt_ctf_event_class_set_emf_uri(event_class, "http://diamon.org/babeltrace/"),
666 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
667 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class), "http://diamon.org/babeltrace/") == 0,
668 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
669 ok(!bt_ctf_event_class_set_emf_uri(event_class, NULL),
670 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
671 ok(!bt_ctf_event_class_get_emf_uri(event_class),
672 "as expected, event class has no EMF URI after reset");
673
674 /* Add event class to the stream class */
675 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
676 "Reject addition of NULL event class to a stream class");
677 ok(bt_ctf_stream_class_add_event_class(stream_class,
678 event_class) == 0, "Add an event class to stream class");
679
680 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
681 ok(ret_stream_class == stream_class,
682 "bt_ctf_event_class_get_stream_class returns the correct stream class");
683 bt_ctf_object_put_ref(ret_stream_class);
684
685 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
686 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
687 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
688 "complex_structure");
689 bt_ctf_object_put_ref(ret_field_type);
690
691 event = bt_ctf_event_create(event_class);
692 ok(event, "Instanciate a complex event");
693
694 ret_event_class = bt_ctf_event_get_class(event);
695 ok(ret_event_class == event_class,
696 "bt_ctf_event_get_class returns the correct event class");
697 bt_ctf_object_put_ref(ret_event_class);
698
699 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
700 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
701 bt_ctf_field_integer_unsigned_set_value(uint_35_field, 0x0DDF00D);
702 ok(bt_ctf_field_integer_unsigned_get_value(uint_35_field,
703 &ret_unsigned_int) == 0,
704 "bt_ctf_field_integer_unsigned_get_value succeeds after setting a value");
705 ok(ret_unsigned_int == 0x0DDF00D,
706 "bt_ctf_field_integer_unsigned_get_value returns the correct value");
707 bt_ctf_object_put_ref(uint_35_field);
708
709 int_16_field = bt_ctf_event_get_payload(event, "int_16");
710 bt_ctf_field_integer_signed_set_value(int_16_field, -12345);
711 ok(bt_ctf_field_integer_signed_get_value(int_16_field,
712 &ret_signed_int) == 0,
713 "bt_ctf_field_integer_signed_get_value succeeds after setting a value");
714 ok(ret_signed_int == -12345,
715 "bt_ctf_field_integer_signed_get_value returns the correct value");
716 bt_ctf_object_put_ref(int_16_field);
717
718 complex_structure_field = bt_ctf_event_get_payload(event,
719 "complex_structure");
720
721 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
722 complex_structure_field, 3);
723 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
724 bt_ctf_object_put_ref(inner_structure_field);
725 bt_ctf_object_put_ref(ret_field_type);
726
727 inner_structure_field = bt_ctf_field_structure_get_field_by_name(
728 complex_structure_field, "inner_structure");
729 a_string_field = bt_ctf_field_structure_get_field_by_name(
730 complex_structure_field, "string");
731 enum_variant_field = bt_ctf_field_structure_get_field_by_name(
732 complex_structure_field, "variant_selector");
733 variant_field = bt_ctf_field_structure_get_field_by_name(
734 complex_structure_field, "variant_value");
735 uint_35_field = bt_ctf_field_structure_get_field_by_name(
736 inner_structure_field, "seq_len");
737 a_sequence_field = bt_ctf_field_structure_get_field_by_name(
738 inner_structure_field, "a_sequence");
739 an_array_field = bt_ctf_field_structure_get_field_by_name(
740 inner_structure_field, "an_array");
741
742 enum_container_field = bt_ctf_field_enumeration_get_container(
743 enum_variant_field);
744 bt_ctf_field_integer_unsigned_set_value(enum_container_field, 1);
745 int_16_field = bt_ctf_field_variant_get_field(variant_field,
746 enum_variant_field);
747 bt_ctf_field_integer_signed_set_value(int_16_field, -200);
748 bt_ctf_object_put_ref(int_16_field);
749 bt_ctf_field_string_set_value(a_string_field,
750 test_string_1);
751 ok(!bt_ctf_field_string_append(a_string_field, test_string_2),
752 "bt_ctf_field_string_append succeeds");
753 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5),
754 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
755 ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[5], 3),
756 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
757 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0),
758 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
759
760 ret_string = bt_ctf_field_string_get_value(a_string_field);
761 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
762 ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0,
763 "bt_ctf_field_string_get_value returns a correct value");
764 bt_ctf_field_integer_unsigned_set_value(uint_35_field,
765 SEQUENCE_TEST_LENGTH);
766
767 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
768 variant_type, enum_variant_field);
769 ok(ret_field_type == int_16_type,
770 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
771
772 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
773 uint_35_field) == 0, "Set a sequence field's length");
774
775 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
776 int_16_field = bt_ctf_field_sequence_get_field(
777 a_sequence_field, i);
778 bt_ctf_field_integer_signed_set_value(int_16_field, 4 - i);
779 bt_ctf_object_put_ref(int_16_field);
780 }
781
782 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
783 int_16_field = bt_ctf_field_array_get_field(
784 an_array_field, i);
785 bt_ctf_field_integer_signed_set_value(int_16_field, i);
786 bt_ctf_object_put_ref(int_16_field);
787 }
788
789 stream_event_ctx_field = bt_ctf_event_get_stream_event_context(event);
790 BT_ASSERT(stream_event_ctx_field);
791 stream_event_ctx_int_field = bt_ctf_field_structure_get_field_by_name(
792 stream_event_ctx_field, "common_event_context");
793 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream_event_ctx_field);
794 bt_ctf_field_integer_unsigned_set_value(stream_event_ctx_int_field, 17);
795 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream_event_ctx_int_field);
796
797 bt_ctf_clock_set_time(clock, ++current_time);
798 ok(bt_ctf_stream_append_event(stream, event) == 0,
799 "Append a complex event to a stream");
800
801 /*
802 * Populate the custom packet context field with a dummy value
803 * otherwise flush will fail.
804 */
805 packet_context = bt_ctf_stream_get_packet_context(stream);
806 packet_context_field = bt_ctf_field_structure_get_field_by_name(packet_context,
807 "custom_packet_context_field");
808 bt_ctf_field_integer_unsigned_set_value(packet_context_field, 1);
809
810 ok(bt_ctf_stream_flush(stream) == 0,
811 "Flush a stream containing a complex event");
812
813 bt_ctf_object_put_ref(uint_35_field);
814 bt_ctf_object_put_ref(a_string_field);
815 bt_ctf_object_put_ref(inner_structure_field);
816 bt_ctf_object_put_ref(complex_structure_field);
817 bt_ctf_object_put_ref(a_sequence_field);
818 bt_ctf_object_put_ref(an_array_field);
819 bt_ctf_object_put_ref(enum_variant_field);
820 bt_ctf_object_put_ref(enum_container_field);
821 bt_ctf_object_put_ref(variant_field);
822 bt_ctf_object_put_ref(packet_context_field);
823 bt_ctf_object_put_ref(packet_context);
824 bt_ctf_object_put_ref(uint_35_type);
825 bt_ctf_object_put_ref(int_16_type);
826 bt_ctf_object_put_ref(string_type);
827 bt_ctf_object_put_ref(sequence_type);
828 bt_ctf_object_put_ref(array_type);
829 bt_ctf_object_put_ref(inner_structure_type);
830 bt_ctf_object_put_ref(complex_structure_type);
831 bt_ctf_object_put_ref(uint_3_type);
832 bt_ctf_object_put_ref(enum_variant_type);
833 bt_ctf_object_put_ref(variant_type);
834 bt_ctf_object_put_ref(ret_field_type);
835 bt_ctf_object_put_ref(event_class);
836 bt_ctf_object_put_ref(event);
837 }
838
839 static
840 void type_field_tests()
841 {
842 struct bt_ctf_field *uint_12;
843 struct bt_ctf_field *int_16;
844 struct bt_ctf_field *string;
845 struct bt_ctf_field_type *composite_structure_type;
846 struct bt_ctf_field_type *structure_seq_type;
847 struct bt_ctf_field_type *string_type;
848 struct bt_ctf_field_type *sequence_type;
849 struct bt_ctf_field_type *uint_8_type;
850 struct bt_ctf_field_type *int_16_type;
851 struct bt_ctf_field_type *uint_12_type =
852 bt_ctf_field_type_integer_create(12);
853 struct bt_ctf_field_type *enumeration_type;
854 struct bt_ctf_field_type *returned_type;
855 const char *ret_string;
856
857 ok(uint_12_type, "Create an unsigned integer type");
858 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
859 BT_CTF_INTEGER_BASE_BINARY) == 0,
860 "Set integer type's base as binary");
861 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
862 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
863 "Set integer type's base as decimal");
864 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
865 BT_CTF_INTEGER_BASE_UNKNOWN),
866 "Reject integer type's base set as unknown");
867 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
868 BT_CTF_INTEGER_BASE_OCTAL) == 0,
869 "Set integer type's base as octal");
870 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
871 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
872 "Set integer type's base as hexadecimal");
873 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
874 "Reject unknown integer base value");
875 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
876 "Set integer type signedness to signed");
877 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
878 "Set integer type signedness to unsigned");
879 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
880 "bt_ctf_field_type_integer_get_size returns a correct value");
881 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
882 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
883
884 ok(bt_ctf_field_type_set_byte_order(NULL,
885 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
886 "bt_ctf_field_type_set_byte_order handles NULL correctly");
887 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
888 (enum bt_ctf_byte_order) 42) < 0,
889 "bt_ctf_field_type_set_byte_order rejects invalid values");
890 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
891 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
892 "Set an integer's byte order to little endian");
893 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
894 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
895 "Set an integer's byte order to big endian");
896 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
897 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
898 "bt_ctf_field_type_get_byte_order returns a correct value");
899
900 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
901 BT_CTF_FIELD_TYPE_ID_INTEGER,
902 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
903
904 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
905 BT_CTF_INTEGER_BASE_HEXADECIMAL,
906 "bt_ctf_field_type_integer_get_base returns a correct value");
907
908 ok(bt_ctf_field_type_integer_set_encoding(NULL,
909 BT_CTF_STRING_ENCODING_ASCII) < 0,
910 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
911 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
912 (enum bt_ctf_string_encoding) 123) < 0,
913 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
914 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
915 BT_CTF_STRING_ENCODING_UTF8) == 0,
916 "Set integer type encoding to UTF8");
917 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) ==
918 BT_CTF_STRING_ENCODING_UTF8,
919 "bt_ctf_field_type_integer_get_encoding returns a correct value");
920
921 int_16_type = bt_ctf_field_type_integer_create(16);
922 BT_ASSERT(int_16_type);
923 ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1),
924 "Set signedness of 16 bit integer to true");
925 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
926 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
927 uint_8_type = bt_ctf_field_type_integer_create(8);
928 sequence_type =
929 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
930 ok(sequence_type, "Create a sequence of int16_t type");
931 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
932 BT_CTF_FIELD_TYPE_ID_SEQUENCE,
933 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
934
935 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
936 sequence_type);
937 ok(!strcmp(ret_string, "seq_len"),
938 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
939 returned_type = bt_ctf_field_type_sequence_get_element_field_type(
940 sequence_type);
941 ok(returned_type == int_16_type,
942 "bt_ctf_field_type_sequence_get_element_field_type returns the correct type");
943 bt_ctf_object_put_ref(returned_type);
944
945 string_type = bt_ctf_field_type_string_create();
946 ok(string_type, "Create a string type");
947 ok(bt_ctf_field_type_string_set_encoding(string_type,
948 BT_CTF_STRING_ENCODING_NONE),
949 "Reject invalid \"None\" string encoding");
950 ok(bt_ctf_field_type_string_set_encoding(string_type,
951 42),
952 "Reject invalid string encoding");
953 ok(bt_ctf_field_type_string_set_encoding(string_type,
954 BT_CTF_STRING_ENCODING_ASCII) == 0,
955 "Set string encoding to ASCII");
956
957 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
958 BT_CTF_STRING_ENCODING_ASCII,
959 "bt_ctf_field_type_string_get_encoding returns the correct value");
960
961 structure_seq_type = bt_ctf_field_type_structure_create();
962 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
963 BT_CTF_FIELD_TYPE_ID_STRUCT,
964 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
965 ok(structure_seq_type, "Create a structure type");
966 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
967 uint_8_type, "seq_len") == 0,
968 "Add a uint8_t type to a structure");
969 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
970 sequence_type, "a_sequence") == 0,
971 "Add a sequence type to a structure");
972
973 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
974 "bt_ctf_field_type_structure_get_field_count returns a correct value");
975
976 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
977 NULL, &returned_type, 1) == 0,
978 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
979 bt_ctf_object_put_ref(returned_type);
980 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
981 &ret_string, NULL, 1) == 0,
982 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
983 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
984 &ret_string, &returned_type, 1) == 0,
985 "bt_ctf_field_type_structure_get_field returns a field");
986 ok(!strcmp(ret_string, "a_sequence"),
987 "bt_ctf_field_type_structure_get_field returns a correct field name");
988 ok(returned_type == sequence_type,
989 "bt_ctf_field_type_structure_get_field returns a correct field type");
990 bt_ctf_object_put_ref(returned_type);
991
992 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
993 structure_seq_type, "a_sequence");
994 ok(returned_type == sequence_type,
995 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
996 bt_ctf_object_put_ref(returned_type);
997
998 composite_structure_type = bt_ctf_field_type_structure_create();
999 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1000 string_type, "a_string") == 0,
1001 "Add a string type to a structure");
1002 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1003 structure_seq_type, "inner_structure") == 0,
1004 "Add a structure type to a structure");
1005
1006 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1007 structure_seq_type, "a_sequence");
1008 ok(returned_type == sequence_type,
1009 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1010 bt_ctf_object_put_ref(returned_type);
1011
1012 int_16 = bt_ctf_field_create(int_16_type);
1013 ok(int_16, "Instanciate a signed 16-bit integer");
1014 uint_12 = bt_ctf_field_create(uint_12_type);
1015 ok(uint_12, "Instanciate an unsigned 12-bit integer");
1016 returned_type = bt_ctf_field_get_type(int_16);
1017 ok(returned_type == int_16_type,
1018 "bt_ctf_field_get_type returns the correct type");
1019
1020 /* Can't modify types after instanciating them */
1021 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1022 BT_CTF_INTEGER_BASE_DECIMAL),
1023 "Check an integer type' base can't be modified after instanciation");
1024 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
1025 "Check an integer type's signedness can't be modified after instanciation");
1026
1027 /* Check overflows are properly tested for */
1028 ok(bt_ctf_field_integer_signed_set_value(int_16, -32768) == 0,
1029 "Check -32768 is allowed for a signed 16-bit integer");
1030 ok(bt_ctf_field_integer_signed_set_value(int_16, 32767) == 0,
1031 "Check 32767 is allowed for a signed 16-bit integer");
1032 ok(bt_ctf_field_integer_signed_set_value(int_16, -42) == 0,
1033 "Check -42 is allowed for a signed 16-bit integer");
1034
1035 ok(bt_ctf_field_integer_unsigned_set_value(uint_12, 4095) == 0,
1036 "Check 4095 is allowed for an unsigned 12-bit integer");
1037 ok(bt_ctf_field_integer_unsigned_set_value(uint_12, 0) == 0,
1038 "Check 0 is allowed for an unsigned 12-bit integer");
1039
1040 string = bt_ctf_field_create(string_type);
1041 ok(string, "Instanciate a string field");
1042 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
1043 "Set a string's value");
1044
1045 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
1046 ok(enumeration_type,
1047 "Create an enumeration type with an unsigned 12-bit integer as container");
1048
1049 bt_ctf_object_put_ref(string);
1050 bt_ctf_object_put_ref(uint_12);
1051 bt_ctf_object_put_ref(int_16);
1052 bt_ctf_object_put_ref(composite_structure_type);
1053 bt_ctf_object_put_ref(structure_seq_type);
1054 bt_ctf_object_put_ref(string_type);
1055 bt_ctf_object_put_ref(sequence_type);
1056 bt_ctf_object_put_ref(uint_8_type);
1057 bt_ctf_object_put_ref(int_16_type);
1058 bt_ctf_object_put_ref(uint_12_type);
1059 bt_ctf_object_put_ref(enumeration_type);
1060 bt_ctf_object_put_ref(returned_type);
1061 }
1062
1063 static
1064 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
1065 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
1066 {
1067 /*
1068 * Append enough events to force the underlying packet to be resized.
1069 * Also tests that a new event can be declared after a stream has been
1070 * instantiated and used/flushed.
1071 */
1072 int ret = 0;
1073 int i;
1074 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
1075 "Spammy_Event");
1076 struct bt_ctf_field_type *integer_type =
1077 bt_ctf_field_type_integer_create(17);
1078 struct bt_ctf_field_type *string_type =
1079 bt_ctf_field_type_string_create();
1080 struct bt_ctf_event *event = NULL;
1081 struct bt_ctf_field *ret_field = NULL;
1082 struct bt_ctf_field_type *ret_field_type = NULL;
1083 uint64_t ret_uint64;
1084 int events_appended = 0;
1085 struct bt_ctf_field *packet_context = NULL,
1086 *packet_context_field = NULL, *stream_event_context = NULL;
1087 struct bt_ctf_field_type *ep_field_1_type = NULL;
1088 struct bt_ctf_field_type *ep_a_string_type = NULL;
1089 struct bt_ctf_field_type *ep_type = NULL;
1090
1091 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
1092 "field_1");
1093 ret |= bt_ctf_event_class_add_field(event_class, string_type,
1094 "a_string");
1095 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
1096 ok(ret == 0, "Add a new event class to a stream class after writing an event");
1097 if (ret) {
1098 goto end;
1099 }
1100
1101 /*
1102 * bt_ctf_stream_class_add_event_class() copies the field types
1103 * of event_class, so we retrieve the new ones to create the
1104 * appropriate fields.
1105 */
1106 ep_type = bt_ctf_event_class_get_payload_field_type(event_class);
1107 BT_ASSERT(ep_type);
1108 ep_field_1_type = bt_ctf_field_type_structure_get_field_type_by_name(
1109 ep_type, "field_1");
1110 BT_ASSERT(ep_field_1_type);
1111 ep_a_string_type = bt_ctf_field_type_structure_get_field_type_by_name(
1112 ep_type, "a_string");
1113 BT_ASSERT(ep_a_string_type);
1114
1115 event = bt_ctf_event_create(event_class);
1116 ret_field = bt_ctf_event_get_payload(event, 0);
1117 ret_field_type = bt_ctf_field_get_type(ret_field);
1118 bt_ctf_object_put_ref(ret_field_type);
1119 bt_ctf_object_put_ref(ret_field);
1120 bt_ctf_object_put_ref(event);
1121
1122 for (i = 0; i < packet_resize_test_length; i++) {
1123 event = bt_ctf_event_create(event_class);
1124 struct bt_ctf_field *integer =
1125 bt_ctf_field_create(ep_field_1_type);
1126 struct bt_ctf_field *string =
1127 bt_ctf_field_create(ep_a_string_type);
1128
1129 ret |= bt_ctf_clock_set_time(clock, ++current_time);
1130 ret |= bt_ctf_field_integer_unsigned_set_value(integer, i);
1131 ret |= bt_ctf_event_set_payload(event, "field_1",
1132 integer);
1133 bt_ctf_object_put_ref(integer);
1134 ret |= bt_ctf_field_string_set_value(string, "This is a test");
1135 ret |= bt_ctf_event_set_payload(event, "a_string",
1136 string);
1137 bt_ctf_object_put_ref(string);
1138
1139 /* Populate stream event context */
1140 stream_event_context =
1141 bt_ctf_event_get_stream_event_context(event);
1142 integer = bt_ctf_field_structure_get_field_by_name(stream_event_context,
1143 "common_event_context");
1144 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream_event_context);
1145 ret |= bt_ctf_field_integer_unsigned_set_value(integer,
1146 i % 42);
1147 bt_ctf_object_put_ref(integer);
1148
1149 ret |= bt_ctf_stream_append_event(stream, event);
1150 bt_ctf_object_put_ref(event);
1151
1152 if (ret) {
1153 break;
1154 }
1155 }
1156
1157 events_appended = !!(i == packet_resize_test_length);
1158 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1159 ok(ret == 0 && ret_uint64 == 0,
1160 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1161 bt_ctf_stream_append_discarded_events(stream, 1000);
1162 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1163 ok(ret == 0 && ret_uint64 == 1000,
1164 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1165
1166 end:
1167 ok(events_appended, "Append 100 000 events to a stream");
1168
1169 /*
1170 * Populate the custom packet context field with a dummy value
1171 * otherwise flush will fail.
1172 */
1173 packet_context = bt_ctf_stream_get_packet_context(stream);
1174 packet_context_field = bt_ctf_field_structure_get_field_by_name(packet_context,
1175 "custom_packet_context_field");
1176 bt_ctf_field_integer_unsigned_set_value(packet_context_field, 2);
1177
1178 ok(bt_ctf_stream_flush(stream) == 0,
1179 "Flush a stream that forces a packet resize");
1180 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1181 ok(ret == 0 && ret_uint64 == 1000,
1182 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
1183 bt_ctf_object_put_ref(integer_type);
1184 bt_ctf_object_put_ref(string_type);
1185 bt_ctf_object_put_ref(packet_context);
1186 bt_ctf_object_put_ref(packet_context_field);
1187 bt_ctf_object_put_ref(stream_event_context);
1188 bt_ctf_object_put_ref(event_class);
1189 bt_ctf_object_put_ref(ep_field_1_type);
1190 bt_ctf_object_put_ref(ep_a_string_type);
1191 bt_ctf_object_put_ref(ep_type);
1192 }
1193
1194 static
1195 void test_empty_stream(struct bt_ctf_writer *writer)
1196 {
1197 int ret = 0;
1198 struct bt_ctf_trace *trace = NULL, *ret_trace = NULL;
1199 struct bt_ctf_stream_class *stream_class = NULL;
1200 struct bt_ctf_stream *stream = NULL;
1201
1202 trace = bt_ctf_writer_get_trace(writer);
1203 if (!trace) {
1204 diag("Failed to get trace from writer");
1205 ret = -1;
1206 goto end;
1207 }
1208
1209 stream_class = bt_ctf_stream_class_create("empty_stream");
1210 if (!stream_class) {
1211 diag("Failed to create stream class");
1212 ret = -1;
1213 goto end;
1214 }
1215
1216 ret = bt_ctf_stream_class_set_packet_context_type(stream_class, NULL);
1217 BT_ASSERT(ret == 0);
1218 ret = bt_ctf_stream_class_set_event_header_type(stream_class, NULL);
1219 BT_ASSERT(ret == 0);
1220
1221 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
1222 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
1223
1224 stream = bt_ctf_writer_create_stream(writer, stream_class);
1225 if (!stream) {
1226 diag("Failed to create writer stream");
1227 ret = -1;
1228 goto end;
1229 }
1230
1231 ret_trace = bt_ctf_stream_class_get_trace(stream_class);
1232 ok(ret_trace == trace,
1233 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
1234 end:
1235 ok(ret == 0,
1236 "Created a stream class with default attributes and an empty stream");
1237 bt_ctf_object_put_ref(trace);
1238 bt_ctf_object_put_ref(ret_trace);
1239 bt_ctf_object_put_ref(stream);
1240 bt_ctf_object_put_ref(stream_class);
1241 }
1242
1243 static
1244 void test_custom_event_header_stream(struct bt_ctf_writer *writer,
1245 struct bt_ctf_clock *clock)
1246 {
1247 int i, ret;
1248 struct bt_ctf_stream_class *stream_class = NULL;
1249 struct bt_ctf_stream *stream = NULL;
1250 struct bt_ctf_field_type *integer_type = NULL,
1251 *sequence_type = NULL, *event_header_type = NULL;
1252 struct bt_ctf_field *integer = NULL, *sequence = NULL,
1253 *event_header = NULL, *packet_header = NULL;
1254 struct bt_ctf_event_class *event_class = NULL;
1255 struct bt_ctf_event *event = NULL;
1256
1257 stream_class = bt_ctf_stream_class_create("custom_event_header_stream");
1258 if (!stream_class) {
1259 fail("Failed to create stream class");
1260 goto end;
1261 }
1262
1263 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
1264 if (ret) {
1265 fail("Failed to set stream class clock");
1266 goto end;
1267 }
1268
1269 /*
1270 * Customize event header to add an "seq_len" integer member
1271 * which will be used as the length of a sequence in an event of this
1272 * stream.
1273 */
1274 event_header_type = bt_ctf_stream_class_get_event_header_type(
1275 stream_class);
1276 if (!event_header_type) {
1277 fail("Failed to get event header type");
1278 goto end;
1279 }
1280
1281 integer_type = bt_ctf_field_type_integer_create(13);
1282 if (!integer_type) {
1283 fail("Failed to create length integer type");
1284 goto end;
1285 }
1286
1287 ret = bt_ctf_field_type_structure_add_field(event_header_type,
1288 integer_type, "seq_len");
1289 if (ret) {
1290 fail("Failed to add a new field to stream event header");
1291 goto end;
1292 }
1293
1294 event_class = bt_ctf_event_class_create("sequence_event");
1295 if (!event_class) {
1296 fail("Failed to create event class");
1297 goto end;
1298 }
1299
1300 /*
1301 * This event's payload will contain a sequence which references
1302 * stream.event.header.seq_len as its length field.
1303 */
1304 sequence_type = bt_ctf_field_type_sequence_create(integer_type,
1305 "stream.event.header.seq_len");
1306 if (!sequence_type) {
1307 fail("Failed to create a sequence");
1308 goto end;
1309 }
1310
1311 ret = bt_ctf_event_class_add_field(event_class, sequence_type,
1312 "some_sequence");
1313 if (ret) {
1314 fail("Failed to add a sequence to an event class");
1315 goto end;
1316 }
1317
1318 ret = bt_ctf_stream_class_add_event_class(stream_class, event_class);
1319 if (ret) {
1320 fail("Failed to add event class to stream class");
1321 goto end;
1322 }
1323
1324 stream = bt_ctf_writer_create_stream(writer, stream_class);
1325 if (!stream) {
1326 fail("Failed to create stream")
1327 goto end;
1328 }
1329
1330 /*
1331 * We have defined a custom packet header field. We have to populate it
1332 * explicitly.
1333 */
1334 packet_header = bt_ctf_stream_get_packet_header(stream);
1335 if (!packet_header) {
1336 fail("Failed to get stream packet header");
1337 goto end;
1338 }
1339
1340 integer = bt_ctf_field_structure_get_field_by_name(packet_header,
1341 "custom_trace_packet_header_field");
1342 if (!integer) {
1343 fail("Failed to retrieve custom_trace_packet_header_field");
1344 goto end;
1345 }
1346
1347 ret = bt_ctf_field_integer_unsigned_set_value(integer, 3487);
1348 if (ret) {
1349 fail("Failed to set custom_trace_packet_header_field value");
1350 goto end;
1351 }
1352 bt_ctf_object_put_ref(integer);
1353
1354 event = bt_ctf_event_create(event_class);
1355 if (!event) {
1356 fail("Failed to create event");
1357 goto end;
1358 }
1359
1360 event_header = bt_ctf_event_get_header(event);
1361 if (!event_header) {
1362 fail("Failed to get event header");
1363 goto end;
1364 }
1365
1366 integer = bt_ctf_field_structure_get_field_by_name(event_header,
1367 "seq_len");
1368 if (!integer) {
1369 fail("Failed to get seq_len field from event header");
1370 goto end;
1371 }
1372
1373 ret = bt_ctf_field_integer_unsigned_set_value(integer, 2);
1374 if (ret) {
1375 fail("Failed to set seq_len value in event header");
1376 goto end;
1377 }
1378
1379 /* Populate both sequence integer fields */
1380 sequence = bt_ctf_event_get_payload(event, "some_sequence");
1381 if (!sequence) {
1382 fail("Failed to retrieve sequence from event");
1383 goto end;
1384 }
1385
1386 ret = bt_ctf_field_sequence_set_length(sequence, integer);
1387 if (ret) {
1388 fail("Failed to set sequence length");
1389 goto end;
1390 }
1391 bt_ctf_object_put_ref(integer);
1392
1393 for (i = 0; i < 2; i++) {
1394 integer = bt_ctf_field_sequence_get_field(sequence, i);
1395 if (ret) {
1396 fail("Failed to retrieve sequence element");
1397 goto end;
1398 }
1399
1400 ret = bt_ctf_field_integer_unsigned_set_value(integer, i);
1401 if (ret) {
1402 fail("Failed to set sequence element value");
1403 goto end;
1404 }
1405
1406 bt_ctf_object_put_ref(integer);
1407 integer = NULL;
1408 }
1409
1410 ret = bt_ctf_stream_append_event(stream, event);
1411 if (ret) {
1412 fail("Failed to append event to stream");
1413 goto end;
1414 }
1415
1416 ret = bt_ctf_stream_flush(stream);
1417 if (ret) {
1418 fail("Failed to flush custom_event_header stream");
1419 }
1420 end:
1421 bt_ctf_object_put_ref(stream);
1422 bt_ctf_object_put_ref(stream_class);
1423 bt_ctf_object_put_ref(event_class);
1424 bt_ctf_object_put_ref(event);
1425 bt_ctf_object_put_ref(integer);
1426 bt_ctf_object_put_ref(sequence);
1427 bt_ctf_object_put_ref(event_header);
1428 bt_ctf_object_put_ref(packet_header);
1429 bt_ctf_object_put_ref(sequence_type);
1430 bt_ctf_object_put_ref(integer_type);
1431 bt_ctf_object_put_ref(event_header_type);
1432 }
1433
1434 static
1435 void test_instanciate_event_before_stream(struct bt_ctf_writer *writer,
1436 struct bt_ctf_clock *clock)
1437 {
1438 int ret = 0;
1439 struct bt_ctf_stream_class *stream_class = NULL;
1440 struct bt_ctf_stream *stream = NULL,
1441 *ret_stream = NULL;
1442 struct bt_ctf_event_class *event_class = NULL;
1443 struct bt_ctf_event *event = NULL;
1444 struct bt_ctf_field_type *integer_type = NULL;
1445 struct bt_ctf_field *payload_field = NULL;
1446 struct bt_ctf_field *integer = NULL;
1447
1448 stream_class = bt_ctf_stream_class_create("event_before_stream_test");
1449 if (!stream_class) {
1450 diag("Failed to create stream class");
1451 ret = -1;
1452 goto end;
1453 }
1454
1455 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
1456 if (ret) {
1457 diag("Failed to set stream class clock");
1458 goto end;
1459 }
1460
1461 event_class = bt_ctf_event_class_create("some_event_class_name");
1462 integer_type = bt_ctf_field_type_integer_create(32);
1463 if (!integer_type) {
1464 diag("Failed to create integer field type");
1465 ret = -1;
1466 goto end;
1467 }
1468
1469 ret = bt_ctf_event_class_add_field(event_class, integer_type,
1470 "integer_field");
1471 if (ret) {
1472 diag("Failed to add field to event class");
1473 goto end;
1474 }
1475
1476 ret = bt_ctf_stream_class_add_event_class(stream_class,
1477 event_class);
1478 if (ret) {
1479 diag("Failed to add event class to stream class");
1480 }
1481
1482 event = bt_ctf_event_create(event_class);
1483 if (!event) {
1484 diag("Failed to create event");
1485 ret = -1;
1486 goto end;
1487 }
1488
1489 payload_field = bt_ctf_event_get_payload_field(event);
1490 if (!payload_field) {
1491 diag("Failed to get event's payload field");
1492 ret = -1;
1493 goto end;
1494 }
1495
1496 integer = bt_ctf_field_structure_get_field_by_index(payload_field, 0);
1497 if (!integer) {
1498 diag("Failed to get integer field payload from event");
1499 ret = -1;
1500 goto end;
1501 }
1502
1503 ret = bt_ctf_field_integer_unsigned_set_value(integer, 1234);
1504 if (ret) {
1505 diag("Failed to set integer field value");
1506 goto end;
1507 }
1508
1509 stream = bt_ctf_writer_create_stream(writer, stream_class);
1510 if (!stream) {
1511 diag("Failed to create writer stream");
1512 ret = -1;
1513 goto end;
1514 }
1515
1516 ret = bt_ctf_stream_append_event(stream, event);
1517 if (ret) {
1518 diag("Failed to append event to stream");
1519 goto end;
1520 }
1521
1522 ret_stream = bt_ctf_event_get_stream(event);
1523 ok(ret_stream == stream,
1524 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
1525 end:
1526 ok(ret == 0,
1527 "Create an event before instanciating its associated stream");
1528 bt_ctf_object_put_ref(stream);
1529 bt_ctf_object_put_ref(ret_stream);
1530 bt_ctf_object_put_ref(stream_class);
1531 bt_ctf_object_put_ref(event_class);
1532 bt_ctf_object_put_ref(event);
1533 bt_ctf_object_put_ref(integer_type);
1534 bt_ctf_object_put_ref(integer);
1535 bt_ctf_object_put_ref(payload_field);
1536 }
1537
1538 static
1539 void append_existing_event_class(struct bt_ctf_stream_class *stream_class)
1540 {
1541 int ret;
1542 struct bt_ctf_event_class *event_class;
1543
1544 event_class = bt_ctf_event_class_create("Simple Event");
1545 BT_ASSERT(event_class);
1546 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class) == 0,
1547 "two event classes with the same name may cohabit within the same stream class");
1548 bt_ctf_object_put_ref(event_class);
1549
1550 event_class = bt_ctf_event_class_create("different name, ok");
1551 BT_ASSERT(event_class);
1552 ret = bt_ctf_event_class_set_id(event_class, 13);
1553 BT_ASSERT(ret == 0);
1554 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
1555 "two event classes with the same ID cannot cohabit within the same stream class");
1556 bt_ctf_object_put_ref(event_class);
1557 }
1558
1559 static
1560 void test_clock_utils(void)
1561 {
1562 int ret;
1563 struct bt_ctf_clock *clock = NULL;
1564
1565 clock = bt_ctf_clock_create("water");
1566 BT_ASSERT(clock);
1567 ret = bt_ctf_clock_set_offset_s(clock, 1234);
1568 BT_ASSERT(!ret);
1569 ret = bt_ctf_clock_set_offset(clock, 1000);
1570 BT_ASSERT(!ret);
1571 ret = bt_ctf_clock_set_frequency(clock, 1000000000);
1572 BT_ASSERT(!ret);
1573 ret = bt_ctf_clock_set_frequency(clock, 1534);
1574 BT_ASSERT(!ret);
1575
1576 BT_CTF_OBJECT_PUT_REF_AND_RESET(clock);
1577 }
1578
1579 int main(int argc, char **argv)
1580 {
1581 const char *env_resize_length;
1582 gchar *trace_path;
1583 gchar *metadata_path;
1584 const char *clock_name = "test_clock";
1585 const char *clock_description = "This is a test clock";
1586 const char *returned_clock_name;
1587 const char *returned_clock_description;
1588 const uint64_t frequency = 1123456789;
1589 const int64_t offset_s = 13515309;
1590 const int64_t offset = 1234567;
1591 int64_t get_offset_s,
1592 get_offset;
1593 const uint64_t precision = 10;
1594 const int is_absolute = 0xFF;
1595 char *metadata_string;
1596 struct bt_ctf_writer *writer;
1597 struct bt_utsname name = {"GNU/Linux", "testhost", "4.4.0-87-generic",
1598 "#110-Ubuntu SMP Tue Jul 18 12:55:35 UTC 2017", "x86_64"};
1599 struct bt_ctf_clock *clock, *ret_clock;
1600 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
1601 struct bt_ctf_stream *stream1;
1602 struct bt_ctf_stream *stream;
1603 const char *ret_string;
1604 const uint8_t *ret_uuid;
1605 bt_uuid_t tmp_uuid = { 0 };
1606 struct bt_ctf_field_type *packet_context_type,
1607 *packet_context_field_type,
1608 *packet_header_type,
1609 *packet_header_field_type,
1610 *integer_type,
1611 *stream_event_context_type,
1612 *ret_field_type,
1613 *event_header_field_type;
1614 struct bt_ctf_field *packet_header, *packet_header_field;
1615 struct bt_ctf_trace *trace;
1616 int ret;
1617
1618 if (argc < 2) {
1619 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
1620 return -1;
1621 }
1622
1623 env_resize_length = getenv("PACKET_RESIZE_TEST_LENGTH");
1624 if (env_resize_length) {
1625 packet_resize_test_length =
1626 (unsigned int) atoi(env_resize_length);
1627 }
1628
1629 plan_tests(NR_TESTS);
1630
1631 trace_path = g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL);
1632 if (!bt_mkdtemp(trace_path)) {
1633 perror("# perror");
1634 }
1635
1636 metadata_path = g_build_filename(trace_path, "metadata", NULL);
1637
1638 writer = bt_ctf_writer_create(trace_path);
1639 ok(writer, "bt_ctf_create succeeds in creating trace with path");
1640
1641 ok(!bt_ctf_writer_get_trace(NULL),
1642 "bt_ctf_writer_get_trace correctly handles NULL");
1643 trace = bt_ctf_writer_get_trace(writer);
1644 ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE),
1645 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE");
1646 ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_UNSPECIFIED),
1647 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED");
1648 ok(trace,
1649 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
1650 ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1651 "Set a trace's byte order to big endian");
1652 ok(bt_ctf_trace_get_native_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1653 "bt_ctf_trace_get_native_byte_order returns a correct endianness");
1654
1655 /* Add environment context to the trace */
1656 ok(bt_ctf_writer_add_environment_field(writer, "host", name.nodename) == 0,
1657 "Add host (%s) environment field to writer instance",
1658 name.nodename);
1659 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
1660 "test_value"),
1661 "bt_ctf_writer_add_environment_field error with NULL writer");
1662 ok(bt_ctf_writer_add_environment_field(writer, NULL,
1663 "test_value"),
1664 "bt_ctf_writer_add_environment_field error with NULL field name");
1665 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
1666 NULL),
1667 "bt_ctf_writer_add_environment_field error with NULL field value");
1668
1669 /* Test bt_ctf_trace_set_environment_field_integer */
1670 ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int",
1671 -194875),
1672 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
1673 ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875),
1674 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
1675 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
1676 -164973),
1677 "bt_ctf_trace_set_environment_field_integer succeeds");
1678
1679 /* Test bt_ctf_trace_set_environment_field_string */
1680 ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str",
1681 "yeah"),
1682 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
1683 ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"),
1684 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
1685 ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
1686 NULL),
1687 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
1688 ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
1689 "oh yeah"),
1690 "bt_ctf_trace_set_environment_field_string succeeds");
1691
1692 /* Test environment field replacement */
1693 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
1694 654321),
1695 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
1696
1697 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
1698 == 0, "Add sysname (%s) environment field to writer instance",
1699 name.sysname);
1700 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
1701 name.nodename) == 0,
1702 "Add nodename (%s) environment field to writer instance",
1703 name.nodename);
1704 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
1705 == 0, "Add release (%s) environment field to writer instance",
1706 name.release);
1707 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
1708 == 0, "Add version (%s) environment field to writer instance",
1709 name.version);
1710 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
1711 == 0, "Add machine (%s) environment field to writer istance",
1712 name.machine);
1713
1714 /* Define a clock and add it to the trace */
1715 ok(bt_ctf_clock_create("signed") == NULL,
1716 "Illegal clock name rejected");
1717 clock = bt_ctf_clock_create(clock_name);
1718 ok(clock, "Clock created sucessfully");
1719 returned_clock_name = bt_ctf_clock_get_name(clock);
1720 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
1721 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
1722 "Returned clock name is valid");
1723
1724 returned_clock_description = bt_ctf_clock_get_description(clock);
1725 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
1726 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
1727 "Clock description set successfully");
1728
1729 returned_clock_description = bt_ctf_clock_get_description(clock);
1730 ok(returned_clock_description,
1731 "bt_ctf_clock_get_description returns a description.");
1732 ok(returned_clock_description ?
1733 !strcmp(returned_clock_description, clock_description) : 0,
1734 "Returned clock description is valid");
1735
1736 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
1737 "bt_ctf_clock_get_frequency returns the correct default frequency");
1738 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
1739 "Set clock frequency");
1740 ok(bt_ctf_clock_get_frequency(clock) == frequency,
1741 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1742
1743 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
1744 "bt_ctf_clock_get_offset_s succeeds");
1745 ok(get_offset_s == DEFAULT_CLOCK_OFFSET_S,
1746 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
1747 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
1748 "Set clock offset (seconds)");
1749 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
1750 "bt_ctf_clock_get_offset_s succeeds");
1751 ok(get_offset_s == offset_s,
1752 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1753
1754 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
1755 "bt_ctf_clock_get_offset succeeds");
1756 ok(get_offset == DEFAULT_CLOCK_OFFSET,
1757 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
1758 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
1759 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
1760 "bt_ctf_clock_get_offset succeeds");
1761 ok(get_offset == offset,
1762 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
1763
1764 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
1765 "bt_ctf_clock_get_precision returns the correct default precision");
1766 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
1767 "Set clock precision");
1768 ok(bt_ctf_clock_get_precision(clock) == precision,
1769 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1770
1771 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
1772 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1773 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
1774 "Set clock absolute property");
1775 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
1776 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1777 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
1778 "Set clock time");
1779 ret_uuid = bt_ctf_clock_get_uuid(clock);
1780 ok(ret_uuid,
1781 "bt_ctf_clock_get_uuid returns a UUID");
1782 if (ret_uuid) {
1783 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
1784 /* Slightly modify UUID */
1785 tmp_uuid[sizeof(tmp_uuid) - 1]++;
1786 }
1787
1788 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
1789 "bt_ctf_clock_set_uuid sets a new uuid successfully");
1790 ret_uuid = bt_ctf_clock_get_uuid(clock);
1791 ok(ret_uuid,
1792 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
1793 ok(uuid_match(ret_uuid, tmp_uuid),
1794 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
1795
1796 /* Define a stream class */
1797 stream_class = bt_ctf_stream_class_create("test_stream");
1798 ret_string = bt_ctf_stream_class_get_name(stream_class);
1799 ok(ret_string && !strcmp(ret_string, "test_stream"),
1800 "bt_ctf_stream_class_get_name returns a correct stream class name");
1801
1802 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
1803 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1804 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
1805 "bt_ctf_stream_class_get_clock handles NULL correctly");
1806
1807 ok(stream_class, "Create stream class");
1808 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
1809 "Set a stream class' clock");
1810 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
1811 ok(ret_clock == clock,
1812 "bt_ctf_stream_class_get_clock returns a correct clock");
1813 bt_ctf_object_put_ref(ret_clock);
1814
1815 /* Test the event fields and event types APIs */
1816 type_field_tests();
1817
1818 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
1819 "bt_ctf_stream_class_get_id returns an error when no id is set");
1820 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
1821 "bt_ctf_stream_class_set_id handles NULL correctly");
1822 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
1823 "Set an stream class' id");
1824 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
1825 "bt_ctf_stream_class_get_id returns the correct value");
1826
1827 /* Validate default event header fields */
1828 ret_field_type = bt_ctf_stream_class_get_event_header_type(
1829 stream_class);
1830 ok(ret_field_type,
1831 "bt_ctf_stream_class_get_event_header_type returns an event header type");
1832 ok(bt_ctf_field_type_get_type_id(ret_field_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
1833 "Default event header type is a structure");
1834 event_header_field_type =
1835 bt_ctf_field_type_structure_get_field_type_by_name(
1836 ret_field_type, "id");
1837 ok(event_header_field_type,
1838 "Default event header type contains an \"id\" field");
1839 ok(bt_ctf_field_type_get_type_id(
1840 event_header_field_type) == BT_CTF_FIELD_TYPE_ID_INTEGER,
1841 "Default event header \"id\" field is an integer");
1842 bt_ctf_object_put_ref(event_header_field_type);
1843 event_header_field_type =
1844 bt_ctf_field_type_structure_get_field_type_by_name(
1845 ret_field_type, "timestamp");
1846 ok(event_header_field_type,
1847 "Default event header type contains a \"timestamp\" field");
1848 ok(bt_ctf_field_type_get_type_id(
1849 event_header_field_type) == BT_CTF_FIELD_TYPE_ID_INTEGER,
1850 "Default event header \"timestamp\" field is an integer");
1851 bt_ctf_object_put_ref(event_header_field_type);
1852 bt_ctf_object_put_ref(ret_field_type);
1853
1854 /* Add a custom trace packet header field */
1855 packet_header_type = bt_ctf_trace_get_packet_header_field_type(trace);
1856 ok(packet_header_type,
1857 "bt_ctf_trace_get_packet_header_field_type returns a packet header");
1858 ok(bt_ctf_field_type_get_type_id(packet_header_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
1859 "bt_ctf_trace_get_packet_header_field_type returns a packet header of type struct");
1860 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1861 packet_header_type, "magic");
1862 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
1863 bt_ctf_object_put_ref(ret_field_type);
1864 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1865 packet_header_type, "uuid");
1866 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
1867 bt_ctf_object_put_ref(ret_field_type);
1868 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1869 packet_header_type, "stream_id");
1870 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
1871 bt_ctf_object_put_ref(ret_field_type);
1872
1873 packet_header_field_type = bt_ctf_field_type_integer_create(22);
1874 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
1875 packet_header_field_type, "custom_trace_packet_header_field"),
1876 "Added a custom trace packet header field successfully");
1877
1878 ok(bt_ctf_trace_set_packet_header_field_type(NULL, packet_header_type) < 0,
1879 "bt_ctf_trace_set_packet_header_field_type handles a NULL trace correctly");
1880 ok(!bt_ctf_trace_set_packet_header_field_type(trace, packet_header_type),
1881 "Set a trace packet_header_type successfully");
1882
1883 /* Add a custom field to the stream class' packet context */
1884 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1885 ok(packet_context_type,
1886 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
1887 ok(bt_ctf_field_type_get_type_id(packet_context_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
1888 "Packet context is a structure");
1889
1890 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
1891 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
1892
1893 integer_type = bt_ctf_field_type_integer_create(32);
1894
1895 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
1896 integer_type) < 0,
1897 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
1898
1899 /* Create a "uint5_t" equivalent custom packet context field */
1900 packet_context_field_type = bt_ctf_field_type_integer_create(5);
1901
1902 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1903 packet_context_field_type, "custom_packet_context_field");
1904 ok(ret == 0, "Packet context field added successfully");
1905
1906 /* Define a stream event context containing a my_integer field. */
1907 stream_event_context_type = bt_ctf_field_type_structure_create();
1908 bt_ctf_field_type_structure_add_field(stream_event_context_type,
1909 integer_type, "common_event_context");
1910
1911 ok(bt_ctf_stream_class_set_event_context_type(NULL,
1912 stream_event_context_type) < 0,
1913 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
1914 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
1915 integer_type) < 0,
1916 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
1917
1918 ok(bt_ctf_stream_class_set_event_context_type(
1919 stream_class, stream_event_context_type) == 0,
1920 "Set a new stream event context type");
1921
1922 ret_field_type = bt_ctf_stream_class_get_event_context_type(
1923 stream_class);
1924 ok(ret_field_type == stream_event_context_type,
1925 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
1926 bt_ctf_object_put_ref(ret_field_type);
1927
1928
1929 /* Instantiate a stream and append events */
1930 ret = bt_ctf_writer_add_clock(writer, clock);
1931 BT_ASSERT(ret == 0);
1932
1933 ok(bt_ctf_trace_get_stream_count(trace) == 0,
1934 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
1935 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
1936 ok(stream1, "Instanciate a stream class from writer");
1937 ok(bt_ctf_trace_get_stream_count(trace) == 1,
1938 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
1939 stream = bt_ctf_trace_get_stream_by_index(trace, 0);
1940 ok(stream == stream1,
1941 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
1942 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream);
1943
1944 /*
1945 * Creating a stream through a writer adds the given stream
1946 * class to the writer's trace, thus registering the stream
1947 * class's clock to the trace.
1948 */
1949
1950 ret_stream_class = bt_ctf_stream_get_class(stream1);
1951 ok(ret_stream_class,
1952 "bt_ctf_stream_get_class returns a stream class");
1953 ok(ret_stream_class == stream_class,
1954 "Returned stream class is of the correct type");
1955
1956 /*
1957 * Packet header, packet context, event header, and stream
1958 * event context types were copied for the resolving
1959 * process
1960 */
1961 BT_CTF_OBJECT_PUT_REF_AND_RESET(packet_header_type);
1962 BT_CTF_OBJECT_PUT_REF_AND_RESET(packet_context_type);
1963 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream_event_context_type);
1964 packet_header_type = bt_ctf_trace_get_packet_header_field_type(trace);
1965 BT_ASSERT(packet_header_type);
1966 packet_context_type =
1967 bt_ctf_stream_class_get_packet_context_type(stream_class);
1968 BT_ASSERT(packet_context_type);
1969 stream_event_context_type =
1970 bt_ctf_stream_class_get_event_context_type(stream_class);
1971 BT_ASSERT(stream_event_context_type);
1972
1973 /*
1974 * Try to modify the packet context type after a stream has been
1975 * created.
1976 */
1977 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
1978 packet_header_field_type, "should_fail");
1979 ok(ret < 0,
1980 "Trace packet header type can't be modified once a stream has been instanciated");
1981
1982 /*
1983 * Try to modify the packet context type after a stream has been
1984 * created.
1985 */
1986 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1987 packet_context_field_type, "should_fail");
1988 ok(ret < 0,
1989 "Packet context type can't be modified once a stream has been instanciated");
1990
1991 /*
1992 * Try to modify the stream event context type after a stream has been
1993 * created.
1994 */
1995 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
1996 integer_type, "should_fail");
1997 ok(ret < 0,
1998 "Stream event context type can't be modified once a stream has been instanciated");
1999
2000 /* Should fail after instanciating a stream (frozen) */
2001 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
2002 "Changes to a stream class that was already instantiated fail");
2003
2004 /* Populate the custom packet header field only once for all tests */
2005 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
2006 "bt_ctf_stream_get_packet_header handles NULL correctly");
2007 packet_header = bt_ctf_stream_get_packet_header(stream1);
2008 ok(packet_header,
2009 "bt_ctf_stream_get_packet_header returns a packet header");
2010 ret_field_type = bt_ctf_field_get_type(packet_header);
2011 ok(ret_field_type == packet_header_type,
2012 "Stream returns a packet header of the appropriate type");
2013 bt_ctf_object_put_ref(ret_field_type);
2014 packet_header_field = bt_ctf_field_structure_get_field_by_name(packet_header,
2015 "custom_trace_packet_header_field");
2016 ok(packet_header_field,
2017 "Packet header structure contains a custom field with the appropriate name");
2018 ret_field_type = bt_ctf_field_get_type(packet_header_field);
2019 ok(!bt_ctf_field_integer_unsigned_set_value(packet_header_field,
2020 54321), "Set custom packet header value successfully");
2021 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
2022 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
2023 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
2024 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
2025 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
2026 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
2027 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
2028 "Successfully set a stream's packet header");
2029
2030 ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0,
2031 "Add environment field to writer after stream creation");
2032
2033 test_clock_utils();
2034
2035 test_instanciate_event_before_stream(writer, clock);
2036
2037 append_simple_event(stream_class, stream1, clock);
2038
2039 packet_resize_test(stream_class, stream1, clock);
2040
2041 append_complex_event(stream_class, stream1, clock);
2042
2043 append_existing_event_class(stream_class);
2044
2045 test_empty_stream(writer);
2046
2047 test_custom_event_header_stream(writer, clock);
2048
2049 metadata_string = bt_ctf_writer_get_metadata_string(writer);
2050 ok(metadata_string, "Get metadata string");
2051
2052 bt_ctf_writer_flush_metadata(writer);
2053
2054 bt_ctf_object_put_ref(clock);
2055 bt_ctf_object_put_ref(ret_stream_class);
2056 bt_ctf_object_put_ref(writer);
2057 bt_ctf_object_put_ref(stream1);
2058 bt_ctf_object_put_ref(packet_context_type);
2059 bt_ctf_object_put_ref(packet_context_field_type);
2060 bt_ctf_object_put_ref(integer_type);
2061 bt_ctf_object_put_ref(stream_event_context_type);
2062 bt_ctf_object_put_ref(ret_field_type);
2063 bt_ctf_object_put_ref(packet_header_type);
2064 bt_ctf_object_put_ref(packet_header_field_type);
2065 bt_ctf_object_put_ref(packet_header);
2066 bt_ctf_object_put_ref(packet_header_field);
2067 bt_ctf_object_put_ref(trace);
2068 free(metadata_string);
2069 bt_ctf_object_put_ref(stream_class);
2070
2071 validate_trace(argv[1], trace_path);
2072
2073 recursive_rmdir(trace_path);
2074 g_free(trace_path);
2075 g_free(metadata_path);
2076
2077 return 0;
2078 }
This page took 0.100815 seconds and 4 git commands to generate.