6 * Copyright 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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.
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.
23 #include <babeltrace/ctf-writer/writer.h>
24 #include <babeltrace/ctf-writer/clock.h>
25 #include <babeltrace/ctf-writer/stream.h>
26 #include <babeltrace/ctf-writer/event.h>
27 #include <babeltrace/ctf-writer/event-types.h>
28 #include <babeltrace/ctf-writer/event-fields.h>
29 #include <babeltrace/ctf/events.h>
33 #include <sys/utsname.h>
45 #define METADATA_LINE_SIZE 512
46 #define SEQUENCE_TEST_LENGTH 10
47 #define PACKET_RESIZE_TEST_LENGTH 100000
49 #define DEFAULT_CLOCK_FREQ 1000000000
50 #define DEFAULT_CLOCK_PRECISION 1
51 #define DEFAULT_CLOCK_OFFSET 0
52 #define DEFAULT_CLOCK_OFFSET_S 0
53 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
54 #define DEFAULT_CLOCK_TIME 0
56 static uint64_t current_time
= 42;
58 void validate_metadata(char *parser_path
, char *metadata_path
)
61 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
62 int parser_output_fd
= -1, metadata_fd
= -1;
69 parser_output_fd
= mkstemp(parser_output_path
);
70 metadata_fd
= open(metadata_path
, O_RDONLY
);
72 unlink(parser_output_path
);
74 if (parser_output_fd
== -1 || metadata_fd
== -1) {
75 diag("Failed create temporary files for metadata parsing.");
83 waitpid(pid
, &status
, 0);
84 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
86 /* ctf-parser-test expects a metadata string on stdin. */
87 ret
= dup2(metadata_fd
, STDIN_FILENO
);
89 perror("# dup2 metadata_fd to STDIN");
93 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
95 perror("# dup2 parser_output_fd to STDOUT");
99 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
101 perror("# dup2 parser_output_fd to STDERR");
105 execl(parser_path
, "ctf-parser-test", NULL
);
106 perror("# Could not launch the ctf metadata parser process");
110 ok(ret
== 0, "Metadata string is valid");
112 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
114 size_t len
= METADATA_LINE_SIZE
;
115 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
117 metadata_fp
= fdopen(metadata_fd
, "r");
119 perror("fdopen on metadata_fd");
124 parser_output_fp
= fdopen(parser_output_fd
, "r");
125 if (!parser_output_fp
) {
126 perror("fdopen on parser_output_fd");
129 parser_output_fd
= -1;
133 diag("malloc failure");
138 /* Output the metadata and parser output as diagnostic */
139 while (getline(&line
, &len
, metadata_fp
) > 0) {
143 rewind(parser_output_fp
);
144 while (getline(&line
, &len
, parser_output_fp
) > 0) {
151 if (fclose(metadata_fp
)) {
152 diag("fclose failure");
155 if (parser_output_fp
) {
156 if (fclose(parser_output_fp
)) {
157 diag("fclose failure");
162 if (parser_output_fd
>= 0) {
163 if (close(parser_output_fd
)) {
167 if (metadata_fd
>= 0) {
168 if (close(metadata_fd
)) {
174 void validate_trace(char *parser_path
, char *trace_path
)
177 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
178 int babeltrace_output_fd
= -1;
185 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
186 unlink(babeltrace_output_path
);
188 if (babeltrace_output_fd
== -1) {
189 diag("Failed to create a temporary file for trace parsing.");
197 waitpid(pid
, &status
, 0);
198 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
200 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
202 perror("# dup2 babeltrace_output_fd to STDOUT");
206 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
208 perror("# dup2 babeltrace_output_fd to STDERR");
212 execl(parser_path
, "babeltrace", trace_path
, NULL
);
213 perror("# Could not launch the babeltrace process");
217 ok(ret
== 0, "Babeltrace could read the resulting trace");
219 if (ret
&& babeltrace_output_fd
>= 0) {
221 size_t len
= METADATA_LINE_SIZE
;
222 FILE *babeltrace_output_fp
= NULL
;
224 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
225 if (!babeltrace_output_fp
) {
226 perror("fdopen on babeltrace_output_fd");
229 babeltrace_output_fd
= -1;
233 diag("malloc error");
235 rewind(babeltrace_output_fp
);
236 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
242 if (babeltrace_output_fp
) {
243 if (fclose(babeltrace_output_fp
)) {
244 diag("fclose error");
249 if (babeltrace_output_fd
>= 0) {
250 if (close(babeltrace_output_fd
)) {
256 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
257 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
259 /* Create and add a simple event class */
260 struct bt_ctf_event_class
*simple_event_class
=
261 bt_ctf_event_class_create("Simple Event");
262 struct bt_ctf_field_type
*uint_12_type
=
263 bt_ctf_field_type_integer_create(12);
264 struct bt_ctf_field_type
*float_type
=
265 bt_ctf_field_type_floating_point_create();
266 struct bt_ctf_field_type
*enum_type
=
267 bt_ctf_field_type_enumeration_create(uint_12_type
);
268 struct bt_ctf_event
*simple_event
;
269 struct bt_ctf_field
*integer_field
;
270 struct bt_ctf_field
*float_field
;
271 struct bt_ctf_field
*enum_field
;
272 struct bt_ctf_field
*enum_container_field
;
273 const char *mapping_name_test
= "truie";
274 const char *mapping_name
;
275 const double double_test_value
= 3.1415;
278 bt_ctf_field_type_set_alignment(float_type
, 32);
279 bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11);
280 bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53);
282 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
283 "escaping; \"test\"", 0, 0) == 0,
284 "Accept enumeration mapping strings containing quotes");
285 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
286 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
287 "Accept enumeration mapping strings containing special characters");
288 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
289 "event clock int float", 5, 22) == 0,
290 "Accept enumeration mapping strings containing reserved keywords");
291 bt_ctf_field_type_enumeration_add_mapping(enum_type
,
292 mapping_name_test
, 42, 42);
293 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
294 "enum_field") == 0, "Add enumeration field to event");
296 ok(uint_12_type
, "Create an unsigned integer type");
297 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
299 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
301 bt_ctf_stream_class_add_event_class(stream_class
,
304 simple_event
= bt_ctf_event_create(simple_event_class
);
307 "Instantiate an event containing a single integer field");
309 integer_field
= bt_ctf_field_create(uint_12_type
);
310 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
311 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
312 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
314 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
315 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
316 "bt_ctf_field_floating_point_get_value fails on an unset float field");
317 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
318 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
319 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
320 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
321 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
322 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
323 "bt_ctf_field_floating_point_get_value returns a double value");
324 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
325 "bt_ctf_field_floating_point_get_value returns a correct value");
327 enum_field
= bt_ctf_field_create(enum_type
);
328 mapping_name
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
329 ok(!mapping_name
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
330 mapping_name
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
331 ok(!mapping_name
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
332 enum_container_field
= bt_ctf_field_enumeration_get_container(
334 ok(bt_ctf_field_unsigned_integer_set_value(
335 enum_container_field
, 42) == 0,
336 "Set enumeration container value");
337 mapping_name
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
338 ok(!strcmp(mapping_name
, mapping_name_test
), "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name");
339 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
341 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
343 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
344 "Append simple event to trace stream");
346 ok(bt_ctf_stream_flush(stream
) == 0,
347 "Flush trace stream with one event");
349 bt_ctf_event_class_put(simple_event_class
);
350 bt_ctf_event_put(simple_event
);
351 bt_ctf_field_type_put(uint_12_type
);
352 bt_ctf_field_type_put(float_type
);
353 bt_ctf_field_type_put(enum_type
);
354 bt_ctf_field_put(integer_field
);
355 bt_ctf_field_put(float_field
);
356 bt_ctf_field_put(enum_field
);
357 bt_ctf_field_put(enum_container_field
);
360 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
361 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
364 const char *test_string
= "Test string";
365 struct bt_ctf_field_type
*uint_35_type
=
366 bt_ctf_field_type_integer_create(35);
367 struct bt_ctf_field_type
*int_16_type
=
368 bt_ctf_field_type_integer_create(16);
369 struct bt_ctf_field_type
*uint_3_type
=
370 bt_ctf_field_type_integer_create(3);
371 struct bt_ctf_field_type
*enum_variant_type
=
372 bt_ctf_field_type_enumeration_create(uint_3_type
);
373 struct bt_ctf_field_type
*variant_type
=
374 bt_ctf_field_type_variant_create(enum_variant_type
,
376 struct bt_ctf_field_type
*string_type
=
377 bt_ctf_field_type_string_create();
378 struct bt_ctf_field_type
*sequence_type
;
379 struct bt_ctf_field_type
*inner_structure_type
=
380 bt_ctf_field_type_structure_create();
381 struct bt_ctf_field_type
*complex_structure_type
=
382 bt_ctf_field_type_structure_create();
383 struct bt_ctf_event_class
*event_class
;
384 struct bt_ctf_event
*event
;
385 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
386 *inner_structure_field
, *complex_structure_field
,
387 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
388 *variant_field
, *ret_field
;
389 int64_t ret_signed_int
;
390 uint64_t ret_unsigned_int
;
391 const char *ret_string
;
393 bt_ctf_field_type_set_alignment(int_16_type
, 32);
394 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
395 bt_ctf_field_type_integer_set_base(uint_35_type
,
396 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
398 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
400 bt_ctf_field_type_structure_add_field(inner_structure_type
,
401 uint_35_type
, "seq_len");
402 bt_ctf_field_type_structure_add_field(inner_structure_type
,
403 sequence_type
, "a_sequence");
405 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
407 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
409 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
410 "UINT35_TYPE", 2, 7);
411 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
412 "An unknown entry"), "Reject a variant field based on an unknown tag value");
413 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
414 "UINT3_TYPE") == 0, "Add a field to a variant");
415 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
417 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
420 bt_ctf_field_type_structure_add_field(complex_structure_type
,
421 enum_variant_type
, "variant_selector");
422 bt_ctf_field_type_structure_add_field(complex_structure_type
,
423 string_type
, "a_string");
424 bt_ctf_field_type_structure_add_field(complex_structure_type
,
425 variant_type
, "variant_value");
426 bt_ctf_field_type_structure_add_field(complex_structure_type
,
427 inner_structure_type
, "inner_structure");
429 ok(bt_ctf_event_class_create("clock") == NULL
,
430 "Reject creation of an event class with an illegal name");
431 event_class
= bt_ctf_event_class_create("Complex Test Event");
432 ok(event_class
, "Create an event class");
433 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
434 "Reject addition of a field with an empty name to an event");
435 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
436 "Reject addition of a field with a NULL type to an event");
437 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
439 "Reject addition of a type with an illegal name to an event");
440 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
442 "Add field of type unsigned integer to an event");
443 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
444 "int_16") == 0, "Add field of type signed integer to an event");
445 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
446 "complex_structure") == 0,
447 "Add composite structure to an event");
449 /* Add event class to the stream class */
450 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
451 "Reject addition of NULL event class to a stream class");
452 ok(bt_ctf_stream_class_add_event_class(stream_class
,
453 event_class
) == 0, "Add an event class to stream class");
455 event
= bt_ctf_event_create(event_class
);
456 ok(event
, "Instanciate a complex event");
458 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
460 printf("uint_35_field is NULL\n");
461 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
462 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
463 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) == -1,
464 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
465 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) == -1,
466 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
467 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
468 &ret_unsigned_int
) == 0,
469 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
470 ok(ret_unsigned_int
== 0x0DDF00D,
471 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
472 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
473 &ret_signed_int
) == -1,
474 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
475 bt_ctf_field_put(uint_35_field
);
477 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
478 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
479 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) == -1,
480 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
481 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) == -1,
482 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
483 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
484 &ret_signed_int
) == 0,
485 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
486 ok(ret_signed_int
== -12345,
487 "bt_ctf_field_signed_integer_get_value returns the correct value");
488 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
489 &ret_unsigned_int
) == -1,
490 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
491 bt_ctf_field_put(int_16_field
);
493 complex_structure_field
= bt_ctf_event_get_payload(event
,
494 "complex_structure");
496 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
497 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
498 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
499 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
500 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
501 complex_structure_field
, 3);
502 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
503 bt_ctf_field_put(inner_structure_field
);
504 ok(ret_field_type
== inner_structure_type
,
505 "bt_ctf_field_structure_get_field_by_index returns a correct field");
506 bt_ctf_field_type_put(ret_field_type
);
508 inner_structure_field
= bt_ctf_field_structure_get_field(
509 complex_structure_field
, "inner_structure");
510 a_string_field
= bt_ctf_field_structure_get_field(
511 complex_structure_field
, "a_string");
512 enum_variant_field
= bt_ctf_field_structure_get_field(
513 complex_structure_field
, "variant_selector");
514 variant_field
= bt_ctf_field_structure_get_field(
515 complex_structure_field
, "variant_value");
516 uint_35_field
= bt_ctf_field_structure_get_field(
517 inner_structure_field
, "seq_len");
518 a_sequence_field
= bt_ctf_field_structure_get_field(
519 inner_structure_field
, "a_sequence");
521 enum_container_field
= bt_ctf_field_enumeration_get_container(
523 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
524 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
526 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
527 bt_ctf_field_put(int_16_field
);
528 ok(!bt_ctf_field_string_get_value(a_string_field
),
529 "bt_ctf_field_string_get_value returns NULL on an unset field");
530 bt_ctf_field_string_set_value(a_string_field
,
532 ok(!bt_ctf_field_string_get_value(NULL
),
533 "bt_ctf_field_string_get_value correctly handles NULL");
534 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
535 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
536 ok(!strcmp(ret_string
, test_string
),
537 "bt_ctf_field_string_get_value returns a correct value");
538 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
539 SEQUENCE_TEST_LENGTH
);
541 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
542 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
543 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
544 uint_35_field
) == 0, "Set a sequence field's length");
545 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
546 ok(ret_field
== uint_35_field
,
547 "bt_ctf_field_sequence_get_length returns the correct length field");
548 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
549 "bt_ctf_field_sequence_get_length properly handles NULL");
551 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
552 int_16_field
= bt_ctf_field_sequence_get_field(
553 a_sequence_field
, i
);
554 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
555 bt_ctf_field_put(int_16_field
);
558 bt_ctf_clock_set_time(clock
, ++current_time
);
559 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
560 "Append a complex event to a stream");
561 ok(bt_ctf_stream_flush(stream
) == 0,
562 "Flush a stream containing a complex event");
564 bt_ctf_field_put(uint_35_field
);
565 bt_ctf_field_put(a_string_field
);
566 bt_ctf_field_put(inner_structure_field
);
567 bt_ctf_field_put(complex_structure_field
);
568 bt_ctf_field_put(a_sequence_field
);
569 bt_ctf_field_put(enum_variant_field
);
570 bt_ctf_field_put(enum_container_field
);
571 bt_ctf_field_put(variant_field
);
572 bt_ctf_field_put(ret_field
);
573 bt_ctf_field_type_put(uint_35_type
);
574 bt_ctf_field_type_put(int_16_type
);
575 bt_ctf_field_type_put(string_type
);
576 bt_ctf_field_type_put(sequence_type
);
577 bt_ctf_field_type_put(inner_structure_type
);
578 bt_ctf_field_type_put(complex_structure_type
);
579 bt_ctf_field_type_put(uint_3_type
);
580 bt_ctf_field_type_put(enum_variant_type
);
581 bt_ctf_field_type_put(variant_type
);
582 bt_ctf_event_class_put(event_class
);
583 bt_ctf_event_put(event
);
586 void type_field_tests()
588 struct bt_ctf_field
*uint_12
;
589 struct bt_ctf_field
*int_16
;
590 struct bt_ctf_field
*string
;
591 struct bt_ctf_field
*enumeration
;
592 struct bt_ctf_field_type
*composite_structure_type
;
593 struct bt_ctf_field_type
*structure_seq_type
;
594 struct bt_ctf_field_type
*string_type
;
595 struct bt_ctf_field_type
*sequence_type
;
596 struct bt_ctf_field_type
*uint_8_type
;
597 struct bt_ctf_field_type
*int_16_type
;
598 struct bt_ctf_field_type
*uint_12_type
=
599 bt_ctf_field_type_integer_create(12);
600 struct bt_ctf_field_type
*enumeration_type
;
601 struct bt_ctf_field_type
*enumeration_sequence_type
;
602 struct bt_ctf_field_type
*enumeration_array_type
;
603 struct bt_ctf_field_type
*returned_type
;
605 returned_type
= bt_ctf_field_get_type(NULL
);
606 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
608 ok(uint_12_type
, "Create an unsigned integer type");
609 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
610 BT_CTF_INTEGER_BASE_BINARY
) == 0,
611 "Set integer type's base as binary");
612 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
613 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
614 "Set integer type's base as decimal");
615 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
616 BT_CTF_INTEGER_BASE_UNKNOWN
),
617 "Reject integer type's base set as unknown");
618 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
619 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
620 "Set integer type's base as octal");
621 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
622 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
623 "Set integer type's base as hexadecimal");
624 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
625 "Reject unknown integer base value");
626 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
627 "Set integer type signedness to signed");
628 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
629 "Set integer type signedness to unsigned");
631 int_16_type
= bt_ctf_field_type_integer_create(16);
632 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
633 uint_8_type
= bt_ctf_field_type_integer_create(8);
635 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
636 ok(sequence_type
, "Create a sequence of int16_t type");
638 string_type
= bt_ctf_field_type_string_create();
639 ok(string_type
, "Create a string type");
640 ok(bt_ctf_field_type_string_set_encoding(string_type
,
642 "Reject invalid \"None\" string encoding");
643 ok(bt_ctf_field_type_string_set_encoding(string_type
,
645 "Reject invalid string encoding");
646 ok(bt_ctf_field_type_string_set_encoding(string_type
,
647 CTF_STRING_ASCII
) == 0,
648 "Set string encoding to ASCII");
650 structure_seq_type
= bt_ctf_field_type_structure_create();
651 ok(structure_seq_type
, "Create a structure type");
652 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
653 uint_8_type
, "seq_len") == 0,
654 "Add a uint8_t type to a structure");
655 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
656 sequence_type
, "a_sequence") == 0,
657 "Add a sequence type to a structure");
658 composite_structure_type
= bt_ctf_field_type_structure_create();
659 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
660 string_type
, "a_string") == 0,
661 "Add a string type to a structure");
662 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
663 structure_seq_type
, "inner_structure") == 0,
664 "Add a structure type to a structure");
666 int_16
= bt_ctf_field_create(int_16_type
);
667 ok(int_16
, "Instanciate a signed 16-bit integer");
668 uint_12
= bt_ctf_field_create(uint_12_type
);
669 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
670 returned_type
= bt_ctf_field_get_type(int_16
);
671 ok(returned_type
== int_16_type
,
672 "bt_ctf_field_get_type returns the correct type");
673 bt_ctf_field_type_put(returned_type
);
675 /* Can't modify types after instanciating them */
676 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
677 BT_CTF_INTEGER_BASE_DECIMAL
),
678 "Check an integer type' base can't be modified after instanciation");
679 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
680 "Check an integer type's signedness can't be modified after instanciation");
682 /* Check signed property is checked */
683 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
684 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
685 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
686 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
688 /* Check overflows are properly tested for */
689 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
690 "Check -32768 is allowed for a signed 16-bit integer");
691 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
692 "Check 32767 is allowed for a signed 16-bit integer");
693 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
694 "Check 32768 is not allowed for a signed 16-bit integer");
695 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
696 "Check -32769 is not allowed for a signed 16-bit integer");
697 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
698 "Check -42 is allowed for a signed 16-bit integer");
700 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
701 "Check 4095 is allowed for an unsigned 12-bit integer");
702 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
703 "Check 4096 is not allowed for a unsigned 12-bit integer");
704 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
705 "Check 0 is allowed for an unsigned 12-bit integer");
707 string
= bt_ctf_field_create(string_type
);
708 ok(string
, "Instanciate a string field");
709 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
710 "Set a string's value");
712 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
714 "Create an enumeration type with an unsigned 12-bit integer as container");
715 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
716 enumeration_type
, "count");
717 ok(!enumeration_sequence_type
,
718 "Check enumeration types are validated when creating a sequence");
719 enumeration_array_type
= bt_ctf_field_type_array_create(
720 enumeration_type
, 10);
721 ok(!enumeration_array_type
,
722 "Check enumeration types are validated when creating an array");
723 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
724 enumeration_type
, "enumeration") == 0,
725 "Check enumeration types are validated when adding them as structure members");
726 enumeration
= bt_ctf_field_create(enumeration_type
);
728 "Check enumeration types are validated before instantiation");
730 bt_ctf_field_put(string
);
731 bt_ctf_field_put(uint_12
);
732 bt_ctf_field_put(int_16
);
733 bt_ctf_field_put(enumeration
);
734 bt_ctf_field_type_put(composite_structure_type
);
735 bt_ctf_field_type_put(structure_seq_type
);
736 bt_ctf_field_type_put(string_type
);
737 bt_ctf_field_type_put(sequence_type
);
738 bt_ctf_field_type_put(uint_8_type
);
739 bt_ctf_field_type_put(int_16_type
);
740 bt_ctf_field_type_put(uint_12_type
);
741 bt_ctf_field_type_put(enumeration_type
);
742 bt_ctf_field_type_put(enumeration_sequence_type
);
743 bt_ctf_field_type_put(enumeration_array_type
);
746 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
747 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
750 * Append enough events to force the underlying packet to be resized.
751 * Also tests that a new event can be declared after a stream has been
752 * instantiated and used/flushed.
756 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
758 struct bt_ctf_field_type
*integer_type
=
759 bt_ctf_field_type_integer_create(17);
760 struct bt_ctf_field_type
*string_type
=
761 bt_ctf_field_type_string_create();
763 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
765 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
767 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
768 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
773 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
774 struct bt_ctf_event
*event
= bt_ctf_event_create(event_class
);
775 struct bt_ctf_field
*integer
=
776 bt_ctf_field_create(integer_type
);
777 struct bt_ctf_field
*string
=
778 bt_ctf_field_create(string_type
);
780 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
781 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
782 ret
|= bt_ctf_event_set_payload(event
, "field_1",
784 bt_ctf_field_put(integer
);
785 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
786 ret
|= bt_ctf_event_set_payload(event
, "a_string",
788 bt_ctf_field_put(string
);
789 ret
|= bt_ctf_stream_append_event(stream
, event
);
790 bt_ctf_event_put(event
);
797 ok(ret
== 0, "Append 100 000 events to a stream");
798 ok(bt_ctf_stream_flush(stream
) == 0,
799 "Flush a stream that forces a packet resize");
800 bt_ctf_field_type_put(integer_type
);
801 bt_ctf_field_type_put(string_type
);
802 bt_ctf_event_class_put(event_class
);
805 int main(int argc
, char **argv
)
807 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
808 char metadata_path
[sizeof(trace_path
) + 9];
809 const char *clock_name
= "test_clock";
810 const char *clock_description
= "This is a test clock";
811 const char *returned_clock_name
;
812 const char *returned_clock_description
;
813 const uint64_t frequency
= 1123456789;
814 const uint64_t offset_s
= 1351530929945824323;
815 const uint64_t offset
= 1234567;
816 const uint64_t precision
= 10;
817 const int is_absolute
= 0xFF;
818 char *metadata_string
;
819 struct bt_ctf_writer
*writer
;
821 char hostname
[HOST_NAME_MAX
];
822 struct bt_ctf_clock
*clock
;
823 struct bt_ctf_stream_class
*stream_class
;
824 struct bt_ctf_stream
*stream1
;
827 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
833 if (!mkdtemp(trace_path
)) {
837 strcpy(metadata_path
, trace_path
);
838 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
840 writer
= bt_ctf_writer_create(trace_path
);
841 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
843 /* Add environment context to the trace */
844 gethostname(hostname
, HOST_NAME_MAX
);
845 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
846 "Add host (%s) environment field to writer instance",
848 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
850 "bt_ctf_writer_add_environment_field error with NULL writer");
851 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
853 "bt_ctf_writer_add_environment_field error with NULL field name");
854 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
856 "bt_ctf_writer_add_environment_field error with NULL field value");
863 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
864 == 0, "Add sysname (%s) environment field to writer instance",
866 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
868 "Add nodename (%s) environment field to writer instance",
870 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
871 == 0, "Add release (%s) environment field to writer instance",
873 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
874 == 0, "Add version (%s) environment field to writer instance",
876 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
877 == 0, "Add machine (%s) environment field to writer istance",
880 /* Define a clock and add it to the trace */
881 ok(bt_ctf_clock_create("signed") == NULL
,
882 "Illegal clock name rejected");
883 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
884 clock
= bt_ctf_clock_create(clock_name
);
885 ok(clock
, "Clock created sucessfully");
886 returned_clock_name
= bt_ctf_clock_get_name(clock
);
887 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
888 ok(strcmp(returned_clock_name
, clock_name
) == 0,
889 "Returned clock name is valid");
891 returned_clock_description
= bt_ctf_clock_get_description(clock
);
892 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
893 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
894 "Clock description set successfully");
896 returned_clock_description
= bt_ctf_clock_get_description(clock
);
897 ok(returned_clock_description
,
898 "bt_ctf_clock_get_description returns a description.");
899 ok(strcmp(returned_clock_description
, clock_description
) == 0,
900 "Returned clock description is valid");
902 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
903 "bt_ctf_clock_get_frequency returns the correct default frequency");
904 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
905 "Set clock frequency");
906 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
907 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
909 ok(bt_ctf_clock_get_offset_s(clock
) == DEFAULT_CLOCK_OFFSET_S
,
910 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
911 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
912 "Set clock offset (seconds)");
913 ok(bt_ctf_clock_get_offset_s(clock
) == offset_s
,
914 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
916 ok(bt_ctf_clock_get_offset(clock
) == DEFAULT_CLOCK_OFFSET
,
917 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
918 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
919 ok(bt_ctf_clock_get_offset(clock
) == offset
,
920 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
922 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
923 "bt_ctf_clock_get_precision returns the correct default precision");
924 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
925 "Set clock precision");
926 ok(bt_ctf_clock_get_precision(clock
) == precision
,
927 "bt_ctf_clock_get_precision returns the correct precision once it is set");
929 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
930 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
931 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
932 "Set clock absolute property");
933 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
934 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
936 ok(bt_ctf_clock_get_time(clock
) == DEFAULT_CLOCK_TIME
,
937 "bt_ctf_clock_get_time returns the correct default time");
938 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
940 ok(bt_ctf_clock_get_time(clock
) == current_time
,
941 "bt_ctf_clock_get_time returns the correct time once it is set");
943 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
944 "Add clock to writer instance");
945 ok(bt_ctf_writer_add_clock(writer
, clock
),
946 "Verify a clock can't be added twice to a writer instance");
948 ok(!bt_ctf_clock_get_name(NULL
),
949 "bt_ctf_clock_get_name correctly handles NULL");
950 ok(!bt_ctf_clock_get_description(NULL
),
951 "bt_ctf_clock_get_description correctly handles NULL");
952 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
953 "bt_ctf_clock_get_frequency correctly handles NULL");
954 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
955 "bt_ctf_clock_get_precision correctly handles NULL");
956 ok(bt_ctf_clock_get_offset_s(NULL
) == -1ULL,
957 "bt_ctf_clock_get_offset_s correctly handles NULL");
958 ok(bt_ctf_clock_get_offset(NULL
) == -1ULL,
959 "bt_ctf_clock_get_offset correctly handles NULL");
960 ok(bt_ctf_clock_get_is_absolute(NULL
) == -1,
961 "bt_ctf_clock_get_is_absolute correctly handles NULL");
962 ok(bt_ctf_clock_get_time(NULL
) == -1ULL,
963 "bt_ctf_clock_get_time correctly handles NULL");
965 ok(bt_ctf_clock_set_description(NULL
, NULL
) == -1,
966 "bt_ctf_clock_set_description correctly handles NULL clock");
967 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) == -1,
968 "bt_ctf_clock_set_frequency correctly handles NULL clock");
969 ok(bt_ctf_clock_set_precision(NULL
, precision
) == -1,
970 "bt_ctf_clock_get_precision correctly handles NULL clock");
971 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) == -1,
972 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
973 ok(bt_ctf_clock_set_offset(NULL
, offset
) == -1,
974 "bt_ctf_clock_set_offset correctly handles NULL clock");
975 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) == -1,
976 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
977 ok(bt_ctf_clock_set_time(NULL
, current_time
) == -1,
978 "bt_ctf_clock_set_time correctly handles NULL clock");
980 /* Define a stream class */
981 stream_class
= bt_ctf_stream_class_create("test_stream");
982 ok(stream_class
, "Create stream class");
983 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
984 "Set a stream class' clock");
986 /* Test the event fields and event types APIs */
989 /* Instantiate a stream and append events */
990 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
991 ok(stream1
, "Instanciate a stream class from writer");
993 /* Should fail after instanciating a stream (locked)*/
994 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
995 "Changes to a stream class that was already instantiated fail");
997 append_simple_event(stream_class
, stream1
, clock
);
999 packet_resize_test(stream_class
, stream1
, clock
);
1001 append_complex_event(stream_class
, stream1
, clock
);
1003 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
1004 ok(metadata_string
, "Get metadata string");
1006 bt_ctf_writer_flush_metadata(writer
);
1007 validate_metadata(argv
[1], metadata_path
);
1008 validate_trace(argv
[2], trace_path
);
1010 bt_ctf_clock_put(clock
);
1011 bt_ctf_stream_class_put(stream_class
);
1012 bt_ctf_writer_put(writer
);
1013 bt_ctf_stream_put(stream1
);
1014 free(metadata_string
);
1016 /* Remove all trace files and delete temporary trace directory */
1017 DIR *trace_dir
= opendir(trace_path
);
1019 perror("# opendir");
1023 struct dirent
*entry
;
1024 while ((entry
= readdir(trace_dir
))) {
1025 if (entry
->d_type
== DT_REG
) {
1026 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);
1031 closedir(trace_dir
);