Port: Include config.h globally trough DEFAULT_INCLUDES
[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 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-ir/stream-class.h>
29 #include <babeltrace/ref.h>
30 #include <babeltrace/ctf/events.h>
31 #include <babeltrace/values.h>
32 #include <unistd.h>
33 #include <babeltrace/compat/stdlib.h>
34 #include <stdio.h>
35 #include <sys/utsname.h>
36 #include <babeltrace/compat/limits.h>
37 #include <string.h>
38 #include <assert.h>
39 #include <sys/wait.h>
40 #include <fcntl.h>
41 #include <babeltrace/compat/dirent.h>
42 #include "tap/tap.h"
43 #include <math.h>
44 #include <float.h>
45
46 #define METADATA_LINE_SIZE 512
47 #define SEQUENCE_TEST_LENGTH 10
48 #define ARRAY_TEST_LENGTH 5
49 #define PACKET_RESIZE_TEST_LENGTH 100000
50
51 #define DEFAULT_CLOCK_FREQ 1000000000
52 #define DEFAULT_CLOCK_PRECISION 1
53 #define DEFAULT_CLOCK_OFFSET 0
54 #define DEFAULT_CLOCK_OFFSET_S 0
55 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
56 #define DEFAULT_CLOCK_TIME 0
57
58 static uint64_t current_time = 42;
59
60 /* Return 1 if uuids match, zero if different. */
61 int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b)
62 {
63 int ret = 0;
64 int i;
65
66 if (!uuid_a || !uuid_b) {
67 goto end;
68 }
69
70 for (i = 0; i < 16; i++) {
71 if (uuid_a[i] != uuid_b[i]) {
72 goto end;
73 }
74 }
75
76 ret = 1;
77 end:
78 return ret;
79 }
80
81 void validate_metadata(char *parser_path, char *metadata_path)
82 {
83 int ret = 0;
84 char parser_output_path[] = "/tmp/parser_output_XXXXXX";
85 int parser_output_fd = -1, metadata_fd = -1;
86
87 if (!metadata_path) {
88 ret = -1;
89 goto result;
90 }
91
92 parser_output_fd = mkstemp(parser_output_path);
93 metadata_fd = open(metadata_path, O_RDONLY);
94
95 unlink(parser_output_path);
96
97 if (parser_output_fd == -1 || metadata_fd == -1) {
98 diag("Failed create temporary files for metadata parsing.");
99 ret = -1;
100 goto result;
101 }
102
103 pid_t pid = fork();
104 if (pid) {
105 int status = 0;
106 waitpid(pid, &status, 0);
107 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
108 } else {
109 /* ctf-parser-test expects a metadata string on stdin. */
110 ret = dup2(metadata_fd, STDIN_FILENO);
111 if (ret < 0) {
112 perror("# dup2 metadata_fd to STDIN");
113 goto result;
114 }
115
116 ret = dup2(parser_output_fd, STDOUT_FILENO);
117 if (ret < 0) {
118 perror("# dup2 parser_output_fd to STDOUT");
119 goto result;
120 }
121
122 ret = dup2(parser_output_fd, STDERR_FILENO);
123 if (ret < 0) {
124 perror("# dup2 parser_output_fd to STDERR");
125 goto result;
126 }
127
128 execl(parser_path, "ctf-parser-test", NULL);
129 perror("# Could not launch the ctf metadata parser process");
130 exit(-1);
131 }
132 result:
133 ok(ret == 0, "Metadata string is valid");
134
135 if (ret && metadata_fd >= 0 && parser_output_fd >= 0) {
136 char *line;
137 size_t len = METADATA_LINE_SIZE;
138 FILE *metadata_fp = NULL, *parser_output_fp = NULL;
139
140 metadata_fp = fdopen(metadata_fd, "r");
141 if (!metadata_fp) {
142 perror("fdopen on metadata_fd");
143 goto close_fp;
144 }
145 metadata_fd = -1;
146
147 parser_output_fp = fdopen(parser_output_fd, "r");
148 if (!parser_output_fp) {
149 perror("fdopen on parser_output_fd");
150 goto close_fp;
151 }
152 parser_output_fd = -1;
153
154 line = malloc(len);
155 if (!line) {
156 diag("malloc failure");
157 }
158
159 rewind(metadata_fp);
160
161 /* Output the metadata and parser output as diagnostic */
162 while (getline(&line, &len, metadata_fp) > 0) {
163 fprintf(stderr, "# %s", line);
164 }
165
166 rewind(parser_output_fp);
167 while (getline(&line, &len, parser_output_fp) > 0) {
168 fprintf(stderr, "# %s", line);
169 }
170
171 free(line);
172 close_fp:
173 if (metadata_fp) {
174 if (fclose(metadata_fp)) {
175 diag("fclose failure");
176 }
177 }
178 if (parser_output_fp) {
179 if (fclose(parser_output_fp)) {
180 diag("fclose failure");
181 }
182 }
183 }
184
185 if (parser_output_fd >= 0) {
186 if (close(parser_output_fd)) {
187 diag("close error");
188 }
189 }
190 if (metadata_fd >= 0) {
191 if (close(metadata_fd)) {
192 diag("close error");
193 }
194 }
195 }
196
197 void validate_trace(char *parser_path, char *trace_path)
198 {
199 int ret = 0;
200 char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX";
201 int babeltrace_output_fd = -1;
202
203 if (!trace_path) {
204 ret = -1;
205 goto result;
206 }
207
208 babeltrace_output_fd = mkstemp(babeltrace_output_path);
209 unlink(babeltrace_output_path);
210
211 if (babeltrace_output_fd == -1) {
212 diag("Failed to create a temporary file for trace parsing.");
213 ret = -1;
214 goto result;
215 }
216
217 pid_t pid = fork();
218 if (pid) {
219 int status = 0;
220 waitpid(pid, &status, 0);
221 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
222 } else {
223 ret = dup2(babeltrace_output_fd, STDOUT_FILENO);
224 if (ret < 0) {
225 perror("# dup2 babeltrace_output_fd to STDOUT");
226 goto result;
227 }
228
229 ret = dup2(babeltrace_output_fd, STDERR_FILENO);
230 if (ret < 0) {
231 perror("# dup2 babeltrace_output_fd to STDERR");
232 goto result;
233 }
234
235 execl(parser_path, "babeltrace", trace_path, NULL);
236 perror("# Could not launch the babeltrace process");
237 exit(-1);
238 }
239 result:
240 ok(ret == 0, "Babeltrace could read the resulting trace");
241
242 if (ret && babeltrace_output_fd >= 0) {
243 char *line;
244 size_t len = METADATA_LINE_SIZE;
245 FILE *babeltrace_output_fp = NULL;
246
247 babeltrace_output_fp = fdopen(babeltrace_output_fd, "r");
248 if (!babeltrace_output_fp) {
249 perror("fdopen on babeltrace_output_fd");
250 goto close_fp;
251 }
252 babeltrace_output_fd = -1;
253
254 line = malloc(len);
255 if (!line) {
256 diag("malloc error");
257 }
258 rewind(babeltrace_output_fp);
259 while (getline(&line, &len, babeltrace_output_fp) > 0) {
260 diag("%s", line);
261 }
262
263 free(line);
264 close_fp:
265 if (babeltrace_output_fp) {
266 if (fclose(babeltrace_output_fp)) {
267 diag("fclose error");
268 }
269 }
270 }
271
272 if (babeltrace_output_fd >= 0) {
273 if (close(babeltrace_output_fd)) {
274 diag("close error");
275 }
276 }
277 }
278
279 void event_copy_tests(struct bt_ctf_event *event)
280 {
281 struct bt_ctf_event *copy;
282 struct bt_ctf_event_class *orig_event_class;
283 struct bt_ctf_event_class *copy_event_class;
284 struct bt_ctf_stream *orig_stream;
285 struct bt_ctf_stream *copy_stream;
286 struct bt_ctf_field *orig_field;
287 struct bt_ctf_field *copy_field;
288
289 /* copy */
290 ok(!bt_ctf_event_copy(NULL),
291 "bt_ctf_event_copy handles NULL correctly");
292 copy = bt_ctf_event_copy(event);
293 ok(copy, "bt_ctf_event_copy returns a valid pointer");
294
295 /* validate event class */
296 orig_event_class = bt_ctf_event_get_class(event);
297 assert(orig_event_class);
298 copy_event_class = bt_ctf_event_get_class(copy);
299 ok(orig_event_class == copy_event_class,
300 "original and copied events share the same event class pointer");
301 bt_put(orig_event_class);
302 bt_put(copy_event_class);
303
304 /* validate stream */
305 orig_stream = bt_ctf_event_get_stream(event);
306 copy_stream = bt_ctf_event_get_stream(copy);
307
308 if (!orig_stream) {
309 ok(!copy_stream, "original and copied events have no stream");
310 } else {
311 ok(orig_stream == copy_stream,
312 "original and copied events share the same stream pointer");
313 }
314 bt_put(orig_stream);
315 bt_put(copy_stream);
316
317 /* header */
318 orig_field = bt_ctf_event_get_header(event);
319 copy_field = bt_ctf_event_get_header(copy);
320
321 if (!orig_field) {
322 ok(!copy_field, "original and copied events have no header");
323 } else {
324 ok(orig_field != copy_field,
325 "original and copied events headers are different pointers");
326 }
327
328 bt_put(orig_field);
329 bt_put(copy_field);
330
331 /* context */
332 orig_field = bt_ctf_event_get_event_context(event);
333 copy_field = bt_ctf_event_get_event_context(copy);
334
335 if (!orig_field) {
336 ok(!copy_field, "original and copied events have no context");
337 } else {
338 ok(orig_field != copy_field,
339 "original and copied events contexts are different pointers");
340 }
341
342 bt_put(orig_field);
343 bt_put(copy_field);
344
345 /* payload */
346 orig_field = bt_ctf_event_get_payload_field(event);
347 copy_field = bt_ctf_event_get_payload_field(copy);
348
349 if (!orig_field) {
350 ok(!copy_field, "original and copied events have no payload");
351 } else {
352 ok(orig_field != copy_field,
353 "original and copied events payloads are different pointers");
354 }
355
356 bt_put(orig_field);
357 bt_put(copy_field);
358
359 bt_put(copy);
360 }
361
362 void append_simple_event(struct bt_ctf_stream_class *stream_class,
363 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
364 {
365 /* Create and add a simple event class */
366 struct bt_ctf_event_class *simple_event_class =
367 bt_ctf_event_class_create("Simple Event");
368 struct bt_ctf_field_type *uint_12_type =
369 bt_ctf_field_type_integer_create(12);
370 struct bt_ctf_field_type *int_64_type =
371 bt_ctf_field_type_integer_create(64);
372 struct bt_ctf_field_type *float_type =
373 bt_ctf_field_type_floating_point_create();
374 struct bt_ctf_field_type *enum_type;
375 struct bt_ctf_field_type *enum_type_unsigned =
376 bt_ctf_field_type_enumeration_create(uint_12_type);
377 struct bt_ctf_field_type *event_context_type =
378 bt_ctf_field_type_structure_create();
379 struct bt_ctf_field_type *returned_type;
380 struct bt_ctf_event *simple_event;
381 struct bt_ctf_field *integer_field;
382 struct bt_ctf_field *float_field;
383 struct bt_ctf_field *enum_field;
384 struct bt_ctf_field *enum_field_unsigned;
385 struct bt_ctf_field *enum_container_field;
386 const char *mapping_name_test = "truie";
387 const double double_test_value = 3.1415;
388 struct bt_ctf_field *enum_container_field_unsigned;
389 const char *mapping_name_negative_test = "negative_value";
390 const char *ret_char;
391 double ret_double;
392 int64_t ret_range_start_int64_t, ret_range_end_int64_t;
393 uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
394 struct bt_ctf_clock *ret_clock;
395 struct bt_ctf_event_class *ret_event_class;
396 struct bt_ctf_field *packet_context;
397 struct bt_ctf_field *packet_context_field;
398 struct bt_ctf_field *stream_event_context;
399 struct bt_ctf_field *stream_event_context_field;
400 struct bt_ctf_field *event_context;
401 struct bt_ctf_field *event_context_field;
402
403 ok(uint_12_type, "Create an unsigned integer type");
404
405 bt_ctf_field_type_integer_set_signed(int_64_type, 1);
406 ok(int_64_type, "Create a signed integer type");
407 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
408
409 returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type);
410 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
411 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
412 ok(!bt_ctf_field_type_enumeration_create(enum_type),
413 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
414 bt_put(returned_type);
415
416 bt_ctf_field_type_set_alignment(float_type, 32);
417 ok(bt_ctf_field_type_get_alignment(NULL) < 0,
418 "bt_ctf_field_type_get_alignment handles NULL correctly");
419 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
420 "bt_ctf_field_type_get_alignment returns a correct value");
421
422 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
423 "Set a floating point type's exponent digit count");
424 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
425 "Set a floating point type's mantissa digit count");
426
427 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0,
428 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
429 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0,
430 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
431 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
432 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
433 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
434 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
435
436 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
437 mapping_name_negative_test, -12345, 0) == 0,
438 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
439 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
440 "escaping; \"test\"", 1, 1) == 0,
441 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
442 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
443 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
444 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
445 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
446 "event clock int float", 5, 22) == 0,
447 "Accept enumeration mapping strings containing reserved keywords");
448 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
449 42, 42);
450 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
451 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
452 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
453 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
454 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
455 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
456 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
457
458 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42) < 0,
459 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
460 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000) < 0,
461 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
462 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55) == 1,
463 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
464
465 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
466 "enum_field") == 0, "Add signed enumeration field to event");
467
468 ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char,
469 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
470 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
471 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL,
472 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
473 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
474 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
475 NULL, &ret_range_end_int64_t) < 0,
476 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
477 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
478 &ret_range_start_int64_t, NULL) < 0,
479 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
480 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char,
481 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
482 "bt_ctf_field_type_enumeration_get_mapping returns a value");
483 ok(!strcmp(ret_char, mapping_name_test),
484 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
485 ok(ret_range_start_int64_t == 42,
486 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
487 ok(ret_range_end_int64_t == 42,
488 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
489
490 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
491 "escaping; \"test\"", 0, 0) == 0,
492 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
493 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
494 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
495 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
496 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
497 "event clock int float", 5, 22) == 0,
498 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
499 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
500 42, 42);
501 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
502 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
503 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something",
504 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
505 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
506 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
507 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
508 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
509
510 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0,
511 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
512 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4,
513 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
514
515 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char,
516 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
517 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
518 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL,
519 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
520 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
521 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
522 NULL, &ret_range_end_uint64_t) < 0,
523 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
524 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
525 &ret_range_start_uint64_t, NULL) < 0,
526 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
527 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char,
528 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
529 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
530 ok(!strcmp(ret_char, mapping_name_test),
531 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
532 ok(ret_range_start_uint64_t == 42,
533 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
534 ok(ret_range_end_uint64_t == 42,
535 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
536
537 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
538 "integer_field");
539 bt_ctf_event_class_add_field(simple_event_class, float_type,
540 "float_field");
541
542 assert(!bt_ctf_event_class_set_id(simple_event_class, 13));
543
544 /* Set an event context type which will contain a single integer*/
545 ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
546 "event_specific_context"),
547 "Add event specific context field");
548 ok(bt_ctf_event_class_get_context_type(NULL) == NULL,
549 "bt_ctf_event_class_get_context_type handles NULL correctly");
550 ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL,
551 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
552
553 ok(bt_ctf_event_class_set_context_type(simple_event_class, NULL) < 0,
554 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
555 ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0,
556 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
557 ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type),
558 "Set an event class' context type successfully");
559 returned_type = bt_ctf_event_class_get_context_type(simple_event_class);
560 ok(returned_type == event_context_type,
561 "bt_ctf_event_class_get_context_type returns the appropriate type");
562 bt_put(returned_type);
563
564 bt_ctf_stream_class_add_event_class(stream_class, simple_event_class);
565
566 ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0,
567 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
568 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
569 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
570 ok(bt_ctf_stream_class_get_event_class(NULL, 0) == NULL,
571 "bt_ctf_stream_class_get_event_class handles NULL correctly");
572 ok(bt_ctf_stream_class_get_event_class(stream_class, 8724) == NULL,
573 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
574 ret_event_class = bt_ctf_stream_class_get_event_class(stream_class, 0);
575 ok(ret_event_class == simple_event_class,
576 "bt_ctf_stream_class_get_event_class returns the correct event class");
577 bt_put(ret_event_class);
578 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL, 0),
579 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
580 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class, 2),
581 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
582 ret_event_class =
583 bt_ctf_stream_class_get_event_class_by_id(stream_class, 13);
584 ok(ret_event_class == simple_event_class,
585 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
586 bt_put(ret_event_class);
587
588 ok(bt_ctf_stream_class_get_event_class_by_name(NULL, "some event name") == NULL,
589 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
590 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, NULL) == NULL,
591 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
592 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, "some event name") == NULL,
593 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
594 ret_event_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, "Simple Event");
595 ok(ret_event_class == simple_event_class,
596 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
597 bt_put(ret_event_class);
598
599 simple_event = bt_ctf_event_create(simple_event_class);
600 ok(simple_event,
601 "Instantiate an event containing a single integer field");
602
603 ok(bt_ctf_event_get_clock(NULL) == NULL,
604 "bt_ctf_event_get_clock handles NULL correctly");
605 ret_clock = bt_ctf_event_get_clock(simple_event);
606 ok(ret_clock == clock,
607 "bt_ctf_event_get_clock returns a correct clock");
608 bt_put(clock);
609
610 integer_field = bt_ctf_field_create(uint_12_type);
611 bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
612 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
613 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
614
615 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
616 ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double),
617 "bt_ctf_field_floating_point_get_value fails on an unset float field");
618 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
619 ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double),
620 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
621 ok(bt_ctf_field_floating_point_get_value(float_field, NULL),
622 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
623 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
624 "bt_ctf_field_floating_point_get_value returns a double value");
625 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
626 "bt_ctf_field_floating_point_get_value returns a correct value");
627
628 enum_field = bt_ctf_field_create(enum_type);
629 ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL);
630 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
631 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
632 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
633 enum_container_field = bt_ctf_field_enumeration_get_container(
634 enum_field);
635 ok(bt_ctf_field_signed_integer_set_value(
636 enum_container_field, -42) == 0,
637 "Set signed enumeration container value");
638 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
639 ok(!strcmp(ret_char, mapping_name_negative_test),
640 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
641 bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
642
643 enum_field_unsigned = bt_ctf_field_create(enum_type_unsigned);
644 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
645 enum_field_unsigned);
646 ok(bt_ctf_field_unsigned_integer_set_value(
647 enum_container_field_unsigned, 42) == 0,
648 "Set unsigned enumeration container value");
649 bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
650 enum_field_unsigned);
651 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned);
652 ok(ret_char && !strcmp(ret_char, mapping_name_test),
653 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
654
655 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
656
657 /* Populate stream event context */
658 stream_event_context = bt_ctf_stream_get_event_context(stream);
659 stream_event_context_field = bt_ctf_field_structure_get_field(
660 stream_event_context, "common_event_context");
661 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field, 42);
662
663 /* Populate the event's context */
664 ok(bt_ctf_event_get_event_context(NULL) == NULL,
665 "bt_ctf_event_get_event_context handles NULL correctly");
666 event_context = bt_ctf_event_get_event_context(simple_event);
667 ok(event_context,
668 "bt_ctf_event_get_event_context returns a field");
669 returned_type = bt_ctf_field_get_type(event_context);
670 ok(returned_type == event_context_type,
671 "bt_ctf_event_get_event_context returns a field of the appropriate type");
672 event_context_field = bt_ctf_field_structure_get_field(event_context,
673 "event_specific_context");
674 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field, 1234),
675 "Successfully set an event context's value");
676 ok(bt_ctf_event_set_event_context(NULL, event_context) < 0,
677 "bt_ctf_event_set_event_context handles a NULL event correctly");
678 ok(bt_ctf_event_set_event_context(simple_event, NULL) < 0,
679 "bt_ctf_event_set_event_context handles a NULL event context correctly");
680 ok(bt_ctf_event_set_event_context(simple_event, event_context_field) < 0,
681 "bt_ctf_event_set_event_context rejects a context of the wrong type");
682 ok(!bt_ctf_event_set_event_context(simple_event, event_context),
683 "Set an event context successfully");
684
685 event_copy_tests(simple_event);
686 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
687 "Append simple event to trace stream");
688
689 ok(bt_ctf_stream_get_packet_context(NULL) == NULL,
690 "bt_ctf_stream_get_packet_context handles NULL correctly");
691 packet_context = bt_ctf_stream_get_packet_context(stream);
692 ok(packet_context,
693 "bt_ctf_stream_get_packet_context returns a packet context");
694
695 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
696 "packet_size");
697 ok(packet_context_field,
698 "Packet context contains the default packet_size field.");
699 bt_put(packet_context_field);
700 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
701 "custom_packet_context_field");
702 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field, 8) == 0,
703 "Custom packet context field value successfully set.");
704
705 ok(bt_ctf_stream_set_packet_context(NULL, packet_context_field) < 0,
706 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
707 ok(bt_ctf_stream_set_packet_context(stream, NULL) < 0,
708 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
709 ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0,
710 "Successfully set a stream's packet context");
711
712 ok(bt_ctf_stream_flush(stream) == 0,
713 "Flush trace stream with one event");
714
715 bt_put(simple_event_class);
716 bt_put(simple_event);
717 bt_put(uint_12_type);
718 bt_put(int_64_type);
719 bt_put(float_type);
720 bt_put(enum_type);
721 bt_put(enum_type_unsigned);
722 bt_put(returned_type);
723 bt_put(event_context_type);
724 bt_put(integer_field);
725 bt_put(float_field);
726 bt_put(enum_field);
727 bt_put(enum_field_unsigned);
728 bt_put(enum_container_field);
729 bt_put(enum_container_field_unsigned);
730 bt_put(packet_context);
731 bt_put(packet_context_field);
732 bt_put(stream_event_context);
733 bt_put(stream_event_context_field);
734 bt_put(event_context);
735 bt_put(event_context_field);
736 }
737
738 void append_complex_event(struct bt_ctf_stream_class *stream_class,
739 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
740 {
741 struct event_class_attrs_counts {
742 int id;
743 int name;
744 int loglevel;
745 int modelemfuri;
746 int unknown;
747 } attrs_count;
748
749 int i;
750 int ret;
751 int64_t int64_value;
752 struct event_class_attrs_counts ;
753 const char *complex_test_event_string = "Complex Test Event";
754 const char *test_string_1 = "Test ";
755 const char *test_string_2 = "string ";
756 const char *test_string_3 = "abcdefghi";
757 const char *test_string_4 = "abcd\0efg\0hi";
758 const char *test_string_cat = "Test string abcdeabcd";
759 struct bt_ctf_field_type *uint_35_type =
760 bt_ctf_field_type_integer_create(35);
761 struct bt_ctf_field_type *int_16_type =
762 bt_ctf_field_type_integer_create(16);
763 struct bt_ctf_field_type *uint_3_type =
764 bt_ctf_field_type_integer_create(3);
765 struct bt_ctf_field_type *enum_variant_type =
766 bt_ctf_field_type_enumeration_create(uint_3_type);
767 struct bt_ctf_field_type *variant_type =
768 bt_ctf_field_type_variant_create(enum_variant_type,
769 "variant_selector");
770 struct bt_ctf_field_type *string_type =
771 bt_ctf_field_type_string_create();
772 struct bt_ctf_field_type *sequence_type;
773 struct bt_ctf_field_type *array_type;
774 struct bt_ctf_field_type *inner_structure_type =
775 bt_ctf_field_type_structure_create();
776 struct bt_ctf_field_type *complex_structure_type =
777 bt_ctf_field_type_structure_create();
778 struct bt_ctf_field_type *ret_field_type;
779 struct bt_ctf_event_class *event_class;
780 struct bt_ctf_event *event;
781 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
782 *inner_structure_field, *complex_structure_field,
783 *a_sequence_field, *enum_variant_field, *enum_container_field,
784 *variant_field, *an_array_field, *ret_field;
785 uint64_t ret_unsigned_int;
786 int64_t ret_signed_int;
787 const char *ret_string;
788 struct bt_ctf_stream_class *ret_stream_class;
789 struct bt_ctf_event_class *ret_event_class;
790 struct bt_ctf_field *packet_context, *packet_context_field;
791 struct bt_value *obj;
792
793 ok(bt_ctf_field_type_set_alignment(int_16_type, 0),
794 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
795 ok(bt_ctf_field_type_set_alignment(int_16_type, 3),
796 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
797 ok(bt_ctf_field_type_set_alignment(int_16_type, 24),
798 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
799 ok(!bt_ctf_field_type_set_alignment(int_16_type, 4),
800 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
801 bt_ctf_field_type_set_alignment(int_16_type, 32);
802 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
803 bt_ctf_field_type_integer_set_base(uint_35_type,
804 BT_CTF_INTEGER_BASE_HEXADECIMAL);
805
806 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
807 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
808 "seq_len");
809
810 ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL,
811 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
812 ret_field_type = bt_ctf_field_type_array_get_element_type(
813 array_type);
814 ok(ret_field_type == int_16_type,
815 "bt_ctf_field_type_array_get_element_type returns the correct type");
816 bt_put(ret_field_type);
817
818 ok(bt_ctf_field_type_array_get_length(NULL) < 0,
819 "bt_ctf_field_type_array_get_length handles NULL correctly");
820 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
821 "bt_ctf_field_type_array_get_length returns the correct length");
822
823 bt_ctf_field_type_structure_add_field(inner_structure_type,
824 uint_35_type, "seq_len");
825 bt_ctf_field_type_structure_add_field(inner_structure_type,
826 sequence_type, "a_sequence");
827 bt_ctf_field_type_structure_add_field(inner_structure_type,
828 array_type, "an_array");
829
830 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
831 "UINT3_TYPE", 0, 0);
832 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
833 "INT16_TYPE", 1, 1);
834 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
835 "UINT35_TYPE", 2, 7);
836
837 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL,
838 "INT16_TYPE") < 0,
839 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
840 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
841 enum_variant_type, NULL) < 0,
842 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
843 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
844 enum_variant_type, "INT16_TYPE") == 1,
845 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
846
847 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1) < 0,
848 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
849 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42) < 0,
850 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
851 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5) == 2,
852 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
853
854 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
855 "An unknown entry"), "Reject a variant field based on an unknown tag value");
856 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
857 "UINT3_TYPE") == 0, "Add a field to a variant");
858 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
859 "INT16_TYPE");
860 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
861 "UINT35_TYPE");
862
863 ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL,
864 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
865 ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type);
866 ok(ret_field_type == enum_variant_type,
867 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
868 bt_put(ret_field_type);
869
870 ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL,
871 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
872 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
873 ok(ret_string ? !strcmp(ret_string, "variant_selector") : 0,
874 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
875 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL,
876 "INT16_TYPE") == NULL,
877 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
878 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type,
879 NULL) == NULL,
880 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
881 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
882 variant_type, "INT16_TYPE");
883 ok(ret_field_type == int_16_type,
884 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
885 bt_put(ret_field_type);
886
887 ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0,
888 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
889 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
890 "bt_ctf_field_type_variant_get_field_count returns the correct count");
891
892 ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0,
893 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
894 ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) == 0,
895 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
896 bt_put(ret_field_type);
897 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) == 0,
898 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
899 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0,
900 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
901 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0,
902 "bt_ctf_field_type_variant_get_field returns a field");
903 ok(!strcmp("INT16_TYPE", ret_string),
904 "bt_ctf_field_type_variant_get_field returns a correct field name");
905 ok(ret_field_type == int_16_type,
906 "bt_ctf_field_type_variant_get_field returns a correct field type");
907 bt_put(ret_field_type);
908
909 bt_ctf_field_type_structure_add_field(complex_structure_type,
910 enum_variant_type, "variant_selector");
911 bt_ctf_field_type_structure_add_field(complex_structure_type,
912 string_type, "a_string");
913 bt_ctf_field_type_structure_add_field(complex_structure_type,
914 variant_type, "variant_value");
915 bt_ctf_field_type_structure_add_field(complex_structure_type,
916 inner_structure_type, "inner_structure");
917
918 ok(bt_ctf_event_class_create("clock") == NULL,
919 "Reject creation of an event class with an illegal name");
920 event_class = bt_ctf_event_class_create(complex_test_event_string);
921 ok(event_class, "Create an event class");
922 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
923 "Reject addition of a field with an empty name to an event");
924 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
925 "Reject addition of a field with a NULL type to an event");
926 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
927 "int"),
928 "Reject addition of a type with an illegal name to an event");
929 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
930 "uint_35") == 0,
931 "Add field of type unsigned integer to an event");
932 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
933 "int_16") == 0, "Add field of type signed integer to an event");
934 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
935 "complex_structure") == 0,
936 "Add composite structure to an event");
937
938 ok(bt_ctf_event_class_get_name(NULL) == NULL,
939 "bt_ctf_event_class_get_name handles NULL correctly");
940 ret_string = bt_ctf_event_class_get_name(event_class);
941 ok(!strcmp(ret_string, complex_test_event_string),
942 "bt_ctf_event_class_get_name returns a correct name");
943 ok(bt_ctf_event_class_get_id(event_class) < 0,
944 "bt_ctf_event_class_get_id returns a negative value when not set");
945 ok(bt_ctf_event_class_get_id(NULL) < 0,
946 "bt_ctf_event_class_get_id handles NULL correctly");
947 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
948 "bt_ctf_event_class_set_id handles NULL correctly");
949 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
950 "Set an event class' id");
951 ok(bt_ctf_event_class_get_id(event_class) == 42,
952 "bt_ctf_event_class_get_id returns the correct value");
953
954 /* Test event class attributes */
955 obj = bt_value_integer_create_init(15);
956 assert(obj);
957 ok(bt_ctf_event_class_set_attribute(NULL, "id", obj),
958 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
959 ok(bt_ctf_event_class_set_attribute(event_class, NULL, obj),
960 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
961 ok(bt_ctf_event_class_set_attribute(event_class, "id", NULL),
962 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
963 assert(!bt_value_integer_set(obj, -3));
964 ok(bt_ctf_event_class_set_attribute(event_class, "id", obj),
965 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
966 assert(!bt_value_integer_set(obj, 11));
967 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
968 ok(!ret && bt_ctf_event_class_get_id(event_class) == 11,
969 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
970 ret = bt_ctf_event_class_set_attribute(event_class, "name", obj);
971 ret &= bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj);
972 ok(ret,
973 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
974 BT_PUT(obj);
975
976 obj = bt_value_integer_create_init(5);
977 assert(obj);
978 ok(!bt_ctf_event_class_set_attribute(event_class, "loglevel", obj),
979 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
980 BT_PUT(obj);
981 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL, "loglevel"),
982 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
983 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, NULL),
984 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
985 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, "meow"),
986 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
987 obj = bt_ctf_event_class_get_attribute_value_by_name(event_class,
988 "loglevel");
989 int64_value = 0;
990 ret = bt_value_integer_get(obj, &int64_value);
991 ok(obj && !ret && int64_value == 5,
992 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
993 BT_PUT(obj);
994
995 obj = bt_value_string_create_init("nu name");
996 assert(obj);
997 assert(!bt_ctf_event_class_set_attribute(event_class, "name", obj));
998 ret_string = bt_ctf_event_class_get_name(event_class);
999 ok(!strcmp(ret_string, "nu name"),
1000 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
1001 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
1002 ret &= bt_ctf_event_class_set_attribute(event_class, "loglevel", obj);
1003 ok(ret,
1004 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1005 BT_PUT(obj);
1006 obj = bt_value_string_create_init("http://kernel.org/");
1007 assert(obj);
1008 assert(!bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj));
1009 BT_PUT(obj);
1010
1011 ok(bt_ctf_event_class_get_attribute_count(NULL),
1012 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1013 ok(bt_ctf_event_class_get_attribute_count(event_class) == 4,
1014 "bt_ctf_event_class_get_attribute_count returns the correct count");
1015 ok(!bt_ctf_event_class_get_attribute_name(NULL, 0),
1016 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1017 ok(!bt_ctf_event_class_get_attribute_name(event_class, 4),
1018 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1019 ok(!bt_ctf_event_class_get_attribute_value(NULL, 0),
1020 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1021 ok(!bt_ctf_event_class_get_attribute_value(event_class, 4),
1022 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1023
1024 memset(&attrs_count, 0, sizeof(attrs_count));
1025
1026 for (i = 0; i < 4; ++i) {
1027 ret_string = bt_ctf_event_class_get_attribute_name(event_class,
1028 i);
1029 obj = bt_ctf_event_class_get_attribute_value(event_class, i);
1030 assert(ret_string && obj);
1031
1032 if (!strcmp(ret_string, "id")) {
1033 attrs_count.id++;
1034 ok(bt_value_is_integer(obj),
1035 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1036 ret_string);
1037 } else if (!strcmp(ret_string, "name")) {
1038 attrs_count.name++;
1039 ok(bt_value_is_string(obj),
1040 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1041 ret_string);
1042 } else if (!strcmp(ret_string, "loglevel")) {
1043 attrs_count.loglevel++;
1044 ok(bt_value_is_integer(obj),
1045 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1046 ret_string);
1047 } else if (!strcmp(ret_string, "model.emf.uri")) {
1048 attrs_count.modelemfuri++;
1049 ok(bt_value_is_string(obj),
1050 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1051 ret_string);
1052 } else {
1053 attrs_count.unknown++;
1054 }
1055
1056 BT_PUT(obj);
1057 }
1058
1059 ok(attrs_count.unknown == 0, "event class has no unknown attributes");
1060 ok(attrs_count.id == 1 && attrs_count.name == 1 &&
1061 attrs_count.loglevel == 1 && attrs_count.modelemfuri == 1,
1062 "event class has one instance of each known attribute");
1063
1064 /* Add event class to the stream class */
1065 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
1066 "Reject addition of NULL event class to a stream class");
1067 ok(bt_ctf_stream_class_add_event_class(stream_class,
1068 event_class) == 0, "Add an event class to stream class");
1069
1070 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
1071 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1072 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
1073 ok(ret_stream_class == stream_class,
1074 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1075 bt_put(ret_stream_class);
1076
1077 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
1078 "bt_ctf_event_class_get_field_count handles NULL correctly");
1079 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
1080 "bt_ctf_event_class_get_field_count returns a correct value");
1081
1082 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
1083 &ret_field_type, 0) < 0,
1084 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1085 ok(bt_ctf_event_class_get_field(event_class, NULL,
1086 &ret_field_type, 0) == 0,
1087 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1088 bt_put(ret_field_type);
1089 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1090 NULL, 0) == 0,
1091 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1092 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1093 &ret_field_type, 42) < 0,
1094 "bt_ctf_event_class_get_field handles an invalid index correctly");
1095 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1096 &ret_field_type, 0) == 0,
1097 "bt_ctf_event_class_get_field returns a field");
1098 ok(ret_field_type == uint_35_type,
1099 "bt_ctf_event_class_get_field returns a correct field type");
1100 bt_put(ret_field_type);
1101 ok(!strcmp(ret_string, "uint_35"),
1102 "bt_ctf_event_class_get_field returns a correct field name");
1103 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
1104 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1105 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
1106 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1107 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
1108 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1109 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
1110 "complex_structure");
1111 ok(ret_field_type == complex_structure_type,
1112 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1113 bt_put(ret_field_type);
1114
1115 event = bt_ctf_event_create(event_class);
1116 ok(event, "Instanciate a complex event");
1117
1118 ok(bt_ctf_event_get_class(NULL) == NULL,
1119 "bt_ctf_event_get_class handles NULL correctly");
1120 ret_event_class = bt_ctf_event_get_class(event);
1121 ok(ret_event_class == event_class,
1122 "bt_ctf_event_get_class returns the correct event class");
1123 bt_put(ret_event_class);
1124
1125 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
1126 if (!uint_35_field) {
1127 printf("uint_35_field is NULL\n");
1128 }
1129
1130 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
1131 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
1132 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
1133 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1134 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
1135 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1136 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
1137 &ret_unsigned_int) == 0,
1138 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1139 ok(ret_unsigned_int == 0x0DDF00D,
1140 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1141 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
1142 &ret_signed_int) < 0,
1143 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1144 bt_put(uint_35_field);
1145
1146 int_16_field = bt_ctf_event_get_payload(event, "int_16");
1147 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
1148 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
1149 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1150 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
1151 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1152 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
1153 &ret_signed_int) == 0,
1154 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1155 ok(ret_signed_int == -12345,
1156 "bt_ctf_field_signed_integer_get_value returns the correct value");
1157 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
1158 &ret_unsigned_int) < 0,
1159 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1160 bt_put(int_16_field);
1161
1162 complex_structure_field = bt_ctf_event_get_payload(event,
1163 "complex_structure");
1164
1165 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
1166 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1167 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
1168 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1169 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
1170 complex_structure_field, 3);
1171 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
1172 bt_put(inner_structure_field);
1173 ok(ret_field_type == inner_structure_type,
1174 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1175 bt_put(ret_field_type);
1176
1177 inner_structure_field = bt_ctf_field_structure_get_field(
1178 complex_structure_field, "inner_structure");
1179 a_string_field = bt_ctf_field_structure_get_field(
1180 complex_structure_field, "a_string");
1181 enum_variant_field = bt_ctf_field_structure_get_field(
1182 complex_structure_field, "variant_selector");
1183 variant_field = bt_ctf_field_structure_get_field(
1184 complex_structure_field, "variant_value");
1185 uint_35_field = bt_ctf_field_structure_get_field(
1186 inner_structure_field, "seq_len");
1187 a_sequence_field = bt_ctf_field_structure_get_field(
1188 inner_structure_field, "a_sequence");
1189 an_array_field = bt_ctf_field_structure_get_field(
1190 inner_structure_field, "an_array");
1191
1192 enum_container_field = bt_ctf_field_enumeration_get_container(
1193 enum_variant_field);
1194 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
1195 int_16_field = bt_ctf_field_variant_get_field(variant_field,
1196 enum_variant_field);
1197 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
1198 bt_put(int_16_field);
1199 ok(!bt_ctf_field_string_get_value(a_string_field),
1200 "bt_ctf_field_string_get_value returns NULL on an unset field");
1201 bt_ctf_field_string_set_value(a_string_field,
1202 test_string_1);
1203 ok(!bt_ctf_field_string_get_value(NULL),
1204 "bt_ctf_field_string_get_value correctly handles NULL");
1205 ok(bt_ctf_field_string_append(NULL, "yeah"),
1206 "bt_ctf_field_string_append correctly handles a NULL string field");
1207 ok(bt_ctf_field_string_append(a_string_field, NULL),
1208 "bt_ctf_field_string_append correctly handles a NULL string value");
1209 ok(!bt_ctf_field_string_append(a_string_field, test_string_2),
1210 "bt_ctf_field_string_append succeeds");
1211 ok(bt_ctf_field_string_append_len(NULL, "oh noes", 3),
1212 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1213 ok(bt_ctf_field_string_append_len(a_string_field, NULL, 3),
1214 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1215 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5),
1216 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1217 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_4, 10),
1218 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1219 ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[4], 3),
1220 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1221 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0),
1222 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1223
1224 ret_string = bt_ctf_field_string_get_value(a_string_field);
1225 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
1226 ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0,
1227 "bt_ctf_field_string_get_value returns a correct value");
1228 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
1229 SEQUENCE_TEST_LENGTH);
1230
1231 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
1232 enum_container_field) == NULL,
1233 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1234 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
1235 NULL) == NULL,
1236 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1237 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
1238 variant_type, enum_variant_field);
1239 ok(ret_field_type == int_16_type,
1240 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1241
1242 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
1243 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1244 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
1245 uint_35_field) == 0, "Set a sequence field's length");
1246 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
1247 ok(ret_field == uint_35_field,
1248 "bt_ctf_field_sequence_get_length returns the correct length field");
1249 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
1250 "bt_ctf_field_sequence_get_length properly handles NULL");
1251
1252 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
1253 int_16_field = bt_ctf_field_sequence_get_field(
1254 a_sequence_field, i);
1255 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
1256 bt_put(int_16_field);
1257 }
1258
1259 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
1260 int_16_field = bt_ctf_field_array_get_field(
1261 an_array_field, i);
1262 bt_ctf_field_signed_integer_set_value(int_16_field, i);
1263 bt_put(int_16_field);
1264 }
1265
1266 bt_ctf_clock_set_time(clock, ++current_time);
1267 ok(bt_ctf_stream_append_event(stream, event) == 0,
1268 "Append a complex event to a stream");
1269
1270 /*
1271 * Populate the custom packet context field with a dummy value
1272 * otherwise flush will fail.
1273 */
1274 packet_context = bt_ctf_stream_get_packet_context(stream);
1275 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1276 "custom_packet_context_field");
1277 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
1278
1279 ok(bt_ctf_stream_flush(stream) == 0,
1280 "Flush a stream containing a complex event");
1281
1282 bt_put(uint_35_field);
1283 bt_put(a_string_field);
1284 bt_put(inner_structure_field);
1285 bt_put(complex_structure_field);
1286 bt_put(a_sequence_field);
1287 bt_put(an_array_field);
1288 bt_put(enum_variant_field);
1289 bt_put(enum_container_field);
1290 bt_put(variant_field);
1291 bt_put(ret_field);
1292 bt_put(packet_context_field);
1293 bt_put(packet_context);
1294 bt_put(uint_35_type);
1295 bt_put(int_16_type);
1296 bt_put(string_type);
1297 bt_put(sequence_type);
1298 bt_put(array_type);
1299 bt_put(inner_structure_type);
1300 bt_put(complex_structure_type);
1301 bt_put(uint_3_type);
1302 bt_put(enum_variant_type);
1303 bt_put(variant_type);
1304 bt_put(ret_field_type);
1305 bt_put(event_class);
1306 bt_put(event);
1307 }
1308
1309 static void field_copy_tests_validate_same_type(struct bt_ctf_field *field,
1310 struct bt_ctf_field_type *expected_type, const char *name)
1311 {
1312 struct bt_ctf_field_type *copy_type;
1313
1314 copy_type = bt_ctf_field_get_type(field);
1315 ok(copy_type == expected_type,
1316 "bt_ctf_field_copy does not copy the type (%s)", name);
1317 bt_put(copy_type);
1318 }
1319
1320 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field *field_a,
1321 struct bt_ctf_field *field_b, const char *name)
1322 {
1323 ok(field_a != field_b,
1324 "bt_ctf_field_copy creates different pointers (%s)", name);
1325 }
1326
1327 void field_copy_tests()
1328 {
1329 struct bt_ctf_field_type *len_type = NULL;
1330 struct bt_ctf_field_type *fp_type = NULL;
1331 struct bt_ctf_field_type *s_type = NULL;
1332 struct bt_ctf_field_type *e_int_type = NULL;
1333 struct bt_ctf_field_type *e_type = NULL;
1334 struct bt_ctf_field_type *v_type = NULL;
1335 struct bt_ctf_field_type *v_label1_type = NULL;
1336 struct bt_ctf_field_type *v_label1_array_type = NULL;
1337 struct bt_ctf_field_type *v_label2_type = NULL;
1338 struct bt_ctf_field_type *v_label2_seq_type = NULL;
1339 struct bt_ctf_field_type *strct_type = NULL;
1340 struct bt_ctf_field *len = NULL;
1341 struct bt_ctf_field *fp = NULL;
1342 struct bt_ctf_field *s = NULL;
1343 struct bt_ctf_field *e_int = NULL;
1344 struct bt_ctf_field *e = NULL;
1345 struct bt_ctf_field *v = NULL;
1346 struct bt_ctf_field *v_selected = NULL;
1347 struct bt_ctf_field *v_selected_cur = NULL;
1348 struct bt_ctf_field *v_selected_0 = NULL;
1349 struct bt_ctf_field *v_selected_1 = NULL;
1350 struct bt_ctf_field *v_selected_2 = NULL;
1351 struct bt_ctf_field *v_selected_3 = NULL;
1352 struct bt_ctf_field *v_selected_4 = NULL;
1353 struct bt_ctf_field *v_selected_5 = NULL;
1354 struct bt_ctf_field *v_selected_6 = NULL;
1355 struct bt_ctf_field *a = NULL;
1356 struct bt_ctf_field *a_0 = NULL;
1357 struct bt_ctf_field *a_1 = NULL;
1358 struct bt_ctf_field *a_2 = NULL;
1359 struct bt_ctf_field *a_3 = NULL;
1360 struct bt_ctf_field *a_4 = NULL;
1361 struct bt_ctf_field *strct = NULL;
1362 struct bt_ctf_field *len_copy = NULL;
1363 struct bt_ctf_field *fp_copy = NULL;
1364 struct bt_ctf_field *s_copy = NULL;
1365 struct bt_ctf_field *e_int_copy = NULL;
1366 struct bt_ctf_field *e_copy = NULL;
1367 struct bt_ctf_field *v_copy = NULL;
1368 struct bt_ctf_field *v_selected_copy = NULL;
1369 struct bt_ctf_field *v_selected_copy_len = NULL;
1370 struct bt_ctf_field *v_selected_0_copy = NULL;
1371 struct bt_ctf_field *v_selected_1_copy = NULL;
1372 struct bt_ctf_field *v_selected_2_copy = NULL;
1373 struct bt_ctf_field *v_selected_3_copy = NULL;
1374 struct bt_ctf_field *v_selected_4_copy = NULL;
1375 struct bt_ctf_field *v_selected_5_copy = NULL;
1376 struct bt_ctf_field *v_selected_6_copy = NULL;
1377 struct bt_ctf_field *a_copy = NULL;
1378 struct bt_ctf_field *a_0_copy = NULL;
1379 struct bt_ctf_field *a_1_copy = NULL;
1380 struct bt_ctf_field *a_2_copy = NULL;
1381 struct bt_ctf_field *a_3_copy = NULL;
1382 struct bt_ctf_field *a_4_copy = NULL;
1383 struct bt_ctf_field *strct_copy = NULL;
1384 uint64_t uint64_t_val;
1385 const char *str_val;
1386 double double_val;
1387 int ret;
1388
1389 /* create len type */
1390 len_type = bt_ctf_field_type_integer_create(32);
1391 assert(len_type);
1392
1393 /* create fp type */
1394 fp_type = bt_ctf_field_type_floating_point_create();
1395 assert(fp_type);
1396
1397 /* create s type */
1398 s_type = bt_ctf_field_type_string_create();
1399 assert(s_type);
1400
1401 /* create e_int type */
1402 e_int_type = bt_ctf_field_type_integer_create(8);
1403 assert(e_int_type);
1404
1405 /* create e type */
1406 e_type = bt_ctf_field_type_enumeration_create(e_int_type);
1407 assert(e_type);
1408 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL1",
1409 10, 15);
1410 assert(!ret);
1411 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL2",
1412 23, 23);
1413 assert(!ret);
1414
1415 /* create v_label1 type */
1416 v_label1_type = bt_ctf_field_type_string_create();
1417 assert(v_label1_type);
1418
1419 /* create v_label1_array type */
1420 v_label1_array_type = bt_ctf_field_type_array_create(v_label1_type, 5);
1421 assert(v_label1_array_type);
1422
1423 /* create v_label2 type */
1424 v_label2_type = bt_ctf_field_type_integer_create(16);
1425 assert(v_label2_type);
1426
1427 /* create v_label2_seq type */
1428 v_label2_seq_type = bt_ctf_field_type_sequence_create(v_label2_type,
1429 "len");
1430 assert(v_label2_seq_type);
1431
1432 /* create v type */
1433 v_type = bt_ctf_field_type_variant_create(e_type, "e");
1434 assert(v_type);
1435 ret = bt_ctf_field_type_variant_add_field(v_type, v_label1_array_type,
1436 "LABEL1");
1437 assert(!ret);
1438 ret = bt_ctf_field_type_variant_add_field(v_type, v_label2_seq_type,
1439 "LABEL2");
1440 assert(!ret);
1441
1442 /* create strct type */
1443 strct_type = bt_ctf_field_type_structure_create();
1444 assert(strct_type);
1445 ret = bt_ctf_field_type_structure_add_field(strct_type, len_type,
1446 "len");
1447 assert(!ret);
1448 ret = bt_ctf_field_type_structure_add_field(strct_type, fp_type, "fp");
1449 assert(!ret);
1450 ret = bt_ctf_field_type_structure_add_field(strct_type, s_type, "s");
1451 assert(!ret);
1452 ret = bt_ctf_field_type_structure_add_field(strct_type, e_type, "e");
1453 assert(!ret);
1454 ret = bt_ctf_field_type_structure_add_field(strct_type, v_type, "v");
1455 assert(!ret);
1456 ret = bt_ctf_field_type_structure_add_field(strct_type,
1457 v_label1_array_type, "a");
1458 assert(!ret);
1459
1460 /* create strct */
1461 strct = bt_ctf_field_create(strct_type);
1462 assert(strct);
1463
1464 /* get len field */
1465 len = bt_ctf_field_structure_get_field(strct, "len");
1466 assert(len);
1467
1468 /* get fp field */
1469 fp = bt_ctf_field_structure_get_field(strct, "fp");
1470 assert(fp);
1471
1472 /* get s field */
1473 s = bt_ctf_field_structure_get_field(strct, "s");
1474 assert(s);
1475
1476 /* get e field */
1477 e = bt_ctf_field_structure_get_field(strct, "e");
1478 assert(e);
1479
1480 /* get e_int (underlying integer) */
1481 e_int = bt_ctf_field_enumeration_get_container(e);
1482 assert(e_int);
1483
1484 /* get v field */
1485 v = bt_ctf_field_structure_get_field(strct, "v");
1486 assert(v);
1487
1488 /* get a field */
1489 a = bt_ctf_field_structure_get_field(strct, "a");
1490 assert(a);
1491
1492 /* set len field */
1493 ret = bt_ctf_field_unsigned_integer_set_value(len, 7);
1494 assert(!ret);
1495
1496 /* set fp field */
1497 ret = bt_ctf_field_floating_point_set_value(fp, 3.14);
1498 assert(!ret);
1499
1500 /* set s field */
1501 ret = bt_ctf_field_string_set_value(s, "btbt");
1502 assert(!ret);
1503
1504 /* set e field (LABEL2) */
1505 ret = bt_ctf_field_unsigned_integer_set_value(e_int, 23);
1506 assert(!ret);
1507
1508 /* set v field */
1509 v_selected = bt_ctf_field_variant_get_field(v, e);
1510 assert(v_selected);
1511 ok(!bt_ctf_field_variant_get_current_field(NULL),
1512 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1513 v_selected_cur = bt_ctf_field_variant_get_current_field(v);
1514 ok(v_selected_cur == v_selected,
1515 "bt_ctf_field_variant_get_current_field returns the current field");
1516 bt_put(v_selected_cur);
1517
1518 /* set selected v field */
1519 ret = bt_ctf_field_sequence_set_length(v_selected, len);
1520 assert(!ret);
1521 v_selected_0 = bt_ctf_field_sequence_get_field(v_selected, 0);
1522 assert(v_selected_0);
1523 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_0, 7);
1524 assert(!ret);
1525 v_selected_1 = bt_ctf_field_sequence_get_field(v_selected, 1);
1526 assert(v_selected_1);
1527 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_1, 6);
1528 assert(!ret);
1529 v_selected_2 = bt_ctf_field_sequence_get_field(v_selected, 2);
1530 assert(v_selected_2);
1531 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_2, 5);
1532 assert(!ret);
1533 v_selected_3 = bt_ctf_field_sequence_get_field(v_selected, 3);
1534 assert(v_selected_3);
1535 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_3, 4);
1536 assert(!ret);
1537 v_selected_4 = bt_ctf_field_sequence_get_field(v_selected, 4);
1538 assert(v_selected_4);
1539 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_4, 3);
1540 assert(!ret);
1541 v_selected_5 = bt_ctf_field_sequence_get_field(v_selected, 5);
1542 assert(v_selected_5);
1543 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_5, 2);
1544 assert(!ret);
1545 v_selected_6 = bt_ctf_field_sequence_get_field(v_selected, 6);
1546 assert(v_selected_6);
1547 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_6, 1);
1548 assert(!ret);
1549
1550 /* set a field */
1551 a_0 = bt_ctf_field_array_get_field(a, 0);
1552 assert(a_0);
1553 ret = bt_ctf_field_string_set_value(a_0, "a_0");
1554 assert(!ret);
1555 a_1 = bt_ctf_field_array_get_field(a, 1);
1556 assert(a_1);
1557 ret = bt_ctf_field_string_set_value(a_1, "a_1");
1558 assert(!ret);
1559 a_2 = bt_ctf_field_array_get_field(a, 2);
1560 assert(a_2);
1561 ret = bt_ctf_field_string_set_value(a_2, "a_2");
1562 assert(!ret);
1563 a_3 = bt_ctf_field_array_get_field(a, 3);
1564 assert(a_3);
1565 ret = bt_ctf_field_string_set_value(a_3, "a_3");
1566 assert(!ret);
1567 a_4 = bt_ctf_field_array_get_field(a, 4);
1568 assert(a_4);
1569 ret = bt_ctf_field_string_set_value(a_4, "a_4");
1570 assert(!ret);
1571
1572 /* create copy of strct */
1573 ok(!bt_ctf_field_copy(NULL),
1574 "bt_ctf_field_copy handles NULL correctly");
1575 strct_copy = bt_ctf_field_copy(strct);
1576 ok(strct_copy,
1577 "bt_ctf_field_copy returns a valid pointer");
1578
1579 /* get all copied fields */
1580 len_copy = bt_ctf_field_structure_get_field(strct_copy, "len");
1581 assert(len_copy);
1582 fp_copy = bt_ctf_field_structure_get_field(strct_copy, "fp");
1583 assert(fp_copy);
1584 s_copy = bt_ctf_field_structure_get_field(strct_copy, "s");
1585 assert(s_copy);
1586 e_copy = bt_ctf_field_structure_get_field(strct_copy, "e");
1587 assert(e_copy);
1588 e_int_copy = bt_ctf_field_enumeration_get_container(e_copy);
1589 assert(e_int_copy);
1590 v_copy = bt_ctf_field_structure_get_field(strct_copy, "v");
1591 assert(v_copy);
1592 v_selected_copy = bt_ctf_field_variant_get_field(v_copy, e_copy);
1593 assert(v_selected_copy);
1594 v_selected_0_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 0);
1595 assert(v_selected_0_copy);
1596 v_selected_1_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 1);
1597 assert(v_selected_1_copy);
1598 v_selected_2_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 2);
1599 assert(v_selected_2_copy);
1600 v_selected_3_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 3);
1601 assert(v_selected_3_copy);
1602 v_selected_4_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 4);
1603 assert(v_selected_4_copy);
1604 v_selected_5_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 5);
1605 assert(v_selected_5_copy);
1606 v_selected_6_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 6);
1607 assert(v_selected_6_copy);
1608 ok(!bt_ctf_field_sequence_get_field(v_selected_copy, 7),
1609 "sequence field copy is not too large");
1610 a_copy = bt_ctf_field_structure_get_field(strct_copy, "a");
1611 assert(a_copy);
1612 a_0_copy = bt_ctf_field_array_get_field(a_copy, 0);
1613 assert(a_0_copy);
1614 a_1_copy = bt_ctf_field_array_get_field(a_copy, 1);
1615 assert(a_1_copy);
1616 a_2_copy = bt_ctf_field_array_get_field(a_copy, 2);
1617 assert(a_2_copy);
1618 a_3_copy = bt_ctf_field_array_get_field(a_copy, 3);
1619 assert(a_3_copy);
1620 a_4_copy = bt_ctf_field_array_get_field(a_copy, 4);
1621 assert(a_4_copy);
1622 ok(!bt_ctf_field_array_get_field(v_selected_copy, 5),
1623 "array field copy is not too large");
1624
1625 /* make sure copied fields are different pointers */
1626 field_copy_tests_validate_diff_ptrs(strct_copy, strct, "strct");
1627 field_copy_tests_validate_diff_ptrs(len_copy, len, "len");
1628 field_copy_tests_validate_diff_ptrs(fp_copy, fp, "fp");
1629 field_copy_tests_validate_diff_ptrs(s_copy, s, "s");
1630 field_copy_tests_validate_diff_ptrs(e_int_copy, e_int, "e_int");
1631 field_copy_tests_validate_diff_ptrs(e_copy, e, "e");
1632 field_copy_tests_validate_diff_ptrs(v_copy, v, "v");
1633 field_copy_tests_validate_diff_ptrs(v_selected_copy, v_selected,
1634 "v_selected");
1635 field_copy_tests_validate_diff_ptrs(v_selected_0_copy, v_selected_0,
1636 "v_selected_0");
1637 field_copy_tests_validate_diff_ptrs(v_selected_1_copy, v_selected_1,
1638 "v_selected_1");
1639 field_copy_tests_validate_diff_ptrs(v_selected_2_copy, v_selected_2,
1640 "v_selected_2");
1641 field_copy_tests_validate_diff_ptrs(v_selected_3_copy, v_selected_3,
1642 "v_selected_3");
1643 field_copy_tests_validate_diff_ptrs(v_selected_4_copy, v_selected_4,
1644 "v_selected_4");
1645 field_copy_tests_validate_diff_ptrs(v_selected_5_copy, v_selected_5,
1646 "v_selected_5");
1647 field_copy_tests_validate_diff_ptrs(v_selected_6_copy, v_selected_6,
1648 "v_selected_6");
1649 field_copy_tests_validate_diff_ptrs(a_copy, a, "a");
1650 field_copy_tests_validate_diff_ptrs(a_0_copy, a_0, "a_0");
1651 field_copy_tests_validate_diff_ptrs(a_1_copy, a_1, "a_1");
1652 field_copy_tests_validate_diff_ptrs(a_2_copy, a_2, "a_2");
1653 field_copy_tests_validate_diff_ptrs(a_3_copy, a_3, "a_3");
1654 field_copy_tests_validate_diff_ptrs(a_4_copy, a_4, "a_4");
1655
1656 /* make sure copied fields share the same types */
1657 field_copy_tests_validate_same_type(strct_copy, strct_type, "strct");
1658 field_copy_tests_validate_same_type(len_copy, len_type, "len");
1659 field_copy_tests_validate_same_type(fp_copy, fp_type, "fp");
1660 field_copy_tests_validate_same_type(e_int_copy, e_int_type, "e_int");
1661 field_copy_tests_validate_same_type(e_copy, e_type, "e");
1662 field_copy_tests_validate_same_type(v_copy, v_type, "v");
1663 field_copy_tests_validate_same_type(v_selected_copy, v_label2_seq_type,
1664 "v_selected");
1665 field_copy_tests_validate_same_type(v_selected_0_copy, v_label2_type,
1666 "v_selected_0");
1667 field_copy_tests_validate_same_type(v_selected_1_copy, v_label2_type,
1668 "v_selected_1");
1669 field_copy_tests_validate_same_type(v_selected_2_copy, v_label2_type,
1670 "v_selected_2");
1671 field_copy_tests_validate_same_type(v_selected_3_copy, v_label2_type,
1672 "v_selected_3");
1673 field_copy_tests_validate_same_type(v_selected_4_copy, v_label2_type,
1674 "v_selected_4");
1675 field_copy_tests_validate_same_type(v_selected_5_copy, v_label2_type,
1676 "v_selected_5");
1677 field_copy_tests_validate_same_type(v_selected_6_copy, v_label2_type,
1678 "v_selected_6");
1679 field_copy_tests_validate_same_type(a_copy, v_label1_array_type, "a");
1680 field_copy_tests_validate_same_type(a_0_copy, v_label1_type, "a_0");
1681 field_copy_tests_validate_same_type(a_1_copy, v_label1_type, "a_1");
1682 field_copy_tests_validate_same_type(a_2_copy, v_label1_type, "a_2");
1683 field_copy_tests_validate_same_type(a_3_copy, v_label1_type, "a_3");
1684 field_copy_tests_validate_same_type(a_4_copy, v_label1_type, "a_4");
1685
1686 /* validate len copy */
1687 ret = bt_ctf_field_unsigned_integer_get_value(len_copy, &uint64_t_val);
1688 assert(!ret);
1689 ok(uint64_t_val == 7,
1690 "bt_ctf_field_copy creates a valid integer field copy");
1691
1692 /* validate fp copy */
1693 ret = bt_ctf_field_floating_point_get_value(fp_copy, &double_val);
1694 assert(!ret);
1695 ok(double_val == 3.14,
1696 "bt_ctf_field_copy creates a valid floating point number field copy");
1697
1698 /* validate s copy */
1699 str_val = bt_ctf_field_string_get_value(s_copy);
1700 ok(str_val && !strcmp(str_val, "btbt"),
1701 "bt_ctf_field_copy creates a valid string field copy");
1702
1703 /* validate e_int copy */
1704 ret = bt_ctf_field_unsigned_integer_get_value(e_int_copy,
1705 &uint64_t_val);
1706 assert(!ret);
1707 ok(uint64_t_val == 23,
1708 "bt_ctf_field_copy creates a valid enum's integer field copy");
1709
1710 /* validate e copy */
1711 str_val = bt_ctf_field_enumeration_get_mapping_name(e_copy);
1712 ok(str_val && !strcmp(str_val, "LABEL2"),
1713 "bt_ctf_field_copy creates a valid enum field copy");
1714
1715 /* validate v_selected copy */
1716 v_selected_copy_len = bt_ctf_field_sequence_get_length(v_selected);
1717 assert(v_selected_copy_len);
1718 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len,
1719 &uint64_t_val);
1720 assert(!ret);
1721 ok(uint64_t_val == 7,
1722 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1723 bt_put(v_selected_copy_len);
1724 v_selected_copy_len = NULL;
1725
1726 /* validate v_selected copy fields */
1727 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy,
1728 &uint64_t_val);
1729 assert(!ret);
1730 ok(uint64_t_val == 7,
1731 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1732 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy,
1733 &uint64_t_val);
1734 assert(!ret);
1735 ok(uint64_t_val == 6,
1736 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1737 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy,
1738 &uint64_t_val);
1739 assert(!ret);
1740 ok(uint64_t_val == 5,
1741 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1742 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy,
1743 &uint64_t_val);
1744 assert(!ret);
1745 ok(uint64_t_val == 4,
1746 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1747 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy,
1748 &uint64_t_val);
1749 assert(!ret);
1750 ok(uint64_t_val == 3,
1751 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1752 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy,
1753 &uint64_t_val);
1754 assert(!ret);
1755 ok(uint64_t_val == 2,
1756 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1757 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy,
1758 &uint64_t_val);
1759 assert(!ret);
1760 ok(uint64_t_val == 1,
1761 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1762
1763 /* validate a copy fields */
1764 str_val = bt_ctf_field_string_get_value(a_0_copy);
1765 ok(str_val && !strcmp(str_val, "a_0"),
1766 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1767 str_val = bt_ctf_field_string_get_value(a_1_copy);
1768 ok(str_val && !strcmp(str_val, "a_1"),
1769 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1770 str_val = bt_ctf_field_string_get_value(a_2_copy);
1771 ok(str_val && !strcmp(str_val, "a_2"),
1772 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1773 str_val = bt_ctf_field_string_get_value(a_3_copy);
1774 ok(str_val && !strcmp(str_val, "a_3"),
1775 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1776 str_val = bt_ctf_field_string_get_value(a_4_copy);
1777 ok(str_val && !strcmp(str_val, "a_4"),
1778 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1779
1780 /* put everything */
1781 bt_put(len_type);
1782 bt_put(fp_type);
1783 bt_put(s_type);
1784 bt_put(e_int_type);
1785 bt_put(e_type);
1786 bt_put(v_type);
1787 bt_put(v_label1_type);
1788 bt_put(v_label1_array_type);
1789 bt_put(v_label2_type);
1790 bt_put(v_label2_seq_type);
1791 bt_put(strct_type);
1792 bt_put(len);
1793 bt_put(fp);
1794 bt_put(s);
1795 bt_put(e_int);
1796 bt_put(e);
1797 bt_put(v);
1798 bt_put(v_selected);
1799 bt_put(v_selected_0);
1800 bt_put(v_selected_1);
1801 bt_put(v_selected_2);
1802 bt_put(v_selected_3);
1803 bt_put(v_selected_4);
1804 bt_put(v_selected_5);
1805 bt_put(v_selected_6);
1806 bt_put(a);
1807 bt_put(a_0);
1808 bt_put(a_1);
1809 bt_put(a_2);
1810 bt_put(a_3);
1811 bt_put(a_4);
1812 bt_put(strct);
1813 bt_put(len_copy);
1814 bt_put(fp_copy);
1815 bt_put(s_copy);
1816 bt_put(e_int_copy);
1817 bt_put(e_copy);
1818 bt_put(v_copy);
1819 bt_put(v_selected_copy);
1820 bt_put(v_selected_0_copy);
1821 bt_put(v_selected_1_copy);
1822 bt_put(v_selected_2_copy);
1823 bt_put(v_selected_3_copy);
1824 bt_put(v_selected_4_copy);
1825 bt_put(v_selected_5_copy);
1826 bt_put(v_selected_6_copy);
1827 bt_put(a_copy);
1828 bt_put(a_0_copy);
1829 bt_put(a_1_copy);
1830 bt_put(a_2_copy);
1831 bt_put(a_3_copy);
1832 bt_put(a_4_copy);
1833 bt_put(strct_copy);
1834 }
1835
1836 void type_field_tests()
1837 {
1838 struct bt_ctf_field *uint_12;
1839 struct bt_ctf_field *int_16;
1840 struct bt_ctf_field *string;
1841 struct bt_ctf_field *enumeration;
1842 struct bt_ctf_field_type *composite_structure_type;
1843 struct bt_ctf_field_type *structure_seq_type;
1844 struct bt_ctf_field_type *string_type;
1845 struct bt_ctf_field_type *sequence_type;
1846 struct bt_ctf_field_type *uint_8_type;
1847 struct bt_ctf_field_type *int_16_type;
1848 struct bt_ctf_field_type *uint_12_type =
1849 bt_ctf_field_type_integer_create(12);
1850 struct bt_ctf_field_type *enumeration_type;
1851 struct bt_ctf_field_type *enumeration_sequence_type;
1852 struct bt_ctf_field_type *enumeration_array_type;
1853 struct bt_ctf_field_type *returned_type;
1854 const char *ret_string;
1855
1856 returned_type = bt_ctf_field_get_type(NULL);
1857 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
1858
1859 ok(uint_12_type, "Create an unsigned integer type");
1860 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1861 BT_CTF_INTEGER_BASE_BINARY) == 0,
1862 "Set integer type's base as binary");
1863 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1864 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1865 "Set integer type's base as decimal");
1866 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1867 BT_CTF_INTEGER_BASE_UNKNOWN),
1868 "Reject integer type's base set as unknown");
1869 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1870 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1871 "Set integer type's base as octal");
1872 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1873 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1874 "Set integer type's base as hexadecimal");
1875 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1876 "Reject unknown integer base value");
1877 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1878 "Set integer type signedness to signed");
1879 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1880 "Set integer type signedness to unsigned");
1881 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1882 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1883 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1884 "bt_ctf_field_type_integer_get_size returns a correct value");
1885 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1886 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1887 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1888 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1889
1890 ok(bt_ctf_field_type_set_byte_order(NULL,
1891 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1892 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1893 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1894 (enum bt_ctf_byte_order) 42) < 0,
1895 "bt_ctf_field_type_set_byte_order rejects invalid values");
1896 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1897 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1898 "Set an integer's byte order to little endian");
1899 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1900 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1901 "Set an integer's byte order to big endian");
1902 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1903 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1904 "bt_ctf_field_type_get_byte_order returns a correct value");
1905 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1906 BT_CTF_BYTE_ORDER_UNKNOWN,
1907 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1908
1909 ok(bt_ctf_field_type_get_type_id(NULL) ==
1910 CTF_TYPE_UNKNOWN,
1911 "bt_ctf_field_type_get_type_id handles NULL correctly");
1912 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1913 CTF_TYPE_INTEGER,
1914 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1915
1916 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1917 BT_CTF_INTEGER_BASE_UNKNOWN,
1918 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1919 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1920 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1921 "bt_ctf_field_type_integer_get_base returns a correct value");
1922
1923 ok(bt_ctf_field_type_integer_set_encoding(NULL, CTF_STRING_ASCII) < 0,
1924 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1925 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1926 (enum ctf_string_encoding) 123) < 0,
1927 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1928 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1929 CTF_STRING_UTF8) == 0,
1930 "Set integer type encoding to UTF8");
1931 ok(bt_ctf_field_type_integer_get_encoding(NULL) == CTF_STRING_UNKNOWN,
1932 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1933 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == CTF_STRING_UTF8,
1934 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1935
1936 int_16_type = bt_ctf_field_type_integer_create(16);
1937 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
1938 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1939 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1940 uint_8_type = bt_ctf_field_type_integer_create(8);
1941 sequence_type =
1942 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1943 ok(sequence_type, "Create a sequence of int16_t type");
1944 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1945 CTF_TYPE_SEQUENCE,
1946 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1947
1948 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1949 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1950 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1951 sequence_type);
1952 ok(!strcmp(ret_string, "seq_len"),
1953 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1954 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1955 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1956 returned_type = bt_ctf_field_type_sequence_get_element_type(
1957 sequence_type);
1958 ok(returned_type == int_16_type,
1959 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1960 bt_put(returned_type);
1961
1962 string_type = bt_ctf_field_type_string_create();
1963 ok(string_type, "Create a string type");
1964 ok(bt_ctf_field_type_string_set_encoding(string_type,
1965 CTF_STRING_NONE),
1966 "Reject invalid \"None\" string encoding");
1967 ok(bt_ctf_field_type_string_set_encoding(string_type,
1968 42),
1969 "Reject invalid string encoding");
1970 ok(bt_ctf_field_type_string_set_encoding(string_type,
1971 CTF_STRING_ASCII) == 0,
1972 "Set string encoding to ASCII");
1973
1974 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1975 CTF_STRING_UNKNOWN,
1976 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1977 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1978 CTF_STRING_ASCII,
1979 "bt_ctf_field_type_string_get_encoding returns the correct value");
1980
1981 structure_seq_type = bt_ctf_field_type_structure_create();
1982 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1983 CTF_TYPE_STRUCT,
1984 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1985 ok(structure_seq_type, "Create a structure type");
1986 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1987 uint_8_type, "seq_len") == 0,
1988 "Add a uint8_t type to a structure");
1989 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1990 sequence_type, "a_sequence") == 0,
1991 "Add a sequence type to a structure");
1992
1993 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1994 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1995 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1996 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1997
1998 ok(bt_ctf_field_type_structure_get_field(NULL,
1999 &ret_string, &returned_type, 1) < 0,
2000 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2001 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2002 NULL, &returned_type, 1) == 0,
2003 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2004 bt_put(returned_type);
2005 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2006 &ret_string, NULL, 1) == 0,
2007 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2008 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2009 &ret_string, &returned_type, 10) < 0,
2010 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2011 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2012 &ret_string, &returned_type, 1) == 0,
2013 "bt_ctf_field_type_structure_get_field returns a field");
2014 ok(!strcmp(ret_string, "a_sequence"),
2015 "bt_ctf_field_type_structure_get_field returns a correct field name");
2016 ok(returned_type == sequence_type,
2017 "bt_ctf_field_type_structure_get_field returns a correct field type");
2018 bt_put(returned_type);
2019
2020 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
2021 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2022 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
2023 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2024 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2025 structure_seq_type, "a_sequence");
2026 ok(returned_type == sequence_type,
2027 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2028 bt_put(returned_type);
2029
2030 composite_structure_type = bt_ctf_field_type_structure_create();
2031 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2032 string_type, "a_string") == 0,
2033 "Add a string type to a structure");
2034 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2035 structure_seq_type, "inner_structure") == 0,
2036 "Add a structure type to a structure");
2037
2038 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2039 NULL, "a_sequence") == NULL,
2040 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2041 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2042 structure_seq_type, NULL) == NULL,
2043 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2044 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2045 structure_seq_type, "a_sequence");
2046 ok(returned_type == sequence_type,
2047 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2048 bt_put(returned_type);
2049
2050 int_16 = bt_ctf_field_create(int_16_type);
2051 ok(int_16, "Instanciate a signed 16-bit integer");
2052 uint_12 = bt_ctf_field_create(uint_12_type);
2053 ok(uint_12, "Instanciate an unsigned 12-bit integer");
2054 returned_type = bt_ctf_field_get_type(int_16);
2055 ok(returned_type == int_16_type,
2056 "bt_ctf_field_get_type returns the correct type");
2057
2058 /* Can't modify types after instanciating them */
2059 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
2060 BT_CTF_INTEGER_BASE_DECIMAL),
2061 "Check an integer type' base can't be modified after instanciation");
2062 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
2063 "Check an integer type's signedness can't be modified after instanciation");
2064
2065 /* Check signed property is checked */
2066 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
2067 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2068 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
2069 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2070
2071 /* Check overflows are properly tested for */
2072 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
2073 "Check -32768 is allowed for a signed 16-bit integer");
2074 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
2075 "Check 32767 is allowed for a signed 16-bit integer");
2076 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
2077 "Check 32768 is not allowed for a signed 16-bit integer");
2078 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
2079 "Check -32769 is not allowed for a signed 16-bit integer");
2080 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
2081 "Check -42 is allowed for a signed 16-bit integer");
2082
2083 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
2084 "Check 4095 is allowed for an unsigned 12-bit integer");
2085 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
2086 "Check 4096 is not allowed for a unsigned 12-bit integer");
2087 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
2088 "Check 0 is allowed for an unsigned 12-bit integer");
2089
2090 string = bt_ctf_field_create(string_type);
2091 ok(string, "Instanciate a string field");
2092 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
2093 "Set a string's value");
2094
2095 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
2096 ok(enumeration_type,
2097 "Create an enumeration type with an unsigned 12-bit integer as container");
2098 enumeration_sequence_type = bt_ctf_field_type_sequence_create(
2099 enumeration_type, "count");
2100 ok(!enumeration_sequence_type,
2101 "Check enumeration types are validated when creating a sequence");
2102 enumeration_array_type = bt_ctf_field_type_array_create(
2103 enumeration_type, 10);
2104 ok(!enumeration_array_type,
2105 "Check enumeration types are validated when creating an array");
2106 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2107 enumeration_type, "enumeration"),
2108 "Check enumeration types are validated when adding them as structure members");
2109 enumeration = bt_ctf_field_create(enumeration_type);
2110 ok(!enumeration,
2111 "Check enumeration types are validated before instantiation");
2112
2113 bt_put(string);
2114 bt_put(uint_12);
2115 bt_put(int_16);
2116 bt_put(enumeration);
2117 bt_put(composite_structure_type);
2118 bt_put(structure_seq_type);
2119 bt_put(string_type);
2120 bt_put(sequence_type);
2121 bt_put(uint_8_type);
2122 bt_put(int_16_type);
2123 bt_put(uint_12_type);
2124 bt_put(enumeration_type);
2125 bt_put(enumeration_sequence_type);
2126 bt_put(enumeration_array_type);
2127 bt_put(returned_type);
2128 }
2129
2130 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
2131 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
2132 {
2133 /*
2134 * Append enough events to force the underlying packet to be resized.
2135 * Also tests that a new event can be declared after a stream has been
2136 * instantiated and used/flushed.
2137 */
2138 int ret = 0;
2139 int i;
2140 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
2141 "Spammy_Event");
2142 struct bt_ctf_field_type *integer_type =
2143 bt_ctf_field_type_integer_create(17);
2144 struct bt_ctf_field_type *string_type =
2145 bt_ctf_field_type_string_create();
2146 struct bt_ctf_event *event = NULL;
2147 struct bt_ctf_field *ret_field = NULL;
2148 struct bt_ctf_field_type *ret_field_type = NULL;
2149 uint64_t ret_uint64;
2150 int events_appended = 0;
2151 struct bt_ctf_field *packet_context = NULL,
2152 *packet_context_field = NULL, *event_context = NULL;
2153
2154 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
2155 "field_1");
2156 ret |= bt_ctf_event_class_add_field(event_class, string_type,
2157 "a_string");
2158 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
2159 ok(ret == 0, "Add a new event class to a stream class after writing an event");
2160 if (ret) {
2161 goto end;
2162 }
2163
2164 event = bt_ctf_event_create(event_class);
2165 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
2166 ret_field_type = bt_ctf_field_get_type(ret_field);
2167 ok(ret_field_type == integer_type,
2168 "bt_ctf_event_get_payload_by_index returns a correct field");
2169 bt_put(ret_field_type);
2170 bt_put(ret_field);
2171
2172 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
2173 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2174 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
2175 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2176 bt_put(event);
2177
2178 ok(bt_ctf_stream_get_event_context(NULL) == NULL,
2179 "bt_ctf_stream_get_event_context handles NULL correctly");
2180 event_context = bt_ctf_stream_get_event_context(stream);
2181 ok(event_context,
2182 "bt_ctf_stream_get_event_context returns a stream event context");
2183 ok(bt_ctf_stream_set_event_context(NULL, event_context) < 0,
2184 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
2185 ok(bt_ctf_stream_set_event_context(stream, NULL) < 0,
2186 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
2187 ok(!bt_ctf_stream_set_event_context(stream, event_context),
2188 "bt_ctf_stream_set_event_context correctly set a stream event context");
2189 ret_field = bt_ctf_field_create(integer_type);
2190 ok(bt_ctf_stream_set_event_context(stream, ret_field) < 0,
2191 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
2192 bt_put(ret_field);
2193
2194 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
2195 event = bt_ctf_event_create(event_class);
2196 struct bt_ctf_field *integer =
2197 bt_ctf_field_create(integer_type);
2198 struct bt_ctf_field *string =
2199 bt_ctf_field_create(string_type);
2200
2201 ret |= bt_ctf_clock_set_time(clock, ++current_time);
2202 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
2203 ret |= bt_ctf_event_set_payload(event, "field_1",
2204 integer);
2205 bt_put(integer);
2206 ret |= bt_ctf_field_string_set_value(string, "This is a test");
2207 ret |= bt_ctf_event_set_payload(event, "a_string",
2208 string);
2209 bt_put(string);
2210
2211 /* Populate stream event context */
2212 integer = bt_ctf_field_structure_get_field(event_context,
2213 "common_event_context");
2214 ret |= bt_ctf_field_unsigned_integer_set_value(integer,
2215 i % 42);
2216 bt_put(integer);
2217
2218 ret |= bt_ctf_stream_append_event(stream, event);
2219 bt_put(event);
2220
2221 if (ret) {
2222 break;
2223 }
2224 }
2225
2226 events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH);
2227 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
2228 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2229 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
2230 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2231 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2232 ok(ret == 0 && ret_uint64 == 0,
2233 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2234 bt_ctf_stream_append_discarded_events(stream, 1000);
2235 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2236 ok(ret == 0 && ret_uint64 == 1000,
2237 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2238
2239 end:
2240 ok(events_appended, "Append 100 000 events to a stream");
2241
2242 /*
2243 * Populate the custom packet context field with a dummy value
2244 * otherwise flush will fail.
2245 */
2246 packet_context = bt_ctf_stream_get_packet_context(stream);
2247 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
2248 "custom_packet_context_field");
2249 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
2250
2251 ok(bt_ctf_stream_flush(stream) == 0,
2252 "Flush a stream that forces a packet resize");
2253 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2254 ok(ret == 0 && ret_uint64 == 1000,
2255 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2256 bt_put(integer_type);
2257 bt_put(string_type);
2258 bt_put(packet_context);
2259 bt_put(packet_context_field);
2260 bt_put(event_context);
2261 bt_put(event_class);
2262 }
2263
2264 void test_empty_stream(struct bt_ctf_writer *writer)
2265 {
2266 int ret = 0;
2267 struct bt_ctf_trace *trace = NULL, *ret_trace = NULL;
2268 struct bt_ctf_stream_class *stream_class = NULL;
2269 struct bt_ctf_stream *stream = NULL;
2270
2271 trace = bt_ctf_writer_get_trace(writer);
2272 if (!trace) {
2273 diag("Failed to get trace from writer");
2274 ret = -1;
2275 goto end;
2276 }
2277
2278 stream_class = bt_ctf_stream_class_create("empty_stream");
2279 if (!stream_class) {
2280 diag("Failed to create stream class");
2281 ret = -1;
2282 goto end;
2283 }
2284
2285 ok(bt_ctf_stream_class_get_trace(NULL) == NULL,
2286 "bt_ctf_stream_class_get_trace handles NULL correctly");
2287 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
2288 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2289
2290 stream = bt_ctf_writer_create_stream(writer, stream_class);
2291 if (!stream) {
2292 diag("Failed to create writer stream");
2293 ret = -1;
2294 goto end;
2295 }
2296
2297 ret_trace = bt_ctf_stream_class_get_trace(stream_class);
2298 ok(ret_trace == trace,
2299 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2300 end:
2301 ok(ret == 0,
2302 "Created a stream class with default attributes and an empty stream");
2303 bt_put(trace);
2304 bt_put(ret_trace);
2305 bt_put(stream);
2306 bt_put(stream_class);
2307 }
2308
2309 void test_custom_event_header_stream(struct bt_ctf_writer *writer)
2310 {
2311 int i, ret;
2312 struct bt_ctf_trace *trace = NULL;
2313 struct bt_ctf_clock *clock = NULL;
2314 struct bt_ctf_stream_class *stream_class = NULL;
2315 struct bt_ctf_stream *stream = NULL;
2316 struct bt_ctf_field_type *integer_type = NULL,
2317 *sequence_type = NULL, *event_header_type = NULL;
2318 struct bt_ctf_field *integer = NULL, *sequence = NULL,
2319 *event_header = NULL, *packet_header = NULL;
2320 struct bt_ctf_event_class *event_class = NULL;
2321 struct bt_ctf_event *event = NULL;
2322
2323 trace = bt_ctf_writer_get_trace(writer);
2324 if (!trace) {
2325 fail("Failed to get trace from writer");
2326 goto end;
2327 }
2328
2329 clock = bt_ctf_trace_get_clock(trace, 0);
2330 if (!clock) {
2331 fail("Failed to get clock from trace");
2332 goto end;
2333 }
2334
2335 stream_class = bt_ctf_stream_class_create("custom_event_header_stream");
2336 if (!stream_class) {
2337 fail("Failed to create stream class");
2338 goto end;
2339 }
2340
2341 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2342 if (ret) {
2343 fail("Failed to set stream class clock");
2344 goto end;
2345 }
2346
2347 /*
2348 * Customize event header to add an "seq_len" integer member
2349 * which will be used as the length of a sequence in an event of this
2350 * stream.
2351 */
2352 event_header_type = bt_ctf_stream_class_get_event_header_type(
2353 stream_class);
2354 if (!event_header_type) {
2355 fail("Failed to get event header type");
2356 goto end;
2357 }
2358
2359 integer_type = bt_ctf_field_type_integer_create(13);
2360 if (!integer_type) {
2361 fail("Failed to create length integer type");
2362 goto end;
2363 }
2364
2365 ret = bt_ctf_field_type_structure_add_field(event_header_type,
2366 integer_type, "seq_len");
2367 if (ret) {
2368 fail("Failed to add a new field to stream event header");
2369 goto end;
2370 }
2371
2372 event_class = bt_ctf_event_class_create("sequence_event");
2373 if (!event_class) {
2374 fail("Failed to create event class");
2375 goto end;
2376 }
2377
2378 /*
2379 * This event's payload will contain a sequence which references
2380 * stream.event.header.seq_len as its length field.
2381 */
2382 sequence_type = bt_ctf_field_type_sequence_create(integer_type,
2383 "stream.event.header.seq_len");
2384 if (!sequence_type) {
2385 fail("Failed to create a sequence");
2386 goto end;
2387 }
2388
2389 ret = bt_ctf_event_class_add_field(event_class, sequence_type,
2390 "some_sequence");
2391 if (ret) {
2392 fail("Failed to add a sequence to an event class");
2393 goto end;
2394 }
2395
2396 ret = bt_ctf_stream_class_add_event_class(stream_class, event_class);
2397 if (ret) {
2398 fail("Failed to add event class to stream class");
2399 goto end;
2400 }
2401
2402 stream = bt_ctf_writer_create_stream(writer, stream_class);
2403 if (!stream) {
2404 fail("Failed to create stream")
2405 goto end;
2406 }
2407
2408 /*
2409 * We have defined a custom packet header field. We have to populate it
2410 * explicitly.
2411 */
2412 packet_header = bt_ctf_stream_get_packet_header(stream);
2413 if (!packet_header) {
2414 fail("Failed to get stream packet header");
2415 goto end;
2416 }
2417
2418 integer = bt_ctf_field_structure_get_field(packet_header,
2419 "custom_trace_packet_header_field");
2420 if (!integer) {
2421 fail("Failed to retrieve custom_trace_packet_header_field");
2422 goto end;
2423 }
2424
2425 ret = bt_ctf_field_unsigned_integer_set_value(integer, 3487);
2426 if (ret) {
2427 fail("Failed to set custom_trace_packet_header_field value");
2428 goto end;
2429 }
2430 bt_put(integer);
2431
2432 event = bt_ctf_event_create(event_class);
2433 if (!event) {
2434 fail("Failed to create event");
2435 goto end;
2436 }
2437
2438 event_header = bt_ctf_event_get_header(event);
2439 if (!event_header) {
2440 fail("Failed to get event header");
2441 goto end;
2442 }
2443
2444 integer = bt_ctf_field_structure_get_field(event_header,
2445 "seq_len");
2446 if (!integer) {
2447 fail("Failed to get seq_len field from event header");
2448 goto end;
2449 }
2450
2451 ret = bt_ctf_field_unsigned_integer_set_value(integer, 2);
2452 if (ret) {
2453 fail("Failed to set seq_len value in event header");
2454 goto end;
2455 }
2456
2457 /* Populate both sequence integer fields */
2458 sequence = bt_ctf_event_get_payload(event, "some_sequence");
2459 if (!sequence) {
2460 fail("Failed to retrieve sequence from event");
2461 goto end;
2462 }
2463
2464 ret = bt_ctf_field_sequence_set_length(sequence, integer);
2465 if (ret) {
2466 fail("Failed to set sequence length");
2467 goto end;
2468 }
2469 bt_put(integer);
2470
2471 for (i = 0; i < 2; i++) {
2472 integer = bt_ctf_field_sequence_get_field(sequence, i);
2473 if (ret) {
2474 fail("Failed to retrieve sequence element");
2475 goto end;
2476 }
2477
2478 ret = bt_ctf_field_unsigned_integer_set_value(integer, i);
2479 if (ret) {
2480 fail("Failed to set sequence element value");
2481 goto end;
2482 }
2483
2484 bt_put(integer);
2485 integer = NULL;
2486 }
2487
2488 ret = bt_ctf_stream_append_event(stream, event);
2489 if (ret) {
2490 fail("Failed to append event to stream");
2491 goto end;
2492 }
2493
2494 ret = bt_ctf_stream_flush(stream);
2495 if (ret) {
2496 fail("Failed to flush custom_event_header stream");
2497 }
2498 end:
2499 bt_put(clock);
2500 bt_put(trace);
2501 bt_put(stream);
2502 bt_put(stream_class);
2503 bt_put(event_class);
2504 bt_put(event);
2505 bt_put(integer);
2506 bt_put(sequence);
2507 bt_put(event_header);
2508 bt_put(packet_header);
2509 bt_put(sequence_type);
2510 bt_put(integer_type);
2511 bt_put(event_header_type);
2512 }
2513
2514 void test_instanciate_event_before_stream(struct bt_ctf_writer *writer)
2515 {
2516 int ret = 0;
2517 struct bt_ctf_trace *trace = NULL;
2518 struct bt_ctf_clock *clock = NULL;
2519 struct bt_ctf_stream_class *stream_class = NULL;
2520 struct bt_ctf_stream *stream = NULL,
2521 *ret_stream = NULL;
2522 struct bt_ctf_event_class *event_class = NULL;
2523 struct bt_ctf_event *event = NULL;
2524 struct bt_ctf_field_type *integer_type = NULL;
2525 struct bt_ctf_field *integer = NULL;
2526
2527 trace = bt_ctf_writer_get_trace(writer);
2528 if (!trace) {
2529 diag("Failed to get trace from writer");
2530 ret = -1;
2531 goto end;
2532 }
2533
2534 clock = bt_ctf_trace_get_clock(trace, 0);
2535 if (!clock) {
2536 diag("Failed to get clock from trace");
2537 ret = -1;
2538 goto end;
2539 }
2540
2541 stream_class = bt_ctf_stream_class_create("event_before_stream_test");
2542 if (!stream_class) {
2543 diag("Failed to create stream class");
2544 ret = -1;
2545 goto end;
2546 }
2547
2548 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2549 if (ret) {
2550 diag("Failed to set stream class clock");
2551 goto end;
2552 }
2553
2554 event_class = bt_ctf_event_class_create("some_event_class_name");
2555 integer_type = bt_ctf_field_type_integer_create(32);
2556 if (!integer_type) {
2557 diag("Failed to create integer field type");
2558 ret = -1;
2559 goto end;
2560 }
2561
2562 ret = bt_ctf_event_class_add_field(event_class, integer_type,
2563 "integer_field");
2564 if (ret) {
2565 diag("Failed to add field to event class");
2566 goto end;
2567 }
2568
2569 ret = bt_ctf_stream_class_add_event_class(stream_class,
2570 event_class);
2571 if (ret) {
2572 diag("Failed to add event class to stream class");
2573 }
2574
2575 event = bt_ctf_event_create(event_class);
2576 if (!event) {
2577 diag("Failed to create event");
2578 ret = -1;
2579 goto end;
2580 }
2581
2582 integer = bt_ctf_event_get_payload_by_index(event, 0);
2583 if (!integer) {
2584 diag("Failed to get integer field payload from event");
2585 ret = -1;
2586 goto end;
2587 }
2588
2589 ret = bt_ctf_field_unsigned_integer_set_value(integer, 1234);
2590 if (ret) {
2591 diag("Failed to set integer field value");
2592 goto end;
2593 }
2594
2595 stream = bt_ctf_writer_create_stream(writer, stream_class);
2596 if (!stream) {
2597 diag("Failed to create writer stream");
2598 ret = -1;
2599 goto end;
2600 }
2601
2602 ok(bt_ctf_event_get_stream(NULL) == NULL,
2603 "bt_ctf_event_get_stream handles NULL correctly");
2604 ok(bt_ctf_event_get_stream(event) == NULL,
2605 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2606
2607 ret = bt_ctf_stream_append_event(stream, event);
2608 if (ret) {
2609 diag("Failed to append event to stream");
2610 goto end;
2611 }
2612
2613 ret_stream = bt_ctf_event_get_stream(event);
2614 ok(ret_stream == stream,
2615 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2616 end:
2617 ok(ret == 0,
2618 "Create an event before instanciating its associated stream");
2619 bt_put(trace);
2620 bt_put(stream);
2621 bt_put(ret_stream);
2622 bt_put(stream_class);
2623 bt_put(event_class);
2624 bt_put(event);
2625 bt_put(integer_type);
2626 bt_put(integer);
2627 bt_put(clock);
2628 }
2629
2630 void append_existing_event_class(struct bt_ctf_stream_class *stream_class)
2631 {
2632 struct bt_ctf_event_class *event_class;
2633
2634 event_class = bt_ctf_event_class_create("Simple Event");
2635 assert(event_class);
2636 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2637 "two event classes with the same name cannot cohabit within the same stream class");
2638 bt_put(event_class);
2639
2640 event_class = bt_ctf_event_class_create("different name, ok");
2641 assert(event_class);
2642 assert(!bt_ctf_event_class_set_id(event_class, 11));
2643 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2644 "two event classes with the same ID cannot cohabit within the same stream class");
2645 bt_put(event_class);
2646 }
2647
2648 int main(int argc, char **argv)
2649 {
2650 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2651 char metadata_path[sizeof(trace_path) + 9];
2652 const char *clock_name = "test_clock";
2653 const char *clock_description = "This is a test clock";
2654 const char *returned_clock_name;
2655 const char *returned_clock_description;
2656 const uint64_t frequency = 1123456789;
2657 const uint64_t offset_s = 1351530929945824323;
2658 const uint64_t offset = 1234567;
2659 const uint64_t precision = 10;
2660 const int is_absolute = 0xFF;
2661 char *metadata_string;
2662 struct bt_ctf_writer *writer;
2663 struct utsname name;
2664 char hostname[BABELTRACE_HOST_NAME_MAX];
2665 struct bt_ctf_clock *clock, *ret_clock;
2666 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
2667 struct bt_ctf_stream *stream1;
2668 const char *ret_string;
2669 const unsigned char *ret_uuid;
2670 unsigned char tmp_uuid[16] = { 0 };
2671 struct bt_ctf_field_type *packet_context_type,
2672 *packet_context_field_type,
2673 *packet_header_type,
2674 *packet_header_field_type,
2675 *integer_type,
2676 *stream_event_context_type,
2677 *ret_field_type,
2678 *event_header_field_type;
2679 struct bt_ctf_field *packet_header, *packet_header_field;
2680 struct bt_ctf_trace *trace;
2681 int ret;
2682 int64_t ret_int64_t;
2683 struct bt_value *obj;
2684
2685 if (argc < 3) {
2686 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2687 return -1;
2688 }
2689
2690 plan_no_plan();
2691
2692 if (!bt_mkdtemp(trace_path)) {
2693 perror("# perror");
2694 }
2695
2696 strcpy(metadata_path, trace_path);
2697 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
2698
2699 writer = bt_ctf_writer_create(trace_path);
2700 ok(writer, "bt_ctf_create succeeds in creating trace with path");
2701
2702 ok(!bt_ctf_writer_get_trace(NULL),
2703 "bt_ctf_writer_get_trace correctly handles NULL");
2704 trace = bt_ctf_writer_get_trace(writer);
2705 ok(trace,
2706 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2707 ok(bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
2708 "Set a trace's byte order to big endian");
2709 ok(bt_ctf_trace_get_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN,
2710 "bt_ctf_trace_get_byte_order returns a correct endianness");
2711
2712 /* Add environment context to the trace */
2713 ret = gethostname(hostname, sizeof(hostname));
2714 if (ret < 0) {
2715 return ret;
2716 }
2717 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
2718 "Add host (%s) environment field to writer instance",
2719 hostname);
2720 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
2721 "test_value"),
2722 "bt_ctf_writer_add_environment_field error with NULL writer");
2723 ok(bt_ctf_writer_add_environment_field(writer, NULL,
2724 "test_value"),
2725 "bt_ctf_writer_add_environment_field error with NULL field name");
2726 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
2727 NULL),
2728 "bt_ctf_writer_add_environment_field error with NULL field value");
2729
2730 /* Test bt_ctf_trace_set_environment_field with an integer object */
2731 obj = bt_value_integer_create_init(23);
2732 assert(obj);
2733 ok(bt_ctf_trace_set_environment_field(NULL, "test_env_int_obj", obj),
2734 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2735 ok(bt_ctf_trace_set_environment_field(trace, NULL, obj),
2736 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2737 ok(bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", NULL),
2738 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2739 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", obj),
2740 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2741 BT_PUT(obj);
2742
2743 /* Test bt_ctf_trace_set_environment_field with a string object */
2744 obj = bt_value_string_create_init("the value");
2745 assert(obj);
2746 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_str_obj", obj),
2747 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2748 BT_PUT(obj);
2749
2750 /* Test bt_ctf_trace_set_environment_field_integer */
2751 ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int",
2752 -194875),
2753 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2754 ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875),
2755 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2756 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
2757 -164973),
2758 "bt_ctf_trace_set_environment_field_integer succeeds");
2759
2760 /* Test bt_ctf_trace_set_environment_field_string */
2761 ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str",
2762 "yeah"),
2763 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2764 ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"),
2765 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2766 ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
2767 NULL),
2768 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2769 ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
2770 "oh yeah"),
2771 "bt_ctf_trace_set_environment_field_string succeeds");
2772
2773 /* Test bt_ctf_trace_get_environment_field_count */
2774 ok(bt_ctf_trace_get_environment_field_count(NULL) < 0,
2775 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2776 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
2777 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2778
2779 /* Test bt_ctf_trace_get_environment_field_name */
2780 ok(bt_ctf_trace_get_environment_field_name(NULL, 0) == NULL,
2781 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2782 ok(bt_ctf_trace_get_environment_field_name(trace, -1) == NULL,
2783 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2784 ok(bt_ctf_trace_get_environment_field_name(trace, 5) == NULL,
2785 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2786 ret_string = bt_ctf_trace_get_environment_field_name(trace, 0);
2787 ok(ret_string && !strcmp(ret_string, "host"),
2788 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2789 ret_string = bt_ctf_trace_get_environment_field_name(trace, 1);
2790 ok(ret_string && !strcmp(ret_string, "test_env_int_obj"),
2791 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2792 ret_string = bt_ctf_trace_get_environment_field_name(trace, 2);
2793 ok(ret_string && !strcmp(ret_string, "test_env_str_obj"),
2794 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2795 ret_string = bt_ctf_trace_get_environment_field_name(trace, 3);
2796 ok(ret_string && !strcmp(ret_string, "test_env_int"),
2797 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2798 ret_string = bt_ctf_trace_get_environment_field_name(trace, 4);
2799 ok(ret_string && !strcmp(ret_string, "test_env_str"),
2800 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2801
2802 /* Test bt_ctf_trace_get_environment_field_value */
2803 ok(bt_ctf_trace_get_environment_field_value(NULL, 0) == NULL,
2804 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2805 ok(bt_ctf_trace_get_environment_field_value(trace, -1) == NULL,
2806 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2807 ok(bt_ctf_trace_get_environment_field_value(trace, 5) == NULL,
2808 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2809 obj = bt_ctf_trace_get_environment_field_value(trace, 1);
2810 ret = bt_value_integer_get(obj, &ret_int64_t);
2811 ok(!ret && ret_int64_t == 23,
2812 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2813 BT_PUT(obj);
2814 obj = bt_ctf_trace_get_environment_field_value(trace, 2);
2815 ret = bt_value_string_get(obj, &ret_string);
2816 ok(!ret && ret_string && !strcmp(ret_string, "the value"),
2817 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2818 BT_PUT(obj);
2819
2820 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2821 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL,
2822 "test_env_str"),
2823 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2824 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, NULL),
2825 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2826 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, "oh oh"),
2827 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2828 obj = bt_ctf_trace_get_environment_field_value_by_name(trace,
2829 "test_env_str");
2830 ret = bt_value_string_get(obj, &ret_string);
2831 ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"),
2832 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2833 BT_PUT(obj);
2834
2835 /* Test environment field replacement */
2836 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
2837 654321),
2838 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2839 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
2840 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2841 obj = bt_ctf_trace_get_environment_field_value(trace, 3);
2842 ret = bt_value_integer_get(obj, &ret_int64_t);
2843 ok(!ret && ret_int64_t == 654321,
2844 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2845 BT_PUT(obj);
2846
2847 /* On Solaris, uname() can return any positive value on success */
2848 if (uname(&name) < 0) {
2849 perror("uname");
2850 return -1;
2851 }
2852
2853 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
2854 == 0, "Add sysname (%s) environment field to writer instance",
2855 name.sysname);
2856 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
2857 name.nodename) == 0,
2858 "Add nodename (%s) environment field to writer instance",
2859 name.nodename);
2860 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
2861 == 0, "Add release (%s) environment field to writer instance",
2862 name.release);
2863 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
2864 == 0, "Add version (%s) environment field to writer instance",
2865 name.version);
2866 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
2867 == 0, "Add machine (%s) environment field to writer istance",
2868 name.machine);
2869
2870 /* Define a clock and add it to the trace */
2871 ok(bt_ctf_clock_create("signed") == NULL,
2872 "Illegal clock name rejected");
2873 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
2874 clock = bt_ctf_clock_create(clock_name);
2875 ok(clock, "Clock created sucessfully");
2876 returned_clock_name = bt_ctf_clock_get_name(clock);
2877 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
2878 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
2879 "Returned clock name is valid");
2880
2881 returned_clock_description = bt_ctf_clock_get_description(clock);
2882 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
2883 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
2884 "Clock description set successfully");
2885
2886 returned_clock_description = bt_ctf_clock_get_description(clock);
2887 ok(returned_clock_description,
2888 "bt_ctf_clock_get_description returns a description.");
2889 ok(returned_clock_description ?
2890 !strcmp(returned_clock_description, clock_description) : 0,
2891 "Returned clock description is valid");
2892
2893 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
2894 "bt_ctf_clock_get_frequency returns the correct default frequency");
2895 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
2896 "Set clock frequency");
2897 ok(bt_ctf_clock_get_frequency(clock) == frequency,
2898 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2899
2900 ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S,
2901 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2902 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
2903 "Set clock offset (seconds)");
2904 ok(bt_ctf_clock_get_offset_s(clock) == offset_s,
2905 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2906
2907 ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET,
2908 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
2909 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
2910 ok(bt_ctf_clock_get_offset(clock) == offset,
2911 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
2912
2913 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
2914 "bt_ctf_clock_get_precision returns the correct default precision");
2915 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
2916 "Set clock precision");
2917 ok(bt_ctf_clock_get_precision(clock) == precision,
2918 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2919
2920 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
2921 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2922 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
2923 "Set clock absolute property");
2924 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
2925 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2926
2927 ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME,
2928 "bt_ctf_clock_get_time returns the correct default time");
2929 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
2930 "Set clock time");
2931 ok(bt_ctf_clock_get_time(clock) == current_time,
2932 "bt_ctf_clock_get_time returns the correct time once it is set");
2933
2934 ok(bt_ctf_writer_add_clock(writer, clock) == 0,
2935 "Add clock to writer instance");
2936 ok(bt_ctf_writer_add_clock(writer, clock),
2937 "Verify a clock can't be added twice to a writer instance");
2938
2939 ok(bt_ctf_trace_get_clock_count(NULL) < 0,
2940 "bt_ctf_trace_get_clock_count correctly handles NULL");
2941 ok(bt_ctf_trace_get_clock_count(trace) == 1,
2942 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
2943 ok(!bt_ctf_trace_get_clock(NULL, 0),
2944 "bt_ctf_trace_get_clock correctly handles NULL");
2945 ok(!bt_ctf_trace_get_clock(trace, -1),
2946 "bt_ctf_trace_get_clock correctly handles negative indexes");
2947 ok(!bt_ctf_trace_get_clock(trace, 1),
2948 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
2949 ret_clock = bt_ctf_trace_get_clock(trace, 0);
2950 ok(ret_clock == clock,
2951 "bt_ctf_trace_get_clock returns the right clock instance");
2952 bt_put(ret_clock);
2953 ok(!bt_ctf_trace_get_clock_by_name(trace, NULL),
2954 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
2955 ok(!bt_ctf_trace_get_clock_by_name(NULL, clock_name),
2956 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
2957 ok(!bt_ctf_trace_get_clock_by_name(NULL, NULL),
2958 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
2959 ret_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name);
2960 ok(ret_clock == clock,
2961 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
2962 bt_put(ret_clock);
2963 ok(!bt_ctf_trace_get_clock_by_name(trace, "random"),
2964 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
2965
2966 ok(!bt_ctf_clock_get_name(NULL),
2967 "bt_ctf_clock_get_name correctly handles NULL");
2968 ok(!bt_ctf_clock_get_description(NULL),
2969 "bt_ctf_clock_get_description correctly handles NULL");
2970 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
2971 "bt_ctf_clock_get_frequency correctly handles NULL");
2972 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
2973 "bt_ctf_clock_get_precision correctly handles NULL");
2974 ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL,
2975 "bt_ctf_clock_get_offset_s correctly handles NULL");
2976 ok(bt_ctf_clock_get_offset(NULL) == -1ULL,
2977 "bt_ctf_clock_get_offset correctly handles NULL");
2978 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
2979 "bt_ctf_clock_get_is_absolute correctly handles NULL");
2980 ok(bt_ctf_clock_get_time(NULL) == -1ULL,
2981 "bt_ctf_clock_get_time correctly handles NULL");
2982
2983 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
2984 "bt_ctf_clock_set_description correctly handles NULL clock");
2985 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
2986 "bt_ctf_clock_set_frequency correctly handles NULL clock");
2987 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
2988 "bt_ctf_clock_get_precision correctly handles NULL clock");
2989 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
2990 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
2991 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
2992 "bt_ctf_clock_set_offset correctly handles NULL clock");
2993 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
2994 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
2995 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
2996 "bt_ctf_clock_set_time correctly handles NULL clock");
2997 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
2998 "bt_ctf_clock_get_uuid correctly handles NULL clock");
2999 ret_uuid = bt_ctf_clock_get_uuid(clock);
3000 ok(ret_uuid,
3001 "bt_ctf_clock_get_uuid returns a UUID");
3002 if (ret_uuid) {
3003 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
3004 /* Slightly modify UUID */
3005 tmp_uuid[sizeof(tmp_uuid) - 1]++;
3006 }
3007
3008 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
3009 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3010 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
3011 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3012 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
3013 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3014 ret_uuid = bt_ctf_clock_get_uuid(clock);
3015 ok(ret_uuid,
3016 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3017 ok(uuid_match(ret_uuid, tmp_uuid),
3018 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3019
3020 /* Define a stream class */
3021 stream_class = bt_ctf_stream_class_create("test_stream");
3022
3023 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
3024 "bt_ctf_stream_class_get_name handles NULL correctly");
3025 ret_string = bt_ctf_stream_class_get_name(stream_class);
3026 ok(ret_string && !strcmp(ret_string, "test_stream"),
3027 "bt_ctf_stream_class_get_name returns a correct stream class name");
3028
3029 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
3030 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3031 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
3032 "bt_ctf_stream_class_get_clock handles NULL correctly");
3033
3034 ok(stream_class, "Create stream class");
3035 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
3036 "Set a stream class' clock");
3037 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
3038 ok(ret_clock == clock,
3039 "bt_ctf_stream_class_get_clock returns a correct clock");
3040 bt_put(ret_clock);
3041
3042 /* Test the event fields and event types APIs */
3043 type_field_tests();
3044
3045 /* Test fields copying */
3046 field_copy_tests();
3047
3048 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
3049 "bt_ctf_stream_class_get_id returns an error when no id is set");
3050 ok(bt_ctf_stream_class_get_id(NULL) < 0,
3051 "bt_ctf_stream_class_get_id handles NULL correctly");
3052 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
3053 "bt_ctf_stream_class_set_id handles NULL correctly");
3054 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
3055 "Set an stream class' id");
3056 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
3057 "bt_ctf_stream_class_get_id returns the correct value");
3058
3059 /* Validate default event header fields */
3060 ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL,
3061 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3062 ret_field_type = bt_ctf_stream_class_get_event_header_type(
3063 stream_class);
3064 ok(ret_field_type,
3065 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3066 ok(bt_ctf_field_type_get_type_id(ret_field_type) == CTF_TYPE_STRUCT,
3067 "Default event header type is a structure");
3068 event_header_field_type =
3069 bt_ctf_field_type_structure_get_field_type_by_name(
3070 ret_field_type, "id");
3071 ok(event_header_field_type,
3072 "Default event header type contains an \"id\" field");
3073 ok(bt_ctf_field_type_get_type_id(
3074 event_header_field_type) == CTF_TYPE_INTEGER,
3075 "Default event header \"id\" field is an integer");
3076 bt_put(event_header_field_type);
3077 event_header_field_type =
3078 bt_ctf_field_type_structure_get_field_type_by_name(
3079 ret_field_type, "timestamp");
3080 ok(event_header_field_type,
3081 "Default event header type contains a \"timestamp\" field");
3082 ok(bt_ctf_field_type_get_type_id(
3083 event_header_field_type) == CTF_TYPE_INTEGER,
3084 "Default event header \"timestamp\" field is an integer");
3085 bt_put(event_header_field_type);
3086 bt_put(ret_field_type);
3087
3088 /* Add a custom trace packet header field */
3089 ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
3090 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3091 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3092 ok(packet_header_type,
3093 "bt_ctf_trace_get_packet_header_type returns a packet header");
3094 ok(bt_ctf_field_type_get_type_id(packet_header_type) == CTF_TYPE_STRUCT,
3095 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3096 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3097 packet_header_type, "magic");
3098 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
3099 bt_put(ret_field_type);
3100 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3101 packet_header_type, "uuid");
3102 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
3103 bt_put(ret_field_type);
3104 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3105 packet_header_type, "stream_id");
3106 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
3107 bt_put(ret_field_type);
3108
3109 packet_header_field_type = bt_ctf_field_type_integer_create(22);
3110 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
3111 packet_header_field_type, "custom_trace_packet_header_field"),
3112 "Added a custom trace packet header field successfully");
3113
3114 ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
3115 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3116 ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0,
3117 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3118 ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
3119 "Set a trace packet_header_type successfully");
3120
3121 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
3122 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3123
3124 /* Add a custom field to the stream class' packet context */
3125 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
3126 ok(packet_context_type,
3127 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3128 ok(bt_ctf_field_type_get_type_id(packet_context_type) == CTF_TYPE_STRUCT,
3129 "Packet context is a structure");
3130
3131 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
3132 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3133 ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL),
3134 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3135
3136 integer_type = bt_ctf_field_type_integer_create(32);
3137 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
3138 integer_type) < 0,
3139 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3140 /* Create a "uint5_t" equivalent custom packet context field */
3141 packet_context_field_type = bt_ctf_field_type_integer_create(5);
3142
3143 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3144 packet_context_field_type, "custom_packet_context_field");
3145 ok(ret == 0, "Packet context field added successfully");
3146
3147 /* Define a stream event context containing a my_integer field. */
3148 ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL,
3149 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3150 ok(bt_ctf_stream_class_get_event_context_type(
3151 stream_class) == NULL,
3152 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3153 stream_event_context_type = bt_ctf_field_type_structure_create();
3154 bt_ctf_field_type_structure_add_field(stream_event_context_type,
3155 integer_type, "common_event_context");
3156
3157 ok(bt_ctf_stream_class_set_event_context_type(NULL,
3158 stream_event_context_type) < 0,
3159 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3160 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3161 NULL) < 0,
3162 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3163 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3164 integer_type) < 0,
3165 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3166
3167 ok(bt_ctf_stream_class_set_event_context_type(
3168 stream_class, stream_event_context_type) == 0,
3169 "Set a new stream event context type");
3170 ret_field_type = bt_ctf_stream_class_get_event_context_type(
3171 stream_class);
3172 ok(ret_field_type == stream_event_context_type,
3173 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3174 bt_put(ret_field_type);
3175
3176 /* Instantiate a stream and append events */
3177 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
3178 ok(stream1, "Instanciate a stream class from writer");
3179
3180 ok(bt_ctf_stream_get_class(NULL) == NULL,
3181 "bt_ctf_stream_get_class correctly handles NULL");
3182 ret_stream_class = bt_ctf_stream_get_class(stream1);
3183 ok(ret_stream_class,
3184 "bt_ctf_stream_get_class returns a stream class");
3185 ok(ret_stream_class == stream_class,
3186 "Returned stream class is of the correct type");
3187
3188 /*
3189 * Try to modify the packet context type after a stream has been
3190 * created.
3191 */
3192 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
3193 packet_header_field_type, "should_fail");
3194 ok(ret < 0,
3195 "Trace packet header type can't be modified once a stream has been instanciated");
3196
3197 /*
3198 * Try to modify the packet context type after a stream has been
3199 * created.
3200 */
3201 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3202 packet_context_field_type, "should_fail");
3203 ok(ret < 0,
3204 "Packet context type can't be modified once a stream has been instanciated");
3205
3206 /*
3207 * Try to modify the stream event context type after a stream has been
3208 * created.
3209 */
3210 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
3211 integer_type, "should_fail");
3212 ok(ret < 0,
3213 "Stream event context type can't be modified once a stream has been instanciated");
3214
3215 /* Should fail after instanciating a stream (frozen) */
3216 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
3217 "Changes to a stream class that was already instantiated fail");
3218
3219 /* Populate the custom packet header field only once for all tests */
3220 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
3221 "bt_ctf_stream_get_packet_header handles NULL correctly");
3222 packet_header = bt_ctf_stream_get_packet_header(stream1);
3223 ok(packet_header,
3224 "bt_ctf_stream_get_packet_header returns a packet header");
3225 ret_field_type = bt_ctf_field_get_type(packet_header);
3226 ok(ret_field_type == packet_header_type,
3227 "Stream returns a packet header of the appropriate type");
3228 bt_put(ret_field_type);
3229 packet_header_field = bt_ctf_field_structure_get_field(packet_header,
3230 "custom_trace_packet_header_field");
3231 ok(packet_header_field,
3232 "Packet header structure contains a custom field with the appropriate name");
3233 ret_field_type = bt_ctf_field_get_type(packet_header_field);
3234 ok(ret_field_type == packet_header_field_type,
3235 "Custom packet header field is of the expected type");
3236 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
3237 54321), "Set custom packet header value successfully");
3238 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
3239 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3240 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
3241 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3242 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
3243 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3244 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
3245 "Successfully set a stream's packet header");
3246
3247 ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0,
3248 "Add environment field to writer after stream creation");
3249
3250 test_instanciate_event_before_stream(writer);
3251
3252 append_simple_event(stream_class, stream1, clock);
3253
3254 packet_resize_test(stream_class, stream1, clock);
3255
3256 append_complex_event(stream_class, stream1, clock);
3257
3258 append_existing_event_class(stream_class);
3259
3260 test_empty_stream(writer);
3261
3262 test_custom_event_header_stream(writer);
3263
3264 metadata_string = bt_ctf_writer_get_metadata_string(writer);
3265 ok(metadata_string, "Get metadata string");
3266
3267 bt_ctf_writer_flush_metadata(writer);
3268 validate_metadata(argv[1], metadata_path);
3269 validate_trace(argv[2], trace_path);
3270
3271 bt_put(clock);
3272 bt_put(ret_stream_class);
3273 bt_put(writer);
3274 bt_put(stream1);
3275 bt_put(packet_context_type);
3276 bt_put(packet_context_field_type);
3277 bt_put(integer_type);
3278 bt_put(stream_event_context_type);
3279 bt_put(ret_field_type);
3280 bt_put(packet_header_type);
3281 bt_put(packet_header_field_type);
3282 bt_put(packet_header);
3283 bt_put(packet_header_field);
3284 bt_put(trace);
3285 free(metadata_string);
3286
3287 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
3288 "bt_ctf_stream_class_get_trace returns NULL after its trace has been reclaimed");
3289 bt_put(stream_class);
3290
3291 /* Remove all trace files and delete temporary trace directory */
3292 DIR *trace_dir = opendir(trace_path);
3293 if (!trace_dir) {
3294 perror("# opendir");
3295 return -1;
3296 }
3297
3298 struct dirent *entry;
3299 while ((entry = readdir(trace_dir))) {
3300 if (entry->d_type == DT_REG) {
3301 unlinkat(bt_dirfd(trace_dir), entry->d_name, 0);
3302 }
3303 }
3304
3305 rmdir(trace_path);
3306 closedir(trace_dir);
3307 return 0;
3308 }
This page took 0.147859 seconds and 5 git commands to generate.