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