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