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