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