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