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