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