Tests: Add tests for trace packet header 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 *event_context_type =
293 bt_ctf_field_type_structure_create();
294 struct bt_ctf_field_type *returned_type;
295 struct bt_ctf_event *simple_event;
296 struct bt_ctf_field *integer_field;
297 struct bt_ctf_field *float_field;
298 struct bt_ctf_field *enum_field;
299 struct bt_ctf_field *enum_field_unsigned;
300 struct bt_ctf_field *enum_container_field;
301 const char *mapping_name_test = "truie";
302 const double double_test_value = 3.1415;
303 struct bt_ctf_field *enum_container_field_unsigned;
304 const char *mapping_name_negative_test = "negative_value";
305 const char *ret_char;
306 double ret_double;
307 int64_t ret_range_start_int64_t, ret_range_end_int64_t;
308 uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
309 struct bt_ctf_clock *ret_clock;
310 struct bt_ctf_event_class *ret_event_class;
311 struct bt_ctf_field *packet_context;
312 struct bt_ctf_field *packet_context_field;
313 struct bt_ctf_field *stream_event_context;
314 struct bt_ctf_field *stream_event_context_field;
315 struct bt_ctf_field *event_context;
316 struct bt_ctf_field *event_context_field;
317
318 ok(uint_12_type, "Create an unsigned integer type");
319
320 bt_ctf_field_type_integer_set_signed(int_64_type, 1);
321 ok(int_64_type, "Create a signed integer type");
322 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
323
324 returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type);
325 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
326 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
327 ok(!bt_ctf_field_type_enumeration_create(enum_type),
328 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
329 bt_ctf_field_type_put(returned_type);
330
331 bt_ctf_field_type_set_alignment(float_type, 32);
332 ok(bt_ctf_field_type_get_alignment(NULL) < 0,
333 "bt_ctf_field_type_get_alignment handles NULL correctly");
334 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
335 "bt_ctf_field_type_get_alignment returns a correct value");
336
337 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
338 "Set a floating point type's exponent digit count");
339 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
340 "Set a floating point type's mantissa digit count");
341
342 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0,
343 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
344 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0,
345 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
346 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
347 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
348 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
349 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
350
351 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
352 mapping_name_negative_test, -12345, 0) == 0,
353 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
354 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
355 "escaping; \"test\"", 1, 1) == 0,
356 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
357 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
358 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
359 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
360 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
361 "event clock int float", 5, 22) == 0,
362 "Accept enumeration mapping strings containing reserved keywords");
363 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
364 42, 42);
365 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
366 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
367 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
368 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
369 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
370 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
371 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
372
373 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42) < 0,
374 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
375 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000) < 0,
376 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
377 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55) == 1,
378 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
379
380 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
381 "enum_field") == 0, "Add signed enumeration field to event");
382
383 ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char,
384 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
385 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
386 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL,
387 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
388 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
389 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
390 NULL, &ret_range_end_int64_t) < 0,
391 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
392 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
393 &ret_range_start_int64_t, NULL) < 0,
394 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
395 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char,
396 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
397 "bt_ctf_field_type_enumeration_get_mapping returns a value");
398 ok(!strcmp(ret_char, mapping_name_test),
399 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
400 ok(ret_range_start_int64_t == 42,
401 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
402 ok(ret_range_end_int64_t == 42,
403 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
404
405 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
406 "escaping; \"test\"", 0, 0) == 0,
407 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
408 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
409 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
410 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
411 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
412 "event clock int float", 5, 22) == 0,
413 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
414 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
415 42, 42);
416 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
417 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
418 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something",
419 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
420 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
421 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
422 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
423 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
424
425 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0,
426 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
427 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4,
428 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
429
430 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char,
431 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
432 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
433 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL,
434 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
435 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
436 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
437 NULL, &ret_range_end_uint64_t) < 0,
438 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
439 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
440 &ret_range_start_uint64_t, NULL) < 0,
441 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
442 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char,
443 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
444 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
445 ok(!strcmp(ret_char, mapping_name_test),
446 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
447 ok(ret_range_start_uint64_t == 42,
448 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
449 ok(ret_range_end_uint64_t == 42,
450 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
451
452 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
453 "integer_field");
454 bt_ctf_event_class_add_field(simple_event_class, float_type,
455 "float_field");
456 bt_ctf_stream_class_add_event_class(stream_class,
457 simple_event_class);
458
459 ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0,
460 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
461 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
462 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
463 ok(bt_ctf_stream_class_get_event_class(NULL, 0) == NULL,
464 "bt_ctf_stream_class_get_event_class handles NULL correctly");
465 ok(bt_ctf_stream_class_get_event_class(stream_class, 8724) == NULL,
466 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
467 ret_event_class = bt_ctf_stream_class_get_event_class(stream_class, 0);
468 ok(ret_event_class == simple_event_class,
469 "bt_ctf_stream_class_get_event_class returns the correct event class");
470 bt_ctf_event_class_put(ret_event_class);
471
472 ok(bt_ctf_stream_class_get_event_class_by_name(NULL, "some event name") == NULL,
473 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
474 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, NULL) == NULL,
475 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
476 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, "some event name") == NULL,
477 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
478 ret_event_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, "Simple Event");
479 ok(ret_event_class == simple_event_class,
480 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
481 bt_ctf_event_class_put(ret_event_class);
482
483 /* Set an event context type which will contain a single integer*/
484 ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
485 "event_specific_context"),
486 "Add event specific context field");
487 ok(bt_ctf_event_class_get_context_type(NULL) == NULL,
488 "bt_ctf_event_class_get_context_type handles NULL correctly");
489 ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL,
490 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
491 ok(bt_ctf_event_class_set_context_type(simple_event_class, NULL) < 0,
492 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
493 ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0,
494 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
495 ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type),
496 "Set an event class' context type successfully");
497 returned_type = bt_ctf_event_class_get_context_type(simple_event_class);
498 ok(returned_type == event_context_type,
499 "bt_ctf_event_class_get_context_type returns the appropriate type");
500 bt_ctf_field_type_put(returned_type);
501
502 simple_event = bt_ctf_event_create(simple_event_class);
503 ok(simple_event,
504 "Instantiate an event containing a single integer field");
505
506 ok(bt_ctf_event_get_clock(NULL) == NULL,
507 "bt_ctf_event_get_clock handles NULL correctly");
508 ret_clock = bt_ctf_event_get_clock(simple_event);
509 ok(ret_clock == clock,
510 "bt_ctf_event_get_clock returns a correct clock");
511 bt_ctf_clock_put(clock);
512
513 integer_field = bt_ctf_field_create(uint_12_type);
514 bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
515 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
516 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
517
518 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
519 ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double),
520 "bt_ctf_field_floating_point_get_value fails on an unset float field");
521 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
522 ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double),
523 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
524 ok(bt_ctf_field_floating_point_get_value(float_field, NULL),
525 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
526 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
527 "bt_ctf_field_floating_point_get_value returns a double value");
528 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
529 "bt_ctf_field_floating_point_get_value returns a correct value");
530
531 enum_field = bt_ctf_field_create(enum_type);
532 ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL);
533 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
534 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
535 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
536 enum_container_field = bt_ctf_field_enumeration_get_container(
537 enum_field);
538 ok(bt_ctf_field_signed_integer_set_value(
539 enum_container_field, -42) == 0,
540 "Set signed enumeration container value");
541 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
542 ok(!strcmp(ret_char, mapping_name_negative_test),
543 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
544 bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
545
546 enum_field_unsigned = bt_ctf_field_create(enum_type_unsigned);
547 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
548 enum_field_unsigned);
549 ok(bt_ctf_field_unsigned_integer_set_value(
550 enum_container_field_unsigned, 42) == 0,
551 "Set unsigned enumeration container value");
552 bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
553 enum_field_unsigned);
554 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned);
555 ok(!strcmp(ret_char, mapping_name_test),
556 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
557
558 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
559
560 /* Populate stream event context */
561 stream_event_context = bt_ctf_stream_get_event_context(stream);
562 stream_event_context_field = bt_ctf_field_structure_get_field(
563 stream_event_context, "common_event_context");
564 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field, 42);
565
566 /* Populate the event's context */
567 ok(bt_ctf_event_get_event_context(NULL) == NULL,
568 "bt_ctf_event_get_event_context handles NULL correctly");
569 event_context = bt_ctf_event_get_event_context(simple_event);
570 ok(event_context,
571 "bt_ctf_event_get_event_context returns a field");
572 returned_type = bt_ctf_field_get_type(event_context);
573 ok(returned_type == event_context_type,
574 "bt_ctf_event_get_event_context returns a field of the appropriate type");
575 event_context_field = bt_ctf_field_structure_get_field(event_context,
576 "event_specific_context");
577 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field, 1234),
578 "Successfully set an event context's value");
579 ok(bt_ctf_event_set_event_context(NULL, event_context) < 0,
580 "bt_ctf_event_set_event_context handles a NULL event correctly");
581 ok(bt_ctf_event_set_event_context(simple_event, NULL) < 0,
582 "bt_ctf_event_set_event_context handles a NULL event context correctly");
583 ok(bt_ctf_event_set_event_context(simple_event, event_context_field) < 0,
584 "bt_ctf_event_set_event_context rejects a context of the wrong type");
585 ok(!bt_ctf_event_set_event_context(simple_event, event_context),
586 "Set an event context successfully");
587
588 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
589 "Append simple event to trace stream");
590
591 ok(bt_ctf_stream_get_packet_context(NULL) == NULL,
592 "bt_ctf_stream_get_packet_context handles NULL correctly");
593 packet_context = bt_ctf_stream_get_packet_context(stream);
594 ok(packet_context,
595 "bt_ctf_stream_get_packet_context returns a packet context");
596
597 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
598 "packet_size");
599 ok(packet_context_field,
600 "Packet context contains the default packet_size field.");
601 bt_ctf_field_put(packet_context_field);
602 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
603 "custom_packet_context_field");
604 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field, 8) == 0,
605 "Custom packet context field value successfully set.");
606
607 ok(bt_ctf_stream_set_packet_context(NULL, packet_context_field) < 0,
608 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
609 ok(bt_ctf_stream_set_packet_context(stream, NULL) < 0,
610 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
611 ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0,
612 "Successfully set a stream's packet context");
613
614 ok(bt_ctf_stream_flush(stream) == 0,
615 "Flush trace stream with one event");
616
617 bt_ctf_event_class_put(simple_event_class);
618 bt_ctf_event_put(simple_event);
619 bt_ctf_field_type_put(uint_12_type);
620 bt_ctf_field_type_put(int_64_type);
621 bt_ctf_field_type_put(float_type);
622 bt_ctf_field_type_put(enum_type);
623 bt_ctf_field_type_put(enum_type_unsigned);
624 bt_ctf_field_type_put(returned_type);
625 bt_ctf_field_type_put(event_context_type);
626 bt_ctf_field_put(integer_field);
627 bt_ctf_field_put(float_field);
628 bt_ctf_field_put(enum_field);
629 bt_ctf_field_put(enum_field_unsigned);
630 bt_ctf_field_put(enum_container_field);
631 bt_ctf_field_put(enum_container_field_unsigned);
632 bt_ctf_field_put(packet_context);
633 bt_ctf_field_put(packet_context_field);
634 bt_ctf_field_put(stream_event_context);
635 bt_ctf_field_put(stream_event_context_field);
636 bt_ctf_field_put(event_context);
637 bt_ctf_field_put(event_context_field);
638 }
639
640 void append_complex_event(struct bt_ctf_stream_class *stream_class,
641 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
642 {
643 int i;
644 const char *complex_test_event_string = "Complex Test Event";
645 const char *test_string = "Test string";
646 struct bt_ctf_field_type *uint_35_type =
647 bt_ctf_field_type_integer_create(35);
648 struct bt_ctf_field_type *int_16_type =
649 bt_ctf_field_type_integer_create(16);
650 struct bt_ctf_field_type *uint_3_type =
651 bt_ctf_field_type_integer_create(3);
652 struct bt_ctf_field_type *enum_variant_type =
653 bt_ctf_field_type_enumeration_create(uint_3_type);
654 struct bt_ctf_field_type *variant_type =
655 bt_ctf_field_type_variant_create(enum_variant_type,
656 "variant_selector");
657 struct bt_ctf_field_type *string_type =
658 bt_ctf_field_type_string_create();
659 struct bt_ctf_field_type *sequence_type;
660 struct bt_ctf_field_type *array_type;
661 struct bt_ctf_field_type *inner_structure_type =
662 bt_ctf_field_type_structure_create();
663 struct bt_ctf_field_type *complex_structure_type =
664 bt_ctf_field_type_structure_create();
665 struct bt_ctf_field_type *ret_field_type;
666 struct bt_ctf_event_class *event_class;
667 struct bt_ctf_event *event;
668 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
669 *inner_structure_field, *complex_structure_field,
670 *a_sequence_field, *enum_variant_field, *enum_container_field,
671 *variant_field, *an_array_field, *ret_field;
672 uint64_t ret_unsigned_int;
673 int64_t ret_signed_int;
674 const char *ret_string;
675 struct bt_ctf_stream_class *ret_stream_class;
676 struct bt_ctf_event_class *ret_event_class;
677 struct bt_ctf_field *packet_context, *packet_context_field;
678
679 bt_ctf_field_type_set_alignment(int_16_type, 32);
680 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
681 bt_ctf_field_type_integer_set_base(uint_35_type,
682 BT_CTF_INTEGER_BASE_HEXADECIMAL);
683
684 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
685 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
686 "seq_len");
687
688 ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL,
689 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
690 ret_field_type = bt_ctf_field_type_array_get_element_type(
691 array_type);
692 ok(ret_field_type == int_16_type,
693 "bt_ctf_field_type_array_get_element_type returns the correct type");
694 bt_ctf_field_type_put(ret_field_type);
695
696 ok(bt_ctf_field_type_array_get_length(NULL) < 0,
697 "bt_ctf_field_type_array_get_length handles NULL correctly");
698 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
699 "bt_ctf_field_type_array_get_length returns the correct length");
700
701 bt_ctf_field_type_structure_add_field(inner_structure_type,
702 uint_35_type, "seq_len");
703 bt_ctf_field_type_structure_add_field(inner_structure_type,
704 sequence_type, "a_sequence");
705 bt_ctf_field_type_structure_add_field(inner_structure_type,
706 array_type, "an_array");
707
708 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
709 "UINT3_TYPE", 0, 0);
710 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
711 "INT16_TYPE", 1, 1);
712 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
713 "UINT35_TYPE", 2, 7);
714
715 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL,
716 "INT16_TYPE") < 0,
717 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
718 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
719 enum_variant_type, NULL) < 0,
720 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
721 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
722 enum_variant_type, "INT16_TYPE") == 1,
723 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
724
725 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1) < 0,
726 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
727 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42) < 0,
728 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
729 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5) == 2,
730 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
731
732 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
733 "An unknown entry"), "Reject a variant field based on an unknown tag value");
734 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
735 "UINT3_TYPE") == 0, "Add a field to a variant");
736 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
737 "INT16_TYPE");
738 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
739 "UINT35_TYPE");
740
741 ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL,
742 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
743 ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type);
744 ok(ret_field_type == enum_variant_type,
745 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
746 bt_ctf_field_type_put(ret_field_type);
747
748 ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL,
749 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
750 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
751 ok(!strcmp(ret_string, "variant_selector"),
752 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
753 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL,
754 "INT16_TYPE") == NULL,
755 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
756 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type,
757 NULL) == NULL,
758 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
759 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
760 variant_type, "INT16_TYPE");
761 ok(ret_field_type == int_16_type,
762 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
763 bt_ctf_field_type_put(ret_field_type);
764
765 ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0,
766 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
767 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
768 "bt_ctf_field_type_variant_get_field_count returns the correct count");
769
770 ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0,
771 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
772 ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) < 0,
773 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
774 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) < 0,
775 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
776 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0,
777 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
778 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0,
779 "bt_ctf_field_type_variant_get_field returns a field");
780 ok(!strcmp("INT16_TYPE", ret_string),
781 "bt_ctf_field_type_variant_get_field returns a correct field name");
782 ok(ret_field_type == int_16_type,
783 "bt_ctf_field_type_variant_get_field returns a correct field type");
784 bt_ctf_field_type_put(ret_field_type);
785
786 bt_ctf_field_type_structure_add_field(complex_structure_type,
787 enum_variant_type, "variant_selector");
788 bt_ctf_field_type_structure_add_field(complex_structure_type,
789 string_type, "a_string");
790 bt_ctf_field_type_structure_add_field(complex_structure_type,
791 variant_type, "variant_value");
792 bt_ctf_field_type_structure_add_field(complex_structure_type,
793 inner_structure_type, "inner_structure");
794
795 ok(bt_ctf_event_class_create("clock") == NULL,
796 "Reject creation of an event class with an illegal name");
797 event_class = bt_ctf_event_class_create(complex_test_event_string);
798 ok(event_class, "Create an event class");
799 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
800 "Reject addition of a field with an empty name to an event");
801 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
802 "Reject addition of a field with a NULL type to an event");
803 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
804 "int"),
805 "Reject addition of a type with an illegal name to an event");
806 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
807 "uint_35") == 0,
808 "Add field of type unsigned integer to an event");
809 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
810 "int_16") == 0, "Add field of type signed integer to an event");
811 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
812 "complex_structure") == 0,
813 "Add composite structure to an event");
814
815 ok(bt_ctf_event_class_get_name(NULL) == NULL,
816 "bt_ctf_event_class_get_name handles NULL correctly");
817 ret_string = bt_ctf_event_class_get_name(event_class);
818 ok(!strcmp(ret_string, complex_test_event_string),
819 "bt_ctf_event_class_get_name returns a correct name");
820 ok(bt_ctf_event_class_get_id(event_class) < 0,
821 "bt_ctf_event_class_get_id returns a negative value when not set");
822 ok(bt_ctf_event_class_get_id(NULL) < 0,
823 "bt_ctf_event_class_get_id handles NULL correctly");
824 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
825 "bt_ctf_event_class_set_id handles NULL correctly");
826 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
827 "Set an event class' id");
828 ok(bt_ctf_event_class_get_id(event_class) == 42,
829 "bt_ctf_event_class_get_id returns the correct value");
830
831 /* Add event class to the stream class */
832 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
833 "Reject addition of NULL event class to a stream class");
834 ok(bt_ctf_stream_class_add_event_class(stream_class,
835 event_class) == 0, "Add an event class to stream class");
836
837 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
838 "bt_ctf_event_class_get_stream_class handles NULL correctly");
839 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
840 ok(ret_stream_class == stream_class,
841 "bt_ctf_event_class_get_stream_class returns the correct stream class");
842 bt_ctf_stream_class_put(ret_stream_class);
843
844 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
845 "bt_ctf_event_class_get_field_count handles NULL correctly");
846 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
847 "bt_ctf_event_class_get_field_count returns a correct value");
848
849 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
850 &ret_field_type, 0) < 0,
851 "bt_ctf_event_class_get_field handles a NULL event class correctly");
852 ok(bt_ctf_event_class_get_field(event_class, NULL,
853 &ret_field_type, 0) < 0,
854 "bt_ctf_event_class_get_field handles a NULL field name correctly");
855 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
856 NULL, 0) < 0,
857 "bt_ctf_event_class_get_field handles a NULL field type correctly");
858 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
859 &ret_field_type, 42) < 0,
860 "bt_ctf_event_class_get_field handles an invalid index correctly");
861 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
862 &ret_field_type, 0) == 0,
863 "bt_ctf_event_class_get_field returns a field");
864 ok(ret_field_type == uint_35_type,
865 "bt_ctf_event_class_get_field returns a correct field type");
866 bt_ctf_field_type_put(ret_field_type);
867 ok(!strcmp(ret_string, "uint_35"),
868 "bt_ctf_event_class_get_field returns a correct field name");
869 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
870 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
871 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
872 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
873 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
874 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
875 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
876 "complex_structure");
877 ok(ret_field_type == complex_structure_type,
878 "bt_ctf_event_class_get_field_by_name returns a correct field type");
879 bt_ctf_field_type_put(ret_field_type);
880
881 event = bt_ctf_event_create(event_class);
882 ok(event, "Instanciate a complex event");
883
884 ok(bt_ctf_event_get_class(NULL) == NULL,
885 "bt_ctf_event_get_class handles NULL correctly");
886 ret_event_class = bt_ctf_event_get_class(event);
887 ok(ret_event_class == event_class,
888 "bt_ctf_event_get_class returns the correct event class");
889 bt_ctf_event_class_put(ret_event_class);
890
891 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
892 if (!uint_35_field) {
893 printf("uint_35_field is NULL\n");
894 }
895
896 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
897 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
898 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
899 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
900 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
901 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
902 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
903 &ret_unsigned_int) == 0,
904 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
905 ok(ret_unsigned_int == 0x0DDF00D,
906 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
907 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
908 &ret_signed_int) < 0,
909 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
910 bt_ctf_field_put(uint_35_field);
911
912 int_16_field = bt_ctf_event_get_payload(event, "int_16");
913 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
914 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
915 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
916 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
917 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
918 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
919 &ret_signed_int) == 0,
920 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
921 ok(ret_signed_int == -12345,
922 "bt_ctf_field_signed_integer_get_value returns the correct value");
923 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
924 &ret_unsigned_int) < 0,
925 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
926 bt_ctf_field_put(int_16_field);
927
928 complex_structure_field = bt_ctf_event_get_payload(event,
929 "complex_structure");
930
931 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
932 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
933 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
934 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
935 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
936 complex_structure_field, 3);
937 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
938 bt_ctf_field_put(inner_structure_field);
939 ok(ret_field_type == inner_structure_type,
940 "bt_ctf_field_structure_get_field_by_index returns a correct field");
941 bt_ctf_field_type_put(ret_field_type);
942
943 inner_structure_field = bt_ctf_field_structure_get_field(
944 complex_structure_field, "inner_structure");
945 a_string_field = bt_ctf_field_structure_get_field(
946 complex_structure_field, "a_string");
947 enum_variant_field = bt_ctf_field_structure_get_field(
948 complex_structure_field, "variant_selector");
949 variant_field = bt_ctf_field_structure_get_field(
950 complex_structure_field, "variant_value");
951 uint_35_field = bt_ctf_field_structure_get_field(
952 inner_structure_field, "seq_len");
953 a_sequence_field = bt_ctf_field_structure_get_field(
954 inner_structure_field, "a_sequence");
955 an_array_field = bt_ctf_field_structure_get_field(
956 inner_structure_field, "an_array");
957
958 enum_container_field = bt_ctf_field_enumeration_get_container(
959 enum_variant_field);
960 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
961 int_16_field = bt_ctf_field_variant_get_field(variant_field,
962 enum_variant_field);
963 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
964 bt_ctf_field_put(int_16_field);
965 ok(!bt_ctf_field_string_get_value(a_string_field),
966 "bt_ctf_field_string_get_value returns NULL on an unset field");
967 bt_ctf_field_string_set_value(a_string_field,
968 test_string);
969 ok(!bt_ctf_field_string_get_value(NULL),
970 "bt_ctf_field_string_get_value correctly handles NULL");
971 ret_string = bt_ctf_field_string_get_value(a_string_field);
972 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
973 ok(ret_string ? !strcmp(ret_string, test_string) : 0,
974 "bt_ctf_field_string_get_value returns a correct value");
975 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
976 SEQUENCE_TEST_LENGTH);
977
978 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
979 enum_container_field) == NULL,
980 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
981 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
982 NULL) == NULL,
983 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
984 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
985 variant_type, enum_variant_field);
986 ok(ret_field_type == int_16_type,
987 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
988
989 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
990 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
991 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
992 uint_35_field) == 0, "Set a sequence field's length");
993 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
994 ok(ret_field == uint_35_field,
995 "bt_ctf_field_sequence_get_length returns the correct length field");
996 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
997 "bt_ctf_field_sequence_get_length properly handles NULL");
998
999 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
1000 int_16_field = bt_ctf_field_sequence_get_field(
1001 a_sequence_field, i);
1002 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
1003 bt_ctf_field_put(int_16_field);
1004 }
1005
1006 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
1007 int_16_field = bt_ctf_field_array_get_field(
1008 an_array_field, i);
1009 bt_ctf_field_signed_integer_set_value(int_16_field, i);
1010 bt_ctf_field_put(int_16_field);
1011 }
1012
1013 bt_ctf_clock_set_time(clock, ++current_time);
1014 ok(bt_ctf_stream_append_event(stream, event) == 0,
1015 "Append a complex event to a stream");
1016
1017 /*
1018 * Populate the custom packet context field with a dummy value
1019 * otherwise flush will fail.
1020 */
1021 packet_context = bt_ctf_stream_get_packet_context(stream);
1022 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1023 "custom_packet_context_field");
1024 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
1025
1026 ok(bt_ctf_stream_flush(stream) == 0,
1027 "Flush a stream containing a complex event");
1028
1029 bt_ctf_field_put(uint_35_field);
1030 bt_ctf_field_put(a_string_field);
1031 bt_ctf_field_put(inner_structure_field);
1032 bt_ctf_field_put(complex_structure_field);
1033 bt_ctf_field_put(a_sequence_field);
1034 bt_ctf_field_put(an_array_field);
1035 bt_ctf_field_put(enum_variant_field);
1036 bt_ctf_field_put(enum_container_field);
1037 bt_ctf_field_put(variant_field);
1038 bt_ctf_field_put(ret_field);
1039 bt_ctf_field_put(packet_context_field);
1040 bt_ctf_field_put(packet_context);
1041 bt_ctf_field_type_put(uint_35_type);
1042 bt_ctf_field_type_put(int_16_type);
1043 bt_ctf_field_type_put(string_type);
1044 bt_ctf_field_type_put(sequence_type);
1045 bt_ctf_field_type_put(array_type);
1046 bt_ctf_field_type_put(inner_structure_type);
1047 bt_ctf_field_type_put(complex_structure_type);
1048 bt_ctf_field_type_put(uint_3_type);
1049 bt_ctf_field_type_put(enum_variant_type);
1050 bt_ctf_field_type_put(variant_type);
1051 bt_ctf_field_type_put(ret_field_type);
1052 bt_ctf_event_class_put(event_class);
1053 bt_ctf_event_put(event);
1054 }
1055
1056 void type_field_tests()
1057 {
1058 struct bt_ctf_field *uint_12;
1059 struct bt_ctf_field *int_16;
1060 struct bt_ctf_field *string;
1061 struct bt_ctf_field *enumeration;
1062 struct bt_ctf_field_type *composite_structure_type;
1063 struct bt_ctf_field_type *structure_seq_type;
1064 struct bt_ctf_field_type *string_type;
1065 struct bt_ctf_field_type *sequence_type;
1066 struct bt_ctf_field_type *uint_8_type;
1067 struct bt_ctf_field_type *int_16_type;
1068 struct bt_ctf_field_type *uint_12_type =
1069 bt_ctf_field_type_integer_create(12);
1070 struct bt_ctf_field_type *enumeration_type;
1071 struct bt_ctf_field_type *enumeration_sequence_type;
1072 struct bt_ctf_field_type *enumeration_array_type;
1073 struct bt_ctf_field_type *returned_type;
1074 const char *ret_string;
1075
1076 returned_type = bt_ctf_field_get_type(NULL);
1077 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
1078
1079 ok(uint_12_type, "Create an unsigned integer type");
1080 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1081 BT_CTF_INTEGER_BASE_BINARY) == 0,
1082 "Set integer type's base as binary");
1083 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1084 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1085 "Set integer type's base as decimal");
1086 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1087 BT_CTF_INTEGER_BASE_UNKNOWN),
1088 "Reject integer type's base set as unknown");
1089 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1090 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1091 "Set integer type's base as octal");
1092 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1093 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1094 "Set integer type's base as hexadecimal");
1095 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1096 "Reject unknown integer base value");
1097 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1098 "Set integer type signedness to signed");
1099 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1100 "Set integer type signedness to unsigned");
1101 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1102 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1103 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1104 "bt_ctf_field_type_integer_get_size returns a correct value");
1105 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1106 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1107 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1108 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1109
1110 ok(bt_ctf_field_type_set_byte_order(NULL,
1111 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1112 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1113 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1114 (enum bt_ctf_byte_order) 42) < 0,
1115 "bt_ctf_field_type_set_byte_order rejects invalid values");
1116 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1117 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1118 "Set an integer's byte order to little endian");
1119 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1120 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1121 "Set an integer's byte order to big endian");
1122 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1123 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1124 "bt_ctf_field_type_get_byte_order returns a correct value");
1125 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1126 BT_CTF_BYTE_ORDER_UNKNOWN,
1127 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1128
1129 ok(bt_ctf_field_type_get_type_id(NULL) ==
1130 CTF_TYPE_UNKNOWN,
1131 "bt_ctf_field_type_get_type_id handles NULL correctly");
1132 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1133 CTF_TYPE_INTEGER,
1134 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1135
1136 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1137 BT_CTF_INTEGER_BASE_UNKNOWN,
1138 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1139 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1140 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1141 "bt_ctf_field_type_integer_get_base returns a correct value");
1142
1143 ok(bt_ctf_field_type_integer_set_encoding(NULL, CTF_STRING_ASCII) < 0,
1144 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1145 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1146 (enum ctf_string_encoding) 123) < 0,
1147 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1148 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1149 CTF_STRING_UTF8) == 0,
1150 "Set integer type encoding to UTF8");
1151 ok(bt_ctf_field_type_integer_get_encoding(NULL) == CTF_STRING_UNKNOWN,
1152 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1153 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == CTF_STRING_UTF8,
1154 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1155
1156 int_16_type = bt_ctf_field_type_integer_create(16);
1157 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
1158 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1159 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1160 uint_8_type = bt_ctf_field_type_integer_create(8);
1161 sequence_type =
1162 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1163 ok(sequence_type, "Create a sequence of int16_t type");
1164 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1165 CTF_TYPE_SEQUENCE,
1166 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1167
1168 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1169 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1170 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1171 sequence_type);
1172 ok(!strcmp(ret_string, "seq_len"),
1173 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1174 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1175 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1176 returned_type = bt_ctf_field_type_sequence_get_element_type(
1177 sequence_type);
1178 ok(returned_type == int_16_type,
1179 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1180 bt_ctf_field_type_put(returned_type);
1181
1182 string_type = bt_ctf_field_type_string_create();
1183 ok(string_type, "Create a string type");
1184 ok(bt_ctf_field_type_string_set_encoding(string_type,
1185 CTF_STRING_NONE),
1186 "Reject invalid \"None\" string encoding");
1187 ok(bt_ctf_field_type_string_set_encoding(string_type,
1188 42),
1189 "Reject invalid string encoding");
1190 ok(bt_ctf_field_type_string_set_encoding(string_type,
1191 CTF_STRING_ASCII) == 0,
1192 "Set string encoding to ASCII");
1193
1194 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1195 CTF_STRING_UNKNOWN,
1196 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1197 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1198 CTF_STRING_ASCII,
1199 "bt_ctf_field_type_string_get_encoding returns the correct value");
1200
1201 structure_seq_type = bt_ctf_field_type_structure_create();
1202 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1203 CTF_TYPE_STRUCT,
1204 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1205 ok(structure_seq_type, "Create a structure type");
1206 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1207 uint_8_type, "seq_len") == 0,
1208 "Add a uint8_t type to a structure");
1209 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1210 sequence_type, "a_sequence") == 0,
1211 "Add a sequence type to a structure");
1212
1213 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1214 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1215 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1216 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1217
1218 ok(bt_ctf_field_type_structure_get_field(NULL,
1219 &ret_string, &returned_type, 1) < 0,
1220 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1221 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1222 NULL, &returned_type, 1) < 0,
1223 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1224 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1225 &ret_string, NULL, 1) < 0,
1226 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1227 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1228 &ret_string, &returned_type, 10) < 0,
1229 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1230 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1231 &ret_string, &returned_type, 1) == 0,
1232 "bt_ctf_field_type_structure_get_field returns a field");
1233 ok(!strcmp(ret_string, "a_sequence"),
1234 "bt_ctf_field_type_structure_get_field returns a correct field name");
1235 ok(returned_type == sequence_type,
1236 "bt_ctf_field_type_structure_get_field returns a correct field type");
1237 bt_ctf_field_type_put(returned_type);
1238
1239 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
1240 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1241 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
1242 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1243 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1244 structure_seq_type, "a_sequence");
1245 ok(returned_type == sequence_type,
1246 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1247 bt_ctf_field_type_put(returned_type);
1248
1249 composite_structure_type = bt_ctf_field_type_structure_create();
1250 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1251 string_type, "a_string") == 0,
1252 "Add a string type to a structure");
1253 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1254 structure_seq_type, "inner_structure") == 0,
1255 "Add a structure type to a structure");
1256
1257 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1258 NULL, "a_sequence") == NULL,
1259 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1260 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1261 structure_seq_type, NULL) == NULL,
1262 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1263 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1264 structure_seq_type, "a_sequence");
1265 ok(returned_type == sequence_type,
1266 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1267 bt_ctf_field_type_put(returned_type);
1268
1269 int_16 = bt_ctf_field_create(int_16_type);
1270 ok(int_16, "Instanciate a signed 16-bit integer");
1271 uint_12 = bt_ctf_field_create(uint_12_type);
1272 ok(uint_12, "Instanciate an unsigned 12-bit integer");
1273 returned_type = bt_ctf_field_get_type(int_16);
1274 ok(returned_type == int_16_type,
1275 "bt_ctf_field_get_type returns the correct type");
1276
1277 /* Can't modify types after instanciating them */
1278 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1279 BT_CTF_INTEGER_BASE_DECIMAL),
1280 "Check an integer type' base can't be modified after instanciation");
1281 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
1282 "Check an integer type's signedness can't be modified after instanciation");
1283
1284 /* Check signed property is checked */
1285 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
1286 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1287 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
1288 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1289
1290 /* Check overflows are properly tested for */
1291 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
1292 "Check -32768 is allowed for a signed 16-bit integer");
1293 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
1294 "Check 32767 is allowed for a signed 16-bit integer");
1295 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
1296 "Check 32768 is not allowed for a signed 16-bit integer");
1297 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
1298 "Check -32769 is not allowed for a signed 16-bit integer");
1299 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
1300 "Check -42 is allowed for a signed 16-bit integer");
1301
1302 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
1303 "Check 4095 is allowed for an unsigned 12-bit integer");
1304 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
1305 "Check 4096 is not allowed for a unsigned 12-bit integer");
1306 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
1307 "Check 0 is allowed for an unsigned 12-bit integer");
1308
1309 string = bt_ctf_field_create(string_type);
1310 ok(string, "Instanciate a string field");
1311 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
1312 "Set a string's value");
1313
1314 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
1315 ok(enumeration_type,
1316 "Create an enumeration type with an unsigned 12-bit integer as container");
1317 enumeration_sequence_type = bt_ctf_field_type_sequence_create(
1318 enumeration_type, "count");
1319 ok(!enumeration_sequence_type,
1320 "Check enumeration types are validated when creating a sequence");
1321 enumeration_array_type = bt_ctf_field_type_array_create(
1322 enumeration_type, 10);
1323 ok(!enumeration_array_type,
1324 "Check enumeration types are validated when creating an array");
1325 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1326 enumeration_type, "enumeration"),
1327 "Check enumeration types are validated when adding them as structure members");
1328 enumeration = bt_ctf_field_create(enumeration_type);
1329 ok(!enumeration,
1330 "Check enumeration types are validated before instantiation");
1331
1332 bt_ctf_field_put(string);
1333 bt_ctf_field_put(uint_12);
1334 bt_ctf_field_put(int_16);
1335 bt_ctf_field_put(enumeration);
1336 bt_ctf_field_type_put(composite_structure_type);
1337 bt_ctf_field_type_put(structure_seq_type);
1338 bt_ctf_field_type_put(string_type);
1339 bt_ctf_field_type_put(sequence_type);
1340 bt_ctf_field_type_put(uint_8_type);
1341 bt_ctf_field_type_put(int_16_type);
1342 bt_ctf_field_type_put(uint_12_type);
1343 bt_ctf_field_type_put(enumeration_type);
1344 bt_ctf_field_type_put(enumeration_sequence_type);
1345 bt_ctf_field_type_put(enumeration_array_type);
1346 bt_ctf_field_type_put(returned_type);
1347 }
1348
1349 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
1350 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
1351 {
1352 /*
1353 * Append enough events to force the underlying packet to be resized.
1354 * Also tests that a new event can be declared after a stream has been
1355 * instantiated and used/flushed.
1356 */
1357 int ret = 0;
1358 int i;
1359 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
1360 "Spammy_Event");
1361 struct bt_ctf_field_type *integer_type =
1362 bt_ctf_field_type_integer_create(17);
1363 struct bt_ctf_field_type *string_type =
1364 bt_ctf_field_type_string_create();
1365 struct bt_ctf_event *event = NULL;
1366 struct bt_ctf_field *ret_field = NULL;
1367 struct bt_ctf_field_type *ret_field_type = NULL;
1368 uint64_t ret_uint64;
1369 int events_appended = 0;
1370 struct bt_ctf_field *packet_context = NULL,
1371 *packet_context_field = NULL, *event_context = NULL;
1372
1373 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
1374 "field_1");
1375 ret |= bt_ctf_event_class_add_field(event_class, string_type,
1376 "a_string");
1377 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
1378 ok(ret == 0, "Add a new event class to a stream class after writing an event");
1379 if (ret) {
1380 goto end;
1381 }
1382
1383 event = bt_ctf_event_create(event_class);
1384 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
1385 ret_field_type = bt_ctf_field_get_type(ret_field);
1386 ok(ret_field_type == integer_type,
1387 "bt_ctf_event_get_payload_by_index returns a correct field");
1388 bt_ctf_field_type_put(ret_field_type);
1389 bt_ctf_field_put(ret_field);
1390
1391 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
1392 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1393 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
1394 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1395 bt_ctf_event_put(event);
1396
1397 ok(bt_ctf_stream_get_event_context(NULL) == NULL,
1398 "bt_ctf_stream_get_event_context handles NULL correctly");
1399 event_context = bt_ctf_stream_get_event_context(stream);
1400 ok(event_context,
1401 "bt_ctf_stream_get_event_context returns a stream event context");
1402 ok(bt_ctf_stream_set_event_context(NULL, event_context) < 0,
1403 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
1404 ok(bt_ctf_stream_set_event_context(stream, NULL) < 0,
1405 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
1406 ok(!bt_ctf_stream_set_event_context(stream, event_context),
1407 "bt_ctf_stream_set_event_context correctly set a stream event context");
1408 ret_field = bt_ctf_field_create(integer_type);
1409 ok(bt_ctf_stream_set_event_context(stream, ret_field) < 0,
1410 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
1411 bt_ctf_field_put(ret_field);
1412
1413 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
1414 event = bt_ctf_event_create(event_class);
1415 struct bt_ctf_field *integer =
1416 bt_ctf_field_create(integer_type);
1417 struct bt_ctf_field *string =
1418 bt_ctf_field_create(string_type);
1419
1420 ret |= bt_ctf_clock_set_time(clock, ++current_time);
1421 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
1422 ret |= bt_ctf_event_set_payload(event, "field_1",
1423 integer);
1424 bt_ctf_field_put(integer);
1425 ret |= bt_ctf_field_string_set_value(string, "This is a test");
1426 ret |= bt_ctf_event_set_payload(event, "a_string",
1427 string);
1428 bt_ctf_field_put(string);
1429
1430 /* Populate stream event context */
1431 integer = bt_ctf_field_structure_get_field(event_context,
1432 "common_event_context");
1433 ret |= bt_ctf_field_unsigned_integer_set_value(integer,
1434 i % 42);
1435 bt_ctf_field_put(integer);
1436
1437 ret |= bt_ctf_stream_append_event(stream, event);
1438 bt_ctf_event_put(event);
1439
1440 if (ret) {
1441 break;
1442 }
1443 }
1444
1445 events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH);
1446 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
1447 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
1448 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
1449 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1450 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1451 ok(ret == 0 && ret_uint64 == 0,
1452 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1453 bt_ctf_stream_append_discarded_events(stream, 1000);
1454 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1455 ok(ret == 0 && ret_uint64 == 1000,
1456 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1457
1458 end:
1459 ok(events_appended, "Append 100 000 events to a stream");
1460
1461 /*
1462 * Populate the custom packet context field with a dummy value
1463 * otherwise flush will fail.
1464 */
1465 packet_context = bt_ctf_stream_get_packet_context(stream);
1466 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1467 "custom_packet_context_field");
1468 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
1469
1470 ok(bt_ctf_stream_flush(stream) == 0,
1471 "Flush a stream that forces a packet resize");
1472 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1473 ok(ret == 0 && ret_uint64 == 1000,
1474 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
1475 bt_ctf_field_type_put(integer_type);
1476 bt_ctf_field_type_put(string_type);
1477 bt_ctf_field_put(packet_context);
1478 bt_ctf_field_put(packet_context_field);
1479 bt_ctf_field_put(event_context);
1480 bt_ctf_event_class_put(event_class);
1481 }
1482
1483 int main(int argc, char **argv)
1484 {
1485 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
1486 char metadata_path[sizeof(trace_path) + 9];
1487 const char *clock_name = "test_clock";
1488 const char *clock_description = "This is a test clock";
1489 const char *returned_clock_name;
1490 const char *returned_clock_description;
1491 const uint64_t frequency = 1123456789;
1492 const uint64_t offset_s = 1351530929945824323;
1493 const uint64_t offset = 1234567;
1494 const uint64_t precision = 10;
1495 const int is_absolute = 0xFF;
1496 char *metadata_string;
1497 struct bt_ctf_writer *writer;
1498 struct utsname name;
1499 char hostname[BABELTRACE_HOST_NAME_MAX];
1500 struct bt_ctf_clock *clock, *ret_clock;
1501 struct bt_ctf_stream_class *stream_class;
1502 struct bt_ctf_stream *stream1;
1503 const char *ret_string;
1504 const unsigned char *ret_uuid;
1505 unsigned char tmp_uuid[16] = { 0 };
1506 struct bt_ctf_field_type *packet_context_type,
1507 *packet_context_field_type,
1508 *packet_header_type,
1509 *packet_header_field_type,
1510 *integer_type,
1511 *stream_event_context_type,
1512 *ret_field_type;
1513 struct bt_ctf_field *packet_header, *packet_header_field;
1514 struct bt_ctf_trace *trace;
1515 int ret;
1516
1517 if (argc < 3) {
1518 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
1519 return -1;
1520 }
1521
1522 plan_no_plan();
1523
1524 if (!mkdtemp(trace_path)) {
1525 perror("# perror");
1526 }
1527
1528 strcpy(metadata_path, trace_path);
1529 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
1530
1531 writer = bt_ctf_writer_create(trace_path);
1532 ok(writer, "bt_ctf_create succeeds in creating trace with path");
1533
1534 /* Add environment context to the trace */
1535 ret = gethostname(hostname, sizeof(hostname));
1536 if (ret < 0) {
1537 return ret;
1538 }
1539 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
1540 "Add host (%s) environment field to writer instance",
1541 hostname);
1542 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
1543 "test_value"),
1544 "bt_ctf_writer_add_environment_field error with NULL writer");
1545 ok(bt_ctf_writer_add_environment_field(writer, NULL,
1546 "test_value"),
1547 "bt_ctf_writer_add_environment_field error with NULL field name");
1548 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
1549 NULL),
1550 "bt_ctf_writer_add_environment_field error with NULL field value");
1551
1552 if (uname(&name)) {
1553 perror("uname");
1554 return -1;
1555 }
1556
1557 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
1558 == 0, "Add sysname (%s) environment field to writer instance",
1559 name.sysname);
1560 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
1561 name.nodename) == 0,
1562 "Add nodename (%s) environment field to writer instance",
1563 name.nodename);
1564 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
1565 == 0, "Add release (%s) environment field to writer instance",
1566 name.release);
1567 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
1568 == 0, "Add version (%s) environment field to writer instance",
1569 name.version);
1570 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
1571 == 0, "Add machine (%s) environment field to writer istance",
1572 name.machine);
1573
1574 /* Define a clock and add it to the trace */
1575 ok(bt_ctf_clock_create("signed") == NULL,
1576 "Illegal clock name rejected");
1577 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
1578 clock = bt_ctf_clock_create(clock_name);
1579 ok(clock, "Clock created sucessfully");
1580 returned_clock_name = bt_ctf_clock_get_name(clock);
1581 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
1582 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
1583 "Returned clock name is valid");
1584
1585 returned_clock_description = bt_ctf_clock_get_description(clock);
1586 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
1587 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
1588 "Clock description set successfully");
1589
1590 returned_clock_description = bt_ctf_clock_get_description(clock);
1591 ok(returned_clock_description,
1592 "bt_ctf_clock_get_description returns a description.");
1593 ok(returned_clock_description ?
1594 !strcmp(returned_clock_description, clock_description) : 0,
1595 "Returned clock description is valid");
1596
1597 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
1598 "bt_ctf_clock_get_frequency returns the correct default frequency");
1599 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
1600 "Set clock frequency");
1601 ok(bt_ctf_clock_get_frequency(clock) == frequency,
1602 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1603
1604 ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S,
1605 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
1606 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
1607 "Set clock offset (seconds)");
1608 ok(bt_ctf_clock_get_offset_s(clock) == offset_s,
1609 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1610
1611 ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET,
1612 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
1613 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
1614 ok(bt_ctf_clock_get_offset(clock) == offset,
1615 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
1616
1617 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
1618 "bt_ctf_clock_get_precision returns the correct default precision");
1619 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
1620 "Set clock precision");
1621 ok(bt_ctf_clock_get_precision(clock) == precision,
1622 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1623
1624 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
1625 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1626 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
1627 "Set clock absolute property");
1628 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
1629 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1630
1631 ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME,
1632 "bt_ctf_clock_get_time returns the correct default time");
1633 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
1634 "Set clock time");
1635 ok(bt_ctf_clock_get_time(clock) == current_time,
1636 "bt_ctf_clock_get_time returns the correct time once it is set");
1637
1638 ok(bt_ctf_writer_add_clock(writer, clock) == 0,
1639 "Add clock to writer instance");
1640 ok(bt_ctf_writer_add_clock(writer, clock),
1641 "Verify a clock can't be added twice to a writer instance");
1642
1643 ok(!bt_ctf_writer_get_trace(NULL),
1644 "bt_ctf_writer_get_trace correctly handles NULL");
1645 trace = bt_ctf_writer_get_trace(writer);
1646 ok(trace,
1647 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
1648 ok(bt_ctf_trace_get_clock_count(NULL) < 0,
1649 "bt_ctf_trace_get_clock_count correctly handles NULL");
1650 ok(bt_ctf_trace_get_clock_count(trace) == 1,
1651 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
1652 ok(!bt_ctf_trace_get_clock(NULL, 0),
1653 "bt_ctf_trace_get_clock correctly handles NULL");
1654 ok(!bt_ctf_trace_get_clock(trace, -1),
1655 "bt_ctf_trace_get_clock correctly handles negative indexes");
1656 ok(!bt_ctf_trace_get_clock(trace, 1),
1657 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
1658 ret_clock = bt_ctf_trace_get_clock(trace, 0);
1659 ok(ret_clock == clock,
1660 "bt_ctf_trace_get_clock returns the right clock instance");
1661 bt_ctf_clock_put(ret_clock);
1662
1663 ok(!bt_ctf_clock_get_name(NULL),
1664 "bt_ctf_clock_get_name correctly handles NULL");
1665 ok(!bt_ctf_clock_get_description(NULL),
1666 "bt_ctf_clock_get_description correctly handles NULL");
1667 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
1668 "bt_ctf_clock_get_frequency correctly handles NULL");
1669 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
1670 "bt_ctf_clock_get_precision correctly handles NULL");
1671 ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL,
1672 "bt_ctf_clock_get_offset_s correctly handles NULL");
1673 ok(bt_ctf_clock_get_offset(NULL) == -1ULL,
1674 "bt_ctf_clock_get_offset correctly handles NULL");
1675 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
1676 "bt_ctf_clock_get_is_absolute correctly handles NULL");
1677 ok(bt_ctf_clock_get_time(NULL) == -1ULL,
1678 "bt_ctf_clock_get_time correctly handles NULL");
1679
1680 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
1681 "bt_ctf_clock_set_description correctly handles NULL clock");
1682 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
1683 "bt_ctf_clock_set_frequency correctly handles NULL clock");
1684 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
1685 "bt_ctf_clock_get_precision correctly handles NULL clock");
1686 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
1687 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
1688 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
1689 "bt_ctf_clock_set_offset correctly handles NULL clock");
1690 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
1691 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
1692 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
1693 "bt_ctf_clock_set_time correctly handles NULL clock");
1694 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
1695 "bt_ctf_clock_get_uuid correctly handles NULL clock");
1696 ret_uuid = bt_ctf_clock_get_uuid(clock);
1697 ok(ret_uuid,
1698 "bt_ctf_clock_get_uuid returns a UUID");
1699 if (ret_uuid) {
1700 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
1701 /* Slightly modify UUID */
1702 tmp_uuid[sizeof(tmp_uuid) - 1]++;
1703 }
1704
1705 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
1706 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
1707 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
1708 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
1709 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
1710 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
1711 ret_uuid = bt_ctf_clock_get_uuid(clock);
1712 ok(ret_uuid,
1713 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
1714 ok(uuid_match(ret_uuid, tmp_uuid),
1715 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
1716
1717 /* Define a stream class */
1718 stream_class = bt_ctf_stream_class_create("test_stream");
1719
1720 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
1721 "bt_ctf_stream_class_get_name handles NULL correctly");
1722 ret_string = bt_ctf_stream_class_get_name(stream_class);
1723 ok(!strcmp(ret_string, "test_stream"),
1724 "bt_ctf_stream_class_get_name returns a correct stream class name");
1725
1726 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
1727 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1728 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
1729 "bt_ctf_stream_class_get_clock handles NULL correctly");
1730
1731 ok(stream_class, "Create stream class");
1732 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
1733 "Set a stream class' clock");
1734 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
1735 ok(ret_clock == clock,
1736 "bt_ctf_stream_class_get_clock returns a correct clock");
1737 bt_ctf_clock_put(ret_clock);
1738
1739 /* Test the event fields and event types APIs */
1740 type_field_tests();
1741
1742 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
1743 "bt_ctf_stream_class_get_id returns an error when no id is set");
1744 ok(bt_ctf_stream_class_get_id(NULL) < 0,
1745 "bt_ctf_stream_class_get_id handles NULL correctly");
1746 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
1747 "bt_ctf_stream_class_set_id handles NULL correctly");
1748 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
1749 "Set an stream class' id");
1750 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
1751 "bt_ctf_stream_class_get_id returns the correct value");
1752
1753 /* Add a custom trace packet header field */
1754 ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
1755 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
1756 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
1757 ok(packet_header_type,
1758 "bt_ctf_trace_get_packet_header_type returns a packet header");
1759 ok(bt_ctf_field_type_get_type_id(packet_header_type) == CTF_TYPE_STRUCT,
1760 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
1761 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1762 packet_header_type, "magic");
1763 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
1764 bt_ctf_field_type_put(ret_field_type);
1765 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1766 packet_header_type, "uuid");
1767 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
1768 bt_ctf_field_type_put(ret_field_type);
1769 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1770 packet_header_type, "stream_id");
1771 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
1772 bt_ctf_field_type_put(ret_field_type);
1773
1774 packet_header_field_type = bt_ctf_field_type_integer_create(22);
1775 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
1776 packet_header_field_type, "custom_trace_packet_header_field"),
1777 "Added a custom trace packet header field successfully");
1778
1779 ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
1780 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
1781 ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0,
1782 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
1783 ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
1784 "Set a trace packet_header_type successfully");
1785
1786 /* Create a "uint5_t" equivalent custom packet context field */
1787 packet_context_field_type = bt_ctf_field_type_integer_create(5);
1788
1789 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
1790 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
1791
1792 /* Add a custom field to the stream class' packet context */
1793 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1794 ok(packet_context_type,
1795 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
1796 ok(bt_ctf_field_type_get_type_id(packet_context_type) == CTF_TYPE_STRUCT,
1797 "Packet context is a structure");
1798
1799 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
1800 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
1801 ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL),
1802 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
1803
1804 integer_type = bt_ctf_field_type_integer_create(32);
1805 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
1806 integer_type) < 0,
1807 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
1808 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1809 packet_context_field_type, "custom_packet_context_field");
1810 ok(ret == 0, "Packet context field added successfully");
1811
1812 /* Define a stream event context containing a my_integer field. */
1813 ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL,
1814 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
1815 ok(bt_ctf_stream_class_get_event_context_type(
1816 stream_class) == NULL,
1817 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
1818 stream_event_context_type = bt_ctf_field_type_structure_create();
1819 bt_ctf_field_type_structure_add_field(stream_event_context_type,
1820 integer_type, "common_event_context");
1821
1822 ok(bt_ctf_stream_class_set_event_context_type(NULL,
1823 stream_event_context_type) < 0,
1824 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
1825 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
1826 NULL) < 0,
1827 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
1828 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
1829 integer_type) < 0,
1830 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
1831
1832 ok(bt_ctf_stream_class_set_event_context_type(
1833 stream_class, stream_event_context_type) == 0,
1834 "Set a new stream event context type");
1835 ret_field_type = bt_ctf_stream_class_get_event_context_type(
1836 stream_class);
1837 ok(ret_field_type == stream_event_context_type,
1838 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
1839 bt_ctf_field_type_put(ret_field_type);
1840
1841 /* Instantiate a stream and append events */
1842 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
1843 ok(stream1, "Instanciate a stream class from writer");
1844
1845 /*
1846 * Try to modify the packet context type after a stream has been
1847 * created.
1848 */
1849 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
1850 packet_header_field_type, "should_fail");
1851 ok(ret < 0,
1852 "Trace packet header type can't be modified once a stream has been instanciated");
1853
1854 /*
1855 * Try to modify the packet context type after a stream has been
1856 * created.
1857 */
1858 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1859 packet_context_field_type, "should_fail");
1860 ok(ret < 0,
1861 "Packet context type can't be modified once a stream has been instanciated");
1862
1863 /*
1864 * Try to modify the stream event context type after a stream has been
1865 * created.
1866 */
1867 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
1868 integer_type, "should_fail");
1869 ok(ret < 0,
1870 "Stream event context type can't be modified once a stream has been instanciated");
1871
1872 /* Should fail after instanciating a stream (frozen) */
1873 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
1874 "Changes to a stream class that was already instantiated fail");
1875
1876 /* Populate the custom packet header field only once for all tests */
1877 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
1878 "bt_ctf_stream_get_packet_header handles NULL correctly");
1879 packet_header = bt_ctf_stream_get_packet_header(stream1);
1880 ok(packet_header,
1881 "bt_ctf_stream_get_packet_header returns a packet header");
1882 ret_field_type = bt_ctf_field_get_type(packet_header);
1883 ok(ret_field_type == packet_header_type,
1884 "Stream returns a packet header of the appropriate type");
1885 bt_ctf_field_type_put(ret_field_type);
1886 packet_header_field = bt_ctf_field_structure_get_field(packet_header,
1887 "custom_trace_packet_header_field");
1888 ok(packet_header_field,
1889 "Packet header structure contains a custom field with the appropriate name");
1890 ret_field_type = bt_ctf_field_get_type(packet_header_field);
1891 ok(ret_field_type == packet_header_field_type,
1892 "Custom packet header field is of the expected type");
1893 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
1894 54321), "Set custom packet header value successfully");
1895 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
1896 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
1897 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
1898 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
1899 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
1900 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
1901 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
1902 "Successfully set a stream's packet header");
1903
1904 append_simple_event(stream_class, stream1, clock);
1905
1906 packet_resize_test(stream_class, stream1, clock);
1907
1908 append_complex_event(stream_class, stream1, clock);
1909
1910 metadata_string = bt_ctf_writer_get_metadata_string(writer);
1911 ok(metadata_string, "Get metadata string");
1912
1913 bt_ctf_writer_flush_metadata(writer);
1914 validate_metadata(argv[1], metadata_path);
1915 validate_trace(argv[2], trace_path);
1916
1917 bt_ctf_clock_put(clock);
1918 bt_ctf_stream_class_put(stream_class);
1919 bt_ctf_writer_put(writer);
1920 bt_ctf_stream_put(stream1);
1921 bt_ctf_field_type_put(packet_context_type);
1922 bt_ctf_field_type_put(packet_context_field_type);
1923 bt_ctf_field_type_put(integer_type);
1924 bt_ctf_field_type_put(stream_event_context_type);
1925 bt_ctf_field_type_put(ret_field_type);
1926 bt_ctf_field_type_put(packet_header_type);
1927 bt_ctf_field_type_put(packet_header_field_type);
1928 bt_ctf_field_put(packet_header);
1929 bt_ctf_field_put(packet_header_field);
1930 bt_ctf_trace_put(trace);
1931 free(metadata_string);
1932
1933 /* Remove all trace files and delete temporary trace directory */
1934 DIR *trace_dir = opendir(trace_path);
1935 if (!trace_dir) {
1936 perror("# opendir");
1937 return -1;
1938 }
1939
1940 struct dirent *entry;
1941 while ((entry = readdir(trace_dir))) {
1942 if (entry->d_type == DT_REG) {
1943 unlinkat(dirfd(trace_dir), entry->d_name, 0);
1944 }
1945 }
1946
1947 rmdir(trace_path);
1948 closedir(trace_dir);
1949
1950 return 0;
1951 }
This page took 0.107822 seconds and 5 git commands to generate.