Commit | Line | Data |
---|---|---|
39d74371 JG |
1 | /* |
2 | * test-ctf-writer.c | |
3 | * | |
4 | * CTF Writer test | |
5 | * | |
6 | * Copyright 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; under version 2 of the License. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | */ | |
21 | ||
39d74371 JG |
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/events.h> | |
29 | #include <unistd.h> | |
559455a9 | 30 | #include <babeltrace/compat/stdlib.h> |
39d74371 JG |
31 | #include <stdio.h> |
32 | #include <sys/utsname.h> | |
0954670a | 33 | #include <babeltrace/compat/limits.h> |
39d74371 JG |
34 | #include <string.h> |
35 | #include <assert.h> | |
36 | #include <unistd.h> | |
37 | #include <sys/wait.h> | |
38 | #include <fcntl.h> | |
eafa875f | 39 | #include <babeltrace/compat/dirent.h> |
39d74371 JG |
40 | #include "tap/tap.h" |
41 | ||
42 | #define METADATA_LINE_SIZE 512 | |
43 | #define SEQUENCE_TEST_LENGTH 10 | |
44 | #define PACKET_RESIZE_TEST_LENGTH 100000 | |
45 | ||
46 | static uint64_t current_time; | |
47 | ||
48 | void validate_metadata(char *parser_path, char *metadata_path) | |
49 | { | |
50 | int ret = 0; | |
51 | char parser_output_path[] = "/tmp/parser_output_XXXXXX"; | |
52 | int parser_output_fd = -1, metadata_fd = -1; | |
53 | ||
54 | if (!metadata_path) { | |
55 | ret = -1; | |
56 | goto result; | |
57 | } | |
58 | ||
59 | parser_output_fd = mkstemp(parser_output_path); | |
60 | metadata_fd = open(metadata_path, O_RDONLY); | |
61 | ||
62 | unlink(parser_output_path); | |
63 | ||
64 | if (parser_output_fd == -1 || metadata_fd == -1) { | |
65 | diag("Failed create temporary files for metadata parsing."); | |
66 | ret = -1; | |
67 | goto result; | |
68 | } | |
69 | ||
70 | pid_t pid = fork(); | |
71 | if (pid) { | |
72 | int status = 0; | |
73 | waitpid(pid, &status, 0); | |
74 | ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1; | |
75 | } else { | |
76 | /* ctf-parser-test expects a metadata string on stdin. */ | |
77 | ret = dup2(metadata_fd, STDIN_FILENO); | |
78 | if (ret < 0) { | |
79 | perror("# dup2 metadata_fd to STDIN"); | |
80 | goto result; | |
81 | } | |
82 | ||
83 | ret = dup2(parser_output_fd, STDOUT_FILENO); | |
84 | if (ret < 0) { | |
85 | perror("# dup2 parser_output_fd to STDOUT"); | |
86 | goto result; | |
87 | } | |
88 | ||
89 | ret = dup2(parser_output_fd, STDERR_FILENO); | |
90 | if (ret < 0) { | |
91 | perror("# dup2 parser_output_fd to STDERR"); | |
92 | goto result; | |
93 | } | |
94 | ||
95 | execl(parser_path, "ctf-parser-test", NULL); | |
96 | perror("# Could not launch the ctf metadata parser process"); | |
97 | exit(-1); | |
98 | } | |
99 | result: | |
100 | ok(ret == 0, "Metadata string is valid"); | |
101 | ||
25afa9e5 | 102 | if (ret && metadata_fd >= 0 && parser_output_fd >= 0) { |
39d74371 JG |
103 | char *line; |
104 | size_t len = METADATA_LINE_SIZE; | |
105 | FILE *metadata_fp = NULL, *parser_output_fp = NULL; | |
106 | ||
107 | metadata_fp = fdopen(metadata_fd, "r"); | |
108 | if (!metadata_fp) { | |
109 | perror("fdopen on metadata_fd"); | |
110 | goto close_fp; | |
111 | } | |
e1d776cd | 112 | metadata_fd = -1; |
39d74371 JG |
113 | |
114 | parser_output_fp = fdopen(parser_output_fd, "r"); | |
115 | if (!parser_output_fp) { | |
116 | perror("fdopen on parser_output_fd"); | |
117 | goto close_fp; | |
118 | } | |
e1d776cd | 119 | parser_output_fd = -1; |
39d74371 JG |
120 | |
121 | line = malloc(len); | |
122 | if (!line) { | |
123 | diag("malloc failure"); | |
124 | } | |
125 | ||
126 | rewind(metadata_fp); | |
127 | ||
128 | /* Output the metadata and parser output as diagnostic */ | |
129 | while (getline(&line, &len, metadata_fp) > 0) { | |
130 | diag("%s", line); | |
131 | } | |
132 | ||
133 | rewind(parser_output_fp); | |
134 | while (getline(&line, &len, parser_output_fp) > 0) { | |
135 | diag("%s", line); | |
136 | } | |
137 | ||
138 | free(line); | |
139 | close_fp: | |
e1d776cd MD |
140 | if (metadata_fp) { |
141 | if (fclose(metadata_fp)) { | |
142 | diag("fclose failure"); | |
143 | } | |
144 | } | |
145 | if (parser_output_fp) { | |
146 | if (fclose(parser_output_fp)) { | |
147 | diag("fclose failure"); | |
148 | } | |
149 | } | |
39d74371 JG |
150 | } |
151 | ||
25afa9e5 | 152 | if (parser_output_fd >= 0) { |
e1d776cd MD |
153 | if (close(parser_output_fd)) { |
154 | diag("close error"); | |
155 | } | |
156 | } | |
25afa9e5 | 157 | if (metadata_fd >= 0) { |
e1d776cd MD |
158 | if (close(metadata_fd)) { |
159 | diag("close error"); | |
160 | } | |
161 | } | |
39d74371 JG |
162 | } |
163 | ||
164 | void validate_trace(char *parser_path, char *trace_path) | |
165 | { | |
166 | int ret = 0; | |
167 | char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX"; | |
168 | int babeltrace_output_fd = -1; | |
169 | ||
170 | if (!trace_path) { | |
171 | ret = -1; | |
172 | goto result; | |
173 | } | |
174 | ||
175 | babeltrace_output_fd = mkstemp(babeltrace_output_path); | |
176 | unlink(babeltrace_output_path); | |
177 | ||
178 | if (babeltrace_output_fd == -1) { | |
179 | diag("Failed to create a temporary file for trace parsing."); | |
180 | ret = -1; | |
181 | goto result; | |
182 | } | |
183 | ||
184 | pid_t pid = fork(); | |
185 | if (pid) { | |
186 | int status = 0; | |
187 | waitpid(pid, &status, 0); | |
188 | ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1; | |
189 | } else { | |
190 | ret = dup2(babeltrace_output_fd, STDOUT_FILENO); | |
191 | if (ret < 0) { | |
192 | perror("# dup2 babeltrace_output_fd to STDOUT"); | |
193 | goto result; | |
194 | } | |
195 | ||
196 | ret = dup2(babeltrace_output_fd, STDERR_FILENO); | |
197 | if (ret < 0) { | |
198 | perror("# dup2 babeltrace_output_fd to STDERR"); | |
199 | goto result; | |
200 | } | |
201 | ||
202 | execl(parser_path, "babeltrace", trace_path, NULL); | |
203 | perror("# Could not launch the babeltrace process"); | |
204 | exit(-1); | |
205 | } | |
206 | result: | |
207 | ok(ret == 0, "Babeltrace could read the resulting trace"); | |
208 | ||
25afa9e5 | 209 | if (ret && babeltrace_output_fd >= 0) { |
39d74371 JG |
210 | char *line; |
211 | size_t len = METADATA_LINE_SIZE; | |
212 | FILE *babeltrace_output_fp = NULL; | |
213 | ||
214 | babeltrace_output_fp = fdopen(babeltrace_output_fd, "r"); | |
215 | if (!babeltrace_output_fp) { | |
216 | perror("fdopen on babeltrace_output_fd"); | |
217 | goto close_fp; | |
218 | } | |
e1d776cd | 219 | babeltrace_output_fd = -1; |
39d74371 JG |
220 | |
221 | line = malloc(len); | |
e1d776cd MD |
222 | if (!line) { |
223 | diag("malloc error"); | |
224 | } | |
39d74371 JG |
225 | rewind(babeltrace_output_fp); |
226 | while (getline(&line, &len, babeltrace_output_fp) > 0) { | |
227 | diag("%s", line); | |
228 | } | |
229 | ||
230 | free(line); | |
231 | close_fp: | |
e1d776cd MD |
232 | if (babeltrace_output_fp) { |
233 | if (fclose(babeltrace_output_fp)) { | |
234 | diag("fclose error"); | |
235 | } | |
236 | } | |
39d74371 JG |
237 | } |
238 | ||
25afa9e5 | 239 | if (babeltrace_output_fd >= 0) { |
e1d776cd MD |
240 | if (close(babeltrace_output_fd)) { |
241 | diag("close error"); | |
242 | } | |
243 | } | |
39d74371 JG |
244 | } |
245 | ||
246 | void append_simple_event(struct bt_ctf_stream_class *stream_class, | |
247 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
248 | { | |
249 | /* Create and add a simple event class */ | |
250 | struct bt_ctf_event_class *simple_event_class = | |
251 | bt_ctf_event_class_create("Simple Event"); | |
252 | struct bt_ctf_field_type *uint_12_type = | |
253 | bt_ctf_field_type_integer_create(12); | |
254 | struct bt_ctf_field_type *float_type = | |
255 | bt_ctf_field_type_floating_point_create(); | |
8382544f JG |
256 | struct bt_ctf_field_type *enum_type = |
257 | bt_ctf_field_type_enumeration_create(uint_12_type); | |
39d74371 JG |
258 | struct bt_ctf_event *simple_event; |
259 | struct bt_ctf_field *integer_field; | |
260 | struct bt_ctf_field *float_field; | |
8382544f JG |
261 | struct bt_ctf_field *enum_field; |
262 | struct bt_ctf_field *enum_container_field; | |
39d74371 JG |
263 | |
264 | bt_ctf_field_type_set_alignment(float_type, 32); | |
265 | bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11); | |
266 | bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53); | |
8382544f JG |
267 | |
268 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, | |
269 | "escaping; \"test\"", 0, 0) == 0, | |
270 | "Accept enumeration mapping strings containing quotes"); | |
271 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, | |
272 | "\tanother \'escaping\'\n test\"", 1, 4) == 0, | |
273 | "Accept enumeration mapping strings containing special characters"); | |
274 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, | |
275 | "event clock int float", 5, 22) == 0, | |
276 | "Accept enumeration mapping strings containing reserved keywords"); | |
277 | ok(bt_ctf_event_class_add_field(simple_event_class, enum_type, | |
278 | "enum_field") == 0, "Add enumeration field to event"); | |
279 | ||
39d74371 JG |
280 | ok(uint_12_type, "Create an unsigned integer type"); |
281 | bt_ctf_event_class_add_field(simple_event_class, uint_12_type, | |
282 | "integer_field"); | |
283 | bt_ctf_event_class_add_field(simple_event_class, float_type, | |
284 | "float_field"); | |
285 | bt_ctf_stream_class_add_event_class(stream_class, | |
286 | simple_event_class); | |
287 | ||
288 | simple_event = bt_ctf_event_create(simple_event_class); | |
289 | ||
290 | ok(simple_event, | |
291 | "Instantiate an event containing a single integer field"); | |
292 | ||
293 | integer_field = bt_ctf_field_create(uint_12_type); | |
294 | bt_ctf_field_unsigned_integer_set_value(integer_field, 42); | |
295 | ok(bt_ctf_event_set_payload(simple_event, "integer_field", | |
296 | integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field"); | |
297 | ||
298 | float_field = bt_ctf_event_get_payload(simple_event, "float_field"); | |
299 | bt_ctf_field_floating_point_set_value(float_field, 3.1415); | |
8382544f JG |
300 | enum_field = bt_ctf_field_create(enum_type); |
301 | enum_container_field = bt_ctf_field_enumeration_get_container( | |
302 | enum_field); | |
303 | ok(bt_ctf_field_unsigned_integer_set_value( | |
304 | enum_container_field, 1) == 0, | |
305 | "Set enumeration container value"); | |
306 | bt_ctf_event_set_payload(simple_event, "enum_field", enum_field); | |
39d74371 JG |
307 | |
308 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time"); | |
309 | ||
310 | ok(bt_ctf_stream_append_event(stream, simple_event) == 0, | |
311 | "Append simple event to trace stream"); | |
312 | ||
313 | ok(bt_ctf_stream_flush(stream) == 0, | |
314 | "Flush trace stream with one event"); | |
315 | ||
316 | bt_ctf_event_class_put(simple_event_class); | |
317 | bt_ctf_event_put(simple_event); | |
318 | bt_ctf_field_type_put(uint_12_type); | |
319 | bt_ctf_field_type_put(float_type); | |
8382544f | 320 | bt_ctf_field_type_put(enum_type); |
39d74371 JG |
321 | bt_ctf_field_put(integer_field); |
322 | bt_ctf_field_put(float_field); | |
8382544f JG |
323 | bt_ctf_field_put(enum_field); |
324 | bt_ctf_field_put(enum_container_field); | |
39d74371 JG |
325 | } |
326 | ||
327 | void append_complex_event(struct bt_ctf_stream_class *stream_class, | |
328 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
329 | { | |
330 | int i; | |
331 | struct bt_ctf_field_type *uint_35_type = | |
332 | bt_ctf_field_type_integer_create(35); | |
333 | struct bt_ctf_field_type *int_16_type = | |
334 | bt_ctf_field_type_integer_create(16); | |
335 | struct bt_ctf_field_type *uint_3_type = | |
336 | bt_ctf_field_type_integer_create(3); | |
337 | struct bt_ctf_field_type *enum_variant_type = | |
338 | bt_ctf_field_type_enumeration_create(uint_3_type); | |
339 | struct bt_ctf_field_type *variant_type = | |
340 | bt_ctf_field_type_variant_create(enum_variant_type, | |
341 | "variant_selector"); | |
342 | struct bt_ctf_field_type *string_type = | |
343 | bt_ctf_field_type_string_create(); | |
344 | struct bt_ctf_field_type *sequence_type; | |
345 | struct bt_ctf_field_type *inner_structure_type = | |
346 | bt_ctf_field_type_structure_create(); | |
347 | struct bt_ctf_field_type *complex_structure_type = | |
348 | bt_ctf_field_type_structure_create(); | |
349 | struct bt_ctf_event_class *event_class; | |
350 | struct bt_ctf_event *event; | |
351 | struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field, | |
352 | *inner_structure_field, *complex_structure_field, | |
353 | *a_sequence_field, *enum_variant_field, *enum_container_field, | |
354 | *variant_field; | |
355 | ||
356 | bt_ctf_field_type_set_alignment(int_16_type, 32); | |
357 | bt_ctf_field_type_integer_set_signed(int_16_type, 1); | |
358 | bt_ctf_field_type_integer_set_base(uint_35_type, | |
359 | BT_CTF_INTEGER_BASE_HEXADECIMAL); | |
360 | ||
361 | sequence_type = bt_ctf_field_type_sequence_create(int_16_type, | |
362 | "seq_len"); | |
363 | bt_ctf_field_type_structure_add_field(inner_structure_type, | |
364 | uint_35_type, "seq_len"); | |
365 | bt_ctf_field_type_structure_add_field(inner_structure_type, | |
366 | sequence_type, "a_sequence"); | |
367 | ||
368 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
369 | "UINT3_TYPE", 0, 0); | |
370 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
371 | "INT16_TYPE", 1, 1); | |
372 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
373 | "UINT35_TYPE", 2, 7); | |
374 | ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type, | |
375 | "An unknown entry"), "Reject a variant field based on an unknown tag value"); | |
376 | ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type, | |
377 | "UINT3_TYPE") == 0, "Add a field to a variant"); | |
378 | bt_ctf_field_type_variant_add_field(variant_type, int_16_type, | |
379 | "INT16_TYPE"); | |
380 | bt_ctf_field_type_variant_add_field(variant_type, uint_35_type, | |
381 | "UINT35_TYPE"); | |
382 | ||
383 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
384 | enum_variant_type, "variant_selector"); | |
385 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
386 | string_type, "a_string"); | |
387 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
388 | variant_type, "variant_value"); | |
389 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
390 | inner_structure_type, "inner_structure"); | |
391 | ||
392 | ok(bt_ctf_event_class_create("clock") == NULL, | |
393 | "Reject creation of an event class with an illegal name"); | |
394 | event_class = bt_ctf_event_class_create("Complex Test Event"); | |
395 | ok(event_class, "Create an event class"); | |
396 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""), | |
397 | "Reject addition of a field with an empty name to an event"); | |
398 | ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"), | |
399 | "Reject addition of a field with a NULL type to an event"); | |
400 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, | |
401 | "int"), | |
402 | "Reject addition of a type with an illegal name to an event"); | |
403 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, | |
404 | "uint_35") == 0, | |
405 | "Add field of type unsigned integer to an event"); | |
406 | ok(bt_ctf_event_class_add_field(event_class, int_16_type, | |
407 | "int_16") == 0, "Add field of type signed integer to an event"); | |
408 | ok(bt_ctf_event_class_add_field(event_class, complex_structure_type, | |
409 | "complex_structure") == 0, | |
410 | "Add composite structure to an event"); | |
411 | ||
412 | /* Add event class to the stream class */ | |
413 | ok(bt_ctf_stream_class_add_event_class(stream_class, NULL), | |
414 | "Reject addition of NULL event class to a stream class"); | |
415 | ok(bt_ctf_stream_class_add_event_class(stream_class, | |
416 | event_class) == 0, "Add an event class to stream class"); | |
417 | ||
418 | event = bt_ctf_event_create(event_class); | |
419 | ok(event, "Instanciate a complex event"); | |
420 | ||
421 | uint_35_field = bt_ctf_event_get_payload(event, "uint_35"); | |
422 | ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance "); | |
423 | bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D); | |
424 | bt_ctf_field_put(uint_35_field); | |
425 | ||
426 | int_16_field = bt_ctf_event_get_payload(event, "int_16"); | |
427 | bt_ctf_field_signed_integer_set_value(int_16_field, -12345); | |
428 | bt_ctf_field_put(int_16_field); | |
429 | ||
430 | complex_structure_field = bt_ctf_event_get_payload(event, | |
431 | "complex_structure"); | |
432 | inner_structure_field = bt_ctf_field_structure_get_field( | |
433 | complex_structure_field, "inner_structure"); | |
434 | a_string_field = bt_ctf_field_structure_get_field( | |
435 | complex_structure_field, "a_string"); | |
436 | enum_variant_field = bt_ctf_field_structure_get_field( | |
437 | complex_structure_field, "variant_selector"); | |
438 | variant_field = bt_ctf_field_structure_get_field( | |
439 | complex_structure_field, "variant_value"); | |
440 | uint_35_field = bt_ctf_field_structure_get_field( | |
441 | inner_structure_field, "seq_len"); | |
442 | a_sequence_field = bt_ctf_field_structure_get_field( | |
443 | inner_structure_field, "a_sequence"); | |
444 | ||
445 | enum_container_field = bt_ctf_field_enumeration_get_container( | |
446 | enum_variant_field); | |
447 | bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1); | |
448 | int_16_field = bt_ctf_field_variant_get_field(variant_field, | |
449 | enum_variant_field); | |
450 | bt_ctf_field_signed_integer_set_value(int_16_field, -200); | |
451 | bt_ctf_field_put(int_16_field); | |
452 | bt_ctf_field_string_set_value(a_string_field, | |
453 | "Test string"); | |
454 | bt_ctf_field_unsigned_integer_set_value(uint_35_field, | |
455 | SEQUENCE_TEST_LENGTH); | |
456 | ok(bt_ctf_field_sequence_set_length(a_sequence_field, | |
457 | uint_35_field) == 0, "Set a sequence field's length"); | |
458 | ||
459 | for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) { | |
460 | int_16_field = bt_ctf_field_sequence_get_field( | |
461 | a_sequence_field, i); | |
462 | bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i); | |
463 | bt_ctf_field_put(int_16_field); | |
464 | } | |
465 | ||
466 | bt_ctf_clock_set_time(clock, ++current_time); | |
467 | ok(bt_ctf_stream_append_event(stream, event) == 0, | |
468 | "Append a complex event to a stream"); | |
469 | ok(bt_ctf_stream_flush(stream) == 0, | |
470 | "Flush a stream containing a complex event"); | |
471 | ||
472 | bt_ctf_field_put(uint_35_field); | |
473 | bt_ctf_field_put(a_string_field); | |
474 | bt_ctf_field_put(inner_structure_field); | |
475 | bt_ctf_field_put(complex_structure_field); | |
476 | bt_ctf_field_put(a_sequence_field); | |
477 | bt_ctf_field_put(enum_variant_field); | |
478 | bt_ctf_field_put(enum_container_field); | |
479 | bt_ctf_field_put(variant_field); | |
480 | bt_ctf_field_type_put(uint_35_type); | |
481 | bt_ctf_field_type_put(int_16_type); | |
482 | bt_ctf_field_type_put(string_type); | |
483 | bt_ctf_field_type_put(sequence_type); | |
484 | bt_ctf_field_type_put(inner_structure_type); | |
485 | bt_ctf_field_type_put(complex_structure_type); | |
486 | bt_ctf_field_type_put(uint_3_type); | |
487 | bt_ctf_field_type_put(enum_variant_type); | |
488 | bt_ctf_field_type_put(variant_type); | |
489 | bt_ctf_event_class_put(event_class); | |
490 | bt_ctf_event_put(event); | |
491 | } | |
492 | ||
493 | void type_field_tests() | |
494 | { | |
495 | struct bt_ctf_field *uint_12; | |
496 | struct bt_ctf_field *int_16; | |
497 | struct bt_ctf_field *string; | |
0abce37e | 498 | struct bt_ctf_field *enumeration; |
39d74371 JG |
499 | struct bt_ctf_field_type *composite_structure_type; |
500 | struct bt_ctf_field_type *structure_seq_type; | |
501 | struct bt_ctf_field_type *string_type; | |
502 | struct bt_ctf_field_type *sequence_type; | |
503 | struct bt_ctf_field_type *uint_8_type; | |
504 | struct bt_ctf_field_type *int_16_type; | |
505 | struct bt_ctf_field_type *uint_12_type = | |
506 | bt_ctf_field_type_integer_create(12); | |
0abce37e JG |
507 | struct bt_ctf_field_type *enumeration_type; |
508 | struct bt_ctf_field_type *enumeration_sequence_type; | |
509 | struct bt_ctf_field_type *enumeration_array_type; | |
39d74371 JG |
510 | |
511 | ok(uint_12_type, "Create an unsigned integer type"); | |
512 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
513 | BT_CTF_INTEGER_BASE_BINARY) == 0, | |
514 | "Set integer type's base as binary"); | |
515 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
516 | BT_CTF_INTEGER_BASE_DECIMAL) == 0, | |
517 | "Set integer type's base as decimal"); | |
518 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
519 | BT_CTF_INTEGER_BASE_UNKNOWN), | |
520 | "Reject integer type's base set as unknown"); | |
521 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
522 | BT_CTF_INTEGER_BASE_OCTAL) == 0, | |
523 | "Set integer type's base as octal"); | |
524 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
525 | BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0, | |
526 | "Set integer type's base as hexadecimal"); | |
527 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417), | |
528 | "Reject unknown integer base value"); | |
529 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0, | |
530 | "Set integer type signedness to signed"); | |
531 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0, | |
532 | "Set integer type signedness to unsigned"); | |
533 | ||
534 | int_16_type = bt_ctf_field_type_integer_create(16); | |
535 | bt_ctf_field_type_integer_set_signed(int_16_type, 1); | |
536 | uint_8_type = bt_ctf_field_type_integer_create(8); | |
537 | sequence_type = | |
538 | bt_ctf_field_type_sequence_create(int_16_type, "seq_len"); | |
539 | ok(sequence_type, "Create a sequence of int16_t type"); | |
540 | ||
541 | string_type = bt_ctf_field_type_string_create(); | |
542 | ok(string_type, "Create a string type"); | |
543 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
544 | CTF_STRING_NONE), | |
545 | "Reject invalid \"None\" string encoding"); | |
546 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
547 | 42), | |
548 | "Reject invalid string encoding"); | |
549 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
550 | CTF_STRING_ASCII) == 0, | |
551 | "Set string encoding to ASCII"); | |
552 | ||
553 | structure_seq_type = bt_ctf_field_type_structure_create(); | |
554 | ok(structure_seq_type, "Create a structure type"); | |
555 | ok(bt_ctf_field_type_structure_add_field(structure_seq_type, | |
556 | uint_8_type, "seq_len") == 0, | |
557 | "Add a uint8_t type to a structure"); | |
558 | ok(bt_ctf_field_type_structure_add_field(structure_seq_type, | |
559 | sequence_type, "a_sequence") == 0, | |
560 | "Add a sequence type to a structure"); | |
561 | composite_structure_type = bt_ctf_field_type_structure_create(); | |
562 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
563 | string_type, "a_string") == 0, | |
564 | "Add a string type to a structure"); | |
565 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
566 | structure_seq_type, "inner_structure") == 0, | |
567 | "Add a structure type to a structure"); | |
568 | ||
569 | int_16 = bt_ctf_field_create(int_16_type); | |
570 | ok(int_16, "Instanciate a signed 16-bit integer"); | |
571 | uint_12 = bt_ctf_field_create(uint_12_type); | |
572 | ok(uint_12, "Instanciate an unsigned 12-bit integer"); | |
573 | ||
574 | /* Can't modify types after instanciating them */ | |
575 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
576 | BT_CTF_INTEGER_BASE_DECIMAL), | |
577 | "Check an integer type' base can't be modified after instanciation"); | |
578 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0), | |
579 | "Check an integer type's signedness can't be modified after instanciation"); | |
580 | ||
581 | /* Check signed property is checked */ | |
582 | ok(bt_ctf_field_signed_integer_set_value(uint_12, -52), | |
583 | "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer"); | |
584 | ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42), | |
585 | "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer"); | |
586 | ||
587 | /* Check overflows are properly tested for */ | |
588 | ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0, | |
589 | "Check -32768 is allowed for a signed 16-bit integer"); | |
590 | ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0, | |
591 | "Check 32767 is allowed for a signed 16-bit integer"); | |
592 | ok(bt_ctf_field_signed_integer_set_value(int_16, 32768), | |
593 | "Check 32768 is not allowed for a signed 16-bit integer"); | |
594 | ok(bt_ctf_field_signed_integer_set_value(int_16, -32769), | |
595 | "Check -32769 is not allowed for a signed 16-bit integer"); | |
596 | ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0, | |
597 | "Check -42 is allowed for a signed 16-bit integer"); | |
598 | ||
599 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0, | |
600 | "Check 4095 is allowed for an unsigned 12-bit integer"); | |
601 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096), | |
602 | "Check 4096 is not allowed for a unsigned 12-bit integer"); | |
603 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0, | |
604 | "Check 0 is allowed for an unsigned 12-bit integer"); | |
605 | ||
606 | string = bt_ctf_field_create(string_type); | |
607 | ok(string, "Instanciate a string field"); | |
608 | ok(bt_ctf_field_string_set_value(string, "A value") == 0, | |
609 | "Set a string's value"); | |
610 | ||
0abce37e JG |
611 | enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type); |
612 | ok(enumeration_type, | |
613 | "Create an enumeration type with an unsigned 12-bit integer as container"); | |
614 | enumeration_sequence_type = bt_ctf_field_type_sequence_create( | |
615 | enumeration_type, "count"); | |
616 | ok(!enumeration_sequence_type, | |
617 | "Check enumeration types are validated when creating a sequence"); | |
618 | enumeration_array_type = bt_ctf_field_type_array_create( | |
619 | enumeration_type, 10); | |
620 | ok(!enumeration_array_type, | |
621 | "Check enumeration types are validated when creating an array"); | |
622 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
35628ac7 | 623 | enumeration_type, "enumeration"), |
0abce37e JG |
624 | "Check enumeration types are validated when adding them as structure members"); |
625 | enumeration = bt_ctf_field_create(enumeration_type); | |
626 | ok(!enumeration, | |
627 | "Check enumeration types are validated before instantiation"); | |
628 | ||
39d74371 JG |
629 | bt_ctf_field_put(string); |
630 | bt_ctf_field_put(uint_12); | |
631 | bt_ctf_field_put(int_16); | |
0abce37e | 632 | bt_ctf_field_put(enumeration); |
39d74371 JG |
633 | bt_ctf_field_type_put(composite_structure_type); |
634 | bt_ctf_field_type_put(structure_seq_type); | |
635 | bt_ctf_field_type_put(string_type); | |
636 | bt_ctf_field_type_put(sequence_type); | |
637 | bt_ctf_field_type_put(uint_8_type); | |
638 | bt_ctf_field_type_put(int_16_type); | |
639 | bt_ctf_field_type_put(uint_12_type); | |
0abce37e JG |
640 | bt_ctf_field_type_put(enumeration_type); |
641 | bt_ctf_field_type_put(enumeration_sequence_type); | |
642 | bt_ctf_field_type_put(enumeration_array_type); | |
39d74371 JG |
643 | } |
644 | ||
645 | void packet_resize_test(struct bt_ctf_stream_class *stream_class, | |
646 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
647 | { | |
648 | /* | |
649 | * Append enough events to force the underlying packet to be resized. | |
650 | * Also tests that a new event can be declared after a stream has been | |
651 | * instantiated and used/flushed. | |
652 | */ | |
653 | int ret = 0; | |
654 | int i; | |
655 | struct bt_ctf_event_class *event_class = bt_ctf_event_class_create( | |
656 | "Spammy_Event"); | |
657 | struct bt_ctf_field_type *integer_type = | |
658 | bt_ctf_field_type_integer_create(17); | |
659 | struct bt_ctf_field_type *string_type = | |
660 | bt_ctf_field_type_string_create(); | |
661 | ||
662 | ret |= bt_ctf_event_class_add_field(event_class, integer_type, | |
663 | "field_1"); | |
664 | ret |= bt_ctf_event_class_add_field(event_class, string_type, | |
665 | "a_string"); | |
666 | ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class); | |
667 | ok(ret == 0, "Add a new event class to a stream class after writing an event"); | |
668 | if (ret) { | |
669 | goto end; | |
670 | } | |
671 | ||
672 | for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) { | |
673 | struct bt_ctf_event *event = bt_ctf_event_create(event_class); | |
674 | struct bt_ctf_field *integer = | |
675 | bt_ctf_field_create(integer_type); | |
676 | struct bt_ctf_field *string = | |
677 | bt_ctf_field_create(string_type); | |
678 | ||
679 | ret |= bt_ctf_clock_set_time(clock, ++current_time); | |
680 | ret |= bt_ctf_field_unsigned_integer_set_value(integer, i); | |
681 | ret |= bt_ctf_event_set_payload(event, "field_1", | |
682 | integer); | |
683 | bt_ctf_field_put(integer); | |
684 | ret |= bt_ctf_field_string_set_value(string, "This is a test"); | |
685 | ret |= bt_ctf_event_set_payload(event, "a_string", | |
686 | string); | |
687 | bt_ctf_field_put(string); | |
688 | ret |= bt_ctf_stream_append_event(stream, event); | |
689 | bt_ctf_event_put(event); | |
690 | ||
691 | if (ret) { | |
692 | break; | |
693 | } | |
694 | } | |
695 | end: | |
696 | ok(ret == 0, "Append 100 000 events to a stream"); | |
697 | ok(bt_ctf_stream_flush(stream) == 0, | |
698 | "Flush a stream that forces a packet resize"); | |
699 | bt_ctf_field_type_put(integer_type); | |
700 | bt_ctf_field_type_put(string_type); | |
701 | bt_ctf_event_class_put(event_class); | |
702 | } | |
703 | ||
704 | int main(int argc, char **argv) | |
705 | { | |
706 | char trace_path[] = "/tmp/ctfwriter_XXXXXX"; | |
707 | char metadata_path[sizeof(trace_path) + 9]; | |
708 | const char *clock_name = "test_clock"; | |
709 | const char *clock_description = "This is a test clock"; | |
710 | const uint64_t frequency = 1000000000; | |
711 | const uint64_t offset_s = 1351530929945824323; | |
712 | const uint64_t offset = 1234567; | |
713 | const uint64_t precision = 10; | |
714 | char *metadata_string; | |
715 | struct bt_ctf_writer *writer; | |
716 | struct utsname name; | |
0954670a | 717 | char hostname[BABELTRACE_HOST_NAME_MAX]; |
39d74371 JG |
718 | struct bt_ctf_clock *clock; |
719 | struct bt_ctf_stream_class *stream_class; | |
720 | struct bt_ctf_stream *stream1; | |
0954670a | 721 | int ret; |
39d74371 JG |
722 | |
723 | if (argc < 3) { | |
724 | printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n"); | |
725 | exit(-1); | |
726 | } | |
727 | ||
728 | plan_no_plan(); | |
729 | ||
559455a9 | 730 | if (!bt_mkdtemp(trace_path)) { |
39d74371 JG |
731 | perror("# perror"); |
732 | } | |
733 | ||
734 | strcpy(metadata_path, trace_path); | |
735 | strcat(metadata_path + sizeof(trace_path) - 1, "/metadata"); | |
736 | ||
737 | writer = bt_ctf_writer_create(trace_path); | |
738 | ok(writer, "bt_ctf_create succeeds in creating trace with path"); | |
739 | ||
740 | /* Add environment context to the trace */ | |
0954670a JG |
741 | ret = gethostname(hostname, sizeof(hostname)); |
742 | if (ret < 0) { | |
743 | return ret; | |
744 | } | |
39d74371 JG |
745 | ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0, |
746 | "Add host (%s) environment field to writer instance", | |
747 | hostname); | |
748 | ok(bt_ctf_writer_add_environment_field(NULL, "test_field", | |
749 | "test_value"), | |
750 | "bt_ctf_writer_add_environment_field error with NULL writer"); | |
751 | ok(bt_ctf_writer_add_environment_field(writer, NULL, | |
752 | "test_value"), | |
753 | "bt_ctf_writer_add_environment_field error with NULL field name"); | |
754 | ok(bt_ctf_writer_add_environment_field(writer, "test_field", | |
755 | NULL), | |
756 | "bt_ctf_writer_add_environment_field error with NULL field value"); | |
757 | ||
68479831 MJ |
758 | /* On Solaris, uname() can return any positive value on success */ |
759 | if (uname(&name) < 0) { | |
39d74371 JG |
760 | perror("uname"); |
761 | return -1; | |
762 | } | |
763 | ||
764 | ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname) | |
765 | == 0, "Add sysname (%s) environment field to writer instance", | |
766 | name.sysname); | |
767 | ok(bt_ctf_writer_add_environment_field(writer, "nodename", | |
768 | name.nodename) == 0, | |
769 | "Add nodename (%s) environment field to writer instance", | |
770 | name.nodename); | |
771 | ok(bt_ctf_writer_add_environment_field(writer, "release", name.release) | |
772 | == 0, "Add release (%s) environment field to writer instance", | |
773 | name.release); | |
774 | ok(bt_ctf_writer_add_environment_field(writer, "version", name.version) | |
775 | == 0, "Add version (%s) environment field to writer instance", | |
776 | name.version); | |
777 | ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine) | |
778 | == 0, "Add machine (%s) environment field to writer istance", | |
779 | name.machine); | |
780 | ||
781 | /* Define a clock and add it to the trace */ | |
782 | ok(bt_ctf_clock_create("signed") == NULL, "Illegal clock name rejected"); | |
783 | ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected"); | |
784 | clock = bt_ctf_clock_create(clock_name); | |
785 | ok(clock, "Clock created sucessfully"); | |
786 | bt_ctf_clock_set_description(clock, clock_description); | |
787 | ||
788 | ok(bt_ctf_clock_set_frequency(clock, frequency) == 0, | |
789 | "Set clock frequency"); | |
790 | ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0, | |
791 | "Set clock offset (seconds)"); | |
792 | ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset"); | |
793 | ok(bt_ctf_clock_set_precision(clock, precision) == 0, | |
794 | "Set clock precision"); | |
795 | ok(bt_ctf_clock_set_is_absolute(clock, 0xFF) == 0, | |
796 | "Set clock absolute property"); | |
797 | ||
798 | ok(bt_ctf_writer_add_clock(writer, clock) == 0, | |
799 | "Add clock to writer instance"); | |
800 | ok(bt_ctf_writer_add_clock(writer, clock), | |
801 | "Verify a clock can't be added twice to a writer instance"); | |
802 | ||
803 | /* Define a stream class */ | |
804 | stream_class = bt_ctf_stream_class_create("test_stream"); | |
805 | ok(stream_class, "Create stream class"); | |
806 | ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0, | |
807 | "Set a stream class' clock"); | |
808 | ||
809 | /* Test the event fields and event types APIs */ | |
810 | type_field_tests(); | |
811 | ||
812 | /* Instantiate a stream and append events */ | |
813 | stream1 = bt_ctf_writer_create_stream(writer, stream_class); | |
814 | ok(stream1, "Instanciate a stream class from writer"); | |
815 | ||
816 | /* Should fail after instanciating a stream (locked)*/ | |
817 | ok(bt_ctf_stream_class_set_clock(stream_class, clock), | |
818 | "Changes to a stream class that was already instantiated fail"); | |
819 | ||
820 | append_simple_event(stream_class, stream1, clock); | |
821 | ||
822 | packet_resize_test(stream_class, stream1, clock); | |
823 | ||
824 | append_complex_event(stream_class, stream1, clock); | |
825 | ||
826 | metadata_string = bt_ctf_writer_get_metadata_string(writer); | |
827 | ok(metadata_string, "Get metadata string"); | |
828 | ||
829 | bt_ctf_writer_flush_metadata(writer); | |
830 | validate_metadata(argv[1], metadata_path); | |
831 | validate_trace(argv[2], trace_path); | |
832 | ||
833 | bt_ctf_clock_put(clock); | |
834 | bt_ctf_stream_class_put(stream_class); | |
835 | bt_ctf_writer_put(writer); | |
836 | bt_ctf_stream_put(stream1); | |
837 | free(metadata_string); | |
838 | ||
839 | /* Remove all trace files and delete temporary trace directory */ | |
840 | DIR *trace_dir = opendir(trace_path); | |
841 | if (!trace_dir) { | |
842 | perror("# opendir"); | |
843 | return -1; | |
844 | } | |
845 | ||
846 | struct dirent *entry; | |
847 | while ((entry = readdir(trace_dir))) { | |
848 | if (entry->d_type == DT_REG) { | |
eafa875f | 849 | unlinkat(bt_dirfd(trace_dir), entry->d_name, 0); |
39d74371 JG |
850 | } |
851 | } | |
852 | ||
853 | rmdir(trace_path); | |
854 | closedir(trace_dir); | |
855 | ||
856 | return 0; | |
857 | } |