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