Port: Replace dirent->d_type by stat S_ISREG
[babeltrace.git] / tests / lib / test_ctf_writer.c
1 /*
2 * test-ctf-writer.c
3 *
4 * CTF Writer test
5 *
6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-ir/stream-class.h>
29 #include <babeltrace/ref.h>
30 #include <babeltrace/ctf/events.h>
31 #include <babeltrace/values.h>
32 #include <unistd.h>
33 #include <babeltrace/compat/stdlib.h>
34 #include <stdio.h>
35 #include <sys/utsname.h>
36 #include <babeltrace/compat/limits.h>
37 #include <string.h>
38 #include <assert.h>
39 #include <sys/wait.h>
40 #include <fcntl.h>
41 #include <babeltrace/compat/dirent.h>
42 #include "tap/tap.h"
43 #include <math.h>
44 #include <float.h>
45 #include <sys/stat.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 obj = bt_value_integer_create_init(15);
957 assert(obj);
958 ok(bt_ctf_event_class_set_attribute(NULL, "id", obj),
959 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
960 ok(bt_ctf_event_class_set_attribute(event_class, NULL, obj),
961 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
962 ok(bt_ctf_event_class_set_attribute(event_class, "id", NULL),
963 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
964 assert(!bt_value_integer_set(obj, -3));
965 ok(bt_ctf_event_class_set_attribute(event_class, "id", obj),
966 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
967 assert(!bt_value_integer_set(obj, 11));
968 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
969 ok(!ret && bt_ctf_event_class_get_id(event_class) == 11,
970 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
971 ret = bt_ctf_event_class_set_attribute(event_class, "name", obj);
972 ret &= bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj);
973 ok(ret,
974 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
975 BT_PUT(obj);
976
977 obj = bt_value_integer_create_init(5);
978 assert(obj);
979 ok(!bt_ctf_event_class_set_attribute(event_class, "loglevel", obj),
980 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
981 BT_PUT(obj);
982 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL, "loglevel"),
983 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
984 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, NULL),
985 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
986 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, "meow"),
987 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
988 obj = bt_ctf_event_class_get_attribute_value_by_name(event_class,
989 "loglevel");
990 int64_value = 0;
991 ret = bt_value_integer_get(obj, &int64_value);
992 ok(obj && !ret && int64_value == 5,
993 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
994 BT_PUT(obj);
995
996 obj = bt_value_string_create_init("nu name");
997 assert(obj);
998 assert(!bt_ctf_event_class_set_attribute(event_class, "name", obj));
999 ret_string = bt_ctf_event_class_get_name(event_class);
1000 ok(!strcmp(ret_string, "nu name"),
1001 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
1002 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
1003 ret &= bt_ctf_event_class_set_attribute(event_class, "loglevel", obj);
1004 ok(ret,
1005 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1006 BT_PUT(obj);
1007 obj = bt_value_string_create_init("http://kernel.org/");
1008 assert(obj);
1009 assert(!bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj));
1010 BT_PUT(obj);
1011
1012 ok(bt_ctf_event_class_get_attribute_count(NULL),
1013 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1014 ok(bt_ctf_event_class_get_attribute_count(event_class) == 4,
1015 "bt_ctf_event_class_get_attribute_count returns the correct count");
1016 ok(!bt_ctf_event_class_get_attribute_name(NULL, 0),
1017 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1018 ok(!bt_ctf_event_class_get_attribute_name(event_class, 4),
1019 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1020 ok(!bt_ctf_event_class_get_attribute_value(NULL, 0),
1021 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1022 ok(!bt_ctf_event_class_get_attribute_value(event_class, 4),
1023 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1024
1025 memset(&attrs_count, 0, sizeof(attrs_count));
1026
1027 for (i = 0; i < 4; ++i) {
1028 ret_string = bt_ctf_event_class_get_attribute_name(event_class,
1029 i);
1030 obj = bt_ctf_event_class_get_attribute_value(event_class, i);
1031 assert(ret_string && obj);
1032
1033 if (!strcmp(ret_string, "id")) {
1034 attrs_count.id++;
1035 ok(bt_value_is_integer(obj),
1036 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1037 ret_string);
1038 } else if (!strcmp(ret_string, "name")) {
1039 attrs_count.name++;
1040 ok(bt_value_is_string(obj),
1041 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1042 ret_string);
1043 } else if (!strcmp(ret_string, "loglevel")) {
1044 attrs_count.loglevel++;
1045 ok(bt_value_is_integer(obj),
1046 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1047 ret_string);
1048 } else if (!strcmp(ret_string, "model.emf.uri")) {
1049 attrs_count.modelemfuri++;
1050 ok(bt_value_is_string(obj),
1051 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1052 ret_string);
1053 } else {
1054 attrs_count.unknown++;
1055 }
1056
1057 BT_PUT(obj);
1058 }
1059
1060 ok(attrs_count.unknown == 0, "event class has no unknown attributes");
1061 ok(attrs_count.id == 1 && attrs_count.name == 1 &&
1062 attrs_count.loglevel == 1 && attrs_count.modelemfuri == 1,
1063 "event class has one instance of each known attribute");
1064
1065 /* Add event class to the stream class */
1066 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
1067 "Reject addition of NULL event class to a stream class");
1068 ok(bt_ctf_stream_class_add_event_class(stream_class,
1069 event_class) == 0, "Add an event class to stream class");
1070
1071 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
1072 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1073 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
1074 ok(ret_stream_class == stream_class,
1075 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1076 bt_put(ret_stream_class);
1077
1078 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
1079 "bt_ctf_event_class_get_field_count handles NULL correctly");
1080 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
1081 "bt_ctf_event_class_get_field_count returns a correct value");
1082
1083 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
1084 &ret_field_type, 0) < 0,
1085 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1086 ok(bt_ctf_event_class_get_field(event_class, NULL,
1087 &ret_field_type, 0) == 0,
1088 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1089 bt_put(ret_field_type);
1090 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1091 NULL, 0) == 0,
1092 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1093 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1094 &ret_field_type, 42) < 0,
1095 "bt_ctf_event_class_get_field handles an invalid index correctly");
1096 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1097 &ret_field_type, 0) == 0,
1098 "bt_ctf_event_class_get_field returns a field");
1099 ok(ret_field_type == uint_35_type,
1100 "bt_ctf_event_class_get_field returns a correct field type");
1101 bt_put(ret_field_type);
1102 ok(!strcmp(ret_string, "uint_35"),
1103 "bt_ctf_event_class_get_field returns a correct field name");
1104 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
1105 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1106 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
1107 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1108 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
1109 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1110 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
1111 "complex_structure");
1112 ok(ret_field_type == complex_structure_type,
1113 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1114 bt_put(ret_field_type);
1115
1116 event = bt_ctf_event_create(event_class);
1117 ok(event, "Instanciate a complex event");
1118
1119 ok(bt_ctf_event_get_class(NULL) == NULL,
1120 "bt_ctf_event_get_class handles NULL correctly");
1121 ret_event_class = bt_ctf_event_get_class(event);
1122 ok(ret_event_class == event_class,
1123 "bt_ctf_event_get_class returns the correct event class");
1124 bt_put(ret_event_class);
1125
1126 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
1127 if (!uint_35_field) {
1128 printf("uint_35_field is NULL\n");
1129 }
1130
1131 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
1132 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
1133 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
1134 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1135 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
1136 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1137 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
1138 &ret_unsigned_int) == 0,
1139 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1140 ok(ret_unsigned_int == 0x0DDF00D,
1141 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1142 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
1143 &ret_signed_int) < 0,
1144 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1145 bt_put(uint_35_field);
1146
1147 int_16_field = bt_ctf_event_get_payload(event, "int_16");
1148 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
1149 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
1150 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1151 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
1152 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1153 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
1154 &ret_signed_int) == 0,
1155 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1156 ok(ret_signed_int == -12345,
1157 "bt_ctf_field_signed_integer_get_value returns the correct value");
1158 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
1159 &ret_unsigned_int) < 0,
1160 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1161 bt_put(int_16_field);
1162
1163 complex_structure_field = bt_ctf_event_get_payload(event,
1164 "complex_structure");
1165
1166 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
1167 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1168 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
1169 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1170 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
1171 complex_structure_field, 3);
1172 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
1173 bt_put(inner_structure_field);
1174 ok(ret_field_type == inner_structure_type,
1175 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1176 bt_put(ret_field_type);
1177
1178 inner_structure_field = bt_ctf_field_structure_get_field(
1179 complex_structure_field, "inner_structure");
1180 a_string_field = bt_ctf_field_structure_get_field(
1181 complex_structure_field, "a_string");
1182 enum_variant_field = bt_ctf_field_structure_get_field(
1183 complex_structure_field, "variant_selector");
1184 variant_field = bt_ctf_field_structure_get_field(
1185 complex_structure_field, "variant_value");
1186 uint_35_field = bt_ctf_field_structure_get_field(
1187 inner_structure_field, "seq_len");
1188 a_sequence_field = bt_ctf_field_structure_get_field(
1189 inner_structure_field, "a_sequence");
1190 an_array_field = bt_ctf_field_structure_get_field(
1191 inner_structure_field, "an_array");
1192
1193 enum_container_field = bt_ctf_field_enumeration_get_container(
1194 enum_variant_field);
1195 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
1196 int_16_field = bt_ctf_field_variant_get_field(variant_field,
1197 enum_variant_field);
1198 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
1199 bt_put(int_16_field);
1200 ok(!bt_ctf_field_string_get_value(a_string_field),
1201 "bt_ctf_field_string_get_value returns NULL on an unset field");
1202 bt_ctf_field_string_set_value(a_string_field,
1203 test_string_1);
1204 ok(!bt_ctf_field_string_get_value(NULL),
1205 "bt_ctf_field_string_get_value correctly handles NULL");
1206 ok(bt_ctf_field_string_append(NULL, "yeah"),
1207 "bt_ctf_field_string_append correctly handles a NULL string field");
1208 ok(bt_ctf_field_string_append(a_string_field, NULL),
1209 "bt_ctf_field_string_append correctly handles a NULL string value");
1210 ok(!bt_ctf_field_string_append(a_string_field, test_string_2),
1211 "bt_ctf_field_string_append succeeds");
1212 ok(bt_ctf_field_string_append_len(NULL, "oh noes", 3),
1213 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1214 ok(bt_ctf_field_string_append_len(a_string_field, NULL, 3),
1215 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1216 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5),
1217 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1218 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_4, 10),
1219 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1220 ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[4], 3),
1221 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1222 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0),
1223 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1224
1225 ret_string = bt_ctf_field_string_get_value(a_string_field);
1226 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
1227 ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0,
1228 "bt_ctf_field_string_get_value returns a correct value");
1229 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
1230 SEQUENCE_TEST_LENGTH);
1231
1232 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
1233 enum_container_field) == NULL,
1234 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1235 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
1236 NULL) == NULL,
1237 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1238 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
1239 variant_type, enum_variant_field);
1240 ok(ret_field_type == int_16_type,
1241 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1242
1243 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
1244 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1245 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
1246 uint_35_field) == 0, "Set a sequence field's length");
1247 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
1248 ok(ret_field == uint_35_field,
1249 "bt_ctf_field_sequence_get_length returns the correct length field");
1250 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
1251 "bt_ctf_field_sequence_get_length properly handles NULL");
1252
1253 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
1254 int_16_field = bt_ctf_field_sequence_get_field(
1255 a_sequence_field, i);
1256 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
1257 bt_put(int_16_field);
1258 }
1259
1260 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
1261 int_16_field = bt_ctf_field_array_get_field(
1262 an_array_field, i);
1263 bt_ctf_field_signed_integer_set_value(int_16_field, i);
1264 bt_put(int_16_field);
1265 }
1266
1267 bt_ctf_clock_set_time(clock, ++current_time);
1268 ok(bt_ctf_stream_append_event(stream, event) == 0,
1269 "Append a complex event to a stream");
1270
1271 /*
1272 * Populate the custom packet context field with a dummy value
1273 * otherwise flush will fail.
1274 */
1275 packet_context = bt_ctf_stream_get_packet_context(stream);
1276 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1277 "custom_packet_context_field");
1278 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
1279
1280 ok(bt_ctf_stream_flush(stream) == 0,
1281 "Flush a stream containing a complex event");
1282
1283 bt_put(uint_35_field);
1284 bt_put(a_string_field);
1285 bt_put(inner_structure_field);
1286 bt_put(complex_structure_field);
1287 bt_put(a_sequence_field);
1288 bt_put(an_array_field);
1289 bt_put(enum_variant_field);
1290 bt_put(enum_container_field);
1291 bt_put(variant_field);
1292 bt_put(ret_field);
1293 bt_put(packet_context_field);
1294 bt_put(packet_context);
1295 bt_put(uint_35_type);
1296 bt_put(int_16_type);
1297 bt_put(string_type);
1298 bt_put(sequence_type);
1299 bt_put(array_type);
1300 bt_put(inner_structure_type);
1301 bt_put(complex_structure_type);
1302 bt_put(uint_3_type);
1303 bt_put(enum_variant_type);
1304 bt_put(variant_type);
1305 bt_put(ret_field_type);
1306 bt_put(event_class);
1307 bt_put(event);
1308 }
1309
1310 static void field_copy_tests_validate_same_type(struct bt_ctf_field *field,
1311 struct bt_ctf_field_type *expected_type, const char *name)
1312 {
1313 struct bt_ctf_field_type *copy_type;
1314
1315 copy_type = bt_ctf_field_get_type(field);
1316 ok(copy_type == expected_type,
1317 "bt_ctf_field_copy does not copy the type (%s)", name);
1318 bt_put(copy_type);
1319 }
1320
1321 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field *field_a,
1322 struct bt_ctf_field *field_b, const char *name)
1323 {
1324 ok(field_a != field_b,
1325 "bt_ctf_field_copy creates different pointers (%s)", name);
1326 }
1327
1328 void field_copy_tests()
1329 {
1330 struct bt_ctf_field_type *len_type = NULL;
1331 struct bt_ctf_field_type *fp_type = NULL;
1332 struct bt_ctf_field_type *s_type = NULL;
1333 struct bt_ctf_field_type *e_int_type = NULL;
1334 struct bt_ctf_field_type *e_type = NULL;
1335 struct bt_ctf_field_type *v_type = NULL;
1336 struct bt_ctf_field_type *v_label1_type = NULL;
1337 struct bt_ctf_field_type *v_label1_array_type = NULL;
1338 struct bt_ctf_field_type *v_label2_type = NULL;
1339 struct bt_ctf_field_type *v_label2_seq_type = NULL;
1340 struct bt_ctf_field_type *strct_type = NULL;
1341 struct bt_ctf_field *len = NULL;
1342 struct bt_ctf_field *fp = NULL;
1343 struct bt_ctf_field *s = NULL;
1344 struct bt_ctf_field *e_int = NULL;
1345 struct bt_ctf_field *e = NULL;
1346 struct bt_ctf_field *v = NULL;
1347 struct bt_ctf_field *v_selected = NULL;
1348 struct bt_ctf_field *v_selected_cur = NULL;
1349 struct bt_ctf_field *v_selected_0 = NULL;
1350 struct bt_ctf_field *v_selected_1 = NULL;
1351 struct bt_ctf_field *v_selected_2 = NULL;
1352 struct bt_ctf_field *v_selected_3 = NULL;
1353 struct bt_ctf_field *v_selected_4 = NULL;
1354 struct bt_ctf_field *v_selected_5 = NULL;
1355 struct bt_ctf_field *v_selected_6 = NULL;
1356 struct bt_ctf_field *a = NULL;
1357 struct bt_ctf_field *a_0 = NULL;
1358 struct bt_ctf_field *a_1 = NULL;
1359 struct bt_ctf_field *a_2 = NULL;
1360 struct bt_ctf_field *a_3 = NULL;
1361 struct bt_ctf_field *a_4 = NULL;
1362 struct bt_ctf_field *strct = NULL;
1363 struct bt_ctf_field *len_copy = NULL;
1364 struct bt_ctf_field *fp_copy = NULL;
1365 struct bt_ctf_field *s_copy = NULL;
1366 struct bt_ctf_field *e_int_copy = NULL;
1367 struct bt_ctf_field *e_copy = NULL;
1368 struct bt_ctf_field *v_copy = NULL;
1369 struct bt_ctf_field *v_selected_copy = NULL;
1370 struct bt_ctf_field *v_selected_copy_len = NULL;
1371 struct bt_ctf_field *v_selected_0_copy = NULL;
1372 struct bt_ctf_field *v_selected_1_copy = NULL;
1373 struct bt_ctf_field *v_selected_2_copy = NULL;
1374 struct bt_ctf_field *v_selected_3_copy = NULL;
1375 struct bt_ctf_field *v_selected_4_copy = NULL;
1376 struct bt_ctf_field *v_selected_5_copy = NULL;
1377 struct bt_ctf_field *v_selected_6_copy = NULL;
1378 struct bt_ctf_field *a_copy = NULL;
1379 struct bt_ctf_field *a_0_copy = NULL;
1380 struct bt_ctf_field *a_1_copy = NULL;
1381 struct bt_ctf_field *a_2_copy = NULL;
1382 struct bt_ctf_field *a_3_copy = NULL;
1383 struct bt_ctf_field *a_4_copy = NULL;
1384 struct bt_ctf_field *strct_copy = NULL;
1385 uint64_t uint64_t_val;
1386 const char *str_val;
1387 double double_val;
1388 int ret;
1389
1390 /* create len type */
1391 len_type = bt_ctf_field_type_integer_create(32);
1392 assert(len_type);
1393
1394 /* create fp type */
1395 fp_type = bt_ctf_field_type_floating_point_create();
1396 assert(fp_type);
1397
1398 /* create s type */
1399 s_type = bt_ctf_field_type_string_create();
1400 assert(s_type);
1401
1402 /* create e_int type */
1403 e_int_type = bt_ctf_field_type_integer_create(8);
1404 assert(e_int_type);
1405
1406 /* create e type */
1407 e_type = bt_ctf_field_type_enumeration_create(e_int_type);
1408 assert(e_type);
1409 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL1",
1410 10, 15);
1411 assert(!ret);
1412 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL2",
1413 23, 23);
1414 assert(!ret);
1415
1416 /* create v_label1 type */
1417 v_label1_type = bt_ctf_field_type_string_create();
1418 assert(v_label1_type);
1419
1420 /* create v_label1_array type */
1421 v_label1_array_type = bt_ctf_field_type_array_create(v_label1_type, 5);
1422 assert(v_label1_array_type);
1423
1424 /* create v_label2 type */
1425 v_label2_type = bt_ctf_field_type_integer_create(16);
1426 assert(v_label2_type);
1427
1428 /* create v_label2_seq type */
1429 v_label2_seq_type = bt_ctf_field_type_sequence_create(v_label2_type,
1430 "len");
1431 assert(v_label2_seq_type);
1432
1433 /* create v type */
1434 v_type = bt_ctf_field_type_variant_create(e_type, "e");
1435 assert(v_type);
1436 ret = bt_ctf_field_type_variant_add_field(v_type, v_label1_array_type,
1437 "LABEL1");
1438 assert(!ret);
1439 ret = bt_ctf_field_type_variant_add_field(v_type, v_label2_seq_type,
1440 "LABEL2");
1441 assert(!ret);
1442
1443 /* create strct type */
1444 strct_type = bt_ctf_field_type_structure_create();
1445 assert(strct_type);
1446 ret = bt_ctf_field_type_structure_add_field(strct_type, len_type,
1447 "len");
1448 assert(!ret);
1449 ret = bt_ctf_field_type_structure_add_field(strct_type, fp_type, "fp");
1450 assert(!ret);
1451 ret = bt_ctf_field_type_structure_add_field(strct_type, s_type, "s");
1452 assert(!ret);
1453 ret = bt_ctf_field_type_structure_add_field(strct_type, e_type, "e");
1454 assert(!ret);
1455 ret = bt_ctf_field_type_structure_add_field(strct_type, v_type, "v");
1456 assert(!ret);
1457 ret = bt_ctf_field_type_structure_add_field(strct_type,
1458 v_label1_array_type, "a");
1459 assert(!ret);
1460
1461 /* create strct */
1462 strct = bt_ctf_field_create(strct_type);
1463 assert(strct);
1464
1465 /* get len field */
1466 len = bt_ctf_field_structure_get_field(strct, "len");
1467 assert(len);
1468
1469 /* get fp field */
1470 fp = bt_ctf_field_structure_get_field(strct, "fp");
1471 assert(fp);
1472
1473 /* get s field */
1474 s = bt_ctf_field_structure_get_field(strct, "s");
1475 assert(s);
1476
1477 /* get e field */
1478 e = bt_ctf_field_structure_get_field(strct, "e");
1479 assert(e);
1480
1481 /* get e_int (underlying integer) */
1482 e_int = bt_ctf_field_enumeration_get_container(e);
1483 assert(e_int);
1484
1485 /* get v field */
1486 v = bt_ctf_field_structure_get_field(strct, "v");
1487 assert(v);
1488
1489 /* get a field */
1490 a = bt_ctf_field_structure_get_field(strct, "a");
1491 assert(a);
1492
1493 /* set len field */
1494 ret = bt_ctf_field_unsigned_integer_set_value(len, 7);
1495 assert(!ret);
1496
1497 /* set fp field */
1498 ret = bt_ctf_field_floating_point_set_value(fp, 3.14);
1499 assert(!ret);
1500
1501 /* set s field */
1502 ret = bt_ctf_field_string_set_value(s, "btbt");
1503 assert(!ret);
1504
1505 /* set e field (LABEL2) */
1506 ret = bt_ctf_field_unsigned_integer_set_value(e_int, 23);
1507 assert(!ret);
1508
1509 /* set v field */
1510 v_selected = bt_ctf_field_variant_get_field(v, e);
1511 assert(v_selected);
1512 ok(!bt_ctf_field_variant_get_current_field(NULL),
1513 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1514 v_selected_cur = bt_ctf_field_variant_get_current_field(v);
1515 ok(v_selected_cur == v_selected,
1516 "bt_ctf_field_variant_get_current_field returns the current field");
1517 bt_put(v_selected_cur);
1518
1519 /* set selected v field */
1520 ret = bt_ctf_field_sequence_set_length(v_selected, len);
1521 assert(!ret);
1522 v_selected_0 = bt_ctf_field_sequence_get_field(v_selected, 0);
1523 assert(v_selected_0);
1524 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_0, 7);
1525 assert(!ret);
1526 v_selected_1 = bt_ctf_field_sequence_get_field(v_selected, 1);
1527 assert(v_selected_1);
1528 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_1, 6);
1529 assert(!ret);
1530 v_selected_2 = bt_ctf_field_sequence_get_field(v_selected, 2);
1531 assert(v_selected_2);
1532 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_2, 5);
1533 assert(!ret);
1534 v_selected_3 = bt_ctf_field_sequence_get_field(v_selected, 3);
1535 assert(v_selected_3);
1536 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_3, 4);
1537 assert(!ret);
1538 v_selected_4 = bt_ctf_field_sequence_get_field(v_selected, 4);
1539 assert(v_selected_4);
1540 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_4, 3);
1541 assert(!ret);
1542 v_selected_5 = bt_ctf_field_sequence_get_field(v_selected, 5);
1543 assert(v_selected_5);
1544 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_5, 2);
1545 assert(!ret);
1546 v_selected_6 = bt_ctf_field_sequence_get_field(v_selected, 6);
1547 assert(v_selected_6);
1548 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_6, 1);
1549 assert(!ret);
1550
1551 /* set a field */
1552 a_0 = bt_ctf_field_array_get_field(a, 0);
1553 assert(a_0);
1554 ret = bt_ctf_field_string_set_value(a_0, "a_0");
1555 assert(!ret);
1556 a_1 = bt_ctf_field_array_get_field(a, 1);
1557 assert(a_1);
1558 ret = bt_ctf_field_string_set_value(a_1, "a_1");
1559 assert(!ret);
1560 a_2 = bt_ctf_field_array_get_field(a, 2);
1561 assert(a_2);
1562 ret = bt_ctf_field_string_set_value(a_2, "a_2");
1563 assert(!ret);
1564 a_3 = bt_ctf_field_array_get_field(a, 3);
1565 assert(a_3);
1566 ret = bt_ctf_field_string_set_value(a_3, "a_3");
1567 assert(!ret);
1568 a_4 = bt_ctf_field_array_get_field(a, 4);
1569 assert(a_4);
1570 ret = bt_ctf_field_string_set_value(a_4, "a_4");
1571 assert(!ret);
1572
1573 /* create copy of strct */
1574 ok(!bt_ctf_field_copy(NULL),
1575 "bt_ctf_field_copy handles NULL correctly");
1576 strct_copy = bt_ctf_field_copy(strct);
1577 ok(strct_copy,
1578 "bt_ctf_field_copy returns a valid pointer");
1579
1580 /* get all copied fields */
1581 len_copy = bt_ctf_field_structure_get_field(strct_copy, "len");
1582 assert(len_copy);
1583 fp_copy = bt_ctf_field_structure_get_field(strct_copy, "fp");
1584 assert(fp_copy);
1585 s_copy = bt_ctf_field_structure_get_field(strct_copy, "s");
1586 assert(s_copy);
1587 e_copy = bt_ctf_field_structure_get_field(strct_copy, "e");
1588 assert(e_copy);
1589 e_int_copy = bt_ctf_field_enumeration_get_container(e_copy);
1590 assert(e_int_copy);
1591 v_copy = bt_ctf_field_structure_get_field(strct_copy, "v");
1592 assert(v_copy);
1593 v_selected_copy = bt_ctf_field_variant_get_field(v_copy, e_copy);
1594 assert(v_selected_copy);
1595 v_selected_0_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 0);
1596 assert(v_selected_0_copy);
1597 v_selected_1_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 1);
1598 assert(v_selected_1_copy);
1599 v_selected_2_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 2);
1600 assert(v_selected_2_copy);
1601 v_selected_3_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 3);
1602 assert(v_selected_3_copy);
1603 v_selected_4_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 4);
1604 assert(v_selected_4_copy);
1605 v_selected_5_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 5);
1606 assert(v_selected_5_copy);
1607 v_selected_6_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 6);
1608 assert(v_selected_6_copy);
1609 ok(!bt_ctf_field_sequence_get_field(v_selected_copy, 7),
1610 "sequence field copy is not too large");
1611 a_copy = bt_ctf_field_structure_get_field(strct_copy, "a");
1612 assert(a_copy);
1613 a_0_copy = bt_ctf_field_array_get_field(a_copy, 0);
1614 assert(a_0_copy);
1615 a_1_copy = bt_ctf_field_array_get_field(a_copy, 1);
1616 assert(a_1_copy);
1617 a_2_copy = bt_ctf_field_array_get_field(a_copy, 2);
1618 assert(a_2_copy);
1619 a_3_copy = bt_ctf_field_array_get_field(a_copy, 3);
1620 assert(a_3_copy);
1621 a_4_copy = bt_ctf_field_array_get_field(a_copy, 4);
1622 assert(a_4_copy);
1623 ok(!bt_ctf_field_array_get_field(v_selected_copy, 5),
1624 "array field copy is not too large");
1625
1626 /* make sure copied fields are different pointers */
1627 field_copy_tests_validate_diff_ptrs(strct_copy, strct, "strct");
1628 field_copy_tests_validate_diff_ptrs(len_copy, len, "len");
1629 field_copy_tests_validate_diff_ptrs(fp_copy, fp, "fp");
1630 field_copy_tests_validate_diff_ptrs(s_copy, s, "s");
1631 field_copy_tests_validate_diff_ptrs(e_int_copy, e_int, "e_int");
1632 field_copy_tests_validate_diff_ptrs(e_copy, e, "e");
1633 field_copy_tests_validate_diff_ptrs(v_copy, v, "v");
1634 field_copy_tests_validate_diff_ptrs(v_selected_copy, v_selected,
1635 "v_selected");
1636 field_copy_tests_validate_diff_ptrs(v_selected_0_copy, v_selected_0,
1637 "v_selected_0");
1638 field_copy_tests_validate_diff_ptrs(v_selected_1_copy, v_selected_1,
1639 "v_selected_1");
1640 field_copy_tests_validate_diff_ptrs(v_selected_2_copy, v_selected_2,
1641 "v_selected_2");
1642 field_copy_tests_validate_diff_ptrs(v_selected_3_copy, v_selected_3,
1643 "v_selected_3");
1644 field_copy_tests_validate_diff_ptrs(v_selected_4_copy, v_selected_4,
1645 "v_selected_4");
1646 field_copy_tests_validate_diff_ptrs(v_selected_5_copy, v_selected_5,
1647 "v_selected_5");
1648 field_copy_tests_validate_diff_ptrs(v_selected_6_copy, v_selected_6,
1649 "v_selected_6");
1650 field_copy_tests_validate_diff_ptrs(a_copy, a, "a");
1651 field_copy_tests_validate_diff_ptrs(a_0_copy, a_0, "a_0");
1652 field_copy_tests_validate_diff_ptrs(a_1_copy, a_1, "a_1");
1653 field_copy_tests_validate_diff_ptrs(a_2_copy, a_2, "a_2");
1654 field_copy_tests_validate_diff_ptrs(a_3_copy, a_3, "a_3");
1655 field_copy_tests_validate_diff_ptrs(a_4_copy, a_4, "a_4");
1656
1657 /* make sure copied fields share the same types */
1658 field_copy_tests_validate_same_type(strct_copy, strct_type, "strct");
1659 field_copy_tests_validate_same_type(len_copy, len_type, "len");
1660 field_copy_tests_validate_same_type(fp_copy, fp_type, "fp");
1661 field_copy_tests_validate_same_type(e_int_copy, e_int_type, "e_int");
1662 field_copy_tests_validate_same_type(e_copy, e_type, "e");
1663 field_copy_tests_validate_same_type(v_copy, v_type, "v");
1664 field_copy_tests_validate_same_type(v_selected_copy, v_label2_seq_type,
1665 "v_selected");
1666 field_copy_tests_validate_same_type(v_selected_0_copy, v_label2_type,
1667 "v_selected_0");
1668 field_copy_tests_validate_same_type(v_selected_1_copy, v_label2_type,
1669 "v_selected_1");
1670 field_copy_tests_validate_same_type(v_selected_2_copy, v_label2_type,
1671 "v_selected_2");
1672 field_copy_tests_validate_same_type(v_selected_3_copy, v_label2_type,
1673 "v_selected_3");
1674 field_copy_tests_validate_same_type(v_selected_4_copy, v_label2_type,
1675 "v_selected_4");
1676 field_copy_tests_validate_same_type(v_selected_5_copy, v_label2_type,
1677 "v_selected_5");
1678 field_copy_tests_validate_same_type(v_selected_6_copy, v_label2_type,
1679 "v_selected_6");
1680 field_copy_tests_validate_same_type(a_copy, v_label1_array_type, "a");
1681 field_copy_tests_validate_same_type(a_0_copy, v_label1_type, "a_0");
1682 field_copy_tests_validate_same_type(a_1_copy, v_label1_type, "a_1");
1683 field_copy_tests_validate_same_type(a_2_copy, v_label1_type, "a_2");
1684 field_copy_tests_validate_same_type(a_3_copy, v_label1_type, "a_3");
1685 field_copy_tests_validate_same_type(a_4_copy, v_label1_type, "a_4");
1686
1687 /* validate len copy */
1688 ret = bt_ctf_field_unsigned_integer_get_value(len_copy, &uint64_t_val);
1689 assert(!ret);
1690 ok(uint64_t_val == 7,
1691 "bt_ctf_field_copy creates a valid integer field copy");
1692
1693 /* validate fp copy */
1694 ret = bt_ctf_field_floating_point_get_value(fp_copy, &double_val);
1695 assert(!ret);
1696 ok(double_val == 3.14,
1697 "bt_ctf_field_copy creates a valid floating point number field copy");
1698
1699 /* validate s copy */
1700 str_val = bt_ctf_field_string_get_value(s_copy);
1701 ok(str_val && !strcmp(str_val, "btbt"),
1702 "bt_ctf_field_copy creates a valid string field copy");
1703
1704 /* validate e_int copy */
1705 ret = bt_ctf_field_unsigned_integer_get_value(e_int_copy,
1706 &uint64_t_val);
1707 assert(!ret);
1708 ok(uint64_t_val == 23,
1709 "bt_ctf_field_copy creates a valid enum's integer field copy");
1710
1711 /* validate e copy */
1712 str_val = bt_ctf_field_enumeration_get_mapping_name(e_copy);
1713 ok(str_val && !strcmp(str_val, "LABEL2"),
1714 "bt_ctf_field_copy creates a valid enum field copy");
1715
1716 /* validate v_selected copy */
1717 v_selected_copy_len = bt_ctf_field_sequence_get_length(v_selected);
1718 assert(v_selected_copy_len);
1719 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len,
1720 &uint64_t_val);
1721 assert(!ret);
1722 ok(uint64_t_val == 7,
1723 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1724 bt_put(v_selected_copy_len);
1725 v_selected_copy_len = NULL;
1726
1727 /* validate v_selected copy fields */
1728 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy,
1729 &uint64_t_val);
1730 assert(!ret);
1731 ok(uint64_t_val == 7,
1732 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1733 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy,
1734 &uint64_t_val);
1735 assert(!ret);
1736 ok(uint64_t_val == 6,
1737 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1738 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy,
1739 &uint64_t_val);
1740 assert(!ret);
1741 ok(uint64_t_val == 5,
1742 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1743 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy,
1744 &uint64_t_val);
1745 assert(!ret);
1746 ok(uint64_t_val == 4,
1747 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1748 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy,
1749 &uint64_t_val);
1750 assert(!ret);
1751 ok(uint64_t_val == 3,
1752 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1753 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy,
1754 &uint64_t_val);
1755 assert(!ret);
1756 ok(uint64_t_val == 2,
1757 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1758 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy,
1759 &uint64_t_val);
1760 assert(!ret);
1761 ok(uint64_t_val == 1,
1762 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1763
1764 /* validate a copy fields */
1765 str_val = bt_ctf_field_string_get_value(a_0_copy);
1766 ok(str_val && !strcmp(str_val, "a_0"),
1767 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1768 str_val = bt_ctf_field_string_get_value(a_1_copy);
1769 ok(str_val && !strcmp(str_val, "a_1"),
1770 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1771 str_val = bt_ctf_field_string_get_value(a_2_copy);
1772 ok(str_val && !strcmp(str_val, "a_2"),
1773 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1774 str_val = bt_ctf_field_string_get_value(a_3_copy);
1775 ok(str_val && !strcmp(str_val, "a_3"),
1776 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1777 str_val = bt_ctf_field_string_get_value(a_4_copy);
1778 ok(str_val && !strcmp(str_val, "a_4"),
1779 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1780
1781 /* put everything */
1782 bt_put(len_type);
1783 bt_put(fp_type);
1784 bt_put(s_type);
1785 bt_put(e_int_type);
1786 bt_put(e_type);
1787 bt_put(v_type);
1788 bt_put(v_label1_type);
1789 bt_put(v_label1_array_type);
1790 bt_put(v_label2_type);
1791 bt_put(v_label2_seq_type);
1792 bt_put(strct_type);
1793 bt_put(len);
1794 bt_put(fp);
1795 bt_put(s);
1796 bt_put(e_int);
1797 bt_put(e);
1798 bt_put(v);
1799 bt_put(v_selected);
1800 bt_put(v_selected_0);
1801 bt_put(v_selected_1);
1802 bt_put(v_selected_2);
1803 bt_put(v_selected_3);
1804 bt_put(v_selected_4);
1805 bt_put(v_selected_5);
1806 bt_put(v_selected_6);
1807 bt_put(a);
1808 bt_put(a_0);
1809 bt_put(a_1);
1810 bt_put(a_2);
1811 bt_put(a_3);
1812 bt_put(a_4);
1813 bt_put(strct);
1814 bt_put(len_copy);
1815 bt_put(fp_copy);
1816 bt_put(s_copy);
1817 bt_put(e_int_copy);
1818 bt_put(e_copy);
1819 bt_put(v_copy);
1820 bt_put(v_selected_copy);
1821 bt_put(v_selected_0_copy);
1822 bt_put(v_selected_1_copy);
1823 bt_put(v_selected_2_copy);
1824 bt_put(v_selected_3_copy);
1825 bt_put(v_selected_4_copy);
1826 bt_put(v_selected_5_copy);
1827 bt_put(v_selected_6_copy);
1828 bt_put(a_copy);
1829 bt_put(a_0_copy);
1830 bt_put(a_1_copy);
1831 bt_put(a_2_copy);
1832 bt_put(a_3_copy);
1833 bt_put(a_4_copy);
1834 bt_put(strct_copy);
1835 }
1836
1837 void type_field_tests()
1838 {
1839 struct bt_ctf_field *uint_12;
1840 struct bt_ctf_field *int_16;
1841 struct bt_ctf_field *string;
1842 struct bt_ctf_field *enumeration;
1843 struct bt_ctf_field_type *composite_structure_type;
1844 struct bt_ctf_field_type *structure_seq_type;
1845 struct bt_ctf_field_type *string_type;
1846 struct bt_ctf_field_type *sequence_type;
1847 struct bt_ctf_field_type *uint_8_type;
1848 struct bt_ctf_field_type *int_16_type;
1849 struct bt_ctf_field_type *uint_12_type =
1850 bt_ctf_field_type_integer_create(12);
1851 struct bt_ctf_field_type *enumeration_type;
1852 struct bt_ctf_field_type *enumeration_sequence_type;
1853 struct bt_ctf_field_type *enumeration_array_type;
1854 struct bt_ctf_field_type *returned_type;
1855 const char *ret_string;
1856
1857 returned_type = bt_ctf_field_get_type(NULL);
1858 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
1859
1860 ok(uint_12_type, "Create an unsigned integer type");
1861 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1862 BT_CTF_INTEGER_BASE_BINARY) == 0,
1863 "Set integer type's base as binary");
1864 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1865 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1866 "Set integer type's base as decimal");
1867 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1868 BT_CTF_INTEGER_BASE_UNKNOWN),
1869 "Reject integer type's base set as unknown");
1870 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1871 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1872 "Set integer type's base as octal");
1873 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1874 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1875 "Set integer type's base as hexadecimal");
1876 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1877 "Reject unknown integer base value");
1878 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1879 "Set integer type signedness to signed");
1880 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1881 "Set integer type signedness to unsigned");
1882 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1883 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1884 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1885 "bt_ctf_field_type_integer_get_size returns a correct value");
1886 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1887 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1888 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1889 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1890
1891 ok(bt_ctf_field_type_set_byte_order(NULL,
1892 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1893 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1894 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1895 (enum bt_ctf_byte_order) 42) < 0,
1896 "bt_ctf_field_type_set_byte_order rejects invalid values");
1897 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1898 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1899 "Set an integer's byte order to little endian");
1900 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1901 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1902 "Set an integer's byte order to big endian");
1903 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1904 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1905 "bt_ctf_field_type_get_byte_order returns a correct value");
1906 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1907 BT_CTF_BYTE_ORDER_UNKNOWN,
1908 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1909
1910 ok(bt_ctf_field_type_get_type_id(NULL) ==
1911 CTF_TYPE_UNKNOWN,
1912 "bt_ctf_field_type_get_type_id handles NULL correctly");
1913 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1914 CTF_TYPE_INTEGER,
1915 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1916
1917 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1918 BT_CTF_INTEGER_BASE_UNKNOWN,
1919 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1920 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1921 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1922 "bt_ctf_field_type_integer_get_base returns a correct value");
1923
1924 ok(bt_ctf_field_type_integer_set_encoding(NULL, CTF_STRING_ASCII) < 0,
1925 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1926 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1927 (enum ctf_string_encoding) 123) < 0,
1928 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1929 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1930 CTF_STRING_UTF8) == 0,
1931 "Set integer type encoding to UTF8");
1932 ok(bt_ctf_field_type_integer_get_encoding(NULL) == CTF_STRING_UNKNOWN,
1933 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1934 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == CTF_STRING_UTF8,
1935 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1936
1937 int_16_type = bt_ctf_field_type_integer_create(16);
1938 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
1939 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1940 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1941 uint_8_type = bt_ctf_field_type_integer_create(8);
1942 sequence_type =
1943 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1944 ok(sequence_type, "Create a sequence of int16_t type");
1945 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1946 CTF_TYPE_SEQUENCE,
1947 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1948
1949 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1950 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1951 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1952 sequence_type);
1953 ok(!strcmp(ret_string, "seq_len"),
1954 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1955 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1956 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1957 returned_type = bt_ctf_field_type_sequence_get_element_type(
1958 sequence_type);
1959 ok(returned_type == int_16_type,
1960 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1961 bt_put(returned_type);
1962
1963 string_type = bt_ctf_field_type_string_create();
1964 ok(string_type, "Create a string type");
1965 ok(bt_ctf_field_type_string_set_encoding(string_type,
1966 CTF_STRING_NONE),
1967 "Reject invalid \"None\" string encoding");
1968 ok(bt_ctf_field_type_string_set_encoding(string_type,
1969 42),
1970 "Reject invalid string encoding");
1971 ok(bt_ctf_field_type_string_set_encoding(string_type,
1972 CTF_STRING_ASCII) == 0,
1973 "Set string encoding to ASCII");
1974
1975 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1976 CTF_STRING_UNKNOWN,
1977 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1978 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1979 CTF_STRING_ASCII,
1980 "bt_ctf_field_type_string_get_encoding returns the correct value");
1981
1982 structure_seq_type = bt_ctf_field_type_structure_create();
1983 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1984 CTF_TYPE_STRUCT,
1985 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1986 ok(structure_seq_type, "Create a structure type");
1987 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1988 uint_8_type, "seq_len") == 0,
1989 "Add a uint8_t type to a structure");
1990 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1991 sequence_type, "a_sequence") == 0,
1992 "Add a sequence type to a structure");
1993
1994 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1995 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1996 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1997 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1998
1999 ok(bt_ctf_field_type_structure_get_field(NULL,
2000 &ret_string, &returned_type, 1) < 0,
2001 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2002 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2003 NULL, &returned_type, 1) == 0,
2004 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2005 bt_put(returned_type);
2006 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2007 &ret_string, NULL, 1) == 0,
2008 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2009 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2010 &ret_string, &returned_type, 10) < 0,
2011 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2012 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2013 &ret_string, &returned_type, 1) == 0,
2014 "bt_ctf_field_type_structure_get_field returns a field");
2015 ok(!strcmp(ret_string, "a_sequence"),
2016 "bt_ctf_field_type_structure_get_field returns a correct field name");
2017 ok(returned_type == sequence_type,
2018 "bt_ctf_field_type_structure_get_field returns a correct field type");
2019 bt_put(returned_type);
2020
2021 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
2022 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2023 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
2024 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2025 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2026 structure_seq_type, "a_sequence");
2027 ok(returned_type == sequence_type,
2028 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2029 bt_put(returned_type);
2030
2031 composite_structure_type = bt_ctf_field_type_structure_create();
2032 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2033 string_type, "a_string") == 0,
2034 "Add a string type to a structure");
2035 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2036 structure_seq_type, "inner_structure") == 0,
2037 "Add a structure type to a structure");
2038
2039 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2040 NULL, "a_sequence") == NULL,
2041 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2042 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2043 structure_seq_type, NULL) == NULL,
2044 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2045 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2046 structure_seq_type, "a_sequence");
2047 ok(returned_type == sequence_type,
2048 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2049 bt_put(returned_type);
2050
2051 int_16 = bt_ctf_field_create(int_16_type);
2052 ok(int_16, "Instanciate a signed 16-bit integer");
2053 uint_12 = bt_ctf_field_create(uint_12_type);
2054 ok(uint_12, "Instanciate an unsigned 12-bit integer");
2055 returned_type = bt_ctf_field_get_type(int_16);
2056 ok(returned_type == int_16_type,
2057 "bt_ctf_field_get_type returns the correct type");
2058
2059 /* Can't modify types after instanciating them */
2060 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
2061 BT_CTF_INTEGER_BASE_DECIMAL),
2062 "Check an integer type' base can't be modified after instanciation");
2063 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
2064 "Check an integer type's signedness can't be modified after instanciation");
2065
2066 /* Check signed property is checked */
2067 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
2068 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2069 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
2070 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2071
2072 /* Check overflows are properly tested for */
2073 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
2074 "Check -32768 is allowed for a signed 16-bit integer");
2075 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
2076 "Check 32767 is allowed for a signed 16-bit integer");
2077 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
2078 "Check 32768 is not allowed for a signed 16-bit integer");
2079 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
2080 "Check -32769 is not allowed for a signed 16-bit integer");
2081 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
2082 "Check -42 is allowed for a signed 16-bit integer");
2083
2084 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
2085 "Check 4095 is allowed for an unsigned 12-bit integer");
2086 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
2087 "Check 4096 is not allowed for a unsigned 12-bit integer");
2088 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
2089 "Check 0 is allowed for an unsigned 12-bit integer");
2090
2091 string = bt_ctf_field_create(string_type);
2092 ok(string, "Instanciate a string field");
2093 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
2094 "Set a string's value");
2095
2096 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
2097 ok(enumeration_type,
2098 "Create an enumeration type with an unsigned 12-bit integer as container");
2099 enumeration_sequence_type = bt_ctf_field_type_sequence_create(
2100 enumeration_type, "count");
2101 ok(!enumeration_sequence_type,
2102 "Check enumeration types are validated when creating a sequence");
2103 enumeration_array_type = bt_ctf_field_type_array_create(
2104 enumeration_type, 10);
2105 ok(!enumeration_array_type,
2106 "Check enumeration types are validated when creating an array");
2107 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2108 enumeration_type, "enumeration"),
2109 "Check enumeration types are validated when adding them as structure members");
2110 enumeration = bt_ctf_field_create(enumeration_type);
2111 ok(!enumeration,
2112 "Check enumeration types are validated before instantiation");
2113
2114 bt_put(string);
2115 bt_put(uint_12);
2116 bt_put(int_16);
2117 bt_put(enumeration);
2118 bt_put(composite_structure_type);
2119 bt_put(structure_seq_type);
2120 bt_put(string_type);
2121 bt_put(sequence_type);
2122 bt_put(uint_8_type);
2123 bt_put(int_16_type);
2124 bt_put(uint_12_type);
2125 bt_put(enumeration_type);
2126 bt_put(enumeration_sequence_type);
2127 bt_put(enumeration_array_type);
2128 bt_put(returned_type);
2129 }
2130
2131 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
2132 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
2133 {
2134 /*
2135 * Append enough events to force the underlying packet to be resized.
2136 * Also tests that a new event can be declared after a stream has been
2137 * instantiated and used/flushed.
2138 */
2139 int ret = 0;
2140 int i;
2141 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
2142 "Spammy_Event");
2143 struct bt_ctf_field_type *integer_type =
2144 bt_ctf_field_type_integer_create(17);
2145 struct bt_ctf_field_type *string_type =
2146 bt_ctf_field_type_string_create();
2147 struct bt_ctf_event *event = NULL;
2148 struct bt_ctf_field *ret_field = NULL;
2149 struct bt_ctf_field_type *ret_field_type = NULL;
2150 uint64_t ret_uint64;
2151 int events_appended = 0;
2152 struct bt_ctf_field *packet_context = NULL,
2153 *packet_context_field = NULL, *event_context = NULL;
2154
2155 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
2156 "field_1");
2157 ret |= bt_ctf_event_class_add_field(event_class, string_type,
2158 "a_string");
2159 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
2160 ok(ret == 0, "Add a new event class to a stream class after writing an event");
2161 if (ret) {
2162 goto end;
2163 }
2164
2165 event = bt_ctf_event_create(event_class);
2166 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
2167 ret_field_type = bt_ctf_field_get_type(ret_field);
2168 ok(ret_field_type == integer_type,
2169 "bt_ctf_event_get_payload_by_index returns a correct field");
2170 bt_put(ret_field_type);
2171 bt_put(ret_field);
2172
2173 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
2174 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2175 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
2176 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2177 bt_put(event);
2178
2179 ok(bt_ctf_stream_get_event_context(NULL) == NULL,
2180 "bt_ctf_stream_get_event_context handles NULL correctly");
2181 event_context = bt_ctf_stream_get_event_context(stream);
2182 ok(event_context,
2183 "bt_ctf_stream_get_event_context returns a stream event context");
2184 ok(bt_ctf_stream_set_event_context(NULL, event_context) < 0,
2185 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
2186 ok(bt_ctf_stream_set_event_context(stream, NULL) < 0,
2187 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
2188 ok(!bt_ctf_stream_set_event_context(stream, event_context),
2189 "bt_ctf_stream_set_event_context correctly set a stream event context");
2190 ret_field = bt_ctf_field_create(integer_type);
2191 ok(bt_ctf_stream_set_event_context(stream, ret_field) < 0,
2192 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
2193 bt_put(ret_field);
2194
2195 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
2196 event = bt_ctf_event_create(event_class);
2197 struct bt_ctf_field *integer =
2198 bt_ctf_field_create(integer_type);
2199 struct bt_ctf_field *string =
2200 bt_ctf_field_create(string_type);
2201
2202 ret |= bt_ctf_clock_set_time(clock, ++current_time);
2203 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
2204 ret |= bt_ctf_event_set_payload(event, "field_1",
2205 integer);
2206 bt_put(integer);
2207 ret |= bt_ctf_field_string_set_value(string, "This is a test");
2208 ret |= bt_ctf_event_set_payload(event, "a_string",
2209 string);
2210 bt_put(string);
2211
2212 /* Populate stream event context */
2213 integer = bt_ctf_field_structure_get_field(event_context,
2214 "common_event_context");
2215 ret |= bt_ctf_field_unsigned_integer_set_value(integer,
2216 i % 42);
2217 bt_put(integer);
2218
2219 ret |= bt_ctf_stream_append_event(stream, event);
2220 bt_put(event);
2221
2222 if (ret) {
2223 break;
2224 }
2225 }
2226
2227 events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH);
2228 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
2229 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2230 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
2231 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2232 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2233 ok(ret == 0 && ret_uint64 == 0,
2234 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2235 bt_ctf_stream_append_discarded_events(stream, 1000);
2236 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2237 ok(ret == 0 && ret_uint64 == 1000,
2238 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2239
2240 end:
2241 ok(events_appended, "Append 100 000 events to a stream");
2242
2243 /*
2244 * Populate the custom packet context field with a dummy value
2245 * otherwise flush will fail.
2246 */
2247 packet_context = bt_ctf_stream_get_packet_context(stream);
2248 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
2249 "custom_packet_context_field");
2250 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
2251
2252 ok(bt_ctf_stream_flush(stream) == 0,
2253 "Flush a stream that forces a packet resize");
2254 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2255 ok(ret == 0 && ret_uint64 == 1000,
2256 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2257 bt_put(integer_type);
2258 bt_put(string_type);
2259 bt_put(packet_context);
2260 bt_put(packet_context_field);
2261 bt_put(event_context);
2262 bt_put(event_class);
2263 }
2264
2265 void test_empty_stream(struct bt_ctf_writer *writer)
2266 {
2267 int ret = 0;
2268 struct bt_ctf_trace *trace = NULL, *ret_trace = NULL;
2269 struct bt_ctf_stream_class *stream_class = NULL;
2270 struct bt_ctf_stream *stream = NULL;
2271
2272 trace = bt_ctf_writer_get_trace(writer);
2273 if (!trace) {
2274 diag("Failed to get trace from writer");
2275 ret = -1;
2276 goto end;
2277 }
2278
2279 stream_class = bt_ctf_stream_class_create("empty_stream");
2280 if (!stream_class) {
2281 diag("Failed to create stream class");
2282 ret = -1;
2283 goto end;
2284 }
2285
2286 ok(bt_ctf_stream_class_get_trace(NULL) == NULL,
2287 "bt_ctf_stream_class_get_trace handles NULL correctly");
2288 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
2289 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2290
2291 stream = bt_ctf_writer_create_stream(writer, stream_class);
2292 if (!stream) {
2293 diag("Failed to create writer stream");
2294 ret = -1;
2295 goto end;
2296 }
2297
2298 ret_trace = bt_ctf_stream_class_get_trace(stream_class);
2299 ok(ret_trace == trace,
2300 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2301 end:
2302 ok(ret == 0,
2303 "Created a stream class with default attributes and an empty stream");
2304 bt_put(trace);
2305 bt_put(ret_trace);
2306 bt_put(stream);
2307 bt_put(stream_class);
2308 }
2309
2310 void test_custom_event_header_stream(struct bt_ctf_writer *writer)
2311 {
2312 int i, ret;
2313 struct bt_ctf_trace *trace = NULL;
2314 struct bt_ctf_clock *clock = NULL;
2315 struct bt_ctf_stream_class *stream_class = NULL;
2316 struct bt_ctf_stream *stream = NULL;
2317 struct bt_ctf_field_type *integer_type = NULL,
2318 *sequence_type = NULL, *event_header_type = NULL;
2319 struct bt_ctf_field *integer = NULL, *sequence = NULL,
2320 *event_header = NULL, *packet_header = NULL;
2321 struct bt_ctf_event_class *event_class = NULL;
2322 struct bt_ctf_event *event = NULL;
2323
2324 trace = bt_ctf_writer_get_trace(writer);
2325 if (!trace) {
2326 fail("Failed to get trace from writer");
2327 goto end;
2328 }
2329
2330 clock = bt_ctf_trace_get_clock(trace, 0);
2331 if (!clock) {
2332 fail("Failed to get clock from trace");
2333 goto end;
2334 }
2335
2336 stream_class = bt_ctf_stream_class_create("custom_event_header_stream");
2337 if (!stream_class) {
2338 fail("Failed to create stream class");
2339 goto end;
2340 }
2341
2342 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2343 if (ret) {
2344 fail("Failed to set stream class clock");
2345 goto end;
2346 }
2347
2348 /*
2349 * Customize event header to add an "seq_len" integer member
2350 * which will be used as the length of a sequence in an event of this
2351 * stream.
2352 */
2353 event_header_type = bt_ctf_stream_class_get_event_header_type(
2354 stream_class);
2355 if (!event_header_type) {
2356 fail("Failed to get event header type");
2357 goto end;
2358 }
2359
2360 integer_type = bt_ctf_field_type_integer_create(13);
2361 if (!integer_type) {
2362 fail("Failed to create length integer type");
2363 goto end;
2364 }
2365
2366 ret = bt_ctf_field_type_structure_add_field(event_header_type,
2367 integer_type, "seq_len");
2368 if (ret) {
2369 fail("Failed to add a new field to stream event header");
2370 goto end;
2371 }
2372
2373 event_class = bt_ctf_event_class_create("sequence_event");
2374 if (!event_class) {
2375 fail("Failed to create event class");
2376 goto end;
2377 }
2378
2379 /*
2380 * This event's payload will contain a sequence which references
2381 * stream.event.header.seq_len as its length field.
2382 */
2383 sequence_type = bt_ctf_field_type_sequence_create(integer_type,
2384 "stream.event.header.seq_len");
2385 if (!sequence_type) {
2386 fail("Failed to create a sequence");
2387 goto end;
2388 }
2389
2390 ret = bt_ctf_event_class_add_field(event_class, sequence_type,
2391 "some_sequence");
2392 if (ret) {
2393 fail("Failed to add a sequence to an event class");
2394 goto end;
2395 }
2396
2397 ret = bt_ctf_stream_class_add_event_class(stream_class, event_class);
2398 if (ret) {
2399 fail("Failed to add event class to stream class");
2400 goto end;
2401 }
2402
2403 stream = bt_ctf_writer_create_stream(writer, stream_class);
2404 if (!stream) {
2405 fail("Failed to create stream")
2406 goto end;
2407 }
2408
2409 /*
2410 * We have defined a custom packet header field. We have to populate it
2411 * explicitly.
2412 */
2413 packet_header = bt_ctf_stream_get_packet_header(stream);
2414 if (!packet_header) {
2415 fail("Failed to get stream packet header");
2416 goto end;
2417 }
2418
2419 integer = bt_ctf_field_structure_get_field(packet_header,
2420 "custom_trace_packet_header_field");
2421 if (!integer) {
2422 fail("Failed to retrieve custom_trace_packet_header_field");
2423 goto end;
2424 }
2425
2426 ret = bt_ctf_field_unsigned_integer_set_value(integer, 3487);
2427 if (ret) {
2428 fail("Failed to set custom_trace_packet_header_field value");
2429 goto end;
2430 }
2431 bt_put(integer);
2432
2433 event = bt_ctf_event_create(event_class);
2434 if (!event) {
2435 fail("Failed to create event");
2436 goto end;
2437 }
2438
2439 event_header = bt_ctf_event_get_header(event);
2440 if (!event_header) {
2441 fail("Failed to get event header");
2442 goto end;
2443 }
2444
2445 integer = bt_ctf_field_structure_get_field(event_header,
2446 "seq_len");
2447 if (!integer) {
2448 fail("Failed to get seq_len field from event header");
2449 goto end;
2450 }
2451
2452 ret = bt_ctf_field_unsigned_integer_set_value(integer, 2);
2453 if (ret) {
2454 fail("Failed to set seq_len value in event header");
2455 goto end;
2456 }
2457
2458 /* Populate both sequence integer fields */
2459 sequence = bt_ctf_event_get_payload(event, "some_sequence");
2460 if (!sequence) {
2461 fail("Failed to retrieve sequence from event");
2462 goto end;
2463 }
2464
2465 ret = bt_ctf_field_sequence_set_length(sequence, integer);
2466 if (ret) {
2467 fail("Failed to set sequence length");
2468 goto end;
2469 }
2470 bt_put(integer);
2471
2472 for (i = 0; i < 2; i++) {
2473 integer = bt_ctf_field_sequence_get_field(sequence, i);
2474 if (ret) {
2475 fail("Failed to retrieve sequence element");
2476 goto end;
2477 }
2478
2479 ret = bt_ctf_field_unsigned_integer_set_value(integer, i);
2480 if (ret) {
2481 fail("Failed to set sequence element value");
2482 goto end;
2483 }
2484
2485 bt_put(integer);
2486 integer = NULL;
2487 }
2488
2489 ret = bt_ctf_stream_append_event(stream, event);
2490 if (ret) {
2491 fail("Failed to append event to stream");
2492 goto end;
2493 }
2494
2495 ret = bt_ctf_stream_flush(stream);
2496 if (ret) {
2497 fail("Failed to flush custom_event_header stream");
2498 }
2499 end:
2500 bt_put(clock);
2501 bt_put(trace);
2502 bt_put(stream);
2503 bt_put(stream_class);
2504 bt_put(event_class);
2505 bt_put(event);
2506 bt_put(integer);
2507 bt_put(sequence);
2508 bt_put(event_header);
2509 bt_put(packet_header);
2510 bt_put(sequence_type);
2511 bt_put(integer_type);
2512 bt_put(event_header_type);
2513 }
2514
2515 void test_instanciate_event_before_stream(struct bt_ctf_writer *writer)
2516 {
2517 int ret = 0;
2518 struct bt_ctf_trace *trace = NULL;
2519 struct bt_ctf_clock *clock = NULL;
2520 struct bt_ctf_stream_class *stream_class = NULL;
2521 struct bt_ctf_stream *stream = NULL,
2522 *ret_stream = NULL;
2523 struct bt_ctf_event_class *event_class = NULL;
2524 struct bt_ctf_event *event = NULL;
2525 struct bt_ctf_field_type *integer_type = NULL;
2526 struct bt_ctf_field *integer = NULL;
2527
2528 trace = bt_ctf_writer_get_trace(writer);
2529 if (!trace) {
2530 diag("Failed to get trace from writer");
2531 ret = -1;
2532 goto end;
2533 }
2534
2535 clock = bt_ctf_trace_get_clock(trace, 0);
2536 if (!clock) {
2537 diag("Failed to get clock from trace");
2538 ret = -1;
2539 goto end;
2540 }
2541
2542 stream_class = bt_ctf_stream_class_create("event_before_stream_test");
2543 if (!stream_class) {
2544 diag("Failed to create stream class");
2545 ret = -1;
2546 goto end;
2547 }
2548
2549 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2550 if (ret) {
2551 diag("Failed to set stream class clock");
2552 goto end;
2553 }
2554
2555 event_class = bt_ctf_event_class_create("some_event_class_name");
2556 integer_type = bt_ctf_field_type_integer_create(32);
2557 if (!integer_type) {
2558 diag("Failed to create integer field type");
2559 ret = -1;
2560 goto end;
2561 }
2562
2563 ret = bt_ctf_event_class_add_field(event_class, integer_type,
2564 "integer_field");
2565 if (ret) {
2566 diag("Failed to add field to event class");
2567 goto end;
2568 }
2569
2570 ret = bt_ctf_stream_class_add_event_class(stream_class,
2571 event_class);
2572 if (ret) {
2573 diag("Failed to add event class to stream class");
2574 }
2575
2576 event = bt_ctf_event_create(event_class);
2577 if (!event) {
2578 diag("Failed to create event");
2579 ret = -1;
2580 goto end;
2581 }
2582
2583 integer = bt_ctf_event_get_payload_by_index(event, 0);
2584 if (!integer) {
2585 diag("Failed to get integer field payload from event");
2586 ret = -1;
2587 goto end;
2588 }
2589
2590 ret = bt_ctf_field_unsigned_integer_set_value(integer, 1234);
2591 if (ret) {
2592 diag("Failed to set integer field value");
2593 goto end;
2594 }
2595
2596 stream = bt_ctf_writer_create_stream(writer, stream_class);
2597 if (!stream) {
2598 diag("Failed to create writer stream");
2599 ret = -1;
2600 goto end;
2601 }
2602
2603 ok(bt_ctf_event_get_stream(NULL) == NULL,
2604 "bt_ctf_event_get_stream handles NULL correctly");
2605 ok(bt_ctf_event_get_stream(event) == NULL,
2606 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2607
2608 ret = bt_ctf_stream_append_event(stream, event);
2609 if (ret) {
2610 diag("Failed to append event to stream");
2611 goto end;
2612 }
2613
2614 ret_stream = bt_ctf_event_get_stream(event);
2615 ok(ret_stream == stream,
2616 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2617 end:
2618 ok(ret == 0,
2619 "Create an event before instanciating its associated stream");
2620 bt_put(trace);
2621 bt_put(stream);
2622 bt_put(ret_stream);
2623 bt_put(stream_class);
2624 bt_put(event_class);
2625 bt_put(event);
2626 bt_put(integer_type);
2627 bt_put(integer);
2628 bt_put(clock);
2629 }
2630
2631 void append_existing_event_class(struct bt_ctf_stream_class *stream_class)
2632 {
2633 struct bt_ctf_event_class *event_class;
2634
2635 event_class = bt_ctf_event_class_create("Simple Event");
2636 assert(event_class);
2637 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2638 "two event classes with the same name cannot cohabit within the same stream class");
2639 bt_put(event_class);
2640
2641 event_class = bt_ctf_event_class_create("different name, ok");
2642 assert(event_class);
2643 assert(!bt_ctf_event_class_set_id(event_class, 11));
2644 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2645 "two event classes with the same ID cannot cohabit within the same stream class");
2646 bt_put(event_class);
2647 }
2648
2649 int main(int argc, char **argv)
2650 {
2651 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2652 char metadata_path[sizeof(trace_path) + 9];
2653 const char *clock_name = "test_clock";
2654 const char *clock_description = "This is a test clock";
2655 const char *returned_clock_name;
2656 const char *returned_clock_description;
2657 const uint64_t frequency = 1123456789;
2658 const uint64_t offset_s = 1351530929945824323;
2659 const uint64_t offset = 1234567;
2660 const uint64_t precision = 10;
2661 const int is_absolute = 0xFF;
2662 char *metadata_string;
2663 struct bt_ctf_writer *writer;
2664 struct utsname name;
2665 char hostname[BABELTRACE_HOST_NAME_MAX];
2666 struct bt_ctf_clock *clock, *ret_clock;
2667 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
2668 struct bt_ctf_stream *stream1;
2669 const char *ret_string;
2670 const unsigned char *ret_uuid;
2671 unsigned char tmp_uuid[16] = { 0 };
2672 struct bt_ctf_field_type *packet_context_type,
2673 *packet_context_field_type,
2674 *packet_header_type,
2675 *packet_header_field_type,
2676 *integer_type,
2677 *stream_event_context_type,
2678 *ret_field_type,
2679 *event_header_field_type;
2680 struct bt_ctf_field *packet_header, *packet_header_field;
2681 struct bt_ctf_trace *trace;
2682 int ret;
2683 int64_t ret_int64_t;
2684 struct bt_value *obj;
2685
2686 if (argc < 3) {
2687 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2688 return -1;
2689 }
2690
2691 plan_no_plan();
2692
2693 if (!bt_mkdtemp(trace_path)) {
2694 perror("# perror");
2695 }
2696
2697 strcpy(metadata_path, trace_path);
2698 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
2699
2700 writer = bt_ctf_writer_create(trace_path);
2701 ok(writer, "bt_ctf_create succeeds in creating trace with path");
2702
2703 ok(!bt_ctf_writer_get_trace(NULL),
2704 "bt_ctf_writer_get_trace correctly handles NULL");
2705 trace = bt_ctf_writer_get_trace(writer);
2706 ok(trace,
2707 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2708 ok(bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
2709 "Set a trace's byte order to big endian");
2710 ok(bt_ctf_trace_get_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN,
2711 "bt_ctf_trace_get_byte_order returns a correct endianness");
2712
2713 /* Add environment context to the trace */
2714 ret = gethostname(hostname, sizeof(hostname));
2715 if (ret < 0) {
2716 return ret;
2717 }
2718 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
2719 "Add host (%s) environment field to writer instance",
2720 hostname);
2721 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
2722 "test_value"),
2723 "bt_ctf_writer_add_environment_field error with NULL writer");
2724 ok(bt_ctf_writer_add_environment_field(writer, NULL,
2725 "test_value"),
2726 "bt_ctf_writer_add_environment_field error with NULL field name");
2727 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
2728 NULL),
2729 "bt_ctf_writer_add_environment_field error with NULL field value");
2730
2731 /* Test bt_ctf_trace_set_environment_field with an integer object */
2732 obj = bt_value_integer_create_init(23);
2733 assert(obj);
2734 ok(bt_ctf_trace_set_environment_field(NULL, "test_env_int_obj", obj),
2735 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2736 ok(bt_ctf_trace_set_environment_field(trace, NULL, obj),
2737 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2738 ok(bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", NULL),
2739 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2740 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", obj),
2741 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2742 BT_PUT(obj);
2743
2744 /* Test bt_ctf_trace_set_environment_field with a string object */
2745 obj = bt_value_string_create_init("the value");
2746 assert(obj);
2747 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_str_obj", obj),
2748 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2749 BT_PUT(obj);
2750
2751 /* Test bt_ctf_trace_set_environment_field_integer */
2752 ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int",
2753 -194875),
2754 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2755 ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875),
2756 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2757 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
2758 -164973),
2759 "bt_ctf_trace_set_environment_field_integer succeeds");
2760
2761 /* Test bt_ctf_trace_set_environment_field_string */
2762 ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str",
2763 "yeah"),
2764 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2765 ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"),
2766 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2767 ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
2768 NULL),
2769 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2770 ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
2771 "oh yeah"),
2772 "bt_ctf_trace_set_environment_field_string succeeds");
2773
2774 /* Test bt_ctf_trace_get_environment_field_count */
2775 ok(bt_ctf_trace_get_environment_field_count(NULL) < 0,
2776 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2777 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
2778 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2779
2780 /* Test bt_ctf_trace_get_environment_field_name */
2781 ok(bt_ctf_trace_get_environment_field_name(NULL, 0) == NULL,
2782 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2783 ok(bt_ctf_trace_get_environment_field_name(trace, -1) == NULL,
2784 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2785 ok(bt_ctf_trace_get_environment_field_name(trace, 5) == NULL,
2786 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2787 ret_string = bt_ctf_trace_get_environment_field_name(trace, 0);
2788 ok(ret_string && !strcmp(ret_string, "host"),
2789 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2790 ret_string = bt_ctf_trace_get_environment_field_name(trace, 1);
2791 ok(ret_string && !strcmp(ret_string, "test_env_int_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, 2);
2794 ok(ret_string && !strcmp(ret_string, "test_env_str_obj"),
2795 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2796 ret_string = bt_ctf_trace_get_environment_field_name(trace, 3);
2797 ok(ret_string && !strcmp(ret_string, "test_env_int"),
2798 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2799 ret_string = bt_ctf_trace_get_environment_field_name(trace, 4);
2800 ok(ret_string && !strcmp(ret_string, "test_env_str"),
2801 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2802
2803 /* Test bt_ctf_trace_get_environment_field_value */
2804 ok(bt_ctf_trace_get_environment_field_value(NULL, 0) == NULL,
2805 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2806 ok(bt_ctf_trace_get_environment_field_value(trace, -1) == NULL,
2807 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2808 ok(bt_ctf_trace_get_environment_field_value(trace, 5) == NULL,
2809 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2810 obj = bt_ctf_trace_get_environment_field_value(trace, 1);
2811 ret = bt_value_integer_get(obj, &ret_int64_t);
2812 ok(!ret && ret_int64_t == 23,
2813 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2814 BT_PUT(obj);
2815 obj = bt_ctf_trace_get_environment_field_value(trace, 2);
2816 ret = bt_value_string_get(obj, &ret_string);
2817 ok(!ret && ret_string && !strcmp(ret_string, "the value"),
2818 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2819 BT_PUT(obj);
2820
2821 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2822 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL,
2823 "test_env_str"),
2824 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2825 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, NULL),
2826 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2827 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, "oh oh"),
2828 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2829 obj = bt_ctf_trace_get_environment_field_value_by_name(trace,
2830 "test_env_str");
2831 ret = bt_value_string_get(obj, &ret_string);
2832 ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"),
2833 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2834 BT_PUT(obj);
2835
2836 /* Test environment field replacement */
2837 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
2838 654321),
2839 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2840 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
2841 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2842 obj = bt_ctf_trace_get_environment_field_value(trace, 3);
2843 ret = bt_value_integer_get(obj, &ret_int64_t);
2844 ok(!ret && ret_int64_t == 654321,
2845 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2846 BT_PUT(obj);
2847
2848 /* On Solaris, uname() can return any positive value on success */
2849 if (uname(&name) < 0) {
2850 perror("uname");
2851 return -1;
2852 }
2853
2854 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
2855 == 0, "Add sysname (%s) environment field to writer instance",
2856 name.sysname);
2857 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
2858 name.nodename) == 0,
2859 "Add nodename (%s) environment field to writer instance",
2860 name.nodename);
2861 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
2862 == 0, "Add release (%s) environment field to writer instance",
2863 name.release);
2864 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
2865 == 0, "Add version (%s) environment field to writer instance",
2866 name.version);
2867 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
2868 == 0, "Add machine (%s) environment field to writer istance",
2869 name.machine);
2870
2871 /* Define a clock and add it to the trace */
2872 ok(bt_ctf_clock_create("signed") == NULL,
2873 "Illegal clock name rejected");
2874 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
2875 clock = bt_ctf_clock_create(clock_name);
2876 ok(clock, "Clock created sucessfully");
2877 returned_clock_name = bt_ctf_clock_get_name(clock);
2878 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
2879 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
2880 "Returned clock name is valid");
2881
2882 returned_clock_description = bt_ctf_clock_get_description(clock);
2883 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
2884 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
2885 "Clock description set successfully");
2886
2887 returned_clock_description = bt_ctf_clock_get_description(clock);
2888 ok(returned_clock_description,
2889 "bt_ctf_clock_get_description returns a description.");
2890 ok(returned_clock_description ?
2891 !strcmp(returned_clock_description, clock_description) : 0,
2892 "Returned clock description is valid");
2893
2894 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
2895 "bt_ctf_clock_get_frequency returns the correct default frequency");
2896 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
2897 "Set clock frequency");
2898 ok(bt_ctf_clock_get_frequency(clock) == frequency,
2899 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2900
2901 ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S,
2902 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2903 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
2904 "Set clock offset (seconds)");
2905 ok(bt_ctf_clock_get_offset_s(clock) == offset_s,
2906 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2907
2908 ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET,
2909 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
2910 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
2911 ok(bt_ctf_clock_get_offset(clock) == offset,
2912 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
2913
2914 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
2915 "bt_ctf_clock_get_precision returns the correct default precision");
2916 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
2917 "Set clock precision");
2918 ok(bt_ctf_clock_get_precision(clock) == precision,
2919 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2920
2921 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
2922 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2923 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
2924 "Set clock absolute property");
2925 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
2926 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2927
2928 ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME,
2929 "bt_ctf_clock_get_time returns the correct default time");
2930 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
2931 "Set clock time");
2932 ok(bt_ctf_clock_get_time(clock) == current_time,
2933 "bt_ctf_clock_get_time returns the correct time once it is set");
2934
2935 ok(bt_ctf_writer_add_clock(writer, clock) == 0,
2936 "Add clock to writer instance");
2937 ok(bt_ctf_writer_add_clock(writer, clock),
2938 "Verify a clock can't be added twice to a writer instance");
2939
2940 ok(bt_ctf_trace_get_clock_count(NULL) < 0,
2941 "bt_ctf_trace_get_clock_count correctly handles NULL");
2942 ok(bt_ctf_trace_get_clock_count(trace) == 1,
2943 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
2944 ok(!bt_ctf_trace_get_clock(NULL, 0),
2945 "bt_ctf_trace_get_clock correctly handles NULL");
2946 ok(!bt_ctf_trace_get_clock(trace, -1),
2947 "bt_ctf_trace_get_clock correctly handles negative indexes");
2948 ok(!bt_ctf_trace_get_clock(trace, 1),
2949 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
2950 ret_clock = bt_ctf_trace_get_clock(trace, 0);
2951 ok(ret_clock == clock,
2952 "bt_ctf_trace_get_clock returns the right clock instance");
2953 bt_put(ret_clock);
2954 ok(!bt_ctf_trace_get_clock_by_name(trace, NULL),
2955 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
2956 ok(!bt_ctf_trace_get_clock_by_name(NULL, clock_name),
2957 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
2958 ok(!bt_ctf_trace_get_clock_by_name(NULL, NULL),
2959 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
2960 ret_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name);
2961 ok(ret_clock == clock,
2962 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
2963 bt_put(ret_clock);
2964 ok(!bt_ctf_trace_get_clock_by_name(trace, "random"),
2965 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
2966
2967 ok(!bt_ctf_clock_get_name(NULL),
2968 "bt_ctf_clock_get_name correctly handles NULL");
2969 ok(!bt_ctf_clock_get_description(NULL),
2970 "bt_ctf_clock_get_description correctly handles NULL");
2971 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
2972 "bt_ctf_clock_get_frequency correctly handles NULL");
2973 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
2974 "bt_ctf_clock_get_precision correctly handles NULL");
2975 ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL,
2976 "bt_ctf_clock_get_offset_s correctly handles NULL");
2977 ok(bt_ctf_clock_get_offset(NULL) == -1ULL,
2978 "bt_ctf_clock_get_offset correctly handles NULL");
2979 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
2980 "bt_ctf_clock_get_is_absolute correctly handles NULL");
2981 ok(bt_ctf_clock_get_time(NULL) == -1ULL,
2982 "bt_ctf_clock_get_time correctly handles NULL");
2983
2984 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
2985 "bt_ctf_clock_set_description correctly handles NULL clock");
2986 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
2987 "bt_ctf_clock_set_frequency correctly handles NULL clock");
2988 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
2989 "bt_ctf_clock_get_precision correctly handles NULL clock");
2990 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
2991 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
2992 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
2993 "bt_ctf_clock_set_offset correctly handles NULL clock");
2994 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
2995 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
2996 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
2997 "bt_ctf_clock_set_time correctly handles NULL clock");
2998 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
2999 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3000 ret_uuid = bt_ctf_clock_get_uuid(clock);
3001 ok(ret_uuid,
3002 "bt_ctf_clock_get_uuid returns a UUID");
3003 if (ret_uuid) {
3004 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
3005 /* Slightly modify UUID */
3006 tmp_uuid[sizeof(tmp_uuid) - 1]++;
3007 }
3008
3009 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
3010 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3011 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
3012 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3013 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
3014 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3015 ret_uuid = bt_ctf_clock_get_uuid(clock);
3016 ok(ret_uuid,
3017 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3018 ok(uuid_match(ret_uuid, tmp_uuid),
3019 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3020
3021 /* Define a stream class */
3022 stream_class = bt_ctf_stream_class_create("test_stream");
3023
3024 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
3025 "bt_ctf_stream_class_get_name handles NULL correctly");
3026 ret_string = bt_ctf_stream_class_get_name(stream_class);
3027 ok(ret_string && !strcmp(ret_string, "test_stream"),
3028 "bt_ctf_stream_class_get_name returns a correct stream class name");
3029
3030 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
3031 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3032 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
3033 "bt_ctf_stream_class_get_clock handles NULL correctly");
3034
3035 ok(stream_class, "Create stream class");
3036 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
3037 "Set a stream class' clock");
3038 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
3039 ok(ret_clock == clock,
3040 "bt_ctf_stream_class_get_clock returns a correct clock");
3041 bt_put(ret_clock);
3042
3043 /* Test the event fields and event types APIs */
3044 type_field_tests();
3045
3046 /* Test fields copying */
3047 field_copy_tests();
3048
3049 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
3050 "bt_ctf_stream_class_get_id returns an error when no id is set");
3051 ok(bt_ctf_stream_class_get_id(NULL) < 0,
3052 "bt_ctf_stream_class_get_id handles NULL correctly");
3053 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
3054 "bt_ctf_stream_class_set_id handles NULL correctly");
3055 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
3056 "Set an stream class' id");
3057 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
3058 "bt_ctf_stream_class_get_id returns the correct value");
3059
3060 /* Validate default event header fields */
3061 ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL,
3062 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3063 ret_field_type = bt_ctf_stream_class_get_event_header_type(
3064 stream_class);
3065 ok(ret_field_type,
3066 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3067 ok(bt_ctf_field_type_get_type_id(ret_field_type) == CTF_TYPE_STRUCT,
3068 "Default event header type is a structure");
3069 event_header_field_type =
3070 bt_ctf_field_type_structure_get_field_type_by_name(
3071 ret_field_type, "id");
3072 ok(event_header_field_type,
3073 "Default event header type contains an \"id\" field");
3074 ok(bt_ctf_field_type_get_type_id(
3075 event_header_field_type) == CTF_TYPE_INTEGER,
3076 "Default event header \"id\" field is an integer");
3077 bt_put(event_header_field_type);
3078 event_header_field_type =
3079 bt_ctf_field_type_structure_get_field_type_by_name(
3080 ret_field_type, "timestamp");
3081 ok(event_header_field_type,
3082 "Default event header type contains a \"timestamp\" field");
3083 ok(bt_ctf_field_type_get_type_id(
3084 event_header_field_type) == CTF_TYPE_INTEGER,
3085 "Default event header \"timestamp\" field is an integer");
3086 bt_put(event_header_field_type);
3087 bt_put(ret_field_type);
3088
3089 /* Add a custom trace packet header field */
3090 ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
3091 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3092 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3093 ok(packet_header_type,
3094 "bt_ctf_trace_get_packet_header_type returns a packet header");
3095 ok(bt_ctf_field_type_get_type_id(packet_header_type) == CTF_TYPE_STRUCT,
3096 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3097 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3098 packet_header_type, "magic");
3099 ok(ret_field_type, "Default packet header type contains a \"magic\" 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, "uuid");
3103 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
3104 bt_put(ret_field_type);
3105 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3106 packet_header_type, "stream_id");
3107 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
3108 bt_put(ret_field_type);
3109
3110 packet_header_field_type = bt_ctf_field_type_integer_create(22);
3111 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
3112 packet_header_field_type, "custom_trace_packet_header_field"),
3113 "Added a custom trace packet header field successfully");
3114
3115 ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
3116 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3117 ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0,
3118 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3119 ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
3120 "Set a trace packet_header_type successfully");
3121
3122 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
3123 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3124
3125 /* Add a custom field to the stream class' packet context */
3126 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
3127 ok(packet_context_type,
3128 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3129 ok(bt_ctf_field_type_get_type_id(packet_context_type) == CTF_TYPE_STRUCT,
3130 "Packet context is a structure");
3131
3132 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
3133 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3134 ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL),
3135 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3136
3137 integer_type = bt_ctf_field_type_integer_create(32);
3138 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
3139 integer_type) < 0,
3140 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3141 /* Create a "uint5_t" equivalent custom packet context field */
3142 packet_context_field_type = bt_ctf_field_type_integer_create(5);
3143
3144 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3145 packet_context_field_type, "custom_packet_context_field");
3146 ok(ret == 0, "Packet context field added successfully");
3147
3148 /* Define a stream event context containing a my_integer field. */
3149 ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL,
3150 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3151 ok(bt_ctf_stream_class_get_event_context_type(
3152 stream_class) == NULL,
3153 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3154 stream_event_context_type = bt_ctf_field_type_structure_create();
3155 bt_ctf_field_type_structure_add_field(stream_event_context_type,
3156 integer_type, "common_event_context");
3157
3158 ok(bt_ctf_stream_class_set_event_context_type(NULL,
3159 stream_event_context_type) < 0,
3160 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3161 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3162 NULL) < 0,
3163 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3164 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3165 integer_type) < 0,
3166 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3167
3168 ok(bt_ctf_stream_class_set_event_context_type(
3169 stream_class, stream_event_context_type) == 0,
3170 "Set a new stream event context type");
3171 ret_field_type = bt_ctf_stream_class_get_event_context_type(
3172 stream_class);
3173 ok(ret_field_type == stream_event_context_type,
3174 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3175 bt_put(ret_field_type);
3176
3177 /* Instantiate a stream and append events */
3178 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
3179 ok(stream1, "Instanciate a stream class from writer");
3180
3181 ok(bt_ctf_stream_get_class(NULL) == NULL,
3182 "bt_ctf_stream_get_class correctly handles NULL");
3183 ret_stream_class = bt_ctf_stream_get_class(stream1);
3184 ok(ret_stream_class,
3185 "bt_ctf_stream_get_class returns a stream class");
3186 ok(ret_stream_class == stream_class,
3187 "Returned stream class is of the correct type");
3188
3189 /*
3190 * Try to modify the packet context type after a stream has been
3191 * created.
3192 */
3193 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
3194 packet_header_field_type, "should_fail");
3195 ok(ret < 0,
3196 "Trace packet header type can't be modified once a stream has been instanciated");
3197
3198 /*
3199 * Try to modify the packet context type after a stream has been
3200 * created.
3201 */
3202 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3203 packet_context_field_type, "should_fail");
3204 ok(ret < 0,
3205 "Packet context type can't be modified once a stream has been instanciated");
3206
3207 /*
3208 * Try to modify the stream event context type after a stream has been
3209 * created.
3210 */
3211 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
3212 integer_type, "should_fail");
3213 ok(ret < 0,
3214 "Stream event context type can't be modified once a stream has been instanciated");
3215
3216 /* Should fail after instanciating a stream (frozen) */
3217 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
3218 "Changes to a stream class that was already instantiated fail");
3219
3220 /* Populate the custom packet header field only once for all tests */
3221 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
3222 "bt_ctf_stream_get_packet_header handles NULL correctly");
3223 packet_header = bt_ctf_stream_get_packet_header(stream1);
3224 ok(packet_header,
3225 "bt_ctf_stream_get_packet_header returns a packet header");
3226 ret_field_type = bt_ctf_field_get_type(packet_header);
3227 ok(ret_field_type == packet_header_type,
3228 "Stream returns a packet header of the appropriate type");
3229 bt_put(ret_field_type);
3230 packet_header_field = bt_ctf_field_structure_get_field(packet_header,
3231 "custom_trace_packet_header_field");
3232 ok(packet_header_field,
3233 "Packet header structure contains a custom field with the appropriate name");
3234 ret_field_type = bt_ctf_field_get_type(packet_header_field);
3235 ok(ret_field_type == packet_header_field_type,
3236 "Custom packet header field is of the expected type");
3237 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
3238 54321), "Set custom packet header value successfully");
3239 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
3240 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3241 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
3242 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3243 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
3244 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3245 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
3246 "Successfully set a stream's packet header");
3247
3248 ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0,
3249 "Add environment field to writer after stream creation");
3250
3251 test_instanciate_event_before_stream(writer);
3252
3253 append_simple_event(stream_class, stream1, clock);
3254
3255 packet_resize_test(stream_class, stream1, clock);
3256
3257 append_complex_event(stream_class, stream1, clock);
3258
3259 append_existing_event_class(stream_class);
3260
3261 test_empty_stream(writer);
3262
3263 test_custom_event_header_stream(writer);
3264
3265 metadata_string = bt_ctf_writer_get_metadata_string(writer);
3266 ok(metadata_string, "Get metadata string");
3267
3268 bt_ctf_writer_flush_metadata(writer);
3269 validate_metadata(argv[1], metadata_path);
3270 validate_trace(argv[2], trace_path);
3271
3272 bt_put(clock);
3273 bt_put(ret_stream_class);
3274 bt_put(writer);
3275 bt_put(stream1);
3276 bt_put(packet_context_type);
3277 bt_put(packet_context_field_type);
3278 bt_put(integer_type);
3279 bt_put(stream_event_context_type);
3280 bt_put(ret_field_type);
3281 bt_put(packet_header_type);
3282 bt_put(packet_header_field_type);
3283 bt_put(packet_header);
3284 bt_put(packet_header_field);
3285 bt_put(trace);
3286 free(metadata_string);
3287
3288 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
3289 "bt_ctf_stream_class_get_trace returns NULL after its trace has been reclaimed");
3290 bt_put(stream_class);
3291
3292 /* Remove all trace files and delete temporary trace directory */
3293 DIR *trace_dir = opendir(trace_path);
3294 if (!trace_dir) {
3295 perror("# opendir");
3296 return -1;
3297 }
3298
3299 struct dirent *entry;
3300 while ((entry = readdir(trace_dir))) {
3301 struct stat st;
3302 char filename[PATH_MAX];
3303
3304 if (snprintf(filename, sizeof(filename), "%s/%s",
3305 trace_path, entry->d_name) <= 0) {
3306 continue;
3307 }
3308
3309 if (stat(entry->d_name, &st)) {
3310 continue;
3311 }
3312
3313 if (S_ISREG(st.st_mode)) {
3314 unlinkat(bt_dirfd(trace_dir), entry->d_name, 0);
3315 }
3316 }
3317
3318 rmdir(trace_path);
3319 closedir(trace_dir);
3320 return 0;
3321 }
This page took 0.167247 seconds and 5 git commands to generate.