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