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