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