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