Implement CTF-IR event fields getters
[babeltrace.git] / tests / lib / test_ctf_writer.c
1 /*
2 * test-ctf-writer.c
3 *
4 * CTF Writer test
5 *
6 * Copyright 2013 - 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 #define _GNU_SOURCE
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>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sys/utsname.h>
34 #include <limits.h>
35 #include <string.h>
36 #include <assert.h>
37 #include <unistd.h>
38 #include <sys/wait.h>
39 #include <fcntl.h>
40 #include <dirent.h>
41 #include "tap/tap.h"
42
43 #define METADATA_LINE_SIZE 512
44 #define SEQUENCE_TEST_LENGTH 10
45 #define PACKET_RESIZE_TEST_LENGTH 100000
46
47 #define DEFAULT_CLOCK_FREQ 1000000000
48 #define DEFAULT_CLOCK_PRECISION 1
49 #define DEFAULT_CLOCK_OFFSET 0
50 #define DEFAULT_CLOCK_OFFSET_S 0
51 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
52 #define DEFAULT_CLOCK_TIME 0
53
54 static uint64_t current_time = 42;
55
56 void validate_metadata(char *parser_path, char *metadata_path)
57 {
58 int ret = 0;
59 char parser_output_path[] = "/tmp/parser_output_XXXXXX";
60 int parser_output_fd = -1, metadata_fd = -1;
61
62 if (!metadata_path) {
63 ret = -1;
64 goto result;
65 }
66
67 parser_output_fd = mkstemp(parser_output_path);
68 metadata_fd = open(metadata_path, O_RDONLY);
69
70 unlink(parser_output_path);
71
72 if (parser_output_fd == -1 || metadata_fd == -1) {
73 diag("Failed create temporary files for metadata parsing.");
74 ret = -1;
75 goto result;
76 }
77
78 pid_t pid = fork();
79 if (pid) {
80 int status = 0;
81 waitpid(pid, &status, 0);
82 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
83 } else {
84 /* ctf-parser-test expects a metadata string on stdin. */
85 ret = dup2(metadata_fd, STDIN_FILENO);
86 if (ret < 0) {
87 perror("# dup2 metadata_fd to STDIN");
88 goto result;
89 }
90
91 ret = dup2(parser_output_fd, STDOUT_FILENO);
92 if (ret < 0) {
93 perror("# dup2 parser_output_fd to STDOUT");
94 goto result;
95 }
96
97 ret = dup2(parser_output_fd, STDERR_FILENO);
98 if (ret < 0) {
99 perror("# dup2 parser_output_fd to STDERR");
100 goto result;
101 }
102
103 execl(parser_path, "ctf-parser-test", NULL);
104 perror("# Could not launch the ctf metadata parser process");
105 exit(-1);
106 }
107 result:
108 ok(ret == 0, "Metadata string is valid");
109
110 if (ret && metadata_fd >= 0 && parser_output_fd >= 0) {
111 char *line;
112 size_t len = METADATA_LINE_SIZE;
113 FILE *metadata_fp = NULL, *parser_output_fp = NULL;
114
115 metadata_fp = fdopen(metadata_fd, "r");
116 if (!metadata_fp) {
117 perror("fdopen on metadata_fd");
118 goto close_fp;
119 }
120 metadata_fd = -1;
121
122 parser_output_fp = fdopen(parser_output_fd, "r");
123 if (!parser_output_fp) {
124 perror("fdopen on parser_output_fd");
125 goto close_fp;
126 }
127 parser_output_fd = -1;
128
129 line = malloc(len);
130 if (!line) {
131 diag("malloc failure");
132 }
133
134 rewind(metadata_fp);
135
136 /* Output the metadata and parser output as diagnostic */
137 while (getline(&line, &len, metadata_fp) > 0) {
138 diag("%s", line);
139 }
140
141 rewind(parser_output_fp);
142 while (getline(&line, &len, parser_output_fp) > 0) {
143 diag("%s", line);
144 }
145
146 free(line);
147 close_fp:
148 if (metadata_fp) {
149 if (fclose(metadata_fp)) {
150 diag("fclose failure");
151 }
152 }
153 if (parser_output_fp) {
154 if (fclose(parser_output_fp)) {
155 diag("fclose failure");
156 }
157 }
158 }
159
160 if (parser_output_fd >= 0) {
161 if (close(parser_output_fd)) {
162 diag("close error");
163 }
164 }
165 if (metadata_fd >= 0) {
166 if (close(metadata_fd)) {
167 diag("close error");
168 }
169 }
170 }
171
172 void validate_trace(char *parser_path, char *trace_path)
173 {
174 int ret = 0;
175 char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX";
176 int babeltrace_output_fd = -1;
177
178 if (!trace_path) {
179 ret = -1;
180 goto result;
181 }
182
183 babeltrace_output_fd = mkstemp(babeltrace_output_path);
184 unlink(babeltrace_output_path);
185
186 if (babeltrace_output_fd == -1) {
187 diag("Failed to create a temporary file for trace parsing.");
188 ret = -1;
189 goto result;
190 }
191
192 pid_t pid = fork();
193 if (pid) {
194 int status = 0;
195 waitpid(pid, &status, 0);
196 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
197 } else {
198 ret = dup2(babeltrace_output_fd, STDOUT_FILENO);
199 if (ret < 0) {
200 perror("# dup2 babeltrace_output_fd to STDOUT");
201 goto result;
202 }
203
204 ret = dup2(babeltrace_output_fd, STDERR_FILENO);
205 if (ret < 0) {
206 perror("# dup2 babeltrace_output_fd to STDERR");
207 goto result;
208 }
209
210 execl(parser_path, "babeltrace", trace_path, NULL);
211 perror("# Could not launch the babeltrace process");
212 exit(-1);
213 }
214 result:
215 ok(ret == 0, "Babeltrace could read the resulting trace");
216
217 if (ret && babeltrace_output_fd >= 0) {
218 char *line;
219 size_t len = METADATA_LINE_SIZE;
220 FILE *babeltrace_output_fp = NULL;
221
222 babeltrace_output_fp = fdopen(babeltrace_output_fd, "r");
223 if (!babeltrace_output_fp) {
224 perror("fdopen on babeltrace_output_fd");
225 goto close_fp;
226 }
227 babeltrace_output_fd = -1;
228
229 line = malloc(len);
230 if (!line) {
231 diag("malloc error");
232 }
233 rewind(babeltrace_output_fp);
234 while (getline(&line, &len, babeltrace_output_fp) > 0) {
235 diag("%s", line);
236 }
237
238 free(line);
239 close_fp:
240 if (babeltrace_output_fp) {
241 if (fclose(babeltrace_output_fp)) {
242 diag("fclose error");
243 }
244 }
245 }
246
247 if (babeltrace_output_fd >= 0) {
248 if (close(babeltrace_output_fd)) {
249 diag("close error");
250 }
251 }
252 }
253
254 void append_simple_event(struct bt_ctf_stream_class *stream_class,
255 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
256 {
257 /* Create and add a simple event class */
258 struct bt_ctf_event_class *simple_event_class =
259 bt_ctf_event_class_create("Simple Event");
260 struct bt_ctf_field_type *uint_12_type =
261 bt_ctf_field_type_integer_create(12);
262 struct bt_ctf_field_type *float_type =
263 bt_ctf_field_type_floating_point_create();
264 struct bt_ctf_field_type *enum_type =
265 bt_ctf_field_type_enumeration_create(uint_12_type);
266 struct bt_ctf_event *simple_event;
267 struct bt_ctf_field *integer_field;
268 struct bt_ctf_field *float_field;
269 struct bt_ctf_field *enum_field;
270 struct bt_ctf_field *enum_container_field;
271
272 bt_ctf_field_type_set_alignment(float_type, 32);
273 bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11);
274 bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53);
275
276 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
277 "escaping; \"test\"", 0, 0) == 0,
278 "Accept enumeration mapping strings containing quotes");
279 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
280 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
281 "Accept enumeration mapping strings containing special characters");
282 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
283 "event clock int float", 5, 22) == 0,
284 "Accept enumeration mapping strings containing reserved keywords");
285 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
286 "enum_field") == 0, "Add enumeration field to event");
287
288 ok(uint_12_type, "Create an unsigned integer type");
289 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
290 "integer_field");
291 bt_ctf_event_class_add_field(simple_event_class, float_type,
292 "float_field");
293 bt_ctf_stream_class_add_event_class(stream_class,
294 simple_event_class);
295
296 simple_event = bt_ctf_event_create(simple_event_class);
297
298 ok(simple_event,
299 "Instantiate an event containing a single integer field");
300
301 integer_field = bt_ctf_field_create(uint_12_type);
302 bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
303 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
304 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
305
306 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
307 bt_ctf_field_floating_point_set_value(float_field, 3.1415);
308 enum_field = bt_ctf_field_create(enum_type);
309 enum_container_field = bt_ctf_field_enumeration_get_container(
310 enum_field);
311 ok(bt_ctf_field_unsigned_integer_set_value(
312 enum_container_field, 1) == 0,
313 "Set enumeration container value");
314 bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
315
316 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
317
318 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
319 "Append simple event to trace stream");
320
321 ok(bt_ctf_stream_flush(stream) == 0,
322 "Flush trace stream with one event");
323
324 bt_ctf_event_class_put(simple_event_class);
325 bt_ctf_event_put(simple_event);
326 bt_ctf_field_type_put(uint_12_type);
327 bt_ctf_field_type_put(float_type);
328 bt_ctf_field_type_put(enum_type);
329 bt_ctf_field_put(integer_field);
330 bt_ctf_field_put(float_field);
331 bt_ctf_field_put(enum_field);
332 bt_ctf_field_put(enum_container_field);
333 }
334
335 void append_complex_event(struct bt_ctf_stream_class *stream_class,
336 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
337 {
338 int i;
339 struct bt_ctf_field_type *uint_35_type =
340 bt_ctf_field_type_integer_create(35);
341 struct bt_ctf_field_type *int_16_type =
342 bt_ctf_field_type_integer_create(16);
343 struct bt_ctf_field_type *uint_3_type =
344 bt_ctf_field_type_integer_create(3);
345 struct bt_ctf_field_type *enum_variant_type =
346 bt_ctf_field_type_enumeration_create(uint_3_type);
347 struct bt_ctf_field_type *variant_type =
348 bt_ctf_field_type_variant_create(enum_variant_type,
349 "variant_selector");
350 struct bt_ctf_field_type *string_type =
351 bt_ctf_field_type_string_create();
352 struct bt_ctf_field_type *sequence_type;
353 struct bt_ctf_field_type *inner_structure_type =
354 bt_ctf_field_type_structure_create();
355 struct bt_ctf_field_type *complex_structure_type =
356 bt_ctf_field_type_structure_create();
357 struct bt_ctf_event_class *event_class;
358 struct bt_ctf_event *event;
359 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
360 *inner_structure_field, *complex_structure_field,
361 *a_sequence_field, *enum_variant_field, *enum_container_field,
362 *variant_field, *ret_field;
363
364 bt_ctf_field_type_set_alignment(int_16_type, 32);
365 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
366 bt_ctf_field_type_integer_set_base(uint_35_type,
367 BT_CTF_INTEGER_BASE_HEXADECIMAL);
368
369 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
370 "seq_len");
371 bt_ctf_field_type_structure_add_field(inner_structure_type,
372 uint_35_type, "seq_len");
373 bt_ctf_field_type_structure_add_field(inner_structure_type,
374 sequence_type, "a_sequence");
375
376 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
377 "UINT3_TYPE", 0, 0);
378 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
379 "INT16_TYPE", 1, 1);
380 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
381 "UINT35_TYPE", 2, 7);
382 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
383 "An unknown entry"), "Reject a variant field based on an unknown tag value");
384 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
385 "UINT3_TYPE") == 0, "Add a field to a variant");
386 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
387 "INT16_TYPE");
388 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
389 "UINT35_TYPE");
390
391 bt_ctf_field_type_structure_add_field(complex_structure_type,
392 enum_variant_type, "variant_selector");
393 bt_ctf_field_type_structure_add_field(complex_structure_type,
394 string_type, "a_string");
395 bt_ctf_field_type_structure_add_field(complex_structure_type,
396 variant_type, "variant_value");
397 bt_ctf_field_type_structure_add_field(complex_structure_type,
398 inner_structure_type, "inner_structure");
399
400 ok(bt_ctf_event_class_create("clock") == NULL,
401 "Reject creation of an event class with an illegal name");
402 event_class = bt_ctf_event_class_create("Complex Test Event");
403 ok(event_class, "Create an event class");
404 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
405 "Reject addition of a field with an empty name to an event");
406 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
407 "Reject addition of a field with a NULL type to an event");
408 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
409 "int"),
410 "Reject addition of a type with an illegal name to an event");
411 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
412 "uint_35") == 0,
413 "Add field of type unsigned integer to an event");
414 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
415 "int_16") == 0, "Add field of type signed integer to an event");
416 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
417 "complex_structure") == 0,
418 "Add composite structure to an event");
419
420 /* Add event class to the stream class */
421 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
422 "Reject addition of NULL event class to a stream class");
423 ok(bt_ctf_stream_class_add_event_class(stream_class,
424 event_class) == 0, "Add an event class to stream class");
425
426 event = bt_ctf_event_create(event_class);
427 ok(event, "Instanciate a complex event");
428
429 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
430 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
431 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
432 bt_ctf_field_put(uint_35_field);
433
434 int_16_field = bt_ctf_event_get_payload(event, "int_16");
435 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
436 bt_ctf_field_put(int_16_field);
437
438 complex_structure_field = bt_ctf_event_get_payload(event,
439 "complex_structure");
440 inner_structure_field = bt_ctf_field_structure_get_field(
441 complex_structure_field, "inner_structure");
442 a_string_field = bt_ctf_field_structure_get_field(
443 complex_structure_field, "a_string");
444 enum_variant_field = bt_ctf_field_structure_get_field(
445 complex_structure_field, "variant_selector");
446 variant_field = bt_ctf_field_structure_get_field(
447 complex_structure_field, "variant_value");
448 uint_35_field = bt_ctf_field_structure_get_field(
449 inner_structure_field, "seq_len");
450 a_sequence_field = bt_ctf_field_structure_get_field(
451 inner_structure_field, "a_sequence");
452
453 enum_container_field = bt_ctf_field_enumeration_get_container(
454 enum_variant_field);
455 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
456 int_16_field = bt_ctf_field_variant_get_field(variant_field,
457 enum_variant_field);
458 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
459 bt_ctf_field_put(int_16_field);
460 bt_ctf_field_string_set_value(a_string_field,
461 "Test string");
462 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
463 SEQUENCE_TEST_LENGTH);
464 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
465 uint_35_field) == 0, "Set a sequence field's length");
466 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
467 ok(ret_field == uint_35_field,
468 "bt_ctf_field_sequence_get_length returns the correct length field");
469 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
470 "bt_ctf_field_sequence_get_length properly handles NULL");
471
472 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
473 int_16_field = bt_ctf_field_sequence_get_field(
474 a_sequence_field, i);
475 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
476 bt_ctf_field_put(int_16_field);
477 }
478
479 bt_ctf_clock_set_time(clock, ++current_time);
480 ok(bt_ctf_stream_append_event(stream, event) == 0,
481 "Append a complex event to a stream");
482 ok(bt_ctf_stream_flush(stream) == 0,
483 "Flush a stream containing a complex event");
484
485 bt_ctf_field_put(uint_35_field);
486 bt_ctf_field_put(a_string_field);
487 bt_ctf_field_put(inner_structure_field);
488 bt_ctf_field_put(complex_structure_field);
489 bt_ctf_field_put(a_sequence_field);
490 bt_ctf_field_put(enum_variant_field);
491 bt_ctf_field_put(enum_container_field);
492 bt_ctf_field_put(variant_field);
493 bt_ctf_field_put(ret_field);
494 bt_ctf_field_type_put(uint_35_type);
495 bt_ctf_field_type_put(int_16_type);
496 bt_ctf_field_type_put(string_type);
497 bt_ctf_field_type_put(sequence_type);
498 bt_ctf_field_type_put(inner_structure_type);
499 bt_ctf_field_type_put(complex_structure_type);
500 bt_ctf_field_type_put(uint_3_type);
501 bt_ctf_field_type_put(enum_variant_type);
502 bt_ctf_field_type_put(variant_type);
503 bt_ctf_event_class_put(event_class);
504 bt_ctf_event_put(event);
505 }
506
507 void type_field_tests()
508 {
509 struct bt_ctf_field *uint_12;
510 struct bt_ctf_field *int_16;
511 struct bt_ctf_field *string;
512 struct bt_ctf_field *enumeration;
513 struct bt_ctf_field_type *composite_structure_type;
514 struct bt_ctf_field_type *structure_seq_type;
515 struct bt_ctf_field_type *string_type;
516 struct bt_ctf_field_type *sequence_type;
517 struct bt_ctf_field_type *uint_8_type;
518 struct bt_ctf_field_type *int_16_type;
519 struct bt_ctf_field_type *uint_12_type =
520 bt_ctf_field_type_integer_create(12);
521 struct bt_ctf_field_type *enumeration_type;
522 struct bt_ctf_field_type *enumeration_sequence_type;
523 struct bt_ctf_field_type *enumeration_array_type;
524
525 ok(uint_12_type, "Create an unsigned integer type");
526 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
527 BT_CTF_INTEGER_BASE_BINARY) == 0,
528 "Set integer type's base as binary");
529 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
530 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
531 "Set integer type's base as decimal");
532 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
533 BT_CTF_INTEGER_BASE_UNKNOWN),
534 "Reject integer type's base set as unknown");
535 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
536 BT_CTF_INTEGER_BASE_OCTAL) == 0,
537 "Set integer type's base as octal");
538 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
539 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
540 "Set integer type's base as hexadecimal");
541 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
542 "Reject unknown integer base value");
543 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
544 "Set integer type signedness to signed");
545 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
546 "Set integer type signedness to unsigned");
547
548 int_16_type = bt_ctf_field_type_integer_create(16);
549 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
550 uint_8_type = bt_ctf_field_type_integer_create(8);
551 sequence_type =
552 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
553 ok(sequence_type, "Create a sequence of int16_t type");
554
555 string_type = bt_ctf_field_type_string_create();
556 ok(string_type, "Create a string type");
557 ok(bt_ctf_field_type_string_set_encoding(string_type,
558 CTF_STRING_NONE),
559 "Reject invalid \"None\" string encoding");
560 ok(bt_ctf_field_type_string_set_encoding(string_type,
561 42),
562 "Reject invalid string encoding");
563 ok(bt_ctf_field_type_string_set_encoding(string_type,
564 CTF_STRING_ASCII) == 0,
565 "Set string encoding to ASCII");
566
567 structure_seq_type = bt_ctf_field_type_structure_create();
568 ok(structure_seq_type, "Create a structure type");
569 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
570 uint_8_type, "seq_len") == 0,
571 "Add a uint8_t type to a structure");
572 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
573 sequence_type, "a_sequence") == 0,
574 "Add a sequence type to a structure");
575 composite_structure_type = bt_ctf_field_type_structure_create();
576 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
577 string_type, "a_string") == 0,
578 "Add a string type to a structure");
579 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
580 structure_seq_type, "inner_structure") == 0,
581 "Add a structure type to a structure");
582
583 int_16 = bt_ctf_field_create(int_16_type);
584 ok(int_16, "Instanciate a signed 16-bit integer");
585 uint_12 = bt_ctf_field_create(uint_12_type);
586 ok(uint_12, "Instanciate an unsigned 12-bit integer");
587
588 /* Can't modify types after instanciating them */
589 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
590 BT_CTF_INTEGER_BASE_DECIMAL),
591 "Check an integer type' base can't be modified after instanciation");
592 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
593 "Check an integer type's signedness can't be modified after instanciation");
594
595 /* Check signed property is checked */
596 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
597 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
598 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
599 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
600
601 /* Check overflows are properly tested for */
602 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
603 "Check -32768 is allowed for a signed 16-bit integer");
604 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
605 "Check 32767 is allowed for a signed 16-bit integer");
606 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
607 "Check 32768 is not allowed for a signed 16-bit integer");
608 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
609 "Check -32769 is not allowed for a signed 16-bit integer");
610 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
611 "Check -42 is allowed for a signed 16-bit integer");
612
613 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
614 "Check 4095 is allowed for an unsigned 12-bit integer");
615 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
616 "Check 4096 is not allowed for a unsigned 12-bit integer");
617 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
618 "Check 0 is allowed for an unsigned 12-bit integer");
619
620 string = bt_ctf_field_create(string_type);
621 ok(string, "Instanciate a string field");
622 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
623 "Set a string's value");
624
625 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
626 ok(enumeration_type,
627 "Create an enumeration type with an unsigned 12-bit integer as container");
628 enumeration_sequence_type = bt_ctf_field_type_sequence_create(
629 enumeration_type, "count");
630 ok(!enumeration_sequence_type,
631 "Check enumeration types are validated when creating a sequence");
632 enumeration_array_type = bt_ctf_field_type_array_create(
633 enumeration_type, 10);
634 ok(!enumeration_array_type,
635 "Check enumeration types are validated when creating an array");
636 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
637 enumeration_type, "enumeration") == 0,
638 "Check enumeration types are validated when adding them as structure members");
639 enumeration = bt_ctf_field_create(enumeration_type);
640 ok(!enumeration,
641 "Check enumeration types are validated before instantiation");
642
643 bt_ctf_field_put(string);
644 bt_ctf_field_put(uint_12);
645 bt_ctf_field_put(int_16);
646 bt_ctf_field_put(enumeration);
647 bt_ctf_field_type_put(composite_structure_type);
648 bt_ctf_field_type_put(structure_seq_type);
649 bt_ctf_field_type_put(string_type);
650 bt_ctf_field_type_put(sequence_type);
651 bt_ctf_field_type_put(uint_8_type);
652 bt_ctf_field_type_put(int_16_type);
653 bt_ctf_field_type_put(uint_12_type);
654 bt_ctf_field_type_put(enumeration_type);
655 bt_ctf_field_type_put(enumeration_sequence_type);
656 bt_ctf_field_type_put(enumeration_array_type);
657 }
658
659 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
660 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
661 {
662 /*
663 * Append enough events to force the underlying packet to be resized.
664 * Also tests that a new event can be declared after a stream has been
665 * instantiated and used/flushed.
666 */
667 int ret = 0;
668 int i;
669 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
670 "Spammy_Event");
671 struct bt_ctf_field_type *integer_type =
672 bt_ctf_field_type_integer_create(17);
673 struct bt_ctf_field_type *string_type =
674 bt_ctf_field_type_string_create();
675
676 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
677 "field_1");
678 ret |= bt_ctf_event_class_add_field(event_class, string_type,
679 "a_string");
680 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
681 ok(ret == 0, "Add a new event class to a stream class after writing an event");
682 if (ret) {
683 goto end;
684 }
685
686 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
687 struct bt_ctf_event *event = bt_ctf_event_create(event_class);
688 struct bt_ctf_field *integer =
689 bt_ctf_field_create(integer_type);
690 struct bt_ctf_field *string =
691 bt_ctf_field_create(string_type);
692
693 ret |= bt_ctf_clock_set_time(clock, ++current_time);
694 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
695 ret |= bt_ctf_event_set_payload(event, "field_1",
696 integer);
697 bt_ctf_field_put(integer);
698 ret |= bt_ctf_field_string_set_value(string, "This is a test");
699 ret |= bt_ctf_event_set_payload(event, "a_string",
700 string);
701 bt_ctf_field_put(string);
702 ret |= bt_ctf_stream_append_event(stream, event);
703 bt_ctf_event_put(event);
704
705 if (ret) {
706 break;
707 }
708 }
709 end:
710 ok(ret == 0, "Append 100 000 events to a stream");
711 ok(bt_ctf_stream_flush(stream) == 0,
712 "Flush a stream that forces a packet resize");
713 bt_ctf_field_type_put(integer_type);
714 bt_ctf_field_type_put(string_type);
715 bt_ctf_event_class_put(event_class);
716 }
717
718 int main(int argc, char **argv)
719 {
720 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
721 char metadata_path[sizeof(trace_path) + 9];
722 const char *clock_name = "test_clock";
723 const char *clock_description = "This is a test clock";
724 const char *returned_clock_name;
725 const char *returned_clock_description;
726 const uint64_t frequency = 1123456789;
727 const uint64_t offset_s = 1351530929945824323;
728 const uint64_t offset = 1234567;
729 const uint64_t precision = 10;
730 const int is_absolute = 0xFF;
731 char *metadata_string;
732 struct bt_ctf_writer *writer;
733 struct utsname name;
734 char hostname[HOST_NAME_MAX];
735 struct bt_ctf_clock *clock;
736 struct bt_ctf_stream_class *stream_class;
737 struct bt_ctf_stream *stream1;
738
739 if (argc < 3) {
740 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
741 exit(-1);
742 }
743
744 plan_no_plan();
745
746 if (!mkdtemp(trace_path)) {
747 perror("# perror");
748 }
749
750 strcpy(metadata_path, trace_path);
751 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
752
753 writer = bt_ctf_writer_create(trace_path);
754 ok(writer, "bt_ctf_create succeeds in creating trace with path");
755
756 /* Add environment context to the trace */
757 gethostname(hostname, HOST_NAME_MAX);
758 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
759 "Add host (%s) environment field to writer instance",
760 hostname);
761 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
762 "test_value"),
763 "bt_ctf_writer_add_environment_field error with NULL writer");
764 ok(bt_ctf_writer_add_environment_field(writer, NULL,
765 "test_value"),
766 "bt_ctf_writer_add_environment_field error with NULL field name");
767 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
768 NULL),
769 "bt_ctf_writer_add_environment_field error with NULL field value");
770
771 if (uname(&name)) {
772 perror("uname");
773 return -1;
774 }
775
776 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
777 == 0, "Add sysname (%s) environment field to writer instance",
778 name.sysname);
779 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
780 name.nodename) == 0,
781 "Add nodename (%s) environment field to writer instance",
782 name.nodename);
783 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
784 == 0, "Add release (%s) environment field to writer instance",
785 name.release);
786 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
787 == 0, "Add version (%s) environment field to writer instance",
788 name.version);
789 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
790 == 0, "Add machine (%s) environment field to writer istance",
791 name.machine);
792
793 /* Define a clock and add it to the trace */
794 ok(bt_ctf_clock_create("signed") == NULL,
795 "Illegal clock name rejected");
796 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
797 clock = bt_ctf_clock_create(clock_name);
798 ok(clock, "Clock created sucessfully");
799 returned_clock_name = bt_ctf_clock_get_name(clock);
800 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
801 ok(strcmp(returned_clock_name, clock_name) == 0,
802 "Returned clock name is valid");
803
804 returned_clock_description = bt_ctf_clock_get_description(clock);
805 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
806 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
807 "Clock description set successfully");
808
809 returned_clock_description = bt_ctf_clock_get_description(clock);
810 ok(returned_clock_description,
811 "bt_ctf_clock_get_description returns a description.");
812 ok(strcmp(returned_clock_description, clock_description) == 0,
813 "Returned clock description is valid");
814
815 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
816 "bt_ctf_clock_get_frequency returns the correct default frequency");
817 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
818 "Set clock frequency");
819 ok(bt_ctf_clock_get_frequency(clock) == frequency,
820 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
821
822 ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S,
823 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
824 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
825 "Set clock offset (seconds)");
826 ok(bt_ctf_clock_get_offset_s(clock) == offset_s,
827 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
828
829 ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET,
830 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
831 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
832 ok(bt_ctf_clock_get_offset(clock) == offset,
833 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
834
835 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
836 "bt_ctf_clock_get_precision returns the correct default precision");
837 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
838 "Set clock precision");
839 ok(bt_ctf_clock_get_precision(clock) == precision,
840 "bt_ctf_clock_get_precision returns the correct precision once it is set");
841
842 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
843 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
844 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
845 "Set clock absolute property");
846 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
847 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
848
849 ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME,
850 "bt_ctf_clock_get_time returns the correct default time");
851 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
852 "Set clock time");
853 ok(bt_ctf_clock_get_time(clock) == current_time,
854 "bt_ctf_clock_get_time returns the correct time once it is set");
855
856 ok(bt_ctf_writer_add_clock(writer, clock) == 0,
857 "Add clock to writer instance");
858 ok(bt_ctf_writer_add_clock(writer, clock),
859 "Verify a clock can't be added twice to a writer instance");
860
861 ok(!bt_ctf_clock_get_name(NULL),
862 "bt_ctf_clock_get_name correctly handles NULL");
863 ok(!bt_ctf_clock_get_description(NULL),
864 "bt_ctf_clock_get_description correctly handles NULL");
865 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
866 "bt_ctf_clock_get_frequency correctly handles NULL");
867 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
868 "bt_ctf_clock_get_precision correctly handles NULL");
869 ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL,
870 "bt_ctf_clock_get_offset_s correctly handles NULL");
871 ok(bt_ctf_clock_get_offset(NULL) == -1ULL,
872 "bt_ctf_clock_get_offset correctly handles NULL");
873 ok(bt_ctf_clock_get_is_absolute(NULL) == -1,
874 "bt_ctf_clock_get_is_absolute correctly handles NULL");
875 ok(bt_ctf_clock_get_time(NULL) == -1ULL,
876 "bt_ctf_clock_get_time correctly handles NULL");
877
878 ok(bt_ctf_clock_set_description(NULL, NULL) == -1,
879 "bt_ctf_clock_set_description correctly handles NULL clock");
880 ok(bt_ctf_clock_set_frequency(NULL, frequency) == -1,
881 "bt_ctf_clock_set_frequency correctly handles NULL clock");
882 ok(bt_ctf_clock_set_precision(NULL, precision) == -1,
883 "bt_ctf_clock_get_precision correctly handles NULL clock");
884 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) == -1,
885 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
886 ok(bt_ctf_clock_set_offset(NULL, offset) == -1,
887 "bt_ctf_clock_set_offset correctly handles NULL clock");
888 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) == -1,
889 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
890 ok(bt_ctf_clock_set_time(NULL, current_time) == -1,
891 "bt_ctf_clock_set_time correctly handles NULL clock");
892
893 /* Define a stream class */
894 stream_class = bt_ctf_stream_class_create("test_stream");
895 ok(stream_class, "Create stream class");
896 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
897 "Set a stream class' clock");
898
899 /* Test the event fields and event types APIs */
900 type_field_tests();
901
902 /* Instantiate a stream and append events */
903 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
904 ok(stream1, "Instanciate a stream class from writer");
905
906 /* Should fail after instanciating a stream (locked)*/
907 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
908 "Changes to a stream class that was already instantiated fail");
909
910 append_simple_event(stream_class, stream1, clock);
911
912 packet_resize_test(stream_class, stream1, clock);
913
914 append_complex_event(stream_class, stream1, clock);
915
916 metadata_string = bt_ctf_writer_get_metadata_string(writer);
917 ok(metadata_string, "Get metadata string");
918
919 bt_ctf_writer_flush_metadata(writer);
920 validate_metadata(argv[1], metadata_path);
921 validate_trace(argv[2], trace_path);
922
923 bt_ctf_clock_put(clock);
924 bt_ctf_stream_class_put(stream_class);
925 bt_ctf_writer_put(writer);
926 bt_ctf_stream_put(stream1);
927 free(metadata_string);
928
929 /* Remove all trace files and delete temporary trace directory */
930 DIR *trace_dir = opendir(trace_path);
931 if (!trace_dir) {
932 perror("# opendir");
933 return -1;
934 }
935
936 struct dirent *entry;
937 while ((entry = readdir(trace_dir))) {
938 if (entry->d_type == DT_REG) {
939 unlinkat(dirfd(trace_dir), entry->d_name, 0);
940 }
941 }
942
943 rmdir(trace_path);
944 closedir(trace_dir);
945
946 return 0;
947 }
This page took 0.080303 seconds and 4 git commands to generate.