test_ctf_writer.c: test packet functions
[babeltrace.git] / tests / lib / test_ctf_writer.c
1 /*
2 * test-ctf-writer.c
3 *
4 * CTF Writer test
5 *
6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-writer/stream-class.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ref.h>
31 #include <babeltrace/ctf/events.h>
32 #include <babeltrace/values.h>
33 #include <unistd.h>
34 #include <babeltrace/compat/stdlib.h>
35 #include <stdio.h>
36 #include <sys/utsname.h>
37 #include <babeltrace/compat/limits.h>
38 #include <babeltrace/compat/stdio.h>
39 #include <string.h>
40 #include <assert.h>
41 #include <sys/wait.h>
42 #include <fcntl.h>
43 #include <babeltrace/compat/dirent.h>
44 #include "tap/tap.h"
45 #include <math.h>
46 #include <float.h>
47 #include <sys/stat.h>
48
49 #define METADATA_LINE_SIZE 512
50 #define SEQUENCE_TEST_LENGTH 10
51 #define ARRAY_TEST_LENGTH 5
52 #define PACKET_RESIZE_TEST_LENGTH 100000
53
54 #define DEFAULT_CLOCK_FREQ 1000000000
55 #define DEFAULT_CLOCK_PRECISION 1
56 #define DEFAULT_CLOCK_OFFSET 0
57 #define DEFAULT_CLOCK_OFFSET_S 0
58 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
59 #define DEFAULT_CLOCK_TIME 0
60 #define DEFAULT_CLOCK_VALUE 0
61
62 #define NR_TESTS 605
63
64 static int64_t current_time = 42;
65
66 /* Return 1 if uuids match, zero if different. */
67 int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b)
68 {
69 int ret = 0;
70 int i;
71
72 if (!uuid_a || !uuid_b) {
73 goto end;
74 }
75
76 for (i = 0; i < 16; i++) {
77 if (uuid_a[i] != uuid_b[i]) {
78 goto end;
79 }
80 }
81
82 ret = 1;
83 end:
84 return ret;
85 }
86
87 void validate_metadata(char *parser_path, char *metadata_path)
88 {
89 int ret = 0;
90 char parser_output_path[] = "/tmp/parser_output_XXXXXX";
91 int parser_output_fd = -1, metadata_fd = -1;
92
93 if (!metadata_path) {
94 ret = -1;
95 goto result;
96 }
97
98 parser_output_fd = mkstemp(parser_output_path);
99 metadata_fd = open(metadata_path, O_RDONLY);
100
101 unlink(parser_output_path);
102
103 if (parser_output_fd == -1 || metadata_fd == -1) {
104 diag("Failed create temporary files for metadata parsing.");
105 ret = -1;
106 goto result;
107 }
108
109 pid_t pid = fork();
110 if (pid) {
111 int status = 0;
112 waitpid(pid, &status, 0);
113 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
114 } else {
115 /* ctf-parser-test expects a metadata string on stdin. */
116 ret = dup2(metadata_fd, STDIN_FILENO);
117 if (ret < 0) {
118 perror("# dup2 metadata_fd to STDIN");
119 goto result;
120 }
121
122 ret = dup2(parser_output_fd, STDOUT_FILENO);
123 if (ret < 0) {
124 perror("# dup2 parser_output_fd to STDOUT");
125 goto result;
126 }
127
128 ret = dup2(parser_output_fd, STDERR_FILENO);
129 if (ret < 0) {
130 perror("# dup2 parser_output_fd to STDERR");
131 goto result;
132 }
133
134 execl(parser_path, "ctf-parser-test", NULL);
135 perror("# Could not launch the ctf metadata parser process");
136 exit(-1);
137 }
138 result:
139 ok(ret == 0, "Metadata string is valid");
140
141 if (ret && metadata_fd >= 0 && parser_output_fd >= 0) {
142 char *line;
143 size_t len = METADATA_LINE_SIZE;
144 FILE *metadata_fp = NULL, *parser_output_fp = NULL;
145
146 metadata_fp = fdopen(metadata_fd, "r");
147 if (!metadata_fp) {
148 perror("fdopen on metadata_fd");
149 goto close_fp;
150 }
151 metadata_fd = -1;
152
153 parser_output_fp = fdopen(parser_output_fd, "r");
154 if (!parser_output_fp) {
155 perror("fdopen on parser_output_fd");
156 goto close_fp;
157 }
158 parser_output_fd = -1;
159
160 line = malloc(len);
161 if (!line) {
162 diag("malloc failure");
163 }
164
165 rewind(metadata_fp);
166
167 /* Output the metadata and parser output as diagnostic */
168 while (bt_getline(&line, &len, metadata_fp) > 0) {
169 fprintf(stderr, "# %s", line);
170 }
171
172 rewind(parser_output_fp);
173 while (bt_getline(&line, &len, parser_output_fp) > 0) {
174 fprintf(stderr, "# %s", line);
175 }
176
177 free(line);
178 close_fp:
179 if (metadata_fp) {
180 if (fclose(metadata_fp)) {
181 diag("fclose failure");
182 }
183 }
184 if (parser_output_fp) {
185 if (fclose(parser_output_fp)) {
186 diag("fclose failure");
187 }
188 }
189 }
190
191 if (parser_output_fd >= 0) {
192 if (close(parser_output_fd)) {
193 diag("close error");
194 }
195 }
196 if (metadata_fd >= 0) {
197 if (close(metadata_fd)) {
198 diag("close error");
199 }
200 }
201 }
202
203 void validate_trace(char *parser_path, char *trace_path)
204 {
205 int ret = 0;
206 char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX";
207 int babeltrace_output_fd = -1;
208
209 if (!trace_path) {
210 ret = -1;
211 goto result;
212 }
213
214 babeltrace_output_fd = mkstemp(babeltrace_output_path);
215 unlink(babeltrace_output_path);
216
217 if (babeltrace_output_fd == -1) {
218 diag("Failed to create a temporary file for trace parsing.");
219 ret = -1;
220 goto result;
221 }
222
223 pid_t pid = fork();
224 if (pid) {
225 int status = 0;
226 waitpid(pid, &status, 0);
227 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
228 } else {
229 ret = dup2(babeltrace_output_fd, STDOUT_FILENO);
230 if (ret < 0) {
231 perror("# dup2 babeltrace_output_fd to STDOUT");
232 goto result;
233 }
234
235 ret = dup2(babeltrace_output_fd, STDERR_FILENO);
236 if (ret < 0) {
237 perror("# dup2 babeltrace_output_fd to STDERR");
238 goto result;
239 }
240
241 execl(parser_path, "babeltrace", trace_path, NULL);
242 perror("# Could not launch the babeltrace process");
243 exit(-1);
244 }
245 result:
246 ok(ret == 0, "Babeltrace could read the resulting trace");
247
248 if (ret && babeltrace_output_fd >= 0) {
249 char *line;
250 size_t len = METADATA_LINE_SIZE;
251 FILE *babeltrace_output_fp = NULL;
252
253 babeltrace_output_fp = fdopen(babeltrace_output_fd, "r");
254 if (!babeltrace_output_fp) {
255 perror("fdopen on babeltrace_output_fd");
256 goto close_fp;
257 }
258 babeltrace_output_fd = -1;
259
260 line = malloc(len);
261 if (!line) {
262 diag("malloc error");
263 }
264 rewind(babeltrace_output_fp);
265 while (bt_getline(&line, &len, babeltrace_output_fp) > 0) {
266 diag("%s", line);
267 }
268
269 free(line);
270 close_fp:
271 if (babeltrace_output_fp) {
272 if (fclose(babeltrace_output_fp)) {
273 diag("fclose error");
274 }
275 }
276 }
277
278 if (babeltrace_output_fd >= 0) {
279 if (close(babeltrace_output_fd)) {
280 diag("close error");
281 }
282 }
283 }
284
285 void append_simple_event(struct bt_ctf_stream_class *stream_class,
286 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
287 {
288 /* Create and add a simple event class */
289 struct bt_ctf_event_class *simple_event_class =
290 bt_ctf_event_class_create("Simple Event");
291 struct bt_ctf_field_type *uint_12_type =
292 bt_ctf_field_type_integer_create(12);
293 struct bt_ctf_field_type *int_64_type =
294 bt_ctf_field_type_integer_create(64);
295 struct bt_ctf_field_type *float_type =
296 bt_ctf_field_type_floating_point_create();
297 struct bt_ctf_field_type *enum_type;
298 struct bt_ctf_field_type *enum_type_unsigned =
299 bt_ctf_field_type_enumeration_create(uint_12_type);
300 struct bt_ctf_field_type *event_context_type =
301 bt_ctf_field_type_structure_create();
302 struct bt_ctf_field_type *event_payload_type = NULL;
303 struct bt_ctf_field_type *returned_type;
304 struct bt_ctf_event *simple_event;
305 struct bt_ctf_field *integer_field;
306 struct bt_ctf_field *float_field;
307 struct bt_ctf_field *enum_field;
308 struct bt_ctf_field *enum_field_unsigned;
309 struct bt_ctf_field *enum_container_field;
310 const char *mapping_name_test = "truie";
311 const double double_test_value = 3.1415;
312 struct bt_ctf_field *enum_container_field_unsigned;
313 const char *mapping_name_negative_test = "negative_value";
314 const char *ret_char;
315 double ret_double;
316 int64_t ret_range_start_int64_t, ret_range_end_int64_t;
317 uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
318 struct bt_ctf_clock *ret_clock;
319 struct bt_ctf_event_class *ret_event_class;
320 struct bt_ctf_field *packet_context;
321 struct bt_ctf_field *packet_context_field;
322 struct bt_ctf_field *stream_event_context;
323 struct bt_ctf_field *stream_event_context_field;
324 struct bt_ctf_field *event_context;
325 struct bt_ctf_field *event_context_field;
326 struct bt_ctf_field_type *ep_integer_field_type = NULL;
327 struct bt_ctf_field_type *ep_enum_field_type = NULL;
328 struct bt_ctf_field_type *ep_enum_field_unsigned_type = NULL;
329 int ret;
330
331 ok(uint_12_type, "Create an unsigned integer type");
332
333 bt_ctf_field_type_integer_set_signed(int_64_type, 1);
334 ok(int_64_type, "Create a signed integer type");
335 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
336
337 returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type);
338 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
339 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
340 ok(!bt_ctf_field_type_enumeration_create(enum_type),
341 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
342 bt_put(returned_type);
343
344 bt_ctf_field_type_set_alignment(float_type, 32);
345 ok(bt_ctf_field_type_get_alignment(NULL) < 0,
346 "bt_ctf_field_type_get_alignment handles NULL correctly");
347 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
348 "bt_ctf_field_type_get_alignment returns a correct value");
349
350 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
351 "Set a floating point type's exponent digit count");
352 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
353 "Set a floating point type's mantissa digit count");
354
355 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0,
356 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
357 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0,
358 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
359 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
360 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
361 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
362 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
363
364 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
365 mapping_name_negative_test, -12345, 0) == 0,
366 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
367 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
368 "escaping; \"test\"", 1, 1) == 0,
369 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
370 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
371 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
372 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
373 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
374 "event clock int float", 5, 22) == 0,
375 "Accept enumeration mapping strings containing reserved keywords");
376 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
377 42, 42);
378 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
379 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
380 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
381 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
382 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
383 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
384 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
385
386 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42) < 0,
387 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
388 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000) < 0,
389 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
390 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55) == 1,
391 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
392
393 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
394 "enum_field") == 0, "Add signed enumeration field to event");
395
396 ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char,
397 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
398 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
399 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL,
400 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
401 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
402 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
403 NULL, &ret_range_end_int64_t) < 0,
404 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
405 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
406 &ret_range_start_int64_t, NULL) < 0,
407 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
408 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char,
409 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
410 "bt_ctf_field_type_enumeration_get_mapping returns a value");
411 ok(!strcmp(ret_char, mapping_name_test),
412 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
413 ok(ret_range_start_int64_t == 42,
414 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
415 ok(ret_range_end_int64_t == 42,
416 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
417
418 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
419 "escaping; \"test\"", 0, 0) == 0,
420 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
421 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
422 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
423 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
424 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
425 "event clock int float", 5, 22) == 0,
426 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
427 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
428 42, 42);
429 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
430 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
431 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something",
432 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
433 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
434 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
435 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
436 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
437
438 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0,
439 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
440 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4,
441 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
442
443 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char,
444 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
445 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
446 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL,
447 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
448 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
449 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
450 NULL, &ret_range_end_uint64_t) < 0,
451 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
452 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
453 &ret_range_start_uint64_t, NULL) < 0,
454 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
455 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char,
456 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
457 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
458 ok(!strcmp(ret_char, mapping_name_test),
459 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
460 ok(ret_range_start_uint64_t == 42,
461 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
462 ok(ret_range_end_uint64_t == 42,
463 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
464
465 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
466 "integer_field");
467 bt_ctf_event_class_add_field(simple_event_class, float_type,
468 "float_field");
469
470 assert(!bt_ctf_event_class_set_id(simple_event_class, 13));
471
472 /* Set an event context type which will contain a single integer*/
473 ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
474 "event_specific_context"),
475 "Add event specific context field");
476 ok(bt_ctf_event_class_get_context_type(NULL) == NULL,
477 "bt_ctf_event_class_get_context_type handles NULL correctly");
478 ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL,
479 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
480
481 ok(bt_ctf_event_class_set_context_type(simple_event_class, NULL) < 0,
482 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
483 ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0,
484 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
485 ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type),
486 "Set an event class' context type successfully");
487 returned_type = bt_ctf_event_class_get_context_type(simple_event_class);
488 ok(returned_type == event_context_type,
489 "bt_ctf_event_class_get_context_type returns the appropriate type");
490 bt_put(returned_type);
491
492 bt_ctf_stream_class_add_event_class(stream_class, simple_event_class);
493
494 /*
495 * bt_ctf_stream_class_add_event_class() copies the field types
496 * of simple_event_class, so we retrieve the new ones to create
497 * the appropriate fields.
498 */
499 BT_PUT(event_context_type);
500 BT_PUT(event_payload_type);
501 event_payload_type = bt_ctf_event_class_get_payload_type(
502 simple_event_class);
503 assert(event_payload_type);
504 event_context_type = bt_ctf_event_class_get_context_type(
505 simple_event_class);
506 assert(event_context_type);
507 ep_integer_field_type =
508 bt_ctf_field_type_structure_get_field_type_by_name(
509 event_payload_type, "integer_field");
510 assert(ep_integer_field_type);
511 ep_enum_field_type =
512 bt_ctf_field_type_structure_get_field_type_by_name(
513 event_payload_type, "enum_field");
514 assert(ep_enum_field_type);
515 ep_enum_field_unsigned_type =
516 bt_ctf_field_type_structure_get_field_type_by_name(
517 event_payload_type, "enum_field_unsigned");
518 assert(ep_enum_field_unsigned_type);
519
520 ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0,
521 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
522 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
523 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
524 ok(bt_ctf_stream_class_get_event_class(NULL, 0) == NULL,
525 "bt_ctf_stream_class_get_event_class handles NULL correctly");
526 ok(bt_ctf_stream_class_get_event_class(stream_class, 8724) == NULL,
527 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
528 ret_event_class = bt_ctf_stream_class_get_event_class(stream_class, 0);
529 ok(ret_event_class == simple_event_class,
530 "bt_ctf_stream_class_get_event_class returns the correct event class");
531 bt_put(ret_event_class);
532 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL, 0),
533 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
534 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class, 2),
535 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
536 ret_event_class =
537 bt_ctf_stream_class_get_event_class_by_id(stream_class, 13);
538 ok(ret_event_class == simple_event_class,
539 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
540 bt_put(ret_event_class);
541
542 ok(bt_ctf_stream_class_get_event_class_by_name(NULL, "some event name") == NULL,
543 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
544 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, NULL) == NULL,
545 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
546 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, "some event name") == NULL,
547 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
548 ret_event_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, "Simple Event");
549 ok(ret_event_class == simple_event_class,
550 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
551 bt_put(ret_event_class);
552
553 simple_event = bt_ctf_event_create(simple_event_class);
554 ok(simple_event,
555 "Instantiate an event containing a single integer field");
556
557 ok(bt_ctf_event_get_clock(NULL) == NULL,
558 "bt_ctf_event_get_clock handles NULL correctly");
559 ret_clock = bt_ctf_event_get_clock(simple_event);
560 ok(ret_clock == clock,
561 "bt_ctf_event_get_clock returns a correct clock");
562 bt_put(clock);
563
564 integer_field = bt_ctf_field_create(ep_integer_field_type);
565 bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
566 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
567 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
568
569 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
570 ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double),
571 "bt_ctf_field_floating_point_get_value fails on an unset float field");
572 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
573 ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double),
574 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
575 ok(bt_ctf_field_floating_point_get_value(float_field, NULL),
576 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
577 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
578 "bt_ctf_field_floating_point_get_value returns a double value");
579 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
580 "bt_ctf_field_floating_point_get_value returns a correct value");
581
582 enum_field = bt_ctf_field_create(ep_enum_field_type);
583 assert(enum_field);
584 ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL);
585 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
586 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
587 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
588 enum_container_field = bt_ctf_field_enumeration_get_container(
589 enum_field);
590 ok(bt_ctf_field_signed_integer_set_value(
591 enum_container_field, -42) == 0,
592 "Set signed enumeration container value");
593 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
594 ok(!strcmp(ret_char, mapping_name_negative_test),
595 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
596 ret = bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
597 assert(!ret);
598
599 enum_field_unsigned = bt_ctf_field_create(ep_enum_field_unsigned_type);
600 assert(enum_field_unsigned);
601 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
602 enum_field_unsigned);
603 ok(bt_ctf_field_unsigned_integer_set_value(
604 enum_container_field_unsigned, 42) == 0,
605 "Set unsigned enumeration container value");
606 ret = bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
607 enum_field_unsigned);
608 assert(!ret);
609 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned);
610 ok(ret_char && !strcmp(ret_char, mapping_name_test),
611 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
612
613 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
614
615 /* Populate stream event context */
616 stream_event_context =
617 bt_ctf_event_get_stream_event_context(simple_event);
618 assert(stream_event_context);
619 stream_event_context_field = bt_ctf_field_structure_get_field(
620 stream_event_context, "common_event_context");
621 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field, 42);
622
623 /* Populate the event's context */
624 ok(bt_ctf_event_get_event_context(NULL) == NULL,
625 "bt_ctf_event_get_event_context handles NULL correctly");
626 event_context = bt_ctf_event_get_event_context(simple_event);
627 ok(event_context,
628 "bt_ctf_event_get_event_context returns a field");
629 returned_type = bt_ctf_field_get_type(event_context);
630 ok(returned_type == event_context_type,
631 "bt_ctf_event_get_event_context returns a field of the appropriate type");
632 event_context_field = bt_ctf_field_structure_get_field(event_context,
633 "event_specific_context");
634 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field, 1234),
635 "Successfully set an event context's value");
636 ok(bt_ctf_event_set_event_context(NULL, event_context) < 0,
637 "bt_ctf_event_set_event_context handles a NULL event correctly");
638 ok(bt_ctf_event_set_event_context(simple_event, NULL) < 0,
639 "bt_ctf_event_set_event_context handles a NULL event context correctly");
640 ok(bt_ctf_event_set_event_context(simple_event, event_context_field) < 0,
641 "bt_ctf_event_set_event_context rejects a context of the wrong type");
642 ok(!bt_ctf_event_set_event_context(simple_event, event_context),
643 "Set an event context successfully");
644
645 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
646 "Append simple event to trace stream");
647
648 ok(bt_ctf_stream_get_packet_context(NULL) == NULL,
649 "bt_ctf_stream_get_packet_context handles NULL correctly");
650 packet_context = bt_ctf_stream_get_packet_context(stream);
651 ok(packet_context,
652 "bt_ctf_stream_get_packet_context returns a packet context");
653
654 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
655 "packet_size");
656 ok(packet_context_field,
657 "Packet context contains the default packet_size field.");
658 bt_put(packet_context_field);
659 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
660 "custom_packet_context_field");
661 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field, 8) == 0,
662 "Custom packet context field value successfully set.");
663
664 ok(bt_ctf_stream_set_packet_context(NULL, packet_context_field) < 0,
665 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
666 ok(bt_ctf_stream_set_packet_context(stream, NULL) < 0,
667 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
668 ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0,
669 "Successfully set a stream's packet context");
670
671 ok(bt_ctf_stream_flush(stream) == 0,
672 "Flush trace stream with one event");
673
674 bt_put(simple_event_class);
675 bt_put(simple_event);
676 bt_put(uint_12_type);
677 bt_put(int_64_type);
678 bt_put(float_type);
679 bt_put(enum_type);
680 bt_put(enum_type_unsigned);
681 bt_put(returned_type);
682 bt_put(event_context_type);
683 bt_put(integer_field);
684 bt_put(float_field);
685 bt_put(enum_field);
686 bt_put(enum_field_unsigned);
687 bt_put(enum_container_field);
688 bt_put(enum_container_field_unsigned);
689 bt_put(packet_context);
690 bt_put(packet_context_field);
691 bt_put(stream_event_context);
692 bt_put(stream_event_context_field);
693 bt_put(event_context);
694 bt_put(event_context_field);
695 bt_put(event_payload_type);
696 bt_put(ep_integer_field_type);
697 bt_put(ep_enum_field_type);
698 bt_put(ep_enum_field_unsigned_type);
699 }
700
701 void append_complex_event(struct bt_ctf_stream_class *stream_class,
702 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
703 {
704 struct event_class_attrs_counts {
705 int id;
706 int name;
707 int loglevel;
708 int modelemfuri;
709 int unknown;
710 } attrs_count;
711
712 int i;
713 int ret;
714 int64_t int64_value;
715 struct event_class_attrs_counts ;
716 const char *complex_test_event_string = "Complex Test Event";
717 const char *test_string_1 = "Test ";
718 const char *test_string_2 = "string ";
719 const char *test_string_3 = "abcdefghi";
720 const char *test_string_4 = "abcd\0efg\0hi";
721 const char *test_string_cat = "Test string abcdeabcd";
722 struct bt_ctf_field_type *uint_35_type =
723 bt_ctf_field_type_integer_create(35);
724 struct bt_ctf_field_type *int_16_type =
725 bt_ctf_field_type_integer_create(16);
726 struct bt_ctf_field_type *uint_3_type =
727 bt_ctf_field_type_integer_create(3);
728 struct bt_ctf_field_type *enum_variant_type =
729 bt_ctf_field_type_enumeration_create(uint_3_type);
730 struct bt_ctf_field_type *variant_type =
731 bt_ctf_field_type_variant_create(enum_variant_type,
732 "variant_selector");
733 struct bt_ctf_field_type *string_type =
734 bt_ctf_field_type_string_create();
735 struct bt_ctf_field_type *sequence_type;
736 struct bt_ctf_field_type *array_type;
737 struct bt_ctf_field_type *inner_structure_type =
738 bt_ctf_field_type_structure_create();
739 struct bt_ctf_field_type *complex_structure_type =
740 bt_ctf_field_type_structure_create();
741 struct bt_ctf_field_type *ret_field_type;
742 struct bt_ctf_event_class *event_class;
743 struct bt_ctf_event *event;
744 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
745 *inner_structure_field, *complex_structure_field,
746 *a_sequence_field, *enum_variant_field, *enum_container_field,
747 *variant_field, *an_array_field, *stream_event_ctx_field,
748 *stream_event_ctx_int_field, *ret_field;
749 uint64_t ret_unsigned_int;
750 int64_t ret_signed_int;
751 const char *ret_string;
752 struct bt_ctf_stream_class *ret_stream_class;
753 struct bt_ctf_event_class *ret_event_class;
754 struct bt_ctf_field *packet_context, *packet_context_field;
755 struct bt_value *obj;
756
757 ok(bt_ctf_field_type_set_alignment(int_16_type, 0),
758 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
759 ok(bt_ctf_field_type_set_alignment(int_16_type, 3),
760 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
761 ok(bt_ctf_field_type_set_alignment(int_16_type, 24),
762 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
763 ok(!bt_ctf_field_type_set_alignment(int_16_type, 4),
764 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
765 bt_ctf_field_type_set_alignment(int_16_type, 32);
766 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
767 bt_ctf_field_type_integer_set_base(uint_35_type,
768 BT_CTF_INTEGER_BASE_HEXADECIMAL);
769
770 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
771 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
772 "seq_len");
773
774 ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL,
775 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
776 ret_field_type = bt_ctf_field_type_array_get_element_type(
777 array_type);
778 ok(ret_field_type == int_16_type,
779 "bt_ctf_field_type_array_get_element_type returns the correct type");
780 bt_put(ret_field_type);
781
782 ok(bt_ctf_field_type_array_get_length(NULL) < 0,
783 "bt_ctf_field_type_array_get_length handles NULL correctly");
784 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
785 "bt_ctf_field_type_array_get_length returns the correct length");
786
787 bt_ctf_field_type_structure_add_field(inner_structure_type,
788 uint_35_type, "seq_len");
789 bt_ctf_field_type_structure_add_field(inner_structure_type,
790 sequence_type, "a_sequence");
791 bt_ctf_field_type_structure_add_field(inner_structure_type,
792 array_type, "an_array");
793
794 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
795 "UINT3_TYPE", 0, 0);
796 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
797 "INT16_TYPE", 1, 1);
798 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
799 "UINT35_TYPE", 2, 7);
800
801 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL,
802 "INT16_TYPE") < 0,
803 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
804 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
805 enum_variant_type, NULL) < 0,
806 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
807 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
808 enum_variant_type, "INT16_TYPE") == 1,
809 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
810
811 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1) < 0,
812 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
813 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42) < 0,
814 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
815 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5) == 2,
816 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
817
818 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
819 "An unknown entry"), "Reject a variant field based on an unknown tag value");
820 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
821 "UINT3_TYPE") == 0, "Add a field to a variant");
822 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
823 "INT16_TYPE");
824 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
825 "UINT35_TYPE");
826
827 ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL,
828 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
829 ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type);
830 ok(ret_field_type == enum_variant_type,
831 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
832 bt_put(ret_field_type);
833
834 ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL,
835 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
836 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
837 ok(ret_string ? !strcmp(ret_string, "variant_selector") : 0,
838 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
839 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL,
840 "INT16_TYPE") == NULL,
841 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
842 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type,
843 NULL) == NULL,
844 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
845 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
846 variant_type, "INT16_TYPE");
847 ok(ret_field_type == int_16_type,
848 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
849 bt_put(ret_field_type);
850
851 ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0,
852 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
853 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
854 "bt_ctf_field_type_variant_get_field_count returns the correct count");
855
856 ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0,
857 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
858 ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) == 0,
859 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
860 bt_put(ret_field_type);
861 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) == 0,
862 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
863 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0,
864 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
865 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0,
866 "bt_ctf_field_type_variant_get_field returns a field");
867 ok(!strcmp("INT16_TYPE", ret_string),
868 "bt_ctf_field_type_variant_get_field returns a correct field name");
869 ok(ret_field_type == int_16_type,
870 "bt_ctf_field_type_variant_get_field returns a correct field type");
871 bt_put(ret_field_type);
872
873 bt_ctf_field_type_structure_add_field(complex_structure_type,
874 enum_variant_type, "variant_selector");
875 bt_ctf_field_type_structure_add_field(complex_structure_type,
876 string_type, "a_string");
877 bt_ctf_field_type_structure_add_field(complex_structure_type,
878 variant_type, "variant_value");
879 bt_ctf_field_type_structure_add_field(complex_structure_type,
880 inner_structure_type, "inner_structure");
881
882 ok(bt_ctf_event_class_create("clock") == NULL,
883 "Reject creation of an event class with an illegal name");
884 event_class = bt_ctf_event_class_create(complex_test_event_string);
885 ok(event_class, "Create an event class");
886 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
887 "Reject addition of a field with an empty name to an event");
888 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
889 "Reject addition of a field with a NULL type to an event");
890 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
891 "int"),
892 "Reject addition of a type with an illegal name to an event");
893 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
894 "uint_35") == 0,
895 "Add field of type unsigned integer to an event");
896 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
897 "int_16") == 0, "Add field of type signed integer to an event");
898 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
899 "complex_structure") == 0,
900 "Add composite structure to an event");
901
902 ok(bt_ctf_event_class_get_name(NULL) == NULL,
903 "bt_ctf_event_class_get_name handles NULL correctly");
904 ret_string = bt_ctf_event_class_get_name(event_class);
905 ok(!strcmp(ret_string, complex_test_event_string),
906 "bt_ctf_event_class_get_name returns a correct name");
907 ok(bt_ctf_event_class_get_id(event_class) < 0,
908 "bt_ctf_event_class_get_id returns a negative value when not set");
909 ok(bt_ctf_event_class_get_id(NULL) < 0,
910 "bt_ctf_event_class_get_id handles NULL correctly");
911 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
912 "bt_ctf_event_class_set_id handles NULL correctly");
913 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
914 "Set an event class' id");
915 ok(bt_ctf_event_class_get_id(event_class) == 42,
916 "bt_ctf_event_class_get_id returns the correct value");
917
918 /* Test event class attributes */
919 obj = bt_value_integer_create_init(15);
920 assert(obj);
921 ok(bt_ctf_event_class_set_attribute(NULL, "id", obj),
922 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
923 ok(bt_ctf_event_class_set_attribute(event_class, NULL, obj),
924 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
925 ok(bt_ctf_event_class_set_attribute(event_class, "id", NULL),
926 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
927 assert(!bt_value_integer_set(obj, -3));
928 ok(bt_ctf_event_class_set_attribute(event_class, "id", obj),
929 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
930 assert(!bt_value_integer_set(obj, 11));
931 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
932 ok(!ret && bt_ctf_event_class_get_id(event_class) == 11,
933 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
934 ret = bt_ctf_event_class_set_attribute(event_class, "name", obj);
935 ret &= bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj);
936 ok(ret,
937 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
938 BT_PUT(obj);
939
940 obj = bt_value_integer_create_init(5);
941 assert(obj);
942 ok(!bt_ctf_event_class_set_attribute(event_class, "loglevel", obj),
943 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
944 BT_PUT(obj);
945 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL, "loglevel"),
946 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
947 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, NULL),
948 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
949 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, "meow"),
950 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
951 obj = bt_ctf_event_class_get_attribute_value_by_name(event_class,
952 "loglevel");
953 int64_value = 0;
954 ret = bt_value_integer_get(obj, &int64_value);
955 ok(obj && !ret && int64_value == 5,
956 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
957 BT_PUT(obj);
958
959 obj = bt_value_string_create_init("nu name");
960 assert(obj);
961 assert(!bt_ctf_event_class_set_attribute(event_class, "name", obj));
962 ret_string = bt_ctf_event_class_get_name(event_class);
963 ok(!strcmp(ret_string, "nu name"),
964 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
965 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
966 ret &= bt_ctf_event_class_set_attribute(event_class, "loglevel", obj);
967 ok(ret,
968 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
969 BT_PUT(obj);
970 obj = bt_value_string_create_init("http://kernel.org/");
971 assert(obj);
972 assert(!bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj));
973 BT_PUT(obj);
974
975 ok(bt_ctf_event_class_get_attribute_count(NULL),
976 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
977 ok(bt_ctf_event_class_get_attribute_count(event_class) == 4,
978 "bt_ctf_event_class_get_attribute_count returns the correct count");
979 ok(!bt_ctf_event_class_get_attribute_name(NULL, 0),
980 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
981 ok(!bt_ctf_event_class_get_attribute_name(event_class, 4),
982 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
983 ok(!bt_ctf_event_class_get_attribute_value(NULL, 0),
984 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
985 ok(!bt_ctf_event_class_get_attribute_value(event_class, 4),
986 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
987
988 memset(&attrs_count, 0, sizeof(attrs_count));
989
990 for (i = 0; i < 4; ++i) {
991 ret_string = bt_ctf_event_class_get_attribute_name(event_class,
992 i);
993 obj = bt_ctf_event_class_get_attribute_value(event_class, i);
994 assert(ret_string && obj);
995
996 if (!strcmp(ret_string, "id")) {
997 attrs_count.id++;
998 ok(bt_value_is_integer(obj),
999 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1000 ret_string);
1001 } else if (!strcmp(ret_string, "name")) {
1002 attrs_count.name++;
1003 ok(bt_value_is_string(obj),
1004 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1005 ret_string);
1006 } else if (!strcmp(ret_string, "loglevel")) {
1007 attrs_count.loglevel++;
1008 ok(bt_value_is_integer(obj),
1009 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1010 ret_string);
1011 } else if (!strcmp(ret_string, "model.emf.uri")) {
1012 attrs_count.modelemfuri++;
1013 ok(bt_value_is_string(obj),
1014 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1015 ret_string);
1016 } else {
1017 attrs_count.unknown++;
1018 }
1019
1020 BT_PUT(obj);
1021 }
1022
1023 ok(attrs_count.unknown == 0, "event class has no unknown attributes");
1024 ok(attrs_count.id == 1 && attrs_count.name == 1 &&
1025 attrs_count.loglevel == 1 && attrs_count.modelemfuri == 1,
1026 "event class has one instance of each known attribute");
1027
1028 /* Add event class to the stream class */
1029 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
1030 "Reject addition of NULL event class to a stream class");
1031 ok(bt_ctf_stream_class_add_event_class(stream_class,
1032 event_class) == 0, "Add an event class to stream class");
1033
1034 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
1035 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1036 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
1037 ok(ret_stream_class == stream_class,
1038 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1039 bt_put(ret_stream_class);
1040
1041 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
1042 "bt_ctf_event_class_get_field_count handles NULL correctly");
1043 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
1044 "bt_ctf_event_class_get_field_count returns a correct value");
1045
1046 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
1047 &ret_field_type, 0) < 0,
1048 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1049 ok(bt_ctf_event_class_get_field(event_class, NULL,
1050 &ret_field_type, 0) == 0,
1051 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1052 bt_put(ret_field_type);
1053 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1054 NULL, 0) == 0,
1055 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1056 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1057 &ret_field_type, 42) < 0,
1058 "bt_ctf_event_class_get_field handles an invalid index correctly");
1059 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1060 &ret_field_type, 0) == 0,
1061 "bt_ctf_event_class_get_field returns a field");
1062 ok(bt_ctf_field_type_compare(ret_field_type, uint_35_type) == 0,
1063 "bt_ctf_event_class_get_field returns a correct field type");
1064 bt_put(ret_field_type);
1065 ok(!strcmp(ret_string, "uint_35"),
1066 "bt_ctf_event_class_get_field returns a correct field name");
1067 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
1068 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1069 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
1070 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1071 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
1072 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1073 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
1074 "complex_structure");
1075 ok(bt_ctf_field_type_compare(ret_field_type, complex_structure_type) == 0,
1076 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1077 bt_put(ret_field_type);
1078
1079 event = bt_ctf_event_create(event_class);
1080 ok(event, "Instanciate a complex event");
1081
1082 ok(bt_ctf_event_get_class(NULL) == NULL,
1083 "bt_ctf_event_get_class handles NULL correctly");
1084 ret_event_class = bt_ctf_event_get_class(event);
1085 ok(ret_event_class == event_class,
1086 "bt_ctf_event_get_class returns the correct event class");
1087 bt_put(ret_event_class);
1088
1089 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
1090 if (!uint_35_field) {
1091 printf("uint_35_field is NULL\n");
1092 }
1093
1094 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
1095 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
1096 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
1097 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1098 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
1099 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1100 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
1101 &ret_unsigned_int) == 0,
1102 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1103 ok(ret_unsigned_int == 0x0DDF00D,
1104 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1105 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
1106 &ret_signed_int) < 0,
1107 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1108 bt_put(uint_35_field);
1109
1110 int_16_field = bt_ctf_event_get_payload(event, "int_16");
1111 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
1112 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
1113 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1114 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
1115 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1116 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
1117 &ret_signed_int) == 0,
1118 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1119 ok(ret_signed_int == -12345,
1120 "bt_ctf_field_signed_integer_get_value returns the correct value");
1121 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
1122 &ret_unsigned_int) < 0,
1123 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1124 bt_put(int_16_field);
1125
1126 complex_structure_field = bt_ctf_event_get_payload(event,
1127 "complex_structure");
1128
1129 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
1130 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1131 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
1132 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1133 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
1134 complex_structure_field, 3);
1135 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
1136 bt_put(inner_structure_field);
1137 ok(bt_ctf_field_type_compare(ret_field_type, inner_structure_type) == 0,
1138 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1139 bt_put(ret_field_type);
1140
1141 inner_structure_field = bt_ctf_field_structure_get_field(
1142 complex_structure_field, "inner_structure");
1143 a_string_field = bt_ctf_field_structure_get_field(
1144 complex_structure_field, "a_string");
1145 enum_variant_field = bt_ctf_field_structure_get_field(
1146 complex_structure_field, "variant_selector");
1147 variant_field = bt_ctf_field_structure_get_field(
1148 complex_structure_field, "variant_value");
1149 uint_35_field = bt_ctf_field_structure_get_field(
1150 inner_structure_field, "seq_len");
1151 a_sequence_field = bt_ctf_field_structure_get_field(
1152 inner_structure_field, "a_sequence");
1153 an_array_field = bt_ctf_field_structure_get_field(
1154 inner_structure_field, "an_array");
1155
1156 enum_container_field = bt_ctf_field_enumeration_get_container(
1157 enum_variant_field);
1158 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
1159 int_16_field = bt_ctf_field_variant_get_field(variant_field,
1160 enum_variant_field);
1161 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
1162 bt_put(int_16_field);
1163 ok(!bt_ctf_field_string_get_value(a_string_field),
1164 "bt_ctf_field_string_get_value returns NULL on an unset field");
1165 bt_ctf_field_string_set_value(a_string_field,
1166 test_string_1);
1167 ok(!bt_ctf_field_string_get_value(NULL),
1168 "bt_ctf_field_string_get_value correctly handles NULL");
1169 ok(bt_ctf_field_string_append(NULL, "yeah"),
1170 "bt_ctf_field_string_append correctly handles a NULL string field");
1171 ok(bt_ctf_field_string_append(a_string_field, NULL),
1172 "bt_ctf_field_string_append correctly handles a NULL string value");
1173 ok(!bt_ctf_field_string_append(a_string_field, test_string_2),
1174 "bt_ctf_field_string_append succeeds");
1175 ok(bt_ctf_field_string_append_len(NULL, "oh noes", 3),
1176 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1177 ok(bt_ctf_field_string_append_len(a_string_field, NULL, 3),
1178 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1179 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5),
1180 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1181 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_4, 10),
1182 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1183 ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[4], 3),
1184 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1185 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0),
1186 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1187
1188 ret_string = bt_ctf_field_string_get_value(a_string_field);
1189 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
1190 ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0,
1191 "bt_ctf_field_string_get_value returns a correct value");
1192 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
1193 SEQUENCE_TEST_LENGTH);
1194
1195 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
1196 enum_container_field) == NULL,
1197 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1198 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
1199 NULL) == NULL,
1200 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1201 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
1202 variant_type, enum_variant_field);
1203 ok(ret_field_type == int_16_type,
1204 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1205
1206 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
1207 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1208 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
1209 uint_35_field) == 0, "Set a sequence field's length");
1210 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
1211 ok(ret_field == uint_35_field,
1212 "bt_ctf_field_sequence_get_length returns the correct length field");
1213 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
1214 "bt_ctf_field_sequence_get_length properly handles NULL");
1215
1216 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
1217 int_16_field = bt_ctf_field_sequence_get_field(
1218 a_sequence_field, i);
1219 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
1220 bt_put(int_16_field);
1221 }
1222
1223 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
1224 int_16_field = bt_ctf_field_array_get_field(
1225 an_array_field, i);
1226 bt_ctf_field_signed_integer_set_value(int_16_field, i);
1227 bt_put(int_16_field);
1228 }
1229
1230 stream_event_ctx_field = bt_ctf_event_get_stream_event_context(event);
1231 assert(stream_event_ctx_field);
1232 stream_event_ctx_int_field = bt_ctf_field_structure_get_field(
1233 stream_event_ctx_field, "common_event_context");
1234 BT_PUT(stream_event_ctx_field);
1235 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field, 17);
1236 BT_PUT(stream_event_ctx_int_field);
1237
1238 bt_ctf_clock_set_time(clock, ++current_time);
1239 ok(bt_ctf_stream_append_event(stream, event) == 0,
1240 "Append a complex event to a stream");
1241
1242 /*
1243 * Populate the custom packet context field with a dummy value
1244 * otherwise flush will fail.
1245 */
1246 packet_context = bt_ctf_stream_get_packet_context(stream);
1247 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1248 "custom_packet_context_field");
1249 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
1250
1251 ok(bt_ctf_stream_flush(stream) == 0,
1252 "Flush a stream containing a complex event");
1253
1254 bt_put(uint_35_field);
1255 bt_put(a_string_field);
1256 bt_put(inner_structure_field);
1257 bt_put(complex_structure_field);
1258 bt_put(a_sequence_field);
1259 bt_put(an_array_field);
1260 bt_put(enum_variant_field);
1261 bt_put(enum_container_field);
1262 bt_put(variant_field);
1263 bt_put(ret_field);
1264 bt_put(packet_context_field);
1265 bt_put(packet_context);
1266 bt_put(uint_35_type);
1267 bt_put(int_16_type);
1268 bt_put(string_type);
1269 bt_put(sequence_type);
1270 bt_put(array_type);
1271 bt_put(inner_structure_type);
1272 bt_put(complex_structure_type);
1273 bt_put(uint_3_type);
1274 bt_put(enum_variant_type);
1275 bt_put(variant_type);
1276 bt_put(ret_field_type);
1277 bt_put(event_class);
1278 bt_put(event);
1279 }
1280
1281 static void field_copy_tests_validate_same_type(struct bt_ctf_field *field,
1282 struct bt_ctf_field_type *expected_type, const char *name)
1283 {
1284 struct bt_ctf_field_type *copy_type;
1285
1286 copy_type = bt_ctf_field_get_type(field);
1287 ok(copy_type == expected_type,
1288 "bt_ctf_field_copy does not copy the type (%s)", name);
1289 bt_put(copy_type);
1290 }
1291
1292 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field *field_a,
1293 struct bt_ctf_field *field_b, const char *name)
1294 {
1295 ok(field_a != field_b,
1296 "bt_ctf_field_copy creates different pointers (%s)", name);
1297 }
1298
1299 void field_copy_tests()
1300 {
1301 struct bt_ctf_field_type *len_type = NULL;
1302 struct bt_ctf_field_type *fp_type = NULL;
1303 struct bt_ctf_field_type *s_type = NULL;
1304 struct bt_ctf_field_type *e_int_type = NULL;
1305 struct bt_ctf_field_type *e_type = NULL;
1306 struct bt_ctf_field_type *v_type = NULL;
1307 struct bt_ctf_field_type *v_label1_type = NULL;
1308 struct bt_ctf_field_type *v_label1_array_type = NULL;
1309 struct bt_ctf_field_type *v_label2_type = NULL;
1310 struct bt_ctf_field_type *v_label2_seq_type = NULL;
1311 struct bt_ctf_field_type *strct_type = NULL;
1312 struct bt_ctf_field *len = NULL;
1313 struct bt_ctf_field *fp = NULL;
1314 struct bt_ctf_field *s = NULL;
1315 struct bt_ctf_field *e_int = NULL;
1316 struct bt_ctf_field *e = NULL;
1317 struct bt_ctf_field *v = NULL;
1318 struct bt_ctf_field *v_selected = NULL;
1319 struct bt_ctf_field *v_selected_cur = NULL;
1320 struct bt_ctf_field *v_selected_0 = NULL;
1321 struct bt_ctf_field *v_selected_1 = NULL;
1322 struct bt_ctf_field *v_selected_2 = NULL;
1323 struct bt_ctf_field *v_selected_3 = NULL;
1324 struct bt_ctf_field *v_selected_4 = NULL;
1325 struct bt_ctf_field *v_selected_5 = NULL;
1326 struct bt_ctf_field *v_selected_6 = NULL;
1327 struct bt_ctf_field *a = NULL;
1328 struct bt_ctf_field *a_0 = NULL;
1329 struct bt_ctf_field *a_1 = NULL;
1330 struct bt_ctf_field *a_2 = NULL;
1331 struct bt_ctf_field *a_3 = NULL;
1332 struct bt_ctf_field *a_4 = NULL;
1333 struct bt_ctf_field *strct = NULL;
1334 struct bt_ctf_field *len_copy = NULL;
1335 struct bt_ctf_field *fp_copy = NULL;
1336 struct bt_ctf_field *s_copy = NULL;
1337 struct bt_ctf_field *e_int_copy = NULL;
1338 struct bt_ctf_field *e_copy = NULL;
1339 struct bt_ctf_field *v_copy = NULL;
1340 struct bt_ctf_field *v_selected_copy = NULL;
1341 struct bt_ctf_field *v_selected_copy_len = NULL;
1342 struct bt_ctf_field *v_selected_0_copy = NULL;
1343 struct bt_ctf_field *v_selected_1_copy = NULL;
1344 struct bt_ctf_field *v_selected_2_copy = NULL;
1345 struct bt_ctf_field *v_selected_3_copy = NULL;
1346 struct bt_ctf_field *v_selected_4_copy = NULL;
1347 struct bt_ctf_field *v_selected_5_copy = NULL;
1348 struct bt_ctf_field *v_selected_6_copy = NULL;
1349 struct bt_ctf_field *a_copy = NULL;
1350 struct bt_ctf_field *a_0_copy = NULL;
1351 struct bt_ctf_field *a_1_copy = NULL;
1352 struct bt_ctf_field *a_2_copy = NULL;
1353 struct bt_ctf_field *a_3_copy = NULL;
1354 struct bt_ctf_field *a_4_copy = NULL;
1355 struct bt_ctf_field *strct_copy = NULL;
1356 uint64_t uint64_t_val;
1357 const char *str_val;
1358 double double_val;
1359 int ret;
1360
1361 /* create len type */
1362 len_type = bt_ctf_field_type_integer_create(32);
1363 assert(len_type);
1364
1365 /* create fp type */
1366 fp_type = bt_ctf_field_type_floating_point_create();
1367 assert(fp_type);
1368
1369 /* create s type */
1370 s_type = bt_ctf_field_type_string_create();
1371 assert(s_type);
1372
1373 /* create e_int type */
1374 e_int_type = bt_ctf_field_type_integer_create(8);
1375 assert(e_int_type);
1376
1377 /* create e type */
1378 e_type = bt_ctf_field_type_enumeration_create(e_int_type);
1379 assert(e_type);
1380 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL1",
1381 10, 15);
1382 assert(!ret);
1383 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL2",
1384 23, 23);
1385 assert(!ret);
1386
1387 /* create v_label1 type */
1388 v_label1_type = bt_ctf_field_type_string_create();
1389 assert(v_label1_type);
1390
1391 /* create v_label1_array type */
1392 v_label1_array_type = bt_ctf_field_type_array_create(v_label1_type, 5);
1393 assert(v_label1_array_type);
1394
1395 /* create v_label2 type */
1396 v_label2_type = bt_ctf_field_type_integer_create(16);
1397 assert(v_label2_type);
1398
1399 /* create v_label2_seq type */
1400 v_label2_seq_type = bt_ctf_field_type_sequence_create(v_label2_type,
1401 "len");
1402 assert(v_label2_seq_type);
1403
1404 /* create v type */
1405 v_type = bt_ctf_field_type_variant_create(e_type, "e");
1406 assert(v_type);
1407 ret = bt_ctf_field_type_variant_add_field(v_type, v_label1_array_type,
1408 "LABEL1");
1409 assert(!ret);
1410 ret = bt_ctf_field_type_variant_add_field(v_type, v_label2_seq_type,
1411 "LABEL2");
1412 assert(!ret);
1413
1414 /* create strct type */
1415 strct_type = bt_ctf_field_type_structure_create();
1416 assert(strct_type);
1417 ret = bt_ctf_field_type_structure_add_field(strct_type, len_type,
1418 "len");
1419 assert(!ret);
1420 ret = bt_ctf_field_type_structure_add_field(strct_type, fp_type, "fp");
1421 assert(!ret);
1422 ret = bt_ctf_field_type_structure_add_field(strct_type, s_type, "s");
1423 assert(!ret);
1424 ret = bt_ctf_field_type_structure_add_field(strct_type, e_type, "e");
1425 assert(!ret);
1426 ret = bt_ctf_field_type_structure_add_field(strct_type, v_type, "v");
1427 assert(!ret);
1428 ret = bt_ctf_field_type_structure_add_field(strct_type,
1429 v_label1_array_type, "a");
1430 assert(!ret);
1431
1432 /* create strct */
1433 strct = bt_ctf_field_create(strct_type);
1434 assert(strct);
1435
1436 /* get len field */
1437 len = bt_ctf_field_structure_get_field(strct, "len");
1438 assert(len);
1439
1440 /* get fp field */
1441 fp = bt_ctf_field_structure_get_field(strct, "fp");
1442 assert(fp);
1443
1444 /* get s field */
1445 s = bt_ctf_field_structure_get_field(strct, "s");
1446 assert(s);
1447
1448 /* get e field */
1449 e = bt_ctf_field_structure_get_field(strct, "e");
1450 assert(e);
1451
1452 /* get e_int (underlying integer) */
1453 e_int = bt_ctf_field_enumeration_get_container(e);
1454 assert(e_int);
1455
1456 /* get v field */
1457 v = bt_ctf_field_structure_get_field(strct, "v");
1458 assert(v);
1459
1460 /* get a field */
1461 a = bt_ctf_field_structure_get_field(strct, "a");
1462 assert(a);
1463
1464 /* set len field */
1465 ret = bt_ctf_field_unsigned_integer_set_value(len, 7);
1466 assert(!ret);
1467
1468 /* set fp field */
1469 ret = bt_ctf_field_floating_point_set_value(fp, 3.14);
1470 assert(!ret);
1471
1472 /* set s field */
1473 ret = bt_ctf_field_string_set_value(s, "btbt");
1474 assert(!ret);
1475
1476 /* set e field (LABEL2) */
1477 ret = bt_ctf_field_unsigned_integer_set_value(e_int, 23);
1478 assert(!ret);
1479
1480 /* set v field */
1481 v_selected = bt_ctf_field_variant_get_field(v, e);
1482 assert(v_selected);
1483 ok(!bt_ctf_field_variant_get_current_field(NULL),
1484 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1485 v_selected_cur = bt_ctf_field_variant_get_current_field(v);
1486 ok(v_selected_cur == v_selected,
1487 "bt_ctf_field_variant_get_current_field returns the current field");
1488 bt_put(v_selected_cur);
1489
1490 /* set selected v field */
1491 ret = bt_ctf_field_sequence_set_length(v_selected, len);
1492 assert(!ret);
1493 v_selected_0 = bt_ctf_field_sequence_get_field(v_selected, 0);
1494 assert(v_selected_0);
1495 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_0, 7);
1496 assert(!ret);
1497 v_selected_1 = bt_ctf_field_sequence_get_field(v_selected, 1);
1498 assert(v_selected_1);
1499 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_1, 6);
1500 assert(!ret);
1501 v_selected_2 = bt_ctf_field_sequence_get_field(v_selected, 2);
1502 assert(v_selected_2);
1503 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_2, 5);
1504 assert(!ret);
1505 v_selected_3 = bt_ctf_field_sequence_get_field(v_selected, 3);
1506 assert(v_selected_3);
1507 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_3, 4);
1508 assert(!ret);
1509 v_selected_4 = bt_ctf_field_sequence_get_field(v_selected, 4);
1510 assert(v_selected_4);
1511 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_4, 3);
1512 assert(!ret);
1513 v_selected_5 = bt_ctf_field_sequence_get_field(v_selected, 5);
1514 assert(v_selected_5);
1515 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_5, 2);
1516 assert(!ret);
1517 v_selected_6 = bt_ctf_field_sequence_get_field(v_selected, 6);
1518 assert(v_selected_6);
1519 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_6, 1);
1520 assert(!ret);
1521
1522 /* set a field */
1523 a_0 = bt_ctf_field_array_get_field(a, 0);
1524 assert(a_0);
1525 ret = bt_ctf_field_string_set_value(a_0, "a_0");
1526 assert(!ret);
1527 a_1 = bt_ctf_field_array_get_field(a, 1);
1528 assert(a_1);
1529 ret = bt_ctf_field_string_set_value(a_1, "a_1");
1530 assert(!ret);
1531 a_2 = bt_ctf_field_array_get_field(a, 2);
1532 assert(a_2);
1533 ret = bt_ctf_field_string_set_value(a_2, "a_2");
1534 assert(!ret);
1535 a_3 = bt_ctf_field_array_get_field(a, 3);
1536 assert(a_3);
1537 ret = bt_ctf_field_string_set_value(a_3, "a_3");
1538 assert(!ret);
1539 a_4 = bt_ctf_field_array_get_field(a, 4);
1540 assert(a_4);
1541 ret = bt_ctf_field_string_set_value(a_4, "a_4");
1542 assert(!ret);
1543
1544 /* create copy of strct */
1545 ok(!bt_ctf_field_copy(NULL),
1546 "bt_ctf_field_copy handles NULL correctly");
1547 strct_copy = bt_ctf_field_copy(strct);
1548 ok(strct_copy,
1549 "bt_ctf_field_copy returns a valid pointer");
1550
1551 /* get all copied fields */
1552 len_copy = bt_ctf_field_structure_get_field(strct_copy, "len");
1553 assert(len_copy);
1554 fp_copy = bt_ctf_field_structure_get_field(strct_copy, "fp");
1555 assert(fp_copy);
1556 s_copy = bt_ctf_field_structure_get_field(strct_copy, "s");
1557 assert(s_copy);
1558 e_copy = bt_ctf_field_structure_get_field(strct_copy, "e");
1559 assert(e_copy);
1560 e_int_copy = bt_ctf_field_enumeration_get_container(e_copy);
1561 assert(e_int_copy);
1562 v_copy = bt_ctf_field_structure_get_field(strct_copy, "v");
1563 assert(v_copy);
1564 v_selected_copy = bt_ctf_field_variant_get_field(v_copy, e_copy);
1565 assert(v_selected_copy);
1566 v_selected_0_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 0);
1567 assert(v_selected_0_copy);
1568 v_selected_1_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 1);
1569 assert(v_selected_1_copy);
1570 v_selected_2_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 2);
1571 assert(v_selected_2_copy);
1572 v_selected_3_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 3);
1573 assert(v_selected_3_copy);
1574 v_selected_4_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 4);
1575 assert(v_selected_4_copy);
1576 v_selected_5_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 5);
1577 assert(v_selected_5_copy);
1578 v_selected_6_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 6);
1579 assert(v_selected_6_copy);
1580 ok(!bt_ctf_field_sequence_get_field(v_selected_copy, 7),
1581 "sequence field copy is not too large");
1582 a_copy = bt_ctf_field_structure_get_field(strct_copy, "a");
1583 assert(a_copy);
1584 a_0_copy = bt_ctf_field_array_get_field(a_copy, 0);
1585 assert(a_0_copy);
1586 a_1_copy = bt_ctf_field_array_get_field(a_copy, 1);
1587 assert(a_1_copy);
1588 a_2_copy = bt_ctf_field_array_get_field(a_copy, 2);
1589 assert(a_2_copy);
1590 a_3_copy = bt_ctf_field_array_get_field(a_copy, 3);
1591 assert(a_3_copy);
1592 a_4_copy = bt_ctf_field_array_get_field(a_copy, 4);
1593 assert(a_4_copy);
1594 ok(!bt_ctf_field_array_get_field(v_selected_copy, 5),
1595 "array field copy is not too large");
1596
1597 /* make sure copied fields are different pointers */
1598 field_copy_tests_validate_diff_ptrs(strct_copy, strct, "strct");
1599 field_copy_tests_validate_diff_ptrs(len_copy, len, "len");
1600 field_copy_tests_validate_diff_ptrs(fp_copy, fp, "fp");
1601 field_copy_tests_validate_diff_ptrs(s_copy, s, "s");
1602 field_copy_tests_validate_diff_ptrs(e_int_copy, e_int, "e_int");
1603 field_copy_tests_validate_diff_ptrs(e_copy, e, "e");
1604 field_copy_tests_validate_diff_ptrs(v_copy, v, "v");
1605 field_copy_tests_validate_diff_ptrs(v_selected_copy, v_selected,
1606 "v_selected");
1607 field_copy_tests_validate_diff_ptrs(v_selected_0_copy, v_selected_0,
1608 "v_selected_0");
1609 field_copy_tests_validate_diff_ptrs(v_selected_1_copy, v_selected_1,
1610 "v_selected_1");
1611 field_copy_tests_validate_diff_ptrs(v_selected_2_copy, v_selected_2,
1612 "v_selected_2");
1613 field_copy_tests_validate_diff_ptrs(v_selected_3_copy, v_selected_3,
1614 "v_selected_3");
1615 field_copy_tests_validate_diff_ptrs(v_selected_4_copy, v_selected_4,
1616 "v_selected_4");
1617 field_copy_tests_validate_diff_ptrs(v_selected_5_copy, v_selected_5,
1618 "v_selected_5");
1619 field_copy_tests_validate_diff_ptrs(v_selected_6_copy, v_selected_6,
1620 "v_selected_6");
1621 field_copy_tests_validate_diff_ptrs(a_copy, a, "a");
1622 field_copy_tests_validate_diff_ptrs(a_0_copy, a_0, "a_0");
1623 field_copy_tests_validate_diff_ptrs(a_1_copy, a_1, "a_1");
1624 field_copy_tests_validate_diff_ptrs(a_2_copy, a_2, "a_2");
1625 field_copy_tests_validate_diff_ptrs(a_3_copy, a_3, "a_3");
1626 field_copy_tests_validate_diff_ptrs(a_4_copy, a_4, "a_4");
1627
1628 /* make sure copied fields share the same types */
1629 field_copy_tests_validate_same_type(strct_copy, strct_type, "strct");
1630 field_copy_tests_validate_same_type(len_copy, len_type, "len");
1631 field_copy_tests_validate_same_type(fp_copy, fp_type, "fp");
1632 field_copy_tests_validate_same_type(e_int_copy, e_int_type, "e_int");
1633 field_copy_tests_validate_same_type(e_copy, e_type, "e");
1634 field_copy_tests_validate_same_type(v_copy, v_type, "v");
1635 field_copy_tests_validate_same_type(v_selected_copy, v_label2_seq_type,
1636 "v_selected");
1637 field_copy_tests_validate_same_type(v_selected_0_copy, v_label2_type,
1638 "v_selected_0");
1639 field_copy_tests_validate_same_type(v_selected_1_copy, v_label2_type,
1640 "v_selected_1");
1641 field_copy_tests_validate_same_type(v_selected_2_copy, v_label2_type,
1642 "v_selected_2");
1643 field_copy_tests_validate_same_type(v_selected_3_copy, v_label2_type,
1644 "v_selected_3");
1645 field_copy_tests_validate_same_type(v_selected_4_copy, v_label2_type,
1646 "v_selected_4");
1647 field_copy_tests_validate_same_type(v_selected_5_copy, v_label2_type,
1648 "v_selected_5");
1649 field_copy_tests_validate_same_type(v_selected_6_copy, v_label2_type,
1650 "v_selected_6");
1651 field_copy_tests_validate_same_type(a_copy, v_label1_array_type, "a");
1652 field_copy_tests_validate_same_type(a_0_copy, v_label1_type, "a_0");
1653 field_copy_tests_validate_same_type(a_1_copy, v_label1_type, "a_1");
1654 field_copy_tests_validate_same_type(a_2_copy, v_label1_type, "a_2");
1655 field_copy_tests_validate_same_type(a_3_copy, v_label1_type, "a_3");
1656 field_copy_tests_validate_same_type(a_4_copy, v_label1_type, "a_4");
1657
1658 /* validate len copy */
1659 ret = bt_ctf_field_unsigned_integer_get_value(len_copy, &uint64_t_val);
1660 assert(!ret);
1661 ok(uint64_t_val == 7,
1662 "bt_ctf_field_copy creates a valid integer field copy");
1663
1664 /* validate fp copy */
1665 ret = bt_ctf_field_floating_point_get_value(fp_copy, &double_val);
1666 assert(!ret);
1667 ok(double_val == 3.14,
1668 "bt_ctf_field_copy creates a valid floating point number field copy");
1669
1670 /* validate s copy */
1671 str_val = bt_ctf_field_string_get_value(s_copy);
1672 ok(str_val && !strcmp(str_val, "btbt"),
1673 "bt_ctf_field_copy creates a valid string field copy");
1674
1675 /* validate e_int copy */
1676 ret = bt_ctf_field_unsigned_integer_get_value(e_int_copy,
1677 &uint64_t_val);
1678 assert(!ret);
1679 ok(uint64_t_val == 23,
1680 "bt_ctf_field_copy creates a valid enum's integer field copy");
1681
1682 /* validate e copy */
1683 str_val = bt_ctf_field_enumeration_get_mapping_name(e_copy);
1684 ok(str_val && !strcmp(str_val, "LABEL2"),
1685 "bt_ctf_field_copy creates a valid enum field copy");
1686
1687 /* validate v_selected copy */
1688 v_selected_copy_len = bt_ctf_field_sequence_get_length(v_selected);
1689 assert(v_selected_copy_len);
1690 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len,
1691 &uint64_t_val);
1692 assert(!ret);
1693 ok(uint64_t_val == 7,
1694 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1695 bt_put(v_selected_copy_len);
1696 v_selected_copy_len = NULL;
1697
1698 /* validate v_selected copy fields */
1699 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy,
1700 &uint64_t_val);
1701 assert(!ret);
1702 ok(uint64_t_val == 7,
1703 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1704 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy,
1705 &uint64_t_val);
1706 assert(!ret);
1707 ok(uint64_t_val == 6,
1708 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1709 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy,
1710 &uint64_t_val);
1711 assert(!ret);
1712 ok(uint64_t_val == 5,
1713 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1714 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy,
1715 &uint64_t_val);
1716 assert(!ret);
1717 ok(uint64_t_val == 4,
1718 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1719 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy,
1720 &uint64_t_val);
1721 assert(!ret);
1722 ok(uint64_t_val == 3,
1723 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1724 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy,
1725 &uint64_t_val);
1726 assert(!ret);
1727 ok(uint64_t_val == 2,
1728 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1729 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy,
1730 &uint64_t_val);
1731 assert(!ret);
1732 ok(uint64_t_val == 1,
1733 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1734
1735 /* validate a copy fields */
1736 str_val = bt_ctf_field_string_get_value(a_0_copy);
1737 ok(str_val && !strcmp(str_val, "a_0"),
1738 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1739 str_val = bt_ctf_field_string_get_value(a_1_copy);
1740 ok(str_val && !strcmp(str_val, "a_1"),
1741 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1742 str_val = bt_ctf_field_string_get_value(a_2_copy);
1743 ok(str_val && !strcmp(str_val, "a_2"),
1744 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1745 str_val = bt_ctf_field_string_get_value(a_3_copy);
1746 ok(str_val && !strcmp(str_val, "a_3"),
1747 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1748 str_val = bt_ctf_field_string_get_value(a_4_copy);
1749 ok(str_val && !strcmp(str_val, "a_4"),
1750 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1751
1752 /* put everything */
1753 bt_put(len_type);
1754 bt_put(fp_type);
1755 bt_put(s_type);
1756 bt_put(e_int_type);
1757 bt_put(e_type);
1758 bt_put(v_type);
1759 bt_put(v_label1_type);
1760 bt_put(v_label1_array_type);
1761 bt_put(v_label2_type);
1762 bt_put(v_label2_seq_type);
1763 bt_put(strct_type);
1764 bt_put(len);
1765 bt_put(fp);
1766 bt_put(s);
1767 bt_put(e_int);
1768 bt_put(e);
1769 bt_put(v);
1770 bt_put(v_selected);
1771 bt_put(v_selected_0);
1772 bt_put(v_selected_1);
1773 bt_put(v_selected_2);
1774 bt_put(v_selected_3);
1775 bt_put(v_selected_4);
1776 bt_put(v_selected_5);
1777 bt_put(v_selected_6);
1778 bt_put(a);
1779 bt_put(a_0);
1780 bt_put(a_1);
1781 bt_put(a_2);
1782 bt_put(a_3);
1783 bt_put(a_4);
1784 bt_put(strct);
1785 bt_put(len_copy);
1786 bt_put(fp_copy);
1787 bt_put(s_copy);
1788 bt_put(e_int_copy);
1789 bt_put(e_copy);
1790 bt_put(v_copy);
1791 bt_put(v_selected_copy);
1792 bt_put(v_selected_0_copy);
1793 bt_put(v_selected_1_copy);
1794 bt_put(v_selected_2_copy);
1795 bt_put(v_selected_3_copy);
1796 bt_put(v_selected_4_copy);
1797 bt_put(v_selected_5_copy);
1798 bt_put(v_selected_6_copy);
1799 bt_put(a_copy);
1800 bt_put(a_0_copy);
1801 bt_put(a_1_copy);
1802 bt_put(a_2_copy);
1803 bt_put(a_3_copy);
1804 bt_put(a_4_copy);
1805 bt_put(strct_copy);
1806 }
1807
1808 void type_field_tests()
1809 {
1810 struct bt_ctf_field *uint_12;
1811 struct bt_ctf_field *int_16;
1812 struct bt_ctf_field *string;
1813 struct bt_ctf_field *enumeration;
1814 struct bt_ctf_field_type *composite_structure_type;
1815 struct bt_ctf_field_type *structure_seq_type;
1816 struct bt_ctf_field_type *string_type;
1817 struct bt_ctf_field_type *sequence_type;
1818 struct bt_ctf_field_type *uint_8_type;
1819 struct bt_ctf_field_type *int_16_type;
1820 struct bt_ctf_field_type *uint_12_type =
1821 bt_ctf_field_type_integer_create(12);
1822 struct bt_ctf_field_type *enumeration_type;
1823 struct bt_ctf_field_type *returned_type;
1824 const char *ret_string;
1825
1826 returned_type = bt_ctf_field_get_type(NULL);
1827 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
1828
1829 ok(uint_12_type, "Create an unsigned integer type");
1830 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1831 BT_CTF_INTEGER_BASE_BINARY) == 0,
1832 "Set integer type's base as binary");
1833 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1834 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1835 "Set integer type's base as decimal");
1836 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1837 BT_CTF_INTEGER_BASE_UNKNOWN),
1838 "Reject integer type's base set as unknown");
1839 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1840 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1841 "Set integer type's base as octal");
1842 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1843 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1844 "Set integer type's base as hexadecimal");
1845 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1846 "Reject unknown integer base value");
1847 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1848 "Set integer type signedness to signed");
1849 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1850 "Set integer type signedness to unsigned");
1851 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1852 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1853 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1854 "bt_ctf_field_type_integer_get_size returns a correct value");
1855 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1856 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1857 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1858 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1859
1860 ok(bt_ctf_field_type_set_byte_order(NULL,
1861 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1862 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1863 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1864 (enum bt_ctf_byte_order) 42) < 0,
1865 "bt_ctf_field_type_set_byte_order rejects invalid values");
1866 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1867 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1868 "Set an integer's byte order to little endian");
1869 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1870 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1871 "Set an integer's byte order to big endian");
1872 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1873 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1874 "bt_ctf_field_type_get_byte_order returns a correct value");
1875 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1876 BT_CTF_BYTE_ORDER_UNKNOWN,
1877 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1878
1879 ok(bt_ctf_field_type_get_type_id(NULL) ==
1880 BT_CTF_TYPE_ID_UNKNOWN,
1881 "bt_ctf_field_type_get_type_id handles NULL correctly");
1882 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1883 BT_CTF_TYPE_ID_INTEGER,
1884 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1885
1886 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1887 BT_CTF_INTEGER_BASE_UNKNOWN,
1888 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1889 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1890 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1891 "bt_ctf_field_type_integer_get_base returns a correct value");
1892
1893 ok(bt_ctf_field_type_integer_set_encoding(NULL,
1894 BT_CTF_STRING_ENCODING_ASCII) < 0,
1895 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1896 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1897 (enum bt_ctf_string_encoding) 123) < 0,
1898 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1899 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1900 BT_CTF_STRING_ENCODING_UTF8) == 0,
1901 "Set integer type encoding to UTF8");
1902 ok(bt_ctf_field_type_integer_get_encoding(NULL) ==
1903 BT_CTF_STRING_ENCODING_UNKNOWN,
1904 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1905 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) ==
1906 BT_CTF_STRING_ENCODING_UTF8,
1907 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1908
1909 int_16_type = bt_ctf_field_type_integer_create(16);
1910 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
1911 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1912 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1913 uint_8_type = bt_ctf_field_type_integer_create(8);
1914 sequence_type =
1915 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1916 ok(sequence_type, "Create a sequence of int16_t type");
1917 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1918 BT_CTF_TYPE_ID_SEQUENCE,
1919 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1920
1921 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1922 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1923 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1924 sequence_type);
1925 ok(!strcmp(ret_string, "seq_len"),
1926 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1927 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1928 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1929 returned_type = bt_ctf_field_type_sequence_get_element_type(
1930 sequence_type);
1931 ok(returned_type == int_16_type,
1932 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1933 bt_put(returned_type);
1934
1935 string_type = bt_ctf_field_type_string_create();
1936 ok(string_type, "Create a string type");
1937 ok(bt_ctf_field_type_string_set_encoding(string_type,
1938 BT_CTF_STRING_ENCODING_NONE),
1939 "Reject invalid \"None\" string encoding");
1940 ok(bt_ctf_field_type_string_set_encoding(string_type,
1941 42),
1942 "Reject invalid string encoding");
1943 ok(bt_ctf_field_type_string_set_encoding(string_type,
1944 BT_CTF_STRING_ENCODING_ASCII) == 0,
1945 "Set string encoding to ASCII");
1946
1947 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1948 BT_CTF_STRING_ENCODING_UNKNOWN,
1949 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1950 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1951 BT_CTF_STRING_ENCODING_ASCII,
1952 "bt_ctf_field_type_string_get_encoding returns the correct value");
1953
1954 structure_seq_type = bt_ctf_field_type_structure_create();
1955 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1956 BT_CTF_TYPE_ID_STRUCT,
1957 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1958 ok(structure_seq_type, "Create a structure type");
1959 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1960 uint_8_type, "seq_len") == 0,
1961 "Add a uint8_t type to a structure");
1962 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1963 sequence_type, "a_sequence") == 0,
1964 "Add a sequence type to a structure");
1965
1966 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1967 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1968 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1969 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1970
1971 ok(bt_ctf_field_type_structure_get_field(NULL,
1972 &ret_string, &returned_type, 1) < 0,
1973 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1974 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1975 NULL, &returned_type, 1) == 0,
1976 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1977 bt_put(returned_type);
1978 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1979 &ret_string, NULL, 1) == 0,
1980 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1981 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1982 &ret_string, &returned_type, 10) < 0,
1983 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1984 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1985 &ret_string, &returned_type, 1) == 0,
1986 "bt_ctf_field_type_structure_get_field returns a field");
1987 ok(!strcmp(ret_string, "a_sequence"),
1988 "bt_ctf_field_type_structure_get_field returns a correct field name");
1989 ok(returned_type == sequence_type,
1990 "bt_ctf_field_type_structure_get_field returns a correct field type");
1991 bt_put(returned_type);
1992
1993 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
1994 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1995 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
1996 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1997 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1998 structure_seq_type, "a_sequence");
1999 ok(returned_type == sequence_type,
2000 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2001 bt_put(returned_type);
2002
2003 composite_structure_type = bt_ctf_field_type_structure_create();
2004 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2005 string_type, "a_string") == 0,
2006 "Add a string type to a structure");
2007 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2008 structure_seq_type, "inner_structure") == 0,
2009 "Add a structure type to a structure");
2010
2011 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2012 NULL, "a_sequence") == NULL,
2013 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2014 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2015 structure_seq_type, NULL) == NULL,
2016 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2017 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2018 structure_seq_type, "a_sequence");
2019 ok(returned_type == sequence_type,
2020 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2021 bt_put(returned_type);
2022
2023 int_16 = bt_ctf_field_create(int_16_type);
2024 ok(int_16, "Instanciate a signed 16-bit integer");
2025 uint_12 = bt_ctf_field_create(uint_12_type);
2026 ok(uint_12, "Instanciate an unsigned 12-bit integer");
2027 returned_type = bt_ctf_field_get_type(int_16);
2028 ok(returned_type == int_16_type,
2029 "bt_ctf_field_get_type returns the correct type");
2030
2031 /* Can't modify types after instanciating them */
2032 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
2033 BT_CTF_INTEGER_BASE_DECIMAL),
2034 "Check an integer type' base can't be modified after instanciation");
2035 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
2036 "Check an integer type's signedness can't be modified after instanciation");
2037
2038 /* Check signed property is checked */
2039 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
2040 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2041 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
2042 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2043
2044 /* Check overflows are properly tested for */
2045 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
2046 "Check -32768 is allowed for a signed 16-bit integer");
2047 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
2048 "Check 32767 is allowed for a signed 16-bit integer");
2049 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
2050 "Check 32768 is not allowed for a signed 16-bit integer");
2051 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
2052 "Check -32769 is not allowed for a signed 16-bit integer");
2053 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
2054 "Check -42 is allowed for a signed 16-bit integer");
2055
2056 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
2057 "Check 4095 is allowed for an unsigned 12-bit integer");
2058 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
2059 "Check 4096 is not allowed for a unsigned 12-bit integer");
2060 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
2061 "Check 0 is allowed for an unsigned 12-bit integer");
2062
2063 string = bt_ctf_field_create(string_type);
2064 ok(string, "Instanciate a string field");
2065 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
2066 "Set a string's value");
2067
2068 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
2069 ok(enumeration_type,
2070 "Create an enumeration type with an unsigned 12-bit integer as container");
2071 enumeration = bt_ctf_field_create(enumeration_type);
2072 ok(!enumeration,
2073 "Check enumeration types are validated before instantiation");
2074
2075 bt_put(string);
2076 bt_put(uint_12);
2077 bt_put(int_16);
2078 bt_put(enumeration);
2079 bt_put(composite_structure_type);
2080 bt_put(structure_seq_type);
2081 bt_put(string_type);
2082 bt_put(sequence_type);
2083 bt_put(uint_8_type);
2084 bt_put(int_16_type);
2085 bt_put(uint_12_type);
2086 bt_put(enumeration_type);
2087 bt_put(returned_type);
2088 }
2089
2090 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
2091 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
2092 {
2093 /*
2094 * Append enough events to force the underlying packet to be resized.
2095 * Also tests that a new event can be declared after a stream has been
2096 * instantiated and used/flushed.
2097 */
2098 int ret = 0;
2099 int i;
2100 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
2101 "Spammy_Event");
2102 struct bt_ctf_field_type *integer_type =
2103 bt_ctf_field_type_integer_create(17);
2104 struct bt_ctf_field_type *string_type =
2105 bt_ctf_field_type_string_create();
2106 struct bt_ctf_event *event = NULL;
2107 struct bt_ctf_field *ret_field = NULL;
2108 struct bt_ctf_field_type *ret_field_type = NULL;
2109 uint64_t ret_uint64;
2110 int events_appended = 0;
2111 struct bt_ctf_field *packet_context = NULL,
2112 *packet_context_field = NULL, *stream_event_context = NULL;
2113 struct bt_ctf_field_type *ep_field_1_type = NULL;
2114 struct bt_ctf_field_type *ep_a_string_type = NULL;
2115 struct bt_ctf_field_type *ep_type = NULL;
2116
2117 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
2118 "field_1");
2119 ret |= bt_ctf_event_class_add_field(event_class, string_type,
2120 "a_string");
2121 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
2122 ok(ret == 0, "Add a new event class to a stream class after writing an event");
2123 if (ret) {
2124 goto end;
2125 }
2126
2127 /*
2128 * bt_ctf_stream_class_add_event_class() copies the field types
2129 * of event_class, so we retrieve the new ones to create the
2130 * appropriate fields.
2131 */
2132 ep_type = bt_ctf_event_class_get_payload_type(event_class);
2133 assert(ep_type);
2134 ep_field_1_type = bt_ctf_field_type_structure_get_field_type_by_name(
2135 ep_type, "field_1");
2136 assert(ep_field_1_type);
2137 ep_a_string_type = bt_ctf_field_type_structure_get_field_type_by_name(
2138 ep_type, "a_string");
2139 assert(ep_a_string_type);
2140
2141 event = bt_ctf_event_create(event_class);
2142 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
2143 ret_field_type = bt_ctf_field_get_type(ret_field);
2144 ok(bt_ctf_field_type_compare(ret_field_type, integer_type) == 0,
2145 "bt_ctf_event_get_payload_by_index returns a correct field");
2146 bt_put(ret_field_type);
2147 bt_put(ret_field);
2148
2149 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
2150 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2151 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
2152 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2153 bt_put(event);
2154
2155 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
2156 event = bt_ctf_event_create(event_class);
2157 struct bt_ctf_field *integer =
2158 bt_ctf_field_create(ep_field_1_type);
2159 struct bt_ctf_field *string =
2160 bt_ctf_field_create(ep_a_string_type);
2161
2162 ret |= bt_ctf_clock_set_time(clock, ++current_time);
2163 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
2164 ret |= bt_ctf_event_set_payload(event, "field_1",
2165 integer);
2166 bt_put(integer);
2167 ret |= bt_ctf_field_string_set_value(string, "This is a test");
2168 ret |= bt_ctf_event_set_payload(event, "a_string",
2169 string);
2170 bt_put(string);
2171
2172 /* Populate stream event context */
2173 stream_event_context =
2174 bt_ctf_event_get_stream_event_context(event);
2175 integer = bt_ctf_field_structure_get_field(stream_event_context,
2176 "common_event_context");
2177 BT_PUT(stream_event_context);
2178 ret |= bt_ctf_field_unsigned_integer_set_value(integer,
2179 i % 42);
2180 bt_put(integer);
2181
2182 ret |= bt_ctf_stream_append_event(stream, event);
2183 bt_put(event);
2184
2185 if (ret) {
2186 break;
2187 }
2188 }
2189
2190 events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH);
2191 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
2192 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2193 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
2194 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2195 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2196 ok(ret == 0 && ret_uint64 == 0,
2197 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2198 bt_ctf_stream_append_discarded_events(stream, 1000);
2199 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2200 ok(ret == 0 && ret_uint64 == 1000,
2201 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2202
2203 end:
2204 ok(events_appended, "Append 100 000 events to a stream");
2205
2206 /*
2207 * Populate the custom packet context field with a dummy value
2208 * otherwise flush will fail.
2209 */
2210 packet_context = bt_ctf_stream_get_packet_context(stream);
2211 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
2212 "custom_packet_context_field");
2213 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
2214
2215 ok(bt_ctf_stream_flush(stream) == 0,
2216 "Flush a stream that forces a packet resize");
2217 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2218 ok(ret == 0 && ret_uint64 == 1000,
2219 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2220 bt_put(integer_type);
2221 bt_put(string_type);
2222 bt_put(packet_context);
2223 bt_put(packet_context_field);
2224 bt_put(stream_event_context);
2225 bt_put(event_class);
2226 bt_put(ep_field_1_type);
2227 bt_put(ep_a_string_type);
2228 bt_put(ep_type);
2229 }
2230
2231 void test_empty_stream(struct bt_ctf_writer *writer)
2232 {
2233 int ret = 0;
2234 struct bt_ctf_trace *trace = NULL, *ret_trace = NULL;
2235 struct bt_ctf_stream_class *stream_class = NULL;
2236 struct bt_ctf_stream *stream = NULL;
2237
2238 trace = bt_ctf_writer_get_trace(writer);
2239 if (!trace) {
2240 diag("Failed to get trace from writer");
2241 ret = -1;
2242 goto end;
2243 }
2244
2245 stream_class = bt_ctf_stream_class_create("empty_stream");
2246 if (!stream_class) {
2247 diag("Failed to create stream class");
2248 ret = -1;
2249 goto end;
2250 }
2251
2252 ok(bt_ctf_stream_class_get_trace(NULL) == NULL,
2253 "bt_ctf_stream_class_get_trace handles NULL correctly");
2254 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
2255 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2256
2257 stream = bt_ctf_writer_create_stream(writer, stream_class);
2258 if (!stream) {
2259 diag("Failed to create writer stream");
2260 ret = -1;
2261 goto end;
2262 }
2263
2264 ret_trace = bt_ctf_stream_class_get_trace(stream_class);
2265 ok(ret_trace == trace,
2266 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2267 end:
2268 ok(ret == 0,
2269 "Created a stream class with default attributes and an empty stream");
2270 bt_put(trace);
2271 bt_put(ret_trace);
2272 bt_put(stream);
2273 bt_put(stream_class);
2274 }
2275
2276 void test_custom_event_header_stream(struct bt_ctf_writer *writer)
2277 {
2278 int i, ret;
2279 struct bt_ctf_trace *trace = NULL;
2280 struct bt_ctf_clock *clock = NULL;
2281 struct bt_ctf_stream_class *stream_class = NULL;
2282 struct bt_ctf_stream *stream = NULL;
2283 struct bt_ctf_field_type *integer_type = NULL,
2284 *sequence_type = NULL, *event_header_type = NULL;
2285 struct bt_ctf_field *integer = NULL, *sequence = NULL,
2286 *event_header = NULL, *packet_header = NULL;
2287 struct bt_ctf_event_class *event_class = NULL;
2288 struct bt_ctf_event *event = NULL;
2289
2290 trace = bt_ctf_writer_get_trace(writer);
2291 if (!trace) {
2292 fail("Failed to get trace from writer");
2293 goto end;
2294 }
2295
2296 clock = bt_ctf_trace_get_clock(trace, 0);
2297 if (!clock) {
2298 fail("Failed to get clock from trace");
2299 goto end;
2300 }
2301
2302 stream_class = bt_ctf_stream_class_create("custom_event_header_stream");
2303 if (!stream_class) {
2304 fail("Failed to create stream class");
2305 goto end;
2306 }
2307
2308 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2309 if (ret) {
2310 fail("Failed to set stream class clock");
2311 goto end;
2312 }
2313
2314 /*
2315 * Customize event header to add an "seq_len" integer member
2316 * which will be used as the length of a sequence in an event of this
2317 * stream.
2318 */
2319 event_header_type = bt_ctf_stream_class_get_event_header_type(
2320 stream_class);
2321 if (!event_header_type) {
2322 fail("Failed to get event header type");
2323 goto end;
2324 }
2325
2326 integer_type = bt_ctf_field_type_integer_create(13);
2327 if (!integer_type) {
2328 fail("Failed to create length integer type");
2329 goto end;
2330 }
2331
2332 ret = bt_ctf_field_type_structure_add_field(event_header_type,
2333 integer_type, "seq_len");
2334 if (ret) {
2335 fail("Failed to add a new field to stream event header");
2336 goto end;
2337 }
2338
2339 event_class = bt_ctf_event_class_create("sequence_event");
2340 if (!event_class) {
2341 fail("Failed to create event class");
2342 goto end;
2343 }
2344
2345 /*
2346 * This event's payload will contain a sequence which references
2347 * stream.event.header.seq_len as its length field.
2348 */
2349 sequence_type = bt_ctf_field_type_sequence_create(integer_type,
2350 "stream.event.header.seq_len");
2351 if (!sequence_type) {
2352 fail("Failed to create a sequence");
2353 goto end;
2354 }
2355
2356 ret = bt_ctf_event_class_add_field(event_class, sequence_type,
2357 "some_sequence");
2358 if (ret) {
2359 fail("Failed to add a sequence to an event class");
2360 goto end;
2361 }
2362
2363 ret = bt_ctf_stream_class_add_event_class(stream_class, event_class);
2364 if (ret) {
2365 fail("Failed to add event class to stream class");
2366 goto end;
2367 }
2368
2369 stream = bt_ctf_writer_create_stream(writer, stream_class);
2370 if (!stream) {
2371 fail("Failed to create stream")
2372 goto end;
2373 }
2374
2375 /*
2376 * We have defined a custom packet header field. We have to populate it
2377 * explicitly.
2378 */
2379 packet_header = bt_ctf_stream_get_packet_header(stream);
2380 if (!packet_header) {
2381 fail("Failed to get stream packet header");
2382 goto end;
2383 }
2384
2385 integer = bt_ctf_field_structure_get_field(packet_header,
2386 "custom_trace_packet_header_field");
2387 if (!integer) {
2388 fail("Failed to retrieve custom_trace_packet_header_field");
2389 goto end;
2390 }
2391
2392 ret = bt_ctf_field_unsigned_integer_set_value(integer, 3487);
2393 if (ret) {
2394 fail("Failed to set custom_trace_packet_header_field value");
2395 goto end;
2396 }
2397 bt_put(integer);
2398
2399 event = bt_ctf_event_create(event_class);
2400 if (!event) {
2401 fail("Failed to create event");
2402 goto end;
2403 }
2404
2405 event_header = bt_ctf_event_get_header(event);
2406 if (!event_header) {
2407 fail("Failed to get event header");
2408 goto end;
2409 }
2410
2411 integer = bt_ctf_field_structure_get_field(event_header,
2412 "seq_len");
2413 if (!integer) {
2414 fail("Failed to get seq_len field from event header");
2415 goto end;
2416 }
2417
2418 ret = bt_ctf_field_unsigned_integer_set_value(integer, 2);
2419 if (ret) {
2420 fail("Failed to set seq_len value in event header");
2421 goto end;
2422 }
2423
2424 /* Populate both sequence integer fields */
2425 sequence = bt_ctf_event_get_payload(event, "some_sequence");
2426 if (!sequence) {
2427 fail("Failed to retrieve sequence from event");
2428 goto end;
2429 }
2430
2431 ret = bt_ctf_field_sequence_set_length(sequence, integer);
2432 if (ret) {
2433 fail("Failed to set sequence length");
2434 goto end;
2435 }
2436 bt_put(integer);
2437
2438 for (i = 0; i < 2; i++) {
2439 integer = bt_ctf_field_sequence_get_field(sequence, i);
2440 if (ret) {
2441 fail("Failed to retrieve sequence element");
2442 goto end;
2443 }
2444
2445 ret = bt_ctf_field_unsigned_integer_set_value(integer, i);
2446 if (ret) {
2447 fail("Failed to set sequence element value");
2448 goto end;
2449 }
2450
2451 bt_put(integer);
2452 integer = NULL;
2453 }
2454
2455 ret = bt_ctf_stream_append_event(stream, event);
2456 if (ret) {
2457 fail("Failed to append event to stream");
2458 goto end;
2459 }
2460
2461 ret = bt_ctf_stream_flush(stream);
2462 if (ret) {
2463 fail("Failed to flush custom_event_header stream");
2464 }
2465 end:
2466 bt_put(clock);
2467 bt_put(trace);
2468 bt_put(stream);
2469 bt_put(stream_class);
2470 bt_put(event_class);
2471 bt_put(event);
2472 bt_put(integer);
2473 bt_put(sequence);
2474 bt_put(event_header);
2475 bt_put(packet_header);
2476 bt_put(sequence_type);
2477 bt_put(integer_type);
2478 bt_put(event_header_type);
2479 }
2480
2481 void test_instanciate_event_before_stream(struct bt_ctf_writer *writer)
2482 {
2483 int ret = 0;
2484 struct bt_ctf_trace *trace = NULL;
2485 struct bt_ctf_clock *clock = NULL;
2486 struct bt_ctf_stream_class *stream_class = NULL;
2487 struct bt_ctf_stream *stream = NULL,
2488 *ret_stream = NULL;
2489 struct bt_ctf_event_class *event_class = NULL;
2490 struct bt_ctf_event *event = NULL;
2491 struct bt_ctf_field_type *integer_type = NULL;
2492 struct bt_ctf_field *integer = NULL;
2493
2494 trace = bt_ctf_writer_get_trace(writer);
2495 if (!trace) {
2496 diag("Failed to get trace from writer");
2497 ret = -1;
2498 goto end;
2499 }
2500
2501 clock = bt_ctf_trace_get_clock(trace, 0);
2502 if (!clock) {
2503 diag("Failed to get clock from trace");
2504 ret = -1;
2505 goto end;
2506 }
2507
2508 stream_class = bt_ctf_stream_class_create("event_before_stream_test");
2509 if (!stream_class) {
2510 diag("Failed to create stream class");
2511 ret = -1;
2512 goto end;
2513 }
2514
2515 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2516 if (ret) {
2517 diag("Failed to set stream class clock");
2518 goto end;
2519 }
2520
2521 event_class = bt_ctf_event_class_create("some_event_class_name");
2522 integer_type = bt_ctf_field_type_integer_create(32);
2523 if (!integer_type) {
2524 diag("Failed to create integer field type");
2525 ret = -1;
2526 goto end;
2527 }
2528
2529 ret = bt_ctf_event_class_add_field(event_class, integer_type,
2530 "integer_field");
2531 if (ret) {
2532 diag("Failed to add field to event class");
2533 goto end;
2534 }
2535
2536 ret = bt_ctf_stream_class_add_event_class(stream_class,
2537 event_class);
2538 if (ret) {
2539 diag("Failed to add event class to stream class");
2540 }
2541
2542 event = bt_ctf_event_create(event_class);
2543 if (!event) {
2544 diag("Failed to create event");
2545 ret = -1;
2546 goto end;
2547 }
2548
2549 integer = bt_ctf_event_get_payload_by_index(event, 0);
2550 if (!integer) {
2551 diag("Failed to get integer field payload from event");
2552 ret = -1;
2553 goto end;
2554 }
2555
2556 ret = bt_ctf_field_unsigned_integer_set_value(integer, 1234);
2557 if (ret) {
2558 diag("Failed to set integer field value");
2559 goto end;
2560 }
2561
2562 stream = bt_ctf_writer_create_stream(writer, stream_class);
2563 if (!stream) {
2564 diag("Failed to create writer stream");
2565 ret = -1;
2566 goto end;
2567 }
2568
2569 ok(bt_ctf_event_get_stream(NULL) == NULL,
2570 "bt_ctf_event_get_stream handles NULL correctly");
2571 ok(bt_ctf_event_get_stream(event) == NULL,
2572 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2573
2574 ret = bt_ctf_stream_append_event(stream, event);
2575 if (ret) {
2576 diag("Failed to append event to stream");
2577 goto end;
2578 }
2579
2580 ret_stream = bt_ctf_event_get_stream(event);
2581 ok(ret_stream == stream,
2582 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2583 end:
2584 ok(ret == 0,
2585 "Create an event before instanciating its associated stream");
2586 bt_put(trace);
2587 bt_put(stream);
2588 bt_put(ret_stream);
2589 bt_put(stream_class);
2590 bt_put(event_class);
2591 bt_put(event);
2592 bt_put(integer_type);
2593 bt_put(integer);
2594 bt_put(clock);
2595 }
2596
2597 void append_existing_event_class(struct bt_ctf_stream_class *stream_class)
2598 {
2599 struct bt_ctf_event_class *event_class;
2600
2601 event_class = bt_ctf_event_class_create("Simple Event");
2602 assert(event_class);
2603 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2604 "two event classes with the same name cannot cohabit within the same stream class");
2605 bt_put(event_class);
2606
2607 event_class = bt_ctf_event_class_create("different name, ok");
2608 assert(event_class);
2609 assert(!bt_ctf_event_class_set_id(event_class, 11));
2610 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2611 "two event classes with the same ID cannot cohabit within the same stream class");
2612 bt_put(event_class);
2613 }
2614
2615 void test_trace_stream_class_clock(void)
2616 {
2617 struct bt_ctf_trace *trace = NULL;
2618 struct bt_ctf_stream_class *sc1 = NULL;
2619 struct bt_ctf_stream_class *sc2 = NULL;
2620 struct bt_ctf_clock *sc1_clock = NULL;
2621 struct bt_ctf_clock *sc2_clock = NULL;
2622 const char *clock_name = "hello";
2623
2624 trace = bt_ctf_trace_create();
2625 assert(trace);
2626 sc1 = bt_ctf_stream_class_create(NULL);
2627 assert(sc1);
2628 sc2 = bt_ctf_stream_class_create(NULL);
2629 assert(sc2);
2630 sc1_clock = bt_ctf_clock_create(clock_name);
2631 assert(sc1_clock);
2632 sc2_clock = bt_ctf_clock_create(clock_name);
2633 assert(sc2_clock);
2634
2635 ok(!bt_ctf_stream_class_set_clock(sc1, sc1_clock),
2636 "bt_ctf_stream_class_set_clock() succeeds for sc1");
2637 ok(!bt_ctf_stream_class_set_clock(sc2, sc2_clock),
2638 "bt_ctf_stream_class_set_clock() succeeds for sc2");
2639 ok(!bt_ctf_trace_add_stream_class(trace, sc1),
2640 "bt_ctf_trace_add_stream_class() succeeds with sc1");
2641 ok(bt_ctf_trace_add_stream_class(trace, sc2),
2642 "bt_ctf_trace_add_stream_class() fails with sc2 (different clock, same name)");
2643
2644 BT_PUT(trace);
2645 BT_PUT(sc1);
2646 BT_PUT(sc2);
2647 BT_PUT(sc1_clock);
2648 BT_PUT(sc2_clock);
2649 }
2650
2651 static
2652 struct bt_ctf_event_class *create_minimal_event_class(void)
2653 {
2654 struct bt_ctf_event_class *ec = NULL;
2655 struct bt_ctf_field_type *int_ft = NULL;
2656 int ret;
2657
2658 int_ft = bt_ctf_field_type_integer_create(23);
2659 assert(int_ft);
2660 ec = bt_ctf_event_class_create("minimal");
2661 assert(ec);
2662 ret = bt_ctf_event_class_add_field(ec, int_ft, "field");
2663 assert(!ret);
2664 BT_PUT(int_ft);
2665
2666 return ec;
2667 }
2668
2669 static
2670 void test_create_writer_vs_non_writer_mode(void)
2671 {
2672 int ret;
2673 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2674 const char *writer_stream_name = "writer stream instance";
2675 struct bt_ctf_writer *writer = NULL;
2676 struct bt_ctf_trace *writer_trace = NULL;
2677 struct bt_ctf_stream_class *writer_sc = NULL;
2678 struct bt_ctf_stream *writer_stream = NULL;
2679 struct bt_ctf_stream *writer_stream2 = NULL;
2680 struct bt_ctf_stream *packet_stream = NULL;
2681 struct bt_ctf_trace *non_writer_trace = NULL;
2682 struct bt_ctf_stream_class *non_writer_sc = NULL;
2683 struct bt_ctf_stream *non_writer_stream = NULL;
2684 struct bt_ctf_stream *non_writer_stream2 = NULL;
2685 struct bt_ctf_event_class *writer_ec = NULL;
2686 struct bt_ctf_event_class *non_writer_ec = NULL;
2687 struct bt_ctf_event *event = NULL;
2688 struct bt_ctf_event *event2 = NULL;
2689 struct bt_ctf_field_type *empty_struct_ft = NULL;
2690 struct bt_ctf_field *int_field = NULL;
2691 struct bt_ctf_clock *writer_clock = NULL;
2692 struct bt_ctf_clock *non_writer_clock = NULL;
2693 struct bt_ctf_packet *packet = NULL;
2694 struct bt_ctf_packet *packet2 = NULL;
2695
2696 if (!bt_mkdtemp(trace_path)) {
2697 perror("# perror");
2698 }
2699
2700 /* Create empty structure field type (event header) */
2701 empty_struct_ft = bt_ctf_field_type_structure_create();
2702 assert(empty_struct_ft);
2703
2704 /* Create writer, writer stream class, stream, and clock */
2705 writer = bt_ctf_writer_create(trace_path);
2706 assert(writer);
2707 writer_trace = bt_ctf_writer_get_trace(writer);
2708 ok(writer_trace, "bt_ctf_writer_get_trace() returns a trace");
2709 writer_sc = bt_ctf_stream_class_create("writer_sc");
2710 assert(writer_sc);
2711 ret = bt_ctf_stream_class_set_event_header_type(writer_sc,
2712 empty_struct_ft);
2713 assert(!ret);
2714 ret = bt_ctf_trace_add_stream_class(writer_trace, writer_sc);
2715 assert(!ret);
2716 writer_stream = bt_ctf_stream_create(writer_sc, writer_stream_name);
2717 assert(writer_stream);
2718 ok(!strcmp(bt_ctf_stream_get_name(writer_stream), writer_stream_name),
2719 "bt_ctf_stream_get_name() returns the stream's name");
2720 writer_clock = bt_ctf_clock_create("writer_clock");
2721 assert(writer_clock);
2722 ret = bt_ctf_trace_add_clock(writer_trace, writer_clock);
2723 assert(!ret);
2724
2725 /* Create non-writer trace, stream class, stream, and clock */
2726 non_writer_trace = bt_ctf_trace_create();
2727 assert(non_writer_trace);
2728 non_writer_sc = bt_ctf_stream_class_create("nonwriter_sc");
2729 assert(non_writer_sc);
2730 ret = bt_ctf_stream_class_set_event_header_type(non_writer_sc,
2731 empty_struct_ft);
2732 assert(!ret);
2733 ret = bt_ctf_trace_add_stream_class(non_writer_trace, non_writer_sc);
2734 assert(!ret);
2735 non_writer_stream = bt_ctf_stream_create(non_writer_sc, NULL);
2736 assert(non_writer_stream);
2737 non_writer_clock = bt_ctf_clock_create("non_writer_clock");
2738 assert(non_writer_clock);
2739 ret = bt_ctf_trace_add_clock(non_writer_trace, non_writer_clock);
2740 assert(!ret);
2741
2742 /* Create event class and event */
2743 writer_ec = create_minimal_event_class();
2744 assert(writer_ec);
2745 ret = bt_ctf_stream_class_add_event_class(writer_sc, writer_ec);
2746 assert(!ret);
2747 event = bt_ctf_event_create(writer_ec);
2748 assert(event);
2749 int_field = bt_ctf_event_get_payload_by_index(event, 0);
2750 assert(int_field);
2751 bt_ctf_field_unsigned_integer_set_value(int_field, 17);
2752
2753 /*
2754 * Verify non-writer stream: it should be impossible to append
2755 * an event to it.
2756 */
2757 ok(bt_ctf_stream_append_event(non_writer_stream, event),
2758 "bt_ctf_stream_append_event() fails with a non-writer stream");
2759
2760 /*
2761 * Verify writer stream: it should be possible to append an
2762 * event to it.
2763 */
2764 ok(!bt_ctf_stream_append_event(writer_stream, event),
2765 "bt_ctf_stream_append_event() succeeds with a writer stream");
2766
2767 /*
2768 * Verify that it's possible to get and set the value of a
2769 * writer mode clock.
2770 */
2771 ok (!bt_ctf_clock_set_value(writer_clock, 1000),
2772 "bt_ctf_clock_set_value() succeeds with a writer mode clock");
2773 ok (bt_ctf_clock_get_value(writer_clock) == 1000,
2774 "bt_ctf_clock_get_value() succeeds with a writer mode clock");
2775
2776 /*
2777 * Verify that it's impossible to get and set the value of a
2778 * non-writer mode clock.
2779 */
2780 ok (bt_ctf_clock_set_value(non_writer_clock, 1000),
2781 "bt_ctf_clock_set_value() fails with a non-writer mode clock");
2782 ok (bt_ctf_clock_get_value(non_writer_clock) == -1ULL,
2783 "bt_ctf_clock_get_value() fails with a non-writer mode clock");
2784
2785 /*
2786 * It should be possible to create a packet from a non-writer
2787 * stream, but not from a writer stream.
2788 */
2789 packet = bt_ctf_packet_create(writer_stream);
2790 ok(!packet, "bt_ctf_packet_create() fails with a writer stream");
2791 packet = bt_ctf_packet_create(non_writer_stream);
2792 ok(packet, "bt_ctf_packet_create() succeeds with a non-writer stream");
2793 packet_stream = bt_ctf_packet_get_stream(packet);
2794 ok(packet_stream == non_writer_stream,
2795 "bt_ctf_packet_get_stream() returns the correct stream");
2796
2797 /*
2798 * It should not be possible to append an event associated to
2799 * a stream to a different stream.
2800 */
2801 writer_stream2 = bt_ctf_stream_create(writer_sc, "zoo");
2802 assert(writer_stream2);
2803 ok(bt_ctf_stream_append_event(writer_stream2, event),
2804 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2805
2806 /*
2807 * It should not be possible to set the packet of an event
2808 * associated to a given stream to a packet associated with
2809 * a different stream.
2810 */
2811 ok(bt_ctf_event_set_packet(event, packet),
2812 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2813
2814 /*
2815 * It should be possible to set the packet of a fresh event, as
2816 * long as the originating stream classes are the same.
2817 */
2818 event2 = bt_ctf_event_create(writer_ec);
2819 assert(event2);
2820 ok(bt_ctf_event_set_packet(event2, packet),
2821 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2822 non_writer_ec = create_minimal_event_class();
2823 assert(non_writer_ec);
2824 ret = bt_ctf_stream_class_add_event_class(non_writer_sc, non_writer_ec);
2825 assert(!ret);
2826 BT_PUT(event2);
2827 event2 = bt_ctf_event_create(non_writer_ec);
2828 assert(event2);
2829 ok(!bt_ctf_event_set_packet(event2, packet),
2830 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2831
2832 /*
2833 * It should be possible to set a packet created from the same
2834 * stream to an event with an existing packet.
2835 */
2836 packet2 = bt_ctf_packet_create(non_writer_stream);
2837 assert(packet2);
2838 ok(!bt_ctf_event_set_packet(event2, packet2),
2839 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2840 BT_PUT(packet2);
2841
2842 /*
2843 * It should not be possible to set a packet created from a
2844 * different stream to an event with an existing packet.
2845 */
2846 non_writer_stream2 = bt_ctf_stream_create(non_writer_sc, "rj45");
2847 assert(non_writer_stream2);
2848 packet2 = bt_ctf_packet_create(non_writer_stream);
2849 assert(packet2);
2850 ok(!bt_ctf_event_set_packet(event2, packet2),
2851 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2852
2853 bt_put(writer);
2854 bt_put(writer_trace);
2855 bt_put(writer_sc);
2856 bt_put(writer_stream);
2857 bt_put(writer_stream2);
2858 bt_put(non_writer_trace);
2859 bt_put(non_writer_sc);
2860 bt_put(non_writer_stream);
2861 bt_put(non_writer_stream2);
2862 bt_put(packet_stream);
2863 bt_put(writer_ec);
2864 bt_put(non_writer_ec);
2865 bt_put(event);
2866 bt_put(event2);
2867 bt_put(int_field);
2868 bt_put(empty_struct_ft);
2869 bt_put(writer_clock);
2870 bt_put(non_writer_clock);
2871 bt_put(packet);
2872 bt_put(packet2);
2873 }
2874
2875 void test_clock_utils(void)
2876 {
2877 int ret;
2878 struct bt_ctf_clock *clock = NULL;
2879
2880 clock = bt_ctf_clock_create("water");
2881 assert(clock);
2882 ret = bt_ctf_clock_set_offset_s(clock, 1234);
2883 assert(clock);
2884 ret = bt_ctf_clock_set_offset(clock, 1000);
2885 assert(!ret);
2886 ret = bt_ctf_clock_set_frequency(clock, 1000000000);
2887 assert(!ret);
2888 ok(bt_ctf_clock_ns_from_value(clock, 4321) == 1234000005321ULL,
2889 "bt_ctf_clock_ns_from_value() returns the correct value with a 1 GHz frequency");
2890 ret = bt_ctf_clock_set_frequency(clock, 1534);
2891 assert(!ret);
2892 ok(bt_ctf_clock_ns_from_value(clock, 4321) ==
2893 (uint64_t) 1237468709256.845,
2894 "bt_ctf_clock_ns_from_value() returns the correct value with a non-1 GHz frequency");
2895
2896 BT_PUT(clock);
2897 }
2898
2899 int main(int argc, char **argv)
2900 {
2901 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2902 char metadata_path[sizeof(trace_path) + 9];
2903 const char *clock_name = "test_clock";
2904 const char *clock_description = "This is a test clock";
2905 const char *returned_clock_name;
2906 const char *returned_clock_description;
2907 const uint64_t frequency = 1123456789;
2908 const int64_t offset_s = 1351530929945824323;
2909 const int64_t offset = 1234567;
2910 int64_t get_offset_s,
2911 get_offset,
2912 get_time;
2913 const uint64_t precision = 10;
2914 const int is_absolute = 0xFF;
2915 char *metadata_string;
2916 struct bt_ctf_writer *writer;
2917 struct utsname name;
2918 char hostname[BABELTRACE_HOST_NAME_MAX];
2919 struct bt_ctf_clock *clock, *ret_clock;
2920 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
2921 struct bt_ctf_stream *stream1;
2922 const char *ret_string;
2923 const unsigned char *ret_uuid;
2924 unsigned char tmp_uuid[16] = { 0 };
2925 struct bt_ctf_field_type *packet_context_type,
2926 *packet_context_field_type,
2927 *packet_header_type,
2928 *packet_header_field_type,
2929 *integer_type,
2930 *stream_event_context_type,
2931 *ret_field_type,
2932 *event_header_field_type;
2933 struct bt_ctf_field *packet_header, *packet_header_field;
2934 struct bt_ctf_trace *trace;
2935 int ret;
2936 int64_t ret_int64_t;
2937 struct bt_value *obj;
2938
2939 if (argc < 3) {
2940 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2941 return -1;
2942 }
2943
2944 plan_tests(NR_TESTS);
2945
2946 if (!bt_mkdtemp(trace_path)) {
2947 perror("# perror");
2948 }
2949
2950 strcpy(metadata_path, trace_path);
2951 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
2952
2953 writer = bt_ctf_writer_create(trace_path);
2954 ok(writer, "bt_ctf_create succeeds in creating trace with path");
2955
2956 ok(!bt_ctf_writer_get_trace(NULL),
2957 "bt_ctf_writer_get_trace correctly handles NULL");
2958 trace = bt_ctf_writer_get_trace(writer);
2959 ok(trace,
2960 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2961 ok(bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
2962 "Set a trace's byte order to big endian");
2963 ok(bt_ctf_trace_get_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN,
2964 "bt_ctf_trace_get_byte_order returns a correct endianness");
2965
2966 /* Add environment context to the trace */
2967 ret = gethostname(hostname, sizeof(hostname));
2968 if (ret < 0) {
2969 return ret;
2970 }
2971 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
2972 "Add host (%s) environment field to writer instance",
2973 hostname);
2974 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
2975 "test_value"),
2976 "bt_ctf_writer_add_environment_field error with NULL writer");
2977 ok(bt_ctf_writer_add_environment_field(writer, NULL,
2978 "test_value"),
2979 "bt_ctf_writer_add_environment_field error with NULL field name");
2980 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
2981 NULL),
2982 "bt_ctf_writer_add_environment_field error with NULL field value");
2983
2984 /* Test bt_ctf_trace_set_environment_field with an integer object */
2985 obj = bt_value_integer_create_init(23);
2986 assert(obj);
2987 ok(bt_ctf_trace_set_environment_field(NULL, "test_env_int_obj", obj),
2988 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2989 ok(bt_ctf_trace_set_environment_field(trace, NULL, obj),
2990 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2991 ok(bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", NULL),
2992 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2993 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", obj),
2994 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2995 BT_PUT(obj);
2996
2997 /* Test bt_ctf_trace_set_environment_field with a string object */
2998 obj = bt_value_string_create_init("the value");
2999 assert(obj);
3000 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_str_obj", obj),
3001 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
3002 BT_PUT(obj);
3003
3004 /* Test bt_ctf_trace_set_environment_field_integer */
3005 ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int",
3006 -194875),
3007 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
3008 ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875),
3009 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
3010 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
3011 -164973),
3012 "bt_ctf_trace_set_environment_field_integer succeeds");
3013
3014 /* Test bt_ctf_trace_set_environment_field_string */
3015 ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str",
3016 "yeah"),
3017 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
3018 ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"),
3019 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
3020 ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
3021 NULL),
3022 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
3023 ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
3024 "oh yeah"),
3025 "bt_ctf_trace_set_environment_field_string succeeds");
3026
3027 /* Test bt_ctf_trace_get_environment_field_count */
3028 ok(bt_ctf_trace_get_environment_field_count(NULL) < 0,
3029 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
3030 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
3031 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
3032
3033 /* Test bt_ctf_trace_get_environment_field_name */
3034 ok(bt_ctf_trace_get_environment_field_name(NULL, 0) == NULL,
3035 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
3036 ok(bt_ctf_trace_get_environment_field_name(trace, -1) == NULL,
3037 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
3038 ok(bt_ctf_trace_get_environment_field_name(trace, 5) == NULL,
3039 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
3040 ret_string = bt_ctf_trace_get_environment_field_name(trace, 0);
3041 ok(ret_string && !strcmp(ret_string, "host"),
3042 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3043 ret_string = bt_ctf_trace_get_environment_field_name(trace, 1);
3044 ok(ret_string && !strcmp(ret_string, "test_env_int_obj"),
3045 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3046 ret_string = bt_ctf_trace_get_environment_field_name(trace, 2);
3047 ok(ret_string && !strcmp(ret_string, "test_env_str_obj"),
3048 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3049 ret_string = bt_ctf_trace_get_environment_field_name(trace, 3);
3050 ok(ret_string && !strcmp(ret_string, "test_env_int"),
3051 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3052 ret_string = bt_ctf_trace_get_environment_field_name(trace, 4);
3053 ok(ret_string && !strcmp(ret_string, "test_env_str"),
3054 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3055
3056 /* Test bt_ctf_trace_get_environment_field_value */
3057 ok(bt_ctf_trace_get_environment_field_value(NULL, 0) == NULL,
3058 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3059 ok(bt_ctf_trace_get_environment_field_value(trace, -1) == NULL,
3060 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
3061 ok(bt_ctf_trace_get_environment_field_value(trace, 5) == NULL,
3062 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3063 obj = bt_ctf_trace_get_environment_field_value(trace, 1);
3064 ret = bt_value_integer_get(obj, &ret_int64_t);
3065 ok(!ret && ret_int64_t == 23,
3066 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3067 BT_PUT(obj);
3068 obj = bt_ctf_trace_get_environment_field_value(trace, 2);
3069 ret = bt_value_string_get(obj, &ret_string);
3070 ok(!ret && ret_string && !strcmp(ret_string, "the value"),
3071 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3072 BT_PUT(obj);
3073
3074 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3075 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL,
3076 "test_env_str"),
3077 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3078 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, NULL),
3079 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3080 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, "oh oh"),
3081 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3082 obj = bt_ctf_trace_get_environment_field_value_by_name(trace,
3083 "test_env_str");
3084 ret = bt_value_string_get(obj, &ret_string);
3085 ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"),
3086 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3087 BT_PUT(obj);
3088
3089 /* Test environment field replacement */
3090 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
3091 654321),
3092 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3093 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
3094 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3095 obj = bt_ctf_trace_get_environment_field_value(trace, 3);
3096 ret = bt_value_integer_get(obj, &ret_int64_t);
3097 ok(!ret && ret_int64_t == 654321,
3098 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3099 BT_PUT(obj);
3100
3101 /* On Solaris, uname() can return any positive value on success */
3102 if (uname(&name) < 0) {
3103 perror("uname");
3104 return -1;
3105 }
3106
3107 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
3108 == 0, "Add sysname (%s) environment field to writer instance",
3109 name.sysname);
3110 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
3111 name.nodename) == 0,
3112 "Add nodename (%s) environment field to writer instance",
3113 name.nodename);
3114 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
3115 == 0, "Add release (%s) environment field to writer instance",
3116 name.release);
3117 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
3118 == 0, "Add version (%s) environment field to writer instance",
3119 name.version);
3120 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
3121 == 0, "Add machine (%s) environment field to writer istance",
3122 name.machine);
3123
3124 /* Define a clock and add it to the trace */
3125 ok(bt_ctf_clock_create("signed") == NULL,
3126 "Illegal clock name rejected");
3127 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
3128 clock = bt_ctf_clock_create(clock_name);
3129 ok(clock, "Clock created sucessfully");
3130 returned_clock_name = bt_ctf_clock_get_name(clock);
3131 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
3132 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
3133 "Returned clock name is valid");
3134
3135 returned_clock_description = bt_ctf_clock_get_description(clock);
3136 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
3137 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
3138 "Clock description set successfully");
3139
3140 returned_clock_description = bt_ctf_clock_get_description(clock);
3141 ok(returned_clock_description,
3142 "bt_ctf_clock_get_description returns a description.");
3143 ok(returned_clock_description ?
3144 !strcmp(returned_clock_description, clock_description) : 0,
3145 "Returned clock description is valid");
3146
3147 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
3148 "bt_ctf_clock_get_frequency returns the correct default frequency");
3149 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
3150 "Set clock frequency");
3151 ok(bt_ctf_clock_get_frequency(clock) == frequency,
3152 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3153
3154 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
3155 "bt_ctf_clock_get_offset_s succeeds");
3156 ok(get_offset_s == DEFAULT_CLOCK_OFFSET_S,
3157 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3158 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
3159 "Set clock offset (seconds)");
3160 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
3161 "bt_ctf_clock_get_offset_s succeeds");
3162 ok(get_offset_s == offset_s,
3163 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3164
3165 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
3166 "bt_ctf_clock_get_offset succeeds");
3167 ok(get_offset == DEFAULT_CLOCK_OFFSET,
3168 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3169 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
3170 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
3171 "bt_ctf_clock_get_offset succeeds");
3172 ok(get_offset == offset,
3173 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3174
3175 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
3176 "bt_ctf_clock_get_precision returns the correct default precision");
3177 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
3178 "Set clock precision");
3179 ok(bt_ctf_clock_get_precision(clock) == precision,
3180 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3181
3182 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
3183 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3184 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
3185 "Set clock absolute property");
3186 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
3187 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3188
3189 ok(bt_ctf_clock_get_time(clock, &get_time) == 0,
3190 "bt_ctf_clock_get_time succeeds");
3191 ok(get_time == DEFAULT_CLOCK_TIME,
3192 "bt_ctf_clock_get_time returns the correct default time");
3193 ok(bt_ctf_clock_get_value(clock) == DEFAULT_CLOCK_VALUE,
3194 "bt_ctf_clock_get_value returns the correct default value");
3195 ok(bt_ctf_clock_set_value(clock, current_time) == 0,
3196 "Set clock value");
3197 ok(bt_ctf_clock_get_value(clock) == current_time,
3198 "bt_ctf_clock_get_value returns the correct value once it is set");
3199 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
3200 "Set clock time");
3201 ok(bt_ctf_clock_get_time(clock, &get_time) == 0,
3202 "bt_ctf_clock_get_time succeeds");
3203 ok(get_time >= current_time - 1 && get_time <= current_time + 1,
3204 "bt_ctf_clock_get_time returns the correct time once it is set");
3205
3206 ok(!bt_ctf_clock_get_name(NULL),
3207 "bt_ctf_clock_get_name correctly handles NULL");
3208 ok(!bt_ctf_clock_get_description(NULL),
3209 "bt_ctf_clock_get_description correctly handles NULL");
3210 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
3211 "bt_ctf_clock_get_frequency correctly handles NULL");
3212 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
3213 "bt_ctf_clock_get_precision correctly handles NULL");
3214 ok(bt_ctf_clock_get_offset_s(NULL, &get_offset_s) < 0,
3215 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3216 ok(bt_ctf_clock_get_offset_s(clock, NULL) < 0,
3217 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3218 ok(bt_ctf_clock_get_offset(NULL, &get_offset) < 0,
3219 "bt_ctf_clock_get_offset correctly handles NULL clock");
3220 ok(bt_ctf_clock_get_offset(clock, NULL) < 0,
3221 "bt_ctf_clock_get_offset correctly handles NULL output");
3222 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
3223 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3224 ok(bt_ctf_clock_get_time(NULL, &get_time) < 0,
3225 "bt_ctf_clock_get_time correctly handles NULL clock");
3226 ok(bt_ctf_clock_get_time(clock, NULL) < 0,
3227 "bt_ctf_clock_get_time correctly handles NULL output");
3228
3229 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
3230 "bt_ctf_clock_set_description correctly handles NULL clock");
3231 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
3232 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3233 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
3234 "bt_ctf_clock_get_precision correctly handles NULL clock");
3235 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
3236 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3237 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
3238 "bt_ctf_clock_set_offset correctly handles NULL clock");
3239 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
3240 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3241 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
3242 "bt_ctf_clock_set_time correctly handles NULL clock");
3243 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
3244 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3245 ret_uuid = bt_ctf_clock_get_uuid(clock);
3246 ok(ret_uuid,
3247 "bt_ctf_clock_get_uuid returns a UUID");
3248 if (ret_uuid) {
3249 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
3250 /* Slightly modify UUID */
3251 tmp_uuid[sizeof(tmp_uuid) - 1]++;
3252 }
3253
3254 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
3255 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3256 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
3257 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3258 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
3259 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3260 ret_uuid = bt_ctf_clock_get_uuid(clock);
3261 ok(ret_uuid,
3262 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3263 ok(uuid_match(ret_uuid, tmp_uuid),
3264 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3265
3266 /* Define a stream class */
3267 stream_class = bt_ctf_stream_class_create("test_stream");
3268
3269 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
3270 "bt_ctf_stream_class_get_name handles NULL correctly");
3271 ret_string = bt_ctf_stream_class_get_name(stream_class);
3272 ok(ret_string && !strcmp(ret_string, "test_stream"),
3273 "bt_ctf_stream_class_get_name returns a correct stream class name");
3274
3275 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
3276 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3277 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
3278 "bt_ctf_stream_class_get_clock handles NULL correctly");
3279
3280 ok(stream_class, "Create stream class");
3281 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
3282 "Set a stream class' clock");
3283 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
3284 ok(ret_clock == clock,
3285 "bt_ctf_stream_class_get_clock returns a correct clock");
3286 bt_put(ret_clock);
3287
3288 /* Test the event fields and event types APIs */
3289 type_field_tests();
3290
3291 /* Test fields copying */
3292 field_copy_tests();
3293
3294 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
3295 "bt_ctf_stream_class_get_id returns an error when no id is set");
3296 ok(bt_ctf_stream_class_get_id(NULL) < 0,
3297 "bt_ctf_stream_class_get_id handles NULL correctly");
3298 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
3299 "bt_ctf_stream_class_set_id handles NULL correctly");
3300 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
3301 "Set an stream class' id");
3302 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
3303 "bt_ctf_stream_class_get_id returns the correct value");
3304
3305 /* Validate default event header fields */
3306 ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL,
3307 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3308 ret_field_type = bt_ctf_stream_class_get_event_header_type(
3309 stream_class);
3310 ok(ret_field_type,
3311 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3312 ok(bt_ctf_field_type_get_type_id(ret_field_type) == BT_CTF_TYPE_ID_STRUCT,
3313 "Default event header type is a structure");
3314 event_header_field_type =
3315 bt_ctf_field_type_structure_get_field_type_by_name(
3316 ret_field_type, "id");
3317 ok(event_header_field_type,
3318 "Default event header type contains an \"id\" field");
3319 ok(bt_ctf_field_type_get_type_id(
3320 event_header_field_type) == BT_CTF_TYPE_ID_INTEGER,
3321 "Default event header \"id\" field is an integer");
3322 bt_put(event_header_field_type);
3323 event_header_field_type =
3324 bt_ctf_field_type_structure_get_field_type_by_name(
3325 ret_field_type, "timestamp");
3326 ok(event_header_field_type,
3327 "Default event header type contains a \"timestamp\" field");
3328 ok(bt_ctf_field_type_get_type_id(
3329 event_header_field_type) == BT_CTF_TYPE_ID_INTEGER,
3330 "Default event header \"timestamp\" field is an integer");
3331 bt_put(event_header_field_type);
3332 bt_put(ret_field_type);
3333
3334 /* Add a custom trace packet header field */
3335 ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
3336 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3337 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3338 ok(packet_header_type,
3339 "bt_ctf_trace_get_packet_header_type returns a packet header");
3340 ok(bt_ctf_field_type_get_type_id(packet_header_type) == BT_CTF_TYPE_ID_STRUCT,
3341 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3342 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3343 packet_header_type, "magic");
3344 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
3345 bt_put(ret_field_type);
3346 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3347 packet_header_type, "uuid");
3348 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
3349 bt_put(ret_field_type);
3350 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3351 packet_header_type, "stream_id");
3352 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
3353 bt_put(ret_field_type);
3354
3355 packet_header_field_type = bt_ctf_field_type_integer_create(22);
3356 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
3357 packet_header_field_type, "custom_trace_packet_header_field"),
3358 "Added a custom trace packet header field successfully");
3359
3360 ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
3361 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3362 ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0,
3363 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3364 ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
3365 "Set a trace packet_header_type successfully");
3366
3367 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
3368 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3369
3370 /* Add a custom field to the stream class' packet context */
3371 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
3372 ok(packet_context_type,
3373 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3374 ok(bt_ctf_field_type_get_type_id(packet_context_type) == BT_CTF_TYPE_ID_STRUCT,
3375 "Packet context is a structure");
3376
3377 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
3378 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3379 ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL),
3380 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3381
3382 integer_type = bt_ctf_field_type_integer_create(32);
3383 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
3384 integer_type) < 0,
3385 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3386 /* Create a "uint5_t" equivalent custom packet context field */
3387 packet_context_field_type = bt_ctf_field_type_integer_create(5);
3388
3389 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3390 packet_context_field_type, "custom_packet_context_field");
3391 ok(ret == 0, "Packet context field added successfully");
3392
3393 /* Define a stream event context containing a my_integer field. */
3394 ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL,
3395 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3396 ok(bt_ctf_stream_class_get_event_context_type(
3397 stream_class) == NULL,
3398 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3399 stream_event_context_type = bt_ctf_field_type_structure_create();
3400 bt_ctf_field_type_structure_add_field(stream_event_context_type,
3401 integer_type, "common_event_context");
3402
3403 ok(bt_ctf_stream_class_set_event_context_type(NULL,
3404 stream_event_context_type) < 0,
3405 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3406 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3407 NULL) < 0,
3408 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3409 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3410 integer_type) < 0,
3411 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3412
3413 ok(bt_ctf_stream_class_set_event_context_type(
3414 stream_class, stream_event_context_type) == 0,
3415 "Set a new stream event context type");
3416 ret_field_type = bt_ctf_stream_class_get_event_context_type(
3417 stream_class);
3418 ok(ret_field_type == stream_event_context_type,
3419 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3420 bt_put(ret_field_type);
3421
3422 /* Instantiate a stream and append events */
3423 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
3424 ok(stream1, "Instanciate a stream class from writer");
3425
3426 /*
3427 * Creating a stream through a writer adds the given stream
3428 * class to the writer's trace, thus registering the stream
3429 * class's clock to the trace.
3430 */
3431 ok(bt_ctf_trace_get_clock_count(NULL) < 0,
3432 "bt_ctf_trace_get_clock_count correctly handles NULL");
3433 ok(bt_ctf_trace_get_clock_count(trace) == 1,
3434 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3435 ok(!bt_ctf_trace_get_clock(NULL, 0),
3436 "bt_ctf_trace_get_clock correctly handles NULL");
3437 ok(!bt_ctf_trace_get_clock(trace, -1),
3438 "bt_ctf_trace_get_clock correctly handles negative indexes");
3439 ok(!bt_ctf_trace_get_clock(trace, 1),
3440 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3441 ret_clock = bt_ctf_trace_get_clock(trace, 0);
3442 ok(ret_clock == clock,
3443 "bt_ctf_trace_get_clock returns the right clock instance");
3444 bt_put(ret_clock);
3445 ok(!bt_ctf_trace_get_clock_by_name(trace, NULL),
3446 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3447 ok(!bt_ctf_trace_get_clock_by_name(NULL, clock_name),
3448 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3449 ok(!bt_ctf_trace_get_clock_by_name(NULL, NULL),
3450 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3451 ret_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name);
3452 ok(ret_clock == clock,
3453 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3454 bt_put(ret_clock);
3455 ok(!bt_ctf_trace_get_clock_by_name(trace, "random"),
3456 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3457
3458 ok(bt_ctf_stream_get_class(NULL) == NULL,
3459 "bt_ctf_stream_get_class correctly handles NULL");
3460 ret_stream_class = bt_ctf_stream_get_class(stream1);
3461 ok(ret_stream_class,
3462 "bt_ctf_stream_get_class returns a stream class");
3463 ok(ret_stream_class == stream_class,
3464 "Returned stream class is of the correct type");
3465
3466 /*
3467 * Packet header, packet context, event header, and stream
3468 * event context types were copied for the resolving
3469 * process
3470 */
3471 BT_PUT(packet_header_type);
3472 BT_PUT(packet_context_type);
3473 BT_PUT(stream_event_context_type);
3474 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3475 assert(packet_header_type);
3476 packet_context_type =
3477 bt_ctf_stream_class_get_packet_context_type(stream_class);
3478 assert(packet_context_type);
3479 stream_event_context_type =
3480 bt_ctf_stream_class_get_event_context_type(stream_class);
3481 assert(stream_event_context_type);
3482
3483 /*
3484 * Try to modify the packet context type after a stream has been
3485 * created.
3486 */
3487 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
3488 packet_header_field_type, "should_fail");
3489 ok(ret < 0,
3490 "Trace packet header type can't be modified once a stream has been instanciated");
3491
3492 /*
3493 * Try to modify the packet context type after a stream has been
3494 * created.
3495 */
3496 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3497 packet_context_field_type, "should_fail");
3498 ok(ret < 0,
3499 "Packet context type can't be modified once a stream has been instanciated");
3500
3501 /*
3502 * Try to modify the stream event context type after a stream has been
3503 * created.
3504 */
3505 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
3506 integer_type, "should_fail");
3507 ok(ret < 0,
3508 "Stream event context type can't be modified once a stream has been instanciated");
3509
3510 /* Should fail after instanciating a stream (frozen) */
3511 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
3512 "Changes to a stream class that was already instantiated fail");
3513
3514 /* Populate the custom packet header field only once for all tests */
3515 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
3516 "bt_ctf_stream_get_packet_header handles NULL correctly");
3517 packet_header = bt_ctf_stream_get_packet_header(stream1);
3518 ok(packet_header,
3519 "bt_ctf_stream_get_packet_header returns a packet header");
3520 ret_field_type = bt_ctf_field_get_type(packet_header);
3521 ok(ret_field_type == packet_header_type,
3522 "Stream returns a packet header of the appropriate type");
3523 bt_put(ret_field_type);
3524 packet_header_field = bt_ctf_field_structure_get_field(packet_header,
3525 "custom_trace_packet_header_field");
3526 ok(packet_header_field,
3527 "Packet header structure contains a custom field with the appropriate name");
3528 ret_field_type = bt_ctf_field_get_type(packet_header_field);
3529 ok(bt_ctf_field_type_compare(ret_field_type, packet_header_field_type) == 0,
3530 "Custom packet header field is of the expected type");
3531 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
3532 54321), "Set custom packet header value successfully");
3533 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
3534 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3535 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
3536 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3537 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
3538 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3539 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
3540 "Successfully set a stream's packet header");
3541
3542 ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0,
3543 "Add environment field to writer after stream creation");
3544
3545 test_clock_utils();
3546
3547 test_trace_stream_class_clock();
3548
3549 test_create_writer_vs_non_writer_mode();
3550
3551 test_instanciate_event_before_stream(writer);
3552
3553 append_simple_event(stream_class, stream1, clock);
3554
3555 packet_resize_test(stream_class, stream1, clock);
3556
3557 append_complex_event(stream_class, stream1, clock);
3558
3559 append_existing_event_class(stream_class);
3560
3561 test_empty_stream(writer);
3562
3563 test_custom_event_header_stream(writer);
3564
3565 metadata_string = bt_ctf_writer_get_metadata_string(writer);
3566 ok(metadata_string, "Get metadata string");
3567
3568 bt_ctf_writer_flush_metadata(writer);
3569 validate_metadata(argv[1], metadata_path);
3570 validate_trace(argv[2], trace_path);
3571
3572 bt_put(clock);
3573 bt_put(ret_stream_class);
3574 bt_put(writer);
3575 bt_put(stream1);
3576 bt_put(packet_context_type);
3577 bt_put(packet_context_field_type);
3578 bt_put(integer_type);
3579 bt_put(stream_event_context_type);
3580 bt_put(ret_field_type);
3581 bt_put(packet_header_type);
3582 bt_put(packet_header_field_type);
3583 bt_put(packet_header);
3584 bt_put(packet_header_field);
3585 bt_put(trace);
3586 free(metadata_string);
3587 bt_put(stream_class);
3588
3589 /* Remove all trace files and delete temporary trace directory */
3590 DIR *trace_dir = opendir(trace_path);
3591 if (!trace_dir) {
3592 perror("# opendir");
3593 return -1;
3594 }
3595
3596 struct dirent *entry;
3597 while ((entry = readdir(trace_dir))) {
3598 struct stat st;
3599 char filename[PATH_MAX];
3600
3601 if (snprintf(filename, sizeof(filename), "%s/%s",
3602 trace_path, entry->d_name) <= 0) {
3603 continue;
3604 }
3605
3606 if (stat(entry->d_name, &st)) {
3607 continue;
3608 }
3609
3610 if (S_ISREG(st.st_mode)) {
3611 unlinkat(bt_dirfd(trace_dir), entry->d_name, 0);
3612 }
3613 }
3614
3615 rmdir(trace_path);
3616 closedir(trace_dir);
3617 return 0;
3618 }
This page took 0.196498 seconds and 5 git commands to generate.