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