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